VaultAPI 0.0.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.
- VaultAPI-0.0.0.dist-info/LICENSE +21 -0
- VaultAPI-0.0.0.dist-info/METADATA +223 -0
- VaultAPI-0.0.0.dist-info/RECORD +17 -0
- VaultAPI-0.0.0.dist-info/WHEEL +5 -0
- VaultAPI-0.0.0.dist-info/entry_points.txt +2 -0
- VaultAPI-0.0.0.dist-info/top_level.txt +1 -0
- vaultapi/__init__.py +68 -0
- vaultapi/auth.py +44 -0
- vaultapi/database.py +69 -0
- vaultapi/exceptions.py +9 -0
- vaultapi/main.py +73 -0
- vaultapi/models.py +205 -0
- vaultapi/payload.py +24 -0
- vaultapi/rate_limit.py +66 -0
- vaultapi/routes.py +352 -0
- vaultapi/squire.py +58 -0
- vaultapi/version.py +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Vignesh Rao
|
|
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,223 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: VaultAPI
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: Lightweight API to store/retrieve secrets to/from an encrypted Database
|
|
5
|
+
Author-email: Vignesh Rao <svignesh1793@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2024 Vignesh Rao
|
|
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
|
|
29
|
+
Project-URL: Docs, https://thevickypedia.github.io/VaultAPI
|
|
30
|
+
Project-URL: Source, https://github.com/thevickypedia/VaultAPI
|
|
31
|
+
Project-URL: Bug Tracker, https://github.com/thevickypedia/VaultAPI/issues
|
|
32
|
+
Project-URL: Release Notes, https://github.com/thevickypedia/VaultAPI/blob/main/release_notes.rst
|
|
33
|
+
Keywords: vaultapi,vault,fastapi,sqlite3,fernet
|
|
34
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
35
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
37
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
38
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
39
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
40
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
41
|
+
Requires-Python: >=3.10
|
|
42
|
+
Description-Content-Type: text/markdown
|
|
43
|
+
License-File: LICENSE
|
|
44
|
+
Requires-Dist: annotated-types==0.7.0
|
|
45
|
+
Requires-Dist: anyio==4.4.0
|
|
46
|
+
Requires-Dist: certifi==2024.8.30
|
|
47
|
+
Requires-Dist: cffi==1.17.1
|
|
48
|
+
Requires-Dist: charset-normalizer==3.3.2
|
|
49
|
+
Requires-Dist: click==8.1.7
|
|
50
|
+
Requires-Dist: cryptography==43.0.1
|
|
51
|
+
Requires-Dist: fastapi==0.114.2
|
|
52
|
+
Requires-Dist: h11==0.14.0
|
|
53
|
+
Requires-Dist: idna==3.10
|
|
54
|
+
Requires-Dist: pycparser==2.22
|
|
55
|
+
Requires-Dist: pydantic==2.9.1
|
|
56
|
+
Requires-Dist: pydantic-settings==2.5.2
|
|
57
|
+
Requires-Dist: pydantic-core==2.23.3
|
|
58
|
+
Requires-Dist: python-dotenv==1.0.1
|
|
59
|
+
Requires-Dist: PyYAML==6.0.2
|
|
60
|
+
Requires-Dist: requests==2.32.3
|
|
61
|
+
Requires-Dist: sniffio==1.3.1
|
|
62
|
+
Requires-Dist: starlette==0.38.5
|
|
63
|
+
Requires-Dist: typing-extensions==4.12.2
|
|
64
|
+
Requires-Dist: urllib3==2.2.3
|
|
65
|
+
Requires-Dist: uvicorn==0.30.6
|
|
66
|
+
Provides-Extra: dev
|
|
67
|
+
Requires-Dist: sphinx==5.1.1; extra == "dev"
|
|
68
|
+
Requires-Dist: pre-commit; extra == "dev"
|
|
69
|
+
Requires-Dist: recommonmark; extra == "dev"
|
|
70
|
+
Requires-Dist: gitverse; extra == "dev"
|
|
71
|
+
|
|
72
|
+
# VaultAPI
|
|
73
|
+
Lightweight API to store/retrieve secrets to/from an encrypted Database
|
|
74
|
+
|
|
75
|
+
![Python][label-pyversion]
|
|
76
|
+
|
|
77
|
+
**Platform Supported**
|
|
78
|
+
|
|
79
|
+
![Platform][label-platform]
|
|
80
|
+
|
|
81
|
+
**Deployments**
|
|
82
|
+
|
|
83
|
+
[![pages][label-actions-pages]][gha_pages]
|
|
84
|
+
[![pypi][label-actions-pypi]][gha_pypi]
|
|
85
|
+
[![markdown][label-actions-markdown]][gha_md_valid]
|
|
86
|
+
|
|
87
|
+
[![Pypi][label-pypi]][pypi]
|
|
88
|
+
[![Pypi-format][label-pypi-format]][pypi-files]
|
|
89
|
+
[![Pypi-status][label-pypi-status]][pypi]
|
|
90
|
+
|
|
91
|
+
## Kick off
|
|
92
|
+
|
|
93
|
+
**Recommendations**
|
|
94
|
+
|
|
95
|
+
- Install `python` [3.10] or [3.11]
|
|
96
|
+
- Use a dedicated [virtual environment]
|
|
97
|
+
|
|
98
|
+
**Install VaultAPI**
|
|
99
|
+
```shell
|
|
100
|
+
python -m pip install vaultapi
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
**Initiate - IDE**
|
|
104
|
+
```python
|
|
105
|
+
import vaultapi
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
if __name__ == '__main__':
|
|
109
|
+
vaultapi.start()
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
**Initiate - CLI**
|
|
113
|
+
```shell
|
|
114
|
+
vaultapi start
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
> Use `vaultapi --help` for usage instructions.
|
|
118
|
+
|
|
119
|
+
## Environment Variables
|
|
120
|
+
|
|
121
|
+
<details>
|
|
122
|
+
<summary><strong>Sourcing environment variables from an env file</strong></summary>
|
|
123
|
+
|
|
124
|
+
> _By default, `VaultAPI` will look for a `.env` file in the current working directory._
|
|
125
|
+
</details>
|
|
126
|
+
|
|
127
|
+
- **HOST** - Hostname for the API server.
|
|
128
|
+
- **PORT** - Port number for the API server.
|
|
129
|
+
- **WORKERS** - Number of workers for the uvicorn server.
|
|
130
|
+
- **APIKEY** - API Key for authentication.
|
|
131
|
+
- **SECRET** - Secret access key to encode/decode the secrets in Datastore.
|
|
132
|
+
- **DATABASE** - FilePath to store the secrets' database.
|
|
133
|
+
- **RATE_LIMIT** - List of dictionaries with `max_requests` and `seconds` to apply as rate limit.
|
|
134
|
+
|
|
135
|
+
<details>
|
|
136
|
+
<summary>Auto generate a <code>SECRET</code> value</summary>
|
|
137
|
+
|
|
138
|
+
This value will be used to encrypt/decrypt the secrets stored in the database.
|
|
139
|
+
|
|
140
|
+
**CLI**
|
|
141
|
+
```shell
|
|
142
|
+
vaultapi keygen
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
**IDE**
|
|
146
|
+
```python
|
|
147
|
+
from cryptography.fernet import Fernet
|
|
148
|
+
print(Fernet.generate_key())
|
|
149
|
+
```
|
|
150
|
+
</details>
|
|
151
|
+
|
|
152
|
+
## Coding Standards
|
|
153
|
+
Docstring format: [`Google`][google-docs] <br>
|
|
154
|
+
Styling conventions: [`PEP 8`][pep8] and [`isort`][isort]
|
|
155
|
+
|
|
156
|
+
## [Release Notes][release-notes]
|
|
157
|
+
**Requirement**
|
|
158
|
+
```shell
|
|
159
|
+
python -m pip install gitverse
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
**Usage**
|
|
163
|
+
```shell
|
|
164
|
+
gitverse-release reverse -f release_notes.rst -t 'Release Notes'
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Linting
|
|
168
|
+
`pre-commit` will ensure linting, run pytest, generate runbook & release notes, and validate hyperlinks in ALL
|
|
169
|
+
markdown files (including Wiki pages)
|
|
170
|
+
|
|
171
|
+
**Requirement**
|
|
172
|
+
```shell
|
|
173
|
+
python -m pip install sphinx==5.1.1 pre-commit recommonmark
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
**Usage**
|
|
177
|
+
```shell
|
|
178
|
+
pre-commit run --all-files
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Pypi Package
|
|
182
|
+
[![pypi-module][label-pypi-package]][pypi-repo]
|
|
183
|
+
|
|
184
|
+
[https://pypi.org/project/VaultAPI/][pypi]
|
|
185
|
+
|
|
186
|
+
## Runbook
|
|
187
|
+
[![made-with-sphinx-doc][label-sphinx-doc]][sphinx]
|
|
188
|
+
|
|
189
|
+
[https://thevickypedia.github.io/VaultAPI/][runbook]
|
|
190
|
+
|
|
191
|
+
## License & copyright
|
|
192
|
+
|
|
193
|
+
© Vignesh Rao
|
|
194
|
+
|
|
195
|
+
Licensed under the [MIT License][license]
|
|
196
|
+
|
|
197
|
+
[label-actions-markdown]: https://github.com/thevickypedia/VaultAPI/actions/workflows/markdown.yaml/badge.svg
|
|
198
|
+
[label-pypi-package]: https://img.shields.io/badge/Pypi%20Package-VaultAPI-blue?style=for-the-badge&logo=Python
|
|
199
|
+
[label-sphinx-doc]: https://img.shields.io/badge/Made%20with-Sphinx-blue?style=for-the-badge&logo=Sphinx
|
|
200
|
+
[label-pyversion]: https://img.shields.io/badge/python-3.10%20%7C%203.11-blue
|
|
201
|
+
[label-platform]: https://img.shields.io/badge/Platform-Linux|macOS|Windows-1f425f.svg
|
|
202
|
+
[label-actions-pages]: https://github.com/thevickypedia/VaultAPI/actions/workflows/pages/pages-build-deployment/badge.svg
|
|
203
|
+
[label-actions-pypi]: https://github.com/thevickypedia/VaultAPI/actions/workflows/python-publish.yaml/badge.svg
|
|
204
|
+
[label-pypi]: https://img.shields.io/pypi/v/VaultAPI
|
|
205
|
+
[label-pypi-format]: https://img.shields.io/pypi/format/VaultAPI
|
|
206
|
+
[label-pypi-status]: https://img.shields.io/pypi/status/VaultAPI
|
|
207
|
+
|
|
208
|
+
[3.10]: https://docs.python.org/3/whatsnew/3.10.html
|
|
209
|
+
[3.11]: https://docs.python.org/3/whatsnew/3.11.html
|
|
210
|
+
[virtual environment]: https://docs.python.org/3/tutorial/venv.html
|
|
211
|
+
[release-notes]: https://github.com/thevickypedia/VaultAPI/blob/master/release_notes.rst
|
|
212
|
+
[gha_pages]: https://github.com/thevickypedia/VaultAPI/actions/workflows/pages/pages-build-deployment
|
|
213
|
+
[gha_pypi]: https://github.com/thevickypedia/VaultAPI/actions/workflows/python-publish.yaml
|
|
214
|
+
[gha_md_valid]: https://github.com/thevickypedia/VaultAPI/actions/workflows/markdown.yaml
|
|
215
|
+
[google-docs]: https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings
|
|
216
|
+
[pep8]: https://www.python.org/dev/peps/pep-0008/
|
|
217
|
+
[isort]: https://pycqa.github.io/isort/
|
|
218
|
+
[sphinx]: https://www.sphinx-doc.org/en/master/man/sphinx-autogen.html
|
|
219
|
+
[pypi]: https://pypi.org/project/VaultAPI
|
|
220
|
+
[pypi-files]: https://pypi.org/project/VaultAPI/#files
|
|
221
|
+
[pypi-repo]: https://packaging.python.org/tutorials/packaging-projects/
|
|
222
|
+
[license]: https://github.com/thevickypedia/VaultAPI/blob/master/LICENSE
|
|
223
|
+
[runbook]: https://thevickypedia.github.io/VaultAPI/
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
vaultapi/__init__.py,sha256=xl6R8Mh1RIh9syQd9gVqwk64xWARNVPgyjY3zdBvkhk,2323
|
|
2
|
+
vaultapi/auth.py,sha256=s9xhLuCQbO5q0vI6T15_nzYtENyw-Bbf9g3rGRlimHc,1461
|
|
3
|
+
vaultapi/database.py,sha256=140YlgkRTCjD5xIzm5fggUo-yRfITIR9dI5SlfV96Fk,2101
|
|
4
|
+
vaultapi/exceptions.py,sha256=99R9L2vmimS68RLt5bpw1QT3C2WELHvKNRDWRDwBKi4,185
|
|
5
|
+
vaultapi/main.py,sha256=eqUwj3WFoneqovE34CXhXiwpkhn25NKDkm12INCoOko,2516
|
|
6
|
+
vaultapi/models.py,sha256=gtFOzZcxhc_IMfAL6Vd_s7JySho8qVaPJUFgR5FzCb4,5504
|
|
7
|
+
vaultapi/payload.py,sha256=s2kc5wUCpSoND9Hc1Tsz4sBnoJQtQdn8WBKlx31aRRI,346
|
|
8
|
+
vaultapi/rate_limit.py,sha256=i4CsbJuQsGVImR_uvF5Olz7Onz8sZdsN37AqhdASfE0,2075
|
|
9
|
+
vaultapi/routes.py,sha256=jNOqje_EAF3U11m_fgBKW2ZgLl0edQ9jg29twBBnTWw,11230
|
|
10
|
+
vaultapi/squire.py,sha256=dipCTxwYKkI07ab-6ZaRHpNhouE8bwBppwu8s9xLQ2Y,1780
|
|
11
|
+
vaultapi/version.py,sha256=ShXQBVjyiSOHxoQJS2BvNG395W4KZfqMxZWBAR0MZrE,22
|
|
12
|
+
VaultAPI-0.0.0.dist-info/LICENSE,sha256=xnHdLNCLt4RW3vtnE_TfN_cjViUHyIv0m8024fS4bws,1068
|
|
13
|
+
VaultAPI-0.0.0.dist-info/METADATA,sha256=hZwurinA8_kmb3zVFkmgpzagiNPGGY7mJDAjcl-g1xE,7870
|
|
14
|
+
VaultAPI-0.0.0.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
|
|
15
|
+
VaultAPI-0.0.0.dist-info/entry_points.txt,sha256=MKe6sVDEAHC3YBZoYWLMvgwupRE_QJ_iHK0umfgGOzs,50
|
|
16
|
+
VaultAPI-0.0.0.dist-info/top_level.txt,sha256=17jtNvOOJqazNgWn2ZTmJqfQau5jan_xF82NAJOINQg,9
|
|
17
|
+
VaultAPI-0.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
vaultapi
|
vaultapi/__init__.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
from cryptography.fernet import Fernet
|
|
5
|
+
|
|
6
|
+
from .main import start, version
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@click.command()
|
|
10
|
+
@click.argument("run", required=False)
|
|
11
|
+
@click.argument("start", required=False)
|
|
12
|
+
@click.argument("keygen", required=False)
|
|
13
|
+
@click.option("--version", "-V", is_flag=True, help="Prints the version.")
|
|
14
|
+
@click.option("--help", "-H", is_flag=True, help="Prints the help section.")
|
|
15
|
+
@click.option(
|
|
16
|
+
"--env",
|
|
17
|
+
"-E",
|
|
18
|
+
type=click.Path(exists=True),
|
|
19
|
+
help="Environment configuration filepath.",
|
|
20
|
+
)
|
|
21
|
+
def commandline(*args, **kwargs) -> None:
|
|
22
|
+
"""Starter function to invoke VaultAPI via CLI commands.
|
|
23
|
+
|
|
24
|
+
**Flags**
|
|
25
|
+
- ``--version | -V``: Prints the version.
|
|
26
|
+
- ``--help | -H``: Prints the help section.
|
|
27
|
+
- ``--env | -E``: Environment configuration filepath.
|
|
28
|
+
"""
|
|
29
|
+
assert sys.argv[0].lower().endswith("vaultapi"), "Invalid commandline trigger!!"
|
|
30
|
+
options = {
|
|
31
|
+
"--version | -V": "Prints the version.",
|
|
32
|
+
"--help | -H": "Prints the help section.",
|
|
33
|
+
"--env | -E": "Environment configuration filepath.",
|
|
34
|
+
"start | run": "Initiates the API server.",
|
|
35
|
+
}
|
|
36
|
+
# weird way to increase spacing to keep all values monotonic
|
|
37
|
+
_longest_key = len(max(options.keys()))
|
|
38
|
+
_pretext = "\n\t* "
|
|
39
|
+
choices = _pretext + _pretext.join(
|
|
40
|
+
f"{k} {'·' * (_longest_key - len(k) + 8)}→ {v}".expandtabs()
|
|
41
|
+
for k, v in options.items()
|
|
42
|
+
)
|
|
43
|
+
if kwargs.get("version"):
|
|
44
|
+
click.echo(f"VaultAPI {version.__version__}")
|
|
45
|
+
sys.exit(0)
|
|
46
|
+
if kwargs.get("help"):
|
|
47
|
+
click.echo(
|
|
48
|
+
f"\nUsage: vaultapi [arbitrary-command]\nOptions (and corresponding behavior):{choices}"
|
|
49
|
+
)
|
|
50
|
+
sys.exit(0)
|
|
51
|
+
trigger = (
|
|
52
|
+
kwargs.get("start") or kwargs.get("run") or kwargs.get("keygen") or ""
|
|
53
|
+
).lower()
|
|
54
|
+
if trigger in ("start", "run"):
|
|
55
|
+
start(env_file=kwargs.get("env"))
|
|
56
|
+
sys.exit(0)
|
|
57
|
+
elif trigger == "keygen":
|
|
58
|
+
key = Fernet.generate_key()
|
|
59
|
+
click.secho(
|
|
60
|
+
f"\nStore this as an env var named 'secret' or pass it as kwargs\n\n{key.decode()}\n"
|
|
61
|
+
)
|
|
62
|
+
sys.exit(0)
|
|
63
|
+
else:
|
|
64
|
+
click.secho(f"\n{kwargs}\nNo command provided", fg="red")
|
|
65
|
+
click.echo(
|
|
66
|
+
f"Usage: vaultapi [arbitrary-command]\nOptions (and corresponding behavior):{choices}"
|
|
67
|
+
)
|
|
68
|
+
sys.exit(1)
|
vaultapi/auth.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import secrets
|
|
3
|
+
import time
|
|
4
|
+
from http import HTTPStatus
|
|
5
|
+
|
|
6
|
+
from fastapi import Request
|
|
7
|
+
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
8
|
+
|
|
9
|
+
from . import exceptions, models
|
|
10
|
+
|
|
11
|
+
LOGGER = logging.getLogger("uvicorn.default")
|
|
12
|
+
EPOCH = lambda: int(time.time()) # noqa: E731
|
|
13
|
+
SECURITY = HTTPBearer()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
async def validate(request: Request, apikey: HTTPAuthorizationCredentials) -> None:
|
|
17
|
+
"""Validates the auth request using HTTPBearer.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
request: Takes the authorization header token as an argument.
|
|
21
|
+
apikey: Basic APIKey required for all the routes.
|
|
22
|
+
|
|
23
|
+
Raises:
|
|
24
|
+
APIResponse:
|
|
25
|
+
- 401: If authorization is invalid.
|
|
26
|
+
- 403: If host address is forbidden.
|
|
27
|
+
"""
|
|
28
|
+
if apikey.credentials.startswith("\\"):
|
|
29
|
+
auth = bytes(apikey.credentials, "utf-8").decode(encoding="unicode_escape")
|
|
30
|
+
else:
|
|
31
|
+
auth = apikey.credentials
|
|
32
|
+
if secrets.compare_digest(auth, models.env.apikey):
|
|
33
|
+
LOGGER.info(
|
|
34
|
+
"Connection received from client-host: %s, host-header: %s, x-fwd-host: %s",
|
|
35
|
+
request.client.host,
|
|
36
|
+
request.headers.get("host"),
|
|
37
|
+
request.headers.get("x-forwarded-host"),
|
|
38
|
+
)
|
|
39
|
+
if user_agent := request.headers.get("user-agent"):
|
|
40
|
+
LOGGER.info("User agent: %s", user_agent)
|
|
41
|
+
return
|
|
42
|
+
raise exceptions.APIResponse(
|
|
43
|
+
status_code=HTTPStatus.UNAUTHORIZED.real, detail=HTTPStatus.UNAUTHORIZED.phrase
|
|
44
|
+
)
|
vaultapi/database.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
from typing import List, Tuple
|
|
2
|
+
|
|
3
|
+
from . import models
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def get_secret(key: str, table_name: str) -> str | None:
|
|
7
|
+
"""Function to retrieve secret from database.
|
|
8
|
+
|
|
9
|
+
Args:
|
|
10
|
+
key: Name of the secret to retrieve.
|
|
11
|
+
table_name: Name of the table where the secret is stored.
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
str:
|
|
15
|
+
Returns the secret value.
|
|
16
|
+
"""
|
|
17
|
+
with models.database.connection:
|
|
18
|
+
cursor = models.database.connection.cursor()
|
|
19
|
+
state = cursor.execute(
|
|
20
|
+
f'SELECT value FROM "{table_name}" WHERE key=(?)', (key,)
|
|
21
|
+
).fetchone()
|
|
22
|
+
if state and state[0]:
|
|
23
|
+
return state[0]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def get_table(table_name: str) -> List[Tuple[str, str]]:
|
|
27
|
+
"""Function to retrieve all key-value pairs from a particular table in the database.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
table_name: Name of the table where the secrets are stored.
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
str:
|
|
34
|
+
Returns the secret value.
|
|
35
|
+
"""
|
|
36
|
+
with models.database.connection:
|
|
37
|
+
cursor = models.database.connection.cursor()
|
|
38
|
+
state = cursor.execute(f'SELECT * FROM "{table_name}"').fetchall()
|
|
39
|
+
return state
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def put_secret(key: str, value: str, table_name: str) -> None:
|
|
43
|
+
"""Function to add secret to the database.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
key: Name of the secret to be stored.
|
|
47
|
+
value: Value of the secret to be stored
|
|
48
|
+
table_name: Name of the table where the secret is stored.
|
|
49
|
+
"""
|
|
50
|
+
with models.database.connection:
|
|
51
|
+
cursor = models.database.connection.cursor()
|
|
52
|
+
cursor.execute(
|
|
53
|
+
f'INSERT INTO "{table_name}" (key, value) VALUES (?,?)',
|
|
54
|
+
(key, value),
|
|
55
|
+
)
|
|
56
|
+
models.database.connection.commit()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def remove_secret(key: str, table_name: str) -> None:
|
|
60
|
+
"""Function to remove a secret from the database.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
key: Name of the secret to be removed.
|
|
64
|
+
table_name: Name of the table where the secret is stored.
|
|
65
|
+
"""
|
|
66
|
+
with models.database.connection:
|
|
67
|
+
cursor = models.database.connection.cursor()
|
|
68
|
+
cursor.execute(f'DELETE FROM "{table_name}" WHERE key=(?)', (key,))
|
|
69
|
+
models.database.connection.commit()
|
vaultapi/exceptions.py
ADDED
vaultapi/main.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import pathlib
|
|
3
|
+
|
|
4
|
+
import uvicorn
|
|
5
|
+
from cryptography.fernet import Fernet
|
|
6
|
+
from fastapi import FastAPI
|
|
7
|
+
from fastapi.middleware.cors import CORSMiddleware
|
|
8
|
+
|
|
9
|
+
from . import models, routes, squire, version
|
|
10
|
+
|
|
11
|
+
LOGGER = logging.getLogger("uvicorn.default")
|
|
12
|
+
VaultAPI = FastAPI(
|
|
13
|
+
title="VaultAPI",
|
|
14
|
+
description="Lightweight service to serve secrets and environment variables",
|
|
15
|
+
version=version.__version__,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def enable_cors() -> None:
|
|
20
|
+
"""Enables CORS policy."""
|
|
21
|
+
LOGGER.info("Setting CORS policy")
|
|
22
|
+
origins = [
|
|
23
|
+
"http://localhost.com",
|
|
24
|
+
"https://localhost.com",
|
|
25
|
+
]
|
|
26
|
+
for website in models.env.endpoints:
|
|
27
|
+
origins.extend(
|
|
28
|
+
[f"http://{website.host}", f"https://{website.host}"]
|
|
29
|
+
) # noqa: HttpUrlsUsage
|
|
30
|
+
VaultAPI.add_middleware(
|
|
31
|
+
CORSMiddleware, # noqa: PyTypeChecker
|
|
32
|
+
allow_origins=origins,
|
|
33
|
+
allow_credentials=True,
|
|
34
|
+
allow_methods=["GET", "POST", "PUT", "DELETE"],
|
|
35
|
+
allow_headers=[
|
|
36
|
+
# Default headers
|
|
37
|
+
"host",
|
|
38
|
+
"user-agent",
|
|
39
|
+
"authorization",
|
|
40
|
+
],
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def start(**kwargs) -> None:
|
|
45
|
+
"""Starter function for the API, which uses uvicorn server as trigger.
|
|
46
|
+
|
|
47
|
+
Keyword Args:
|
|
48
|
+
env_file: Env filepath to load the environment variables.
|
|
49
|
+
apikey: API Key to authenticate the server.
|
|
50
|
+
secret: Secret access key to access the secret content.
|
|
51
|
+
host: Hostname for the API server.
|
|
52
|
+
port: Port number for the API server.
|
|
53
|
+
workers: Number of workers for the uvicorn server.
|
|
54
|
+
database: FilePath to store the auth database that handles the authentication errors.
|
|
55
|
+
rate_limit: List of dictionaries with ``max_requests`` and ``seconds`` to apply as rate limit.
|
|
56
|
+
log_config: Logging configuration as a dict or a FilePath. Supports .yaml/.yml, .json or .ini formats.
|
|
57
|
+
"""
|
|
58
|
+
models.env = squire.load_env(**kwargs)
|
|
59
|
+
models.session.fernet = Fernet(models.env.secret)
|
|
60
|
+
models.database = models.Database(models.env.database)
|
|
61
|
+
models.database.create_table("default", ["key", "value"])
|
|
62
|
+
module_name = pathlib.Path(__file__)
|
|
63
|
+
enable_cors()
|
|
64
|
+
VaultAPI.routes.extend(routes.get_all_routes())
|
|
65
|
+
kwargs = dict(
|
|
66
|
+
host=models.env.host,
|
|
67
|
+
port=models.env.port,
|
|
68
|
+
workers=models.env.workers,
|
|
69
|
+
app=f"{module_name.parent.stem}.{module_name.stem}:{VaultAPI.title}",
|
|
70
|
+
)
|
|
71
|
+
if models.env.log_config:
|
|
72
|
+
kwargs["log_config"] = models.env.log_config
|
|
73
|
+
uvicorn.run(**kwargs)
|
vaultapi/models.py
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import pathlib
|
|
2
|
+
import re
|
|
3
|
+
import socket
|
|
4
|
+
import sqlite3
|
|
5
|
+
from typing import Any, Dict, List, Set, Tuple
|
|
6
|
+
|
|
7
|
+
from cryptography.fernet import Fernet
|
|
8
|
+
from pydantic import (
|
|
9
|
+
BaseModel,
|
|
10
|
+
Field,
|
|
11
|
+
FilePath,
|
|
12
|
+
HttpUrl,
|
|
13
|
+
NewPath,
|
|
14
|
+
PositiveInt,
|
|
15
|
+
field_validator,
|
|
16
|
+
)
|
|
17
|
+
from pydantic_settings import BaseSettings
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def complexity_checker(secret: str, simple: bool = False) -> None:
|
|
21
|
+
"""Verifies the strength of a secret.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
secret: Value of the secret.
|
|
25
|
+
simple: Boolean flag to increase complexity.
|
|
26
|
+
|
|
27
|
+
See Also:
|
|
28
|
+
A secret is considered strong if it at least has:
|
|
29
|
+
|
|
30
|
+
- 32 characters
|
|
31
|
+
- 1 digit
|
|
32
|
+
- 1 symbol
|
|
33
|
+
- 1 uppercase letter
|
|
34
|
+
- 1 lowercase letter
|
|
35
|
+
|
|
36
|
+
Raises:
|
|
37
|
+
AssertionError: When at least 1 of the above conditions fail to match.
|
|
38
|
+
"""
|
|
39
|
+
char_limit = 8 if simple else 32
|
|
40
|
+
|
|
41
|
+
# calculates the length
|
|
42
|
+
assert (
|
|
43
|
+
len(secret) >= char_limit
|
|
44
|
+
), f"Minimum secret length is {char_limit}, received {len(secret)}"
|
|
45
|
+
|
|
46
|
+
# searches for digits
|
|
47
|
+
assert re.search(r"\d", secret), "secret must include an integer"
|
|
48
|
+
|
|
49
|
+
if simple:
|
|
50
|
+
return
|
|
51
|
+
|
|
52
|
+
# searches for uppercase
|
|
53
|
+
assert re.search(
|
|
54
|
+
r"[A-Z]", secret
|
|
55
|
+
), "secret must include at least one uppercase letter"
|
|
56
|
+
|
|
57
|
+
# searches for lowercase
|
|
58
|
+
assert re.search(
|
|
59
|
+
r"[a-z]", secret
|
|
60
|
+
), "secret must include at least one lowercase letter"
|
|
61
|
+
|
|
62
|
+
# searches for symbols
|
|
63
|
+
assert re.search(
|
|
64
|
+
r"[ !#$%&'()*+,-./[\\\]^_`{|}~" + r'"]', secret
|
|
65
|
+
), "secret must contain at least one special character"
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class Database:
|
|
69
|
+
"""Creates a connection and instantiates the cursor.
|
|
70
|
+
|
|
71
|
+
>>> Database
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
filepath: Name of the database file.
|
|
75
|
+
timeout: Timeout for the connection to database.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
def __init__(self, filepath: FilePath | str, timeout: int = 10):
|
|
79
|
+
"""Instantiates the class ``Database`` to create a connection and a cursor."""
|
|
80
|
+
if not filepath.endswith(".db"):
|
|
81
|
+
filepath = filepath + ".db"
|
|
82
|
+
self.connection = sqlite3.connect(
|
|
83
|
+
database=filepath, check_same_thread=False, timeout=timeout
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
def create_table(self, table_name: str, columns: List[str] | Tuple[str]) -> None:
|
|
87
|
+
"""Creates the table with the required columns.
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
table_name: Name of the table that has to be created.
|
|
91
|
+
columns: List of columns that has to be created.
|
|
92
|
+
"""
|
|
93
|
+
with self.connection:
|
|
94
|
+
cursor = self.connection.cursor()
|
|
95
|
+
# Use f-string or %s as table names cannot be parametrized
|
|
96
|
+
cursor.execute(
|
|
97
|
+
f"CREATE TABLE IF NOT EXISTS {table_name!r} ({', '.join(columns)})"
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
database: Database = Database # noqa: PyTypeChecker
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class RateLimit(BaseModel):
|
|
105
|
+
"""Object to store the rate limit settings.
|
|
106
|
+
|
|
107
|
+
>>> RateLimit
|
|
108
|
+
|
|
109
|
+
"""
|
|
110
|
+
|
|
111
|
+
max_requests: PositiveInt
|
|
112
|
+
seconds: PositiveInt
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class Session(BaseModel):
|
|
116
|
+
"""Object to store session information.
|
|
117
|
+
|
|
118
|
+
>>> Session
|
|
119
|
+
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
fernet: Fernet | None = None
|
|
123
|
+
info: Dict[str, str] = {}
|
|
124
|
+
rps: Dict[str, int] = {}
|
|
125
|
+
allowed_origins: Set[str] = set()
|
|
126
|
+
|
|
127
|
+
class Config:
|
|
128
|
+
"""Config to allow arbitrary types."""
|
|
129
|
+
|
|
130
|
+
arbitrary_types_allowed = True
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class EnvConfig(BaseSettings):
|
|
134
|
+
"""Object to load environment variables.
|
|
135
|
+
|
|
136
|
+
>>> EnvConfig
|
|
137
|
+
|
|
138
|
+
"""
|
|
139
|
+
|
|
140
|
+
apikey: str
|
|
141
|
+
secret: str
|
|
142
|
+
database: FilePath | NewPath | str = Field("secrets.db", pattern=".*.db$")
|
|
143
|
+
endpoints: HttpUrl | List[HttpUrl] = []
|
|
144
|
+
host: str = socket.gethostbyname("localhost") or "0.0.0.0"
|
|
145
|
+
port: PositiveInt = 8080
|
|
146
|
+
workers: PositiveInt = 1
|
|
147
|
+
log_config: FilePath | Dict[str, Any] | None = None
|
|
148
|
+
allowed_origins: List[str] = []
|
|
149
|
+
rate_limit: RateLimit | List[RateLimit] = []
|
|
150
|
+
|
|
151
|
+
@field_validator("endpoints", mode="after", check_fields=True)
|
|
152
|
+
def parse_endpoints(
|
|
153
|
+
cls, value: HttpUrl | List[HttpUrl]
|
|
154
|
+
) -> List[HttpUrl]: # noqa: PyMethodParameters
|
|
155
|
+
"""Validate endpoints to enable CORS policy."""
|
|
156
|
+
if isinstance(value, list):
|
|
157
|
+
return value
|
|
158
|
+
return [value]
|
|
159
|
+
|
|
160
|
+
@field_validator("apikey", mode="after")
|
|
161
|
+
def parse_apikey(cls, value: str | None) -> str | None: # noqa: PyMethodParameters
|
|
162
|
+
"""Parse API key to validate complexity."""
|
|
163
|
+
if value:
|
|
164
|
+
try:
|
|
165
|
+
complexity_checker(value, True)
|
|
166
|
+
except AssertionError as error:
|
|
167
|
+
raise ValueError(error.__str__())
|
|
168
|
+
return value
|
|
169
|
+
|
|
170
|
+
@field_validator("secret", mode="after")
|
|
171
|
+
def parse_api_secret(
|
|
172
|
+
cls, value: str | None
|
|
173
|
+
) -> str | None: # noqa: PyMethodParameters
|
|
174
|
+
"""Parse API secret to validate complexity."""
|
|
175
|
+
if value:
|
|
176
|
+
try:
|
|
177
|
+
complexity_checker(value)
|
|
178
|
+
except AssertionError as error:
|
|
179
|
+
raise ValueError(error.__str__())
|
|
180
|
+
return value
|
|
181
|
+
|
|
182
|
+
@classmethod
|
|
183
|
+
def from_env_file(cls, env_file: pathlib.Path) -> "EnvConfig":
|
|
184
|
+
"""Create Settings instance from environment file.
|
|
185
|
+
|
|
186
|
+
Args:
|
|
187
|
+
env_file: Name of the env file.
|
|
188
|
+
|
|
189
|
+
Returns:
|
|
190
|
+
EnvConfig:
|
|
191
|
+
Loads the ``EnvConfig`` model.
|
|
192
|
+
"""
|
|
193
|
+
return cls(_env_file=env_file)
|
|
194
|
+
|
|
195
|
+
class Config:
|
|
196
|
+
"""Extra configuration for EnvConfig object."""
|
|
197
|
+
|
|
198
|
+
extra = "ignore"
|
|
199
|
+
hide_input_in_errors = True
|
|
200
|
+
arbitrary_types_allowed = True
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
# noinspection PyTypeChecker
|
|
204
|
+
env: EnvConfig = EnvConfig
|
|
205
|
+
session = Session()
|
vaultapi/payload.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from pydantic import BaseModel
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class DeleteSecret(BaseModel):
|
|
5
|
+
"""Payload for delete-secret API call.
|
|
6
|
+
|
|
7
|
+
>>> DeleteSecret
|
|
8
|
+
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
key: str
|
|
12
|
+
table_name: str = "default"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class PutSecret(BaseModel):
|
|
16
|
+
"""Payload for put-secret API call.
|
|
17
|
+
|
|
18
|
+
>>> DeleteSecret
|
|
19
|
+
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
key: str
|
|
23
|
+
value: str
|
|
24
|
+
table_name: str = "default"
|
vaultapi/rate_limit.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import time
|
|
3
|
+
from http import HTTPStatus
|
|
4
|
+
|
|
5
|
+
from fastapi import HTTPException, Request
|
|
6
|
+
|
|
7
|
+
from . import models
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class RateLimiter:
|
|
11
|
+
"""Object that implements the ``RateLimiter`` functionality.
|
|
12
|
+
|
|
13
|
+
>>> RateLimiter
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, rps: models.RateLimit):
|
|
18
|
+
# noinspection PyUnresolvedReferences
|
|
19
|
+
"""Instantiates the object with the necessary args.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
rps: RateLimit object with ``max_requests`` and ``seconds``.
|
|
23
|
+
|
|
24
|
+
Attributes:
|
|
25
|
+
max_requests: Maximum requests to allow in a given time frame.
|
|
26
|
+
seconds: Number of seconds after which the cache is set to expire.
|
|
27
|
+
"""
|
|
28
|
+
self.max_requests = rps.max_requests
|
|
29
|
+
self.seconds = rps.seconds
|
|
30
|
+
self.start_time = time.time()
|
|
31
|
+
self.exception = HTTPException(
|
|
32
|
+
status_code=HTTPStatus.TOO_MANY_REQUESTS.value,
|
|
33
|
+
detail=HTTPStatus.TOO_MANY_REQUESTS.phrase,
|
|
34
|
+
# reset headers, which will invalidate auth token
|
|
35
|
+
headers={"Retry-After": str(math.ceil(self.seconds))},
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
def init(self, request: Request) -> None:
|
|
39
|
+
"""Checks if the number of calls exceeds the rate limit for the given identifier.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
request: The incoming request object.
|
|
43
|
+
|
|
44
|
+
Raises:
|
|
45
|
+
429: Too many requests.
|
|
46
|
+
"""
|
|
47
|
+
if forwarded := request.headers.get("x-forwarded-for"):
|
|
48
|
+
identifier = forwarded.split(",")[0]
|
|
49
|
+
else:
|
|
50
|
+
identifier = request.client.host
|
|
51
|
+
identifier += ":" + request.url.path
|
|
52
|
+
|
|
53
|
+
current_time = time.time()
|
|
54
|
+
|
|
55
|
+
# Reset if the time window has passed
|
|
56
|
+
if current_time - self.start_time > self.seconds:
|
|
57
|
+
models.session.rps[identifier] = 1
|
|
58
|
+
self.start_time = current_time
|
|
59
|
+
|
|
60
|
+
if models.session.rps.get(identifier):
|
|
61
|
+
if models.session.rps[identifier] >= self.max_requests:
|
|
62
|
+
raise self.exception
|
|
63
|
+
else:
|
|
64
|
+
models.session.rps[identifier] += 1
|
|
65
|
+
else:
|
|
66
|
+
models.session.rps[identifier] = 1
|
vaultapi/routes.py
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import sqlite3
|
|
3
|
+
from http import HTTPStatus
|
|
4
|
+
from typing import Dict, List
|
|
5
|
+
|
|
6
|
+
from fastapi import Depends, Request
|
|
7
|
+
from fastapi.responses import RedirectResponse
|
|
8
|
+
from fastapi.routing import APIRoute
|
|
9
|
+
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
10
|
+
|
|
11
|
+
from . import auth, database, exceptions, models, payload, rate_limit
|
|
12
|
+
|
|
13
|
+
LOGGER = logging.getLogger("uvicorn.default")
|
|
14
|
+
security = HTTPBearer()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
async def retrieve_secret(key: str, table_name: str) -> str | None:
|
|
18
|
+
"""Retrieve an existing secret from a table in the database.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
key: Name of the secret to retrieve.
|
|
22
|
+
table_name: Name of the table where the secret is stored.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
str:
|
|
26
|
+
Returns the secret value.
|
|
27
|
+
"""
|
|
28
|
+
try:
|
|
29
|
+
return database.get_secret(key=key, table_name=table_name)
|
|
30
|
+
except sqlite3.OperationalError as error:
|
|
31
|
+
LOGGER.error(error)
|
|
32
|
+
raise exceptions.APIResponse(
|
|
33
|
+
status_code=HTTPStatus.BAD_REQUEST.real, detail=error.args[0]
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
async def retrieve_secrets(table_name: str, keys: List[str] = None) -> Dict[str, str]:
|
|
38
|
+
"""Retrieve multiple secrets from a table or retrieve the table as a whole.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
table_name: Name of the table where the secret is stored.
|
|
42
|
+
keys: List of keys for which the values have to be retrieved.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
Dict[str, str]:
|
|
46
|
+
Returns the key-value pairs for secret key and it's value.
|
|
47
|
+
"""
|
|
48
|
+
if keys:
|
|
49
|
+
values = {}
|
|
50
|
+
for key in keys:
|
|
51
|
+
if value := await retrieve_secret(key, table_name):
|
|
52
|
+
values[key] = value
|
|
53
|
+
return values
|
|
54
|
+
else:
|
|
55
|
+
try:
|
|
56
|
+
return dict(database.get_table(table_name))
|
|
57
|
+
except sqlite3.OperationalError as error:
|
|
58
|
+
LOGGER.error(error)
|
|
59
|
+
raise exceptions.APIResponse(
|
|
60
|
+
status_code=HTTPStatus.BAD_REQUEST.real, detail=error.args[0]
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
async def get_secret(
|
|
65
|
+
request: Request,
|
|
66
|
+
key: str,
|
|
67
|
+
table_name: str = "default",
|
|
68
|
+
apikey: HTTPAuthorizationCredentials = Depends(security),
|
|
69
|
+
):
|
|
70
|
+
"""**API function to retrieve a secret.**
|
|
71
|
+
|
|
72
|
+
**Args:**
|
|
73
|
+
|
|
74
|
+
request: Reference to the FastAPI request object.
|
|
75
|
+
key: Name of the secret to be retrieved.
|
|
76
|
+
table_name: Name of the table where the secret is stored.
|
|
77
|
+
apikey: API Key to authenticate the request.
|
|
78
|
+
|
|
79
|
+
**Raises:**
|
|
80
|
+
|
|
81
|
+
APIResponse:
|
|
82
|
+
Raises the HTTPStatus object with a status code and detail as response.
|
|
83
|
+
"""
|
|
84
|
+
await auth.validate(request, apikey)
|
|
85
|
+
if value := await retrieve_secret(key, table_name):
|
|
86
|
+
LOGGER.info("Secret value for '%s' was retrieved", key)
|
|
87
|
+
decrypted = models.session.fernet.decrypt(value).decode(encoding="UTF-8")
|
|
88
|
+
raise exceptions.APIResponse(status_code=HTTPStatus.OK.real, detail=decrypted)
|
|
89
|
+
LOGGER.info("Secret value for '%s' NOT found in the datastore", key)
|
|
90
|
+
raise exceptions.APIResponse(
|
|
91
|
+
status_code=HTTPStatus.NOT_FOUND.real, detail=HTTPStatus.NOT_FOUND.phrase
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
async def get_secrets(
|
|
96
|
+
request: Request,
|
|
97
|
+
keys: str,
|
|
98
|
+
table_name: str = "default",
|
|
99
|
+
apikey: HTTPAuthorizationCredentials = Depends(security),
|
|
100
|
+
):
|
|
101
|
+
"""**API function to retrieve multiple secrets at a time.**
|
|
102
|
+
|
|
103
|
+
**Args:**
|
|
104
|
+
|
|
105
|
+
request: Reference to the FastAPI request object.
|
|
106
|
+
key: Comma separated list of secret names to be retrieved.
|
|
107
|
+
table_name: Name of the table where the secrets are stored.
|
|
108
|
+
apikey: API Key to authenticate the request.
|
|
109
|
+
|
|
110
|
+
**Raises:**
|
|
111
|
+
|
|
112
|
+
APIResponse:
|
|
113
|
+
Raises the HTTPStatus object with a status code and detail as response.
|
|
114
|
+
"""
|
|
115
|
+
await auth.validate(request, apikey)
|
|
116
|
+
# keys = [key.strip() for key in keys.split(",") if key.strip()]
|
|
117
|
+
keys = list(filter(None, map(str.strip, keys.split(","))))
|
|
118
|
+
keys_ct = len(keys)
|
|
119
|
+
try:
|
|
120
|
+
assert keys_ct >= 1, f"Expected at least one key, received {keys_ct}"
|
|
121
|
+
except AssertionError as error:
|
|
122
|
+
LOGGER.error(error)
|
|
123
|
+
raise exceptions.APIResponse(
|
|
124
|
+
status_code=HTTPStatus.BAD_REQUEST.real, detail=error.args[0]
|
|
125
|
+
)
|
|
126
|
+
if values := await retrieve_secrets(table_name, keys):
|
|
127
|
+
values_ct = len(values)
|
|
128
|
+
try:
|
|
129
|
+
assert (
|
|
130
|
+
values_ct == keys_ct
|
|
131
|
+
), f"Number of keys [{keys_ct}] requested didn't match the number of values [{values_ct}] retrieved."
|
|
132
|
+
LOGGER.info("Secret value for %d (%s) were retrieved", keys_ct, keys)
|
|
133
|
+
code = HTTPStatus.OK.real
|
|
134
|
+
except AssertionError as error:
|
|
135
|
+
LOGGER.warning(error)
|
|
136
|
+
code = HTTPStatus.PARTIAL_CONTENT.real
|
|
137
|
+
decrypted = {
|
|
138
|
+
key: models.session.fernet.decrypt(value).decode(encoding="UTF-8")
|
|
139
|
+
for key, value in values.items()
|
|
140
|
+
}
|
|
141
|
+
raise exceptions.APIResponse(status_code=code, detail=decrypted)
|
|
142
|
+
if keys_ct == 1:
|
|
143
|
+
LOGGER.info("Secret value for '%s' NOT found in the datastore", keys[0])
|
|
144
|
+
else:
|
|
145
|
+
LOGGER.info(
|
|
146
|
+
"Secret values for %d keys %s were NOT found in the datastore",
|
|
147
|
+
keys_ct,
|
|
148
|
+
keys,
|
|
149
|
+
)
|
|
150
|
+
raise exceptions.APIResponse(
|
|
151
|
+
status_code=HTTPStatus.NOT_FOUND.real, detail=HTTPStatus.NOT_FOUND.phrase
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
async def get_table(
|
|
156
|
+
request: Request,
|
|
157
|
+
table_name: str = "default",
|
|
158
|
+
apikey: HTTPAuthorizationCredentials = Depends(security),
|
|
159
|
+
):
|
|
160
|
+
"""**API function to retrieve ALL the key-value pairs stored in a particular table.**
|
|
161
|
+
|
|
162
|
+
**Args:**
|
|
163
|
+
|
|
164
|
+
request: Reference to the FastAPI request object.
|
|
165
|
+
table_name: Name of the table where the secrets are stored.
|
|
166
|
+
apikey: API Key to authenticate the request.
|
|
167
|
+
|
|
168
|
+
**Raises:**
|
|
169
|
+
|
|
170
|
+
APIResponse:
|
|
171
|
+
Raises the HTTPStatus object with a status code and detail as response.
|
|
172
|
+
"""
|
|
173
|
+
await auth.validate(request, apikey)
|
|
174
|
+
table_content = await retrieve_secrets(table_name)
|
|
175
|
+
decrypted = {
|
|
176
|
+
key: models.session.fernet.decrypt(value).decode(encoding="UTF-8")
|
|
177
|
+
for key, value in table_content.items()
|
|
178
|
+
}
|
|
179
|
+
raise exceptions.APIResponse(status_code=HTTPStatus.OK.real, detail=decrypted)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
async def put_secret(
|
|
183
|
+
request: Request,
|
|
184
|
+
data: payload.PutSecret,
|
|
185
|
+
apikey: HTTPAuthorizationCredentials = Depends(security),
|
|
186
|
+
):
|
|
187
|
+
"""**API function to add secrets to database.**
|
|
188
|
+
|
|
189
|
+
**Args:**
|
|
190
|
+
|
|
191
|
+
request: Reference to the FastAPI request object.
|
|
192
|
+
data: Payload with ``key``, ``value``, and ``table_name`` as body.
|
|
193
|
+
apikey: API Key to authenticate the request.
|
|
194
|
+
|
|
195
|
+
**Raises:**
|
|
196
|
+
|
|
197
|
+
APIResponse:
|
|
198
|
+
Raises the HTTPStatus object with a status code and detail as response.
|
|
199
|
+
"""
|
|
200
|
+
await auth.validate(request, apikey)
|
|
201
|
+
if await retrieve_secret(data.key, data.table_name):
|
|
202
|
+
LOGGER.info("Secret value for '%s' will be overridden", data.key)
|
|
203
|
+
else:
|
|
204
|
+
LOGGER.info(
|
|
205
|
+
"Storing a secret value for '%s' to the table '%s' in the datastore",
|
|
206
|
+
data.key,
|
|
207
|
+
data.table_name,
|
|
208
|
+
)
|
|
209
|
+
encrypted = models.session.fernet.encrypt(data.value.encode(encoding="UTF-8"))
|
|
210
|
+
database.put_secret(key=data.key, value=encrypted, table_name=data.table_name)
|
|
211
|
+
raise exceptions.APIResponse(
|
|
212
|
+
status_code=HTTPStatus.OK.real, detail=HTTPStatus.OK.phrase
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
async def delete_secret(
|
|
217
|
+
request: Request,
|
|
218
|
+
data: payload.DeleteSecret,
|
|
219
|
+
apikey: HTTPAuthorizationCredentials = Depends(security),
|
|
220
|
+
):
|
|
221
|
+
"""**API function to delete secrets from database.**
|
|
222
|
+
|
|
223
|
+
**Args:**
|
|
224
|
+
|
|
225
|
+
request: Reference to the FastAPI request object.
|
|
226
|
+
data: Payload with ``key`` and ``table_name`` as body.
|
|
227
|
+
apikey: API Key to authenticate the request.
|
|
228
|
+
|
|
229
|
+
**Raises:**
|
|
230
|
+
|
|
231
|
+
APIResponse:
|
|
232
|
+
Raises the HTTPStatus object with a status code and detail as response.
|
|
233
|
+
"""
|
|
234
|
+
await auth.validate(request, apikey)
|
|
235
|
+
if await retrieve_secret(data.key, data.table_name):
|
|
236
|
+
LOGGER.info("Secret value for '%s' will be removed", data.key)
|
|
237
|
+
else:
|
|
238
|
+
LOGGER.warning("Secret value for '%s' NOT found", data.key)
|
|
239
|
+
raise exceptions.APIResponse(
|
|
240
|
+
status_code=HTTPStatus.NOT_FOUND.real, detail=HTTPStatus.NOT_FOUND.phrase
|
|
241
|
+
)
|
|
242
|
+
database.remove_secret(key=data.key, table_name=data.table_name)
|
|
243
|
+
raise exceptions.APIResponse(
|
|
244
|
+
status_code=HTTPStatus.OK.real, detail=HTTPStatus.OK.phrase
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
async def create_table(
|
|
249
|
+
request: Request,
|
|
250
|
+
table_name: str,
|
|
251
|
+
apikey: HTTPAuthorizationCredentials = Depends(security),
|
|
252
|
+
):
|
|
253
|
+
"""**API function to create a new table in the database.**
|
|
254
|
+
|
|
255
|
+
**Args:**
|
|
256
|
+
|
|
257
|
+
request: Reference to the FastAPI request object.
|
|
258
|
+
table_name: Name of the table to be created.
|
|
259
|
+
apikey: API Key to authenticate the request.
|
|
260
|
+
|
|
261
|
+
**Raises:**
|
|
262
|
+
|
|
263
|
+
APIResponse:
|
|
264
|
+
Raises the HTTPStatus object with a status code and detail as response.
|
|
265
|
+
"""
|
|
266
|
+
await auth.validate(request, apikey)
|
|
267
|
+
try:
|
|
268
|
+
models.database.create_table(table_name, ["key", "value"])
|
|
269
|
+
except sqlite3.OperationalError as error:
|
|
270
|
+
LOGGER.error(error)
|
|
271
|
+
raise exceptions.APIResponse(
|
|
272
|
+
status_code=HTTPStatus.EXPECTATION_FAILED.real, detail=error.args[0]
|
|
273
|
+
)
|
|
274
|
+
raise exceptions.APIResponse(
|
|
275
|
+
status_code=HTTPStatus.OK.real, detail=HTTPStatus.OK.phrase
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
async def health() -> Dict[str, str]:
|
|
280
|
+
"""Healthcheck endpoint.
|
|
281
|
+
|
|
282
|
+
Returns:
|
|
283
|
+
Dict[str, str]:
|
|
284
|
+
Returns the health response.
|
|
285
|
+
"""
|
|
286
|
+
return {"STATUS": "OK"}
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
async def docs() -> RedirectResponse:
|
|
290
|
+
"""Redirect to docs page.
|
|
291
|
+
|
|
292
|
+
Returns:
|
|
293
|
+
RedirectResponse:
|
|
294
|
+
Redirects the user to ``/docs`` page.
|
|
295
|
+
"""
|
|
296
|
+
return RedirectResponse("/docs")
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def get_all_routes() -> List[APIRoute]:
|
|
300
|
+
"""Get all the routes to be added for the API server.
|
|
301
|
+
|
|
302
|
+
Returns:
|
|
303
|
+
List[APIRoute]:
|
|
304
|
+
Returns the routes as a list of APIRoute objects.
|
|
305
|
+
"""
|
|
306
|
+
dependencies = [
|
|
307
|
+
Depends(dependency=rate_limit.RateLimiter(each_rate_limit).init)
|
|
308
|
+
for each_rate_limit in models.env.rate_limit
|
|
309
|
+
]
|
|
310
|
+
routes = [
|
|
311
|
+
APIRoute(path="/", endpoint=docs, methods=["GET"], include_in_schema=False),
|
|
312
|
+
APIRoute(
|
|
313
|
+
path="/health", endpoint=health, methods=["GET"], include_in_schema=False
|
|
314
|
+
),
|
|
315
|
+
APIRoute(
|
|
316
|
+
path="/get-secret",
|
|
317
|
+
endpoint=get_secret,
|
|
318
|
+
methods=["GET"],
|
|
319
|
+
dependencies=dependencies,
|
|
320
|
+
),
|
|
321
|
+
APIRoute(
|
|
322
|
+
path="/get-secrets",
|
|
323
|
+
endpoint=get_secrets,
|
|
324
|
+
methods=["GET"],
|
|
325
|
+
dependencies=dependencies,
|
|
326
|
+
),
|
|
327
|
+
APIRoute(
|
|
328
|
+
path="/get-table",
|
|
329
|
+
endpoint=get_table,
|
|
330
|
+
methods=["GET"],
|
|
331
|
+
dependencies=dependencies,
|
|
332
|
+
),
|
|
333
|
+
APIRoute(
|
|
334
|
+
path="/put-secret",
|
|
335
|
+
endpoint=put_secret,
|
|
336
|
+
methods=["PUT"],
|
|
337
|
+
dependencies=dependencies,
|
|
338
|
+
),
|
|
339
|
+
APIRoute(
|
|
340
|
+
path="/delete-secret",
|
|
341
|
+
endpoint=delete_secret,
|
|
342
|
+
methods=["DELETE"],
|
|
343
|
+
dependencies=dependencies,
|
|
344
|
+
),
|
|
345
|
+
APIRoute(
|
|
346
|
+
path="/create-table",
|
|
347
|
+
endpoint=create_table,
|
|
348
|
+
methods=["POST"],
|
|
349
|
+
dependencies=dependencies,
|
|
350
|
+
),
|
|
351
|
+
]
|
|
352
|
+
return routes
|
vaultapi/squire.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import pathlib
|
|
4
|
+
|
|
5
|
+
import yaml
|
|
6
|
+
|
|
7
|
+
from .models import EnvConfig
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def envfile_loader(filename: str | os.PathLike) -> EnvConfig:
|
|
11
|
+
"""Loads environment variables based on filetypes.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
filename: Filename from where env vars have to be loaded.
|
|
15
|
+
|
|
16
|
+
Returns:
|
|
17
|
+
EnvConfig:
|
|
18
|
+
Returns a reference to the ``EnvConfig`` object.
|
|
19
|
+
"""
|
|
20
|
+
env_file = pathlib.Path(filename)
|
|
21
|
+
if env_file.suffix.lower() == ".json":
|
|
22
|
+
with open(env_file) as stream:
|
|
23
|
+
env_data = json.load(stream)
|
|
24
|
+
return EnvConfig(**{k.lower(): v for k, v in env_data.items()})
|
|
25
|
+
elif env_file.suffix.lower() in (".yaml", ".yml"):
|
|
26
|
+
with open(env_file) as stream:
|
|
27
|
+
env_data = yaml.load(stream, yaml.FullLoader)
|
|
28
|
+
return EnvConfig(**{k.lower(): v for k, v in env_data.items()})
|
|
29
|
+
elif not env_file.suffix or env_file.suffix.lower() in (
|
|
30
|
+
".text",
|
|
31
|
+
".txt",
|
|
32
|
+
"",
|
|
33
|
+
):
|
|
34
|
+
return EnvConfig.from_env_file(env_file)
|
|
35
|
+
else:
|
|
36
|
+
raise ValueError(
|
|
37
|
+
"\n\tUnsupported format for 'env_file', can be one of (.json, .yaml, .yml, .txt, .text, or null)"
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def load_env(**kwargs) -> EnvConfig:
|
|
42
|
+
"""Merge env vars from env_file with kwargs, giving priority to kwargs.
|
|
43
|
+
|
|
44
|
+
See Also:
|
|
45
|
+
This function allows env vars to be loaded partially from .env files and partially through kwargs.
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
EnvConfig:
|
|
49
|
+
Returns a reference to the ``EnvConfig`` object.
|
|
50
|
+
"""
|
|
51
|
+
if env_file := kwargs.get("env_file"):
|
|
52
|
+
file_env = envfile_loader(env_file).model_dump()
|
|
53
|
+
elif os.path.isfile(".env"):
|
|
54
|
+
file_env = envfile_loader(".env").model_dump()
|
|
55
|
+
else:
|
|
56
|
+
file_env = {}
|
|
57
|
+
merged_env = {**file_env, **kwargs}
|
|
58
|
+
return EnvConfig(**merged_env)
|
vaultapi/version.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.0.0"
|