|
|
|
|
@ -12,9 +12,152 @@ from virsh_panel.shell_contracts import WG_ALLOWED_IPS_SH
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _loader_from_script(script):
|
|
|
|
|
def _loader_from_script(script, *, observe=False):
|
|
|
|
|
start = script.index('VPC_STATE_DIR="/etc/virt-cloud/vpc-dataplane"')
|
|
|
|
|
return script[start:script.index('vpc_state_load || exit 74', start)]
|
|
|
|
|
end = 'FABRIC_NFT_SNAPSHOT=' if observe else 'vpc_state_load || exit 74'
|
|
|
|
|
return script[start:script.index(end, start)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _two_vpc_plans():
|
|
|
|
|
first = _plan()
|
|
|
|
|
payload = first.vpc.canonical()
|
|
|
|
|
payload['vpc_id'] = 'vpc-second-network'
|
|
|
|
|
payload['operation_id'] = 'vpc-second-network-op-0001'
|
|
|
|
|
payload['cidr'] = '10.45.0.0/24'
|
|
|
|
|
for subnet in payload['subnets']:
|
|
|
|
|
subnet['cidr'] = subnet['cidr'].replace('10.44.', '10.45.')
|
|
|
|
|
for member in payload['members']:
|
|
|
|
|
member['private_ip_cidr'] = member['private_ip_cidr'].replace(
|
|
|
|
|
'10.44.', '10.45.')
|
|
|
|
|
payload['fabric_peers'] = [{
|
|
|
|
|
'host_id': first.peers[0].host_id,
|
|
|
|
|
'public_key': first.peers[0].public_key,
|
|
|
|
|
'overlay_ip': first.peers[0].overlay_cidr,
|
|
|
|
|
'tenant_allowed_ips': ['10.45.0.11/32'],
|
|
|
|
|
}]
|
|
|
|
|
second = dataplane_module.parse_vpc_dataplane_plan(
|
|
|
|
|
payload, first.host_id, fabric_plan_digest=first.fabric_plan_digest)
|
|
|
|
|
return first, second
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _state_text(plan, *, cidr_after_routes=False):
|
|
|
|
|
text = dataplane_module._state(plan, dataplane_module._routes(plan))
|
|
|
|
|
if cidr_after_routes:
|
|
|
|
|
# State membership must be checked after parsing the complete record.
|
|
|
|
|
# A route can precede its CIDR without inheriting a previous file's CIDR.
|
|
|
|
|
lines = text.splitlines()
|
|
|
|
|
cidr = next(line for line in lines if line.startswith('cidr|'))
|
|
|
|
|
lines.remove(cidr)
|
|
|
|
|
lines.append(cidr)
|
|
|
|
|
text = '\n'.join(lines) + '\n'
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _run_multi_vpc_loader(tmp_path, plan, texts, mode):
|
|
|
|
|
state_dir = tmp_path / 'states'
|
|
|
|
|
state_dir.mkdir(mode=0o700)
|
|
|
|
|
# The production loader sorts valid token filenames through the shell glob.
|
|
|
|
|
# Fixed token names let callers exchange the two real rendered records and
|
|
|
|
|
# prove that acceptance does not depend on which VPC is read first.
|
|
|
|
|
for index, text in enumerate(texts, 1):
|
|
|
|
|
state_file = state_dir / f'{index:011x}.state'
|
|
|
|
|
state_file.write_text(text)
|
|
|
|
|
state_file.chmod(0o600)
|
|
|
|
|
observe = mode == 'observe'
|
|
|
|
|
script = (dataplane_module.render_observe_script(plan) if observe
|
|
|
|
|
else dataplane_module.render_apply_script(plan))
|
|
|
|
|
harness = '\n'.join((
|
|
|
|
|
'set -eu',
|
|
|
|
|
_loader_from_script(script, observe=observe),
|
|
|
|
|
'VPC_STATE_DIR=' + shlex.quote(str(state_dir)),
|
|
|
|
|
'VPC_PEER_FILE=' + shlex.quote(str(tmp_path / 'peers')),
|
|
|
|
|
'VPC_ROUTE_FILE=' + shlex.quote(str(tmp_path / 'routes')),
|
|
|
|
|
'VPC_TENANT_FILE=' + shlex.quote(str(tmp_path / 'tenants')),
|
|
|
|
|
'FIREWALL_MODE=native_nftables',
|
|
|
|
|
r'''
|
|
|
|
|
vc_fixture_stat() {
|
|
|
|
|
[ "$1" = -c ] || return 9
|
|
|
|
|
case "$2" in
|
|
|
|
|
'%u:%a')
|
|
|
|
|
if [ "$3" = "$VPC_STATE_DIR" ]; then printf '0:700\n'
|
|
|
|
|
elif [ "${3%/*}" = "$VPC_STATE_DIR" ] && [ -f "$3" ]; then
|
|
|
|
|
printf '0:600\n'
|
|
|
|
|
else return 9; fi ;;
|
|
|
|
|
'%s') [ "${3%/*}" = "$VPC_STATE_DIR" ] && [ -f "$3" ] || return 9
|
|
|
|
|
wc -c < "$3" | tr -d ' ' ;;
|
|
|
|
|
*) return 9 ;;
|
|
|
|
|
esac
|
|
|
|
|
}
|
|
|
|
|
STAT=vc_fixture_stat
|
|
|
|
|
vpc_state_load
|
|
|
|
|
printf 'state=%s\n' "$VPC_STATE_STATUS"
|
|
|
|
|
'''.strip(),
|
|
|
|
|
))
|
|
|
|
|
return subprocess.run(['/bin/sh', '-c', harness], text=True,
|
|
|
|
|
capture_output=True, check=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('mode', ['apply', 'observe'])
|
|
|
|
|
@pytest.mark.parametrize('reverse', [False, True])
|
|
|
|
|
@pytest.mark.parametrize('cidr_after_routes', [False, True])
|
|
|
|
|
def test_multi_vpc_state_load_keeps_each_route_in_its_own_cidr(
|
|
|
|
|
tmp_path, mode, reverse, cidr_after_routes):
|
|
|
|
|
first, second = _two_vpc_plans()
|
|
|
|
|
texts = [_state_text(plan, cidr_after_routes=cidr_after_routes)
|
|
|
|
|
for plan in (first, second)]
|
|
|
|
|
result = _run_multi_vpc_loader(
|
|
|
|
|
tmp_path, first, texts[::-1] if reverse else texts, mode)
|
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
|
|
|
assert result.stdout == 'state=owned\n'
|
|
|
|
|
expected_routes = []
|
|
|
|
|
for plan, prefix in ((first, '10.44.0'), (second, '10.45.0')):
|
|
|
|
|
table = plan.resources.route_table
|
|
|
|
|
expected_routes.extend([
|
|
|
|
|
f'{prefix}.10/32|{plan.resources.bridge}|{table}|-|100',
|
|
|
|
|
f'{prefix}.11/32|wg-fabric0|{table}|-|100',
|
|
|
|
|
])
|
|
|
|
|
assert sorted((tmp_path / 'routes').read_text().splitlines()) == sorted(
|
|
|
|
|
expected_routes)
|
|
|
|
|
assert sorted((tmp_path / 'tenants').read_text().splitlines()) == [
|
|
|
|
|
'10.44.0.10/32', '10.44.0.11/32',
|
|
|
|
|
'10.45.0.10/32', '10.45.0.11/32',
|
|
|
|
|
]
|
|
|
|
|
key = first.peers[0].public_key
|
|
|
|
|
assert sorted((tmp_path / 'peers').read_text().splitlines()) == [
|
|
|
|
|
key + '|10.44.0.11/32', key + '|10.45.0.11/32',
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('mode', ['apply', 'observe'])
|
|
|
|
|
@pytest.mark.parametrize('reverse', [False, True])
|
|
|
|
|
@pytest.mark.parametrize('cidr_after_routes', [False, True])
|
|
|
|
|
@pytest.mark.parametrize('foreign_prefix', ['10.45.0.99/32', '10.46.0.99/32'])
|
|
|
|
|
def test_multi_vpc_state_load_rejects_routes_outside_the_owning_record(
|
|
|
|
|
tmp_path, mode, reverse, cidr_after_routes, foreign_prefix):
|
|
|
|
|
first, second = _two_vpc_plans()
|
|
|
|
|
bad = _state_text(first, cidr_after_routes=cidr_after_routes).replace(
|
|
|
|
|
'route|10.44.0.11/32|', 'route|' + foreign_prefix + '|')
|
|
|
|
|
texts = [bad, _state_text(second)]
|
|
|
|
|
result = _run_multi_vpc_loader(
|
|
|
|
|
tmp_path, first, texts[::-1] if reverse else texts, mode)
|
|
|
|
|
assert result.returncode == (1 if mode == 'observe' else 71), result.stderr
|
|
|
|
|
assert 'state=owned' not in result.stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('mode', ['apply', 'observe'])
|
|
|
|
|
@pytest.mark.parametrize('reverse', [False, True])
|
|
|
|
|
def test_multi_vpc_state_load_rejects_duplicate_peer_ips_across_records(
|
|
|
|
|
tmp_path, mode, reverse):
|
|
|
|
|
first, second = _two_vpc_plans()
|
|
|
|
|
key = first.peers[0].public_key
|
|
|
|
|
duplicate = _state_text(second).replace(
|
|
|
|
|
'peer|' + second.peers[0].host_id + '|' + key + '|10.45.0.11/32',
|
|
|
|
|
'peer|' + second.peers[0].host_id + '|' + key + '|10.44.0.11/32')
|
|
|
|
|
texts = [_state_text(first), duplicate]
|
|
|
|
|
result = _run_multi_vpc_loader(
|
|
|
|
|
tmp_path, first, texts[::-1] if reverse else texts, mode)
|
|
|
|
|
assert result.returncode == (1 if mode == 'observe' else 71), result.stderr
|
|
|
|
|
assert 'state=owned' not in result.stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('peer_mode', ['none', 'valid', 'duplicate_ip', 'public_ip'])
|
|
|
|
|
|