VaultAPI-Client 0.1.2__py3-none-any.whl → 0.3.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.
@@ -0,0 +1,38 @@
1
+ import argparse
2
+ import json
3
+
4
+ from .exceptions import VaultAPIClientError # noqa: F401
5
+ from .main import VaultAPIClient
6
+
7
+ version = "0.3.0"
8
+
9
+
10
+ def commandline():
11
+ """Entrypoint for vaultapi commandline."""
12
+ parser = argparse.ArgumentParser()
13
+ parser.add_argument(
14
+ "-V", "--version", action="store_true", help="Show version information"
15
+ )
16
+ parser.add_argument(
17
+ "--aws",
18
+ action="store_true",
19
+ help="Flag to use AWS parameter store and secrets manager to retrieve the server credentials. "
20
+ "Requires 'pip install VaultAPI-Client[aws]'",
21
+ )
22
+ parser.add_argument(
23
+ "--get-secret",
24
+ help="Retrieve a secret from Vault using the secret key, or leave it empty to get the entire table.",
25
+ )
26
+ parser.add_argument(
27
+ "--table",
28
+ help="Table name where the secrets are stored. Required to retrieve a secret from the Vault API.",
29
+ )
30
+ kwargs = dict(parser.parse_args()._get_kwargs())
31
+ if kwargs["version"]:
32
+ print(f"VaultAPI Client: {version}")
33
+ exit(0)
34
+ assert kwargs["table"] is not None, "table name must be provided"
35
+ vaultapi_client = VaultAPIClient(aws=kwargs["aws"])
36
+ kwargs.pop("version", None)
37
+ kwargs.pop("aws", None)
38
+ print(json.dumps(vaultapi_client.decrypt(**kwargs), indent=2))
@@ -1,9 +1,7 @@
1
- import logging
2
1
  import os
2
+ import warnings
3
3
  from typing import List
4
4
 
5
- LOGGER = logging.getLogger(__name__)
6
-
7
5
 
8
6
  class AWSClient:
9
7
  """AWS client object to retrieve parameters (parameter store) or secrets (from secrets manager).
@@ -15,6 +13,7 @@ class AWSClient:
15
13
  def __init__(self):
16
14
  """Instantiates the client object."""
17
15
  try:
16
+ # noinspection PyPackageRequirements,PyUnresolvedReferences
18
17
  import boto3
19
18
  except ModuleNotFoundError:
20
19
  raise EnvironmentError(
@@ -30,7 +29,7 @@ class AWSClient:
30
29
  self.secret_client = session.client(service_name="secretsmanager")
31
30
  self.ssm_client = session.client(service_name="ssm")
32
31
 
33
- def get_aws_secrets(self, name: str = None) -> str | List[str] | None:
32
+ def get_aws_secrets(self, name: str | None = None) -> str | List[str] | None:
34
33
  """Get secrets from AWS secretsmanager.
35
34
 
36
35
  Args:
@@ -41,18 +40,17 @@ class AWSClient:
41
40
  Returns the value of the secret or list of all secrets' names.
42
41
  """
43
42
  if name:
44
- LOGGER.info("Retrieving the secret '%s' from AWS secrets manager", name)
45
43
  try:
46
44
  response = self.secret_client.get_secret_value(SecretId=name)
47
45
  except Exception as error:
48
- LOGGER.exception(error)
49
- return
46
+ warnings.warn(error.__str__(), category=UserWarning, stacklevel=3)
47
+ return None
50
48
  return response["SecretString"]
51
49
  paginator = self.secret_client.get_paginator("list_secrets")
52
50
  page_results = paginator.paginate().build_full_result()
53
51
  return [page["Name"] for page in page_results["SecretList"]]
54
52
 
55
- def get_aws_params(self, name: str = None) -> str | List[str] | None:
53
+ def get_aws_params(self, name: str | None = None) -> str | List[str] | None:
56
54
  """Get SSM parameters from AWS.
57
55
 
58
56
  Args:
@@ -63,12 +61,11 @@ class AWSClient:
63
61
  Returns the value of the parameter or list of all parameter names.
