-
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
Open
fatih-acar
wants to merge
1
commit into
stable
Choose a base branch
from
fac-ssl-context
base: stable
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
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
| 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,25 @@ def default_infrahub_branch(self) -> str: | |
| def password_authentication(self) -> bool: | ||
| return bool(self.username) | ||
|
|
||
| @property | ||
| def tls_context(self) -> bool | ssl.SSLContext: | ||
| if self._ssl_context: | ||
| return self._ssl_context | ||
|
|
||
| if self.tls_insecure: | ||
| return False | ||
|
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. I thought you mentioned that there was some processing needed even if we just used HTTP to create the context each time? Is it that this doesn't happen if we use HTTP together with tls_insecure=True? |
||
|
|
||
| if self.tls_ca_file: | ||
| self._ssl_context = ssl.create_default_context(cafile=self.tls_ca_file) | ||
|
|
||
| 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 +195,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 | ||
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,9 @@ | ||
| -----BEGIN CERTIFICATE----- | ||
| MIIBQDCB86ADAgECAhR6y429KiST51bZy+t330M7dN5SbzAFBgMrZXAwFjEUMBIG | ||
| A1UEAwwLZXhhbXBsZS5jb20wHhcNMjUxMDE1MTE0MjUwWhcNMzUxMDEzMTE0MjUw | ||
| WjAWMRQwEgYDVQQDDAtleGFtcGxlLmNvbTAqMAUGAytlcAMhAPIl8y8AXSWF33vX | ||
| JT2YwhMJzarOuSdPif01Gxr3Rr6Lo1MwUTAdBgNVHQ4EFgQU4heN1ZhyXpOujgcJ | ||
| WZ4LQk2m7RAwHwYDVR0jBBgwFoAU4heN1ZhyXpOujgcJWZ4LQk2m7RAwDwYDVR0T | ||
| AQH/BAUwAwEB/zAFBgMrZXADQQBoEf+8R+KWwGdaoeqinWOvrqbVZatMis0eUMvA | ||
| o+vABSPU7LIYGxLT6fpUwFSTvempzNqGZMVJ9UvVH+hYDU4D | ||
| -----END CERTIFICATE----- |
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,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----- |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if this was cached by the config during the creation of the config object and we populated
config.__ssl_contextat that time. However if we do that I'm not sure that it it would be possible to use the.set_ssl_context()method within the Config.clone, or rather it would be possible but at that time we'd already have created a new SSL Context within the clone method. So I think this might be the best option for now. I guess the only thing to remember is that we'd need to initialize a Client with a config in order to populate the config for reuse elsewhere but I think we always do that.In order to ensure this stays in place I think we'll want an additional unittest below where you also clone the config and verify that the context is the same object in both of the configs.