Skip to content

Commit

Permalink
Not all GnuPG return codes were analyzed (rc != 0) and not all releva…
Browse files Browse the repository at this point in the history
…nt GnuPG error information was returned by the 'ansible.builtin.apt_key' module (#74477)
  • Loading branch information
maximmasiutin committed Apr 30, 2021
1 parent f541c0a commit 695cab6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/74478-apt_key-gpg-error-check.yaml
@@ -0,0 +1,2 @@
bugfixes:
- The 'ansible.builtin.apt_key' module did not properly handle GnuPG errors (https://github.com/ansible/ansible/issues/74477)
39 changes: 33 additions & 6 deletions lib/ansible/modules/apt_key.py
Expand Up @@ -253,6 +253,8 @@ def all_keys(module, keyring, short_format):
else:
cmd = "%s adv --list-public-keys --keyid-format=long" % apt_key_bin
(rc, out, err) = module.run_command(cmd)
if rc != 0:
module.fail_json(msg="Unable to list public keys", cmd=cmd, rc=rc, stdout=out, stderr=err)

return parse_output_for_keys(out, short_format)

Expand Down Expand Up @@ -323,10 +325,10 @@ def import_key(module, keyring, keyserver, key_id):
# Out of retries
if rc == 2 and 'not found on keyserver' in out:
msg = 'Key %s not found on keyserver %s' % (key_id, keyserver)
module.fail_json(cmd=cmd, msg=msg)
module.fail_json(cmd=cmd, msg=msg, forced_environment=lang_env)
else:
msg = "Error fetching key %s from keyserver: %s" % (key_id, keyserver)
module.fail_json(cmd=cmd, msg=msg, rc=rc, stdout=out, stderr=err)
module.fail_json(cmd=cmd, msg=msg, forced_environment=lang_env, rc=rc, stdout=out, stderr=err)
return True


Expand All @@ -336,23 +338,48 @@ def add_key(module, keyfile, keyring, data=None):
cmd = "%s --keyring %s add -" % (apt_key_bin, keyring)
else:
cmd = "%s add -" % apt_key_bin
(rc, out, err) = module.run_command(cmd, data=data, check_rc=True, binary_data=True)
(rc, out, err) = module.run_command(cmd, data=data, binary_data=True)
if rc != 0:
module.fail_json(
msg="Unable to add a key from binary data",
cmd=cmd,
rc=rc,
stdout=out,
stderr=err,
)
else:
if keyring:
cmd = "%s --keyring %s add %s" % (apt_key_bin, keyring, keyfile)
else:
cmd = "%s add %s" % (apt_key_bin, keyfile)
(rc, out, err) = module.run_command(cmd, check_rc=True)
(rc, out, err) = module.run_command(cmd)
if rc != 0:
module.fail_json(
msg="Unable to add a key from file %s" % (keyfile),
cmd=cmd,
rc=rc,
keyfile=keyfile,
stdout=out,
stderr=err,
)
return True


def remove_key(module, key_id, keyring):
# FIXME: use module.run_command, fail at point of error and don't discard useful stdin/stdout
if keyring:
cmd = '%s --keyring %s del %s' % (apt_key_bin, keyring, key_id)
else:
cmd = '%s del %s' % (apt_key_bin, key_id)
(rc, out, err) = module.run_command(cmd, check_rc=True)
(rc, out, err) = module.run_command(cmd)
if rc != 0:
module.fail_json(
msg="Unable to remove a key with id %s" % (key_id),
cmd=cmd,
rc=rc,
key_id=key_id,
stdout=out,
stderr=err,
)
return True


Expand Down

0 comments on commit 695cab6

Please sign in to comment.