-
Notifications
You must be signed in to change notification settings - Fork 2
Secret manager: Passwords #68
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
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
File renamed without changes.
Empty file.
Empty file.
121 changes: 121 additions & 0 deletions
121
genesis_core/agent/universal/drivers/secret/backend/db.py
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,121 @@ | ||
| # Copyright 2025 Genesis Corporation. | ||
| # | ||
| # All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. You may obtain | ||
| # a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import secrets | ||
| import logging | ||
| import typing as tp | ||
|
|
||
| from restalchemy.dm import filters as dm_filters | ||
| from restalchemy.storage import exceptions as ra_exc | ||
| from gcl_sdk.agents.universal.dm import models | ||
| from gcl_sdk.agents.universal.clients.backend import base | ||
| from gcl_sdk.agents.universal.clients.backend import exceptions | ||
|
|
||
| from genesis_core.secret import constants as sc | ||
| from genesis_core.secret.dm import models as secret_dm | ||
| from genesis_core.agent.universal.drivers.secret.dm import models as driver_dm | ||
|
|
||
|
|
||
| LOG = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class DatabaseSecretBackendClient(base.AbstractBackendClient): | ||
| """Secret Backend client based on SQL database.""" | ||
|
|
||
| def get(self, resource: models.Resource) -> dict[str, tp.Any]: | ||
| """Get the resource value in dictionary format.""" | ||
| try: | ||
| driver_password = driver_dm.Password.objects.get_one( | ||
| filters={ | ||
| "uuid": dm_filters.EQ(resource.uuid), | ||
| }, | ||
| ) | ||
| except ra_exc.RecordNotFound: | ||
| raise exceptions.ResourceNotFound(resource=resource) | ||
|
|
||
| return driver_password.meta | ||
|
|
||
| def create(self, resource: models.Resource) -> dict[str, tp.Any]: | ||
| """Creates the resource. Returns the created resource.""" | ||
| try: | ||
| self.get(resource) | ||
| except exceptions.ResourceNotFound: | ||
| pass | ||
| else: | ||
| raise exceptions.ResourceAlreadyExists(resource=resource) | ||
|
|
||
| password = secret_dm.Password.from_ua_resource(resource) | ||
|
|
||
| # Validate structure of password model | ||
| if ( | ||
| sc.SecretMethod[password.method].is_auto | ||
| and password.value is not None | ||
| ): | ||
| raise ValueError("Cannot create auto-generated password.") | ||
|
|
||
| if ( | ||
| not sc.SecretMethod[password.method].is_auto | ||
| and password.value is None | ||
| ): | ||
| raise ValueError("Cannot create non-auto-generated password.") | ||
|
|
||
| # Generate plain password | ||
| if sc.SecretMethod[password.method].is_auto: | ||
| if password.method == sc.SecretMethod.AUTO_HEX: | ||
| plain_password = secrets.token_hex(16) | ||
| elif password.method == sc.SecretMethod.AUTO_URL_SAFE: | ||
| plain_password = secrets.token_urlsafe(16) | ||
| else: | ||
| raise ValueError("Unknown auto-generated password method.") | ||
| else: | ||
| plain_password = password.value | ||
|
|
||
| # Build password from the plain view | ||
| pass_value = password.constructor.build(plain_password) | ||
|
|
||
| # Build storagable password and save | ||
| driver_password = driver_dm.Password.from_password_resource( | ||
| resource, pass_value | ||
| ) | ||
| driver_password.save() | ||
| return driver_password.meta | ||
|
|
||
| def update(self, resource: models.Resource) -> dict[str, tp.Any]: | ||
| """Update the resource. Returns the updated resource.""" | ||
|
|
||
| # The simplest implementation. Update via recreation. | ||
| self.delete(resource) | ||
| return self.create(resource) | ||
|
|
||
| def list(self, kind: str, **kwargs) -> list[dict[str, tp.Any]]: | ||
| """Lists all resources by kind.""" | ||
| secrets = driver_dm.Password.objects.get_all() | ||
| return [s.meta for s in secrets] | ||
|
|
||
| def delete(self, resource: models.Resource) -> None: | ||
| """Delete the resource.""" | ||
| try: | ||
| self.get(resource) | ||
| except exceptions.ResourceNotFound: | ||
| raise exceptions.ResourceNotFound(resource=resource) | ||
|
|
||
| password = driver_dm.Password.objects.get_one( | ||
| filters={ | ||
| "uuid": dm_filters.EQ(resource.uuid), | ||
| } | ||
| ) | ||
| password.delete() | ||
Empty file.
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,61 @@ | ||
| # Copyright 2025 Genesis Corporation. | ||
| # | ||
| # All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. You may obtain | ||
| # a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import typing as tp | ||
|
|
||
| from restalchemy.dm import properties | ||
| from restalchemy.dm import models | ||
| from restalchemy.dm import types | ||
| from restalchemy.storage.sql import orm | ||
| from gcl_sdk.agents.universal.dm import models as ua_models | ||
|
|
||
| from genesis_core.secret import constants as sc | ||
| from genesis_core.secret.dm import models as secret_dm | ||
|
|
||
|
|
||
| class Password( | ||
| models.ModelWithUUID, | ||
| models.ModelWithTimestamp, | ||
| orm.SQLStorableMixin, | ||
| ): | ||
| __tablename__ = "storage_passwords" | ||
|
|
||
| status = properties.property( | ||
| types.Enum([s.value for s in sc.SecretStatus]), | ||
| default=sc.SecretStatus.NEW.value, | ||
| ) | ||
| value = properties.property( | ||
| types.String(min_length=1, max_length=512), | ||
| required=True, | ||
| ) | ||
| # Some additional metadata about the secret | ||
| meta = properties.property(types.Dict(), default=lambda: {}) | ||
|
|
||
| @classmethod | ||
| def from_password_resource( | ||
| cls, resource: ua_models.TargetResource, password_value: str | ||
| ) -> Password: | ||
| meta = resource.value.copy() | ||
| meta["value"] = password_value | ||
| meta["status"] = sc.SecretStatus.ACTIVE.value | ||
|
|
||
| return cls( | ||
| uuid=resource.uuid, | ||
| value=password_value, | ||
| status=sc.SecretStatus.ACTIVE.value, | ||
| meta=meta, | ||
| ) |
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,44 @@ | ||
| # Copyright 2025 Genesis Corporation. | ||
| # | ||
| # All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. You may obtain | ||
| # a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| from gcl_sdk.agents.universal.drivers import direct | ||
| from gcl_sdk.agents.universal.storage import fs | ||
|
|
||
| from genesis_core.agent.universal.drivers.secret.backend import db as db_back | ||
|
|
||
|
|
||
| LOG = logging.getLogger(__name__) | ||
|
|
||
| AGENT_WORK_DIR = "/var/lib/genesis/universal_agent/" | ||
|
|
||
|
|
||
| class PasswordCapabilityDriver(direct.DirectAgentDriver): | ||
| """Password capability driver.""" | ||
|
|
||
| def __init__(self): | ||
| storage = fs.FileAgentStorage( | ||
| AGENT_WORK_DIR, "password_cap_storage.json" | ||
| ) | ||
| client = db_back.DatabaseSecretBackendClient() | ||
|
|
||
| super().__init__(storage=storage, client=client) | ||
|
|
||
| def get_capabilities(self) -> list[str]: | ||
| """Returns a list of capabilities supported by the driver.""" | ||
| return ["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
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
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.