Repository navigation
feat: add one libvirt secret per ceph-access backend, prune stale secrets #235
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,11 +145,14 @@ def __init__(self, *args, **kwargs): | |
| <secret ephemeral='no' private='no'> | ||
| <uuid>$uuid</uuid> | ||
| <usage type='ceph'> | ||
| <name>client.cinder-ceph secret</name> | ||
| <name>$usage</name> | ||
| </usage> | ||
| </secret> | ||
| """) | ||
|
|
||
| LEGACY_SECRET_USAGE = "client.cinder-ceph secret" | ||
| CEPH_ACCESS_USAGE_PREFIX = "ceph-access." | ||
|
|
||
| # As defined in the snap/snapcraft.yaml | ||
| MONITORING_SERVICES = [ | ||
| "libvirt-exporter", | ||
|
|
@@ -2257,10 +2260,12 @@ def _is_hw_virt_supported() -> bool: | |
| return False | ||
|
|
||
|
|
||
| def _set_secret(conn, secret_uuid: str, secret_value: str) -> None: | ||
| def _set_secret( | ||
| conn, secret_uuid: str, secret_value: str, usage: str = LEGACY_SECRET_USAGE | ||
| ) -> None: | ||
| """Set the ceph access secret in libvirt.""" | ||
| logging.info(f"Setting secret {secret_uuid}") | ||
| new_secret = conn.secretDefineXML(SECRET_XML.substitute(uuid=secret_uuid)) | ||
| new_secret = conn.secretDefineXML(SECRET_XML.substitute(uuid=secret_uuid, usage=usage)) | ||
| # nova assumes the secret is raw and always encodes it *1, so decode it | ||
| # before storing it. | ||
| # *1 https://opendev.org/openstack/nova/src/branch/stable/2023.1/nova/ | ||
|
|
@@ -2275,7 +2280,7 @@ def _get_libvirt(): | |
| return libvirt | ||
|
|
||
|
|
||
| def _ensure_secret(secret_uuid: str, secret_value: str) -> None: | ||
| def _ensure_secret(secret_uuid: str, secret_value: str, usage: str = LEGACY_SECRET_USAGE) -> None: | ||
| """Ensure libvirt has the ceph access secret with the correct value.""" | ||
| libvirt = _get_libvirt() | ||
| conn = libvirt.open("qemu:///system") | ||
|
|
@@ -2292,35 +2297,68 @@ def _ensure_secret(secret_uuid: str, secret_value: str) -> None: | |
| secret = None | ||
| else: | ||
| raise | ||
| # Secret is stored raw so encode it before comparison. | ||
| if secret == base64.b64encode(secret_value.encode()): | ||
| # Secret is stored decode fetched value before compare | ||
| if secret == base64.b64decode(secret_value): | ||
| logging.info(f"Secret {secret_uuid} has desired value.") | ||
| else: | ||
| logging.info(f"Secret {secret_uuid} has wrong value, replacing.") | ||
| secretobj.undefine() | ||
| _set_secret(conn, secret_uuid, secret_value) | ||
| _set_secret(conn, secret_uuid, secret_value, usage) | ||
| else: | ||
| logging.info(f"Secret {secret_uuid} not found, creating.") | ||
| _set_secret(conn, secret_uuid, secret_value) | ||
| _set_secret(conn, secret_uuid, secret_value, usage) | ||
|
|
||
|
|
||
| def _desired_ceph_secrets(snap: Snap) -> dict[str, tuple[str, str]]: | ||
| """Libvirt secrets this host should have: {uuid: (key, usage)}.""" | ||
| secrets: dict[str, tuple[str, str]] = {} | ||
| compute = snap.config.get_options("compute").as_dict().get("compute") or {} | ||
| if all(k in compute for k in ("rbd-secret-uuid", "rbd-key")): | ||
| secrets[compute["rbd-secret-uuid"]] = ( | ||
| compute["rbd-key"], | ||
| LEGACY_SECRET_USAGE, | ||
| ) | ||
| try: | ||
| backends = snap.config.get_options("ceph-access").as_dict().get("ceph-access") or {} | ||
| except UnknownConfigKey: # charm without multi-backend support | ||
| backends = {} | ||
| for app, creds in backends.items(): | ||
| if creds and creds.get("uuid") and creds.get("key"): | ||
| secrets[creds["uuid"]] = (creds["key"], f"{CEPH_ACCESS_USAGE_PREFIX}{app}") | ||
| return secrets | ||
|
|
||
|
|
||
| def _prune_ceph_secrets(keep: set[str]) -> None: | ||
| """Undefine per-backend secrets whose backend is gone.""" | ||
| libvirt = _get_libvirt() | ||
| conn = libvirt.open("qemu:///system") | ||
| for secret in conn.listAllSecrets(): | ||
| if secret.usageType() != libvirt.VIR_SECRET_USAGE_TYPE_CEPH: | ||
| continue | ||
| if not (secret.usageID() or "").startswith(CEPH_ACCESS_USAGE_PREFIX): | ||
| continue | ||
| if secret.UUIDString() in keep: | ||
| continue | ||
| logging.info(f"Removing secret {secret.UUIDString()} ({secret.usageID()})") | ||
| secret.undefine() | ||
|
|
||
|
|
||
| def _configure_ceph(snap) -> None: | ||
| """Configure ceph client. | ||
| """Define one libvirt secret per backend and remove stale ones. | ||
|
|
||
| :param snap: the snap reference | ||
| :type snap: Snap | ||
| :return: None | ||
| A failing backend is logged and skipped so it cannot block the rest | ||
| of the configure hook. | ||
| """ | ||
| logging.info("Configuring ceph access") | ||
| context = ( | ||
| snap.config.get_options( | ||
| "compute", | ||
| ) | ||
| .as_dict() | ||
| .get("compute") | ||
| ) | ||
| if all(k in context for k in ("rbd-key", "rbd-secret-uuid")): | ||
| _ensure_secret(context["rbd-secret-uuid"], context["rbd-key"]) | ||
| libvirt = _get_libvirt() | ||
| desired = _desired_ceph_secrets(snap) | ||
| # Prune first, then add to handle same app getting new UUID | ||
| _prune_ceph_secrets(keep=set(desired)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a backend changes UUID, this removes the old secret before trying to define the replacement. I injected a failure in that second step: the old secret was already gone and configure returned normally. Could we preserve the old secret if the UUID change fails and add a regression test for that path? |
||
| for secret_uuid, (key, usage) in desired.items(): | ||
| try: | ||
| _ensure_secret(secret_uuid, key, usage) | ||
| except libvirt.libvirtError: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If libvirt rejects the replacement, the old secret has already been undefined and this catch lets the configure hook succeed. I reproduced that with an injected libvirt error. Can we set the value in place when the UUID is unchanged and surface failed updates to the caller? A successful snap configuration change can otherwise leave the compute without the secret it needs. Secret replacement |
||
| logging.exception(f"Failed to configure libvirt secret {secret_uuid} ({usage})") | ||
|
|
||
|
|
||
| def _configure_networking(snap: Snap, ovs_cli: OVSCli, context: dict) -> None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This opens libvirt even when the host has no Ceph settings. I injected an unavailable libvirt socket with an empty Ceph config. The previous implementation returned without opening the socket, while this path raised before the configure hook could reach Masakari or SR-IOV. Can we handle cleanup failure here so unrelated configuration can still proceed?
Configure hook order