-
Notifications
You must be signed in to change notification settings - Fork 728
Expand file tree
/
Copy pathmacsec.py
More file actions
228 lines (187 loc) · 7.87 KB
/
Copy pathmacsec.py
File metadata and controls
228 lines (187 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""
Generic helper functions for MACsec testing.
This module provides reusable utility functions for MACsec-related tests,
including port configuration, secure channel/association management, and verification.
"""
from swsscommon import swsscommon
from dvslib.dvs_database import DVSDatabase
from test_macsec import WPASupplicantMock
class TestMacsecHelper:
"""Helper class for MACsec-related test operations."""
@staticmethod
def enable_macsec_on_port(dvs, port_name, with_secure_channels=True):
"""
Enable MACsec on a port with optional secure channels and associations.
Args:
dvs: Docker Virtual Switch instance
port_name: Port name to enable MACsec on
with_secure_channels: If True, create Secure Channels and Associations with static keys
Returns:
WPASupplicantMock: The WPA supplicant mock instance
"""
wpa = WPASupplicantMock(dvs)
wpa.init_macsec_port(port_name)
# Configure MACsec port with protection and encryption enabled
wpa.config_macsec_port(port_name, {
"enable": True,
"enable_protect": True,
"enable_encrypt": True,
"send_sci": True,
})
# If requested, create Secure Channels and Associations with static keys
if with_secure_channels:
local_mac_address = "00-15-5D-78-FF-C1"
peer_mac_address = "00-15-5D-78-FF-C2"
macsec_port_identifier = 1
an = 0 # Association Number
sak = "0" * 32 # SAK: 128-bit key (32 hex chars)
auth_key = "0" * 32 # Auth key: 128-bit key (32 hex chars)
packet_number = 1
ssci = 1 # Short SCI
salt = "0" * 24 # Salt for XPN cipher suites
# Create Transmit Secure Channel (local) - MUST come first!
wpa.create_transmit_sc(
port_name,
local_mac_address,
macsec_port_identifier)
# Create Receive Secure Channel (from peer)
wpa.create_receive_sc(
port_name,
peer_mac_address,
macsec_port_identifier)
# Create Receive Secure Association with static keys
wpa.create_receive_sa(
port_name,
peer_mac_address,
macsec_port_identifier,
an,
sak,
auth_key,
packet_number,
ssci,
salt)
# Create Transmit Secure Association with static keys
wpa.create_transmit_sa(
port_name,
local_mac_address,
macsec_port_identifier,
an,
sak,
auth_key,
packet_number,
ssci,
salt)
# Enable Receive SA
wpa.set_enable_receive_sa(
port_name,
peer_mac_address,
macsec_port_identifier,
an,
True)
# Enable MACsec control
wpa.set_macsec_control(port_name, True)
# Enable Transmit SA
wpa.set_enable_transmit_sa(
port_name,
local_mac_address,
macsec_port_identifier,
an,
True)
return wpa
@staticmethod
def cleanup_macsec(dvs, port_name):
"""
Cleanup MACsec configuration on a port to prevent test pollution.
Args:
dvs: Docker Virtual Switch instance
port_name: Port name to cleanup
"""
try:
wpa = WPASupplicantMock(dvs)
app_db = dvs.get_app_db()
# Disable MACsec control first
wpa.set_macsec_control(port_name, False)
# Delete all SAs for this port (must delete before SCs)
for table in ["MACSEC_EGRESS_SA_TABLE", "MACSEC_INGRESS_SA_TABLE"]:
for key in app_db.get_keys(table):
if key.startswith(f"{port_name}:"):
app_db.delete_entry(table, key)
# Delete all SCs for this port
for table in ["MACSEC_EGRESS_SC_TABLE", "MACSEC_INGRESS_SC_TABLE"]:
for key in app_db.get_keys(table):
if key.startswith(f"{port_name}:"):
app_db.delete_entry(table, key)
# Finally delete the MACsec port entry
wpa.deinit_macsec_port(port_name)
except Exception as e:
print(f"Cleanup encountered error: {e}")
@staticmethod
def verify_macsec_for_port_in_gb_asic_db(dvs, port_name, should_exist=True):
"""
Verify MACsec objects for a specific port exist (or don't exist) in GB_ASIC_DB.
This method checks if the specified port's MACsec configuration is present
in GB_ASIC_DB by mapping port name to line-side OID via GB_COUNTERS_DB and
checking if any MACSEC_PORT entry references that port OID.
Args:
dvs: Docker Virtual Switch instance
port_name: Name of the port to check (e.g., "Ethernet4")
should_exist: True if objects should exist, False otherwise
Returns:
bool: True if verification passes
"""
gb_asic_db = DVSDatabase(swsscommon.GB_ASIC_DB, dvs.redis_sock)
gb_counters_db = DVSDatabase(swsscommon.GB_COUNTERS_DB, dvs.redis_sock)
# Get port's line-side OID from GB_COUNTERS_DB
# Gearbox ports are stored with "_line" suffix for line-side port
port_map = gb_counters_db.get_entry("COUNTERS_PORT_NAME_MAP", "")
line_port_key = f"{port_name}_line"
expected_port_oid = port_map.get(line_port_key)
if not expected_port_oid:
# Port not found in GB_COUNTERS_DB (not a gearbox port)
return not should_exist
# Check if any MACSEC_PORT entry references this port OID
macsec_port_keys = gb_asic_db.get_keys("ASIC_STATE:SAI_OBJECT_TYPE_MACSEC_PORT")
port_found = any(
gb_asic_db.get_entry("ASIC_STATE:SAI_OBJECT_TYPE_MACSEC_PORT", key).get(
"SAI_MACSEC_PORT_ATTR_PORT_ID"
) == expected_port_oid
for key in macsec_port_keys
)
if should_exist:
return port_found
else:
return not port_found
@staticmethod
def verify_macsec_for_port_in_asic_db(dvs, port_name, should_exist=True):
"""
Verify MACsec objects for a specific port exist (or don't exist) in ASIC_DB (NPU).
This method checks if the specified port's MACsec configuration is present
in ASIC_DB by mapping port name to OID via COUNTERS_DB and checking if any
MACSEC_PORT entry references that port OID.
Args:
dvs: Docker Virtual Switch instance
port_name: Name of the port to check (e.g., "Ethernet0")
should_exist: True if objects should exist, False otherwise
Returns:
bool: True if verification passes
"""
asic_db = dvs.get_asic_db()
counters_db = dvs.get_counters_db()
# Get port OID from COUNTERS_DB port name map
port_map = counters_db.get_entry("COUNTERS_PORT_NAME_MAP", "")
expected_port_oid = port_map.get(port_name)
if not expected_port_oid:
# Port not found in COUNTERS_DB
return not should_exist
# Check if any MACSEC_PORT entry references this port OID
macsec_port_keys = asic_db.get_keys("ASIC_STATE:SAI_OBJECT_TYPE_MACSEC_PORT")
port_found = any(
asic_db.get_entry("ASIC_STATE:SAI_OBJECT_TYPE_MACSEC_PORT", key).get(
"SAI_MACSEC_PORT_ATTR_PORT_ID"
) == expected_port_oid
for key in macsec_port_keys
)
if should_exist:
return port_found
else:
return not port_found