|
|
|
|
@ -2,8 +2,10 @@
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from dataclasses import replace
|
|
|
|
|
import json
|
|
|
|
|
import shlex
|
|
|
|
|
import subprocess
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from test_vpc_nft_dataplane import _plan
|
|
|
|
|
@ -53,7 +55,21 @@ def _state_text(plan, *, cidr_after_routes=False):
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _run_multi_vpc_loader(tmp_path, plan, texts, mode):
|
|
|
|
|
def _with_global_peer_tenants(plan, *extra_ips):
|
|
|
|
|
"""Use the same trusted peer closure that the coordinator supplies."""
|
|
|
|
|
payload = plan.vpc.canonical()
|
|
|
|
|
tenants = tuple(sorted(set(plan.peers[0].tenant_allowed_ips) | set(extra_ips)))
|
|
|
|
|
peer = plan.peers[0]
|
|
|
|
|
payload['fabric_peers'] = [{
|
|
|
|
|
'host_id': peer.host_id, 'public_key': peer.public_key,
|
|
|
|
|
'overlay_ip': peer.overlay_cidr, 'tenant_allowed_ips': list(tenants),
|
|
|
|
|
}]
|
|
|
|
|
return dataplane_module.parse_vpc_dataplane_plan(
|
|
|
|
|
payload, plan.host_id, fabric_plan_digest=plan.fabric_plan_digest,
|
|
|
|
|
trusted_tenant_allowed_ips={peer.host_id: tenants})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _run_multi_vpc_loader(tmp_path, plan, texts, mode, *, extra_script=''):
|
|
|
|
|
state_dir = tmp_path / 'states'
|
|
|
|
|
state_dir.mkdir(mode=0o700)
|
|
|
|
|
# The production loader sorts valid token filenames through the shell glob.
|
|
|
|
|
@ -92,6 +108,7 @@ STAT=vc_fixture_stat
|
|
|
|
|
vpc_state_load
|
|
|
|
|
printf 'state=%s\n' "$VPC_STATE_STATUS"
|
|
|
|
|
'''.strip(),
|
|
|
|
|
extra_script,
|
|
|
|
|
))
|
|
|
|
|
return subprocess.run(['/bin/sh', '-c', harness], text=True,
|
|
|
|
|
capture_output=True, check=False)
|
|
|
|
|
@ -128,6 +145,212 @@ def test_multi_vpc_state_load_keeps_each_route_in_its_own_cidr(
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('mode', ['apply', 'observe'])
|
|
|
|
|
@pytest.mark.parametrize('records', ['single', 'forward', 'reverse'])
|
|
|
|
|
@pytest.mark.parametrize('cidr_after_routes', [False, True])
|
|
|
|
|
def test_global_peer_tenants_join_routes_once_in_the_fabric_address_universe(
|
|
|
|
|
tmp_path, mode, records, cidr_after_routes):
|
|
|
|
|
first, second = _two_vpc_plans()
|
|
|
|
|
extra = '10.46.0.21/32'
|
|
|
|
|
first = _with_global_peer_tenants(first, extra)
|
|
|
|
|
plans = [first] if records == 'single' else [first, second]
|
|
|
|
|
if records == 'reverse':
|
|
|
|
|
plans.reverse()
|
|
|
|
|
texts = [_state_text(plan, cidr_after_routes=cidr_after_routes)
|
|
|
|
|
for plan in plans]
|
|
|
|
|
result = _run_multi_vpc_loader(tmp_path, first, texts, mode)
|
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
|
|
|
assert result.stdout == 'state=owned\n'
|
|
|
|
|
route_ips = {row['destination_cidr'] for plan in plans
|
|
|
|
|
for row in dataplane_module._routes(plan)}
|
|
|
|
|
expected = sorted(route_ips | {extra})
|
|
|
|
|
actual = (tmp_path / 'tenants').read_text().splitlines()
|
|
|
|
|
assert sorted(actual) == expected
|
|
|
|
|
assert len(actual) == len(set(actual))
|
|
|
|
|
expected_peers = sorted(
|
|
|
|
|
peer.public_key + '|' + address
|
|
|
|
|
for plan in plans for peer in plan.peers
|
|
|
|
|
for address in peer.tenant_allowed_ips)
|
|
|
|
|
assert sorted((tmp_path / 'peers').read_text().splitlines()) == expected_peers
|
|
|
|
|
# A global peer authorization contributes no route in this VPC's table.
|
|
|
|
|
# It must not weaken the independent per-record route/CIDR contract.
|
|
|
|
|
routes = (tmp_path / 'routes').read_text().splitlines()
|
|
|
|
|
assert {row.split('|', 1)[0] for row in routes} == route_ips
|
|
|
|
|
assert extra not in route_ips
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('mode', ['apply', 'observe'])
|
|
|
|
|
@pytest.mark.parametrize('cidr_after_routes', [False, True])
|
|
|
|
|
def test_peer_only_state_keeps_its_global_tenants_without_local_routes(
|
|
|
|
|
tmp_path, mode, cidr_after_routes):
|
|
|
|
|
carrier = _with_global_peer_tenants(_plan(), '10.46.0.21/32')
|
|
|
|
|
# The loader supports a retained peer-only record after local members and
|
|
|
|
|
# routes disappear. Render that record with the real state producer;
|
|
|
|
|
# extract the loader from an ordinary participating-host transaction.
|
|
|
|
|
peer_only = replace(carrier, members=())
|
|
|
|
|
text = _state_text(peer_only, cidr_after_routes=cidr_after_routes)
|
|
|
|
|
assert '\nroute|' not in text and '\nlocal_neighbor|' not in text
|
|
|
|
|
result = _run_multi_vpc_loader(tmp_path, carrier, [text], mode)
|
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
|
|
|
assert result.stdout == 'state=owned\n'
|
|
|
|
|
assert (tmp_path / 'routes').read_text() == ''
|
|
|
|
|
actual = (tmp_path / 'tenants').read_text().splitlines()
|
|
|
|
|
assert sorted(actual) == ['10.44.0.11/32', '10.46.0.21/32']
|
|
|
|
|
assert len(actual) == len(set(actual))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('mode', ['apply', 'observe'])
|
|
|
|
|
@pytest.mark.parametrize('reverse', [False, True])
|
|
|
|
|
@pytest.mark.parametrize('cidr_after_routes', [False, True])
|
|
|
|
|
def test_global_tenant_union_still_rejects_duplicate_peer_ownership(
|
|
|
|
|
tmp_path, mode, reverse, cidr_after_routes):
|
|
|
|
|
first, second = _two_vpc_plans()
|
|
|
|
|
first = _with_global_peer_tenants(first, '10.46.0.21/32')
|
|
|
|
|
second = _with_global_peer_tenants(second, '10.46.0.21/32')
|
|
|
|
|
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 == (1 if mode == 'observe' else 71), result.stderr
|
|
|
|
|
assert 'state=owned' not in result.stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('mode', ['apply', 'observe'])
|
|
|
|
|
@pytest.mark.parametrize('cidr_after_routes', [False, True])
|
|
|
|
|
def test_global_peer_authorization_cannot_authorize_a_foreign_state_route(
|
|
|
|
|
tmp_path, mode, cidr_after_routes):
|
|
|
|
|
extra = '10.46.0.21/32'
|
|
|
|
|
plan = _with_global_peer_tenants(_plan(), extra)
|
|
|
|
|
text = _state_text(plan, cidr_after_routes=cidr_after_routes).replace(
|
|
|
|
|
'route|10.44.0.11/32|', 'route|' + extra + '|')
|
|
|
|
|
result = _run_multi_vpc_loader(tmp_path, plan, [text], 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'])
|
|
|
|
|
def test_tenant_union_deduplication_cannot_hide_duplicate_route_ownership(
|
|
|
|
|
tmp_path, mode):
|
|
|
|
|
plan = _with_global_peer_tenants(_plan(), '10.46.0.21/32')
|
|
|
|
|
text = _state_text(plan)
|
|
|
|
|
# A remote route bypasses the local-neighbor/direct-route check. Its
|
|
|
|
|
# duplicate must still fail before tenant-set de-duplication can hide it.
|
|
|
|
|
text += next(line for line in text.splitlines()
|
|
|
|
|
if line.startswith('route|') and '|wg-fabric0|' in line) + '\n'
|
|
|
|
|
result = _run_multi_vpc_loader(tmp_path, plan, [text], mode)
|
|
|
|
|
assert result.returncode == (1 if mode == 'observe' else 71), result.stderr
|
|
|
|
|
assert 'state=owned' not in result.stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _stateful_tenant_nft(tmp_path, mutation):
|
|
|
|
|
tool = tmp_path / 'nft'
|
|
|
|
|
tool.write_text(
|
|
|
|
|
'#!' + sys.executable + '\nROOT=' + repr(str(tmp_path))
|
|
|
|
|
+ '\nMUTATION=' + repr(mutation) + '\n' + r'''
|
|
|
|
|
import ipaddress,json,re,sys
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
root=Path(ROOT);args=sys.argv[1:]
|
|
|
|
|
if args==['-s','list','table','inet','virt_cloud_wg_fabric']:
|
|
|
|
|
print('fixture-owned-fabric-table')
|
|
|
|
|
elif args[:2]==['-c','-f'] or args[:1]==['-f']:
|
|
|
|
|
batch=Path(args[-1]).read_text()
|
|
|
|
|
lines=batch.splitlines()
|
|
|
|
|
assert lines[0]=='flush set inet virt_cloud_wg_fabric vpc_tenant_ips',lines
|
|
|
|
|
match=re.fullmatch(r'add element inet virt_cloud_wg_fabric vpc_tenant_ips \{ (.*) \}',lines[1])
|
|
|
|
|
assert len(lines)==2 and match,batch
|
|
|
|
|
values=[str(ipaddress.ip_network(value.strip(),strict=True))
|
|
|
|
|
for value in match[1].split(',')]
|
|
|
|
|
assert all(value.endswith('/32') for value in values),values
|
|
|
|
|
assert len(values)==len(set(values)),values
|
|
|
|
|
(root/('checked.batch' if args[0]=='-c' else 'applied.batch')).write_text(batch)
|
|
|
|
|
if args[0]!='-c':
|
|
|
|
|
if MUTATION=='missing':values.remove('10.46.0.21/32')
|
|
|
|
|
elif MUTATION=='extra':values.append('10.47.0.99/32')
|
|
|
|
|
(root/'live-set.json').write_text(json.dumps(sorted(values)))
|
|
|
|
|
elif args==['list','set','inet','virt_cloud_wg_fabric','vpc_tenant_ips']:
|
|
|
|
|
values=json.loads((root/'live-set.json').read_text())
|
|
|
|
|
print('table inet virt_cloud_wg_fabric {\n set vpc_tenant_ips {\n type ipv4_addr\n flags interval')
|
|
|
|
|
print(' elements = { '+', '.join(value.removesuffix('/32') for value in values)+' }')
|
|
|
|
|
print(' }\n}')
|
|
|
|
|
else:raise SystemExit(90)
|
|
|
|
|
''')
|
|
|
|
|
tool.chmod(0o700)
|
|
|
|
|
return tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('mode', ['apply', 'observe'])
|
|
|
|
|
@pytest.mark.parametrize('mutation', ['none', 'missing', 'extra'])
|
|
|
|
|
def test_global_tenant_refresh_and_fabric_observer_share_the_exact_universe(
|
|
|
|
|
tmp_path, mode, mutation):
|
|
|
|
|
from test_fabric import FakeManager, KEY1, KEY2, plan as fabric_plan
|
|
|
|
|
from virsh_panel.fabric import FabricHostAdapter, _VPC_NFT_OBSERVE_FRAGMENT
|
|
|
|
|
|
|
|
|
|
base = _plan()
|
|
|
|
|
carrier = replace(base, peers=(replace(base.peers[0], public_key=KEY2),))
|
|
|
|
|
carrier = _with_global_peer_tenants(carrier, '10.46.0.21/32')
|
|
|
|
|
nft = _stateful_tenant_nft(tmp_path, mutation)
|
|
|
|
|
script = '\n'.join((
|
|
|
|
|
'NFT=' + shlex.quote(str(nft)),
|
|
|
|
|
'EXPECTED_DIGEST=' + shlex.quote(carrier.fabric_plan_digest),
|
|
|
|
|
# Ownership is independent of the tenant-universe contract exercised
|
|
|
|
|
# here. Both exact-state reads must see this same owned table.
|
|
|
|
|
'nft_plan_owned() { [ "$1" = fixture-owned-fabric-table ] && '
|
|
|
|
|
'[ "$2" = "$EXPECTED_DIGEST" ]; }',
|
|
|
|
|
'vpc_state_refresh_nft',
|
|
|
|
|
'if vpc_state_nft_set_matches; then printf "nft_matches=yes\\n"; '
|
|
|
|
|
'else printf "nft_matches=no\\n"; fi',
|
|
|
|
|
_VPC_NFT_OBSERVE_FRAGMENT,
|
|
|
|
|
))
|
|
|
|
|
result = _run_multi_vpc_loader(
|
|
|
|
|
tmp_path, carrier, [_state_text(carrier)], mode, extra_script=script)
|
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
|
|
|
wanted = ['10.44.0.10/32', '10.44.0.11/32', '10.46.0.21/32']
|
|
|
|
|
assert (tmp_path / 'checked.batch').read_text() == (
|
|
|
|
|
tmp_path / 'applied.batch').read_text()
|
|
|
|
|
matches = mutation == 'none'
|
|
|
|
|
assert ('nft_matches=yes\n' in result.stdout) == matches
|
|
|
|
|
|
|
|
|
|
# Use the real loader output and real nft observation fragment for every
|
|
|
|
|
# VPC marker. The existing manager supplies only the unrelated healthy
|
|
|
|
|
# Fabric background; the adapter's strict union comparison is unchanged.
|
|
|
|
|
peer_rows = (tmp_path / 'peers').read_text().splitlines()
|
|
|
|
|
tenant_ips = (tmp_path / 'tenants').read_text().splitlines()
|
|
|
|
|
nft_markers = '\n'.join(line for line in result.stdout.splitlines()
|
|
|
|
|
if line.startswith('__VC_FABRIC_VPC_NFT_')) + '\n'
|
|
|
|
|
current = fabric_plan()
|
|
|
|
|
manager = FakeManager(KEY1)
|
|
|
|
|
original_exec = manager._exec
|
|
|
|
|
|
|
|
|
|
def readback(argv, *, timeout, input_text=None):
|
|
|
|
|
background = original_exec(argv, timeout=timeout, input_text=input_text)
|
|
|
|
|
output = '\n'.join(line for line in background.stdout.splitlines()
|
|
|
|
|
if not line.startswith('__VC_FABRIC_VPC_')) + '\n'
|
|
|
|
|
peer = current.nodes[1]
|
|
|
|
|
marker = f'__VC_FABRIC_PEER__={KEY2}|{peer.endpoint}|{peer.allowed_ip}'
|
|
|
|
|
extras = ','.join(row.split('|', 1)[1] for row in peer_rows)
|
|
|
|
|
output = output.replace(marker + '\n', marker + ',' + extras + '\n')
|
|
|
|
|
output += '__VC_FABRIC_VPC_STATE__=owned\n'
|
|
|
|
|
output += ''.join('__VC_FABRIC_VPC_TENANT__=' + row + '\n'
|
|
|
|
|
for row in peer_rows)
|
|
|
|
|
output += ''.join('__VC_FABRIC_VPC_TENANT_IP__=' + ip + '\n'
|
|
|
|
|
for ip in tenant_ips)
|
|
|
|
|
output += nft_markers
|
|
|
|
|
return background.__class__(background.args, background.returncode,
|
|
|
|
|
output, background.stderr)
|
|
|
|
|
|
|
|
|
|
manager._exec = readback
|
|
|
|
|
observed = FabricHostAdapter(manager).observe(
|
|
|
|
|
current, current.nodes[0], {'host-a': KEY1, 'host-b': KEY2})
|
|
|
|
|
assert observed['observed']['peer_allowed_exact'] is True
|
|
|
|
|
assert observed['observed']['vpc_nft_state'] == 'owned'
|
|
|
|
|
assert observed['observed']['vpc_nft_exact'] is matches
|
|
|
|
|
assert observed['config_ok'] is matches
|
|
|
|
|
assert 'add element inet virt_cloud_wg_fabric vpc_tenant_ips { ' + (
|
|
|
|
|
', '.join(wanted)) + ' }' in (tmp_path / 'applied.batch').read_text()
|
|
|
|
|
assert observed['observed']['vpc_nft_ips'] == json.loads(
|
|
|
|
|
(tmp_path / 'live-set.json').read_text())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('mode', ['apply', 'observe'])
|
|
|
|
|
@pytest.mark.parametrize('reverse', [False, True])
|
|
|
|
|
@pytest.mark.parametrize('cidr_after_routes', [False, True])
|
|
|
|
|
|