From 2e659244f72cdea5b117e510a06ccd325d51eb9e Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Thu, 20 Jul 2017 14:30:41 -0400 Subject: [PATCH] fix a bug that breaks inventory updates if no credential is supplied see: #7206 --- awx/main/tasks.py | 4 ++-- awx/main/tests/unit/test_tasks.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/awx/main/tasks.py b/awx/main/tasks.py index 83af9cb9b4..1f6ca23bc8 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -481,14 +481,14 @@ class BaseTask(Task): if 'OPENSSH PRIVATE KEY' in data and not openssh_keys_supported: raise RuntimeError(OPENSSH_KEY_ERROR) for credential, data in private_data.get('credentials', {}).iteritems(): - name = 'credential_%d' % credential.pk # OpenSSH formatted keys must have a trailing newline to be # accepted by ssh-add. if 'OPENSSH PRIVATE KEY' in data and not data.endswith('\n'): data += '\n' # For credentials used with ssh-add, write to a named pipe which # will be read then closed, instead of leaving the SSH key on disk. - if credential.kind in ('ssh', 'scm') and not ssh_too_old: + if credential and credential.kind in ('ssh', 'scm') and not ssh_too_old: + name = 'credential_%d' % credential.pk path = os.path.join(kwargs['private_data_dir'], name) run.open_fifo_write(path, data) private_data_files['credentials']['ssh'] = path diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index 79984d6832..dad6429fa0 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -1165,6 +1165,24 @@ class TestInventoryUpdateCredentials(TestJobExecution): ) ) + def test_source_without_credential(self): + self.instance.source = 'ec2' + + def run_pexpect_side_effect(*args, **kwargs): + args, cwd, env, stdout = args + + assert 'AWS_ACCESS_KEY_ID' not in env + assert 'AWS_SECRET_ACCESS_KEY' not in env + assert 'EC2_INI_PATH' in env + + config = ConfigParser.ConfigParser() + config.read(env['EC2_INI_PATH']) + assert 'ec2' in config.sections() + return ['successful', 0] + + self.run_pexpect.side_effect = run_pexpect_side_effect + self.task.run(self.pk) + def test_ec2_source(self): aws = CredentialType.defaults['aws']() self.instance.source = 'ec2'