Base64Secret
What it is
Base64Secretis anISecretAdapterimplementation that stores multiple key/value secrets inside a single underlying secret value.- The underlying secret is a Base64-encoded
.env-style text (parsed viapython-dotenv), fetched and persisted through anotherISecretAdapter.
Public API
class Base64Secret(ISecretAdapter)__init__(secret_adapter: ISecretAdapter, base64_secret_key: str)- Wraps an existing secret adapter and uses
base64_secret_keyas the storage location for the Base64-encoded bundle.
- Wraps an existing secret adapter and uses
get(key: str, default: Any = None) -> str | Any | None- Returns the decoded value for
key, ordefaultif not present.
- Returns the decoded value for
set(key: str, value: str) -> None- Updates/creates
keyin the decoded bundle and writes the re-encoded bundle back tobase64_secret_key.
- Updates/creates
remove(key: str) -> None- Removes
keyfrom the decoded bundle (no-op if missing) and writes the updated bundle back.
- Removes
list() -> Dict[str, str | None]- Returns all decoded secrets as a dict.
Configuration/Dependencies
- Depends on:
- An underlying
ISecretAdapterinstance (secret_adapter) that implementsgetandset. python-dotenv(dotenv_values) for parsing.envcontent.- Standard library
base64for encoding/decoding.
- An underlying
Usage
from naas_abi_core.services.secret.adaptors.secondary.Base64Secret import Base64Secret
from naas_abi_core.services.secret.SecretPorts import ISecretAdapter
class MemorySecretAdapter(ISecretAdapter):
def __init__(self):
self.store = {}
def get(self, key, default=None):
return self.store.get(key, default)
def set(self, key, value):
self.store[key] = value
def remove(self, key):
self.store.pop(key, None)
def list(self):
return dict(self.store)
underlying = MemorySecretAdapter()
secrets = Base64Secret(underlying, base64_secret_key="B64_SECRETS")
secrets.set("API_KEY", "abc123")
print(secrets.get("API_KEY")) # "abc123"
print(secrets.list()) # {"API_KEY": "abc123"}
secrets.remove("API_KEY")
print(secrets.get("API_KEY", None)) # None
Caveats
- If the underlying value at
base64_secret_keyis not valid Base64 or not valid UTF-8 after decoding, decoding will raise an exception. - Values are written as
.envlines in the formKEY="value". Values are coerced to strings when decoding and when setting.