|
| 1 | +# |
| 2 | +# Copyright (c) 2021 Nitric Technologies Pty Ltd. |
| 3 | +# |
| 4 | +# This file is part of Nitric Python 3 SDK. |
| 5 | +# See https://github.com/nitrictech/python-sdk for further info. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | +from __future__ import annotations |
| 20 | +from dataclasses import dataclass |
| 21 | +from typing import Union |
| 22 | + |
| 23 | +from nitric.utils import new_default_channel |
| 24 | +from nitric.proto.nitric.secret.v1 import SecretServiceStub, Secret as SecretMessage, SecretVersion as VersionMessage |
| 25 | + |
| 26 | + |
| 27 | +class Secrets(object): |
| 28 | + """ |
| 29 | + Nitric secrets management client. |
| 30 | +
|
| 31 | + This client insulates application code from stack specific secrets managements services. |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self): |
| 35 | + """Construct a Nitric Storage Client.""" |
| 36 | + self._channel = new_default_channel() |
| 37 | + self._secrets_stub = SecretServiceStub(channel=self._channel) |
| 38 | + |
| 39 | + def __del__(self): |
| 40 | + # close the channel when this client is destroyed |
| 41 | + if self._channel is not None: |
| 42 | + self._channel.close() |
| 43 | + |
| 44 | + def secret(self, name: str): |
| 45 | + """Return a reference to a secret container from the connected secrets management service.""" |
| 46 | + return SecretContainer(_secrets=self, name=name) |
| 47 | + |
| 48 | + |
| 49 | +def _secret_to_wire(secret: SecretContainer) -> SecretMessage: |
| 50 | + return SecretMessage(name=secret.name) |
| 51 | + |
| 52 | + |
| 53 | +@dataclass(frozen=True) |
| 54 | +class SecretContainer(object): |
| 55 | + """A reference to a secret container, used to store and retrieve secret versions.""" |
| 56 | + |
| 57 | + _secrets: Secrets |
| 58 | + name: str |
| 59 | + |
| 60 | + async def put(self, value: Union[str, bytes]) -> SecretVersion: |
| 61 | + """ |
| 62 | + Create a new secret version, making it the latest and storing the provided value. |
| 63 | +
|
| 64 | + :param value: the secret value to store |
| 65 | + """ |
| 66 | + if isinstance(value, str): |
| 67 | + value = bytes(value, "utf-8") |
| 68 | + |
| 69 | + secret_message = _secret_to_wire(self) |
| 70 | + |
| 71 | + response = await self._secrets._secrets_stub.put(secret=secret_message, value=value) |
| 72 | + return self.version(version=response.secret_version.version) |
| 73 | + |
| 74 | + def version(self, version: str): |
| 75 | + """ |
| 76 | + Return a reference to a specific version of a secret. |
| 77 | +
|
| 78 | + Can be used to retrieve the secret value associated with the version. |
| 79 | + """ |
| 80 | + return SecretVersion(_secrets=self._secrets, secret=self, id=version) |
| 81 | + |
| 82 | + def latest(self): |
| 83 | + """ |
| 84 | + Return a reference to the 'latest' secret version. |
| 85 | +
|
| 86 | + Note: using 'access' on this reference may return different values between requests if a |
| 87 | + new version is created between access calls. |
| 88 | + """ |
| 89 | + return self.version("latest") |
| 90 | + |
| 91 | + |
| 92 | +def _secret_version_to_wire(version: SecretVersion) -> VersionMessage: |
| 93 | + return VersionMessage(_secret_to_wire(version.secret), version=version.id) |
| 94 | + |
| 95 | + |
| 96 | +@dataclass(frozen=True) |
| 97 | +class SecretVersion(object): |
| 98 | + """A reference to a version of a secret, used to access the value of the version.""" |
| 99 | + |
| 100 | + _secrets: Secrets |
| 101 | + secret: SecretContainer |
| 102 | + id: str |
| 103 | + |
| 104 | + async def access(self) -> SecretValue: |
| 105 | + """Return the value stored against this version of the secret.""" |
| 106 | + version_message = _secret_version_to_wire(self) |
| 107 | + response = await self._secrets._secrets_stub.access(secret_version=version_message) |
| 108 | + |
| 109 | + # Construct a new SecretVersion if the response version id doesn't match this reference. |
| 110 | + # This ensures calls to access from the 'latest' version return new version objects |
| 111 | + # with a fixed version id. |
| 112 | + static_version = ( |
| 113 | + self |
| 114 | + if response.secret_version.version == self.id |
| 115 | + else SecretVersion(_secrets=self._secrets, secret=self.secret, id=response.secret_version.version) |
| 116 | + ) |
| 117 | + |
| 118 | + return SecretValue(version=static_version, value=response.value) |
| 119 | + |
| 120 | + |
| 121 | +@dataclass(frozen=True) |
| 122 | +class SecretValue(object): |
| 123 | + """Represents the value of a secret, tied to a specific version.""" |
| 124 | + |
| 125 | + # The version containing this value. Never 'latest', always a specific version. |
| 126 | + version: SecretVersion |
| 127 | + value: bytes |
| 128 | + |
| 129 | + def __str__(self) -> str: |
| 130 | + return self.value.decode("utf-8") |
| 131 | + |
| 132 | + def __bytes__(self) -> bytes: |
| 133 | + return self.value |
| 134 | + |
| 135 | + def as_string(self): |
| 136 | + """Return the content of this secret value as a string.""" |
| 137 | + return str(self) |
| 138 | + |
| 139 | + def as_bytes(self): |
| 140 | + """Return the content of this secret value.""" |
| 141 | + return bytes(self) |
0 commit comments