From 384acd32ae95d0186930bd5f350e664d21a1ea0d Mon Sep 17 00:00:00 2001 From: Roger Luethi Date: Thu, 19 Nov 2026 14:29:20 +0100 Subject: [PATCH 1/2] Mask secrets in sync ironic update output Pass the node updates through mask_secrets before printing them in the "Updating baremetal node" task output of _sync_ironic_device, as the dry-run path already does. sync_ironic now hands each device's template variables to _sync_ironic_device, so secret values rendered into other strings are masked as well. The attributes sent to Ironic are unchanged; mask_secrets works on a deep copy. Until now only the dry-run output was masked. The live sync printed the node updates verbatim, with only the driver password removed (it is dropped from the diff because Ironic never returns it). Any value under a key containing "password" or "secret" or starting with "ironic_osism_" in instance_info, extra or properties went to the task output in clear text, as did a secret rendered into a non-secret value such as kernel parameters. The collection of secret values from template variables moves out of _sync_ironic_device_dry_run into _collect_secret_values, shared by both paths. template_vars is an optional argument of _sync_ironic_device; without it, masking falls back to the key-based rules. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Roger Luethi --- osism/tasks/conductor/ironic.py | 35 ++++++++--- .../unit/tasks/conductor/test_ironic_sync.py | 61 +++++++++++++++++++ 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/osism/tasks/conductor/ironic.py b/osism/tasks/conductor/ironic.py index 33b8603a7..bc868e5a2 100644 --- a/osism/tasks/conductor/ironic.py +++ b/osism/tasks/conductor/ironic.py @@ -352,8 +352,27 @@ def _prettify_for_display(obj): return result +def _collect_secret_values(template_vars): + """Return the secret template values, for masking them inside strings.""" + secret_values = set() + for key, value in (template_vars or {}).items(): + if isinstance(value, str) and ( + "password" in key.lower() + or "secret" in key.lower() + or key.lower().startswith("ironic_osism_") + ): + secret_values.add(value) + return secret_values + + def _sync_ironic_device( - request_id, device, node_attributes, ports_attributes, adopt, force + request_id, + device, + node_attributes, + ports_attributes, + adopt, + force, + template_vars=None, ): osism_utils.push_task_output(request_id, f"Processing device {device.name}\n") node = openstack.baremetal_node_show(device.name, ignore_missing=True) @@ -382,9 +401,11 @@ def _sync_ironic_device( if not node_updates["driver_info"]: node_updates.pop("driver_info", None) if node_updates or force: + secret_values = _collect_secret_values(template_vars) osism_utils.push_task_output( request_id, - f"Updating baremetal node for {device.name} with {node_updates}\n", + f"Updating baremetal node for {device.name} with " + f"{mask_secrets(node_updates, secret_values=secret_values)}\n", ) # NOTE: Pop target_raid_config from node_attributes since they cannot be set using node updates target_raid_config = node_attributes.pop("target_raid_config", None) @@ -572,14 +593,7 @@ def _sync_ironic_device_dry_run( request_id, device, node_attributes, ports_attributes, adopt, force, template_vars ): # Collect actual secret values for string-level masking - secret_values = set() - for key, value in template_vars.items(): - if isinstance(value, str) and ( - "password" in key.lower() - or "secret" in key.lower() - or key.lower().startswith("ironic_osism_") - ): - secret_values.add(value) + secret_values = _collect_secret_values(template_vars) masked_attributes = _prettify_for_display( mask_secrets(node_attributes, secret_values=secret_values) @@ -874,6 +888,7 @@ def sync_ironic( ports_attributes, adopt, force, + template_vars=template_vars, ) except Exception as exc: osism_utils.push_task_output( diff --git a/tests/unit/tasks/conductor/test_ironic_sync.py b/tests/unit/tasks/conductor/test_ironic_sync.py index 8dcbb5ba5..97fa63cd8 100644 --- a/tests/unit/tasks/conductor/test_ironic_sync.py +++ b/tests/unit/tasks/conductor/test_ironic_sync.py @@ -225,6 +225,53 @@ def test_update_called_with_full_node_attributes(osism_utils, openstack, deep_co openstack.baremetal_node_update.assert_called_once_with("uuid-1", node_attributes) +def test_update_message_masks_secret_keys(osism_utils, openstack): + # The real deep_compare is used, so the extra delta comes from the + # attributes themselves. The message is masked; the payload is not. + openstack.baremetal_node_show.return_value = _make_node() + openstack.baremetal_node_validate.return_value = _validation(management=False) + node_attributes = { + "driver": "redfish", + "extra": {"ironic_osism_token": "hermes-token"}, + } + + ironic._sync_ironic_device( + "req", _make_device(), node_attributes, [], adopt=False, force=False + ) + + assert _pushed(osism_utils, "Updating baremetal node for node1") + assert not _pushed(osism_utils, "hermes-token") + openstack.baremetal_node_update.assert_called_once_with( + "uuid-1", + {"driver": "redfish", "extra": {"ironic_osism_token": "hermes-token"}}, + ) + + +def test_update_message_masks_secret_template_values(osism_utils, openstack): + # A secret rendered into a value under a non-secret key is only found + # through the template variables it came from. + openstack.baremetal_node_show.return_value = _make_node() + openstack.baremetal_node_validate.return_value = _validation(management=False) + node_attributes = { + "driver": "redfish", + "instance_info": {"kernel_append_params": "console=ttyS0 pw=persephone-pw"}, + } + + ironic._sync_ironic_device( + "req", + _make_device(), + node_attributes, + [], + adopt=False, + force=False, + template_vars={"remote_board_password": "persephone-pw"}, + ) + + assert _pushed(osism_utils, "Updating baremetal node for node1") + assert not _pushed(osism_utils, "persephone-pw") + openstack.baremetal_node_update.assert_called_once_with("uuid-1", node_attributes) + + def test_driver_password_only_change_skips_update(osism_utils, openstack, deep_compare): # The password key is resolved via driver_params[driver]["password"] and # popped before evaluating the change. When it was the only driver_info @@ -704,6 +751,20 @@ def test_sync_ironic_lock_acquired_calls_device_and_releases(sync_env): assert _pushed(sync_env.utils, "Could not fully synchronize device") +def test_sync_ironic_passes_template_vars_to_device_sync(sync_env): + sync_env.netbox.get_devices.return_value = [_make_device()] + sync_env.prepare.return_value = ( + {"driver": "redfish"}, + {"remote_board_password": "persephone-pw"}, + ) + + ironic.sync_ironic("req", MagicMock()) + + assert sync_env.sync_device.call_args.kwargs["template_vars"] == { + "remote_board_password": "persephone-pw" + } + + def test_sync_ironic_lock_not_acquired(sync_env): sync_env.netbox.get_devices.return_value = [_make_device()] sync_env.lock.acquire.return_value = False From 18500baea555b3f61f30280bae5ce186a80cc384 Mon Sep 17 00:00:00 2001 From: Roger Luethi Date: Thu, 19 Nov 2026 14:23:30 +0100 Subject: [PATCH 2/2] Fix sync ironic KeyError for fake-hardware nodes Look up the driver's password key in driver_params with .get() at both places where the ironic sync compares driver_info, so a driver without an entry is treated as having no password key. This affects the real sync (_sync_ironic_device) and the dry-run update branch (_sync_ironic_device_dry_run). driver_params only lists ipmi and redfish. After computing the driver_info diff, the sync drops the password key from it because Ironic never returns the password. That key was looked up with driver_params[driver]["password"], which raises KeyError for any other driver, e.g. fake-hardware. The other uses of driver_params in this module already check membership first. The sync now creates a new node as a stub with only driver and automated_clean, then always runs the update procedure. The stub has no driver_info, so every new fake-hardware node produces a driver_info diff and fails on its first sync: Could not fully synchronize device node1 with ironic: 'fake-hardware' The node is left in enroll without instance_info or extra and is never moved to manageable/available, so nothing can be deployed on it. A config-only workaround does not exist: Ironic's redfish hardware type has no fake power interface, so a node without a real BMC needs the fake-hardware driver. This is the mode metalbox's fake Ironic setup uses. For a driver without a driver_params entry that does carry a password in driver_info (e.g. ilo_password), the password now stays in the driver_info diff. It is sent to Ironic on every sync, which is harmless, and it appears in the "Updating baremetal node" output, where the masking of that output hides it. Add regression tests for the create path, using the real deep_compare so the stub node's missing driver_info produces the diff, and for the dry-run update branch. A further test checks that an unmapped driver's password is masked in the update output. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Roger Luethi --- osism/tasks/conductor/ironic.py | 10 +-- .../unit/tasks/conductor/test_ironic_sync.py | 66 +++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/osism/tasks/conductor/ironic.py b/osism/tasks/conductor/ironic.py index bc868e5a2..c4fdce22b 100644 --- a/osism/tasks/conductor/ironic.py +++ b/osism/tasks/conductor/ironic.py @@ -395,8 +395,8 @@ def _sync_ironic_device( deep_compare(node_attributes, node, node_updates) if "driver_info" in node_updates: # NOTE: The password is not returned by ironic, so we cannot make a comparision and it would always be updated. Therefore we pop it from the dictionary - password_key = driver_params[node_attributes["driver"]]["password"] - if password_key in node_updates["driver_info"]: + password_key = driver_params.get(node_attributes["driver"], {}).get("password") + if password_key and password_key in node_updates["driver_info"]: node_updates["driver_info"].pop(password_key, None) if not node_updates["driver_info"]: node_updates.pop("driver_info", None) @@ -638,8 +638,10 @@ def _indent_json(obj): node_updates = {} deep_compare(node_attributes, node, node_updates) if "driver_info" in node_updates: - password_key = driver_params[node_attributes["driver"]]["password"] - if password_key in node_updates["driver_info"]: + password_key = driver_params.get(node_attributes["driver"], {}).get( + "password" + ) + if password_key and password_key in node_updates["driver_info"]: node_updates["driver_info"].pop(password_key, None) if not node_updates["driver_info"]: node_updates.pop("driver_info", None) diff --git a/tests/unit/tasks/conductor/test_ironic_sync.py b/tests/unit/tasks/conductor/test_ironic_sync.py index 97fa63cd8..76f751828 100644 --- a/tests/unit/tasks/conductor/test_ironic_sync.py +++ b/tests/unit/tasks/conductor/test_ironic_sync.py @@ -207,6 +207,50 @@ def test_target_raid_config_failure_raises(osism_utils, openstack, deep_compare) ) +def test_create_path_updates_node_with_driver_without_params(osism_utils, openstack): + # A driver with no driver_params entry (fake-hardware has no BMC + # credentials) has no password key to drop from the driver_info diff. + # The real deep_compare is used: the stub node carries no driver_info, + # so the create path always produces a driver_info delta. + openstack.baremetal_node_show.return_value = None + openstack.baremetal_node_create.return_value = _make_node(provision_state="enroll") + openstack.baremetal_node_validate.return_value = _validation(management=False) + node_attributes = { + "driver": "fake-hardware", + "driver_info": { + "deploy_kernel": "http://metalbox/osism-ipa.kernel", + "deploy_ramdisk": "http://metalbox/osism-ipa.initramfs", + }, + "instance_info": {"image_source": "http://metalbox/osism-image.qcow2"}, + } + + ironic._sync_ironic_device( + "req", _make_device(), node_attributes, [], adopt=False, force=False + ) + + openstack.baremetal_node_update.assert_called_once_with("uuid-1", node_attributes) + + +def test_create_path_masks_password_of_driver_without_params(osism_utils, openstack): + # Without a driver_params entry the password key is not dropped from the + # driver_info diff, so it reaches the update message and must be masked. + openstack.baremetal_node_show.return_value = None + openstack.baremetal_node_create.return_value = _make_node(provision_state="enroll") + openstack.baremetal_node_validate.return_value = _validation(management=False) + node_attributes = { + "driver": "ilo", + "driver_info": {"ilo_address": "192.0.2.10", "ilo_password": "ariadne-pw"}, + } + + ironic._sync_ironic_device( + "req", _make_device(), node_attributes, [], adopt=False, force=False + ) + + assert _pushed(osism_utils, "Updating baremetal node for node1") + assert not _pushed(osism_utils, "ariadne-pw") + openstack.baremetal_node_update.assert_called_once_with("uuid-1", node_attributes) + + # =========================================================================== # _sync_ironic_device -- node update path # =========================================================================== @@ -638,6 +682,28 @@ def test_dry_run_update_branch_no_update_needed( assert _pushed(osism_utils, "no update needed") +def test_dry_run_update_branch_driver_without_params( + osism_utils, openstack, mask_secrets +): + # Same driver_info handling as the real sync, on the dry-run side: a + # driver without a driver_params entry reports its driver_info change. + node = _make_node() + node["driver"] = "fake-hardware" + node["driver_info"] = {"deploy_kernel": "http://metalbox/old.kernel"} + openstack.baremetal_node_show.return_value = node + node_attributes = { + "driver": "fake-hardware", + "driver_info": {"deploy_kernel": "http://metalbox/osism-ipa.kernel"}, + } + + ironic._sync_ironic_device_dry_run( + "req", _make_device(), node_attributes, [], False, False, {} + ) + + assert _pushed(osism_utils, "Would UPDATE baremetal node") + assert _pushed(osism_utils, "http://metalbox/osism-ipa.kernel") + + def test_dry_run_update_branch_port_reconciliation( osism_utils, openstack, deep_compare, mask_secrets ):