-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set transit time bucketing via env vars
Update runbook and decryption logic
- Loading branch information
1 parent
3a7af40
commit b934c77
Showing
21 changed files
with
199 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Transit Protection | ||
|
||
VaultAPI includes an added security feature that protects retrieved secrets during transit to the client. | ||
|
||
1. Decrypts the requested secret values from the database (uses Fernet algorithm) | ||
2. Constructs a payload with the requested key-value pairs. | ||
3. Encrypts the payload with the API key and a timestamp that's valid for 60s | ||
|
||
### Other security recommendations | ||
|
||
- Set `ALLOWED_ORIGINS` to known origins, consider using reverse-proxy if the origin is public facing. | ||
- Set `ALLOWED_IP_RANGE` to known IPv4 address range, to allow access only to specific IP addresses. | ||
- Set `TRANSIT_KEY_LENGTH` to strong value (`16`/`24`/`32`...) to increase transit security. | ||
- Set `TRANSIT_TIME_BUCKET` to a lower value to set the decryption timeframe to a minimum. | ||
|
||
### Transit decryption logic in various languages | ||
|
||
### Python | ||
|
||
**Install requirements** | ||
```shell | ||
pip install requests cryptography | ||
``` | ||
|
||
**Run decrypt** | ||
```shell | ||
python decrypt.py | ||
``` | ||
|
||
### Go lang | ||
|
||
**Run decrypt** | ||
```shell | ||
go run decrypt.go | ||
``` | ||
|
||
### JavaScript | ||
|
||
**Install requirements** | ||
```shell | ||
npm install axios | ||
``` | ||
|
||
**Run decrypt** | ||
```shell | ||
node decrypt.js | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import base64 | ||
import hashlib | ||
import json | ||
import os | ||
import time | ||
from typing import Any, ByteString, Dict | ||
|
||
import requests | ||
from cryptography.hazmat.primitives.ciphers.aead import AESGCM | ||
|
||
APIKEY = os.environ["APIKEY"] | ||
|
||
TRANSIT_TIME_BUCKET = os.environ.get("TRANSIT_TIME_BUCKET", 60) | ||
TRANSIT_KEY_LENGTH = os.environ.get("TRANSIT_KEY_LENGTH", 60) | ||
HOST = os.environ.get("HOST", "0.0.0.0") | ||
PORT = os.environ.get("PORT", 8080) | ||
|
||
|
||
def transit_decrypt(ciphertext: str | ByteString) -> Dict[str, Any]: | ||
"""Decrypt transit encrypted payload.""" | ||
epoch = int(time.time()) // TRANSIT_TIME_BUCKET | ||
hash_object = hashlib.sha256(f"{epoch}.{APIKEY}".encode()) | ||
aes_key = hash_object.digest()[:TRANSIT_KEY_LENGTH] | ||
if isinstance(ciphertext, str): | ||
ciphertext = base64.b64decode(ciphertext) | ||
decrypted = AESGCM(aes_key).decrypt(ciphertext[:12], ciphertext[12:], b"") | ||
return json.loads(decrypted) | ||
|
||
|
||
def get_cipher() -> str: | ||
"""Get ciphertext from the server.""" | ||
headers = { | ||
"accept": "application/json", | ||
"Authorization": f"Bearer {APIKEY}", | ||
} | ||
params = { | ||
"table_name": "default", | ||
} | ||
response = requests.get( | ||
f"http://{HOST}:{PORT}/get-table", # noqa: HttpUrlsUsage | ||
params=params, | ||
headers=headers, | ||
) | ||
assert response.ok, response.text | ||
return response.json()["detail"] | ||
|
||
|
||
print(transit_decrypt(ciphertext=get_cipher())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.