Skip to content

Commit

Permalink
Merge pull request #7 from digorgonzola/db_aws_secret
Browse files Browse the repository at this point in the history
Db aws secret
  • Loading branch information
digorgonzola authored Nov 23, 2023
2 parents 346fa27 + 53f2fd5 commit b2d2d38
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 9 deletions.
28 changes: 28 additions & 0 deletions app/app/aws_secrets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import boto3
from botocore.exceptions import ClientError


def get_secret(secret_name, region_name):

secret_name = secret_name
region_name = region_name

# Create a Secrets Manager client
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=region_name
)

try:
get_secret_value_response = client.get_secret_value(
SecretId=secret_name
)
except ClientError as e:
# For a list of exceptions thrown, see
# https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
raise e

secret = get_secret_value_response['SecretString']

return secret
14 changes: 11 additions & 3 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"""

import os
from .aws_secrets import get_secret
import json

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -40,7 +42,7 @@
)
)

DEFAULT_AUTO_FIELD='django.db.models.AutoField'
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

# Application definition
INSTALLED_APPS = [
Expand Down Expand Up @@ -90,6 +92,13 @@
WSGI_APPLICATION = 'app.wsgi.application'


# Retrieve the database secret
db_secret = None
if bool(int(os.environ.get('GET_DB_SECRET', 1))):
db_secret = json.loads(get_secret(os.environ.get('DB_SECRET_NAME'), os.environ.get('DB_SECRET_REGION')))

db_pass = os.environ.get('DB_PASS') if db_secret is None else db_secret['password']

# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

Expand All @@ -99,7 +108,7 @@
'HOST': os.environ.get('DB_HOST'),
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASS'),
'PASSWORD': db_pass,
}
}

Expand Down Expand Up @@ -132,7 +141,6 @@

USE_I18N = True


USE_TZ = True


Expand Down
40 changes: 40 additions & 0 deletions docker-compose-aws-secret.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: "3"

services:
app:
build:
context: .
image: api
ports:
- "8000:8000"
volumes:
- ./app:/app
- ${HOME}/.aws:/home/user/.aws
command: >
sh -c "python manage.py wait_for_db &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
environment:
- AWS_PROFILE=YOUR_AWS_PROFILE
- DB_HOST=db
- DB_NAME=api
- DB_USER=postgres
- DB_SECRET_NAME=/your/rds/secret
- DB_SECRET_REGION=aa-something-1
- DEBUG=1
- S3_STORAGE_BACKEND=0
depends_on:
db:
condition: service_healthy

db:
image: postgres:12-alpine
environment:
- POSTGRES_DB=api
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=PUT_THE_SECRET_PASSWORD_VALUE_HERE
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5
5 changes: 3 additions & 2 deletions docker-compose-proxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ services:
- static_data:/vol/web
environment:
- DB_HOST=db
- DB_NAME=app
- DB_NAME=api
- DB_USER=postgres
- DB_PASS=supersecretpassword
- GET_DB_SECRET=0
- ALLOWED_HOSTS=*
- ALLOWED_CIDR_NETS=127.0.0.0/8
- S3_STORAGE_BACKEND=0
Expand All @@ -36,7 +37,7 @@ services:
db:
image: postgres:10-alpine
environment:
- POSTGRES_DB=app
- POSTGRES_DB=api
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=supersecretpassword
healthcheck:
Expand Down
5 changes: 3 additions & 2 deletions docker-compose-s3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ services:
python manage.py runserver 0.0.0.0:8000"
environment:
- DB_HOST=db
- DB_NAME=app
- DB_NAME=api
- DB_USER=postgres
- DB_PASS=supersecretpassword
- GET_DB_SECRET=0
- DEBUG=1
- S3_STORAGE_BACKEND=1
- S3_STORAGE_BUCKET_NAME=static
Expand Down Expand Up @@ -47,7 +48,7 @@ services:
db:
image: postgres:12-alpine
environment:
- POSTGRES_DB=app
- POSTGRES_DB=api
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=supersecretpassword
healthcheck:
Expand Down
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ services:
python manage.py runserver 0.0.0.0:8000"
environment:
- DB_HOST=db
- DB_NAME=app
- DB_NAME=api
- DB_USER=postgres
- DB_PASS=supersecretpassword
- GET_DB_SECRET=0
- DEBUG=1
- S3_STORAGE_BACKEND=0
depends_on:
Expand All @@ -27,7 +28,7 @@ services:
db:
image: postgres:12-alpine
environment:
- POSTGRES_DB=app
- POSTGRES_DB=api
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=supersecretpassword
healthcheck:
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
aws-secretsmanager-caching>=1.1.1.5,<1.1.2.0
boto3>=1.29.4,<1.30.0
botocore>=1.32.6,<1.33.0
Django>=4.2.7,<4.3.0
djangorestframework>=3.14.0,<3.15.0
django-allow-cidr>=0.7.1,<0.8.0
Expand Down
8 changes: 8 additions & 0 deletions tests/config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
schemaVersion: 2.0.0

commandTests:
- name: check aws-secretsmanager-caching is installed
command: pip
args: [ "show", "-q", "aws-secretsmanager-caching" ]
exitCode: 0
- name: check boto3 is installed
command: pip
args: [ "show", "-q", "boto3" ]
exitCode: 0
- name: check botocore is installed
command: pip
args: [ "show", "-q", "botocore" ]
exitCode: 0
- name: check django is installed
command: pip
args: ["show", "-q", "Django"]
Expand Down

0 comments on commit b2d2d38

Please sign in to comment.