-
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.
feat(sync-secrets-with-s3): automatically update secrets with the buc…
…ket in order to communicate with the napse app
- Loading branch information
Tom JEANNESSON
committed
Jan 7, 2024
1 parent
7dcde74
commit 3e33a0b
Showing
12 changed files
with
74 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .db_essentials import sync_master_key_s3 |
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,30 @@ | ||
import json | ||
from time import sleep | ||
|
||
import environ | ||
from django.db.models.signals import post_migrate | ||
from django.dispatch import receiver | ||
from django_napse.core.settings import napse_settings | ||
from utils import sync_file_with_s3 | ||
|
||
|
||
@receiver(post_migrate) | ||
def sync_master_key_s3(sender, **kwargs): | ||
env = environ.Env() | ||
if "AWS_ACCESS_KEY_ID" not in env or "AWS_SECRET_ACCESS_KEY" not in env or "AWS_S3_BUCKET_URI" not in env: | ||
print("AWS credentials not found. Skipping sync_master_key_s3") | ||
return | ||
master_key_created = False | ||
counter = 0 | ||
while not master_key_created: | ||
with open(napse_settings.NAPSE_SECRETS_FILE_PATH, "r") as f: | ||
data = json.load(f) | ||
if "master_key" in data: | ||
master_key_created = True | ||
counter += 1 | ||
sleep(1) | ||
print(f"Waiting for master key to be created. {counter} seconds passed") | ||
if counter > 10: | ||
error_msg = "Master key was not created in 10 seconds" | ||
raise ValueError(error_msg) | ||
sync_file_with_s3(napse_settings.NAPSE_SECRETS_FILE_PATH, "napse-eb-bucket", "napse-secrets.json") |
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
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 @@ | ||
from .sysnc_file_with_s3 import sync_file_with_s3 |
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,8 @@ | ||
from django.db.backends.sqlite3.base import SqliteDatabaseWrapper | ||
|
||
|
||
class SqliteWithBusyTimeout(SqliteDatabaseWrapper): | ||
def _cursor(self): | ||
cursor = super()._cursor() | ||
cursor.execute("PRAGMA busy_timeout = 5000") | ||
return cursor |
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,17 @@ | ||
import json | ||
|
||
import boto3 | ||
import environ | ||
|
||
|
||
def sync_file_with_s3(path: str, bucket_name: str, key: str): | ||
env = environ.Env() | ||
if "AWS_ACCESS_KEY_ID" not in env or "AWS_SECRET_ACCESS_KEY" not in env or "AWS_S3_BUCKET_URI" not in env: | ||
error_msg = "AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY or AWS_S3_BUCKET_URI is not set in .env file" | ||
raise ValueError(error_msg) | ||
|
||
s3 = boto3.client("s3", aws_access_key_id=env.get_value("AWS_ACCESS_KEY_ID"), aws_secret_access_key=env.get_value("AWS_SECRET_ACCESS_KEY")) | ||
with open(path, "r") as f: | ||
data = json.load(f) | ||
s3.put_object(Bucket=bucket_name, Key=key, Body=json.dumps(data)) | ||
print(f"Successfully synced {path} with S3 bucket {bucket_name} with key {key}") |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
brew install [email protected] | ||
python3.11 -m pip install --upgrade pip | ||
pip3 install virtualenv | ||
pip3 install pip-tools | ||
python3 -m virtualenv .venv --python=python3.11 | ||
|
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