Skip to content

Add support for key in CephX keyring definition #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions plugins/module_utils/cephadm_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,31 @@
import datetime


def generate_ceph_cmd(sub_cmd, args):
def generate_ceph_cmd(sub_cmd, args, key_entry=None):
'''
Generate 'ceph' command line to execute
'''

cmd = [
'cephadm',
'--timeout',
'60',
'shell',
'--',
'ceph',
]
cmd.extend(sub_cmd + args)
cmd = []

if key_entry:
cmd = [
'cephadm',
'shell',
'--',
'bash',
'-c',
f'echo -e "{key_entry}" | ceph {" ".join(sub_cmd)} {" ".join(args)}'
]
else:
cmd = [
'cephadm',
'--timeout',
'60',
'shell',
'--',
'ceph',
]
cmd.extend(sub_cmd + args)

return cmd

Expand Down
67 changes: 58 additions & 9 deletions plugins/modules/cephadm_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
default: {}
required: false
type: dict
key:
description:
- Secret value of the key. If specified, this key will be
used explicitly instead of being generated.
required: false
type: str
output_format:
description:
- The key output format when retrieving the information of an
Expand Down Expand Up @@ -150,6 +156,25 @@ def create_key(name, caps): # noqa: E501
return cmd


def create_key_by_import(name, caps, key):
'''
Create a CephX key by import
'''
cmd = []

caps_cli = []
for k, v in caps.items():
caps_cli.append(f'caps {k} = "{v}"')

key_entry = f"[{name}]\n\tkey = {key}\n\t" + "\n\t".join(caps_cli)

sub_cmd = ['auth', 'import']
args = ['-i', '-']
cmd.append(generate_ceph_cmd(sub_cmd=sub_cmd, args=args, key_entry=key_entry))

return cmd


def update_key(name, caps):
'''
Update the caps of a CephX key
Expand All @@ -168,6 +193,15 @@ def update_key(name, caps):
return cmd


def update_key_by_import(name, caps, key=None):
'''
Update a CephX key by re-importing it
'''
cmd = create_key_by_import(name, caps, key)

return cmd


def delete_key(name):
'''
Delete a CephX key
Expand Down Expand Up @@ -264,6 +298,7 @@ def run_module():
state=dict(type='str', required=False, default='present', choices=['present', 'absent', # noqa: E501
'list', 'info']), # noqa: E501
caps=dict(type='dict', required=False, default={}),
key=dict(type='str', required=False, default=None),
output_format=dict(type='str', required=False, default='json', choices=['json', 'plain', 'xml', 'yaml']) # noqa: E501
)

Expand All @@ -276,6 +311,7 @@ def run_module():
state = module.params['state']
name = module.params.get('name')
caps = module.params.get('caps')
key = module.params.get('key')
output_format = module.params.get('output_format')

changed = False
Expand Down Expand Up @@ -318,20 +354,33 @@ def run_module():
result["rc"] = 0
module.exit_json(**result)
else:
rc, cmd, out, err = exec_commands(module, update_key(name, caps)) # noqa: E501
if key and key != _key:
Copy link
Member

@mnasiadka mnasiadka Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is _key defined?

rc, cmd, out, err = exec_commands(
module, update_key_by_import(name, caps, key)) # noqa: E501
else:
rc, cmd, out, err = exec_commands(
module, update_key(name, caps)) # noqa: E501
if rc != 0:
result["msg"] = "Couldn't update caps for {0}".format(name)
result["stdout"] = "Couldn't update {0}".format(name)
result["stderr"] = err
module.fail_json(**result)
module.exit_json(**result)
changed = True

else:
rc, cmd, out, err = exec_commands(module, create_key(name, caps)) # noqa: E501
if rc != 0:
result["msg"] = "Couldn't create {0}".format(name)
result["stderr"] = err
module.fail_json(**result)
changed = True
if key:
rc, cmd, out, err = exec_commands(module, create_key_by_import(name, caps, key))
if rc != 0:
result["stdout"] = "Couldn't import {0}".format(name)
result["stderr"] = err
module.exit_json(**result)
changed = True
else:
rc, cmd, out, err = exec_commands(module, create_key(name, caps)) # noqa: E501
if rc != 0:
result["stdout"] = "Couldn't create {0}".format(name)
result["stderr"] = err
module.exit_json(**result)
changed = True

elif state == "absent":
rc, cmd, out, err = exec_commands(
Expand Down
3 changes: 2 additions & 1 deletion roles/keys/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
name: "{{ item.name }}"
state: "{{ item.state | default(omit) }}"
caps: "{{ item.caps }}"
secret: "{{ item.key | default(omit) }}"
key: "{{ item.key | default(omit) }}"
with_items: "{{ cephadm_keys }}"
delegate_to: "{{ groups['mons'][0] }}"
run_once: true
no_log: "{{ item.key is defined }}"
Loading