VaultAPI-Client 0.3.2__py3-none-any.whl → 0.3.3__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.
- vaultapi_client/__init__.py +1 -1
- vaultapi_client/aws.py +62 -30
- vaultapi_client/config.py +24 -11
- vaultapi_client/main.py +129 -21
- vaultapi_client/session.py +28 -10
- {vaultapi_client-0.3.2.dist-info → vaultapi_client-0.3.3.dist-info}/METADATA +2 -2
- vaultapi_client-0.3.3.dist-info/RECORD +14 -0
- {vaultapi_client-0.3.2.dist-info → vaultapi_client-0.3.3.dist-info}/WHEEL +1 -1
- vaultapi_client-0.3.2.dist-info/RECORD +0 -14
- {vaultapi_client-0.3.2.dist-info → vaultapi_client-0.3.3.dist-info}/entry_points.txt +0 -0
- {vaultapi_client-0.3.2.dist-info → vaultapi_client-0.3.3.dist-info}/licenses/LICENSE +0 -0
- {vaultapi_client-0.3.2.dist-info → vaultapi_client-0.3.3.dist-info}/top_level.txt +0 -0
vaultapi_client/__init__.py
CHANGED
vaultapi_client/aws.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import json
|
|
1
2
|
import os
|
|
2
3
|
import warnings
|
|
3
|
-
from typing import List
|
|
4
|
+
from typing import Any, List
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
class AWSClient:
|
|
@@ -21,52 +22,83 @@ class AWSClient:
|
|
|
21
22
|
)
|
|
22
23
|
|
|
23
24
|
session = boto3.Session(
|
|
24
|
-
profile_name=os.environ.get("AWS_PROFILE_NAME"),
|
|
25
|
-
aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID"),
|
|
26
|
-
aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY"),
|
|
27
|
-
region_name=os.environ.get("AWS_DEFAULT_REGION"),
|
|
25
|
+
profile_name=os.environ.get("AWS_PROFILE_NAME", ""),
|
|
26
|
+
aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID", ""),
|
|
27
|
+
aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY", ""),
|
|
28
|
+
region_name=os.environ.get("AWS_DEFAULT_REGION", ""),
|
|
28
29
|
)
|
|
29
30
|
self.secret_client = session.client(service_name="secretsmanager")
|
|
30
31
|
self.ssm_client = session.client(service_name="ssm")
|
|
31
32
|
|
|
32
|
-
def get_aws_secrets(self, name: str
|
|
33
|
+
def get_aws_secrets(self, name: str) -> str | List[str] | None:
|
|
33
34
|
"""Get secrets from AWS secretsmanager.
|
|
34
35
|
|
|
35
36
|
Args:
|
|
36
|
-
name:
|
|
37
|
+
name: Name of the secret.
|
|
37
38
|
|
|
38
39
|
Returns:
|
|
39
40
|
str | List[str]:
|
|
40
41
|
Returns the value of the secret or list of all secrets' names.
|
|
41
42
|
"""
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
page_results = paginator.paginate().build_full_result()
|
|
51
|
-
return [page["Name"] for page in page_results["SecretList"]]
|
|
52
|
-
|
|
53
|
-
def get_aws_params(self, name: str | None = None) -> str | List[str] | None:
|
|
43
|
+
try:
|
|
44
|
+
response = self.secret_client.get_secret_value(SecretId=name)
|
|
45
|
+
except Exception as error:
|
|
46
|
+
warnings.warn(error.__str__(), category=UserWarning, stacklevel=3)
|
|
47
|
+
return None
|
|
48
|
+
return response["SecretString"]
|
|
49
|
+
|
|
50
|
+
def get_aws_params(self, name: str) -> str | List[str] | None:
|
|
54
51
|
"""Get SSM parameters from AWS.
|
|
55
52
|
|
|
56
53
|
Args:
|
|
57
|
-
name:
|
|
54
|
+
name: Name of the parameter.
|
|
58
55
|
|
|
59
56
|
Returns:
|
|
60
57
|
str | List[str]:
|
|
61
58
|
Returns the value of the parameter or list of all parameter names.
|
|
62
59
|
"""
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
60
|
+
try:
|
|
61
|
+
response = self.ssm_client.get_parameter(Name=name, WithDecryption=True)
|
|
62
|
+
except Exception as error:
|
|
63
|
+
warnings.warn(error.__str__(), category=UserWarning, stacklevel=3)
|
|
64
|
+
return None
|
|
65
|
+
return response["Parameter"]["Value"]
|
|
66
|
+
|
|
67
|
+
def put_aws_secrets(self, name: str, value: Any) -> None:
|
|
68
|
+
"""Add secret to AWS secrets manager.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
name: Name of the secret.
|
|
72
|
+
value: Value of the secret.
|
|
73
|
+
"""
|
|
74
|
+
try:
|
|
75
|
+
response = self.secret_client.put_secret_value(
|
|
76
|
+
SecretId=name,
|
|
77
|
+
SecretString=value if isinstance(value, str) else json.dumps(value),
|
|
78
|
+
)
|
|
79
|
+
except Exception as error:
|
|
80
|
+
warnings.warn(error.__str__(), category=UserWarning, stacklevel=3)
|
|
81
|
+
return
|
|
82
|
+
if response.get("ResponseMetadata", {}).get("HTTPStatusCode", 400) != 200:
|
|
83
|
+
warnings.warn(
|
|
84
|
+
response.get("ResponseMetadata"), category=UserWarning, stacklevel=3
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
def put_aws_params(self, name: str, value: str) -> None:
|
|
88
|
+
"""Add a parameter to AWS systems manager.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
name: Name of the parameter.
|
|
92
|
+
value: Value of the parameter.
|
|
93
|
+
"""
|
|
94
|
+
try:
|
|
95
|
+
response = self.ssm_client.put_parameter(
|
|
96
|
+
Name=name, Value=value, Type="SecureString", Overwrite=True
|
|
97
|
+
)
|
|
98
|
+
except Exception as error:
|
|
99
|
+
warnings.warn(error.__str__(), category=UserWarning, stacklevel=3)
|
|
100
|
+
return
|
|
101
|
+
if response.get("ResponseMetadata", {}).get("HTTPStatusCode", 400) != 200:
|
|
102
|
+
warnings.warn(
|
|
103
|
+
response.get("ResponseMetadata"), category=UserWarning, stacklevel=3
|
|
104
|
+
)
|
vaultapi_client/config.py
CHANGED
|
@@ -137,31 +137,40 @@ class EnvConfig:
|
|
|
137
137
|
Fernet(self.vault_secret)
|
|
138
138
|
|
|
139
139
|
|
|
140
|
-
def getenv(*args, default: str = None) -> str:
|
|
140
|
+
def getenv(*args, default: str | None = None) -> str | None:
|
|
141
141
|
"""Returns the key-ed environment variable or the default value."""
|
|
142
142
|
for key in args:
|
|
143
|
-
if value := os.
|
|
143
|
+
if value := os.getenv(key.lower()) or os.getenv(key.upper()):
|
|
144
144
|
return value
|
|
145
145
|
return default
|
|
146
146
|
|
|
147
147
|
|
|
148
|
-
def resolve_secrets(
|
|
148
|
+
def resolve_secrets(
|
|
149
|
+
aws: bool,
|
|
150
|
+
vault_server: str | None = None,
|
|
151
|
+
vault_apikey: str | None = None,
|
|
152
|
+
vault_secret: str | None = None,
|
|
153
|
+
vault_transit_time_bucket: int | None = None,
|
|
154
|
+
vault_transit_key_length: int | None = None,
|
|
155
|
+
) -> EnvConfig | NoReturn:
|
|
149
156
|
"""Tries to retrieve the required secret from environment variable or AWS parameter or the AWS secrets manager."""
|
|
150
157
|
base_env_vars = dict(
|
|
151
|
-
vault_server=getenv("vault_server", "server"),
|
|
152
|
-
vault_apikey=getenv("vault_apikey", "apikey"),
|
|
153
|
-
vault_secret=getenv("vault_secret", "secret"),
|
|
154
|
-
vault_transit_time_bucket=
|
|
155
|
-
|
|
158
|
+
vault_server=vault_server or getenv("vault_server", "server"),
|
|
159
|
+
vault_apikey=vault_apikey or getenv("vault_apikey", "apikey"),
|
|
160
|
+
vault_secret=vault_secret or getenv("vault_secret", "secret"),
|
|
161
|
+
vault_transit_time_bucket=(
|
|
162
|
+
vault_transit_time_bucket
|
|
163
|
+
or getenv("vault_transit_time_bucket", "transit_time_bucket")
|
|
156
164
|
),
|
|
157
|
-
vault_transit_key_length=
|
|
158
|
-
|
|
165
|
+
vault_transit_key_length=(
|
|
166
|
+
vault_transit_key_length
|
|
167
|
+
or getenv("vault_transit_key_length", "transit_time_bucket")
|
|
159
168
|
),
|
|
160
169
|
)
|
|
161
170
|
if all(base_env_vars.values()):
|
|
162
171
|
return EnvConfig(**base_env_vars)
|
|
163
172
|
unsatisfied = [k for k, v in base_env_vars.items() if not v]
|
|
164
|
-
if
|
|
173
|
+
if aws:
|
|
165
174
|
aws_client = AWSClient()
|
|
166
175
|
resolved_env_vars = {
|
|
167
176
|
**base_env_vars,
|
|
@@ -170,6 +179,10 @@ def resolve_secrets(try_aws: bool) -> EnvConfig | NoReturn:
|
|
|
170
179
|
for k in unsatisfied
|
|
171
180
|
},
|
|
172
181
|
}
|
|
182
|
+
if not resolved_env_vars.get("vault_transit_time_bucket"):
|
|
183
|
+
resolved_env_vars["vault_transit_time_bucket"] = 60
|
|
184
|
+
if not resolved_env_vars.get("vault_transit_key_length"):
|
|
185
|
+
resolved_env_vars["vault_transit_key_length"] = 32
|
|
173
186
|
if all(resolved_env_vars.values()):
|
|
174
187
|
return EnvConfig(**resolved_env_vars)
|
|
175
188
|
unsatisfied = [k for k, v in resolved_env_vars.items() if not v]
|
vaultapi_client/main.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
|
2
2
|
from typing import Dict, List
|
|
3
3
|
|
|
4
4
|
import dotenv
|
|
5
|
+
from cryptography.fernet import Fernet
|
|
5
6
|
|
|
6
7
|
from vaultapi_client.config import EndpointMapping, getenv, resolve_secrets
|
|
7
8
|
from vaultapi_client.session import Session
|
|
@@ -15,20 +16,42 @@ class VaultAPIClient:
|
|
|
15
16
|
|
|
16
17
|
"""
|
|
17
18
|
|
|
18
|
-
def __init__(
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
aws: bool = getenv("vault_aws", default="0") in ("1", "true"),
|
|
22
|
+
vault_server: str | None = None,
|
|
23
|
+
vault_apikey: str | None = None,
|
|
24
|
+
vault_secret: str | None = None,
|
|
25
|
+
vault_transit_time_bucket: int | None = None,
|
|
26
|
+
vault_transit_key_length: int | None = None,
|
|
27
|
+
timeout: int | float | tuple | None = (5, 30),
|
|
28
|
+
):
|
|
19
29
|
"""Instantiates the VaultAPIClient object."""
|
|
20
|
-
self.env_config = resolve_secrets(
|
|
30
|
+
self.env_config = resolve_secrets(
|
|
31
|
+
aws=aws,
|
|
32
|
+
vault_server=vault_server,
|
|
33
|
+
vault_apikey=vault_apikey,
|
|
34
|
+
vault_secret=vault_secret,
|
|
35
|
+
vault_transit_time_bucket=vault_transit_time_bucket,
|
|
36
|
+
vault_transit_key_length=vault_transit_key_length,
|
|
37
|
+
)
|
|
21
38
|
self.transit_shield = TransitShield(self.env_config)
|
|
22
39
|
self.SESSION = Session(self.env_config)
|
|
40
|
+
self.fernet = Fernet(self.env_config.vault_secret)
|
|
41
|
+
self.timeout = timeout
|
|
23
42
|
|
|
24
43
|
def _get_cipher(
|
|
25
|
-
self,
|
|
44
|
+
self,
|
|
45
|
+
endpoint: EndpointMapping,
|
|
46
|
+
query_params: Dict[str, str],
|
|
47
|
+
timeout: int | float | tuple | None = None,
|
|
26
48
|
) -> str:
|
|
27
49
|
"""Get ciphertext from the server.
|
|
28
50
|
|
|
29
51
|
Args:
|
|
30
52
|
endpoint: API endpoint to request.
|
|
31
53
|
query_params: Query parameters to send with the request.
|
|
54
|
+
timeout: Timeout for the request in seconds. Can be a float or a tuple (connect_timeout, read_timeout).
|
|
32
55
|
|
|
33
56
|
Returns:
|
|
34
57
|
str:
|
|
@@ -37,14 +60,21 @@ class VaultAPIClient:
|
|
|
37
60
|
return self.SESSION.get(
|
|
38
61
|
endpoint,
|
|
39
62
|
params=query_params,
|
|
63
|
+
timeout=timeout or self.timeout,
|
|
40
64
|
)
|
|
41
65
|
|
|
42
|
-
def dotenv_to_table(
|
|
66
|
+
def dotenv_to_table(
|
|
67
|
+
self,
|
|
68
|
+
table_name: str,
|
|
69
|
+
dotenv_file: str,
|
|
70
|
+
timeout: int | float | tuple | None = None,
|
|
71
|
+
) -> Dict[str, str]:
|
|
43
72
|
"""Store all the env vars from a .env file into the database.
|
|
44
73
|
|
|
45
74
|
Args:
|
|
46
75
|
table_name: Name of the table to store secrets.
|
|
47
76
|
dotenv_file: Dot env filename.
|
|
77
|
+
timeout: Timeout for the request in seconds. Can be a float or a tuple (connect_timeout, read_timeout).
|
|
48
78
|
"""
|
|
49
79
|
try:
|
|
50
80
|
assert os.path.isfile(dotenv_file)
|
|
@@ -53,21 +83,29 @@ class VaultAPIClient:
|
|
|
53
83
|
env_vars = {
|
|
54
84
|
k: v for k, v in dotenv.dotenv_values(dotenv_file).items() if v is not None
|
|
55
85
|
}
|
|
56
|
-
return self.update_secret(
|
|
86
|
+
return self.update_secret(
|
|
87
|
+
secrets=env_vars, table_name=table_name, timeout=timeout or self.timeout
|
|
88
|
+
)
|
|
57
89
|
|
|
58
|
-
def table_to_env(
|
|
90
|
+
def table_to_env(
|
|
91
|
+
self,
|
|
92
|
+
table_name: str,
|
|
93
|
+
dotenv_file: str | None = None,
|
|
94
|
+
timeout: int | float | tuple | None = None,
|
|
95
|
+
) -> None:
|
|
59
96
|
"""Retrieve all secrets from the database and store them as env vars.
|
|
60
97
|
|
|
61
98
|
Args:
|
|
62
99
|
table_name: Vault table name,
|
|
63
100
|
dotenv_file: Dot env filename to store secrets in addition to env vars.
|
|
101
|
+
timeout: Timeout for the request in seconds. Can be a float or a tuple (connect_timeout, read_timeout).
|
|
64
102
|
"""
|
|
65
103
|
if dotenv_file:
|
|
66
104
|
try:
|
|
67
105
|
assert os.path.isfile(dotenv_file)
|
|
68
106
|
except AssertionError:
|
|
69
107
|
raise FileNotFoundError(dotenv_file)
|
|
70
|
-
secrets = self.get_table(table_name)
|
|
108
|
+
secrets = self.get_table(table_name, timeout=timeout or self.timeout)
|
|
71
109
|
for key, value in secrets.items():
|
|
72
110
|
os.environ[key] = value
|
|
73
111
|
if dotenv_file:
|
|
@@ -75,12 +113,18 @@ class VaultAPIClient:
|
|
|
75
113
|
dotenv_path=dotenv_file, key_to_set=key, value_to_set=value
|
|
76
114
|
)
|
|
77
115
|
|
|
78
|
-
def update_secret(
|
|
116
|
+
def update_secret(
|
|
117
|
+
self,
|
|
118
|
+
secrets: Dict[str, str],
|
|
119
|
+
table_name: str,
|
|
120
|
+
timeout: int | float | tuple | None = None,
|
|
121
|
+
) -> Dict[str, str]:
|
|
79
122
|
"""Update or create secrets in the vault.
|
|
80
123
|
|
|
81
124
|
Args:
|
|
82
125
|
secrets: Key value pairs with multiple secrets.
|
|
83
126
|
table_name: Table name.
|
|
127
|
+
timeout: Timeout for the request in seconds. Can be a float or a tuple (connect_timeout, read_timeout).
|
|
84
128
|
|
|
85
129
|
Returns:
|
|
86
130
|
Dict[str, str]:
|
|
@@ -92,14 +136,44 @@ class VaultAPIClient:
|
|
|
92
136
|
"secrets": self.transit_shield.encrypt(payload=secrets),
|
|
93
137
|
"table_name": table_name,
|
|
94
138
|
},
|
|
139
|
+
timeout=timeout or self.timeout,
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
def encryptor(self, value: str) -> str:
|
|
143
|
+
"""Encrypt a value.
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
value: Value to encrypt.
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
str:
|
|
150
|
+
Returns the encrypted string.
|
|
151
|
+
"""
|
|
152
|
+
return self.fernet.encrypt(data=value.encode(encoding="utf-8")).decode(
|
|
153
|
+
encoding="utf-8"
|
|
95
154
|
)
|
|
96
155
|
|
|
97
|
-
def
|
|
156
|
+
def decryptor(self, token: str) -> str:
|
|
157
|
+
"""Decrypt a token.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
token: Token to decrypt
|
|
161
|
+
|
|
162
|
+
Returns:
|
|
163
|
+
str:
|
|
164
|
+
Returns the decrypted payload.
|
|
165
|
+
"""
|
|
166
|
+
return self.fernet.decrypt(token=token).decode(encoding="utf-8")
|
|
167
|
+
|
|
168
|
+
def delete_secret(
|
|
169
|
+
self, key: str, table_name: str, timeout: int | float | tuple | None = None
|
|
170
|
+
) -> Dict[str, str]:
|
|
98
171
|
"""Delete a secret from the vault.
|
|
99
172
|
|
|
100
173
|
Args:
|
|
101
174
|
key: Key for the secret.
|
|
102
175
|
table_name: Table name.
|
|
176
|
+
timeout: Timeout for the request in seconds. Can be a float or a tuple (connect_timeout, read_timeout).
|
|
103
177
|
|
|
104
178
|
Returns:
|
|
105
179
|
Dict[str, str]:
|
|
@@ -111,37 +185,51 @@ class VaultAPIClient:
|
|
|
111
185
|
"key": key,
|
|
112
186
|
"table_name": table_name,
|
|
113
187
|
},
|
|
188
|
+
timeout=timeout or self.timeout,
|
|
114
189
|
)
|
|
115
190
|
|
|
116
|
-
def list_tables(self) -> List[str]:
|
|
191
|
+
def list_tables(self, timeout: int | float | tuple | None = None) -> List[str]:
|
|
117
192
|
"""List all available tables.
|
|
118
193
|
|
|
194
|
+
Args:
|
|
195
|
+
timeout: Timeout for the request in seconds. Can be a float or a tuple (connect_timeout, read_timeout).
|
|
196
|
+
|
|
119
197
|
Returns:
|
|
120
198
|
List[str]:
|
|
121
199
|
Returns the available table names as a list of strings.
|
|
122
200
|
"""
|
|
123
|
-
return self.SESSION.get(
|
|
201
|
+
return self.SESSION.get(
|
|
202
|
+
EndpointMapping.list_tables, timeout=timeout or self.timeout
|
|
203
|
+
)
|
|
124
204
|
|
|
125
|
-
def create_table(
|
|
205
|
+
def create_table(
|
|
206
|
+
self, table_name: str, timeout: int | float | tuple | None = None
|
|
207
|
+
) -> Dict[str, str]:
|
|
126
208
|
"""Creates a new table in the vault database.
|
|
127
209
|
|
|
128
210
|
Args:
|
|
129
211
|
table_name: Table name.
|
|
212
|
+
timeout: Timeout for the request in seconds. Can be a float or a tuple (connect_timeout, read_timeout).
|
|
130
213
|
|
|
131
214
|
Returns:
|
|
132
215
|
Dict[str, str]:
|
|
133
216
|
Returns the server response.
|
|
134
217
|
"""
|
|
135
218
|
return self.SESSION.post(
|
|
136
|
-
EndpointMapping.create_table,
|
|
219
|
+
EndpointMapping.create_table,
|
|
220
|
+
params={"table_name": table_name},
|
|
221
|
+
timeout=timeout or self.timeout,
|
|
137
222
|
)
|
|
138
223
|
|
|
139
|
-
def rename_table(
|
|
224
|
+
def rename_table(
|
|
225
|
+
self, table_name: str, new_name: str, timeout: int | float | tuple | None = None
|
|
226
|
+
) -> str:
|
|
140
227
|
"""Renames a table in the vault database.
|
|
141
228
|
|
|
142
229
|
Args:
|
|
143
230
|
table_name: Table name to rename.
|
|
144
231
|
new_name: New table name.
|
|
232
|
+
timeout: Timeout for the request in seconds. Can be a float or a tuple (connect_timeout, read_timeout).
|
|
145
233
|
|
|
146
234
|
Returns:
|
|
147
235
|
str:
|
|
@@ -151,50 +239,66 @@ class VaultAPIClient:
|
|
|
151
239
|
EndpointMapping.rename_table,
|
|
152
240
|
params={"table_name": table_name},
|
|
153
241
|
json=dict(new_name=new_name),
|
|
242
|
+
timeout=timeout or self.timeout,
|
|
154
243
|
)
|
|
155
244
|
|
|
156
|
-
def delete_table(
|
|
245
|
+
def delete_table(
|
|
246
|
+
self, table_name: str, timeout: int | float | tuple | None = None
|
|
247
|
+
) -> Dict[str, str]:
|
|
157
248
|
"""Deletes an existing table.
|
|
158
249
|
|
|
159
250
|
Args:
|
|
160
251
|
table_name: Table name.
|
|
252
|
+
timeout: Timeout for the request in seconds. Can be a float or a tuple (connect_timeout, read_timeout).
|
|
161
253
|
|
|
162
254
|
Returns:
|
|
163
255
|
Dict[str, str]:
|
|
164
256
|
Returns the server response.
|
|
165
257
|
"""
|
|
166
258
|
return self.SESSION.delete(
|
|
167
|
-
EndpointMapping.delete_table,
|
|
259
|
+
EndpointMapping.delete_table,
|
|
260
|
+
params={"table_name": table_name},
|
|
261
|
+
timeout=timeout or self.timeout,
|
|
168
262
|
)
|
|
169
263
|
|
|
170
|
-
def get_secret(
|
|
264
|
+
def get_secret(
|
|
265
|
+
self, key: str, table_name: str, timeout: int | float | tuple | None = None
|
|
266
|
+
) -> Dict[str, str]:
|
|
171
267
|
"""Retrieves multiple secrets from a table.
|
|
172
268
|
|
|
173
269
|
Args:
|
|
174
270
|
key: Comma separated list of secret names to be retrieved.
|
|
175
271
|
table_name: Table name.
|
|
272
|
+
timeout: Timeout for the request in seconds. Can be a float or a tuple (connect_timeout, read_timeout).
|
|
176
273
|
|
|
177
274
|
Returns:
|
|
178
275
|
Dict[str, str]:
|
|
179
276
|
Returns a dictionary of decrypted values.
|
|
180
277
|
"""
|
|
181
278
|
cipher_text = self._get_cipher(
|
|
182
|
-
EndpointMapping.get_secret,
|
|
279
|
+
EndpointMapping.get_secret,
|
|
280
|
+
query_params={"key": key, "table_name": table_name},
|
|
281
|
+
timeout=timeout or self.timeout,
|
|
183
282
|
)
|
|
184
283
|
return self.transit_shield.decrypt(ciphertext=cipher_text)
|
|
185
284
|
|
|
186
|
-
def get_table(
|
|
285
|
+
def get_table(
|
|
286
|
+
self, table_name: str, timeout: int | float | tuple | None = None
|
|
287
|
+
) -> Dict[str, str]:
|
|
187
288
|
"""Retrieves all the secrets stored in a table.
|
|
188
289
|
|
|
189
290
|
Args:
|
|
190
291
|
table_name: Table name.
|
|
292
|
+
timeout: Timeout for the request in seconds. Can be a float or a tuple (connect_timeout, read_timeout).
|
|
191
293
|
|
|
192
294
|
Returns:
|
|
193
295
|
Dict[str, str]:
|
|
194
296
|
Returns a dictionary of decrypted values.
|
|
195
297
|
"""
|
|
196
298
|
cipher_text = self._get_cipher(
|
|
197
|
-
EndpointMapping.get_table,
|
|
299
|
+
EndpointMapping.get_table,
|
|
300
|
+
query_params={"table_name": table_name},
|
|
301
|
+
timeout=timeout or self.timeout,
|
|
198
302
|
)
|
|
199
303
|
return self.transit_shield.decrypt(ciphertext=cipher_text)
|
|
200
304
|
|
|
@@ -222,5 +326,9 @@ class VaultAPIClient:
|
|
|
222
326
|
else:
|
|
223
327
|
endpoint = EndpointMapping.get_table
|
|
224
328
|
return self.transit_shield.decrypt(
|
|
225
|
-
self._get_cipher(
|
|
329
|
+
self._get_cipher(
|
|
330
|
+
endpoint=EndpointMapping(endpoint),
|
|
331
|
+
query_params=params,
|
|
332
|
+
timeout=self.timeout,
|
|
333
|
+
)
|
|
226
334
|
)
|
vaultapi_client/session.py
CHANGED
|
@@ -12,10 +12,12 @@ from vaultapi_client.util import urljoin
|
|
|
12
12
|
|
|
13
13
|
def _request_error(error: requests.RequestException) -> NoReturn:
|
|
14
14
|
"""Uses requests module base exception to raise a custom server error."""
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
status_code = 0
|
|
16
|
+
response = "Unknown reason"
|
|
17
|
+
if error.response:
|
|
18
|
+
status_code = error.response.status_code
|
|
19
|
+
response = error.response.json() or error.response.text
|
|
20
|
+
raise VaultAPIServerError(f"Request failed: {response}", status_code=status_code)
|
|
19
21
|
|
|
20
22
|
|
|
21
23
|
def generate_bearer_token(token: str) -> str:
|
|
@@ -87,6 +89,7 @@ class Session:
|
|
|
87
89
|
endpoint: EndpointMapping,
|
|
88
90
|
params: Dict[str, Any] | None = None,
|
|
89
91
|
json: Dict[str, Any] | None = None,
|
|
92
|
+
timeout: int | float | tuple | None = None,
|
|
90
93
|
) -> Any:
|
|
91
94
|
"""Intercepts all HTTP requests and applies centralized error handling.
|
|
92
95
|
|
|
@@ -95,6 +98,7 @@ class Session:
|
|
|
95
98
|
endpoint: Endpoint to make a request.
|
|
96
99
|
params: Query parameters to pass to the request.
|
|
97
100
|
json: JSON data to pass to the request.
|
|
101
|
+
timeout: Timeout in seconds.
|
|
98
102
|
|
|
99
103
|
Returns:
|
|
100
104
|
Any:
|
|
@@ -107,7 +111,12 @@ class Session:
|
|
|
107
111
|
headers = self.headers(token=f"{self.apikey}.{self.secret}")
|
|
108
112
|
try:
|
|
109
113
|
response = self.session.request(
|
|
110
|
-
method=method,
|
|
114
|
+
method=method,
|
|
115
|
+
url=url,
|
|
116
|
+
headers=headers,
|
|
117
|
+
params=params,
|
|
118
|
+
json=json,
|
|
119
|
+
timeout=timeout,
|
|
111
120
|
)
|
|
112
121
|
response.raise_for_status()
|
|
113
122
|
return process_response(response)
|
|
@@ -119,42 +128,51 @@ class Session:
|
|
|
119
128
|
endpoint: EndpointMapping,
|
|
120
129
|
params: Dict[str, Any] | None = None,
|
|
121
130
|
json: Dict[str, Any] | None = None,
|
|
131
|
+
timeout: int | float | tuple | None = None,
|
|
122
132
|
) -> Any:
|
|
123
133
|
"""Make GET request to the server and process the response."""
|
|
124
|
-
return self.request("GET", endpoint, params=params, json=json)
|
|
134
|
+
return self.request("GET", endpoint, params=params, json=json, timeout=timeout)
|
|
125
135
|
|
|
126
136
|
def put(
|
|
127
137
|
self,
|
|
128
138
|
endpoint: EndpointMapping,
|
|
129
139
|
params: Dict[str, Any] | None = None,
|
|
130
140
|
json: Dict[str, Any] | None = None,
|
|
141
|
+
timeout: int | float | tuple | None = None,
|
|
131
142
|
) -> Any:
|
|
132
143
|
"""Make PUT request to the server and process the response."""
|
|
133
|
-
return self.request("PUT", endpoint, params=params, json=json)
|
|
144
|
+
return self.request("PUT", endpoint, params=params, json=json, timeout=timeout)
|
|
134
145
|
|
|
135
146
|
def post(
|
|
136
147
|
self,
|
|
137
148
|
endpoint: EndpointMapping,
|
|
138
149
|
params: Dict[str, Any] | None = None,
|
|
139
150
|
json: Dict[str, Any] | None = None,
|
|
151
|
+
timeout: int | float | tuple | None = None,
|
|
140
152
|
) -> Any:
|
|
141
153
|
"""Make POST request to the server and process the response."""
|
|
142
|
-
return self.request("POST", endpoint, params=params, json=json)
|
|
154
|
+
return self.request("POST", endpoint, params=params, json=json, timeout=timeout)
|
|
143
155
|
|
|
144
156
|
def patch(
|
|
145
157
|
self,
|
|
146
158
|
endpoint: EndpointMapping,
|
|
147
159
|
params: Dict[str, Any] | None = None,
|
|
148
160
|
json: Dict[str, Any] | None = None,
|
|
161
|
+
timeout: int | float | tuple | None = None,
|
|
149
162
|
) -> Any:
|
|
150
163
|
"""Make PATCH request to the server and process the response."""
|
|
151
|
-
return self.request(
|
|
164
|
+
return self.request(
|
|
165
|
+
"PATCH", endpoint, params=params, json=json, timeout=timeout
|
|
166
|
+
)
|
|
152
167
|
|
|
153
168
|
def delete(
|
|
154
169
|
self,
|
|
155
170
|
endpoint: EndpointMapping,
|
|
156
171
|
params: Dict[str, Any] | None = None,
|
|
157
172
|
json: Dict[str, Any] | None = None,
|
|
173
|
+
timeout: int | float | tuple | None = None,
|
|
158
174
|
) -> Any:
|
|
159
175
|
"""Make DELETE request to the server and process the response."""
|
|
160
|
-
return self.request(
|
|
176
|
+
return self.request(
|
|
177
|
+
"DELETE", endpoint, params=params, json=json, timeout=timeout
|
|
178
|
+
)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: VaultAPI-Client
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.3
|
|
4
4
|
Summary: Client application for VaultAPI Server
|
|
5
5
|
Author-email: Vignesh Rao <svignesh1793@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -37,7 +37,7 @@ Classifier: Topic :: Security :: Cryptography
|
|
|
37
37
|
Requires-Python: >=3.10
|
|
38
38
|
Description-Content-Type: text/markdown
|
|
39
39
|
License-File: LICENSE
|
|
40
|
-
Requires-Dist: cryptography==
|
|
40
|
+
Requires-Dist: cryptography==50.0.*
|
|
41
41
|
Requires-Dist: packaging==26.*
|
|
42
42
|
Requires-Dist: python-dotenv==1.2.*
|
|
43
43
|
Requires-Dist: requests==2.*
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
vaultapi_client/__init__.py,sha256=RD8VuOd7NjVf2T6OADNqT0NVf1Kz2I0Ea9NiptNYs1M,1308
|
|
2
|
+
vaultapi_client/aws.py,sha256=YZ1zmii9g6cPkelZDq3S91UAMsanJHEHUWicO3HgRps,3661
|
|
3
|
+
vaultapi_client/config.py,sha256=2o3iUasGLqaPZobLw9moOtvZeS2mIo2zVRhbDYD8Ogg,6862
|
|
4
|
+
vaultapi_client/exceptions.py,sha256=hpZxtnSa1SHNDaTJwYdwXFuUkzi2ZdB23apKrKOOUzk,602
|
|
5
|
+
vaultapi_client/main.py,sha256=qvdja7cHl_JMx_SOA-KStboDbj8qy6laKzE3cYJG_cw,10952
|
|
6
|
+
vaultapi_client/session.py,sha256=3C8eN27vwoFJH1ZLBcPhrU47EvkemJgVN5D3vEt0Hi8,6062
|
|
7
|
+
vaultapi_client/transit.py,sha256=ZH4OF-TO50zQfcCy6e1f202QWYaBPk9bzZ2bWhSPQxo,3114
|
|
8
|
+
vaultapi_client/util.py,sha256=qMD1Hpu3_GmwxNJ8AM-k5yu-QlN--qes00LT--Q5kkQ,393
|
|
9
|
+
vaultapi_client-0.3.3.dist-info/licenses/LICENSE,sha256=_sOIKJWdD2o1WwwDIwYB2qTP2nlSWqT5Tyg9jr1Xa4w,1070
|
|
10
|
+
vaultapi_client-0.3.3.dist-info/METADATA,sha256=WyMEvFHIu32g1wdn6LsvtGTcGTYQFyNCuEPN-u7TNuQ,6160
|
|
11
|
+
vaultapi_client-0.3.3.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
12
|
+
vaultapi_client-0.3.3.dist-info/entry_points.txt,sha256=P3CaBVJ9RvRJy7xvd9A4o3HYEfoM4wStZcQ8L6837NE,64
|
|
13
|
+
vaultapi_client-0.3.3.dist-info/top_level.txt,sha256=J0qf2Tuj66RGC_VTF9f3Ce137qPqL8n0V21-1xwp1nw,16
|
|
14
|
+
vaultapi_client-0.3.3.dist-info/RECORD,,
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
vaultapi_client/__init__.py,sha256=hURJp9_84PSwOgy2V7SrgqyaVBNFp8w3c7UbTSNt-T4,1308
|
|
2
|
-
vaultapi_client/aws.py,sha256=1y-hSg76K5oc1wqbOBVuGhVZV3Ckt2ttTEdL5O0i4yo,2694
|
|
3
|
-
vaultapi_client/config.py,sha256=uy-DjVn8382EoSCodmlFLUGrLa9A6aoesjgymHw4inE,6284
|
|
4
|
-
vaultapi_client/exceptions.py,sha256=hpZxtnSa1SHNDaTJwYdwXFuUkzi2ZdB23apKrKOOUzk,602
|
|
5
|
-
vaultapi_client/main.py,sha256=6joJFNJRFNM1Te2ixzGdzB9iGbrgonIvYZeAUQ60ncc,6956
|
|
6
|
-
vaultapi_client/session.py,sha256=sSN92klmGBRffI131ByI95tk42-VNMi3ytxqHwC5dTo,5347
|
|
7
|
-
vaultapi_client/transit.py,sha256=ZH4OF-TO50zQfcCy6e1f202QWYaBPk9bzZ2bWhSPQxo,3114
|
|
8
|
-
vaultapi_client/util.py,sha256=qMD1Hpu3_GmwxNJ8AM-k5yu-QlN--qes00LT--Q5kkQ,393
|
|
9
|
-
vaultapi_client-0.3.2.dist-info/licenses/LICENSE,sha256=_sOIKJWdD2o1WwwDIwYB2qTP2nlSWqT5Tyg9jr1Xa4w,1070
|
|
10
|
-
vaultapi_client-0.3.2.dist-info/METADATA,sha256=yfKd7qFy_YpsdxysAAaegfk5g8vwdu0gGJAYXu2yYp0,6160
|
|
11
|
-
vaultapi_client-0.3.2.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
|
|
12
|
-
vaultapi_client-0.3.2.dist-info/entry_points.txt,sha256=P3CaBVJ9RvRJy7xvd9A4o3HYEfoM4wStZcQ8L6837NE,64
|
|
13
|
-
vaultapi_client-0.3.2.dist-info/top_level.txt,sha256=J0qf2Tuj66RGC_VTF9f3Ce137qPqL8n0V21-1xwp1nw,16
|
|
14
|
-
vaultapi_client-0.3.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|