从数据库转储中清除敏感信息
django-sanitized-dump的Python项目详细描述
Django消毒垃圾场
清除数据库转储中的敏感信息
支持:
- PostgreSQL
- mysql
入门
pip install django-sanitized-dump
或pip install django-sanitized-dump[MySQL]
如果使用mysql- 将
sanitized_dump
添加到INSTALLED_APPS
- 初始化配置文件:
./manage.py init_sanitizer
- 检查新创建的
.sanitizerconfig
文件并修改卫生策略以满足您的需求。 - 运行
./manage.py check_sanitizerconfig
以验证.sanitizerconfig
是否包含所有模型和字段 - 获取经过清理的数据库转储:
./manage.py create_sanitized_dump > dump.sql
DB卫生
DB卫生设施的重吊装由以下人员完成:https://github.com/andersinno/python-database-sanitizer
配置
配置文件用于定义如何清理数据的策略。策略为每个模型字段定义了一个卫生功能。
示例配置
config:addons:-"ai-sanitizers"-"some-other-lib"strategy:user:first_name:"name.first_name"last_name:"name.last_name"education:created:nullmodified:nullid:nullfield:"education.field"school:"education.school"started:"datetime.datetime"credits:nullinformation:"string.loremipsum_preserved"file_file:null
自定义消毒液示例
# /sanitizers/name.pydefsanitize_first_name(value):returnfaker.first_name()defsanitize_last_name(value):returnfaker.last_name()# /sanitizers/education.pydefsanitize_field(value):return"Some field"defsanitize_schoo(value):return"My school"
验证消毒剂返回值
Note: This should not be done in the initial implementation of the sanitizer but is up to the sanitizer functions. This is just a nice to have but not of a high priority.
# /sanitizers/name.pydefsanitize_first_name(value):returnfaker.first_name()defsanitize_last_name(value):returnfaker.last_name()# /sanitizers/education.pydefsanitize_field(value):return"Some field"defsanitize_schoo(value):return"My school"
验证消毒剂返回值
Note: This should not be done in the initial implementation of the sanitizer but is up to the sanitizer functions. This is just a nice to have but not of a high priority.
Note: This should not be done in the initial implementation of the sanitizer but is up to the sanitizer functions. This is just a nice to have but not of a high priority.
检查返回的值是否与传递给消毒剂的参数值的类型相同。 例如,如果将mysql datetime值传递给消毒剂,则还应返回mysql datetime值。
配置方法分辨率顺序
- 内部的自定义消毒液。/消毒液
- 附加消毒剂(
config.addons
) - 核心消毒剂
django管理命令
消毒垃圾场
config.addons
)./manage.py create_sanitized_dump > dump.sql
- 警告未处理的字段
- 创建数据库转储(
mysqldump
/pgdump
) - 使用消毒剂
检查消毒垃圾场
./manage.py check_sanitizerconfig
- 如果存在未处理的数据库字段,则返回错误代码
check可用于ci环境中检测模型中不存在的更改 消毒剂配置。
初始消毒剂
./manage.py init_sanitizer
- 从当前数据库状态创建配置