diff --git a/changelogs/fragments/vyos_facts_secure_boot.yml b/changelogs/fragments/vyos_facts_secure_boot.yml new file mode 100644 index 00000000..cedae768 --- /dev/null +++ b/changelogs/fragments/vyos_facts_secure_boot.yml @@ -0,0 +1,5 @@ +--- +minor_changes: + - vyos_facts - new fact ansible_net_secure_boot in the default subset, with the device's Secure + Boot state - enabled, uefi (false for legacy BIOS) and, when Secure Boot is enabled, the + kernel signing certificates. Images without the show secure-boot command do not return it. diff --git a/docs/vyos.vyos.vyos_facts_module.rst b/docs/vyos.vyos.vyos_facts_module.rst index ec45d6d5..6893d566 100644 --- a/docs/vyos.vyos.vyos_facts_module.rst +++ b/docs/vyos.vyos.vyos_facts_module.rst @@ -1,330 +1,433 @@ .. _vyos.vyos.vyos_facts_module: ******************** vyos.vyos.vyos_facts ******************** **Get facts about vyos devices.** Version added: 1.0.0 .. contents:: :local: :depth: 1 Synopsis -------- - Collects facts from network devices running the vyos operating system. This module places the facts gathered in the fact tree keyed by the respective resource name. The facts module will always collect a base set of facts from the device and can enable or disable collection of additional facts. Parameters ---------- .. raw:: html
Parameter Choices/Defaults Comments
available_network_resources
boolean
    Choices:
  • no ←
  • yes
When 'True' a list of network resources for which resource modules are available will be provided.
gather_network_resources
list / elements=string
When supplied, this argument will restrict the facts collected to a given subset. Possible values for this argument include all and the resources like interfaces. Can specify a list of values to include a larger subset. Values can also be used with an initial ! to specify that a specific subset should not be collected. Valid subsets are 'all', 'interfaces', 'l3_interfaces', 'lag_interfaces', 'lldp_global', 'lldp_interfaces', 'static_routes', 'firewall_rules', 'firewall_global', 'firewall_interfaces', 'ospfv3', 'ospfv2'.
gather_subset
list / elements=string
Default:
"min"
When supplied, this argument will restrict the facts collected to a given subset. Possible values for this argument include all, default, config, neighbors and min. Can specify a list of values to include a larger subset. Values can also be used with an initial ! to specify that a specific subset should not be collected.

Notes ----- .. note:: - Tested against VyOS 1.3.8, 1.4.2, the upcoming 1.5, and the rolling release of spring 2025. - This module works with connection ``ansible.netcommon.network_cli``. See `the VyOS OS Platform Options <../network/user_guide/platform_vyos.html>`_. - For more information on using Ansible to manage network devices see the :ref:`Ansible Network Guide ` Examples -------- .. code-block:: yaml # Gather all facts - vyos.vyos.vyos_facts: gather_subset: all gather_network_resources: all # collect only the config and default facts - vyos.vyos.vyos_facts: gather_subset: config # collect everything exception the config - vyos.vyos.vyos_facts: gather_subset: '!config' # Collect only the interfaces facts - vyos.vyos.vyos_facts: gather_subset: - '!all' - '!min' gather_network_resources: - interfaces # Do not collect interfaces facts - vyos.vyos.vyos_facts: gather_network_resources: - '!interfaces' # Collect interfaces and minimal default facts - vyos.vyos.vyos_facts: gather_subset: min gather_network_resources: interfaces Return Values ------------- Common return values are documented `here `_, the following are the fields unique to this module: .. raw:: html - + - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -
KeyKey Returned Description
+
ansible_net_api
string
always
The name of the transport

+
ansible_net_commits
list
when present
The set of available configuration revisions

+
ansible_net_config
string
when config is configured
The running-config from the device

+
ansible_net_gather_network_resources
list
always
The list of fact resource subsets collected from the device

+
ansible_net_gather_subset
list
always
The list of subsets gathered by the module

+
ansible_net_hostname
string
always
The configured system hostname

+
ansible_net_model
string
always
The device model string

+
ansible_net_neighbors
list
when interface is configured
The set of LLDP neighbors

+
ansible_net_python_version
string
always
The Python version Ansible controller is using

