diff --git a/osism/tasks/conductor/ironic.py b/osism/tasks/conductor/ironic.py index 33b8603a7..c4fdce22b 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) @@ -376,15 +395,17 @@ 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) 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) @@ -624,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) @@ -874,6 +890,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..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 # =========================================================================== @@ -225,6 +269,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 @@ -591,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 ): @@ -704,6 +817,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