64
62
  """
65
63
  if name:
66
- LOGGER.info("Retrieving the parameter '%s' from AWS parameter store", name)
67
64
  try:
68
65
  response = self.ssm_client.get_parameter(Name=name, WithDecryption=True)
69
66
  except Exception as error:
70
- LOGGER.exception(error)
71
- return
67
+ warnings.warn(error.__str__(), category=UserWarning, stacklevel=3)
68
+ return None
72
69
  return response["Parameter"]["Value"]
73
70
  paginator = self.ssm_client.get_paginator("describe_parameters")
74
71
  page_results = paginator.paginate().build_full_result()
@@ -1,35 +1,38 @@
1
1
  import os
2
+ from enum import Enum
3
+ from typing import NoReturn
2
4
 
3
5
  import dotenv
4
6
  import requests
5
7
  from cryptography.fernet import Fernet
8
+ from packaging import version
6
9
 
7
- from vaultapi.aws import AWSClient
8
- from vaultapi.exceptions import VaultAPIClientError
9
- from vaultapi.util import urljoin
10
+ from vaultapi_client.aws import AWSClient
11
+ from vaultapi_client.exceptions import VaultAPIClientError, VaultAPIServerError
12
+ from vaultapi_client.util import urljoin
10
13
 
11
14
  env_file = os.environ.get("ENV_FILE") or os.environ.get("env_file") or ".env"
12
15
  dotenv.load_dotenv(env_file)
16
+ MINIMUM_SERVER_VERSION = version.parse("0.6.0")
13
17
 
14
18
 
15
- class EndpointMapping:
19
+ class EndpointMapping(Enum):
16
20
  """Enum like function to get all endpoint names to avoid hard coding.
17
21
 
18
22
  >>> EndpointMapping
19
23
 
20
24
  """
21
25
 
22
- health: str = "/health"
23
- get_table: str = "/get-table"
24
- get_secret: str = "/get-secret"
25
- put_secret: str = "/put-secret"
26
- list_tables: str = "/list-tables"
27
- create_table: str = "/create-table"
28
- delete_table: str = "/delete-table"
29
- delete_secret: str = "/delete-secret"
30
-
31
-
32
- server_map = EndpointMapping()
26
+ health = "/health"
27
+ version = "/version"
28
+ get_table = "/get-table"
29
+ get_secret = "/get-secret"
30
+ put_secret = "/put-secret"
31
+ list_tables = "/list-tables"
32
+ create_table = "/create-table"
33
+ rename_table = "/rename-table"
34
+ delete_table = "/delete-table"
35
+ delete_secret = "/delete-secret"
33
36
 
34
37
 
35
38
  class EnvConfig:
@@ -39,19 +42,56 @@ class EnvConfig:
39
42
 
40
43
  """
41
44
 
42
- def __init__(self, **kwargs):
45
+ def __init__(
46
+ self,
47
+ vault_server: str,
48
+ vault_apikey: str,
49
+ vault_secret: str,
50
+ vault_transit_key_length: int,
51
+ vault_transit_time_bucket: int,
52
+ ) -> None:
43
53
  """Instantiates the env config."""
44
- self.vault_server: str = kwargs.get("vault_server")
45
- self.vault_apikey: str = kwargs.get("vault_apikey")
46
- self.vault_secret: str = kwargs.get("vault_secret")
47
- self.transit_time_bucket: int = int(kwargs.get("vault_transit_time_bucket"))
48
- self.transit_key_length: int = int(kwargs.get("vault_transit_key_length"))
54
+ self.vault_server: str = vault_server
55
+ self.vault_apikey: str = vault_apikey
56
+ self.vault_secret: str = vault_secret
57
+ self.transit_key_length: int = int(vault_transit_key_length)
58
+ self.transit_time_bucket: int = int(vault_transit_time_bucket)
49
59
  self.__assert__()
50
60
 
51
- def __assert__(self):
61
+ def server_check(self, endpoint: EndpointMapping) -> None | NoReturn:
62
+ """Checks if the server is reachable and matches the required version.
63
+
64
+ Args:
65
+ endpoint: Endpoint to check.
66
+ """
67
+ try:
68
+ response = requests.get(
69
+ url=urljoin(self.vault_server, endpoint), timeout=(1, 1)
70
+ )
71
+ response.raise_for_status()
72
+ except requests.RequestException as error:
73
+ context = (
74
+ error.response.text
75
+ if (error.response and error.response.text)
76
+ else str(error.__context__)
77
+ )
78
+ raise VaultAPIServerError(
79
+ message=context,
80
+ status_code=getattr(error.response, "status_code", None),
81
+ )
82
+ if endpoint == EndpointMapping.version:
83
+ # Make sure server_version is more than MINIMUM_SERVER_VERSION
84
+ server_version = version.parse(response.json())
85
+ if server_version < MINIMUM_SERVER_VERSION:
86
+ raise VaultAPIServerError(
87
+ f"Server version [{server_version}] is below the minimum required version "
88
+ f"[{MINIMUM_SERVER_VERSION}]. Please upgrade the server [OR] use the client version <=0.2.0."
89
+ )
90
+
91
+ def __assert__(self) -> None | NoReturn:
52
92
  """Run assertions for server config."""
