Django,安全性和设置 [英] Django, Security and Settings

查看:200
本文介绍了Django,安全性和设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处中,我们将所有数据库信息添加为文本:

From here, we add all database info as text:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'mydatabase',
    'USER': 'mydatabaseuser',
    'PASSWORD': 'mypassword',
    'HOST': '127.0.0.1',
    'PORT': '5432',
    }
 }

这是一种安全的方法吗?有什么方法可以将此数据另存为加密数据?

Is it a secure way? Is there any way to save this data as Encrypted data?

推荐答案

这是不安全的,任何有权访问您的源代码管理的人现在可以访问您的数据库。

It isn't secure, anyone with access to your source control now has access to your database.

存储敏感数据的两种主要方法是使用环境变量或通过json文件

The two main methods of storing sensitive data are either with environment variables or via a json file


摘录自设置-使用JSON文件隐藏机密数据。原始作者是 Antoine Pinsard 弗雷德利。归属详细信息可以在贡献者页面上找到。来源已获得 CC BY-SA 3.0 的许可,并且可以在文档存档。参考主题ID:942和示例ID:8734。

Excerpted from Settings - Hiding secret data using a JSON file. The original authors were Antoine Pinsard and fredley. Attribution details can be found on the contributor page. The source is licenced under CC BY-SA 3.0 and may be found in the Documentation archive. Reference topic ID: 942 and example ID: 8734.

使用JSON文件隐藏机密数据

在使用诸如Git或SVN之类的VCS时,某些机密数据绝不能进行版本控制(无论存储库是公共的还是私有的)。

When using a VCS such as Git or SVN, there are some secret data that must never be versioned (whether the repository is public or private).

在这些数据中,您找到了 SECRET_KEY 设置和数据库密码。

Among those data, you find the SECRET_KEY setting and the database password.

从版本控制中隐藏这些设置的常见做法是在项目的根目录下创建文件 secrets.json 感谢 Django的两个独家新闻 ):

A common practice to hide these settings from version control is to create a file secrets.json at the root of your project (thanks "Two Scoops of Django" for the idea):

{
    "SECRET_KEY": "N4HE:AMk:.Ader5354DR453TH8SHTQr",
    "DB_PASSWORD": "v3ry53cr3t"
}

并将其添加到您的忽略列表( .gitignore 作为git):

And add it to your ignore list (.gitignore for git):

*.py[co]
*.sw[po]
*~
/secrets.json

然后将以下功能添加到您的设置模块:

Then add the following function to your settings module:

import json
import os
from django.core.exceptions import ImproperlyConfigured

with open(os.path.join(BASE_DIR, 'secrets.json')) as secrets_file:
    secrets = json.load(secrets_file)

def get_secret(setting, secrets=secrets):
    """Get secret setting or fail with ImproperlyConfigured"""
    try:
        return secrets[setting]
    except KeyError:
        raise ImproperlyConfigured("Set the {} setting".format(setting))

然后按以下方式填写设置:

Then fill the settings this way:

SECRET_KEY = get_secret('SECRET_KEY')
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgres',
        'NAME': 'db_name',
        'USER': 'username',
        'PASSWORD': get_secret('DB_PASSWORD'),
    },
}

积分:< a href = https://www.twoscoopspress.com/ rel = noreferrer> Django的两个摘要:Django 1.8的最佳实践,作者Daniel Roy Greenfeld和Audrey RoyGreenfeld。版权所有2015 Two Scoops Press(ISBN 978-0981467344)


摘录自设置-使用环境变量来管理服务器之间的设置。原始作者为 sudshekhar ssice NBajanca 。归属详细信息可在贡献者页面上找到。来源已获得 CC BY-SA 3.0 的许可,并且可以在文档存档。参考主题ID:942和示例ID:3580。

Excerpted from Settings - Using Environment variables to manage Settings across servers. The original authors were sudshekhar, ssice and NBajanca. Attribution details can be found on the contributor page. The source is licenced under CC BY-SA 3.0 and may be found in the Documentation archive. Reference topic ID: 942 and example ID: 3580.

使用环境变量来管理服务器之间的设置

十二要素应用程序

由于配置可能会在部署环境之间发生变化,因此这是一种非常有趣的修改配置,而无需挖掘应用程序的源代码,也无需在应用程序文件和源代码存储库中保密。

As configurations are likely to change between deployment environments, this is a very interesting way to modify the configuration without having to dig in the app's source code, as well as keeping secrets outside the application files and source code repository.

在Django中,主要设置位于<项目文件夹中的code> settings.py 。由于它是一个简单的Python文件,因此可以使用标准库中Python的 os 模块访问环境(甚至具有适当的默认值)。

In Django, the main settings are located as settings.py in your project's folder. As it is a simple Python file, you can use Python's os module from the standard library to access the environment (and even have appropriate defaults).

import os

SECRET_KEY = os.environ.get('APP_SECRET_KEY', 'unsafe-secret-key')

DEBUG = os.environ.get('DJANGO_DEBUG', "True") == "True"

ALLOWED_HOSTS = os.environ.get('DJANGO_ALLOWED_HOSTS', '').split()

DATABASES = {
    'default': {
        'ENGINE': os.environ.get('APP_DB_ENGINE', 'django.db.backends.sqlite3'),
        'NAME': os.environ.get('DB_NAME', 'db.sqlite'),    
        'USER': os.environ.get('DB_USER', ''),
        'PASSWORD': os.environ.get('DB_PASSWORD', ''),
        'HOST': os.environ.get('DB_HOST', None),
        'PORT': os.environ.get('DB_PORT', None),
        'CONN_MAX_AGE': 600,
    }
}

使用Django,您可以更改数据库技术,以便您可以在开发计算机上使用sqlite3(对于提交到源代码控制系统,这应该是理智的默认设置)。尽管这是可行的,但不建议这样做:

With Django you can change your database technology, so that you can use sqlite3 on your development machine (and that should be a sane default for committing to a source control system). Although this is possible it is not advisable:


后备服务是应用程序的数据库,排队系统或缓存之类的领域开发/产品奇偶校验很重要。 (十二要素应用-开发人员/产品奇偶校验

这篇关于Django,安全性和设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