"""Whether a sender's address is Docker's (#6003): the container's own networks read from /proc/net/route.""" import ipaddress from plugins.syslog.network import on_own_network, subnets_from # A swarm task on the ingress network (10.0.0.0/24) and an overlay (10.0.5.0/24), # with its default route through the bridge gateway 172.18.0.1. ROUTE = """Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT eth1 00000000 010012AC 0003 0 0 0 00000000 0 0 0 eth0 0000000A 00000000 0001 0 0 0 00FFFFFF 0 0 0 eth2 0005000A 00000000 0001 0 0 0 00FFFFFF 0 0 0 eth1 000012AC 00000000 0001 0 0 0 0000FFFF 0 0 0 eth9 nonsense """ def test_the_directly_connected_networks_are_read_the_default_route_left_out(): assert subnets_from(ROUTE) == [ipaddress.IPv4Network("10.0.0.0/24"), ipaddress.IPv4Network("10.0.5.0/24"), ipaddress.IPv4Network("172.18.0.0/16")] def test_a_sender_on_a_container_network_is_flagged_and_a_lan_one_is_not(): nets = subnets_from(ROUTE) assert on_own_network("10.0.0.2", nets) and on_own_network("172.18.0.1", nets) assert not on_own_network("192.168.1.20", nets) assert not on_own_network("not an address", nets) and not on_own_network("10.0.0.2", [])