apt_key 9.27 KB
Newer Older
Jayson Vantuyl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/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
25
author: Jayson Vantuyl & others
26
version_added: "1.0"
Jayson Vantuyl committed
27 28 29 30 31 32 33 34 35 36 37 38 39
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:
            - identifier of key
40 41 42 43 44
    data:
        required: false
        default: none
        description:
            - keyfile contents
45 46 47 48 49
    file:
        required: false
        default: none
        description:
            - keyfile path
50 51 52 53 54
    keyring:
        required: false
        default: none
        description:
            - path to specific keyring file in /etc/apt/trusted.gpg.d
55
        version_added: "1.3"
Jayson Vantuyl committed
56 57 58 59 60
    url:
        required: false
        default: none
        description:
            - url to retrieve key from.
61
    keyserver:
62
        version_added: "1.6"
63 64 65 66
        required: false
        default: none
        description:
            - keyserver to retrieve key from.
Jayson Vantuyl committed
67 68 69 70 71 72
    state:
        required: false
        choices: [ absent, present ]
        default: present
        description:
            - used to specify if key is being added or revoked
73 74 75 76 77 78 79 80
    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']

Jayson Vantuyl committed
81 82
'''

83 84 85 86 87 88 89 90 91 92
EXAMPLES = '''
# 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

93 94
# Remove a Apt specific signing key, leading 0x is valid
- apt_key: id=0x473041FA state=absent
95

96
# Add a key from a file on the Ansible server
97
- apt_key: data="{{ lookup('file', 'apt.gpg') }}" state=present
98 99 100

