Repository navigation
feat: support mtls for query api #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NguyenHoangSon96
wants to merge
5
commits into
main
Choose a base branch
from
feat/support-mtls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cc54831
feat: support mtls for query api
NguyenHoangSon96 136b1ca
docs: add examples for config mtls
NguyenHoangSon96 a8e5cfc
docs: add examples for config mtls
NguyenHoangSon96 fc89be8
feat: support mtls for query api
NguyenHoangSon96 4b7b65c
docs: add examples for config mtls
NguyenHoangSon96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| from pyarrow.flight import FlightClient, Ticket, FlightCallOptions, FlightStreamReader | ||
|
|
||
| from influxdb_client_3.version import USER_AGENT | ||
| from influxdb_client_3.write_client.client.util.helpers import read_certs | ||
|
|
||
|
|
||
| class QueryApiOptions(object): | ||
|
|
@@ -14,7 +15,9 @@ class QueryApiOptions(object): | |
|
|
||
| Attributes | ||
| ---------- | ||
| tls_root_certs (bytes): contents of an SSL root certificate or chain read as bytes | ||
| tls_root_certs (bytes): contents of an SSL root certificate or chain read as bytes | ||
| cert_chain (bytes): client certificate if using mutual TLS | ||
| private_key (bytes): client private key for cert_chain is using mutual TLS | ||
| tls_verify (bool): whether to verify SSL certificates or not | ||
| proxy (str): URL to a proxy server | ||
| flight_client_options (dict): base set of flight client options passed to internal pyarrow.flight.FlightClient | ||
|
|
@@ -24,6 +27,9 @@ class QueryApiOptions(object): | |
| """ | ||
| _DEFAULT_TIMEOUT = 300.0 | ||
| tls_root_certs: bytes = None | ||
| cert_chain: bytes = None | ||
| private_key: bytes = None | ||
| private_key_password: str = None | ||
| tls_verify: bool = None | ||
| proxy: str = None | ||
| flight_client_options: dict = None | ||
|
|
@@ -37,11 +43,17 @@ def __init__(self, root_certs_path: str, | |
| flight_client_options: dict, | ||
| timeout: float = _DEFAULT_TIMEOUT, | ||
| disable_grpc_compression: bool = False, | ||
| middleware: list = None): | ||
| middleware: list = None, | ||
| cert_chain: str = None, | ||
| private_key: str = None, | ||
| private_key_password: str = None): | ||
| """ | ||
| Initialize a set of QueryApiOptions | ||
|
|
||
| :param root_certs_path: path to a certificate .pem file. | ||
| :param cert_chain: client certificate if using mutual TLS | ||
| :param private_key: client private key for cert_chain is using mutual TLS | ||
| :param private_key_password: password for private_key if using mutual TLS | ||
| :param verify: whether to verify SSL certificates or not. | ||
| :param proxy: URL of a proxy server, if required. | ||
| :param flight_client_options: set of flight_client_options | ||
|
|
@@ -51,20 +63,21 @@ def __init__(self, root_certs_path: str, | |
| :param middleware: list of middleware functions to be applied to Flight calls. | ||
| """ | ||
| if root_certs_path: | ||
| self.tls_root_certs = self._read_certs(root_certs_path) | ||
| self.tls_root_certs = read_certs(root_certs_path) | ||
| if cert_chain: | ||
| self.cert_chain = read_certs(cert_chain) | ||
| if private_key: | ||
| if private_key_password: | ||
| self.private_key = read_certs(private_key, private_key_password) | ||
| else: | ||
| self.private_key = read_certs(private_key) | ||
| self.tls_verify = verify | ||
| self.proxy = proxy | ||
| self.flight_client_options = flight_client_options | ||
| self.timeout = timeout | ||
| self.disable_grpc_compression = disable_grpc_compression | ||
| self.middleware = middleware | ||
|
|
||
| def _read_certs(self, path: str) -> bytes: | ||
| with open(path, "rb") as certs_file: | ||
| certs = certs_file.read() | ||
| # Normalize PEM line endings so behavior is stable across platforms. | ||
| return certs.replace(b"\r\n", b"\n").replace(b"\r", b"\n") | ||
|
|
||
|
|
||
| class QueryApiOptionsBuilder(object): | ||
| """ | ||
|
|
@@ -77,12 +90,18 @@ class QueryApiOptionsBuilder(object): | |
| options = QueryApiOptionsBuilder()\ | ||
| .proxy("http://internal.tunnel.proxy:8080") \ | ||
| .root_certs("/home/fred/.etc/ssl/alt_certs.pem") \ | ||
| .cert_chain("/home/fred/.etc/ssl/client.crt") \ | ||
| .private_key("/home/fred/.etc/ssl/client.key") \ | ||
|
Comment on lines
+93
to
+94
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why /home/fred? |
||
| .private_key_password("password") \ | ||
| .tls_verify(True) \ | ||
| .build() | ||
|
|
||
| client = QueryApi(connection, token, None, None, options) | ||
| """ | ||
| _root_certs_path: str = None | ||
| _cert_chain: str = None | ||
| _private_key: str = None | ||
| _private_key_password: str = None | ||
| _tls_verify: bool = True | ||
| _proxy: str = None | ||
| _flight_client_options: dict = None | ||
|
|
@@ -94,6 +113,18 @@ def root_certs(self, path: str): | |
| self._root_certs_path = path | ||
| return self | ||
|
|
||
| def cert_chain(self, path: str): | ||
| self._cert_chain = path | ||
| return self | ||
|
|
||
| def private_key(self, path: str): | ||
| self._private_key = path | ||
| return self | ||
|
|
||
| def private_key_password(self, password: str): | ||
| self._private_key_password = password | ||
| return self | ||
|
|
||
| def tls_verify(self, verify: bool): | ||
| self._tls_verify = verify | ||
| return self | ||
|
|
@@ -123,6 +154,9 @@ def build(self) -> QueryApiOptions: | |
| """Build a QueryApiOptions object with previously set values""" | ||
| return QueryApiOptions( | ||
| root_certs_path=self._root_certs_path, | ||
| cert_chain=self._cert_chain, | ||
| private_key=self._private_key, | ||
| private_key_password=self._private_key_password, | ||
| verify=self._tls_verify, | ||
| proxy=self._proxy, | ||
| flight_client_options=self._flight_client_options, | ||
|
|
@@ -180,8 +214,35 @@ def __init__(self, | |
| if ('generic_options' in options.flight_client_options and | ||
| 'grpc.secondary_user_agent' in dict(options.flight_client_options["generic_options"]).keys()): | ||
| self._flight_client_options['generic_options'].remove(default_user_agent) | ||
|
|
||
| if options.tls_root_certs: | ||
| self._flight_client_options["tls_root_certs"] = options.tls_root_certs | ||
| if options.cert_chain: | ||
| self._flight_client_options["cert_chain"] = options.cert_chain | ||
| if options.private_key: | ||
| self._flight_client_options["private_key"] = options.private_key | ||
| elif (options.private_key is None and options.cert_chain and | ||
| not self._flight_client_options.get("private_key")): | ||
| # Only fall back to using cert_chain as private_key if the provided PEM | ||
| # content clearly includes a private key block (common in combined PEM files). | ||
| pem = options.cert_chain | ||
| if isinstance(pem, (bytes, bytearray)): | ||
| pem_text = pem.decode("utf-8", errors="ignore") | ||
| else: | ||
| pem_text = str(pem) | ||
| has_private_key = ("BEGIN PRIVATE KEY" in pem_text or | ||
| "BEGIN RSA PRIVATE KEY" in pem_text or | ||
| "BEGIN EC PRIVATE KEY" in pem_text or | ||
| "BEGIN ENCRYPTED PRIVATE KEY" in pem_text | ||
| ) | ||
| if has_private_key: | ||
| self._flight_client_options["private_key"] = options.cert_chain | ||
| else: | ||
| raise ValueError( | ||
| "mTLS misconfiguration: 'cert_chain' was provided without a " | ||
| "'private_key'. Provide 'private_key' explicitly, or pass a " | ||
| "combined PEM that includes a private key block." | ||
| ) | ||
| if options.proxy: | ||
| self._proxy = options.proxy | ||
| if options.tls_verify is not None: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #!/bin/bash | ||
| # | ||
| # The MIT License | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the "Software"), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in | ||
| # all copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| # THE SOFTWARE. | ||
| # | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| KEY_PASSWORD="changeit" | ||
| INFLUXDB_SERVER_NAME="influxdb" | ||
| OTHER_SERVER_NAME="other-server" | ||
| CERTS_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../tests/data/certificates" && pwd)" | ||
|
|
||
| for SERVER_NAME in "$INFLUXDB_SERVER_NAME" "$OTHER_SERVER_NAME"; do | ||
| echo "### Generate server key and certificate for $SERVER_NAME..." | ||
| openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 | openssl pkcs8 -topk8 -nocrypt -out "$CERTS_DIR/${SERVER_NAME}.key" | ||
|
|
||
| openssl req -new -x509 -key "$CERTS_DIR/${SERVER_NAME}.key" -out "$CERTS_DIR/${SERVER_NAME}.crt" -days 3650 \ | ||
| -subj "/CN=localhost/O=Development/C=US" \ | ||
| -addext "subjectAltName = DNS:localhost,IP:127.0.0.1" | ||
|
|
||
| cat "$CERTS_DIR/${SERVER_NAME}.crt" "$CERTS_DIR/${SERVER_NAME}.key" > "$CERTS_DIR/${SERVER_NAME}.pem" | ||
|
|
||
| # openssl pkcs12 -export -in "$CERTS_DIR/${SERVER_NAME}.crt" -inkey "$CERTS_DIR/${SERVER_NAME}.key" \ | ||
| # -out "$CERTS_DIR/${SERVER_NAME}.p12" -name "myalias" -password "pass:$KEY_PASSWORD" | ||
| done | ||
|
|
||
| echo "### Generate client key..." | ||
| openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 | openssl pkcs8 -topk8 -nocrypt -out "$CERTS_DIR/client.key" | ||
| openssl req -new -x509 -key "$CERTS_DIR/client.key" -out "$CERTS_DIR/client.crt" -days 3650 \ | ||
| -subj "/CN=test-client/O=Development/C=US" | ||
| openssl rsa -in "$CERTS_DIR/client.key" -aes256 -passout "pass:$KEY_PASSWORD" -out "$CERTS_DIR/client-encrypted.key" | ||
|
|
||
| cat "$CERTS_DIR/client.crt" "$CERTS_DIR/client.key" > "$CERTS_DIR/client.pem" | ||
|
|
||
| #openssl pkcs12 -export -in "$CERTS_DIR/client.crt" -inkey "$CERTS_DIR/client.key" \ | ||
| # -out "$CERTS_DIR/client.p12" -name "myalias" -password "pass:$KEY_PASSWORD" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
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buJ | ||
| bgthH22wNZnzjzQxAP1WDDYTNwIxR2ekmvZvIZvmvs973xsrSW6n89r7QYR4DgWX | ||
| D7A5O5TMe467keKjtadOrDWUTd3fQmKwHjpW1XhYmBbgzU6dwMbS6u37xQnweREA | ||
| PMfUztsG6mKUg5a4FFruqNAu0cnFTRTW3Fw71PuFmJtgG6ELZ+bsep5E2eDQzWL1 | ||
| e6Su1fk822SGjqqzukx0fKFdVW6Mq0x7yz6Q1D7v0gPusVnnBViQW0h+onWJcdsF | ||
| UAPjBLRw627VvUc0+8PKBszz3yRn/ycIxWTryOzRtRFOPBrSsk0kBWHKh9kWWAgb | ||
| SqLAiaXPMif3vIsobJEUo6B7WE+Ktfu6JwPF7StKKPW6vFbs+Eo1RVVsuMaPkh/4 | ||
| 47SzMYPceq9iGta/gnjG/NaLNqbjfdvwUAjPET6khMvTlEFAk4T5KvF8EhWyuTur | ||
| fEU4s+HmBcg85x1R4k70bVK6lfZWr3RfPpsP0+v38BjFuSU9X0f7biGDQvYJTg3r | ||
| Hnf/EaZn+YmEyhoGsRt30I9/HiCiropJbczfAPQrvsrZc0y8ik7pF1kTlJT1YrSw | ||
| 67N0sLnCM53ovUlQRpbkUIIdaJcKgRGAKKC/LSqfRibiZjm+tGR4iQmnVVG+0Nhi | ||
| Fd2SRaMZ0OgVXG0K8RVfrR78GtEy2SHWDfOIsF/qpk5s1ilGYC0V3SzjbB/tV0Lf | ||
| 1ZsHBJ2V8wCz/KV7O8b4DehO3X1fmhLnmhnoO8o2oJH6huoJOY1f39iB19PIIG7/ | ||
| Qs+HBtm1ieVCG9xdnfE5zGUtvCrPaDdwWP5fpfBs/vrJMtU23Y0xEv8= | ||
| -----END ENCRYPTED PRIVATE KEY----- |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.