From 41e2b27817c783f3b4b142441ed827e1827482d6 Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Fri, 5 Jan 2024 10:24:10 -0500 Subject: [PATCH] mgr/cephadm: refactor keyring simplification out of get_keyring_with_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 --- .../mgr/cephadm/services/cephadmservice.py | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/pybind/mgr/cephadm/services/cephadmservice.py b/src/pybind/mgr/cephadm/services/cephadmservice.py index d211bbaa309f4..6e3ee927341cd 100644 --- a/src/pybind/mgr/cephadm/services/cephadmservice.py +++ b/src/pybind/mgr/cephadm/services/cephadmservice.py @@ -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, @@ -307,22 +325,7 @@ class CephadmService(metaclass=ABCMeta): }) 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. -- 2.39.5