# 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
101 102 103
'''


104
# FIXME: standardize into module_common
Jayson Vantuyl committed
105 106
from traceback import format_exc
from re import compile as re_compile
107
# FIXME: standardize into module_common
Jayson Vantuyl committed
108 109 110
from distutils.spawn import find_executable
from os import environ
from sys import exc_info
111
import traceback
Jayson Vantuyl committed
112 113 114 115 116 117

match_key = re_compile("^gpg:.*key ([0-9a-fA-F]+):.*$")

REQUIRED_EXECUTABLES=['gpg', 'grep', 'apt-key']


118 119 120
def check_missing_binaries(module):
    missing = [e for e in REQUIRED_EXECUTABLES if not find_executable(e)]
    if len(missing):
121
        module.fail_json(msg="binaries are missing", names=missing)
122

123
def all_keys(module, keyring, short_format):
124
    if keyring:
125
        cmd = "apt-key --keyring %s adv --list-public-keys --keyid-format=long" % keyring
126
    else:
127
        cmd = "apt-key adv --list-public-keys --keyid-format=long"
128
    (rc, out, err) = module.run_command(cmd)
129 130 131
    results = []
    lines = out.split('\n')
    for line in lines:
132 133 134 135 136
        if line.startswith("pub"):
            tokens = line.split()
            code = tokens[1]
            (len_type, real_code) = code.split("/")
            results.append(real_code)
137 138
    if short_format:
        results = shorten_key_ids(results)
139 140
    return results

141 142 143 144 145 146 147 148 149
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
150 151 152 153

def download_key(module, url):
    # FIXME: move get_url code to common, allow for in-memory D/L, support proxies
    # and reuse here
Jayson Vantuyl committed
154
    if url is None:
155
        module.fail_json(msg="needed a URL but was not specified")
156

157
    try:
158
        rsp, info = fetch_url(module, url)
159 160 161
        if info['status'] != 200:
            module.fail_json(msg="Failed to download key at %s: %s" % (url, info['msg']))

162
        return rsp.read()
163
    except Exception:
164
        module.fail_json(msg="error getting key id from url: %s" % url, traceback=format_exc())
165

166 167 168 169
def import_key(module, keyserver, key_id):
    cmd = "apt-key adv --keyserver %s --recv %s" % (keyserver, key_id)
    (rc, out, err) = module.run_command(cmd, check_rc=True)
    return True
170

171
def add_key(module, keyfile, keyring, data=None):
172
    if data is not None:
173 174 175 176
        if keyring:
            cmd = "apt-key --keyring %s add -" % keyring
        else:
            cmd = "apt-key add -"
177 178
        (rc, out, err) = module.run_command(cmd, data=data, check_rc=True, binary_data=True)
    else:
179 180 181 182
        if keyring:
            cmd = "apt-key --keyring %s add %s" % (keyring, keyfile)
        else:
            cmd = "apt-key add %s" % (keyfile)
183
        (rc, out, err) = module.run_command(cmd, check_rc=True)
184
    return True
Jayson Vantuyl committed
185

186
def remove_key(module, key_id, keyring):
187
    # FIXME: use module.run_command, fail at point of error and don't discard useful stdin/stdout
188 189 190 191
    if keyring:
        cmd = 'apt-key --keyring %s del %s' % (keyring, key_id)
    else:
        cmd = 'apt-key del %s' % key_id
192 193
    (rc, out, err) = module.run_command(cmd, check_rc=True)
    return True
Jayson Vantuyl committed
194 195 196 197 198 199

def main():
    module = AnsibleModule(
        argument_spec=dict(
            id=dict(required=False, default=None),
            url=dict(required=False),
200
            data=dict(required=False),
201
            file=dict(required=False),
202
            key=dict(required=False),
203
            keyring=dict(required=False),
204
            validate_certs=dict(default='yes', type='bool'),
205
            keyserver=dict(required=False),
Jayson Vantuyl committed
206
            state=dict(required=False, choices=['present', 'absent'], default='present')
207
        ),
208
        supports_check_mode=True
Jayson Vantuyl committed
209 210
    )

211 212 213
    key_id          = module.params['id']
    url             = module.params['url']
    data            = module.params['data']
214
    filename        = module.params['file']
215
    keyring         = module.params['keyring']
216
    state           = module.params['state']
217
    keyserver       = module.params['keyserver']
218
    changed         = False
219

220 221 222
    if key_id:
        try:
            _ = int(key_id, 16)
223 224
            if key_id.startswith('0x'):
                key_id = key_id[2:]
225
            key_id = key_id.upper()
226
        except ValueError:
227
            module.fail_json(msg="Invalid key_id", id=key_id)
228

229 230
    # FIXME: I think we have a common facility for this, if not, want
    check_missing_binaries(module)
Jayson Vantuyl committed
231

232 233
    short_format = (key_id is not None and len(key_id) == 8)
    keys = all_keys(module, keyring, short_format)
lessmian committed
234
    return_values = {}
Jayson Vantuyl committed
235 236

    if state == 'present':
237 238
        if key_id and key_id in keys:
            module.exit_json(changed=False)
Jayson Vantuyl committed
239
        else:
240
            if not filename and not data and not keyserver:
241 242 243
                data = download_key(module, url)
            if key_id and key_id in keys:
                module.exit_json(changed=False)
Jayson Vantuyl committed
244
            else:
245 246
                if module.check_mode:
                    module.exit_json(changed=True)
247
                if filename:
248
                    add_key(module, filename, keyring)
249 250
                elif keyserver:
                    import_key(module, keyserver, key_id)
251
                else:
252
                    add_key(module, "-", keyring, data)
253
                changed=False
254
                keys2 = all_keys(module, keyring, short_format)
255 256 257 258 259
                if len(keys) != len(keys2):
                    changed=True
                if key_id and not key_id in keys2:
                    module.fail_json(msg="key does not seem to have been added", id=key_id)
                module.exit_json(changed=changed)
Jayson Vantuyl committed
260
    elif state == 'absent':
261 262 263
        if not key_id:
            module.fail_json(msg="key is required")
        if key_id in keys:
264 265
            if module.check_mode:
                module.exit_json(changed=True)
266
            if remove_key(module, key_id, keyring):
Jayson Vantuyl committed
267 268
                changed=True
            else:
269
                # FIXME: module.fail_json  or exit-json immediately at point of failure
lessmian committed
270
                module.fail_json(msg="error removing key_id", **return_values)
Jayson Vantuyl committed
271

lessmian committed
272
    module.exit_json(changed=changed, **return_values)
Jayson Vantuyl committed
273

274 275
# import module snippets
from ansible.module_utils.basic import *
276
from ansible.module_utils.urls import *
Jayson Vantuyl committed
277
main()