Skip to content

Commit

Permalink
parse-nm: handle cloned-mac-address special cases
Browse files Browse the repository at this point in the history
The property "cloned-mac-address" might contain one of these special
values: "preserve", "permanent", "random" and "stable". Currently we
accept only a valid MAC address as value.

This change will leave the cloned-mac-address property in the
networkmanager.passthrough section if it contains one of these values
and will try to validate it otherwise.

Related to LP: #2026230
  • Loading branch information
daniloegea authored and slyon committed Jul 10, 2023
1 parent 6dedb27 commit 0c7a0ea
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/parse-nm.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ keyfile_handle_generic_uint(GKeyFile* kf, const gchar* group, const gchar* key,

static void
keyfile_handle_common(GKeyFile* kf, NetplanNetDefinition* nd, const gchar* group) {
keyfile_handle_generic_str(kf, group, "cloned-mac-address", &nd->set_mac);
keyfile_handle_generic_uint(kf, group, "mtu", &nd->mtubytes, NETPLAN_MTU_UNSPEC);
keyfile_handle_generic_str(kf, group, "mac-address", &nd->match.mac);
if (nd->match.mac)
Expand All @@ -159,6 +158,26 @@ keyfile_handle_bridge_uint(GKeyFile* kf, const gchar* key, NetplanNetDefinition*
}
}

static void
keyfile_handle_cloned_mac_address(GKeyFile *kf, NetplanNetDefinition* nd, const gchar* group)
{
g_autofree gchar* mac = g_key_file_get_string(kf, group, "cloned-mac-address", NULL);

if (!mac) return;

/* If the value of "cloned-mac-address" is one of the below we don't try to
* parse it and leave it in the passthrough section.
*/
if ( g_strcmp0(mac, "preserve")
&& g_strcmp0(mac, "permanent")
&& g_strcmp0(mac, "random")
&& g_strcmp0(mac, "stable")
) {
nd->set_mac = g_strdup(mac);
_kf_clear_key(kf, group, "cloned-mac-address");
}
}

static void
parse_addresses(GKeyFile* kf, const gchar* group, GArray** ip_arr)
{
Expand Down Expand Up @@ -782,6 +801,7 @@ netplan_parser_load_keyfile(NetplanParser* npp, const char* filename, GError** e
}

keyfile_handle_common(kf, nd, "ethernet");
keyfile_handle_cloned_mac_address(kf, nd, "ethernet");
}

/* Wifis */
Expand All @@ -794,6 +814,7 @@ netplan_parser_load_keyfile(NetplanParser* npp, const char* filename, GError** e
}

keyfile_handle_common(kf, nd, "wifi");
keyfile_handle_cloned_mac_address(kf, nd, "wifi");
}

/* Cleanup some implicit keys */
Expand Down
71 changes: 71 additions & 0 deletions tests/parser/test_keyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1739,3 +1739,74 @@ def test_invalid_tunnel_mode(self):
method=auto\n'''.format(UUID), expect_fail=True)

self.assertIn('missing or invalid \'mode\' property for tunnel', out)

def test_keyfile_wifi_random_cloned_mac_address(self):
self.generate_from_keyfile('''[connection]
type=wifi
uuid={}
id=myid with spaces
interface-name=eth0
[wifi]
ssid=SOME-SSID
mode=infrastructure
cloned-mac-address=random
[wifi-security]
key-mgmt=ieee8021x
[ipv4]
method=auto
dns-search='''.format(UUID))
self.assert_netplan({UUID: '''network:
version: 2
wifis:
NM-{}:
renderer: NetworkManager
match:
name: "eth0"
dhcp4: true
access-points:
"SOME-SSID":
auth:
key-management: "802.1x"
networkmanager:
uuid: "{}"
name: "myid with spaces"
passthrough:
wifi.cloned-mac-address: "random"
ipv4.dns-search: ""
networkmanager:
uuid: "{}"
name: "myid with spaces"
'''.format(UUID, UUID, UUID)})

def test_keyfile_ethernet_random_cloned_mac_address(self):
self.generate_from_keyfile('''[connection]
type=ethernet
uuid={}
id=myid with spaces
interface-name=eth0
[ethernet]
cloned-mac-address=random
[ipv4]
method=auto
dns-search='''.format(UUID))
self.assert_netplan({UUID: '''network:
version: 2
ethernets:
NM-{}:
renderer: NetworkManager
match:
name: "eth0"
dhcp4: true
wakeonlan: true
networkmanager:
uuid: "{}"
name: "myid with spaces"
passthrough:
ethernet.cloned-mac-address: "random"
ipv4.dns-search: ""
'''.format(UUID, UUID)})

0 comments on commit 0c7a0ea

Please sign in to comment.