-
Notifications
You must be signed in to change notification settings - Fork 5
Enh/secrets json encoders #222
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
23d588e
add exposing of secrets
MBueschelberger bfcf30a
add settings for exposing secrets during serialization
MBueschelberger 6e54693
update secretconfig, triplestoreconfig and add pytests
MBueschelberger 29c2063
Merge branch 'master' into enh/secrets-json-encoders
MBueschelberger f690e37
add doc-strings
MBueschelberger 2502951
Update oteapi/models/secretconfig.py
MBueschelberger 66817be
Update oteapi/models/triplestoreconfig.py
MBueschelberger d69f56a
Update tests/models/test_functionconfig.py
MBueschelberger 5c9d70c
update settings-description
MBueschelberger b36b1a0
Merge branch 'enh/secrets-json-encoders' of https://github.com/EMMC-A…
MBueschelberger e226c45
run pre-commit hooks
MBueschelberger 674fe94
Merge branch 'master' into enh/secrets-json-encoders
MBueschelberger 61637db
Apply suggestions from code review
MBueschelberger 2971e60
add doc-string to Config of TriplestoreConfig, update typing-types
MBueschelberger 8a3ec95
Change SecretConfig base class & use class kwargs
CasperWA c248d99
Update tests/models/test_functionconfig.py
MBueschelberger 35438a5
Update oteapi/settings.py
MBueschelberger 5a9f31a
Update tests/models/test_triplestoreconfig.py
MBueschelberger 94c9c2e
Update tests/models/test_triplestoreconfig.py
MBueschelberger 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # secretconfig | ||
|
|
||
| ::: oteapi.models.secretconfig | ||
| options: | ||
| show_if_no_docstring: true |
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,3 @@ | ||
| # settings | ||
|
|
||
| ::: oteapi.settings |
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,20 +1,14 @@ | ||
| """Pydantic Function Configuration Data Model.""" | ||
| from typing import Optional | ||
|
|
||
| from pydantic import Field, SecretStr | ||
| from pydantic import Field | ||
|
|
||
| from oteapi.models.genericconfig import GenericConfig | ||
| from oteapi.models.secretconfig import SecretConfig | ||
|
|
||
|
|
||
| class FunctionConfig(GenericConfig): | ||
| class FunctionConfig(GenericConfig, SecretConfig): | ||
| """Function Strategy Data Configuration.""" | ||
|
|
||
| functionType: str = Field( | ||
| ..., | ||
| description=("Type of registered function strategy."), | ||
| ) | ||
|
|
||
| secret: Optional[SecretStr] = Field( | ||
| None, | ||
| description="Authorization secret given when executing a function.", | ||
| ) |
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,57 @@ | ||
| """AttrDict for specifying user credentials or secrets.""" | ||
| import json | ||
| from typing import TYPE_CHECKING, Optional | ||
|
|
||
| from pydantic import BaseModel, Field, SecretStr | ||
|
|
||
| from oteapi.settings import settings | ||
|
|
||
| if TYPE_CHECKING: # pragma: no cover | ||
| from typing import Any, Callable | ||
|
|
||
|
|
||
| def json_dumps(model: dict, default: "Callable[[Any], Any]") -> "str": | ||
| """Alternative function for dumping exposed | ||
| secrets to json when model is serialized. | ||
|
|
||
| Parameters: | ||
| model: The pydantic model to serialize. | ||
| default: A pass-through to the standard `json.dumps()`'s `default` parameter. | ||
| From the `json.dumps()` doc-string: `default(obj)` is a function that should | ||
| return a serializable version of `obj` or raise `TypeError`. | ||
| The default simply raises `TypeError`. | ||
|
|
||
| Returns: | ||
| The result of `json.dumps()` after handling possible secrets. | ||
|
|
||
| """ | ||
| return json.dumps( | ||
| { | ||
| key: ( | ||
| value.get_secret_value() | ||
| if settings.expose_secrets and isinstance(value, SecretStr) | ||
| else value | ||
| ) | ||
| for key, value in model.items() | ||
| }, | ||
| default=default, | ||
| ) | ||
|
|
||
|
|
||
| class SecretConfig(BaseModel, json_dumps=json_dumps): | ||
| """Simple model for handling secret in other config-models.""" | ||
|
|
||
| user: Optional[SecretStr] = Field(None, description="User name for authentication.") | ||
| password: Optional[SecretStr] = Field( | ||
| None, description="Password for authentication." | ||
| ) | ||
| token: Optional[SecretStr] = Field( | ||
| None, | ||
| description="An access token for providing access and meta data to an application.", | ||
| ) | ||
| client_id: Optional[SecretStr] = Field( | ||
| None, description="Client ID for an OAUTH2 client." | ||
| ) | ||
| client_secret: Optional[SecretStr] = Field( | ||
| None, description="Client secret for an OAUTH2 client." | ||
| ) |
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """BaseSettings for oteapi-core. | ||
| This `configuration/settings`-class is intended to be incorporated as a | ||
| parentclass into the configuration of an FastAPI application. | ||
| See `https://fastapi.tiangolo.com/advanced/settings/` as reference. | ||
|
|
||
| Otherwise, check `https://github.com/EMMC-ASBL/oteapi-services/blob/master/app/main.py` | ||
| for a direct example of an inclusion of the OTE api and its settings into an FastAPI instance. | ||
| """ | ||
| from pydantic import BaseSettings, Field | ||
|
|
||
|
|
||
| class OteApiCoreSettings(BaseSettings): | ||
| """Basic configuration for the oteapi-core.""" | ||
|
|
||
| expose_secrets: bool = Field( | ||
| False, | ||
| description="Whether `SecretStr` in `pydantic` should be exposed or not.\n\n" | ||
| "!!! warning\n" | ||
| " Depending on the configuration and user management of the services" | ||
| " using oteapi-core, secrets might be readable by other users when serialized!" | ||
| " This especially takes place when then models and configs are put into the cache." | ||
| " Hence be careful while using this option in production.", | ||
| ) | ||
|
|
||
| class Config: | ||
| """Pydantic config for the OteApiCoreSettings.""" | ||
|
|
||
| env_prefix = "OTEAPI_" | ||
|
|
||
|
|
||
| settings = OteApiCoreSettings() |
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,38 @@ | ||
| """Tests for `oteapi.models.functionconfig`""" | ||
|
|
||
|
|
||
| def test_functionconfig() -> None: | ||
| """Pytest for FunctionConfig, mainly for testing the included secrets.""" | ||
| import json | ||
|
|
||
| from oteapi.models.functionconfig import FunctionConfig | ||
| from oteapi.settings import settings | ||
|
|
||
| base_config = {"functionType": "foo/bar", "token": "abc"} | ||
| config_exposed = { | ||
| "user": None, | ||
| "password": None, | ||
| "token": "abc", | ||
| "client_id": None, | ||
| "client_secret": None, | ||
| "configuration": {}, | ||
| "description": "Function Strategy Data Configuration.", | ||
| "functionType": "foo/bar", | ||
| } | ||
|
|
||
| config_hidden = { | ||
| "user": None, | ||
| "password": None, | ||
| "token": "**********", | ||
| "client_id": None, | ||
| "client_secret": None, | ||
| "configuration": {}, | ||
| "description": "Function Strategy Data Configuration.", | ||
| "functionType": "foo/bar", | ||
| } | ||
|
|
||
| settings.expose_secrets = False | ||
| assert FunctionConfig(**base_config).json() == json.dumps(config_hidden) | ||
|
|
||
| settings.expose_secrets = True | ||
| assert FunctionConfig(**base_config).json() == json.dumps(config_exposed) |
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,32 @@ | ||
| """Tests for `oteapi.models.secretconfig`""" | ||
|
|
||
|
|
||
| def test_secretconfig(): | ||
| """Pytest for SecretConfig.""" | ||
| import json | ||
|
|
||
| from oteapi.models.secretconfig import SecretConfig | ||
| from oteapi.settings import settings | ||
|
|
||
| base_config = {"token": "abc"} | ||
| config_exposed = { | ||
| "user": None, | ||
| "password": None, | ||
| "token": "abc", | ||
| "client_id": None, | ||
| "client_secret": None, | ||
| } | ||
|
|
||
| config_hidden = { | ||
| "user": None, | ||
| "password": None, | ||
| "token": "**********", | ||
| "client_id": None, | ||
| "client_secret": None, | ||
| } | ||
|
|
||
| settings.expose_secrets = False | ||
| assert SecretConfig(**base_config).json() == json.dumps(config_hidden) | ||
|
|
||
| settings.expose_secrets = True | ||
| assert SecretConfig(**base_config).json() == json.dumps(config_exposed) |
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.