|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | +from swsscommon import swsscommon |
| 5 | +from parameterized import parameterized |
| 6 | +from sonic_py_common.general import load_module_from_source |
| 7 | +from unittest import TestCase, mock |
| 8 | +from pyfakefs.fake_filesystem_unittest import patchfs |
| 9 | + |
| 10 | +from .test_redfish_acl_vectors import REDFISH_ACL_TEST_VECTOR, REDFISH_ACL_DISABLED_FEATURE_VECTORS |
| 11 | +from tests.common.mock_configdb import MockConfigDb |
| 12 | + |
| 13 | + |
| 14 | +DBCONFIG_PATH = '/var/run/redis/sonic-db/database_config.json' |
| 15 | + |
| 16 | + |
| 17 | +class TestCaclmgrdRedfishAcl(TestCase): |
| 18 | + """ |
| 19 | + Verifies that an ACL_TABLE referencing the REDFISH service produces |
| 20 | + iptables rules on tcp/443. Guards against the ACL_SERVICES["REDFISH"] |
| 21 | + entry being removed or renamed. |
| 22 | + """ |
| 23 | + def setUp(self): |
| 24 | + swsscommon.ConfigDBConnector = MockConfigDb |
| 25 | + test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 26 | + modules_path = os.path.dirname(test_path) |
| 27 | + scripts_path = os.path.join(modules_path, "scripts") |
| 28 | + sys.path.insert(0, modules_path) |
| 29 | + caclmgrd_path = os.path.join(scripts_path, 'caclmgrd') |
| 30 | + self.caclmgrd = load_module_from_source('caclmgrd', caclmgrd_path) |
| 31 | + |
| 32 | + @parameterized.expand(REDFISH_ACL_TEST_VECTOR) |
| 33 | + @patchfs |
| 34 | + def test_caclmgrd_redfish_acl(self, test_name, test_data, fs): |
| 35 | + if not os.path.exists(DBCONFIG_PATH): |
| 36 | + fs.create_file(DBCONFIG_PATH) |
| 37 | + |
| 38 | + MockConfigDb.set_config_db(test_data["config_db"]) |
| 39 | + self.caclmgrd.ControlPlaneAclManager.get_namespace_mgmt_ip = mock.MagicMock() |
| 40 | + self.caclmgrd.ControlPlaneAclManager.get_namespace_mgmt_ipv6 = mock.MagicMock() |
| 41 | + self.caclmgrd.ControlPlaneAclManager.generate_block_ip2me_traffic_iptables_commands = mock.MagicMock(return_value=[]) |
| 42 | + self.caclmgrd.ControlPlaneAclManager.get_chain_list = mock.MagicMock(return_value=["INPUT", "FORWARD", "OUTPUT"]) |
| 43 | + self.caclmgrd.ControlPlaneAclManager.get_chassis_midplane_interface_ip = mock.MagicMock(return_value='') |
| 44 | + caclmgrd_daemon = self.caclmgrd.ControlPlaneAclManager("caclmgrd") |
| 45 | + |
| 46 | + self.assertTrue(caclmgrd_daemon.RedfishAllowed, |
| 47 | + "Seed at __init__ should set RedfishAllowed=True for these vectors") |
| 48 | + |
| 49 | + self.assertIn("REDFISH", caclmgrd_daemon.ACL_SERVICES, |
| 50 | + "ACL_SERVICES is missing the 'REDFISH' entry — see scripts/caclmgrd") |
| 51 | + self.assertEqual(caclmgrd_daemon.ACL_SERVICES["REDFISH"]["dst_ports"], ["443"]) |
| 52 | + self.assertEqual(caclmgrd_daemon.ACL_SERVICES["REDFISH"]["ip_protocols"], ["tcp"]) |
| 53 | + |
| 54 | + iptables_rules_ret, _ = caclmgrd_daemon.get_acl_rules_and_translate_to_iptables_commands('', MockConfigDb()) |
| 55 | + test_data['return'] = [tuple(i) for i in test_data['return']] |
| 56 | + iptables_rules_ret = [tuple(i) for i in iptables_rules_ret] |
| 57 | + self.assertEqual(set(test_data["return"]).issubset(set(iptables_rules_ret)), True, |
| 58 | + "Expected iptables rules not produced. Got: {}".format(iptables_rules_ret)) |
| 59 | + |
| 60 | + @parameterized.expand(REDFISH_ACL_DISABLED_FEATURE_VECTORS) |
| 61 | + @patchfs |
| 62 | + def test_caclmgrd_redfish_acl_skipped_when_feature_disabled(self, test_name, test_data, fs): |
| 63 | + """ |
| 64 | + REDFISH only ships on BMC-equipped platforms. When the redfish FEATURE |
| 65 | + is "disabled", RedfishAllowed stays False and an operator template |
| 66 | + referencing the REDFISH service must not program any tcp/443 iptables |
| 67 | + rules -- otherwise it would silently govern whatever else (if anything) |
| 68 | + listens on 443. Parametrized over IPv4 and IPv6 to confirm the gate |
| 69 | + skips REDFISH regardless of IP family. |
| 70 | + """ |
| 71 | + if not os.path.exists(DBCONFIG_PATH): |
| 72 | + fs.create_file(DBCONFIG_PATH) |
| 73 | + |
| 74 | + MockConfigDb.set_config_db(test_data["config_db"]) |
| 75 | + self.caclmgrd.ControlPlaneAclManager.get_namespace_mgmt_ip = mock.MagicMock() |
| 76 | + self.caclmgrd.ControlPlaneAclManager.get_namespace_mgmt_ipv6 = mock.MagicMock() |
| 77 | + self.caclmgrd.ControlPlaneAclManager.generate_block_ip2me_traffic_iptables_commands = mock.MagicMock(return_value=[]) |
| 78 | + self.caclmgrd.ControlPlaneAclManager.get_chain_list = mock.MagicMock(return_value=["INPUT", "FORWARD", "OUTPUT"]) |
| 79 | + self.caclmgrd.ControlPlaneAclManager.get_chassis_midplane_interface_ip = mock.MagicMock(return_value='') |
| 80 | + caclmgrd_daemon = self.caclmgrd.ControlPlaneAclManager("caclmgrd") |
| 81 | + |
| 82 | + self.assertFalse(caclmgrd_daemon.RedfishAllowed, |
| 83 | + "Seed at __init__ should set RedfishAllowed=False when FEATURE.redfish.state=disabled") |
| 84 | + |
| 85 | + iptables_rules_ret, _ = caclmgrd_daemon.get_acl_rules_and_translate_to_iptables_commands('', MockConfigDb()) |
| 86 | + |
| 87 | + port_443_rules = [r for r in iptables_rules_ret if "--dport" in r and "443" in r] |
| 88 | + self.assertEqual(port_443_rules, test_data["return"], |
| 89 | + "REDFISH ACL should be skipped when RedfishAllowed=False, " |
| 90 | + "but found tcp/443 rules: {}".format(port_443_rules)) |
| 91 | + |
| 92 | + @patchfs |
| 93 | + def test_handle_redfish_feature_events(self, fs): |
| 94 | + """ |
| 95 | + Drive handle_redfish_feature_events (the FEATURE-table subscription |
| 96 | + handler) directly, covering every branch: enable transition, disable |
| 97 | + transition, non-redfish event ignored, and same-state no-op. |
| 98 | + """ |
| 99 | + if not os.path.exists(DBCONFIG_PATH): |
| 100 | + fs.create_file(DBCONFIG_PATH) |
| 101 | + |
| 102 | + MockConfigDb.set_config_db({ |
| 103 | + "DEVICE_METADATA": {"localhost": {}}, |
| 104 | + "FEATURE": {"redfish": {"state": "disabled"}}, |
| 105 | + }) |
| 106 | + self.caclmgrd.ControlPlaneAclManager.get_namespace_mgmt_ip = mock.MagicMock() |
| 107 | + self.caclmgrd.ControlPlaneAclManager.get_namespace_mgmt_ipv6 = mock.MagicMock() |
| 108 | + self.caclmgrd.ControlPlaneAclManager.generate_block_ip2me_traffic_iptables_commands = mock.MagicMock(return_value=[]) |
| 109 | + self.caclmgrd.ControlPlaneAclManager.get_chain_list = mock.MagicMock(return_value=["INPUT", "FORWARD", "OUTPUT"]) |
| 110 | + self.caclmgrd.ControlPlaneAclManager.get_chassis_midplane_interface_ip = mock.MagicMock(return_value='') |
| 111 | + caclmgrd_daemon = self.caclmgrd.ControlPlaneAclManager("caclmgrd") |
| 112 | + self.assertFalse(caclmgrd_daemon.RedfishAllowed) |
| 113 | + |
| 114 | + def sub_with(events): |
| 115 | + sub = mock.MagicMock() |
| 116 | + sub.pop.side_effect = events + [("", None, None)] |
| 117 | + return sub |
| 118 | + |
| 119 | + # enable: flag flips True and namespace queued for re-walk |
| 120 | + notif = set() |
| 121 | + caclmgrd_daemon.handle_redfish_feature_events( |
| 122 | + sub_with([("redfish", "SET", (("state", "enabled"),))]), "", notif) |
| 123 | + self.assertTrue(caclmgrd_daemon.RedfishAllowed) |
| 124 | + self.assertIn("", notif) |
| 125 | + |
| 126 | + # disable: flag flips False and namespace queued |
| 127 | + notif = set() |
| 128 | + caclmgrd_daemon.handle_redfish_feature_events( |
| 129 | + sub_with([("redfish", "SET", (("state", "disabled"),))]), "", notif) |
| 130 | + self.assertFalse(caclmgrd_daemon.RedfishAllowed) |
| 131 | + self.assertIn("", notif) |
| 132 | + |
| 133 | + # non-redfish FEATURE event: ignored, nothing queued |
| 134 | + notif = set() |
| 135 | + caclmgrd_daemon.handle_redfish_feature_events( |
| 136 | + sub_with([("snmp", "SET", (("state", "enabled"),))]), "", notif) |
| 137 | + self.assertFalse(caclmgrd_daemon.RedfishAllowed) |
| 138 | + self.assertEqual(notif, set()) |
| 139 | + |
| 140 | + # same-state event (already disabled): no-op, nothing queued |
| 141 | + notif = set() |
| 142 | + caclmgrd_daemon.handle_redfish_feature_events( |
| 143 | + sub_with([("redfish", "SET", (("state", "disabled"),))]), "", notif) |
| 144 | + self.assertFalse(caclmgrd_daemon.RedfishAllowed) |
| 145 | + self.assertEqual(notif, set()) |
0 commit comments