-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c652d8
commit 1c9f484
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 @@ | ||
import logging | ||
|
||
from click import argument, command | ||
from hvac import Client | ||
from Babylon.utils.clients import pass_hvac_client | ||
from Babylon.utils.decorators import injectcontext | ||
from Babylon.utils.environment import Environment | ||
from Babylon.utils.response import CommandResponse | ||
|
||
logger = logging.getLogger("Babylon") | ||
env = Environment() | ||
|
||
|
||
@command(help="KEY_VALUE: The key-value pair to add or update, in the format key=value") | ||
@injectcontext() | ||
@pass_hvac_client | ||
@argument("organization_id", type=str) | ||
@argument("workspace_key", type=str) | ||
@argument("cluster_name", type=str) | ||
@argument("key_value", type=str) | ||
def set_workspace_secrets(hvac_client: Client, organization_id: str, workspace_key: str, cluster_name: str, | ||
key_value: str) -> CommandResponse: | ||
""" | ||
Set a secret in workspaces scope | ||
""" | ||
org_tenant = f"{env.organization_name}/{env.tenant_id}" | ||
secret_path = f"{org_tenant}/clusters/{cluster_name}/{env.environ_id}/workspaces/{organization_id}-{workspace_key}" | ||
key, value = key_value.split('=', 1) | ||
existing_secrets = hvac_client.read(path=secret_path) | ||
if existing_secrets: | ||
secrets = existing_secrets['data'] | ||
secrets[key] = value | ||
else: | ||
secrets = {f"{key}": value} | ||
|
||
hvac_client.write(path=secret_path, **secrets) | ||
logger.info("[vault] successfully add workspace secret") | ||
return CommandResponse.success() |