[英]Launching script to export environment variables from Python
I'm trying to remove all passwords and secrets from applications. 我正在尝试从应用程序中删除所有密码和机密。
To do this, I'm using environment variables. 为此,我正在使用环境变量。 I would like to avoid putting this specific stuff into
~/.bashrc
or other built in files. 我想避免将这些特定内容放入
~/.bashrc
或其他内置文件中。 It would be ideal to call a specific script that exports the desired variables. 调用导出所需变量的特定脚本是理想的。
The reason for this is because the environment variables contain passwords and I'm trying to isolate them outside of all code except the environment scripts that set environment variables. 原因是环境变量包含密码,我试图将它们隔离在除设置环境变量的环境脚本之外的所有代码之外。
To summarize the desired flow: 总结所需的流程:
contents of testpy.py
testpy.py的内容
#!/usr/bin/env python
import os
import subprocess
env_file='/tmp/env_file'
db_pass_var='DBPASS'
#execute bash script that sets the env vars
subprocess.call([".", env_file], shell=True)
#try to get the variable set and print it
print os.getenv(db_pass, 'fail')
contents of /tmp/env_file
/ tmp / env_file的内容
#!/usr/bin/env bash
export DBUSER="myuser"
export DBPASS="mypass"
I've tried various methods like subprocess.Popen
commands I found on other stackoverflow threads but nothing seems to work. 我尝试过各种方法,比如我在其他stackoverflow线程上找到的
subprocess.Popen
命令,但似乎没有任何工作。
Alternatives that I want to avoid: 我想避免的替代方案:
Running the env_file script first, then launching the python code. 首先运行env_file脚本,然后启动python代码。 The goal is to run it all from 1 application.
目标是从1个应用程序运行它。
Putting the passwords into a config file. 将密码放入配置文件中。 The goal is environment variables.
目标是环境变量。 Config files I would like to reserve to pointing to what variable to call, rather than setting the env vars themselves.
配置文件我想保留指向要调用的变量,而不是自己设置env变量。 This is because config files are checked into source control.
这是因为配置文件被检入源代码控制。 The only thing I want to leave out of source control is the actual
export
environment variables themselves which contain all sensitive environment info. 我唯一想要放弃源代码控制的是实际的
export
环境变量本身,它们包含所有敏感的环境信息。
There's an easier way. 有一种更简单的方法。 Just provide a wrapper script that sets the environment variables and then calls your main script.
只需提供一个包装器脚本来设置环境变量,然后调用主脚本。
It's convenient to use the envdir
utility for this. 为此使用
envdir
实用程序很方便。 If you can't use that for some reason, then set env vars directly using os.environ
and spawn off your main script as a child process. 如果由于某种原因无法使用它,那么直接使用
os.environ
设置env vars并将主脚本作为子进程生成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.