53
- response = requests.get(url=urljoin(self.vault_server, server_map.health))
54
- assert response.ok, response.text
93
+ self.server_check(EndpointMapping.health)
94
+ self.server_check(EndpointMapping.version)
55
95
  try:
56
96
  assert self.transit_key_length in (16, 24, 32)
57
97
  except AssertionError:
@@ -82,7 +122,7 @@ def getenv(*args, default: str = None) -> str:
82
122
  return default
83
123
 
84
124
 
85
- def resolve_secrets(try_aws: bool):
125
+ def resolve_secrets(try_aws: bool) -> EnvConfig | NoReturn:
86
126
  """Tries to retrieve the required secret from environment variable or AWS parameter or the AWS secrets manager."""
87
127
  base_env_vars = dict(
88
128
  vault_server=getenv("vault_server", "server"),
@@ -0,0 +1,20 @@
1
+ class VaultAPIError(BaseException):
2
+ """Base error class for VaultAPI."""
3
+
4
+
5
+ class VaultAPIClientError(VaultAPIError):
6
+ """Subclass for client error."""
7
+
8
+
9
+ class InvalidCipherText(VaultAPIClientError):
10
+ """Subclass for invalid tag."""
11
+
12
+
13
+ class VaultAPIServerError(VaultAPIClientError):
14
+ """Subclass for errors when connecting to the server."""
15
+
16
+ def __init__(self, message: str, status_code: int | None = None):
17
+ """Super class to handle custom error message and status code."""
18
+ self.message = message
19
+ self.status_code = status_code
20
+ super().__init__(message)
@@ -1,23 +1,11 @@
1
1
  import os
2
- from typing import Any, Dict, List
2
+ from typing import Dict, List
3
3
 
4
4
  import dotenv
5
- import requests
6
5
 
7
- from vaultapi.aws import LOGGER
8
- from vaultapi.config import getenv, resolve_secrets, server_map
9
- from vaultapi.transit import TransitShield
10
- from vaultapi.util import urljoin
11
-
12
-
13
- def process_response(response: requests.Response) -> Any:
14
- """Asserts on the response code, and returns the response detail.
15
-
16
- Args:
17
- response: Takes the Response object as an argument.
18
- """
19
- assert response.ok, response.text
20
- return response.json()["detail"]
6
+ from vaultapi_client.config import EndpointMapping, getenv, resolve_secrets
7
+ from vaultapi_client.session import Session
8
+ from vaultapi_client.transit import TransitShield
21
9
 
22
10
 
23
11
  class VaultAPIClient:
@@ -31,28 +19,25 @@ class VaultAPIClient:
31
19
  """Instantiates the VaultAPIClient object."""
32
20
  self.env_config = resolve_secrets(aws)
33
21
  self.transit_shield = TransitShield(self.env_config)
34
- self.SESSION = requests.Session()
35
- self.SESSION.headers = {
36
- "accept": "application/json",
37
- "Authorization": f"Bearer {self.env_config.vault_apikey}",
38
- }
22
+ self.SESSION = Session(self.env_config)
39
23
 
40
- def _get_cipher(self, server_url: str, query_params: Dict[str, str]) -> str:
24
+ def _get_cipher(
25
+ self, endpoint: EndpointMapping, query_params: Dict[str, str]
26
+ ) -> str:
41
27
  """Get ciphertext from the server.
42
28
 
43
29
  Args:
44
- server_url: Server URL to make the request to.
30
+ endpoint: API endpoint to request.
45
31
  query_params: Query parameters to send with the request.
46
32
 
47
33
  Returns:
48
34
  str:
49
35
  Returns the ciphertext.
50
36
  """
51
- response = self.SESSION.get(
52
- server_url,
37
+ return self.SESSION.get(
38
+ endpoint,
53
39
  params=query_params,
54
40
  )
55
- return process_response(response)
56
41
 
57
42
  def dotenv_to_table(self, table_name: str, dotenv_file: str) -> Dict[str, str]:
58
43
  """Store all the env vars from a .env file into the database.
@@ -65,9 +50,31 @@ class VaultAPIClient:
65
50
  assert os.path.isfile(dotenv_file)
66
51
  except AssertionError:
67
52
  raise FileNotFoundError(dotenv_file)
68
- env_vars = dotenv.dotenv_values(dotenv_file)
53
+ env_vars = {
54
+ k: v for k, v in dotenv.dotenv_values(dotenv_file).items() if v is not None
55
+ }
69
56
  return self.update_secret(secrets=env_vars, table_name=table_name)
70
57
 
58
+ def table_to_env(self, table_name: str, dotenv_file: str | None = None) -> None:
59
+ """Retrieve all secrets from the database and store them as env vars.
60
+
61
+ Args:
62
+ table_name: Vault table name,
63
+ dotenv_file: Dot env filename to store secrets in addition to env vars.
64
+ """
65
+ if dotenv_file:
66
+ try:
67
+ assert os.path.isfile(dotenv_file)
68
+ except AssertionError:
69
+ raise FileNotFoundError(dotenv_file)
70
+ secrets = self.get_table(table_name)
71
+ for key, value in secrets.items():
72
+ os.environ[key] = value
73
+ if dotenv_file:
74
+ dotenv.set_key(
75
+ dotenv_path=dotenv_file, key_to_set=key, value_to_set=value
76
+ )
77
+
71
78
  def update_secret(self, secrets: Dict[str, str], table_name: str) -> Dict[str, str]:
72
79
  """Update or create secrets in the vault.
73
80
 
@@ -79,15 +86,13 @@ class VaultAPIClient:
79
86
  Dict[str, str]:
80
87
  Returns the server response.
81
88
  """
82
- url = urljoin(self.env_config.vault_server, server_map.put_secret)
83
- response = self.SESSION.put(
84
- url,
89
+ return self.SESSION.put(
90
+ EndpointMapping.put_secret,
85
91
  json={
86
92
  "secrets": self.transit_shield.encrypt(payload=secrets),
87
93
  "table_name": table_name,
88
94
  },
89
95
  )
90
- return process_response(response)
91
96
 
92
97
  def delete_secret(self, key: str, table_name: str) -> Dict[str, str]:
93
98
  """Delete a secret from the vault.
@@ -100,15 +105,13 @@ class VaultAPIClient:
100
105
  Dict[str, str]:
101
106
  Returns the server response.
102
107
  """
103
- url = urljoin(self.env_config.vault_server, server_map.delete_secret)
104
- response = self.SESSION.delete(
105
- url,
108
+ return self.SESSION.delete(
109
+ EndpointMapping.delete_secret,
106
110
  json={
107
111
  "key": key,
108
112
  "table_name": table_name,
109
113
  },
110
114
  )
111
- return process_response(response)
112
115
 
113
116
  def list_tables(self) -> List[str]:
114
117
  """List all available tables.
@@ -117,12 +120,10 @@ class VaultAPIClient:
117
120
  List[str]:
118
121
  Returns the available table names as a list of strings.
119
122
  """
120
- url = urljoin(self.env_config.vault_server, server_map.list_tables)
121
- response = self.SESSION.get(url)
122
- return process_response(response)
123
+ return self.SESSION.get(EndpointMapping.list_tables)
123
124
 
124
125
  def create_table(self, table_name: str) -> Dict[str, str]:
125
- """Creates a new table in the vault.
126
+ """Creates a new table in the vault database.
126
127
 
127
128
  Args:
128
129
  table_name: Table name.
@@ -131,9 +132,26 @@ class VaultAPIClient:
131
132
  Dict[str, str]:
132
133
  Returns the server response.
133
134
  """
134
- url = urljoin(self.env_config.vault_server, server_map.create_table)
135
- response = self.SESSION.post(url, params={"table_name": table_name})
136
- return process_response(response)
135
+ return self.SESSION.post(
136
+ EndpointMapping.create_table, params={"table_name": table_name}
137
+ )
138
+
139
+ def rename_table(self, table_name: str, new_name: str) -> str:
140
+ """Renames a table in the vault database.
141
+
142
+ Args:
143
+ table_name: Table name to rename.
144
+ new_name: New table name.
145
+
146
+ Returns:
147
+ str:
148
+ Returns the server response.
149
+ """
150
+ return self.SESSION.patch(
151
+ EndpointMapping.rename_table,
152
+ params={"table_name": table_name},
153
+ json=dict(new_name=new_name),
154
+ )
137
155
 
138
156
  def delete_table(self, table_name: str) -> Dict[str, str]:
139
157
  """Deletes an existing table.
@@ -145,9 +163,9 @@ class VaultAPIClient:
145
163
  Dict[str, str]:
146
164
  Returns the server response.
147
165
  """
148
- url = urljoin(self.env_config.vault_server, server_map.delete_table)
149
- response = self.SESSION.delete(url, params={"table_name": table_name})
150
- return process_response(response)
166
+ return self.SESSION.delete(
167
+ EndpointMapping.delete_table, params={"table_name": table_name}
168
+ )
151
169
 
152
170
  def get_secret(self, key: str, table_name: str) -> Dict[str, str]:
153
171
  """Retrieves multiple secrets from a table.
@@ -160,9 +178,10 @@ class VaultAPIClient:
160
178
  Dict[str, str]:
161
179
  Returns a dictionary of decrypted values.
162
180
  """
163
- url = urljoin(self.env_config.vault_server, server_map.get_secret)
164
- cipher_text = self._get_cipher(url, {"key": key, "table_name": table_name})
165
- return self.transit_shield.decrypt(cipher_text)
181
+ cipher_text = self._get_cipher(
182
+ EndpointMapping.get_secret, {"key": key, "table_name": table_name}
183
+ )
184
+ return self.transit_shield.decrypt(ciphertext=cipher_text)
166
185
 
167
186
  def get_table(self, table_name: str) -> Dict[str, str]:
168
187
  """Retrieves all the secrets stored in a table.
@@ -174,14 +193,15 @@ class VaultAPIClient:
174
193
  Dict[str, str]:
175
194
  Returns a dictionary of decrypted values.
176
195
  """
177
- url = urljoin(self.env_config.vault_server, server_map.get_table)
178
- cipher_text = self._get_cipher(url, {"table_name": table_name})
179
- return self.transit_shield.decrypt(cipher_text)
196
+ cipher_text = self._get_cipher(
197
+ EndpointMapping.get_table, {"table_name": table_name}
198
+ )
199
+ return self.transit_shield.decrypt(ciphertext=cipher_text)
180
200
 
181
201
  def decrypt(
182
202
  self,
183
203
  table: str,
184
- get_secret: str = None,
204
+ get_secret: str | None = None,
185
205
  ) -> Dict[str, str] | str:
186
206
  """Decrypt function.
187
207
 
@@ -194,12 +214,13 @@ class VaultAPIClient:
194
214
  Returns a dictionary of decrypted values.
195
215
  """
196
216
  if not table:
197
- LOGGER.warning("No table_name was given")
198
- return {}
217
+ raise ValueError("Table name is required to decrypt.")
199
218
  params = dict(table_name=table)
200
219
  if get_secret:
201
- url = urljoin(self.env_config.vault_server, server_map.get_secret)
220
+ endpoint = EndpointMapping.get_secret
202
221
  params["key"] = get_secret
203
222
  else:
204
- url = urljoin(self.env_config.vault_server, server_map.get_table)
205
- return self.transit_shield.decrypt(self._get_cipher(url, params))
223
+ endpoint = EndpointMapping.get_table
224
+ return self.transit_shield.decrypt(
225
+ self._get_cipher(endpoint=EndpointMapping(endpoint), query_params=params)
226
+ )
@@ -0,0 +1,159 @@
1
+ import hashlib
2
+ import hmac
3
+ import time
4
+ from typing import Any, Callable, Dict, NoReturn
5
+
6
+ import requests
7
+
8
+ from vaultapi_client.config import EndpointMapping, EnvConfig
9
+ from vaultapi_client.exceptions import VaultAPIServerError
10
+ from vaultapi_client.util import urljoin
11
+
12
+
13
+ def _request_error(error: requests.RequestException) -> NoReturn:
14
+ """Uses requests module base exception to raise a custom server error."""
15
+ raise VaultAPIServerError(
16
+ f"Request failed: {error}",
17
+ status_code=getattr(error.response, "status_code", None),
18
+ )
19
+
20
+
21
+ def generate_bearer_token(token: str) -> str:
22
+ """Generate an authentication header value for API requests.
23
+
24
+ Args:
25
+ token: Shared API token used as the HMAC secret key.
26
+
27
+ See Also:
28
+ - The generated header contains:
29
+ - A Unix timestamp in seconds.
30
+ - An HMAC-SHA512 signature computed using the API token as the secret key and the timestamp as the message.
31
+ - | The server can validate the signature and ensure the timestamp is within an acceptable age window,
32
+ | to reduce replay attacks.
33
+
34
+ Example:
35
+ Authorization: Signature=<signature>,timestamp=<timestamp>
36
+
37
+ Returns:
38
+ str:
39
+ Authentication header value containing the signature and timestamp.
40
+ """
41
+ timestamp = str(int(time.time()))
42
+ signature = hmac.new(
43
+ token.encode("utf-8"),
44
+ timestamp.encode("utf-8"),
45
+ hashlib.sha512,
46
+ ).hexdigest()
47
+ return f"Signature={signature},timestamp={timestamp}"
48
+
49
+
50
+ def process_response(response: requests.Response) -> Any:
51
+ """Asserts on the response code, and returns the response detail.
52
+
53
+ Args:
54
+ response: Takes the Response object as an argument.
55
+ """
56
+ try:
57
+ response.raise_for_status()
58
+ return response.json()["detail"]
59
+ except requests.RequestException as error:
60
+ _request_error(error)
61
+ except requests.JSONDecodeError as error:
62
+ raise VaultAPIServerError(
63
+ message=f"Invalid JSON response from the server: {error}"
64
+ )
65
+
66
+
67
+ class Session:
68
+ """Custom requests session with centralized error handling, headers infusion, and response processing.
69
+
70
+ >>> Session
71
+
72
+ """
73
+
74
+ def __init__(self, env_config: EnvConfig) -> None:
75
+ self.apikey = env_config.vault_apikey
76
+ self.secret = env_config.vault_secret
77
+ self.server = env_config.vault_server
78
+ self.headers: Callable = lambda token: {
79
+ "Accept": "application/json",
80
+ "Authorization": f"Bearer {generate_bearer_token(token)}",
81
+ }
82
+
83
+ def request(
84
+ self,
85
+ method: str,
86
+ endpoint: EndpointMapping,
87
+ params: Dict[str, Any] | None = None,
88
+ json: Dict[str, Any] | None = None,
89
+ ) -> Any:
90
+ """Intercepts all HTTP requests and applies centralized error handling.
91
+
92
+ Args:
93
+ method: HTTP method to make a request.
94
+ endpoint: Endpoint to make a request.
95
+ params: Query parameters to pass to the request.
96
+ json: JSON data to pass to the request.
97
+
98
+ Returns:
99
+ Any:
100
+ Response data from the server.
101
+ """
102
+ url = urljoin(self.server, endpoint)
103
+ if method in ("GET", "POST"):
104
+ headers = self.headers(token=self.apikey)
105
+ else:
106
+ headers = self.headers(token=f"{self.apikey}.{self.secret}")
107
+ try:
108
+ response = requests.request(
109
+ method=method, url=url, headers=headers, params=params, json=json
110
+ )
111
+ response.raise_for_status()
112
+ return process_response(response)
113
+ except requests.exceptions.RequestException as error:
114
+ _request_error(error)
115
+
116
+ def get(
117
+ self,
118
+ endpoint: EndpointMapping,
119
+ params: Dict[str, Any] | None = None,
120
+ json: Dict[str, Any] | None = None,
121
+ ) -> Any:
122
+ """Make GET request to the server and process the response."""
123
+ return self.request("GET", endpoint, params=params, json=json)
124
+
125
+ def put(
126
+ self,
127
+ endpoint: EndpointMapping,
128
+ params: Dict[str, Any] | None = None,
129
+ json: Dict[str, Any] | None = None,
130
+ ) -> Any:
131
+ """Make PUT request to the server and process the response."""
132
+ return self.request("PUT", endpoint, params=params, json=json)
133
+
134
+ def post(
135
+ self,
136
+ endpoint: EndpointMapping,
137
+ params: Dict[str, Any] | None = None,
138
+ json: Dict[str, Any] | None = None,
139
+ ) -> Any:
140
+ """Make POST request to the server and process the response."""
141
+ return self.request("POST", endpoint, params=params, json=json)
142
+
143
+ def patch(
144
+ self,
145
+ endpoint: EndpointMapping,
146
+ params: Dict[str, Any] | None = None,
147
+ json: Dict[str, Any] | None = None,
148
+ ) -> Any:
149
+ """Make PATCH request to the server and process the response."""
150
+ return self.request("PATCH", endpoint, params=params, json=json)
151
+
152
+ def delete(
153
+ self,
154
+ endpoint: EndpointMapping,
155
+ params: Dict[str, Any] | None = None,
156
+ json: Dict[str, Any] | None = None,
157
+ ) -> Any:
158
+ """Make DELETE request to the server and process the response."""
159
+ return self.request("DELETE", endpoint, params=params, json=json)
@@ -5,9 +5,11 @@ import secrets
5
5
  import time
6
6
  from typing import Any, ByteString, Dict
7
7
 
8
+ from cryptography.exceptions import InvalidTag
8
9
  from cryptography.hazmat.primitives.ciphers.aead import AESGCM
9
10
 
10
- from vaultapi.config import EnvConfig
11
+ from vaultapi_client.config import EnvConfig
12
+ from vaultapi_client.exceptions import InvalidCipherText
11
13
 
12
14
 
13
15
  class TransitShield:
@@ -83,5 +85,8 @@ class TransitShield:
83
85
  aes_key = hash_object.digest()[: self.env_config.transit_key_length]
84
86
  if isinstance(ciphertext, str):
85
87
  ciphertext = base64.b64decode(ciphertext)
86
- decrypted = AESGCM(aes_key).decrypt(ciphertext[:12], ciphertext[12:], b"")
88
+ try:
89
+ decrypted = AESGCM(aes_key).decrypt(ciphertext[:12], ciphertext[12:], b"")
90
+ except InvalidTag:
91
+ raise InvalidCipherText("Ciphertext has either expired or invalid!")
87
92
  return json.loads(decrypted)
@@ -0,0 +1,19 @@
1
+ from enum import Enum
2
+ from typing import Any
3
+
4
+
5
+ def normalize(segment: Any) -> str:
6
+ """Normalize the URL."""
7
+ if isinstance(segment, Enum):
8
+ segment = segment.value
9
+ return str(segment).strip("/")
10
+
11
+
12
+ def urljoin(*args) -> str:
13
+ """Joins given arguments into a url.
14
+
15
+ Returns:
16
+ str:
17
+ Joined url.
18
+ """
19
+ return "/".join(map(lambda x: normalize(x), args))
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: VaultAPI-Client
3
- Version: 0.1.2
3
+ Version: 0.3.0
4
4
  Summary: Client application for VaultAPI Server
5
5
  Author-email: Vignesh Rao <svignesh1793@gmail.com>
6
6
  License: MIT License
@@ -40,6 +40,7 @@ Provides-Extra: dev
40
40
  Requires-Dist: pre-commit; extra == "dev"
41
41
  Provides-Extra: aws
42
42
  Requires-Dist: boto3==1.36.*; extra == "aws"
43
+ Dynamic: license-file
43
44
 
44
45
  # VaultAPI-Client-python
45
46
  Client application for VaultAPI Server
@@ -72,12 +73,12 @@ python -m pip install vaultapi
72
73
  ```
73
74
 
74
75
  **Initiate - IDE**
75
- ```python
76
- import vaultapi
77
76
 
77
+ ```python
78
+ import vaultapi_client
78
79
 
79
80
  if __name__ == '__main__':
80
- vaultapi_client = vaultapi.VaultAPIClient()
81
+ vaultapi_client = vaultapi_client.VaultAPIClient()
81
82
  vaultapi_client.get_secret(key="mykey", table_name="mytable")
82
83
  ```
83
84
 
@@ -99,6 +100,8 @@ vaultapi --table mytable
99
100
  - **APIKEY** - API Key for authentication.
100
101
  - **SECRET** - Secret key used for transit encryption.
101
102
  - **VAULT_SERVER** - VaultAPI server URL.
103
+ - **TRANSIT_KEY_LENGTH** - AES key length used for transit encryption. Defaults to `32`
104
+ - **TRANSIT_TIME_BUCKET** - Interval for which the transit epoch will remain constant. Defaults to `60`
102
105
 
103
106
  ## Coding Standards
104
107
  Docstring format: [`Google`][google-docs] <br>
@@ -0,0 +1,14 @@
1
+ vaultapi_client/__init__.py,sha256=EpHR8g5-TpGoV-6fo_5PEy43sJaKdy8wMrCl_F0hrsQ,1308
2
+ vaultapi_client/aws.py,sha256=1y-hSg76K5oc1wqbOBVuGhVZV3Ckt2ttTEdL5O0i4yo,2694
3
+ vaultapi_client/config.py,sha256=Z8dGcxk5FKuQj0HpAe8Wq_bwEcuT8TamrXdrbNadFVg,5456
4
+ vaultapi_client/exceptions.py,sha256=hpZxtnSa1SHNDaTJwYdwXFuUkzi2ZdB23apKrKOOUzk,602
5
+ vaultapi_client/main.py,sha256=6joJFNJRFNM1Te2ixzGdzB9iGbrgonIvYZeAUQ60ncc,6956
6
+ vaultapi_client/session.py,sha256=rh2aazo-8gjFIIiSohaZdmiUiZpjWxMQZqCVxl09uqo,5287
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.0.dist-info/licenses/LICENSE,sha256=_sOIKJWdD2o1WwwDIwYB2qTP2nlSWqT5Tyg9jr1Xa4w,1070
10
+ vaultapi_client-0.3.0.dist-info/METADATA,sha256=t7T2o9UBD37po-Sa6UArzhY1Tt5X4_nJadSdocFTsOw,5911
11
+ vaultapi_client-0.3.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
12
+ vaultapi_client-0.3.0.dist-info/entry_points.txt,sha256=P3CaBVJ9RvRJy7xvd9A4o3HYEfoM4wStZcQ8L6837NE,64
13
+ vaultapi_client-0.3.0.dist-info/top_level.txt,sha256=J0qf2Tuj66RGC_VTF9f3Ce137qPqL8n0V21-1xwp1nw,16
14
+ vaultapi_client-0.3.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.8.0)
2
+ Generator: setuptools (82.0.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ vaultapi-client = vaultapi_client:commandline
@@ -0,0 +1 @@
1
+ vaultapi_client
@@ -1,13 +0,0 @@
1
- vaultapi/__init__.py,sha256=4Qrvj-J-MKCdBly6xVIxdVV1ueDp30hwqSfiBWjab8I,1507
2
- vaultapi/aws.py,sha256=FpFDP9jDmiXek_zzgVMQ5FZoqzyh0HIR4vqnry0Ap-U,2722
3
- vaultapi/config.py,sha256=06PBsWd5RxehK1BvFfDCzm96Dic1RK-qOp9rCRpZC6k,3873
4
- vaultapi/exceptions.py,sha256=KUTAshsxk7ewcZsjGibGP0076L0QrLzuADRnrv30FK4,90
5
- vaultapi/main.py,sha256=zcVAPOzKVdB0eDqbxr8GNCNBXfC3L2LSEc2Jh8lim1o,6593
6
- vaultapi/transit.py,sha256=VaP4bSryulgpMn4tXcGnEUCJ1AAZYzHw4EwYhdIF4XE,2878
7
- vaultapi/util.py,sha256=REd8SIycx9q6AQoaHG8UfkbWfPHUtSSz7E3ngWRc4WM,262
8
- VaultAPI_Client-0.1.2.dist-info/LICENSE,sha256=_sOIKJWdD2o1WwwDIwYB2qTP2nlSWqT5Tyg9jr1Xa4w,1070
9
- VaultAPI_Client-0.1.2.dist-info/METADATA,sha256=3sWt5TXIHhgB0uWooRgHbYVK3YJL8u6e_a0QCSqi3ko,5683
10
- VaultAPI_Client-0.1.2.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
11
- VaultAPI_Client-0.1.2.dist-info/entry_points.txt,sha256=MKe6sVDEAHC3YBZoYWLMvgwupRE_QJ_iHK0umfgGOzs,50
12
- VaultAPI_Client-0.1.2.dist-info/top_level.txt,sha256=17jtNvOOJqazNgWn2ZTmJqfQau5jan_xF82NAJOINQg,9
13
- VaultAPI_Client-0.1.2.dist-info/RECORD,,
@@ -1,2 +0,0 @@
1
- [console_scripts]
2
- vaultapi = vaultapi:commandline
@@ -1 +0,0 @@
1
- vaultapi
vaultapi/__init__.py DELETED
@@ -1,47 +0,0 @@
1
- import argparse
2
- import json
3
-
4
- from .exceptions import VaultAPIClientError
5
- from .main import LOGGER, VaultAPIClient
6
-
7
- version = "0.1.2"
8
-
9
-
10
- def commandline():
11
- """Entrypoint for vaultapi commandline."""
12
- parser = argparse.ArgumentParser()
13
- parser.add_argument(
14
- "-V", "--version", action="store_true", help="Show version information"
15
- )
16
- parser.add_argument(
17
- "--aws",
18
- action="store_true",
19
- help="Flag to use AWS parameter store and secrets manager to retrieve the server credentials. "
20
- "Requires 'pip install VaultAPI-Client[aws]'",
21
- )
22
- parser.add_argument(
23
- "--get-secret", help="Retrieve a secret from Vault using the secret key"
24
- )
25
- parser.add_argument(
26
- "--get-secrets",
27
- help="Retrieve multiple secrets from Vault with a comma separated list of keys",
28
- )
29
- parser.add_argument(
30
- "--table",
31
- help="Table name where the secrets are stored. "
32
- "Can be used with --get-secret/--get-secrets or itself to retrieve all the secrets in a table",
33
- )
34
- args = parser.parse_args()
35
- if args.version:
36
- print(f"VaultAPI Client: {version}")
37
- exit(0)
38
- try:
39
- vaultapi_client = VaultAPIClient(args.aws)
40
- except VaultAPIClientError as error:
41
- LOGGER.exception(error)
42
- exit(1)
43
- kwargs = dict(args._get_kwargs())
44
- kwargs.pop("version", None)
45
- kwargs.pop("aws", None)
46
- LOGGER.debug(kwargs)
47
- print(json.dumps(vaultapi_client.decrypt(**kwargs), indent=2))
vaultapi/exceptions.py DELETED
@@ -1,2 +0,0 @@
1
- class VaultAPIClientError(Exception):
2
- """Base error class for the VaultAPI Client."""
vaultapi/util.py DELETED
@@ -1,8 +0,0 @@
1
- def urljoin(*args) -> str:
2
- """Joins given arguments into an url. Trailing but not leading slashes are stripped for each argument.
3
-
4
- Returns:
5
- str:
6
- Joined url.
7
- """
8
- return "/".join(map(lambda x: str(x).rstrip("/").lstrip("/"), args))