2017-08-09 129 views
0

我需要访问需要用户名和密码的服务,我不希望将其存储在代码中。该代码在具有kerberos的Linux服务器上运行。在linux中存储凭证

任何人都可以指出的例子,允许我在Linux上用Python或bash shell从linux存储密码?

目前我在一个只有当前用户权限的文件中使用明文密码。

我已经指出了一个指南使用PowerShell:

#The following command create a txt file containing a encrypted version of 
#the password string (in this example "[email protected]"). This is something you do once while 
#logged on as the account that will eventually decrypt the password 
####################################################################################### 
"[email protected]" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\Temp\Password.txt" 


####################################################################################### 
#In your script that are using the encrypted password, you can do the following: 
####################################################################################### 
# 1) Read the encrypted password from the txt file and convert it to a SecureString object 
$pass = Get-Content "C:\Temp\Password.txt" | ConvertTo-SecureString 

# 2) Convert the SecureString object to a plain text variable that can be used in the script 
#The decrypted password is now available in the $UnsecurePassword variable. 
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass) 
$UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) 
cls 
Write-Host $UnsecurePassword 
+0

将加密密码放入文件没有多大意义。脚本必须包含解密密钥,所以任何可以读取脚本的人都可以解密文件中的密码。 – Barmar

+0

@Barmar >> Powershell技巧中涉及到一些Windows魔法,也就是说,如果您将加密文件复制到另一台机器或从另一个用户的会话访问它,您将无法使用“automagic”键。好的,在Windows中有多个特权升级问题,但这是另一回事...... –

+0

[我需要在Python中安全地存储用户名和密码,我的选项是什么?](https://stackoverflow.com/questions/7014953/I-需要对安全店内-A-的用户名和密码,在-python的 - 什么 - 是 - 我的选项) –

回答

0

对于存储配置值,我建议使用configparser,让您存储任何类型的配置和参数的配置文件。

存储用户名和密码当然非常敏感。一种方法是用主密钥加密数据,只有您自己知道并且不在任何地方进行硬编码。相反,每次运行或启动脚本时,都可以提示该主密钥,然后解密存储在配置文件中的加密密码。这种方法解释为here