+
+ ansible_net_secure_boot + +
+ dictionary +
+
when the device reports its Secure Boot state +
The Secure Boot state of the device, from show secure-boot.
+
Only on images that have the command; older images do not return it.
+
+
Sample:
+
{'enabled': True, 'uefi': True, 'certificates': [{'issuer': 'CN=VyOS Secure Boot CA', 'subject': 'CN=VyOS Secure Boot Signer'}]}
+
  +
+ certificates + +
+ list + / elements=dictionary +
+
+
Issuer and subject of the kernel signing certificates, when Secure Boot is enabled.
+
+
   +
+ issuer + +
+ string +
+
+
Certificate issuer.
+
+
   +
+ subject + +
+ string +
+
+
Certificate subject.
+
+
  +
+ enabled + +
+ boolean +
+
+
Whether Secure Boot is enabled.
+
+
  +
+ uefi + +
+ boolean +
+
+
Whether the system booted with UEFI; false for legacy BIOS.
+
+
ansible_net_serialnum
string
always
The serial number of the device

+
ansible_net_version
string
always
The version of the software running



Status ------ Authors ~~~~~~~ - Nathaniel Case (@qalthos) - Nilashish Chakraborty (@Nilashishc) - Rohit Thakur (@rohitthakur2590) diff --git a/plugins/module_utils/network/vyos/facts/legacy/base.py b/plugins/module_utils/network/vyos/facts/legacy/base.py index 59666e19..238cc3b0 100644 --- a/plugins/module_utils/network/vyos/facts/legacy/base.py +++ b/plugins/module_utils/network/vyos/facts/legacy/base.py @@ -1,159 +1,208 @@ # -*- coding: utf-8 -*- # Copyright 2019 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) """ The VyOS interfaces fact class It is in this file the configuration is collected from the device for a given resource, parsed, and the facts tree is populated based on the configuration. """ from __future__ import absolute_import, division, print_function + __metaclass__ = type import platform import re +from ansible.module_utils.common.text.converters import to_text +from ansible.module_utils.connection import ConnectionError + from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.vyos import ( get_capabilities, + get_connection, run_commands, ) class LegacyFactsBase(object): COMMANDS = frozenset() def __init__(self, module): self.module = module self.facts = dict() self.warnings = list() self.responses = None def populate(self): self.responses = run_commands(self.module, list(self.COMMANDS)) class Default(LegacyFactsBase): COMMANDS = [ "show version", ] def populate(self): super(Default, self).populate() data = self.responses[0] self.facts["serialnum"] = self.parse_serialnum(data) self.facts.update(self.platform_facts()) + secure_boot = self.secure_boot_facts() + if secure_boot: + self.facts["secure_boot"] = secure_boot + + def secure_boot_facts(self): + """Secure Boot state, on images that have show secure-boot. + + Older images reject the command; the fact is then left out. + """ + data = self.run_optional("show secure-boot") + if data is None: + return None + match = re.search(r"^SecureBoot\s+(enabled|disabled)\s*$", data, re.M) + if not match: + return None + facts = { + "enabled": match.group(1) == "enabled", + "uefi": "legacy BIOS mode" not in data, + } + if facts["enabled"]: + detail = self.run_optional("show secure-boot detail") or "" + facts["certificates"] = [ + {"issuer": issuer, "subject": subject} + for issuer, subject in re.findall( + r"^Issuer:\s+(.+?)\s*\nSubject:\s+(.+?)\s*$", + detail, + re.M, + ) + ] + return facts + + def run_optional(self, command): + """Output of a command the image may not have, or None when the device rejects it. + + Only "Invalid command" means the image lacks the command; other connection errors + (timeouts, disconnects) fail the module as in run_commands. + """ + try: + return get_connection(self.module).run_commands(commands=[command])[0] + except ConnectionError as exc: + msg = to_text(exc, errors="surrogate_then_replace") + if "Invalid command" in msg: + return None + self.module.fail_json(msg=msg) def parse_serialnum(self, data): match = re.search(r"(?:HW|Hardware) S/N:\s+(\S+)", data) if match: return match.group(1) def platform_facts(self): platform_facts = {} resp = get_capabilities(self.module) device_info = resp["device_info"] platform_facts["system"] = device_info["network_os"] for item in ("model", "image", "version", "platform", "hostname"): val = device_info.get("network_os_%s" % item) if val: platform_facts[item] = val platform_facts["api"] = resp["network_api"] platform_facts["python_version"] = platform.python_version() return platform_facts class Config(LegacyFactsBase): COMMANDS = [ "show configuration commands", "show system commit", ] def populate(self): super(Config, self).populate() self.facts["config"] = self.responses commits = self.responses[1] entries = list() entry = None for line in commits.split("\n"): match = re.match(r"(\d+)\s+(.+)by(.+)via(.+)", line) if match: if entry: entries.append(entry) entry = dict( revision=match.group(1), datetime=match.group(2), by=str(match.group(3)).strip(), via=str(match.group(4)).strip(), comment=None, ) else: entry["comment"] = line.strip() self.facts["commits"] = entries class Neighbors(LegacyFactsBase): COMMANDS = [ "show lldp neighbors", "show lldp neighbors detail", ] def populate(self): super(Neighbors, self).populate() all_neighbors = self.responses[0] if "LLDP not configured" not in all_neighbors: neighbors = self.parse(self.responses[1]) self.facts["neighbors"] = self.parse_neighbors(neighbors) def parse(self, data): parsed = list() values = None for line in data.split("\n"): if not line: continue if line[0] == " ": values += "\n%s" % line elif line.startswith("Interface"): if values: parsed.append(values) values = line if values: parsed.append(values) return parsed def parse_neighbors(self, data): facts = dict() for item in data: interface = self.parse_interface(item) host = self.parse_host(item) port = self.parse_port(item) if interface not in facts: facts[interface] = list() facts[interface].append(dict(host=host, port=port)) return facts def parse_interface(self, data): match = re.search(r"^Interface:\s+(\S+),", data) return match.group(1) def parse_host(self, data): match = re.search(r"SysName:\s+(.+)$", data, re.M) if match: return match.group(1) def parse_port(self, data): match = re.search(r"PortDescr:\s+(.+)$", data, re.M) if match: return match.group(1) diff --git a/plugins/modules/vyos_facts.py b/plugins/modules/vyos_facts.py index 3d6d1b05..dd7874ca 100644 --- a/plugins/modules/vyos_facts.py +++ b/plugins/modules/vyos_facts.py @@ -1,176 +1,207 @@ #!/usr/bin/python # -*- coding: utf-8 -*- # Copyright 2019 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function + __metaclass__ = type """ The module file for vyos_facts """ DOCUMENTATION = """ module: vyos_facts short_description: Get facts about vyos devices. description: - Collects facts from network devices running the vyos operating system. This module places the facts gathered in the fact tree keyed by the respective resource name. The facts module will always collect a base set of facts from the device and can enable or disable collection of additional facts. version_added: 1.0.0 author: - Nathaniel Case (@qalthos) - Nilashish Chakraborty (@Nilashishc) - Rohit Thakur (@rohitthakur2590) extends_documentation_fragment: - vyos.vyos.vyos notes: - Tested against VyOS 1.3.8, 1.4.2, the upcoming 1.5, and the rolling release of spring 2025. - This module works with connection C(ansible.netcommon.network_cli). See L(the VyOS OS Platform Options,../network/user_guide/platform_vyos.html). options: gather_subset: description: - When supplied, this argument will restrict the facts collected to a given subset. Possible values for this argument include C(all), C(default), C(config), C(neighbors) and C(min). Can specify a list of values to include a larger subset. Values can also be used with an initial C(!) to specify that a specific subset should not be collected. required: false default: 'min' type: list elements: str gather_network_resources: description: - When supplied, this argument will restrict the facts collected to a given subset. Possible values for this argument include all and the resources like interfaces. Can specify a list of values to include a larger subset. Values can also be used with an initial C(!) to specify that a specific subset should not be collected. Valid subsets are 'all', 'interfaces', 'l3_interfaces', 'lag_interfaces', 'lldp_global', 'lldp_interfaces', 'static_routes', 'firewall_rules', 'firewall_global', 'firewall_interfaces', 'ospfv3', 'ospfv2'. required: false type: list elements: str available_network_resources: description: When 'True' a list of network resources for which resource modules are available will be provided. type: bool default: false """ EXAMPLES = """ # Gather all facts - vyos.vyos.vyos_facts: gather_subset: all gather_network_resources: all # collect only the config and default facts - vyos.vyos.vyos_facts: gather_subset: config # collect everything exception the config - vyos.vyos.vyos_facts: gather_subset: '!config' # Collect only the interfaces facts - vyos.vyos.vyos_facts: gather_subset: - '!all' - '!min' gather_network_resources: - interfaces # Do not collect interfaces facts - vyos.vyos.vyos_facts: gather_network_resources: - '!interfaces' # Collect interfaces and minimal default facts - vyos.vyos.vyos_facts: gather_subset: min gather_network_resources: interfaces """ RETURN = """ ansible_net_config: description: The running-config from the device returned: when config is configured type: str ansible_net_commits: description: The set of available configuration revisions returned: when present type: list ansible_net_hostname: description: The configured system hostname returned: always type: str ansible_net_model: description: The device model string returned: always type: str ansible_net_serialnum: description: The serial number of the device returned: always type: str +ansible_net_secure_boot: + description: + - The Secure Boot state of the device, from C(show secure-boot). + - Only on images that have the command; older images do not return it. + returned: when the device reports its Secure Boot state + type: dict + contains: + enabled: + description: Whether Secure Boot is enabled. + type: bool + uefi: + description: Whether the system booted with UEFI; C(false) for legacy BIOS. + type: bool + certificates: + description: Issuer and subject of the kernel signing certificates, when Secure Boot is enabled. + type: list + elements: dict + contains: + issuer: + description: Certificate issuer. + type: str + subject: + description: Certificate subject. + type: str + sample: + enabled: true + uefi: true + certificates: + - issuer: CN=VyOS Secure Boot CA + subject: CN=VyOS Secure Boot Signer ansible_net_version: description: The version of the software running returned: always type: str ansible_net_neighbors: description: The set of LLDP neighbors returned: when interface is configured type: list ansible_net_gather_subset: description: The list of subsets gathered by the module returned: always type: list ansible_net_api: description: The name of the transport returned: always type: str ansible_net_python_version: description: The Python version Ansible controller is using returned: always type: str ansible_net_gather_network_resources: description: The list of fact resource subsets collected from the device returned: always type: list """ from ansible.module_utils.basic import AnsibleModule from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.facts.facts import ( FactsArgs, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.facts import ( FACT_RESOURCE_SUBSETS, Facts, ) def main(): """ Main entry point for module execution :returns: ansible_facts """ argument_spec = FactsArgs.argument_spec module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = [] ansible_facts = {} if module.params.get("available_network_resources"): ansible_facts["available_network_resources"] = sorted(FACT_RESOURCE_SUBSETS.keys()) result = Facts(module).get_facts() additional_facts, additional_warnings = result ansible_facts.update(additional_facts) warnings.extend(additional_warnings) module.exit_json(ansible_facts=ansible_facts, warnings=warnings) if __name__ == "__main__": main() diff --git a/tests/integration/targets/vyos_facts/tests/cli/basic_facts.yaml b/tests/integration/targets/vyos_facts/tests/cli/basic_facts.yaml index 50e95889..3ace9b2c 100644 --- a/tests/integration/targets/vyos_facts/tests/cli/basic_facts.yaml +++ b/tests/integration/targets/vyos_facts/tests/cli/basic_facts.yaml @@ -1,77 +1,96 @@ --- - name: get host name register: vyos_host vyos.vyos.vyos_command: commands: - show host name - name: get version info register: vyos_version vyos.vyos.vyos_command: commands: - show version - name: collect all facts from the device register: result vyos.vyos.vyos_facts: gather_subset: all - name: check that hostname is present assert: that: - result.ansible_facts.ansible_net_hostname == vyos_host.stdout[0] - name: check that subsets are present assert: that: - "'neighbors' in result.ansible_facts.ansible_net_gather_subset" - "'default' in result.ansible_facts.ansible_net_gather_subset" - "'config' in result.ansible_facts.ansible_net_gather_subset" - name: check that version info is present assert: that: - result.ansible_facts.ansible_net_version in (vyos_version.stdout_lines[0] | select('search', 'Version:') | list | first) - result.ansible_facts.ansible_net_model in (vyos_version.stdout_lines[0] | select('search', 'Hardware model:') | list | first) - result.ansible_facts.ansible_net_serialnum in (vyos_version.stdout_lines[0] | select('search', 'Hardware S/N:') | list | first) - name: check that config info is present assert: that: - result.ansible_facts.ansible_net_commits is defined - result.ansible_facts.ansible_net_config is defined - name: Get list of avaliable network resources for VyOS register: result vyos.vyos.vyos_facts: available_network_resources: true gather_network_resources: all - name: Assert that correct available_resources_list returned assert: that: - result.changed == false - "{{ result['ansible_facts']['available_network_resources'] | symmetric_difference( result['ansible_facts']['ansible_net_gather_network_resources'] ) | length == 0 }}" - name: test getting default facts register: result vyos.vyos.vyos_facts: - assert: that: - result.changed == false - "'default' in result.ansible_facts.ansible_net_gather_subset" - "'config' not in result.ansible_facts.ansible_net_gather_subset" - result.ansible_facts.ansible_net_hostname is defined - result.ansible_facts.ansible_net_model is defined - result.ansible_facts.ansible_net_python_version is defined - result.ansible_facts.ansible_net_serialnum is defined - result.ansible_facts.ansible_net_system is defined - result.ansible_facts.ansible_net_version is defined - result.ansible_facts.ansible_network_resources == {} + +- name: Collect the default facts, including Secure Boot where the image reports it + register: result + vyos.vyos.vyos_facts: + gather_subset: default + +- name: Read the Secure Boot state directly, on images that have the command + register: secure_boot + when: result.ansible_facts.ansible_net_secure_boot is defined + vyos.vyos.vyos_command: + commands: + - show secure-boot + +- name: Check that the Secure Boot facts match the device + when: result.ansible_facts.ansible_net_secure_boot is defined + assert: + that: + - result.ansible_facts.ansible_net_secure_boot.enabled == ('SecureBoot enabled' in secure_boot.stdout[0]) + - result.ansible_facts.ansible_net_secure_boot.uefi == ('legacy BIOS' not in secure_boot.stdout[0]) diff --git a/tests/unit/modules/network/vyos/fixtures/show_secure-boot b/tests/unit/modules/network/vyos/fixtures/show_secure-boot new file mode 100644 index 00000000..977a06a5 --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/show_secure-boot @@ -0,0 +1,2 @@ +System run in legacy BIOS mode! +SecureBoot disabled diff --git a/tests/unit/modules/network/vyos/test_vyos_facts.py b/tests/unit/modules/network/vyos/test_vyos_facts.py index 3f85bf7d..8deb01ee 100644 --- a/tests/unit/modules/network/vyos/test_vyos_facts.py +++ b/tests/unit/modules/network/vyos/test_vyos_facts.py @@ -1,109 +1,180 @@ # (c) 2016 Red Hat Inc. # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . # Make coding more python3-ish from __future__ import absolute_import, division, print_function + __metaclass__ = type import json from unittest.mock import patch +from ansible.module_utils.connection import ConnectionError + from ansible_collections.vyos.vyos.plugins.modules import vyos_facts from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args from .vyos_module import TestVyosModule, load_fixture class TestVyosFactsModule(TestVyosModule): module = vyos_facts def setUp(self): super(TestVyosFactsModule, self).setUp() self.mock_run_commands = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.legacy.base.run_commands", ) self.run_commands = self.mock_run_commands.start() + self.mock_get_connection = patch( + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.legacy.base.get_connection", + ) + self.get_connection = self.mock_get_connection.start() + self.mock_get_resource_connection = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.facts.facts.get_resource_connection", ) self.get_resource_connection = self.mock_get_resource_connection.start() + # command outputs a test sets directly, instead of a fixture file + self.outputs = {} + self.mock_get_capabilities = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.legacy.base.get_capabilities", ) self.get_capabilities = self.mock_get_capabilities.start() self.get_capabilities.return_value = { "device_info": { "network_os": "vyos", "network_os_hostname": "vyos01", "network_os_model": "VMware", "network_os_version": "VyOS 1.1.7", "network_os_major_version": "1.1", }, "network_api": "cliconf", } def tearDown(self): super(TestVyosFactsModule, self).tearDown() self.mock_run_commands.stop() + self.mock_get_connection.stop() self.mock_get_capabilities.stop() self.mock_get_resource_connection.stop() def load_fixtures(self, commands=None, filename=None): def load_from_file(*args, **kwargs): module, commands = args output = list() for item in commands: try: obj = json.loads(item) command = obj["command"] except ValueError: command = item + if command in self.outputs: + if isinstance(self.outputs[command], Exception): + raise self.outputs[command] + output.append(self.outputs[command]) + continue filename = str(command).replace(" ", "_") output.append(load_fixture(filename)) return output self.run_commands.side_effect = load_from_file + self.get_connection.return_value.run_commands.side_effect = ( + lambda commands, **kwargs: load_from_file(None, commands) + ) def test_vyos_facts_default(self): set_module_args(dict(gather_subset="default")) result = self.execute_module() facts = result.get("ansible_facts") - self.assertEqual(len(facts), 10) + self.assertEqual(len(facts), 11) self.assertEqual(facts["ansible_net_hostname"].strip(), "vyos01") self.assertEqual(facts["ansible_net_version"], "VyOS 1.1.7") def test_vyos_facts_not_all(self): set_module_args(dict(gather_subset="!all")) result = self.execute_module() facts = result.get("ansible_facts") - self.assertEqual(len(facts), 10) + self.assertEqual(len(facts), 11) self.assertEqual(facts["ansible_net_hostname"].strip(), "vyos01") self.assertEqual(facts["ansible_net_version"], "VyOS 1.1.7") def test_vyos_facts_exclude_most(self): set_module_args(dict(gather_subset=["!neighbors", "!config"])) result = self.execute_module() facts = result.get("ansible_facts") - self.assertEqual(len(facts), 10) + self.assertEqual(len(facts), 11) self.assertEqual(facts["ansible_net_hostname"].strip(), "vyos01") self.assertEqual(facts["ansible_net_version"], "VyOS 1.1.7") def test_vyos_facts_invalid_subset(self): set_module_args(dict(gather_subset="cereal")) self.execute_module(failed=True) + + def test_vyos_facts_secure_boot_legacy_bios(self): + # real output of a rolling image booted with legacy BIOS (fixture show_secure-boot) + set_module_args(dict(gather_subset="default")) + facts = self.execute_module()["ansible_facts"] + self.assertEqual(facts["ansible_net_secure_boot"], {"enabled": False, "uefi": False}) + + def test_vyos_facts_secure_boot_uefi_disabled(self): + # real output of a rolling image booted with UEFI, Secure Boot off + self.outputs = {"show secure-boot": "SecureBoot disabled"} + set_module_args(dict(gather_subset="default")) + facts = self.execute_module()["ansible_facts"] + self.assertEqual(facts["ansible_net_secure_boot"], {"enabled": False, "uefi": True}) + + def test_vyos_facts_secure_boot_enabled(self): + # format of the op-mode script (secure_boot.py); placeholder certificate names + self.outputs = { + "show secure-boot": "SecureBoot enabled", + "show secure-boot detail": "Issuer: CN=VyOS Secure Boot CA\n" + "Subject: CN=VyOS Secure Boot Signer", + } + set_module_args(dict(gather_subset="default")) + facts = self.execute_module()["ansible_facts"] + self.assertEqual( + facts["ansible_net_secure_boot"], + { + "enabled": True, + "uefi": True, + "certificates": [ + { + "issuer": "CN=VyOS Secure Boot CA", + "subject": "CN=VyOS Secure Boot Signer", + }, + ], + }, + ) + + def test_vyos_facts_secure_boot_not_supported(self): + # images without the command (1.5.1 and older): no fact, and no failure + self.outputs = {"show secure-boot": ConnectionError("Invalid command: show [secure-boot]")} + set_module_args(dict(gather_subset="default")) + facts = self.execute_module()["ansible_facts"] + self.assertNotIn("ansible_net_secure_boot", facts) + self.assertEqual(len(facts), 10) + + def test_vyos_facts_secure_boot_connection_error(self): + # a connection problem is not "command not supported": the module fails + self.outputs = {"show secure-boot": ConnectionError("timeout value 30 seconds reached")} + set_module_args(dict(gather_subset="default")) + result = self.execute_module(failed=True) + self.assertIn("timeout value 30 seconds reached", result["msg"])