automizor 0.3.0__py3-none-any.whl → 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
automizor/vault.py DELETED
@@ -1,190 +0,0 @@
1
- import json
2
- import os
3
- from dataclasses import asdict, dataclass
4
-
5
- import requests
6
-
7
-
8
- class AutomizorVaultError(RuntimeError):
9
- """Exception raised for errors encountered while interacting with the Vault."""
10
-
11
-
12
- @dataclass
13
- class Secret:
14
- """
15
- Represents a secret, comprising a name and its associated values.
16
-
17
- Attributes:
18
- name (str): The name of the secret.
19
- value (dict): The secret's values, stored in a dictionary as key-value pairs.
20
- """
21
-
22
- name: str
23
- value: dict
24
-
25
- def get(self, key, default=None):
26
- """Return the value for key if key is in the dictionary, else default."""
27
- return self.value.get(key, default)
28
-
29
- def items(self):
30
- """secret.items() -> a set-like object providing a view on secret's items."""
31
- return self.value.items()
32
-
33
- def update(self, pairs: dict) -> None:
34
- self.value.update(pairs)
35
-
36
- def __getitem__(self, key):
37
- return self.value[key]
38
-
39
- def __setitem__(self, key, value):
40
- self.value[key] = value
41
-
42
- def __contains__(self, key):
43
- return key in self.value
44
-
45
- def __iter__(self):
46
- return iter(self.value)
47
-
48
- def __len__(self):
49
- return len(self.value)
50
-
51
- def __repr__(self):
52
- keys = ", ".join(self.value.keys())
53
- return f"Secret(name={self.name}, keys={keys})"
54
-
55
-
56
- class Vault:
57
- """
58
- `Vault` is a library to manage secrets within an the `Automizor Platform`,
59
- providing functionality to retrieve and update secrets. It supports interaction
60
- with the `Vault API` (by default) or a local file for secret storage, determined
61
- by environment variable configuration.
62
-
63
- The Vault class uses environment variables to configure the API host, API token,
64
- which are set by the `Automizor Agent`.
65
-
66
- You may want to set the environment variables in your local environment for testing
67
- purposes. The variables which must exist are:
68
-
69
- - ``AUTOMIZOR_API_HOST``: The host URL of the `Automizor API`
70
- - ``AUTOMIZOR_API_TOKEN``: The token used for authenticating with the `Automizor API`
71
-
72
- In addition, you can set the following environment variable to use a local file for
73
- secret storage:
74
-
75
- - ``AUTOMIZOR_SECRET_FILE``: The path to a local file where secrets are stored.
76
-
77
- Example of a local secret file:
78
-
79
- .. code-block:: json
80
-
81
- {
82
- "my_secret_name": {
83
- "key": "value"
84
- }
85
- }
86
-
87
- Example usage:
88
-
89
- .. code-block:: python
90
-
91
- from automizor.vault import Vault
92
-
93
- vault = Vault()
94
-
95
- def read_secret():
96
- secret = vault.get_secret("my_secret_name")
97
- print(secret["key"]) # Output: "value"
98
-
99
- def update_secret():
100
- secret = vault.get_secret("my_secret_name")
101
- secret["new_key"] = "new_value"
102
- vault.set_secret(secret)
103
-
104
- """
105
-
106
- def __init__(self):
107
- self._api_host = os.getenv("AUTOMIZOR_API_HOST")
108
- self._api_token = os.getenv("AUTOMIZOR_API_TOKEN")
109
- self._secret_file = os.getenv("AUTOMIZOR_SECRET_FILE")
110
-
111
- @property
112
- def headers(self) -> dict:
113
- """Headers for API requests, including Authorization and Content-Type."""
114
- return {
115
- "Authorization": f"Token {self._api_token}",
116
- "Content-Type": "application/json",
117
- }
118
-
119
- def get_secret(self, name) -> Secret:
120
- """
121
- Retrieves a secret by its name. Fetches from a local file or queries the
122
- `Automizor API`, based on configuration.
123
-
124
- Args:
125
- name (str): The name of the secret to retrieve.
126
-
127
- Returns:
128
- Secret: The retrieved secret.
129
-
130
- Raises:
131
- AutomizorVaultError: If retrieving the secret fails.
132
- """
133
-
134
- if self._secret_file:
135
- return self._read_file_secret(name)
136
- return self._read_vault_secret(name)
137
-
138
- def set_secret(self, secret: Secret) -> Secret:
139
- """
140
- Updates a secret. Writes to a local file or sends to the `Automizor API`,
141
- based on configuration.
142
-
143
- Args:
144
- secret (Secret): The secret to update.
145
-
146
- Returns:
147
- Secret: The updated secret.
148
-
149
- Raises:
150
- AutomizorVaultError: If updating the secret fails.
151
- """
152
-
153
- if self._secret_file:
154
- return self._write_file_secret(secret)
155
- return self._write_vault_secret(secret)
156
-
157
- def _read_file_secret(self, name: str) -> Secret:
158
- with open(self._secret_file, "r", encoding="utf-8") as file:
159
- secrets = json.load(file)
160
- value = secrets.get(name, {})
161
- return Secret(name=name, value=value)
162
-
163
- def _read_vault_secret(self, name: str) -> Secret:
164
- url = f"https://{self._api_host}/api/v1/vault/secret/{name}/"
165
- try:
166
- response = requests.get(url, headers=self.headers, timeout=10)
167
- response.raise_for_status()
168
- return Secret(**response.json())
169
- except Exception as exc:
170
- raise AutomizorVaultError(f"Failed to get secret: {exc}") from exc
171
-
172
- def _write_file_secret(self, secret: Secret):
173
- with open(self._secret_file, "r+", encoding="utf-8") as file:
174
- secrets = json.load(file)
175
- secrets[secret.name] = secret.value
176
- file.seek(0)
177
- file.write(json.dumps(secrets, indent=4))
178
- file.truncate()
179
- return secret
180
-
181
- def _write_vault_secret(self, secret: Secret) -> Secret:
182
- url = f"https://{self._api_host}/api/v1/vault/secret/{secret.name}/"
183
- try:
184
- response = requests.put(
185
- url, headers=self.headers, timeout=10, json=asdict(secret)
186
- )
187
- response.raise_for_status()
188
- return Secret(**response.json())
189
- except Exception as exc:
190
- raise AutomizorVaultError(f"Failed to set secret: {exc}") from exc
@@ -1,19 +0,0 @@
1
- automizor/__init__.py,sha256=EqIccytbgmIh3EOfv7QIiksdjJSlB-o2LbyXxRtoMGs,18
2
- automizor/job.py,sha256=L2NkM-BkvJpeO_SH0BMgternD9M83K_Yv_ANhf1k3FI,4354
3
- automizor/storage.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- automizor/vault.py,sha256=mluaCcJCMxu2g0iTBJ6ntoZATn8eotfZb4rdzWsBslU,5845
5
- automizor/job/__init__.py,sha256=g-n56j50AGPCE23Nm0QFZCrkCa-c_weqvPcEX3I1NQY,1087
6
- automizor/job/_exceptions.py,sha256=zngd7Vv4dkCfwmkigaHiyu5gKAgmcRHt095h55KlbDg,121
7
- automizor/job/_job.py,sha256=NkoNnJxmdkqdF-Qxm4taal-Go0COVFo57tZAaWM1ihI,5365
8
- automizor/storage/__init__.py,sha256=KuWO-Pb4FQXj68Ewv8QZR9XeKsROCR-wusdWf0osaLw,1674
9
- automizor/storage/_exceptions.py,sha256=LOtgshWg3gOFhDZlcMWhXLT_q11zpTBEA85NqKnSi4A,129
10
- automizor/storage/_storage.py,sha256=IwTw6PYYNwJtGI4ZfiqY0-SkgCoaYnykkP_hHVuy9IU,5777
11
- automizor/vault/__init__.py,sha256=37KiP4K9y6rphpzW7xAlnQiLjubRBfxzkH_kNRSNg2k,1092
12
- automizor/vault/_exceptions.py,sha256=Wblvmaj6F0pIiTAH7X3JuxqTprUA5tvuuRAs9YgbiBI,125
13
- automizor/vault/_secret.py,sha256=pks_3uvD1IhYirOaZ2cAOxX2r9vzvXqLa-aDzJysreE,1136
14
- automizor/vault/_vault.py,sha256=d8ymE-qSHY31GDEToql9KHUQNLpMzb469MCzvtpie_U,4714
15
- automizor-0.3.0.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
16
- automizor-0.3.0.dist-info/METADATA,sha256=olJvbx__hqQFXHeaXPlSkOzcd4vBKuFAY8Nu1lLyARg,661
17
- automizor-0.3.0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
18
- automizor-0.3.0.dist-info/top_level.txt,sha256=gScDy4I3tP6BMYAsTAlBXrxVh3E00zV0UioxwXJOI3Y,10
19
- automizor-0.3.0.dist-info/RECORD,,