Skip to content

Commit

Permalink
mgr/cephadm: refactor keyring simplification out of get_keyring_with_…
Browse files Browse the repository at this point in the history
…caps

Refactor get_keyring_with_caps such that the keyring simplification code
is moved into a new function that can be used in other locations.
get_keyring_with_caps will now call the new function to return the
simplified & consistent keyring output.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
  • Loading branch information
phlogistonjohn committed Mar 21, 2024
1 parent a500f42 commit 41e2b27
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/pybind/mgr/cephadm/services/cephadmservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ def get_auth_entity(daemon_type: str, daemon_id: str, host: str = "") -> AuthEnt
raise OrchestratorError(f"unknown daemon type {daemon_type}")


def simplified_keyring(entity: str, contents: str) -> str:
# strip down keyring
# - don't include caps (auth get includes them; get-or-create does not)
# - use pending key if present
key = None
for line in contents.splitlines():
if ' = ' not in line:
continue
line = line.strip()
(ls, rs) = line.split(' = ', 1)
if ls == 'key' and not key:
key = rs
if ls == 'pending key':
key = rs
keyring = f'[{entity}]\nkey = {key}\n'
return keyring


class CephadmDaemonDeploySpec:
# typing.NamedTuple + Generic is broken in py36
def __init__(self, host: str, daemon_id: str,
Expand Down Expand Up @@ -307,22 +325,7 @@ def get_keyring_with_caps(self, entity: AuthEntity, caps: List[str]) -> str:
})
if err:
raise OrchestratorError(f"Unable to fetch keyring for {entity}: {err}")

# strip down keyring
# - don't include caps (auth get includes them; get-or-create does not)
# - use pending key if present
key = None
for line in keyring.splitlines():
if ' = ' not in line:
continue
line = line.strip()
(ls, rs) = line.split(' = ', 1)
if ls == 'key' and not key:
key = rs
if ls == 'pending key':
key = rs
keyring = f'[{entity}]\nkey = {key}\n'
return keyring
return simplified_keyring(entity, keyring)

def _inventory_get_fqdn(self, hostname: str) -> str:
"""Get a host's FQDN with its hostname.
Expand Down

0 comments on commit 41e2b27

Please sign in to comment.