Repository navigation
feat(ironic): add a panos firmware interface #2778
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
Merged
+290
−43
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
python/ironic-understack/ironic_understack/drivers/panos_base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| """What every PAN-OS (Palo Alto) interface shares.""" | ||
|
|
||
| from ironic.common import exception | ||
|
|
||
|
|
||
| class PanosInterface: | ||
| """Mixin for PAN-OS interfaces: each reaches the appliance at management_ip.""" | ||
|
|
||
| def get_properties(self): | ||
| return { | ||
| "management_ip": "Address of the appliance's management interface, " | ||
| "used for the XML API. Required.", | ||
| } | ||
|
|
||
| def validate(self, task): | ||
| if not task.node.driver_info.get("management_ip"): | ||
| raise exception.MissingParameterValue( | ||
| f"Node {task.node.uuid} is missing driver_info management_ip" | ||
| ) |
116 changes: 116 additions & 0 deletions
116
python/ironic-understack/ironic_understack/drivers/panos_firmware.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """Firmware interface for PAN-OS (Palo Alto) appliances.""" | ||
|
|
||
| import requests | ||
| from ironic import objects | ||
| from ironic.common import exception | ||
| from ironic.drivers import base | ||
| from oslo_log import log | ||
|
|
||
| from ironic_understack.drivers.panos_base import PanosInterface | ||
| from ironic_understack.utils import panos_api | ||
|
|
||
| LOG = log.getLogger(__name__) | ||
|
|
||
| VENDOR = "Palo Alto Networks" | ||
|
|
||
| # ``show system info`` field -> Ironic firmware component name. Content | ||
| # packages that the appliance has never installed report an empty or "0" | ||
| # version and are skipped. | ||
| COMPONENT_FIELDS = ( | ||
| ("sw-version", "panos"), | ||
| ("app-version", "app-content"), | ||
| ("av-version", "antivirus"), | ||
| ("wildfire-version", "wildfire"), | ||
| ("global-protect-client-package-version", "globalprotect-client"), | ||
| ("device-dictionary-version", "device-dictionary"), | ||
| ) | ||
| NOT_INSTALLED = {"", "0", "unknown", "None"} | ||
|
|
||
|
|
||
| class PanosFirmware(PanosInterface, base.FirmwareInterface): | ||
| """Record the software versions a PAN-OS appliance reports. | ||
|
|
||
| Updating is not implemented; this only populates the node's firmware | ||
| components so the running PAN-OS release and content versions are visible | ||
| through the Ironic API. | ||
| """ | ||
|
|
||
| def update(self, task, settings): | ||
| raise exception.UnsupportedDriverExtension( | ||
| driver=task.node.driver, extension="firmware update" | ||
| ) | ||
|
|
||
| @base.verify_step(priority=5) | ||
| @base.clean_step(priority=0, requires_ramdisk=False) | ||
| @base.service_step(requires_ramdisk=False) | ||
| def cache_firmware_components(self, task): | ||
| """Store or update the appliance's software versions on the node.""" | ||
| address = task.node.driver_info["management_ip"] | ||
| client = panos_api.PanosClient(address) | ||
| try: | ||
| client.login() | ||
| system = client.system_info() | ||
| except (panos_api.PanosApiError, requests.RequestException) as e: | ||
| raise exception.IronicException( | ||
| f"Cannot read firmware versions from PAN-OS {address}: {e}" | ||
| ) from e | ||
| record_versions(task, system) | ||
|
|
||
|
|
||
| def record_versions(task, system: dict[str, str]) -> None: | ||
| """Store the versions ``show system info`` reported as firmware components.""" | ||
| settings = _components_from_system_info(system) | ||
| if not settings: | ||
| raise exception.UnsupportedDriverExtension( | ||
| driver=task.node.driver, extension="firmware components" | ||
| ) | ||
| _sync(task, settings) | ||
|
|
||
|
|
||
| def _components_from_system_info(system: dict[str, str]) -> list[dict]: | ||
| """Return a firmware component for each installed version.""" | ||
| # The appliance itself is the one thing here with an identity. | ||
| appliance = { | ||
| "vendor": VENDOR, | ||
| "model": system.get("model") or None, | ||
| "serial_number": system.get("serial") or None, | ||
| } | ||
| return [ | ||
| { | ||
| "component": component, | ||
| "current_version": version, | ||
| **(appliance if component == "panos" else dict.fromkeys(appliance)), | ||
| } | ||
| for field, component in COMPONENT_FIELDS | ||
| if (version := system.get(field, "")) not in NOT_INSTALLED | ||
| ] | ||
|
|
||
|
|
||
| def _sync(task, settings: list[dict]) -> None: | ||
| node_id = task.node.id | ||
| create_list, update_list, _ = ( | ||
| objects.FirmwareComponentList.sync_firmware_components( | ||
| task.context, node_id, settings | ||
| ) | ||
| ) | ||
| for new in create_list: | ||
| objects.FirmwareComponent( | ||
| task.context, | ||
| node_id=node_id, | ||
| component=new["component"], | ||
| current_version=new["current_version"], | ||
| vendor=new["vendor"], | ||
| model=new["model"], | ||
| serial_number=new["serial_number"], | ||
| ).create() | ||
| for up in update_list: | ||
| existing = objects.FirmwareComponent.get( | ||
| task.context, node_id=node_id, name=up["component"] | ||
| ) | ||
| if existing.current_version != up["current_version"]: | ||
| existing.last_version_flashed = up["current_version"] | ||
| existing.current_version = up["current_version"] | ||
| existing.vendor = up["vendor"] | ||
| existing.model = up["model"] | ||
| existing.serial_number = up["serial_number"] | ||
| existing.save() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
python/ironic-understack/ironic_understack/tests/test_panos_firmware.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
| import requests | ||
| from ironic import objects | ||
| from ironic.common import exception | ||
|
|
||
| from ironic_understack.drivers import panos_firmware | ||
| from ironic_understack.drivers.panos_firmware import PanosFirmware | ||
| from ironic_understack.utils import panos_api | ||
|
|
||
| SYSTEM = { | ||
| "model": "PA-1410", | ||
| "serial": "000000000001", | ||
| "sw-version": "11.0.0", | ||
| "app-version": "8900-9000", | ||
| "av-version": "0", | ||
| "wildfire-version": "", | ||
| "global-protect-client-package-version": "6.2.0", | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def task(): | ||
| task = Mock() | ||
| task.node.id = 7 | ||
| task.node.uuid = "node-uuid" | ||
| task.node.driver_info = {"management_ip": "192.0.2.10"} | ||
| return task | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def client(mocker): | ||
| client = Mock() | ||
| client.system_info.return_value = dict(SYSTEM) | ||
| mocker.patch.object(panos_api, "PanosClient", return_value=client) | ||
| return client | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sync(mocker): | ||
| objects.register_all() | ||
| mocker.patch.object(objects, "FirmwareComponent") | ||
| return mocker.patch.object( | ||
| objects.FirmwareComponentList, | ||
| "sync_firmware_components", | ||
| return_value=([], [], []), | ||
| ) | ||
|
|
||
|
|
||
| def test_steps_are_registered(): | ||
| fw = PanosFirmware() | ||
| assert {s["step"]: s["priority"] for s in fw.get_clean_steps(Mock())} == { | ||
| "cache_firmware_components": 0 | ||
| } | ||
| assert [s["step"] for s in fw.get_verify_steps(Mock())] == [ | ||
| "cache_firmware_components" | ||
| ] | ||
| assert [s["step"] for s in fw.get_service_steps(Mock())] == [ | ||
| "cache_firmware_components" | ||
| ] | ||
|
|
||
|
|
||
| def test_components_skip_uninstalled_content(): | ||
| settings = panos_firmware._components_from_system_info(SYSTEM) | ||
| assert {s["component"]: s["current_version"] for s in settings} == { | ||
| "panos": "11.0.0", | ||
| "app-content": "8900-9000", | ||
| "globalprotect-client": "6.2.0", | ||
| } | ||
| panos = settings[0] | ||
| assert panos["vendor"] == "Palo Alto Networks" | ||
| assert panos["model"] == "PA-1410" | ||
| assert panos["serial_number"] == "000000000001" | ||
|
|
||
|
|
||
| def test_cache_creates_new_components(task, client, sync, mocker): | ||
| sync.return_value = ( | ||
| panos_firmware._components_from_system_info(SYSTEM), | ||
| [], | ||
| [], | ||
| ) | ||
|
|
||
| PanosFirmware().cache_firmware_components(task) | ||
|
|
||
| panos_api.PanosClient.assert_called_once_with("192.0.2.10") | ||
| assert objects.FirmwareComponent.call_count == 3 | ||
| created = objects.FirmwareComponent.call_args_list[0].kwargs | ||
| assert created["component"] == "panos" | ||
| assert created["current_version"] == "11.0.0" | ||
|
|
||
|
|
||
| def test_cache_updates_changed_component(task, client, sync): | ||
| new = panos_firmware._components_from_system_info(SYSTEM)[0] | ||
| sync.return_value = ([], [new], []) | ||
| existing = Mock(current_version="10.2.0") | ||
| objects.FirmwareComponent.get.return_value = existing | ||
|
|
||
| PanosFirmware().cache_firmware_components(task) | ||
|
|
||
| assert existing.current_version == "11.0.0" | ||
| assert existing.last_version_flashed == "11.0.0" | ||
| existing.save.assert_called_once() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "error", | ||
| [panos_api.PanosApiError("rejected"), requests.ConnectionError("unreachable")], | ||
| ) | ||
| def test_cache_failure(task, client, sync, error): | ||
| client.login.side_effect = error | ||
| with pytest.raises(exception.IronicException, match=r"192\.0\.2\.10"): | ||
| PanosFirmware().cache_firmware_components(task) | ||
| sync.assert_not_called() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.