할머니의 콤퓨타 도전기
Django에서 secret key 관리하기 본문
django에서 secret key
는 Git과 같은 공개된 장소에 올리면 안된다. 이를 별도의 json 파일
로 분리해서 관리해야한다.
Django secret key는 cryptographic signing
을 제공하는데 사용된다.
- https://docs.djangoproject.com/en/3.0/topics/signing/
- it can be useful to pass data through an untrusted medium.
- crpytographically signed values can be passed through an untrusted channel safe in the knowledge that any tampering will be detected
이미 노출이 된 상태라면 아래의 Django secret key generator
로 새로운 secret key를 생성할 수 있다.
1. secret key를 관리하기 위해서 secrets.json
파일을 생성해서 동적으로 불러온다.
import json
import sys
SECRETS_PATH = os.path.join(BASE_DIR, 'secrets.json')
secrets = json.loads(open(SECRETS_PATH).read()) # json 파일 python 객체로 변환
for key, value in secrets.items(): # json 파일은 dict 자료형으로 변환됨 따라서 .items()로 key와 value 값 가져옴
setattr(sys.modules[__name__], key, value) # settings 모듈 속성 지정
2. 마지막으로 .gitignore
에 secrets.json
을 추가해주면 된다.
secrets.json
Comments