2020-03-17 22:34:11 +08:00
|
|
|
#!/usr/bin/python3
|
2020-09-06 10:17:02 +08:00
|
|
|
|
2018-03-18 22:53:45 +08:00
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
|
|
ANSIBLE_METADATA = {
|
|
|
|
'metadata_version': '1.1',
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'
|
|
|
|
}
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: ceph_key
|
|
|
|
|
|
|
|
author: Sebastien Han <seb@redhat.com>
|
|
|
|
|
|
|
|
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
|
2020-10-03 12:56:06 +08:00
|
|
|
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)
|
2018-03-18 22:53:45 +08:00
|
|
|
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.
|
|
|
|
required: true
|
2020-03-17 22:34:11 +08:00
|
|
|
choices: ['present', 'absent', 'list', 'info']
|
2018-03-18 22:53:45 +08:00
|
|
|
default: list
|
|
|
|
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:
|
2018-12-03 18:59:49 +08:00
|
|
|
- Destination to write the keyring, can a file or a directory
|
2018-03-18 22:53:45 +08:00
|
|
|
required: false
|
|
|
|
default: /etc/ceph/
|
2018-10-24 00:38:40 +08:00
|
|
|
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
|
2018-03-18 22:53:45 +08:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
|
|
|
|
keys_to_create:
|
2018-10-24 00:38:40 +08:00
|
|
|
- { 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
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
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
|
2018-04-20 22:35:39 +08:00
|
|
|
mode: 0400
|
2018-03-18 22:53:45 +08:00
|
|
|
import_key: False
|
|
|
|
|
|
|
|
- name: create monitor initial keyring
|
|
|
|
ceph_key:
|
|
|
|
name: mon.
|
|
|
|
state: present
|
|
|
|
secret: AQAin8tUMICVFBAALRHNrV0Z4MXupRw4v9JQ6Q==
|
|
|
|
caps:
|
|
|
|
mon: allow *
|
2018-11-21 23:17:04 +08:00
|
|
|
dest: "/var/lib/ceph/tmp/"
|
2018-03-18 22:53:45 +08:00
|
|
|
import_key: False
|
|
|
|
|
|
|
|
- name: create cephx key
|
|
|
|
ceph_key:
|
|
|
|
name: "{{ keys_to_create }}"
|
2020-10-03 12:56:06 +08:00
|
|
|
user: client.bootstrap-rgw
|
|
|
|
user_key: /var/lib/ceph/bootstrap-rgw/ceph.keyring
|
2018-03-18 22:53:45 +08:00
|
|
|
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: list cephx keys
|
|
|
|
ceph_key:
|
|
|
|
state: list
|
2018-10-24 00:38:40 +08:00
|
|
|
|
|
|
|
- name: fetch cephx keys
|
|
|
|
ceph_key:
|
|
|
|
state: fetch_initial_keys
|
2018-03-18 22:53:45 +08:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''# '''
|
|
|
|
|
2018-10-24 00:38:40 +08:00
|
|
|
from ansible.module_utils.basic import AnsibleModule # noqa E402
|
|
|
|
import datetime # noqa E402
|
|
|
|
import grp # noqa E402
|
|
|
|
import json # noqa E402
|
|
|
|
import os # noqa E402
|
|
|
|
import pwd # noqa E402
|
|
|
|
import stat # noqa E402
|
|
|
|
import struct # noqa E402
|
|
|
|
import time # noqa E402
|
|
|
|
import base64 # noqa E402
|
|
|
|
import socket # noqa E402
|
|
|
|
|
|
|
|
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
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
|
2019-03-20 16:14:17 +08:00
|
|
|
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)
|
|
|
|
|
2020-09-06 10:17:02 +08:00
|
|
|
|
2018-03-18 22:53:45 +08:00
|
|
|
def fatal(message, module):
|
|
|
|
'''
|
|
|
|
Report a fatal error and exit
|
|
|
|
'''
|
|
|
|
|
|
|
|
if module:
|
|
|
|
module.fail_json(msg=message, rc=1)
|
|
|
|
else:
|
|
|
|
raise(Exception(message))
|
|
|
|
|
|
|
|
|
2018-11-16 17:46:10 +08:00
|
|
|
def container_exec(binary, container_image):
|
|
|
|
'''
|
|
|
|
Build the docker CLI to run a command inside a container
|
|
|
|
'''
|
|
|
|
|
2018-11-18 23:10:11 +08:00
|
|
|
container_binary = os.getenv('CEPH_CONTAINER_BINARY')
|
|
|
|
command_exec = [container_binary,
|
2018-11-16 17:46:10 +08:00
|
|
|
'run',
|
|
|
|
'--rm',
|
|
|
|
'--net=host',
|
|
|
|
'-v', '/etc/ceph:/etc/ceph:z',
|
|
|
|
'-v', '/var/lib/ceph/:/var/lib/ceph/:z',
|
|
|
|
'-v', '/var/log/ceph/:/var/log/ceph/:z',
|
2019-03-11 18:22:42 +08:00
|
|
|
'--entrypoint=' + binary, container_image]
|
2018-11-16 17:46:10 +08:00
|
|
|
return command_exec
|
|
|
|
|
|
|
|
|
|
|
|
def is_containerized():
|
|
|
|
'''
|
|
|
|
Check if we are running on a containerized cluster
|
|
|
|
'''
|
|
|
|
|
|
|
|
if 'CEPH_CONTAINER_IMAGE' in os.environ:
|
|
|
|
container_image = os.getenv('CEPH_CONTAINER_IMAGE')
|
|
|
|
else:
|
|
|
|
container_image = None
|
|
|
|
|
|
|
|
return container_image
|
|
|
|
|
|
|
|
|
2018-03-18 22:53:45 +08:00
|
|
|
def generate_secret():
|
|
|
|
'''
|
|
|
|
Generate a CephX secret
|
|
|
|
'''
|
|
|
|
|
|
|
|
key = os.urandom(16)
|
|
|
|
header = struct.pack('<hiih', 1, int(time.time()), 0, len(key))
|
|
|
|
secret = base64.b64encode(header + key)
|
|
|
|
|
|
|
|
return secret
|
|
|
|
|
|
|
|
|
2020-10-03 12:56:06 +08:00
|
|
|
def generate_caps(_type, caps):
|
2018-03-18 22:53:45 +08:00
|
|
|
'''
|
|
|
|
Generate CephX capabilities list
|
|
|
|
'''
|
|
|
|
|
2020-10-03 12:56:06 +08:00
|
|
|
caps_cli = []
|
|
|
|
|
2018-10-04 13:48:03 +08:00
|
|
|
for k, v in caps.items():
|
2018-04-20 22:35:39 +08:00
|
|
|
# makes sure someone didn't pass an empty var,
|
|
|
|
# we don't want to add an empty cap
|
2018-03-18 22:53:45 +08:00
|
|
|
if len(k) == 0:
|
|
|
|
continue
|
|
|
|
if _type == "ceph-authtool":
|
2020-10-03 12:56:06 +08:00
|
|
|
caps_cli.extend(["--cap"])
|
|
|
|
caps_cli.extend([k, v])
|
2018-03-18 22:53:45 +08:00
|
|
|
|
2020-10-03 12:56:06 +08:00
|
|
|
return caps_cli
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
|
2020-10-03 12:56:06 +08:00
|
|
|
def generate_ceph_cmd(cluster, args, user, user_key_path, container_image=None):
|
2018-03-18 22:53:45 +08:00
|
|
|
'''
|
|
|
|
Generate 'ceph' command line to execute
|
|
|
|
'''
|
|
|
|
|
2018-11-16 17:46:10 +08:00
|
|
|
if container_image:
|
|
|
|
binary = 'ceph'
|
|
|
|
cmd = container_exec(
|
|
|
|
binary, container_image)
|
|
|
|
else:
|
|
|
|
binary = ['ceph']
|
|
|
|
cmd = binary
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
base_cmd = [
|
2018-10-24 00:38:40 +08:00
|
|
|
'-n',
|
|
|
|
user,
|
|
|
|
'-k',
|
2020-10-03 12:56:06 +08:00
|
|
|
user_key_path,
|
2018-03-18 22:53:45 +08:00
|
|
|
'--cluster',
|
|
|
|
cluster,
|
|
|
|
'auth',
|
|
|
|
]
|
|
|
|
|
|
|
|
cmd.extend(base_cmd + args)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
2018-11-16 17:46:10 +08:00
|
|
|
def generate_ceph_authtool_cmd(cluster, name, secret, caps, dest, container_image=None): # noqa E501
|
2018-03-18 22:53:45 +08:00
|
|
|
'''
|
|
|
|
Generate 'ceph-authtool' command line to execute
|
|
|
|
'''
|
|
|
|
|
2018-11-16 17:46:10 +08:00
|
|
|
if container_image:
|
|
|
|
binary = 'ceph-authtool'
|
|
|
|
cmd = container_exec(
|
|
|
|
binary, container_image)
|
|
|
|
else:
|
|
|
|
binary = ['ceph-authtool']
|
|
|
|
cmd = binary
|
|
|
|
|
|
|
|
base_cmd = [
|
2018-03-18 22:53:45 +08:00
|
|
|
'--create-keyring',
|
2018-11-21 23:17:04 +08:00
|
|
|
dest,
|
2018-03-18 22:53:45 +08:00
|
|
|
'--name',
|
|
|
|
name,
|
|
|
|
'--add-key',
|
|
|
|
secret,
|
|
|
|
]
|
|
|
|
|
2018-11-16 17:46:10 +08:00
|
|
|
cmd.extend(base_cmd)
|
2020-10-03 12:56:06 +08:00
|
|
|
cmd.extend(generate_caps("ceph-authtool", caps))
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
2020-10-03 12:56:06 +08:00
|
|
|
def create_key(module, result, cluster, user, user_key_path, name, secret, caps, import_key, dest, container_image=None): # noqa E501
|
2018-03-18 22:53:45 +08:00
|
|
|
'''
|
|
|
|
Create a CephX key
|
|
|
|
'''
|
|
|
|
|
|
|
|
cmd_list = []
|
|
|
|
if not secret:
|
|
|
|
secret = generate_secret()
|
|
|
|
|
2020-10-03 12:56:06 +08:00
|
|
|
if user == 'client.admin':
|
|
|
|
args = ['import', '-i', dest]
|
|
|
|
else:
|
|
|
|
args = ['get-or-create', name]
|
|
|
|
args.extend(generate_caps(None, caps))
|
|
|
|
args.extend(['-o', dest])
|
|
|
|
|
2018-03-18 22:53:45 +08:00
|
|
|
cmd_list.append(generate_ceph_authtool_cmd(
|
2018-11-16 17:37:07 +08:00
|
|
|
cluster, name, secret, caps, dest, container_image))
|
2018-03-18 22:53:45 +08:00
|
|
|
|
2020-10-03 12:56:06 +08:00
|
|
|
if import_key or user != 'client.admin':
|
2018-10-24 00:38:40 +08:00
|
|
|
cmd_list.append(generate_ceph_cmd(
|
2020-10-03 12:56:06 +08:00
|
|
|
cluster, args, user, user_key_path, container_image))
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
return cmd_list
|
|
|
|
|
|
|
|
|
2020-10-03 12:56:06 +08:00
|
|
|
def delete_key(cluster, user, user_key_path, name, container_image=None):
|
2018-03-18 22:53:45 +08:00
|
|
|
'''
|
|
|
|
Delete a CephX key
|
|
|
|
'''
|
|
|
|
|
|
|
|
cmd_list = []
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'del',
|
|
|
|
name,
|
|
|
|
]
|
|
|
|
|
2018-10-24 00:38:40 +08:00
|
|
|
cmd_list.append(generate_ceph_cmd(
|
2020-10-03 12:56:06 +08:00
|
|
|
cluster, args, user, user_key_path, container_image))
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
return cmd_list
|
|
|
|
|
|
|
|
|
2020-10-03 12:56:06 +08:00
|
|
|
def get_key(cluster, user, user_key_path, name, dest, container_image=None):
|
2018-11-21 23:17:04 +08:00
|
|
|
'''
|
|
|
|
Get a CephX key (write on the filesystem)
|
|
|
|
'''
|
|
|
|
|
|
|
|
cmd_list = []
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'get',
|
|
|
|
name,
|
|
|
|
'-o',
|
|
|
|
dest,
|
|
|
|
]
|
|
|
|
|
|
|
|
cmd_list.append(generate_ceph_cmd(
|
2020-10-03 12:56:06 +08:00
|
|
|
cluster, args, user, user_key_path, container_image))
|
2018-11-21 23:17:04 +08:00
|
|
|
|
|
|
|
return cmd_list
|
|
|
|
|
|
|
|
|
2020-10-03 12:56:06 +08:00
|
|
|
def info_key(cluster, name, user, user_key_path, output_format, container_image=None): # noqa E501
|
2018-03-18 22:53:45 +08:00
|
|
|
'''
|
|
|
|
Get information about a CephX key
|
|
|
|
'''
|
|
|
|
|
|
|
|
cmd_list = []
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'get',
|
|
|
|
name,
|
|
|
|
'-f',
|
2018-10-24 00:38:40 +08:00
|
|
|
output_format,
|
2018-03-18 22:53:45 +08:00
|
|
|
]
|
|
|
|
|
2018-10-24 00:38:40 +08:00
|
|
|
cmd_list.append(generate_ceph_cmd(
|
2020-10-03 12:56:06 +08:00
|
|
|
cluster, args, user, user_key_path, container_image))
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
return cmd_list
|
|
|
|
|
|
|
|
|
2020-10-03 12:56:06 +08:00
|
|
|
def list_keys(cluster, user, user_key_path, container_image=None):
|
2018-03-18 22:53:45 +08:00
|
|
|
'''
|
|
|
|
List all CephX keys
|
|
|
|
'''
|
|
|
|
|
|
|
|
cmd_list = []
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'ls',
|
|
|
|
'-f',
|
|
|
|
'json',
|
|
|
|
]
|
|
|
|
|
2018-10-24 00:38:40 +08:00
|
|
|
cmd_list.append(generate_ceph_cmd(
|
2020-10-03 12:56:06 +08:00
|
|
|
cluster, args, user, user_key_path, container_image))
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
return cmd_list
|
|
|
|
|
|
|
|
|
|
|
|
def exec_commands(module, cmd_list):
|
|
|
|
'''
|
|
|
|
Execute command(s)
|
|
|
|
'''
|
|
|
|
|
|
|
|
for cmd in cmd_list:
|
|
|
|
rc, out, err = module.run_command(cmd)
|
|
|
|
if rc != 0:
|
|
|
|
return rc, cmd, out, err
|
|
|
|
|
|
|
|
return rc, cmd, out, err
|
|
|
|
|
|
|
|
|
2018-12-07 02:34:49 +08:00
|
|
|
def lookup_ceph_initial_entities(module, out):
|
2018-10-24 00:38:40 +08:00
|
|
|
'''
|
|
|
|
Lookup Ceph initial keys entries in the auth map
|
|
|
|
'''
|
|
|
|
|
|
|
|
# convert out to json, ansible returns a string...
|
|
|
|
try:
|
|
|
|
out_dict = json.loads(out)
|
|
|
|
except ValueError as e:
|
|
|
|
fatal("Could not decode 'ceph auth list' json output: {}".format(e), module) # noqa E501
|
|
|
|
|
|
|
|
entities = []
|
|
|
|
if "auth_dump" in out_dict:
|
|
|
|
for key in out_dict["auth_dump"]:
|
|
|
|
for k, v in key.items():
|
|
|
|
if k == "entity":
|
|
|
|
if v in CEPH_INITIAL_KEYS:
|
|
|
|
entities.append(v)
|
|
|
|
else:
|
|
|
|
fatal("'auth_dump' key not present in json output:", module) # noqa E501
|
|
|
|
|
2020-09-06 10:17:02 +08:00
|
|
|
if len(entities) != len(CEPH_INITIAL_KEYS) and not str_to_bool(os.environ.get('CEPH_ROLLING_UPDATE', False)): # noqa E501
|
2018-12-18 08:43:35 +08:00
|
|
|
# must be missing in auth_dump, as if it were in CEPH_INITIAL_KEYS
|
|
|
|
# it'd be in entities from the above test. Report what's missing.
|
|
|
|
missing = []
|
|
|
|
for e in CEPH_INITIAL_KEYS:
|
|
|
|
if e not in entities:
|
|
|
|
missing.append(e)
|
2020-09-06 10:17:02 +08:00
|
|
|
fatal("initial keyring does not contain keys: " + ' '.join(missing), module) # noqa E501
|
2018-10-24 00:38:40 +08:00
|
|
|
return entities
|
|
|
|
|
|
|
|
|
|
|
|
def build_key_path(cluster, entity):
|
|
|
|
'''
|
|
|
|
Build key path depending on the key type
|
|
|
|
'''
|
|
|
|
|
|
|
|
if "admin" in entity:
|
|
|
|
path = "/etc/ceph"
|
2019-03-11 18:49:32 +08:00
|
|
|
keyring_filename = cluster + "." + entity + ".keyring"
|
|
|
|
key_path = os.path.join(path, keyring_filename)
|
2018-10-24 00:38:40 +08:00
|
|
|
elif "bootstrap" in entity:
|
|
|
|
path = "/var/lib/ceph"
|
|
|
|
# bootstrap keys show up as 'client.boostrap-osd'
|
|
|
|
# however the directory is called '/var/lib/ceph/bootstrap-osd'
|
|
|
|
# so we need to substring 'client.'
|
|
|
|
entity_split = entity.split('.')[1]
|
2019-03-11 18:49:32 +08:00
|
|
|
keyring_filename = cluster + ".keyring"
|
|
|
|
key_path = os.path.join(path, entity_split, keyring_filename)
|
2018-10-24 00:38:40 +08:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return key_path
|
|
|
|
|
|
|
|
|
2018-03-18 22:53:45 +08:00
|
|
|
def run_module():
|
|
|
|
module_args = dict(
|
|
|
|
cluster=dict(type='str', required=False, default='ceph'),
|
|
|
|
name=dict(type='str', required=False),
|
|
|
|
state=dict(type='str', required=True),
|
|
|
|
caps=dict(type='dict', required=False, default=None),
|
2020-09-24 00:00:30 +08:00
|
|
|
secret=dict(type='str', required=False, default=None, no_log=True),
|
2018-03-18 22:53:45 +08:00
|
|
|
import_key=dict(type='bool', required=False, default=True),
|
2018-11-16 17:37:07 +08:00
|
|
|
dest=dict(type='str', required=False, default='/etc/ceph/'),
|
2020-10-03 12:56:06 +08:00
|
|
|
user=dict(type='str', required=False, default='client.admin'),
|
|
|
|
user_key=dict(type='str', required=False, default=None)
|
2018-03-18 22:53:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=module_args,
|
2018-04-20 22:35:39 +08:00
|
|
|
supports_check_mode=True,
|
|
|
|
add_file_common_args=True,
|
2018-03-18 22:53:45 +08:00
|
|
|
)
|
|
|
|
|
2019-11-14 17:30:34 +08:00
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
|
|
|
2018-03-18 22:53:45 +08:00
|
|
|
# Gather module parameters in variables
|
|
|
|
state = module.params['state']
|
|
|
|
name = module.params.get('name')
|
|
|
|
cluster = module.params.get('cluster')
|
|
|
|
caps = module.params.get('caps')
|
|
|
|
secret = module.params.get('secret')
|
|
|
|
import_key = module.params.get('import_key')
|
|
|
|
dest = module.params.get('dest')
|
2020-10-03 12:56:06 +08:00
|
|
|
user = module.params.get('user')
|
|
|
|
user_key = module.params.get('user_key')
|
2018-03-18 22:53:45 +08:00
|
|
|
|
2020-03-17 22:34:11 +08:00
|
|
|
changed = False
|
|
|
|
|
2018-03-18 22:53:45 +08:00
|
|
|
result = dict(
|
2020-03-17 22:34:11 +08:00
|
|
|
changed=changed,
|
2018-03-18 22:53:45 +08:00
|
|
|
stdout='',
|
|
|
|
stderr='',
|
|
|
|
rc='',
|
|
|
|
start='',
|
|
|
|
end='',
|
|
|
|
delta='',
|
|
|
|
)
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
return result
|
|
|
|
|
|
|
|
startd = datetime.datetime.now()
|
|
|
|
|
2018-11-16 17:46:10 +08:00
|
|
|
# will return either the image name or None
|
|
|
|
container_image = is_containerized()
|
|
|
|
|
2018-03-18 22:53:45 +08:00
|
|
|
# Test if the key exists, if it does we skip its creation
|
|
|
|
# We only want to run this check when a key needs to be added
|
|
|
|
# There is no guarantee that any cluster is running and we don't need one
|
2020-03-17 22:34:11 +08:00
|
|
|
key_exist = 1
|
|
|
|
_secret = secret
|
|
|
|
_caps = caps
|
2020-10-03 12:56:06 +08:00
|
|
|
key_exist = 1
|
|
|
|
|
|
|
|
if not user_key:
|
|
|
|
user_key_filename = '{}.{}.keyring'.format(cluster, user)
|
|
|
|
user_key_dir = '/etc/ceph'
|
|
|
|
user_key_path = os.path.join(user_key_dir, user_key_filename)
|
|
|
|
else:
|
|
|
|
user_key_path = user_key
|
|
|
|
|
|
|
|
output_format = "json"
|
|
|
|
|
|
|
|
if (state in ["present", "update"]):
|
2020-04-03 16:24:32 +08:00
|
|
|
# if dest is not a directory, the user wants to change the file's name
|
|
|
|
# (e,g: /etc/ceph/ceph.mgr.ceph-mon2.keyring)
|
|
|
|
if not os.path.isdir(dest):
|
|
|
|
file_path = dest
|
|
|
|
else:
|
|
|
|
if 'bootstrap' in dest:
|
2020-09-06 10:17:02 +08:00
|
|
|
# Build a different path for bootstrap keys as there are stored
|
|
|
|
# as /var/lib/ceph/bootstrap-rbd/ceph.keyring
|
2020-04-03 16:24:32 +08:00
|
|
|
keyring_filename = cluster + '.keyring'
|
|
|
|
else:
|
|
|
|
keyring_filename = cluster + "." + name + ".keyring"
|
|
|
|
file_path = os.path.join(dest, keyring_filename)
|
|
|
|
|
|
|
|
file_args['path'] = file_path
|
|
|
|
|
2020-10-03 12:56:06 +08:00
|
|
|
if import_key:
|
|
|
|
_info_key = []
|
|
|
|
rc, cmd, out, err = exec_commands(
|
|
|
|
module, info_key(cluster, name, user, user_key_path, output_format, container_image)) # noqa E501
|
|
|
|
key_exist = rc
|
|
|
|
if not caps and key_exist != 0:
|
|
|
|
fatal("Capabilities must be provided when state is 'present'", module) # noqa E501
|
|
|
|
if key_exist != 0 and secret is None and caps is None:
|
|
|
|
fatal("Keyring doesn't exist, you must provide 'secret' and 'caps'", module) # noqa E501
|
|
|
|
if key_exist == 0:
|
|
|
|
_info_key = json.loads(out)
|
|
|
|
if not secret:
|
|
|
|
secret = _info_key[0]['key']
|
|
|
|
_secret = _info_key[0]['key']
|
|
|
|
if not caps:
|
|
|
|
caps = _info_key[0]['caps']
|
|
|
|
_caps = _info_key[0]['caps']
|
|
|
|
if secret == _secret and caps == _caps:
|
|
|
|
if not os.path.isfile(file_path):
|
|
|
|
rc, cmd, out, err = exec_commands(module, get_key(cluster, user, user_key_path, name, file_path, container_image)) # noqa E501
|
|
|
|
result["rc"] = rc
|
|
|
|
if rc != 0:
|
|
|
|
result["stdout"] = "Couldn't fetch the key {0} at {1}.".format(name, file_path) # noqa E501
|
|
|
|
module.exit_json(**result)
|
|
|
|
result["stdout"] = "fetched the key {0} at {1}.".format(name, file_path) # noqa E501
|
|
|
|
|
|
|
|
result["stdout"] = "{0} already exists and doesn't need to be updated.".format(name) # noqa E501
|
2020-04-03 16:24:32 +08:00
|
|
|
result["rc"] = 0
|
|
|
|
module.exit_json(**result)
|
2018-03-18 22:53:45 +08:00
|
|
|
|
2020-03-17 22:34:11 +08:00
|
|
|
# "update" is here only for backward compatibility
|
|
|
|
if state in ["present", "update"]:
|
|
|
|
if not caps and import_key and rc != 0:
|
2018-10-24 00:38:40 +08:00
|
|
|
fatal("Capabilities must be provided when state is 'present'", module) # noqa E501
|
2020-03-17 22:34:11 +08:00
|
|
|
if import_key and key_exist != 0 and secret is None and caps is None:
|
|
|
|
fatal("Keyring doesn't exist, you must provide 'secret' and 'caps'", module) # noqa E501
|
2018-03-18 22:53:45 +08:00
|
|
|
|
2020-03-17 22:34:11 +08:00
|
|
|
# There's no need to run create_key() if neither secret nor caps have changed
|
|
|
|
if (key_exist == 0 and (secret != _secret or caps != _caps)) or key_exist != 0:
|
|
|
|
rc, cmd, out, err = exec_commands(module, create_key(
|
2020-10-03 12:56:06 +08:00
|
|
|
module, result, cluster, user, user_key_path, name, secret, caps, import_key, file_path, container_image)) # noqa E501
|
2020-03-17 22:34:11 +08:00
|
|
|
if rc != 0:
|
|
|
|
result["stdout"] = "Couldn't create or update {0}".format(name)
|
|
|
|
result["stderr"] = err
|
|
|
|
module.exit_json(**result)
|
|
|
|
changed = True
|
2018-03-18 22:53:45 +08:00
|
|
|
|
2020-03-17 22:34:11 +08:00
|
|
|
module.set_fs_attributes_if_different(file_args, False)
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
elif state == "absent":
|
2020-10-03 12:56:06 +08:00
|
|
|
if key_exist == 0:
|
|
|
|
rc, cmd, out, err = exec_commands(
|
|
|
|
module, delete_key(cluster, user, user_key_path, name, container_image))
|
|
|
|
if rc == 0:
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
rc = 0
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
elif state == "info":
|
2018-10-24 00:38:40 +08:00
|
|
|
user = "client.admin"
|
2019-03-11 18:49:32 +08:00
|
|
|
keyring_filename = cluster + '.' + user + '.keyring'
|
|
|
|
user_key = os.path.join("/etc/ceph/", keyring_filename)
|
2018-10-24 00:38:40 +08:00
|
|
|
output_format = "json"
|
2018-03-18 22:53:45 +08:00
|
|
|
rc, cmd, out, err = exec_commands(
|
2020-10-03 12:56:06 +08:00
|
|
|
module, info_key(cluster, name, user, user_key_path, output_format, container_image)) # noqa E501
|
2020-07-09 22:24:15 +08:00
|
|
|
if rc != 0:
|
|
|
|
result["stdout"] = "skipped, since {0} does not exist".format(name)
|
|
|
|
result['rc'] = 0
|
|
|
|
module.exit_json(**result)
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
elif state == "list":
|
2018-10-24 00:38:40 +08:00
|
|
|
user = "client.admin"
|
2019-03-11 18:49:32 +08:00
|
|
|
keyring_filename = cluster + '.' + user + '.keyring'
|
|
|
|
user_key = os.path.join("/etc/ceph/", keyring_filename)
|
2018-03-18 22:53:45 +08:00
|
|
|
rc, cmd, out, err = exec_commands(
|
2020-10-03 12:56:06 +08:00
|
|
|
module, list_keys(cluster, user, user_key_path, container_image))
|
2018-10-24 00:38:40 +08:00
|
|
|
|
|
|
|
elif state == "fetch_initial_keys":
|
2019-01-26 10:48:28 +08:00
|
|
|
hostname = socket.gethostname().split('.', 1)[0]
|
2018-10-24 00:38:40 +08:00
|
|
|
user = "mon."
|
2019-03-11 18:49:32 +08:00
|
|
|
keyring_filename = cluster + "-" + hostname + "/keyring"
|
2020-10-03 12:56:06 +08:00
|
|
|
user_key_path = os.path.join("/var/lib/ceph/mon/", keyring_filename)
|
2018-10-24 00:38:40 +08:00
|
|
|
rc, cmd, out, err = exec_commands(
|
2020-10-03 12:56:06 +08:00
|
|
|
module, list_keys(cluster, user, user_key_path, container_image))
|
2018-10-24 00:38:40 +08:00
|
|
|
if rc != 0:
|
2020-09-06 10:17:02 +08:00
|
|
|
result["stdout"] = "failed to retrieve ceph keys"
|
2019-01-16 17:27:23 +08:00
|
|
|
result["sdterr"] = err
|
2018-10-24 00:38:40 +08:00
|
|
|
result['rc'] = 0
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2018-12-07 02:34:49 +08:00
|
|
|
entities = lookup_ceph_initial_entities(module, out)
|
2018-10-24 00:38:40 +08:00
|
|
|
|
|
|
|
output_format = "plain"
|
|
|
|
for entity in entities:
|
|
|
|
key_path = build_key_path(cluster, entity)
|
|
|
|
if key_path is None:
|
|
|
|
fatal("Failed to build key path, no entity yet?", module)
|
|
|
|
elif os.path.isfile(key_path):
|
|
|
|
# if the key is already on the filesystem
|
|
|
|
# there is no need to fetch it again
|
|
|
|
continue
|
|
|
|
|
|
|
|
extra_args = [
|
|
|
|
'-o',
|
|
|
|
key_path,
|
|
|
|
]
|
|
|
|
|
|
|
|
info_cmd = info_key(cluster, entity, user,
|
2020-10-03 12:56:06 +08:00
|
|
|
user_key_path, output_format, container_image)
|
2018-10-24 00:38:40 +08:00
|
|
|
# we use info_cmd[0] because info_cmd is an array made of an array
|
|
|
|
info_cmd[0].extend(extra_args)
|
|
|
|
rc, cmd, out, err = exec_commands(
|
|
|
|
module, info_cmd) # noqa E501
|
|
|
|
|
2018-11-16 17:46:10 +08:00
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
|
|
file_args['path'] = key_path
|
|
|
|
module.set_fs_attributes_if_different(file_args, False)
|
2018-03-18 22:53:45 +08:00
|
|
|
else:
|
|
|
|
module.fail_json(
|
2020-03-17 22:34:11 +08:00
|
|
|
msg='State must either be "present" or "absent" or "list" or "info" or "fetch_initial_keys".', changed=False, rc=1) # noqa E501
|
2018-03-18 22:53:45 +08:00
|
|
|
|
|
|
|
endd = datetime.datetime.now()
|
|
|
|
delta = endd - startd
|
|
|
|
|
|
|
|
result = dict(
|
|
|
|
cmd=cmd,
|
|
|
|
start=str(startd),
|
|
|
|
end=str(endd),
|
|
|
|
delta=str(delta),
|
|
|
|
rc=rc,
|
2018-11-19 16:56:45 +08:00
|
|
|
stdout=out.rstrip("\r\n"),
|
|
|
|
stderr=err.rstrip("\r\n"),
|
2020-03-17 22:34:11 +08:00
|
|
|
changed=changed,
|
2018-03-18 22:53:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg='non-zero return code', **result)
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
run_module()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|