From 66f8234718f171b7a2e489c62b2f1831c59a83ce Mon Sep 17 00:00:00 2001 From: Catherine Redfield Date: Thu, 15 Feb 2024 15:47:53 -0500 Subject: [PATCH] feature(schema): add networkv2 schema Adds networkv2 schema definition file, removes skipping of schema validation for networkv2, and adds unit tests for same. --- cloudinit/config/schema.py | 26 +- .../schemas/schema-network-config-v2.json | 435 ++++++++++++++++++ tests/unittests/config/test_schema.py | 80 +++- 3 files changed, 523 insertions(+), 18 deletions(-) create mode 100644 cloudinit/config/schemas/schema-network-config-v2.json diff --git a/cloudinit/config/schema.py b/cloudinit/config/schema.py index 54c1708ee4a..80dd38ae34e 100644 --- a/cloudinit/config/schema.py +++ b/cloudinit/config/schema.py @@ -66,6 +66,7 @@ # 3. Add the new version definition to versions.schema.cloud-config.json USERDATA_SCHEMA_FILE = "schema-cloud-config-v1.json" NETWORK_CONFIG_V1_SCHEMA_FILE = "schema-network-config-v1.json" +NETWORK_CONFIG_V2_SCHEMA_FILE = "schema-network-config-v2.json" _YAML_MAP = {True: "true", False: "false", None: "null"} SCHEMA_DOC_TMPL = """ @@ -169,7 +170,9 @@ class SchemaType(Enum): "latest": USERDATA_SCHEMA_FILE, }, SchemaType.NETWORK_CONFIG: { - "latest": NETWORK_CONFIG_V1_SCHEMA_FILE, + "latest": NETWORK_CONFIG_V2_SCHEMA_FILE, + 1: NETWORK_CONFIG_V1_SCHEMA_FILE, + 2: NETWORK_CONFIG_V2_SCHEMA_FILE, }, } @@ -717,15 +720,14 @@ def validate_cloudconfig_schema( NETWORK_CONFIG """ if schema_type == SchemaType.NETWORK_CONFIG: - if network_schema_version(config) == 2: + network_version = network_schema_version(config) + if network_version == 2: if netplan_validate_network_schema( network_config=config, strict=strict, log_details=log_details ): # Schema was validated by netplan return True - # network-config schema version 2 but no netplan. - # TODO(add JSON schema definition for network version 2) - return False + schema = get_schema(schema_type, network_version) if schema is None: schema = get_schema(schema_type) @@ -1125,15 +1127,6 @@ def validate_cloudconfig_file( network_config=cloudconfig, strict=True, annotate=annotate ): return True # schema validation performed by netplan - if network_version != 1: - # Validation requires JSON schema definition in - # cloudinit/config/schemas/schema-network-config-v1.json - print( - "Skipping network-config schema validation." - " No network schema for version:" - f" {network_schema_version(cloudconfig)}" - ) - return False try: if not validate_cloudconfig_schema( cloudconfig, schema=schema, strict=True, log_deprecations=False @@ -1557,13 +1550,14 @@ def get_schema_dir() -> str: return os.path.join(os.path.dirname(os.path.abspath(__file__)), "schemas") -def get_schema(schema_type: SchemaType = SchemaType.CLOUD_CONFIG) -> dict: +def get_schema(schema_type: SchemaType = SchemaType.CLOUD_CONFIG, version: int = None) -> dict: """Return jsonschema for a specific type. Return empty schema when no specific schema file exists. """ + version = version or "latest" schema_file = os.path.join( - get_schema_dir(), SCHEMA_FILES_BY_TYPE[schema_type]["latest"] + get_schema_dir(), SCHEMA_FILES_BY_TYPE[schema_type][version] ) full_schema = None try: diff --git a/cloudinit/config/schemas/schema-network-config-v2.json b/cloudinit/config/schemas/schema-network-config-v2.json new file mode 100644 index 00000000000..95c8e9cf4aa --- /dev/null +++ b/cloudinit/config/schemas/schema-network-config-v2.json @@ -0,0 +1,435 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$defs": { + "renderer": { + "type": "string", + "description": "Use the given networking backend for this definition. Default is networkd.", + "enum": [ + "networkd", + "NetworkManager" + ] + }, + "dhcp-overrides": { + "type": "object", + "description": "DHCP behaviour overrides. Overrides will only have an effect if the corresponding DHCP type is enabled.", + "additionalProperties": false, + "properties": { + "hostname": { + "type": "string", + "description": "Unsupported for dhcp6-overrides when used with the networkd renderer." + }, + "route-metric": { + "type": "string", + "description": "Unsupported for dhcp6-overrides when used with the networkd renderer." + }, + "send-hostname": { + "type": "boolean", + "description": "Unsupported for dhcp6-overrides when used with the networkd renderer." + }, + "use-dns": { + "type": "boolean" + }, + "use-domains": { + "type": "string" + }, + "use-hostname": { + "type": "boolean" + }, + "use-mtu": { + "type": "boolean", + "description": "Unsupported for dhcp6-overrides when used with the networkd renderer." + }, + "use-ntp": { + "type": "boolean" + }, + "use-routes": { + "type": "boolean", + "description": "Unsupported for dhcp6-overrides when used with the networkd renderer." + } + } + }, + "gateway": { + "type": "string", + "description": "Deprecated, see Netplan#default-routes. Set default gateway for IPv4/6, for manual address configuration. This requires setting addresses too. Gateway IPs must be in a form recognised by inet_pton(3)." + }, + "mapping": { + "type": "object", + "properties": { + "renderer": { + "$ref": "#/$defs/renderer" + }, + "dhcp4": { + "type": ["boolean", "string"], + "description": "Enable DHCP for IPv4. Off by default.", + "enum": ["yes", "no", true, false] + }, + "dhcp6": { + "type": ["boolean", "string"], + "description": "Enable DHCP for IPv6. Off by default.", + "enum": ["yes", "no", true, false] + }, + "dhcp4-overrides": { + "$ref": "#/$defs/dhcp-overrides" + }, + "dhcp6-overrides": { + "$ref": "#/$defs/dhcp-overrides" + }, + "addresses": { + "type": "array", + "description": "Add static addresses to the interface in addition to the ones received through DHCP or RA. Each sequence entry is in CIDR notation, i.e., of the form addr/prefixlen. addr is an IPv4 or IPv6 address as recognised by inet_pton(3) and prefixlen the number of bits of the subnet.", + "items": { + "type": "string" + } + }, + "gateway4": { + "$ref": "#/$defs/gateway" + }, + "gateway6": { + "$ref": "#/$defs/gateway" + }, + "mtu": { + "type": "integer", + "description": "The MTU key represents a device’s Maximum Transmission Unit, the largest size packet or frame, specified in octets (eight-bit bytes), that can be sent in a packet- or frame-based network. Specifying mtu is optional." + }, + "nameservers": { + "type": "object", + "additionalProperties": false, + "description": "Set DNS servers and search domains, for manual address configuration. There are two supported fields: addresses: is a list of IPv4 or IPv6 addresses similar to gateway*, and search: is a list of search domains.", + "properties": { + "search": { + "type": "array", + "items": { + "type": "string" + } + }, + "addresses": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "routes": { + "type": "array", + "description": "", + "items": { + "type": "object", + "required": [ + "to", + "via" + ], + "additionalProperties": false, + "properties": { + "to": { + "type": "string" + }, + "via": { + "type": "string" + }, + "metric": { + "type": "integer" + } + } + } + } + } + }, + "mapping_physical": { + "allOf": [ + { + "$ref": "#/$defs/mapping" + } + ], + "properties": { + "match": { + "type": "object", + "description": "This selects a subset of available physical devices by various hardware properties. The following configuration will then apply to all matching devices, as soon as they appear. All specified properties must match.", + "additionalProperties": false, + "properties": { + "name": { + "type": "string", + "description": "Current interface name. Globs are supported, and the primary use case for matching on names, as selecting one fixed name can be more easily achieved with having no match: at all and just using the ID (see above). Note that currently only networkd supports globbing, NetworkManager does not." + }, + "macaddress": { + "type": "string", + "description": "Device’s MAC address in the form xx:xx:xx:xx:xx:xx. Globs are not allowed. Letters must be lowercase." + }, + "driver": { + "type": "string", + "description": "Kernel driver name, corresponding to the DRIVER udev property. Globs are supported. Matching on driver is only supported with networkd." + }, + "set-name": { + "type": "string", + "description": "When matching on unique properties such as path or MAC, or with additional assumptions such as ''there will only ever be one wifi device'', match rules can be written so that they only match one device. Then this property can be used to give that device a more specific/desirable/nicer name than the default from udev’s ifnames. Any additional device that satisfies the match rules will then fail to get renamed and keep the original kernel name (and dmesg will show an error)." + }, + "wakeonlan": { + "type": "boolean", + "description": "Enable wake on LAN. Off by default." + } + } + } + } + }, + "mapping_bond": { + "allOf": [ + { + "$ref": "#/$defs/mapping" + } + ], + "properties": { + "interfaces": { + "type": "array", + "description": "All devices matching this ID list will be added to the bond.", + "items": { + "type": "string" + } + }, + "parameters": { + "type": "object", + "description": "Customisation parameters for special bonding options. Time values are specified in seconds unless otherwise specified." + }, + "mode": { + "type": "string", + "description": "Set the bonding mode used for the interfaces. The default is balance-rr (round robin).", + "enum": [ + "balance-rr", + "active-backup", + "balance-xor", + "broadcast", + "802.3ad", + "balance-tlb", + "balance-alb" + ] + }, + "lacp-rate": { + "type": "string", + "description": "Set the rate at which LACPDUs are transmitted. This is only useful in 802.3ad mode. Possible values are slow (30 seconds, default), and fast (every second).", + "enum": [ + "fast", + "slow" + ] + }, + "mii-monitor-interval": { + "type": "string", + "description": "Specifies the interval for MII monitoring (verifying if an interface of the bond has carrier). The default is 0; which disables MII monitoring." + }, + "min-links": { + "type": "integer", + "description": "The minimum number of links up in a bond to consider the bond interface to be up." + }, + "transmit-hash-policy": { + "type": "string", + "description": "Specifies the transmit hash policy for the selection of slaves. This is only useful in balance-xor, 802.3ad and balance-tlb modes.", + "enum": [ + "layer2", + "layer3+4", + "layer2+3", + "encap2+3", + "encap3+4" + ] + }, + "ad-select": { + "type": "string", + "description": "Set the aggregation selection mode. This option is only used in 802.3ad mode.", + "enum": [ + "stable", + "bandwidth", + "count" + ] + }, + "all-slaves-active": { + "type": "boolean", + "description": "If the bond should drop duplicate frames received on inactive ports, set this option to false. If they should be delivered, set this option to true. The default value is false, and is the desirable behaviour in most situations." + }, + "arp-interval": { + "type": "integer", + "description": "Set the interval value for how frequently ARP link monitoring should happen. The default value is 0, which disables ARP monitoring." + }, + "arp-ip-targets": { + "type": "array", + "description": "IPs of other hosts on the link which should be sent ARP requests in order to validate that a slave is up. This option is only used when arp-interval is set to a value other than 0. At least one IP address must be given for ARP link monitoring to function. Only IPv4 addresses are supported. You can specify up to 16 IP addresses. The default value is an empty list.", + "items": { + "type": "string" + } + }, + "arp-validate": { + "type": "string", + "description": "Configure how ARP replies are to be validated when using ARP link monitoring.", + "enum": [ + "none", + "active", + "backup", + "all" + ] + }, + "arp-all-targets": { + "type": "string", + "description": "Specify whether to use any ARP IP target being up as sufficient for a slave to be considered up; or if all the targets must be up. This is only used for active-backup mode when arp-validate is enabled.", + "enum": [ + "any", + "all" + ] + }, + "up-delay": { + "type": "integer", + "description": "Specify the delay before enabling a link once the link is physically up. The default value is 0." + }, + "down-delay": { + "type": "integer", + "description": "Specify the delay before enabling a link once the link has been lost. The default value is 0." + }, + "fail-over-mac-policy": { + "type": "string", + "description": "Set whether to set all slaves to the same MAC address when adding them to the bond, or how else the system should handle MAC addresses.", + "enum": [ + "none", + "active", + "follow" + ] + }, + "gratuitous-arp": { + "type": "integer", + "description": "Specify how many ARP packets to send after failover. Once a link is up on a new slave, a notification is sent and possibly repeated if this value is set to a number greater than 1. The default value is 1 and valid values are between 1 and 255. This only affects active-backup mode." + }, + "packets-per-slave": { + "type": "integer", + "description": "In balance-rr mode, specifies the number of packets to transmit on a slave before switching to the next. When this value is set to 0, slaves are chosen at random. Allowable values are between 0 and 65535. The default value is 1. This setting is only used in balance-rr mode." + }, + "primary-reselect-policy": { + "type": "string", + "description": "Set the reselection policy for the primary slave. On failure of the active slave, the system will use this policy to decide how the new active slave will be chosen and how recovery will be handled.", + "enum": [ + "always", + "better", + "failure" + ] + }, + "learn-packet-interval": { + "type": "string", + "description": "Specify the interval between sending Learning packets to each slave. The value range is between 1 and 0x7fffffff. The default value is 1. This option only affects balance-tlb and balance-alb modes." + } + } + }, + "mapping_bridge": { + "allOf": [ + { + "$ref": "#/$defs/mapping" + } + ], + "properties": { + "interfaces": { + "type": "array", + "description": "All devices matching this ID list will be added to the bridge.", + "items": { + "type": "string" + } + }, + "parameters": { + "type": "object", + "description": "Customisation parameters for special bridging options. Time values are specified in seconds unless otherwise stated." + }, + "ageing-time": { + "type": "integer", + "description": "Set the period of time to keep a MAC address in the forwarding database after a packet is received." + }, + "priority": { + "type": "integer", + "description": "Set the priority value for the bridge. This value should be a number between 0 and 65535. Lower values mean higher priority. The bridge with the higher priority will be elected as the root bridge." + }, + "forward-delay": { + "type": "integer", + "description": "Specify the period of time the bridge will remain in Listening and Learning states before getting to the Forwarding state. This value should be set in seconds for the systemd backend, and in milliseconds for the NetworkManager backend." + }, + "hello-time": { + "type": "integer", + "description": "Specify the interval between two hello packets being sent out from the root and designated bridges. Hello packets communicate information about the network topology." + }, + "max-age": { + "type": "integer", + "description": "Set the maximum age of a hello packet. If the last hello packet is older than that value, the bridge will attempt to become the root bridge." + }, + "path-cost": { + "type": "integer", + "description": "Set the cost of a path on the bridge. Faster interfaces should have a lower cost. This allows a finer control on the network topology so that the fastest paths are available whenever possible." + }, + "stp": { + "type": "boolean", + "description": "Define whether the bridge should use Spanning Tree Protocol. The default value is “true”, which means that Spanning Tree should be used." + } + } + }, + "mapping_vlan": { + "allOf": [ + { + "$ref": "#/$defs/mapping" + } + ], + "properties": { + "id": { + "type": "integer", + "description": "VLAN ID, a number between 0 and 4094." + }, + "link": { + "type": "string", + "description": "ID of the underlying device definition on which this VLAN gets created." + } + } + }, + "network_config_version2": { + "type": "object", + "additionalProperties": false, + "required": [ + "version" + ], + "properties": { + "version": { + "type": "integer", + "enum": [ + 2 + ] + }, + "ethernets": { + "type": "object", + "additionalProperties": { + "$ref": "#/$defs/mapping_physical" + } + }, + "bonds": { + "type": "object", + "additionalProperties": { + "$ref": "#/$defs/mapping_bond" + } + }, + "bridges": { + "type": "object", + "additionalProperties": { + "$ref": "#/$defs/mapping_bridge" + } + }, + "vlans": { + "type": "object", + "additionalProperties": { + "$ref": "#/$defs/mapping_vlan" + } + } + } + } + }, + "oneOf": [ + { + "$ref": "#/$defs/network_config_version2" + }, + { + "required": [ + "network" + ], + "properties": { + "network": { + "$ref": "#/$defs/network_config_version2" + } + }, + "additionalProperties": false + } + ] +} diff --git a/tests/unittests/config/test_schema.py b/tests/unittests/config/test_schema.py index 8ac04373e49..487273f154a 100644 --- a/tests/unittests/config/test_schema.py +++ b/tests/unittests/config/test_schema.py @@ -2242,9 +2242,85 @@ class TestNetworkSchema: ( pytest.param( {"network": {"config": [], "version": 2}}, + pytest.raises( + SchemaValidationError, + match=re.escape("Additional properties are not allowed ('config' was unexpected)"), + ), + "", + id="net_v2_invalid_config", + ), + pytest.param( + {"network": {"version": 2, "ethernets": {"eno1": {"dhcp4": True}}}}, does_not_raise(), - "Skipping netplan schema validation. No netplan available", - id="net_v2_skipped", + "", + id="net_v2_simple_example", + ), + pytest.param( + { + "network": { + "version": 2, + "ethernets": { + "id0": { + "match": { + "macaddress": "00:11:22:33:44:55", + }, + "wakeonlan": True, + "dhcp4": True, + "addresses": [ + "192.168.14.2/24", + "2001:1::1/64", + ], + "gateway4": "192.168.14.1", + "gateway6": "2001:1::2", + "nameservers": { + "search": ["foo.local", "bar.local"], + "addresses": ["8.8.8.8"], + }, + "routes": [ + { + "to": "192.0.2.0/24", + "via": "11.0.0.1", + "metric": 3, + }, + ] + }, + "lom": { + "match": { + "driver": "ixgbe" + }, + "set-name": "lom1", + "dhcp6": True, + }, + "switchports": { + "match": { + "name": "enp2*" + }, + "mtu": 1280, + }, + }, + "bonds": { + "bond0": { + "interfaces": ["id0", "lom"] + }, + }, + "bridges": { + "br0": { + "interfaces": ["wlp1s0", "switchports"], + "dhcp4": True, + }, + }, + "vlans": { + "en-intra": { + "id": 1, + "link": "id0", + "dhcp4": "yes" + }, + }, + } + }, + does_not_raise(), + "", + id="net_v2_complex_example", ), pytest.param( {"network": {"version": 1}},