我正在使用Fabric 2.4.0进行GettingStarted处理,并且无法找到一种优雅安全的方法来解密SSH密钥以将其传递给Fabric。我的代码如下:
#!/usr/bin/env python3
"""fabfile"""
from fabric import Connection
c = Connection('user@ip.address')
result = c.run('uname -s')
在这个简单的示例中,运行python fabfile.py
时出现错误:
File "/Users/user.name/GitHub/app_name/fabfile.py", line 6, in <module>
result = c.run('uname -s')
File "<decorator-gen-3>", line 2, in run
File "/Users/user.name/anaconda/envs/virtual_env/lib/python3.6/site-packages/fabric/connection.py", line 29, in opens
self.open()
File "/Users/user.name/anaconda/envs/virtual_env/lib/python3.6/site-packages/fabric/connection.py", line 615, in open
self.client.connect(**kwargs)
File "/Users/user.name/anaconda/envs/virtual_env/lib/python3.6/site-packages/paramiko/client.py", line 437, in connect
passphrase,
File "/Users/user.name/anaconda/envs/virtual_env/lib/python3.6/site-packages/paramiko/client.py", line 749, in _auth
raise saved_exception
File "/Users/user.name/anaconda/envs/virtual_env/lib/python3.6/site-packages/paramiko/client.py", line 720, in _auth
filename, pkey_class, passphrase
File "/Users/user.name/anaconda/envs/virtual_env/lib/python3.6/site-packages/paramiko/client.py", line 571, in _key_from_filepath
key = klass.from_private_key_file(key_path, password)
File "/Users/user.name/anaconda/envs/virtual_env/lib/python3.6/site-packages/paramiko/pkey.py", line 206, in from_private_key_file
key = cls(filename=filename, password=password)
File "/Users/user.name/anaconda/envs/virtual_env/lib/python3.6/site-packages/paramiko/rsakey.py", line 55, in __init__
self._from_private_key_file(filename, password)
File "/Users/user.name/anaconda/envs/virtual_env/lib/python3.6/site-packages/paramiko/rsakey.py", line 175, in _from_private_key_file
data = self._read_private_key_file("RSA", filename, password)
File "/Users/user.name/anaconda/envs/virtual_env/lib/python3.6/site-packages/paramiko/pkey.py", line 279, in _read_private_key_file
data = self._read_private_key(tag, f, password)
File "/Users/user.name/anaconda/envs/virtual_env/lib/python3.6/site-packages/paramiko/pkey.py", line 329, in _read_private_key
raise PasswordRequiredException("Private key file is encrypted")
paramiko.ssh_exception.PasswordRequiredException: Private key file is encrypted
python fabfile.py
是运行上述命令的可接受语法吗? os.environ()
? 我在authentication的文档中看到结构注释The connect_kwargs.passphrase config option is the most direct way to supply a passphrase to be used automatically.
但是随后紧接着它说:
这种类型的材料不使用实际的磁盘配置文件 总是明智的,但请记住配置系统能够 从其他来源(例如您的Shell环境或 甚至任意的远程数据库。
从安全的角度来看,我担心设置错误的方式。使用环境变量传递ssh密码是否足够安全?如果是这样,如何使此命令与加密的SSH密钥一起使用,才能正常工作的代码片段如何?
#!/usr/bin/env python3
"""fabfile"""
from fabric import Connection
c = Connection('user@ip.address')
result = c.run('uname -s')
答案 0 :(得分:1)
在使用CLI选项执行命令之前,系统会提示您的脚本用户输入SSH密钥的密码。
这需要将密码短语合并到转发给Paramiko的connect_kwargs
选项中。
fab --prompt-for-passphrase <command>
上述方法的问题是每次运行命令时都必须输入密码。
按照文档中的建议,您可以在Shell会话中导出环境变量。 例如
export SSH_PASSPHRASE="gongo-aso!"
然后可以从传递给connect_kwargs
对象的构造函数的Connection
选项中读取并使用它。
#!/usr/bin/env python3
"""fabfile"""
from os import getenv
from fabric import Connection
connect_kwargs = {
'passphrase': getenv('SSH_PASSPHRASE')
}
c = Connection('user@ip.address', connect_kwargs=connect_kwargs)
result = c.run('uname -s')