VaultAPI-Client 0.0.1__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-0.0.1.dist-info/LICENSE +21 -0
- VaultAPI_Client-0.0.1.dist-info/METADATA +151 -0
- VaultAPI_Client-0.0.1.dist-info/RECORD +8 -0
- VaultAPI_Client-0.0.1.dist-info/WHEEL +5 -0
- VaultAPI_Client-0.0.1.dist-info/entry_points.txt +2 -0
- VaultAPI_Client-0.0.1.dist-info/top_level.txt +1 -0
- vaultapi/__init__.py +38 -0
- vaultapi/main.py +173 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 TheVickypedia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: VaultAPI-Client
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Client application for VaultAPI Server
|
|
5
|
+
Author-email: Vignesh Rao <svignesh1793@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 TheVickypedia
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/thevickypedia/VaultAPI-Client-python
|
|
29
|
+
Project-URL: Source, https://github.com/thevickypedia/VaultAPI-Client-python
|
|
30
|
+
Keywords: vaultapi,vault,fastapi,sqlite3,fernet
|
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
32
|
+
Classifier: Programming Language :: Python :: 3
|
|
33
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
34
|
+
Classifier: Operating System :: OS Independent
|
|
35
|
+
Classifier: Topic :: Security :: Cryptography
|
|
36
|
+
Requires-Python: >=3.10
|
|
37
|
+
Description-Content-Type: text/markdown
|
|
38
|
+
License-File: LICENSE
|
|
39
|
+
Provides-Extra: dev
|
|
40
|
+
Requires-Dist: pre-commit; extra == "dev"
|
|
41
|
+
|
|
42
|
+
# VaultAPI-Client-python
|
|
43
|
+
Client application for VaultAPI Server
|
|
44
|
+
|
|
45
|
+
![Python][label-pyversion]
|
|
46
|
+
|
|
47
|
+
**Platform Supported**
|
|
48
|
+
|
|
49
|
+
![Platform][label-platform]
|
|
50
|
+
|
|
51
|
+
**Deployments**
|
|
52
|
+
|
|
53
|
+
[![pypi][label-actions-pypi]][gha_pypi]
|
|
54
|
+
[![markdown][label-actions-markdown]][gha_md_valid]
|
|
55
|
+
|
|
56
|
+
[![Pypi][label-pypi]][pypi]
|
|
57
|
+
[![Pypi-format][label-pypi-format]][pypi-files]
|
|
58
|
+
[![Pypi-status][label-pypi-status]][pypi]
|
|
59
|
+
|
|
60
|
+
## Kick off
|
|
61
|
+
|
|
62
|
+
**Recommendations**
|
|
63
|
+
|
|
64
|
+
- Install `python` [3.10] or [3.11]
|
|
65
|
+
- Use a dedicated [virtual environment]
|
|
66
|
+
|
|
67
|
+
**Install VaultAPI**
|
|
68
|
+
```shell
|
|
69
|
+
python -m pip install vaultapi
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Initiate - IDE**
|
|
73
|
+
```python
|
|
74
|
+
import vaultapi
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
if __name__ == '__main__':
|
|
78
|
+
vaultapi.decrypt(get_secret="mykey", table="mytable")
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Initiate - CLI**
|
|
82
|
+
```shell
|
|
83
|
+
vaultapi --table mytable
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
> Use `vaultapi --help` for usage instructions.
|
|
87
|
+
|
|
88
|
+
## Environment Variables
|
|
89
|
+
|
|
90
|
+
<details>
|
|
91
|
+
<summary><strong>Sourcing environment variables from an env file</strong></summary>
|
|
92
|
+
|
|
93
|
+
> _By default, `VaultAPI-Client` will look for a `.env` file in the current working directory._
|
|
94
|
+
</details>
|
|
95
|
+
|
|
96
|
+
**Mandatory**
|
|
97
|
+
- **APIKEY** - API Key for authentication.
|
|
98
|
+
- **VAULT_SERVER** - VaultAPI server URL.
|
|
99
|
+
|
|
100
|
+
## Coding Standards
|
|
101
|
+
Docstring format: [`Google`][google-docs] <br>
|
|
102
|
+
Styling conventions: [`PEP 8`][pep8] and [`isort`][isort]
|
|
103
|
+
|
|
104
|
+
## Linting
|
|
105
|
+
`pre-commit` will ensure linting, run pytest, generate runbook & release notes, and validate hyperlinks in ALL
|
|
106
|
+
markdown files (including Wiki pages)
|
|
107
|
+
|
|
108
|
+
**Requirement**
|
|
109
|
+
```shell
|
|
110
|
+
python -m pip install pre-commit
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**Usage**
|
|
114
|
+
```shell
|
|
115
|
+
pre-commit run --all-files
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Pypi Package
|
|
119
|
+
[![pypi-module][label-pypi-package]][pypi-repo]
|
|
120
|
+
|
|
121
|
+
[https://pypi.org/project/VaultAPI-Client/][pypi]
|
|
122
|
+
|
|
123
|
+
## License & copyright
|
|
124
|
+
|
|
125
|
+
© Vignesh Rao
|
|
126
|
+
|
|
127
|
+
Licensed under the [MIT License][license]
|
|
128
|
+
|
|
129
|
+
[label-actions-markdown]: https://github.com/thevickypedia/VaultAPI-Client-python/actions/workflows/markdown.yml/badge.svg
|
|
130
|
+
[label-pypi-package]: https://img.shields.io/badge/Pypi%20Package-VaultAPI_Client-blue?style=for-the-badge&logo=Python
|
|
131
|
+
[label-pyversion]: https://img.shields.io/badge/python-3.10%20%7C%203.11-blue
|
|
132
|
+
[label-platform]: https://img.shields.io/badge/Platform-Linux|macOS|Windows-1f425f.svg
|
|
133
|
+
[label-actions-pypi]: https://github.com/thevickypedia/VaultAPI-Client-python/actions/workflows/python-publish.yml/badge.svg
|
|
134
|
+
[label-pypi]: https://img.shields.io/pypi/v/VaultAPI-Client
|
|
135
|
+
[label-pypi-format]: https://img.shields.io/pypi/format/VaultAPI-Client
|
|
136
|
+
[label-pypi-status]: https://img.shields.io/pypi/status/VaultAPI-Client
|
|
137
|
+
|
|
138
|
+
[3.10]: https://docs.python.org/3/whatsnew/3.10.html
|
|
139
|
+
[3.11]: https://docs.python.org/3/whatsnew/3.11.html
|
|
140
|
+
[virtual environment]: https://docs.python.org/3/tutorial/venv.html
|
|
141
|
+
[release-notes]: https://github.com/thevickypedia/VaultAPI-Client-python/blob/main/release_notes.rst
|
|
142
|
+
[gha_pages]: https://github.com/thevickypedia/VaultAPI-Client-python/actions/workflows/pages/pages-build-deployment
|
|
143
|
+
[gha_pypi]: https://github.com/thevickypedia/VaultAPI-Client-python/actions/workflows/python-publish.yml
|
|
144
|
+
[gha_md_valid]: https://github.com/thevickypedia/VaultAPI-Client-python/actions/workflows/markdown.yml
|
|
145
|
+
[google-docs]: https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings
|
|
146
|
+
[pep8]: https://www.python.org/dev/peps/pep-0008/
|
|
147
|
+
[isort]: https://pycqa.github.io/isort/
|
|
148
|
+
[pypi]: https://pypi.org/project/VaultAPI-Client
|
|
149
|
+
[pypi-files]: https://pypi.org/project/VaultAPI-Client/#files
|
|
150
|
+
[pypi-repo]: https://packaging.python.org/tutorials/packaging-projects/
|
|
151
|
+
[license]: https://github.com/thevickypedia/VaultAPI-Client-python/blob/main/LICENSE
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
vaultapi/__init__.py,sha256=OK8cd0ecZPp6zy9eFsfJAuedoqu0BkeRnqKUH61dn80,1220
|
|
2
|
+
vaultapi/main.py,sha256=JWUloacXr1EDXFIQCGAOhTN4LKRq4GH4tCNHTFUeMXs,4732
|
|
3
|
+
VaultAPI_Client-0.0.1.dist-info/LICENSE,sha256=_sOIKJWdD2o1WwwDIwYB2qTP2nlSWqT5Tyg9jr1Xa4w,1070
|
|
4
|
+
VaultAPI_Client-0.0.1.dist-info/METADATA,sha256=aT5fRttW98DnZ23yHDSd6pNGc8DPfAbm3rgjKaBCJng,5521
|
|
5
|
+
VaultAPI_Client-0.0.1.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
|
6
|
+
VaultAPI_Client-0.0.1.dist-info/entry_points.txt,sha256=MKe6sVDEAHC3YBZoYWLMvgwupRE_QJ_iHK0umfgGOzs,50
|
|
7
|
+
VaultAPI_Client-0.0.1.dist-info/top_level.txt,sha256=17jtNvOOJqazNgWn2ZTmJqfQau5jan_xF82NAJOINQg,9
|
|
8
|
+
VaultAPI_Client-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
vaultapi
|
vaultapi/__init__.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import json
|
|
3
|
+
|
|
4
|
+
from .main import decrypt # noqa: F401
|
|
5
|
+
|
|
6
|
+
version = "0.0.1"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def commandline():
|
|
10
|
+
"""Entrypoint for vaultapi commandline."""
|
|
11
|
+
parser = argparse.ArgumentParser()
|
|
12
|
+
parser.add_argument(
|
|
13
|
+
"-V", "--version", action="store_true", help="Show version information"
|
|
14
|
+
)
|
|
15
|
+
parser.add_argument("--decipher", help="Decipher a given ciphertext")
|
|
16
|
+
parser.add_argument(
|
|
17
|
+
"--get-secret", help="Retrieve a secret from Vault using the secret key"
|
|
18
|
+
)
|
|
19
|
+
parser.add_argument(
|
|
20
|
+
"--get-secrets",
|
|
21
|
+
help="Retrieve multiple secrets from Vault with a comma separated list of keys",
|
|
22
|
+
)
|
|
23
|
+
parser.add_argument(
|
|
24
|
+
"--table",
|
|
25
|
+
help="Table name where the secrets are stored. "
|
|
26
|
+
"Can be used with --get-secret/--get-secrets or itself to retrieve all the secrets in a table",
|
|
27
|
+
)
|
|
28
|
+
args = parser.parse_args()
|
|
29
|
+
if args.version:
|
|
30
|
+
print(f"VaultAPI Client: {version}")
|
|
31
|
+
exit(0)
|
|
32
|
+
if args.decipher:
|
|
33
|
+
print(json.dumps(decrypt(cipher=args.decipher), indent=2))
|
|
34
|
+
exit(0)
|
|
35
|
+
kwargs = dict(args._get_kwargs())
|
|
36
|
+
kwargs.pop("version", None)
|
|
37
|
+
kwargs.pop("decipher", None)
|
|
38
|
+
print(json.dumps(decrypt(**kwargs), indent=2))
|
vaultapi/main.py
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import hashlib
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import time
|
|
6
|
+
from typing import Any, ByteString, Dict
|
|
7
|
+
|
|
8
|
+
import dotenv
|
|
9
|
+
import requests
|
|
10
|
+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
11
|
+
|
|
12
|
+
env_file = os.environ.get("ENV_FILE") or os.environ.get("env_file") or ".env"
|
|
13
|
+
dotenv.load_dotenv(env_file)
|
|
14
|
+
|
|
15
|
+
VAULT_SERVER = os.environ.get("VAULT_SERVER") or os.environ.get("vault_server")
|
|
16
|
+
APIKEY = os.environ.get("APIKEY") or os.environ.get("apikey")
|
|
17
|
+
|
|
18
|
+
TRANSIT_TIME_BUCKET = os.environ.get("TRANSIT_TIME_BUCKET", 60)
|
|
19
|
+
TRANSIT_KEY_LENGTH = os.environ.get("TRANSIT_KEY_LENGTH", 60)
|
|
20
|
+
|
|
21
|
+
SESSION = requests.Session()
|
|
22
|
+
SESSION.headers = {
|
|
23
|
+
"accept": "application/json",
|
|
24
|
+
"Authorization": f"Bearer {APIKEY}",
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def urljoin(*args) -> str:
|
|
29
|
+
"""Joins given arguments into an url. Trailing but not leading slashes are stripped for each argument.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
str:
|
|
33
|
+
Joined url.
|
|
34
|
+
"""
|
|
35
|
+
return "/".join(map(lambda x: str(x).rstrip("/").lstrip("/"), args))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def transit_decrypt(ciphertext: str | ByteString) -> Dict[str, Any]:
|
|
39
|
+
"""Decrypt transit encrypted payload.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
ciphertext: Ciphertext to decrypt.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
Dict[str, str]:
|
|
46
|
+
Returns a dictionary of decrypted values.
|
|
47
|
+
"""
|
|
48
|
+
epoch = int(time.time()) // TRANSIT_TIME_BUCKET
|
|
49
|
+
hash_object = hashlib.sha256(f"{epoch}.{APIKEY}".encode())
|
|
50
|
+
aes_key = hash_object.digest()[:TRANSIT_KEY_LENGTH]
|
|
51
|
+
if isinstance(ciphertext, str):
|
|
52
|
+
ciphertext = base64.b64decode(ciphertext)
|
|
53
|
+
decrypted = AESGCM(aes_key).decrypt(ciphertext[:12], ciphertext[12:], b"")
|
|
54
|
+
return json.loads(decrypted)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def get_cipher(server_url: str, query_params: Dict[str, str]) -> str:
|
|
58
|
+
"""Get ciphertext from the server.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
server_url: Server URL to make the request to.
|
|
62
|
+
query_params: Query parameters to send with the request.
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
str:
|
|
66
|
+
Returns the ciphertext.
|
|
67
|
+
"""
|
|
68
|
+
response = SESSION.get(
|
|
69
|
+
server_url,
|
|
70
|
+
params=query_params,
|
|
71
|
+
)
|
|
72
|
+
assert response.ok, response.text
|
|
73
|
+
return response.json()["detail"]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def update_secret(key: str, value: str, table_name: str) -> Dict[str, str]:
|
|
77
|
+
"""Update or create a new secret in the vault.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
key: Key for the secret.
|
|
81
|
+
value: Value for the secret.
|
|
82
|
+
table_name: Table name.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
Dict[str, str]:
|
|
86
|
+
Returns the server response.
|
|
87
|
+
"""
|
|
88
|
+
url = urljoin(VAULT_SERVER, "put-secret")
|
|
89
|
+
response = SESSION.put(
|
|
90
|
+
url,
|
|
91
|
+
json={
|
|
92
|
+
"key": key,
|
|
93
|
+
"value": value,
|
|
94
|
+
"table_name": table_name,
|
|
95
|
+
},
|
|
96
|
+
)
|
|
97
|
+
assert response.ok, response.text
|
|
98
|
+
return response.json()["detail"]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def delete_secret(key: str, table_name: str) -> Dict[str, str]:
|
|
102
|
+
"""Delete a secret from the vault.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
key: Key for the secret.
|
|
106
|
+
table_name: Table name.
|
|
107
|
+
|
|
108
|
+
Returns:
|
|
109
|
+
Dict[str, str]:
|
|
110
|
+
Returns the server response.
|
|
111
|
+
"""
|
|
112
|
+
url = urljoin(VAULT_SERVER, "delete-secret")
|
|
113
|
+
response = SESSION.delete(
|
|
114
|
+
url,
|
|
115
|
+
json={
|
|
116
|
+
"key": key,
|
|
117
|
+
"table_name": table_name,
|
|
118
|
+
},
|
|
119
|
+
)
|
|
120
|
+
assert response.ok, response.text
|
|
121
|
+
return response.json()["detail"]
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def create_table(table_name: str) -> Dict[str, str]:
|
|
125
|
+
"""Creates a new table in the vault.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
table_name: Table name.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
Dict[str, str]:
|
|
132
|
+
Returns the server response.
|
|
133
|
+
"""
|
|
134
|
+
url = urljoin(VAULT_SERVER, "create-table")
|
|
135
|
+
response = SESSION.post(url, params={"table_name": table_name})
|
|
136
|
+
assert response.ok, response.text
|
|
137
|
+
return response.json()["detail"]
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def decrypt(
|
|
141
|
+
cipher: str = None,
|
|
142
|
+
table: str = None,
|
|
143
|
+
get_secret: str = None,
|
|
144
|
+
get_secrets: str = None,
|
|
145
|
+
) -> Dict[str, str]:
|
|
146
|
+
"""Decrypt function.
|
|
147
|
+
|
|
148
|
+
Args:
|
|
149
|
+
cipher: Ciphertext to decrypt.
|
|
150
|
+
table: Table name to retrieve.
|
|
151
|
+
get_secret: Secret key to retrieve.
|
|
152
|
+
get_secrets: Comma separated list of secret keys to retrieve.
|
|
153
|
+
|
|
154
|
+
Returns:
|
|
155
|
+
Dict[str, str]:
|
|
156
|
+
Returns a dictionary of decrypted values.
|
|
157
|
+
"""
|
|
158
|
+
if cipher:
|
|
159
|
+
return transit_decrypt(ciphertext=cipher)
|
|
160
|
+
assert all(
|
|
161
|
+
(APIKEY, VAULT_SERVER)
|
|
162
|
+
), "'APIKEY' and 'VAULT_SERVER' environment variables are required to connect to the server"
|
|
163
|
+
assert table, "'table' is required when cipher text is not provided"
|
|
164
|
+
params = dict(table_name=table)
|
|
165
|
+
if get_secret:
|
|
166
|
+
url = urljoin(VAULT_SERVER, "get-secret")
|
|
167
|
+
params["key"] = get_secret
|
|
168
|
+
elif get_secrets:
|
|
169
|
+
url = urljoin(VAULT_SERVER, "get-secrets")
|
|
170
|
+
params["keys"] = get_secrets
|
|
171
|
+
else:
|
|
172
|
+
url = urljoin(VAULT_SERVER, "get-table")
|
|
173
|
+
return transit_decrypt(ciphertext=get_cipher(url, params))
|