Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
解析 yaml 配置文件中的的环境变量
模仿 Ruby 的 ERB 能力,把 yaml 配置文件中的环境变量做简单解析。
执行脚本:
python -m pip install --upgrade pip
python -m pip install pyyaml-erb
在代码中引入 pyyaml-erb 包,读取配置 yml 配置文件即可:
import erb.yml as yaml
with open(SETTING_PATH, 'r', encoding='utf-8') as file :
settings = yaml.load(file.read())
配置示例可参考 settings.yml,使用教程可参考单元测试 test_yaml_erb.py。
例如环境变量为 JAVA_HOME
,只需要在 yaml 配置为 <%= ENV["KEY"] %>
或 <%= ${KEY} %>
的值表达式即可识别并解析。
一般而言,值表达式有以下几种配置模式:
key_1: <%= ENV["VAR_1"] %>
: 默认的使用方式key_2: <%= ENV["VAR_2"] or None %>
: 跟默认方式一样,多了默认值为 None,没意义key_3: <%= ENV["VAR_3"] || null %>
: 跟默认方式一样,多了默认值为 None,没意义key_4: <%= ENV["VAR_4"] || "nil" %>
: 跟默认方式一样,多了默认值为 None,没意义key_5: <%= ENV["VAR_5"] || default %>
: 若环境变量不存在,会设置为默认值key_6: "<%= ENV['VAR_6'] or 'default' %>"
: 若环境变量不存在,会设置为默认值key_7: <%= ENV["VAR_7"] || 7 %>
: 若环境变量不存在,会设置为默认值,且默认值会解析为整型key_8: <%= ENV["VAR_8"] || 1.23 %>
: 若环境变量不存在,会设置为默认值,且默认值会解析为浮点型key_9: <%= ENV["VAR_9"] || true %>
: 若环境变量不存在,会设置为默认值,且默认值会解析为布尔型key_10: <%= ENV["VAR_10"] || 'False' %>
: 若环境变量不存在,会设置为默认值,且默认值会解析为布尔型key_0: '<%= ENV["VAR_0"] || ${VAR_11} or default %>'
: 混合模式引号用双引号或单引号都可以,值表达式外围用不用引号包围都可以,表达式之间用
||
或or
都可以
FAQs
Used to parse environment variables in the yaml configuration file
We found that pyyaml-erb demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.