feat: support parse config file with Variable Expansion for some secret keys #342
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
根据早期讨论的提案 -> 在配置文件中对敏感 token 支持环境变量语法声明。
实现与改动
本 PR 增加支持的配置项包括:
downloader.username
downloader.password
rss_parser.token
notification.token
notification.chat_id
proxy.username
proxy.password
本次实现对代码中所有使用处无感无改动,实现原理:
在
pydantic
Model 上使用@property
getter 字段,通过原生expandvars
方法做 shell 变量扩展(Variable expansion),用于支持原本形如
settings.rss_parser.token
直接访问的用法对于
pydantic
Field 字段添加alias
字段支持,并对dict()
方法添加默认by_alias
参数,用于 Model 的序列化和反序列化过程中字段名称正确,以支持如下几种使用方式:
settings.dict()
Config.parse_obj()
settings.__dict__.update()
这总体使得所有访问值的地方(形如
settings.rss_parser.token
) 都能获取到展开后具体环境变量的值,而通过
settings.dict()
给到 WebUI 和保存配置的是原始字符串${XXXX_VAR}