Skip to content

Commit 830ee02

Browse files
Flask-Vault Public Release
0 parents  commit 830ee02

File tree

16 files changed

+790
-0
lines changed

16 files changed

+790
-0
lines changed

.flake8

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[flake8]
2+
ignore = E226,E302,E41,W503,C901
3+
max-line-length = 130
4+
exclude = tests/*
5+
max-complexity = 10

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
__pycache__
2+
*__pycache__*
3+
pyrigthconfig.json
4+
.vscode
5+
.mypy_cache
6+
dist
7+
ob.egg-info
8+
.venv
9+
./install.sh
10+
TODO.txt
11+
poetry.lock
12+
dist
13+
ob.egg-info

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023-present Adriano Romanazzo <github.com/multiversecoder>
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.

MANIFEST.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prune Flask_Vault.egg-info
2+
prune tests
3+
include requirements.txt
4+
include README.md
5+
include LICENSE
6+
include MANIFEST.in
7+
include setup.cfg
8+
include .flake8
9+
global-exclude __pycache__
10+
global-exclude *.py[co]

README.md

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
# About Flask Vault
2+
3+
[**Flask-Vault**](https://github.com/multiversecoder/Flask-Vault) provides several cli commands to store secrets that you do not want to keep in the clear, using symmetric encryption with **AES-GCM**. These commands allow you to save very important credentials such as API keys, database credentials, etc.
4+
5+
In addition, **Flask-Vault** contains several helpers to simplify the encryption and decryption of data.
6+
7+
# Why Flask-Vault instead of Dotenv?
8+
9+
In the world of web development, safeguarding sensitive information is a paramount concern. When it comes to managing credentials when using [Flask](https://flask.palletsprojects.com/), two prominent options emerge: **Flask-Vault** and [Dotenv](https://github.com/theskumar/python-dotenv). While both have their merits, **Flask-Vault** stands out as the superior choice for securing critical data.
10+
11+
**Flask-Vault** offers a robust solution for protecting sensitive information. Stored in an encrypted credentials.yml.enc file, this data is shielded by an added layer of security. It can only be decrypted with the master key, ensuring that even if the file falls into the wrong hands, the information remains inaccessible.
12+
13+
Additionally, the encrypted editor, accessed through `flask vault edit`, allows for secure direct editing of the credentials file. This feature is invaluable for making swift adjustments to sensitive information without compromising security.
14+
15+
While both **Flask-Vault** and **Dotenv** serve important functions, **Flask-Vault** stands as the superior choice for securing sensitive data. Its robust encryption and seamless integration within **Flask** make it the ideal solution for managing critical information. By prioritizing security without sacrificing accessibility, **Flask-Vault** empowers developers to build and maintain applications with the utmost confidence in their data protection measures.
16+
17+
# About Flask-Vault Cryptography
18+
19+
The encryption used by `flask_vault` is **AES-GCM** with a *128-bit key*.
20+
21+
### Why AES-GCM 128?
22+
23+
Here are some benefits of using AES-GCM with a 128-bit key:
24+
- Security: AES-GCM is considered secure and is widely used in various applications including TLS (Transport Layer Security) for securing internet communication.
25+
26+
- Efficiency: AES-GCM is known for being relatively efficient in terms of computational resources. This is especially important in scenarios where computational power may be limited, such as on IoT (Internet of Things) devices.
27+
28+
- Fast Encryption and Decryption: AES-GCM is optimized for modern processors, which means that it can encrypt and decrypt data relatively quickly. This is important for performance-sensitive applications.
29+
30+
- Parallelization: AES-GCM encryption and decryption can be parallelized, which means that it can take advantage of multiple processing cores in modern CPUs.
31+
32+
- Authenticated Encryption with Associated Data (AEAD): AES-GCM provides both confidentiality and integrity, which means that not only is the data encrypted, but it also includes a message authentication code (MAC) to verify that the data has not been tampered with.
33+
34+
- Nonce-Based: AES-GCM requires a unique initialization vector (IV), called a nonce, for each encryption operation. This means that even if you encrypt the same data with the same key multiple times, the ciphertext will be different, adding an extra layer of security.
35+
36+
- Widely Supported: AES-GCM is supported by many cryptographic libraries and frameworks, making it a practical choice for a wide range of applications.
37+
38+
- Compliance: AES-GCM is often recommended or required by various security standards and compliance frameworks.
39+
40+
# Flask-Vault Dependencies
41+
42+
Flask-Vault uses few dependencies to secure data and files, here are the 2 main dependencies of this library:
43+
- [Cryptography](https://cryptography.io/)
44+
- [PyYAML](https://pyyaml.org/)
45+
46+
```toml
47+
python = ">=3.7"
48+
cryptography = "^41.0.3"
49+
pyyaml = "^6.0.1"
50+
```
51+
52+
# Getting Started with Flask-Vault
53+
54+
### Installing
55+
56+
Install and update using [pip](https://pip.pypa.io/en/stable/getting-started/)
57+
58+
```sh
59+
$> pip install Flask-Vault
60+
```
61+
62+
Install and update using [poetry](https://python-poetry.org/)
63+
64+
```sh
65+
$> poetry add Flask-Vault
66+
```
67+
68+
# Documentation
69+
70+
## How to integrate Flask-Vault with your Flask application.
71+
72+
## Use `flask_vault.cli.vault_cli` to enable `Flask-Vault`.
73+
74+
```python
75+
from flask import Flask
76+
from flask_vault.cli import vault_cli
77+
78+
app = Flask(__name__)
79+
80+
...
81+
# Register Command using the
82+
app.cli.add_command(vault_cli)
83+
...
84+
85+
```
86+
87+
88+
## Using a different editor to show/edit credentials
89+
90+
The default editor used by `Flask-Vault` is **vi**.
91+
92+
The example below shows how to use an editor other than **vi** to show or edit credentials saved in `credentials.yml.enc`. As shown, the example will use `nano` editor to use the `flask vault show` command.
93+
94+
```sh
95+
$> EDITOR=nano flask vault show
96+
```
97+
98+
## Cli Commands
99+
100+
- > **flask vault init**: The `flask vault init` command will initialize the environment needed for Flask-Vault to operate. This command will create the `credentials.yml.enc` file, the `master.key` file and the `tmp` folder (which will be used internally by Flask-Vault). If you run this command a second time, no action will be taken since you will not be able to overwrite the created files.
101+
- > **flask vault get [secret_name]**: The `flask vault get` command will decrypt `credentials.yml.enc` and display the selected secret in terminal.
102+
- > **flask vault show**: The `flask vault show` command will decrypt the contents of the `credentials.yml.enc` file using `master.key` and open it in read-only mode to show the saved credentials.
103+
- > **flask vault edit**: The `flask vault show` command will decrypt the contents of the `credentials.yml.enc` file using `master.key` and open it in edit mode.
104+
- > **flask vault encrypt [filename]**: The `flask vault encrypt` command will create an encrypted file. The generated file will be protected by AES-GCM encryption and will use a `.enc` extension to distinguish it from the plaintext file.
105+
- > **flask vault decrypt [filename]**: The `flask vault decrypt` command will decrypt a file with the extension `.enc` and display its contents in the terminal.
106+
107+
108+
## Encrypting data using `flask_vault.utils.aes_gcm_encrypt`
109+
110+
Encrypting data without exposing the encryption key within the application using `flask_vault.utils.aes_gcm_encrypt`
111+
112+
```python
113+
from flask import Flask
114+
from flask_vault.utils import aes_gcm_encrypt
115+
116+
app = Flask(__name__)
117+
118+
119+
@app.route("/encrypted", methods=["GET"])
120+
def encrypted():
121+
ctx = {
122+
"encrypted": aes_gcm_encrypt("my.app.secret")
123+
}
124+
return render_template("encrypted.html", **ctx)
125+
```
126+
127+
## Decrypting data using `flask_vault.utils.aes_gcm_decrypt`
128+
129+
Decrypting data without exposing the encryption key within the application using `flask_vault.utils.aes_gcm_decrypt`
130+
131+
```python
132+
from flask import Flask
133+
from flask_vault.utils import aes_gcm_decrypt
134+
135+
136+
app = Flask(__name__)
137+
138+
@æpp.route("/decrypt", methods=["GET"])
139+
def decrypt():
140+
encrypted_data = request.args.get("encrypted", None)
141+
ctx = {
142+
"plaintext": aes_gcm_decrypt(encrypted_data) if encrypted_data is not None else "Missing encrypted data!"
143+
}
144+
return render_template("decrypt.html", **ctx)
145+
```
146+
147+
## Obtaining credentials stored inside the `credentials.yml.enc` file
148+
149+
The example below shows how to get secrets from the `credentials.yml.enc` file using Flask-Vault's `get_secrets` function. In this example `get_secret` will be used to configure the database, preventing anyone who does not have access to the `master.key` from reading the username, password, and database name in clear text
150+
151+
152+
#### credentials.yml.enc (after editing)
153+
154+
```yaml
155+
---
156+
# ... other stuff
157+
db.name: my-db-name
158+
db.username: root
159+
db.password: my-db-password
160+
```
161+
162+
### How to get secrets from `credentials.yml.enc`
163+
164+
Use the `flask_vault.utils.get_secret` function to obtain secrets and credentials stored inside the `credentials.yml.enc` file.
165+
166+
#### app.py using `flask_vault.utils.get_secret`
167+
```python
168+
import sys
169+
import mariadb
170+
from flask import Flask, request, g
171+
from flask_vault.utils import get_secret
172+
173+
app = Flask(__name__)
174+
175+
def get_db():
176+
db = getattr(g, "_database", None)
177+
if db is None:
178+
try:
179+
conn = mariadb.connect(
180+
user=get_secret("db.username"),
181+
password=get_secret("db.password")
182+
host="127.0.0.1",
183+
port=3306,
184+
database=get_secret("db.name"),
185+
)
186+
db = g._database = conn
187+
except mariadb.Error as e:
188+
print(f"Error connecting to MariaDB Platform: {e}")
189+
sys.exit(1)
190+
return db
191+
192+
@app.teardown_appcontext
193+
def close_connection(exception):
194+
db = getattr(g, "_database", None)
195+
if db is not None:
196+
db.close()
197+
```
198+
199+
# Contributing
200+
201+
Questions, comments or improvements, please create an issue on [Github](https://github.com/multiversecoder/Flask-Vault/issues).
202+
203+
To suggest a change to the code or documentation, please create a new pull request on GitHub. Also, please squash multiple commits into a single commit in your pull request by rebasing onto the master branch.
204+
205+
206+
# Donation
207+
208+
If you feel that my work has been useful and you are interested in supporting this project and any future projects, please leave me a donation using one of the following cryptocurrencies.
209+
210+
211+
- **Bitcoin (Segwit)**: `bc1q8vnltjge25dks05tv49eknvrar6pzu7837mnvt`
212+
213+
# License
214+
215+
MIT License
216+
217+
Copyright (c) 2023-present Adriano Romanazzo <github.com/multiversecoder>
218+
219+
Permission is hereby granted, free of charge, to any person obtaining a copy
220+
of this software and associated documentation files (the "Software"), to deal
221+
in the Software without restriction, including without limitation the rights
222+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
223+
copies of the Software, and to permit persons to whom the Software is
224+
furnished to do so, subject to the following conditions:
225+
226+
The above copyright notice and this permission notice shall be included in all
227+
copies or substantial portions of the Software.
228+
229+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
230+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
231+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
232+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
233+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
234+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
235+
SOFTWARE.

flask_vault/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)