2013-01-03 23:09:29 +01: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/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: apt_key
|
2015-06-15 21:53:30 +02:00
|
|
|
author: "Jayson Vantuyl & others (@jvantuyl)"
|
2013-03-30 20:44:34 +01:00
|
|
|
version_added: "1.0"
|
2013-01-03 23:09:29 +01:00
|
|
|
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 16:49:50 +02:00
|
|
|
- identifier of key. Including this allows check mode to correctly report the changed state.
|
2013-06-14 13:29:45 +02:00
|
|
|
data:
|
|
|
|
required: false
|
|
|
|
default: none
|
|
|
|
description:
|
|
|
|
- keyfile contents
|
2013-06-14 13:26:36 +02:00
|
|
|
file:
|
|
|
|
required: false
|
|
|
|
default: none
|
|
|
|
description:
|
|
|
|
- keyfile path
|
2013-07-25 03:10:17 +02:00
|
|
|
keyring:
|
|
|
|
required: false
|
|
|
|
default: none
|
|
|
|
description:
|
|
|
|
- path to specific keyring file in /etc/apt/trusted.gpg.d
|
2013-08-03 20:22:38 +02:00
|
|
|
version_added: "1.3"
|
2013-01-03 23:09:29 +01:00
|
|
|
url:
|
|
|
|
required: false
|
|
|
|
default: none
|
|
|
|
description:
|
|
|
|
- url to retrieve key from.
|
2013-12-08 11:53:33 +01:00
|
|
|
keyserver:
|
2014-04-02 23:17:52 +02:00
|
|
|
version_added: "1.6"
|
2013-12-08 11:53:33 +01:00
|
|
|
required: false
|
|
|
|
default: none
|
|
|
|
description:
|
|
|
|
- keyserver to retrieve key from.
|
2013-01-03 23:09:29 +01:00
|
|
|
state:
|
|
|
|
required: false
|
|
|
|
choices: [ absent, present ]
|
|
|
|
default: present
|
|
|
|
description:
|
|
|
|
- used to specify if key is being added or revoked
|
2014-03-10 22:06:52 +01:00
|
|
|
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']
|
|
|
|
|
2013-01-03 23:09:29 +01:00
|
|
|
'''
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
2014-10-03 21:10:45 +02:00
|
|
|
# Add an apt key by id from a keyserver
|
|
|
|
- apt_key: keyserver=keyserver.ubuntu.com id=36A1D7869245C8950F966E92D8576A8BA88D21E9
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
# Add an Apt signing key, uses whichever key is at the URL
|
|
|
|
- apt_key: url=https://ftp-master.debian.org/keys/archive-key-6.0.asc state=present
|
|
|
|
|
|
|
|
# Add an Apt signing key, will not download if present
|
|
|
|
- apt_key: id=473041FA url=https://ftp-master.debian.org/keys/archive-key-6.0.asc state=present
|
|
|
|
|
|
|
|
# Remove an Apt signing key, uses whichever key is at the URL
|
|
|
|
- apt_key: url=https://ftp-master.debian.org/keys/archive-key-6.0.asc state=absent
|
|
|
|
|
2013-10-16 20:42:42 +02:00
|
|
|
# Remove a Apt specific signing key, leading 0x is valid
|
|
|
|
- apt_key: id=0x473041FA state=absent
|
2013-06-18 22:18:31 +02:00
|
|
|
|
2013-10-16 20:42:42 +02:00
|
|
|
# Add a key from a file on the Ansible server
|
2013-06-18 22:18:31 +02:00
|
|
|
- apt_key: data="{{ lookup('file', 'apt.gpg') }}" state=present
|
2013-07-25 03:10:17 +02:00
|
|
|
|
|
|
|
# Add an Apt signing key to a specific keyring file
|
|
|
|
- apt_key: id=473041FA url=https://ftp-master.debian.org/keys/archive-key-6.0.asc keyring=/etc/apt/trusted.gpg.d/debian.gpg state=present
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
2013-01-28 21:41:46 +01:00
|
|
|
# FIXME: standardize into module_common
|
2013-01-03 23:09:29 +01:00
|
|
|
from traceback import format_exc
|
|
|
|
from re import compile as re_compile
|
2013-01-28 21:41:46 +01:00
|
|
|
# FIXME: standardize into module_common
|
2013-01-03 23:09:29 +01:00
|
|
|
from distutils.spawn import find_executable
|
|
|
|
from os import environ
|
|
|
|
from sys import exc_info
|
2013-01-28 21:41:46 +01:00
|
|
|
import traceback
|
2013-01-03 23:09:29 +01:00
|
|
|
|
|
|
|
match_key = re_compile("^gpg:.*key ([0-9a-fA-F]+):.*$")
|
|
|
|
|
|
|
|
REQUIRED_EXECUTABLES=['gpg', 'grep', 'apt-key']
|
|
|
|
|
|
|
|
|
2013-01-28 21:41:46 +01:00
|
|
|
def check_missing_binaries(module):
|
|
|
|
missing = [e for e in REQUIRED_EXECUTABLES if not find_executable(e)]
|
|
|
|
if len(missing):
|
2014-03-13 18:58:20 +01:00
|
|
|
module.fail_json(msg="binaries are missing", names=missing)
|
2013-01-28 21:41:46 +01:00
|
|
|
|
2014-04-17 03:51:19 +02:00
|
|
|
def all_keys(module, keyring, short_format):
|
2013-07-25 03:10:17 +02:00
|
|
|
if keyring:
|
2014-04-17 03:51:19 +02:00
|
|
|
cmd = "apt-key --keyring %s adv --list-public-keys --keyid-format=long" % keyring
|
2013-07-25 03:10:17 +02:00
|
|
|
else:
|
2014-04-17 03:51:19 +02:00
|
|
|
cmd = "apt-key adv --list-public-keys --keyid-format=long"
|
2013-07-25 03:10:17 +02:00
|
|
|
(rc, out, err) = module.run_command(cmd)
|
2013-01-28 21:41:46 +01:00
|
|
|
results = []
|
|
|
|
lines = out.split('\n')
|
|
|
|
for line in lines:
|
2016-08-16 18:01:07 +02:00
|
|
|
if line.startswith("pub") or line.startswith("sub"):
|
2013-02-18 01:48:02 +01:00
|
|
|
tokens = line.split()
|
|
|
|
code = tokens[1]
|
|
|
|
(len_type, real_code) = code.split("/")
|
|
|
|
results.append(real_code)
|
2014-04-17 03:51:19 +02:00
|
|
|
if short_format:
|
|
|
|
results = shorten_key_ids(results)
|
2013-01-28 21:41:46 +01:00
|
|
|
return results
|
|
|
|
|
2014-04-17 03:51:19 +02: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
|
2013-01-28 21:41:46 +01:00
|
|
|
|
|
|
|
def download_key(module, url):
|
|
|
|
# FIXME: move get_url code to common, allow for in-memory D/L, support proxies
|
|
|
|
# and reuse here
|
2013-01-03 23:09:29 +01:00
|
|
|
if url is None:
|
2013-01-28 21:41:46 +01:00
|
|
|
module.fail_json(msg="needed a URL but was not specified")
|
2014-06-25 15:30:34 +02:00
|
|
|
|
2013-01-28 21:41:46 +01:00
|
|
|
try:
|
2014-03-12 16:31:01 +01:00
|
|
|
rsp, info = fetch_url(module, url)
|
2014-06-25 15:30:34 +02:00
|
|
|
if info['status'] != 200:
|
|
|
|
module.fail_json(msg="Failed to download key at %s: %s" % (url, info['msg']))
|
|
|
|
|
2014-03-10 22:06:52 +01:00
|
|
|
return rsp.read()
|
2013-12-04 13:56:57 +01:00
|
|
|
except Exception:
|
2014-04-01 13:55:58 +02:00
|
|
|
module.fail_json(msg="error getting key id from url: %s" % url, traceback=format_exc())
|
2013-01-28 21:41:46 +01:00
|
|
|
|
2016-02-09 19:51:16 +01:00
|
|
|
def import_key(module, keyring, keyserver, key_id):
|
|
|
|
if keyring:
|
|
|
|
cmd = "apt-key --keyring %s adv --keyserver %s --recv %s" % (keyring, keyserver, key_id)
|
|
|
|
else:
|
|
|
|
cmd = "apt-key adv --keyserver %s --recv %s" % (keyserver, key_id)
|
2016-06-22 07:42:44 +02:00
|
|
|
for retry in xrange(5):
|
|
|
|
(rc, out, err) = module.run_command(cmd)
|
|
|
|
if rc == 0:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# Out of retries
|
|
|
|
module.fail_json(cmd=cmd, msg="error fetching key from keyserver: %s" % keyserver,
|
|
|
|
rc=rc, stdout=out, stderr=err)
|
2013-12-08 11:53:33 +01:00
|
|
|
return True
|
2013-01-28 21:41:46 +01:00
|
|
|
|
2013-07-25 03:10:17 +02:00
|
|
|
def add_key(module, keyfile, keyring, data=None):
|
2013-06-26 20:46:29 +02:00
|
|
|
if data is not None:
|
2013-07-25 03:10:17 +02:00
|
|
|
if keyring:
|
|
|
|
cmd = "apt-key --keyring %s add -" % keyring
|
|
|
|
else:
|
|
|
|
cmd = "apt-key add -"
|
2013-06-26 20:46:29 +02:00
|
|
|
(rc, out, err) = module.run_command(cmd, data=data, check_rc=True, binary_data=True)
|
|
|
|
else:
|
2013-07-25 03:10:17 +02:00
|
|
|
if keyring:
|
|
|
|
cmd = "apt-key --keyring %s add %s" % (keyring, keyfile)
|
|
|
|
else:
|
|
|
|
cmd = "apt-key add %s" % (keyfile)
|
2013-06-26 20:46:29 +02:00
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True)
|
2013-01-28 21:41:46 +01:00
|
|
|
return True
|
2013-01-03 23:09:29 +01:00
|
|
|
|
2013-07-25 03:10:17 +02:00
|
|
|
def remove_key(module, key_id, keyring):
|
2013-01-28 21:41:46 +01:00
|
|
|
# FIXME: use module.run_command, fail at point of error and don't discard useful stdin/stdout
|
2013-07-25 03:10:17 +02:00
|
|
|
if keyring:
|
|
|
|
cmd = 'apt-key --keyring %s del %s' % (keyring, key_id)
|
|
|
|
else:
|
|
|
|
cmd = 'apt-key del %s' % key_id
|
2013-01-28 21:41:46 +01:00
|
|
|
(rc, out, err) = module.run_command(cmd, check_rc=True)
|
|
|
|
return True
|
2013-01-03 23:09:29 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
id=dict(required=False, default=None),
|
|
|
|
url=dict(required=False),
|
2013-01-28 21:41:46 +01:00
|
|
|
data=dict(required=False),
|
2013-06-14 13:26:36 +02:00
|
|
|
file=dict(required=False),
|
2013-01-28 21:41:46 +01:00
|
|
|
key=dict(required=False),
|
2013-07-25 03:10:17 +02:00
|
|
|
keyring=dict(required=False),
|
2014-03-10 22:06:52 +01:00
|
|
|
validate_certs=dict(default='yes', type='bool'),
|
2013-12-08 11:53:33 +01:00
|
|
|
keyserver=dict(required=False),
|
2013-01-03 23:09:29 +01:00
|
|
|
state=dict(required=False, choices=['present', 'absent'], default='present')
|
2013-01-28 21:41:46 +01:00
|
|
|
),
|
2013-02-27 21:23:35 +01:00
|
|
|
supports_check_mode=True
|
2013-01-03 23:09:29 +01:00
|
|
|
)
|
|
|
|
|
2013-01-28 21:41:46 +01:00
|
|
|
key_id = module.params['id']
|
|
|
|
url = module.params['url']
|
|
|
|
data = module.params['data']
|
2013-06-26 20:46:29 +02:00
|
|
|
filename = module.params['file']
|
2013-07-25 03:10:17 +02:00
|
|
|
keyring = module.params['keyring']
|
2013-01-28 21:41:46 +01:00
|
|
|
state = module.params['state']
|
2013-12-08 11:53:33 +01:00
|
|
|
keyserver = module.params['keyserver']
|
2013-01-28 21:41:46 +01:00
|
|
|
changed = False
|
2013-10-16 20:42:42 +02:00
|
|
|
|
2016-01-12 02:07:07 +01: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
|
|
|
|
|
2013-10-17 11:59:12 +02:00
|
|
|
if key_id:
|
|
|
|
try:
|
|
|
|
_ = int(key_id, 16)
|
2013-10-30 12:50:34 +01:00
|
|
|
if key_id.startswith('0x'):
|
|
|
|
key_id = key_id[2:]
|
2016-01-12 02:07:07 +01:00
|
|
|
key_id = key_id.upper()[-8:]
|
2013-10-17 11:59:12 +02:00
|
|
|
except ValueError:
|
2014-03-13 15:28:37 +01:00
|
|
|
module.fail_json(msg="Invalid key_id", id=key_id)
|
2013-10-16 20:42:42 +02:00
|
|
|
|
2013-01-28 21:41:46 +01:00
|
|
|
# FIXME: I think we have a common facility for this, if not, want
|
|
|
|
check_missing_binaries(module)
|
2013-01-03 23:09:29 +01:00
|
|
|
|
2016-01-12 02:07:07 +01:00
|
|
|
short_format = True
|
2014-04-17 03:51:19 +02:00
|
|
|
keys = all_keys(module, keyring, short_format)
|
2013-02-18 17:03:50 +01:00
|
|
|
return_values = {}
|
2013-01-03 23:09:29 +01:00
|
|
|
|
|
|
|
if state == 'present':
|
2013-01-28 21:41:46 +01:00
|
|
|
if key_id and key_id in keys:
|
|
|
|
module.exit_json(changed=False)
|
2013-01-03 23:09:29 +01:00
|
|
|
else:
|
2013-12-08 11:53:33 +01:00
|
|
|
if not filename and not data and not keyserver:
|
2013-01-28 21:41:46 +01:00
|
|
|
data = download_key(module, url)
|
|
|
|
if key_id and key_id in keys:
|
|
|
|
module.exit_json(changed=False)
|
2013-01-03 23:09:29 +01:00
|
|
|
else:
|
2013-02-27 21:23:35 +01:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2013-06-26 20:46:29 +02:00
|
|
|
if filename:
|
2013-07-25 03:10:17 +02:00
|
|
|
add_key(module, filename, keyring)
|
2013-12-08 11:53:33 +01:00
|
|
|
elif keyserver:
|
2016-02-09 19:51:16 +01:00
|
|
|
import_key(module, keyring, keyserver, key_id)
|
2013-06-26 20:46:29 +02:00
|
|
|
else:
|
2013-07-25 03:10:17 +02:00
|
|
|
add_key(module, "-", keyring, data)
|
2013-01-28 21:41:46 +01:00
|
|
|
changed=False
|
2014-04-17 03:51:19 +02:00
|
|
|
keys2 = all_keys(module, keyring, short_format)
|
2013-01-28 21:41:46 +01:00
|
|
|
if len(keys) != len(keys2):
|
|
|
|
changed=True
|
2016-01-12 02:07:07 +01:00
|
|
|
if key_id and not key_id in keys2:
|
2013-01-28 21:41:46 +01:00
|
|
|
module.fail_json(msg="key does not seem to have been added", id=key_id)
|
|
|
|
module.exit_json(changed=changed)
|
2013-01-03 23:09:29 +01:00
|
|
|
elif state == 'absent':
|
2013-01-28 21:41:46 +01:00
|
|
|
if not key_id:
|
|
|
|
module.fail_json(msg="key is required")
|
|
|
|
if key_id in keys:
|
2013-02-27 21:23:35 +01:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2013-07-25 03:10:17 +02:00
|
|
|
if remove_key(module, key_id, keyring):
|
2013-01-03 23:09:29 +01:00
|
|
|
changed=True
|
|
|
|
else:
|
2013-01-28 21:41:46 +01:00
|
|
|
# FIXME: module.fail_json or exit-json immediately at point of failure
|
2013-02-18 17:03:50 +01:00
|
|
|
module.fail_json(msg="error removing key_id", **return_values)
|
2013-01-03 23:09:29 +01:00
|
|
|
|
2013-02-18 17:03:50 +01:00
|
|
|
module.exit_json(changed=changed, **return_values)
|
2013-01-03 23:09:29 +01:00
|
|
|
|
2013-12-02 21:11:23 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2014-03-10 22:06:52 +01:00
|
|
|
from ansible.module_utils.urls import *
|
2013-01-03 23:09:29 +01:00
|
|
|
main()
|