[英]Managing django secrets
我正在創建一個將在 AWS 中托管的 Django web 應用程序。 我正在我的筆記本電腦(Mac)上開發並使用 docker compose 在 AWS 中運行 postgres 數據庫。 因此,有時我在 PyCharm 中使用我的 Mac 中的開發數據庫,而其他時候我需要編譯和運行 docker 以測試該功能在容器中是否有效。
我面臨的挑戰是管理所有這些地點的秘密。 最好有一個“秘密保險庫”,它可以在運行時從 docker 或 PyCharm 中訪問,以使生活更簡單。 我熟悉 .env 文件以及導入這些文件的方式,但這也需要我在構建時將它們復制到 docker 容器中。
是否有一些“簡單而強大”的方法可以更輕松地管理它? 感覺就像在不同的環境中擁有不同的 .env 文件的副本會帶來自己的風險。
經過一番研究,我為此選擇使用 Amazon Secrets Manager ( https://aws.amazon.com/secrets-manager/ )。 這樣,我只需要擔心兩個關鍵值即可保護我的所有變量。 我已經編輯了 my.profile 文件(在我的本地和 docker 環境中)以包括:
export aws_access_key_id=AWSPUBLICKEY
export aws_secret_access_key=AWSSUPERSECRETKEY
在 Python 中,然后我導入環境變量,然后可以按如下方式從 AWS 訪問機密(請注意,我必須使用完整的 arn 地址引用“secret_name”):
import boto3
import base64
import os
from botocore.exceptions import ClientError
# Use this code snippet in your app.
# If you need more information about configurations or implementing the sample code, visit the AWS docs:
# https://aws.amazon.com/developers/getting-started/python/
# add aws_access_key_id / aws_secret_access_key to environment variables
global AWS_ACCESS
global AWS_SECRET
AWS_ACCESS = os.environ.get('aws_access_key_id')
AWS_SECRET = os.environ.get('aws_secret_access_key')
def get_secret():
secret_name = "arn:aws:secretsmanager:us-east-1:547847502175:secret:python_multiple_test-rQR9qo"
region_name = "us-east-1"
#print(AWS_ACCESS)
#print(AWS_SECRET)
# Create a Secrets Manager client
session = boto3.session.Session()
client = session.client(
aws_access_key_id=AWS_ACCESS,
aws_secret_access_key=AWS_SECRET,
service_name='secretsmanager',
region_name=region_name
)
# In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
# See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
# We rethrow the exception by default.
try:
get_secret_value_response = client.get_secret_value(
SecretId=secret_name
)
except ClientError as e:
if e.response['Error']['Code'] == 'DecryptionFailureException':
# Secrets Manager can't decrypt the protected secret text using the provided KMS key.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
elif e.response['Error']['Code'] == 'InternalServiceErrorException':
# An error occurred on the server side.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
elif e.response['Error']['Code'] == 'InvalidParameterException':
# You provided an invalid value for a parameter.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
elif e.response['Error']['Code'] == 'InvalidRequestException':
# You provided a parameter value that is not valid for the current state of the resource.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
elif e.response['Error']['Code'] == 'ResourceNotFoundException':
# We can't find the resource that you asked for.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
else:
# Decrypts secret using the associated KMS key.
# Depending on whether the secret is a string or binary, one of these fields will be populated.
if 'SecretString' in get_secret_value_response:
secret = get_secret_value_response['SecretString']
else:
decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])
# Your code goes here.
print(secret)
這樣,我的 Python 代碼可以盡可能保持干凈,依靠密鑰檢索來訪問我的秘密密碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.