-
Notifications
You must be signed in to change notification settings - Fork 6
feat(client): add ssl context cache and config #581
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,10 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| import ssl | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from copy import deepcopy | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Any | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| from pydantic import Field, field_validator, model_validator | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from pydantic import Field, PrivateAttr, field_validator, model_validator | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from pydantic_settings import BaseSettings, SettingsConfigDict | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing_extensions import Self | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -78,6 +79,7 @@ class ConfigBase(BaseSettings): | |||||||||||||||||||||||||||||||||||||||||||||||||
| Can be useful to test with self-signed certificates.""", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| tls_ca_file: str | None = Field(default=None, description="File path to CA cert or bundle in PEM format") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| _ssl_context: ssl.SSLContext | None = PrivateAttr(default=None) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| @model_validator(mode="before") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -133,6 +135,28 @@ def default_infrahub_branch(self) -> str: | |||||||||||||||||||||||||||||||||||||||||||||||||
| def password_authentication(self) -> bool: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return bool(self.username) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def tls_context(self) -> ssl.SSLContext: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if self._ssl_context: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._ssl_context | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if self.tls_insecure: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self._ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self._ssl_context.check_hostname = False | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self._ssl_context.verify_mode = ssl.CERT_NONE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._ssl_context | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if self.tls_ca_file: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self._ssl_context = ssl.create_default_context(cafile=self.tls_ca_file) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+143
to
+150
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. Validate mutual exclusivity of tls_insecure and tls_ca_file. When Consider adding a model validator to enforce mutual exclusivity: + @model_validator(mode="after")
+ def validate_tls_config(self) -> Self:
+ if self.tls_insecure and self.tls_ca_file:
+ raise ValueError("'tls_insecure' and 'tls_ca_file' are mutually exclusive")
+ return self
+
@property
def tls_context(self) -> ssl.SSLContext:📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Comment on lines
+149
to
+150
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. Validate tls_ca_file exists before passing to SSL context. Line 150 passes Add a field validator to check file existence: + @field_validator("tls_ca_file")
+ @classmethod
+ def validate_tls_ca_file(cls, value: str | None) -> str | None:
+ if value is not None:
+ from pathlib import Path
+ if not Path(value).is_file():
+ raise ValueError(f"TLS CA file not found: {value}")
+ return value
+
@property
def tls_context(self) -> ssl.SSLContext:🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if self._ssl_context is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self._ssl_context = ssl.create_default_context() | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._ssl_context | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def set_ssl_context(self, context: ssl.SSLContext) -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self._ssl_context = context | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| class Config(ConfigBase): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| recorder: RecorderType = Field(default=RecorderType.NONE, description="Select builtin recorder for later replay.") | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -174,4 +198,7 @@ def clone(self, branch: str | None = None) -> Config: | |||||||||||||||||||||||||||||||||||||||||||||||||
| if field not in covered_keys: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| config[field] = deepcopy(getattr(self, field)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| return Config(**config) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| new_config = Config(**config) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if self._ssl_context: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| new_config.set_ssl_context(self._ssl_context) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return new_config | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| -----BEGIN CERTIFICATE----- | ||
| MIIBQDCB86ADAgECAhR6y429KiST51bZy+t330M7dN5SbzAFBgMrZXAwFjEUMBIG | ||
| A1UEAwwLZXhhbXBsZS5jb20wHhcNMjUxMDE1MTE0MjUwWhcNMzUxMDEzMTE0MjUw | ||
| WjAWMRQwEgYDVQQDDAtleGFtcGxlLmNvbTAqMAUGAytlcAMhAPIl8y8AXSWF33vX | ||
| JT2YwhMJzarOuSdPif01Gxr3Rr6Lo1MwUTAdBgNVHQ4EFgQU4heN1ZhyXpOujgcJ | ||
| WZ4LQk2m7RAwHwYDVR0jBBgwFoAU4heN1ZhyXpOujgcJWZ4LQk2m7RAwDwYDVR0T | ||
| AQH/BAUwAwEB/zAFBgMrZXADQQBoEf+8R+KWwGdaoeqinWOvrqbVZatMis0eUMvA | ||
| o+vABSPU7LIYGxLT6fpUwFSTvempzNqGZMVJ9UvVH+hYDU4D | ||
| -----END CERTIFICATE----- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| -----BEGIN CERTIFICATE----- | ||
| MIIBQDCB86ADAgECAhQTRmRZxUSA5L7VfYJb3/t+dRK0ETAFBgMrZXAwFjEUMBIG | ||
| A1UEAwwLZXhhbXBsZS5jb20wHhcNMjUxMDE1MTE0MzM0WhcNMzUxMDEzMTE0MzM0 | ||
| WjAWMRQwEgYDVQQDDAtleGFtcGxlLmNvbTAqMAUGAytlcAMhAK1O3ZhE5qzfT7Qx | ||
| +0My3ToDVDi5wwpllkKn0X50zXFao1MwUTAdBgNVHQ4EFgQUH+qBMU+h4t1vdLbO | ||
| jMSSgXdURewwHwYDVR0jBBgwFoAUH+qBMU+h4t1vdLbOjMSSgXdURewwDwYDVR0T | ||
| AQH/BAUwAwEB/zAFBgMrZXADQQB3Z03f3gQcktxk4h/v8pVi5soz8viPx17TSPXf | ||
| 1WYG+Jlk4C5GQ+tyjZgZUE9LL2BFRYBv28V/NPT/0TjPGtcC | ||
| -----END CERTIFICATE----- |
Uh oh!
There was an error while loading. Please reload this page.