# Copyright 2018, Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import, division, print_function __metaclass__ = type from ansible.module_utils.basic import AnsibleModule try: from ansible.module_utils.ca_common import generate_cmd, \ is_containerized, \ container_exec, \ fatal except ImportError: from module_utils.ca_common import generate_cmd, \ is_containerized, \ container_exec, \ fatal import datetime import json import os import struct import time import base64 import socket ANSIBLE_METADATA = { 'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community' } DOCUMENTATION = ''' --- module: ceph_key author: Sebastien Han short_description: Manage Cephx key(s) version_added: "2.6" description: - Manage CephX creation, deletion and updates. It can also list and get information about keyring(s). options: cluster: description: - The ceph cluster name. required: false default: ceph name: description: - name of the CephX key required: true user: description: - entity used to perform operation. It corresponds to the -n option (--name) required: false user_key: description: - the path to the keyring corresponding to the user being used. It corresponds to the -k option (--keyring) state: description: - If 'present' is used, the module creates a keyring with the associated capabilities. If 'present' is used and a secret is provided the module will always add the key. Which means it will update the keyring if the secret changes, the same goes for the capabilities. If 'absent' is used, the module will simply delete the keyring. If 'list' is used, the module will list all the keys and will return a json output. If 'info' is used, the module will return in a json format the description of a given keyring. If 'generate_secret' is used, the module will simply output a cephx keyring. required: false choices: ['present', 'update', 'absent', 'list', 'info', 'fetch_initial_keys', 'generate_secret'] default: present caps: description: - CephX key capabilities default: None required: false secret: description: - keyring's secret value required: false default: None import_key: description: - Wether or not to import the created keyring into Ceph. This can be useful for someone that only wants to generate keyrings but not add them into Ceph. required: false default: True dest: description: - Destination to write the keyring, can a file or a directory required: false default: /etc/ceph/ fetch_initial_keys: description: - Fetch client.admin and bootstrap key. This is only needed for Nautilus and above. Writes down to the filesystem the initial keys generated by the monitor. # noqa: E501 This command can ONLY run from a monitor node. required: false default: false output_format: description: - The key output format when retrieving the information of an entity. required: false default: json ''' EXAMPLES = ''' keys_to_create: - { name: client.key, key: "AQAin8tUUK84ExAA/QgBtI7gEMWdmnvKBzlXdQ==", caps: { mon: "allow rwx", mds: "allow *" } , mode: "0600" } # noqa: E501 - { name: client.cle, caps: { mon: "allow r", osd: "allow *" } , mode: "0600" } # noqa: E501 caps: mon: "allow rwx" mds: "allow *" - name: create ceph admin key ceph_key: name: client.admin state: present secret: AQAin8tU2DsKFBAAFIAzVTzkL3+gtAjjpQiomw== caps: mon: allow * osd: allow * mgr: allow * mds: allow mode: 0400 import_key: False - name: create monitor initial keyring ceph_key: name: mon. state: present secret: AQAin8tUMICVFBAALRHNrV0Z4MXupRw4v9JQ6Q== caps: mon: allow * dest: "/var/lib/ceph/tmp/" import_key: False - name: create cephx key ceph_key: name: "{{ keys_to_create }}" user: client.bootstrap-rgw user_key: /var/lib/ceph/bootstrap-rgw/ceph.keyring state: present caps: "{{ caps }}" - name: create cephx key but don't import it in Ceph ceph_key: name: "{{ keys_to_create }}" state: present caps: "{{ caps }}" import_key: False - name: delete cephx key ceph_key: name: "my_key" state: absent - name: info cephx key ceph_key: name: "my_key"" state: info - name: info cephx admin key (plain) ceph_key: name: client.admin output_format: plain state: info register: client_admin_key - name: list cephx keys ceph_key: state: list - name: fetch cephx keys ceph_key: state: fetch_initial_keys ''' RETURN = '''# ''' CEPH_INITIAL_KEYS = ['client.admin', 'client.bootstrap-mds', 'client.bootstrap-mgr', # noqa: E501 'client.bootstrap-osd', 'client.bootstrap-rbd', 'client.bootstrap-rbd-mirror', 'client.bootstrap-rgw'] # noqa: E501 def str_to_bool(val): try: val = val.lower() except AttributeError: val = str(val).lower() if val == 'true': return True elif val == 'false': return False else: raise ValueError("Invalid input value: %s" % val) def generate_secret(): ''' Generate a CephX secret ''' key = os.urandom(16) header = struct.pack('