2014-09-26 01:01:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
# (c) 2012, Jayson Vantuyl <jayson@aggressive.ly>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-12-06 10:35:05 +00:00
|
|
|
ANSIBLE_METADATA = {'status': ['preview'],
|
|
|
|
'supported_by': 'core',
|
|
|
|
'version': '1.0'}
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: apt_key
|
2015-06-15 19:53:30 +00:00
|
|
|
author: "Jayson Vantuyl & others (@jvantuyl)"
|
2014-09-26 01:01:01 +00:00
|
|
|
version_added: "1.0"
|
|
|
|
short_description: Add or remove an apt key
|
|
|
|
description:
|
|
|
|
- Add or remove an I(apt) key, optionally downloading it
|
|
|
|
notes:
|
|
|
|
- doesn't download the key unless it really needs it
|
|
|
|
- as a sanity check, downloaded key id must match the one specified
|
|
|
|
- best practice is to specify the key id and the url
|
|
|
|
options:
|
|
|
|
id:
|
|
|
|
required: false
|
|
|
|
default: none
|
|
|
|
description:
|
2015-05-07 14:49:50 +00:00
|
|
|
- identifier of key. Including this allows check mode to correctly report the changed state.
|
2016-10-22 15:55:34 +00:00
|
|
|
- "If specifying a subkey's id be aware that apt-key does not understand how to remove keys via a subkey id. Specify the primary key's id instead."
|
2014-09-26 01:01:01 +00:00
|
|
|
data:
|
|
|
|
required: false
|
|
|
|
default: none
|
|
|
|
description:
|
2016-10-22 15:55:34 +00:00
|
|
|
- keyfile contents to add to the keyring
|
2014-09-26 01:01:01 +00:00
|
|
|
file:
|
|
|
|
required: false
|
|
|
|
default: none
|
|
|
|
description:
|
2016-11-02 19:30:52 +00:00
|
|
|
- path to a keyfile on the remote server to add to the keyring
|
2014-09-26 01:01:01 +00:00
|
|
|
keyring:
|
|
|
|
required: false
|
|
|
|
default: none
|
|
|
|
description:
|
|
|
|
- path to specific keyring file in /etc/apt/trusted.gpg.d
|
|
|
|
version_added: "1.3"
|
|
|
|
url:
|
|
|
|
required: false
|
|
|
|
default: none
|
|
|
|
description:
|
|
|
|
- url to retrieve key from.
|
|
|
|
keyserver:
|
|
|
|
version_added: "1.6"
|
|
|
|
required: false
|
|
|
|
default: none
|
|
|
|
description:
|
|
|
|
- keyserver to retrieve key from.
|
|
|
|
state:
|
|
|
|
required: false
|
|
|
|
choices: [ absent, present ]
|
|
|
|
default: present
|
|
|
|
description:
|
|
|
|
- used to specify if key is being added or revoked
|
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates for the target url will not be validated. This should only be used
|
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
|
|
|
choices: ['yes', 'no']
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2014-10-03 19:10:45 +00:00
|
|
|
# Add an apt key by id from a keyserver
|
2016-10-12 21:57:00 +00:00
|
|
|
- apt_key:
|
|
|
|
keyserver: keyserver.ubuntu.com
|
|
|
|
id: 36A1D7869245C8950F966E92D8576A8BA88D21E9
|
2014-10-03 19:10:45 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
# Add an Apt signing key, uses whichever key is at the URL
|
2016-10-12 21:57:00 +00:00
|
|
|
- apt_key:
|
|
|
|
url: "https://ftp-master.debian.org/keys/archive-key-6.0.asc"
|
|
|
|
state: present
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# Add an Apt signing key, will not download if present
|
2016-10-12 21:57:00 +00:00
|
|
|
- apt_key:
|
|
|
|
id: 473041FA
|
|
|
|
url: "https://ftp-master.debian.org/keys/archive-key-6.0.asc"
|
|
|
|
state: present
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# Remove an Apt signing key, uses whichever key is at the URL
|
2016-10-12 21:57:00 +00:00
|
|
|
- apt_key:
|
|
|
|
url: "https://ftp-master.debian.org/keys/archive-key-6.0.asc"
|
|
|
|
state: absent
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# Remove a Apt specific signing key, leading 0x is valid
|
2016-10-12 21:57:00 +00:00
|
|
|
- apt_key:
|
|
|
|
id: 0x473041FA
|
|
|
|
state: absent
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# Add a key from a file on the Ansible server
|
2016-10-12 21:57:00 +00:00
|
|
|
- apt_key:
|
|
|
|
data: "{{ lookup('file', 'apt.gpg') }}"
|
|
|
|
state: present
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# Add an Apt signing key to a specific keyring file
|
2016-10-12 21:57:00 +00:00
|
|
|
- apt_key:
|
|
|
|
id: 473041FA
|
|
|
|
url: "https://ftp-master.debian.org/keys/archive-key-6.0.asc"
|
|
|
|
keyring: /etc/apt/trusted.gpg.d/debian.gpg
|
2016-11-02 19:30:52 +00:00
|
|
|
|
|
|
|
# Add Apt signing key on remote server to keyring
|
2016-10-12 21:57:00 +00:00
|
|
|
- apt_key:
|
|
|
|
id: 473041FA
|
|
|
|
file: /tmp/apt.gpg
|
|
|
|
state: present
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
# FIXME: standardize into module_common
|
|
|
|
from traceback import format_exc
|
|
|
|
|
2016-10-22 15:55:34 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
|
|
|
|
|
|
|
|
apt_key_bin = None
|
|
|
|
|
|
|
|
|
|
|
|
def find_needed_binaries(module):
|
|
|
|
global apt_key_bin
|
|
|
|
|
|
|
|
apt_key_bin = module.get_bin_path('apt-key', required=True)
|
|
|
|
|
|
|
|
### FIXME: Is there a reason that gpg and grep are checked? Is it just
|
|
|
|
# cruft or does the apt .deb package not require them (and if they're not
|
|
|
|
# installed, /usr/bin/apt-key fails?)
|
|
|
|
module.get_bin_path('gpg', required=True)
|
|
|
|
module.get_bin_path('grep', required=True)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_key_id(key_id):
|
|
|
|
"""validate the key_id and break it into segments
|
|
|
|
|
|
|
|
:arg key_id: The key_id as supplied by the user. A valid key_id will be
|
|
|
|
8, 16, or more hexadecimal chars with an optional leading ``0x``.
|
|
|
|
:returns: The portion of key_id suitable for apt-key del, the portion
|
|
|
|
suitable for comparisons with --list-public-keys, and the portion that
|
|
|
|
can be used with --recv-key. If key_id is long enough, these will be
|
|
|
|
the last 8 characters of key_id, the last 16 characters, and all of
|
|
|
|
key_id. If key_id is not long enough, some of the values will be the
|
|
|
|
same.
|
|
|
|
|
|
|
|
* apt-key del <= 1.10 has a bug with key_id != 8 chars
|
|
|
|
* apt-key adv --list-public-keys prints 16 chars
|
|
|
|
* apt-key adv --recv-key can take more chars
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Make sure the key_id is valid hexadecimal
|
|
|
|
int(key_id, 16)
|
|
|
|
|
|
|
|
key_id = key_id.upper()
|
|
|
|
if key_id.startswith('0X'):
|
|
|
|
key_id = key_id[2:]
|
|
|
|
|
|
|
|
key_id_len = len(key_id)
|
|
|
|
if (key_id_len != 8 and key_id_len != 16) and key_id_len <= 16:
|
|
|
|
raise ValueError('key_id must be 8, 16, or 16+ hexadecimal characters in length')
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-10-22 15:55:34 +00:00
|
|
|
short_key_id = key_id[-8:]
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-10-22 15:55:34 +00:00
|
|
|
fingerprint = key_id
|
|
|
|
if key_id_len > 16:
|
|
|
|
fingerprint = key_id[-16:]
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-10-22 15:55:34 +00:00
|
|
|
return short_key_id, fingerprint, key_id
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-09-14 20:26:09 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def all_keys(module, keyring, short_format):
|
|
|
|
if keyring:
|
2016-10-22 15:55:34 +00:00
|
|
|
cmd = "%s --keyring %s adv --list-public-keys --keyid-format=long" % (apt_key_bin, keyring)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
2016-10-22 15:55:34 +00:00
|
|
|
cmd = "%s adv --list-public-keys --keyid-format=long" % apt_key_bin
|
2014-09-26 01:01:01 +00:00
|
|
|
(rc, out, err) = module.run_command(cmd)
|
|
|
|
results = []
|
2016-09-09 00:56:09 +00:00
|
|
|
lines = to_native(out).split('\n')
|
2014-09-26 01:01:01 +00:00
|
|
|
for line in lines:
|
2016-08-16 16:01:07 +00:00
|
|
|
if line.startswith("pub") or line.startswith("sub"):
|
2014-09-26 01:01:01 +00:00
|
|
|
tokens = line.split()
|
|
|
|
code = tokens[1]
|
|
|
|
(len_type, real_code) = code.split("/")
|
|
|
|
results.append(real_code)
|
|
|
|
if short_format:
|
|
|
|
results = shorten_key_ids(results)
|
|
|
|
return results
|
|
|
|
|
2016-09-14 20:26:09 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def shorten_key_ids(key_id_list):
|
|
|
|
"""
|
|
|
|
Takes a list of key ids, and converts them to the 'short' format,
|
|
|
|
by reducing them to their last 8 characters.
|
|
|
|
"""
|
|
|
|
short = []
|
|
|
|
for key in key_id_list:
|
|
|
|
short.append(key[-8:])
|
|
|
|
return short
|
|
|
|
|
2016-09-14 20:26:09 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def download_key(module, url):
|
|
|
|
# FIXME: move get_url code to common, allow for in-memory D/L, support proxies
|
|
|
|
# and reuse here
|
|
|
|
if url is None:
|
|
|
|
module.fail_json(msg="needed a URL but was not specified")
|
|
|
|
|
|
|
|
try:
|
|
|
|
rsp, info = fetch_url(module, url)
|
|
|
|
if info['status'] != 200:
|
|
|
|
module.fail_json(msg="Failed to download key at %s: %s" % (url, info['msg']))
|
|
|
|
|
|
|
|
return rsp.read()
|
|
|
|
except Exception:
|
|
|
|
module.fail_json(msg="error getting key id from url: %s" % url, traceback=format_exc())
|
|
|
|
|
2016-09-14 20:26:09 +00:00
|
|
|
|
2016-02-09 18:51:16 +00:00
|
|
|
def import_key(module, keyring, keyserver, key_id):
|
|
|
|
if keyring:
|
2016-10-22 15:55:34 +00:00
|
|
|
cmd = "%s --keyring %s adv --keyserver %s --recv %s" % (apt_key_bin, keyring, keyserver, key_id)
|
2016-02-09 18:51:16 +00:00
|
|
|
else:
|
2016-10-22 15:55:34 +00:00
|
|
|
cmd = "%s adv --keyserver %s --recv %s" % (apt_key_bin, keyserver, key_id)
|
2016-09-14 20:26:09 +00:00
|
|
|
for retry in range(5):
|
2016-10-22 15:55:34 +00:00
|
|
|
lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
|
|
|
|
(rc, out, err) = module.run_command(cmd, environ_update=lang_env)
|
2016-06-22 05:42:44 +00:00
|
|
|
if rc == 0:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# Out of retries
|
2016-10-22 15:55:34 +00:00
|
|
|
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)
|
|
|
|
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)
|
2014-09-26 01:01:01 +00:00
|
|
|
return True
|
|
|
|
|
2016-09-14 20:26:09 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def add_key(module, keyfile, keyring, data=None):
|
|
|
|
if data is not None:
|
|
|
|
if keyring:
|
2016-10-22 15:55:34 +00:00
|
|
|
cmd = "%s --keyring %s add -" % (apt_key_bin, keyring)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
2016-10-22 15:55:34 +00:00
|
|
|
cmd = "%s add -" % apt_key_bin
|
2014-09-26 01:01:01 +00:00
|
|
|
(rc, out, err) = module.run_command(cmd, data=data, check_rc=True, binary_data=True)
|
|
|
|
else:
|
|
|
|
if keyring:
|
2016-10-22 15:55:34 +00:00
|
|
|
cmd = "%s --keyring %s add %s" % (apt_key_bin, keyring, keyfile)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
2016-10-22 15:55:34 +00:00
|
|
|
cmd = "%s add %s" % (apt_key_bin, keyfile)
|
2014-09-26 01:01:01 +00:00
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True)
|
|
|
|
return True
|
|
|
|
|
2016-09-14 20:26:09 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
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:
|
2016-10-22 15:55:34 +00:00
|
|
|
cmd = '%s --keyring %s del %s' % (apt_key_bin, keyring, key_id)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
2016-10-22 15:55:34 +00:00
|
|
|
cmd = '%s del %s' % (apt_key_bin, key_id)
|
2014-09-26 01:01:01 +00:00
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True)
|
|
|
|
return True
|
|
|
|
|
2016-09-14 20:26:09 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
id=dict(required=False, default=None),
|
|
|
|
url=dict(required=False),
|
|
|
|
data=dict(required=False),
|
2016-10-22 15:55:34 +00:00
|
|
|
file=dict(required=False, type='path'),
|
2014-09-26 01:01:01 +00:00
|
|
|
key=dict(required=False),
|
2016-10-22 15:55:34 +00:00
|
|
|
keyring=dict(required=False, type='path'),
|
2014-09-26 01:01:01 +00:00
|
|
|
validate_certs=dict(default='yes', type='bool'),
|
|
|
|
keyserver=dict(required=False),
|
|
|
|
state=dict(required=False, choices=['present', 'absent'], default='present')
|
|
|
|
),
|
2016-10-22 15:55:34 +00:00
|
|
|
supports_check_mode=True,
|
|
|
|
mutually_exclusive=(('filename', 'keyserver', 'data', 'url'),),
|
2014-09-26 01:01:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
key_id = module.params['id']
|
|
|
|
url = module.params['url']
|
|
|
|
data = module.params['data']
|
|
|
|
filename = module.params['file']
|
|
|
|
keyring = module.params['keyring']
|
|
|
|
state = module.params['state']
|
|
|
|
keyserver = module.params['keyserver']
|
|
|
|
changed = False
|
|
|
|
|
2016-10-22 15:55:34 +00:00
|
|
|
fingerprint = short_key_id = key_id
|
|
|
|
short_format = False
|
2014-09-26 01:01:01 +00:00
|
|
|
if key_id:
|
|
|
|
try:
|
2016-10-22 16:29:47 +00:00
|
|
|
short_key_id, fingerprint, key_id = parse_key_id(key_id)
|
2014-09-26 01:01:01 +00:00
|
|
|
except ValueError:
|
2016-10-22 15:55:34 +00:00
|
|
|
module.fail_json(msg='Invalid key_id', id=key_id)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-10-22 15:55:34 +00:00
|
|
|
if len(fingerprint) == 8:
|
|
|
|
short_format = True
|
|
|
|
|
|
|
|
find_needed_binaries(module)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
keys = all_keys(module, keyring, short_format)
|
|
|
|
return_values = {}
|
|
|
|
|
|
|
|
if state == 'present':
|
2016-10-22 15:55:34 +00:00
|
|
|
if fingerprint and fingerprint in keys:
|
2014-09-26 01:01:01 +00:00
|
|
|
module.exit_json(changed=False)
|
2016-10-22 15:55:34 +00:00
|
|
|
elif fingerprint and fingerprint not in keys and module.check_mode:
|
|
|
|
### TODO: Someday we could go further -- write keys out to
|
|
|
|
# a temporary file and then extract the key id from there via gpg
|
|
|
|
# to decide if the key is installed or not.
|
|
|
|
module.exit_json(changed=True)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
|
|
|
if not filename and not data and not keyserver:
|
|
|
|
data = download_key(module, url)
|
2016-10-22 15:55:34 +00:00
|
|
|
|
|
|
|
if filename:
|
|
|
|
add_key(module, filename, keyring)
|
|
|
|
elif keyserver:
|
|
|
|
import_key(module, keyring, keyserver, key_id)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
2016-10-22 15:55:34 +00:00
|
|
|
add_key(module, "-", keyring, data)
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
keys2 = all_keys(module, keyring, short_format)
|
|
|
|
if len(keys) != len(keys2):
|
|
|
|
changed=True
|
|
|
|
|
|
|
|
if fingerprint and fingerprint not in keys2:
|
|
|
|
module.fail_json(msg="key does not seem to have been added", id=key_id)
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
elif state == 'absent':
|
|
|
|
if not key_id:
|
|
|
|
module.fail_json(msg="key is required")
|
2016-10-22 15:55:34 +00:00
|
|
|
if fingerprint in keys:
|
2014-09-26 01:01:01 +00:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2016-10-22 15:55:34 +00:00
|
|
|
|
|
|
|
# we use the "short" id: key_id[-8:], short_format=True
|
|
|
|
# it's a workaround for https://bugs.launchpad.net/ubuntu/+source/apt/+bug/1481871
|
|
|
|
if remove_key(module, short_key_id, keyring):
|
|
|
|
keys = all_keys(module, keyring, short_format)
|
|
|
|
if fingerprint in keys:
|
|
|
|
module.fail_json(msg="apt-key del did not return an error but the key was not removed (check that the id is correct and *not* a subkey)", id=key_id)
|
|
|
|
changed = True
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
2016-10-22 15:55:34 +00:00
|
|
|
# FIXME: module.fail_json or exit-json immediately at point of failure
|
2014-09-26 01:01:01 +00:00
|
|
|
module.fail_json(msg="error removing key_id", **return_values)
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, **return_values)
|
|
|
|
|
2016-10-22 15:55:34 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|