Add environment variable SSH_PRIVATE_KEY_FILE

Additionally to the variable SSH_PRIVATE_KEY, which can be used to
reference a private key saved as a string within a variable (the previous
documentation falsely stated, that it references a path and has been
adjusted), the new variable SSH_PRIVATE_KEY_FILE can now be used to reference
the path to a file containing the private key.

Signed-off-by: Jasper Orschulko <jasper@fancydomain.eu>
[Jan: Fix logic for detecting set env vars]
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
Jasper Orschulko
2021-07-03 23:35:40 +02:00
committed by Jan Kiszka
parent 4d2b16e04d
commit 15de0142d4
3 changed files with 105 additions and 69 deletions

View File

@@ -50,7 +50,8 @@ class Macro:
SetupDir(),
]
if 'SSH_PRIVATE_KEY' in os.environ:
if ('SSH_PRIVATE_KEY' in os.environ
or 'SSH_PRIVATE_KEY_FILE' in os.environ):
self.setup_commands.append(SetupSSHAgent())
self.setup_commands += [
@@ -65,7 +66,9 @@ class Macro:
else:
self.setup_commands = []
if use_common_cleanup and 'SSH_PRIVATE_KEY' in os.environ:
if (use_common_cleanup
and ('SSH_PRIVATE_KEY' in os.environ
or 'SSH_PRIVATE_KEY_FILE' in os.environ)):
self.cleanup_commands = [
CleanupSSHAgent(),
]

View File

@@ -268,6 +268,15 @@ def get_build_environ(build_system):
return env
def ssh_add_key_file(env, key_path):
"""
Adds an ssh key file to the ssh-agent
"""
with open(key_path) as f:
key = f.read()
ssh_add_key(env, key)
def ssh_add_key(env, key):
"""
Adds an ssh key to the ssh-agent
@@ -307,20 +316,32 @@ def ssh_setup_agent(envkeys=None):
Starts the ssh-agent
"""
env = get_context().environ
envkeys = envkeys or ['SSH_PRIVATE_KEY']
envkeys = envkeys or ['SSH_PRIVATE_KEY', 'SSH_PRIVATE_KEY_FILE']
output = os.popen('ssh-agent -s').readlines()
for line in output:
matches = re.search(r"(\S+)\=(\S+)\;", line)
if matches:
env[matches.group(1)] = matches.group(2)
found = False
for envkey in envkeys:
key = os.environ.get(envkey)
if key:
logging.info("adding SSH key")
ssh_add_key(env, key)
if envkey == 'SSH_PRIVATE_KEY_FILE':
key_path = os.environ.get(envkey)
if key_path:
found = True
logging.info("adding SSH key")
ssh_add_key_file(env, key_path)
else:
logging.warning('%s is missing', envkey)
key = os.environ.get(envkey)
if key:
found = True
logging.info("adding SSH key")
ssh_add_key(env, key)
if found is not True:
warning = "None of the following environment keys were set: " + \
", ".join(envkeys)
logging.warning(warning)
def ssh_no_host_key_check():