Ejemplo n.º 1
0
 def test_adds_connected_multiple_racks_ipv6(self):
     rack1 = factory.make_RackController()
     factory.make_RegionRackRPCConnection(rack1)
     rack2 = factory.make_RackController()
     factory.make_RegionRackRPCConnection(rack2)
     network = factory.make_ipv6_network()
     subnet = factory.make_Subnet(cidr=str(network.cidr))
     static_ip1 = factory.make_StaticIPAddress(
         alloc_type=IPADDRESS_TYPE.AUTO,
         ip=factory.pick_ip_in_Subnet(subnet),
         subnet=subnet,
         interface=rack1.get_boot_interface(),
     )
     static_ip2 = factory.make_StaticIPAddress(
         alloc_type=IPADDRESS_TYPE.AUTO,
         ip=factory.pick_ip_in_Subnet(subnet),
         subnet=subnet,
         interface=rack2.get_boot_interface(),
     )
     domain = get_internal_domain()
     self.assertEqual(get_resource_name_for_subnet(subnet),
                      domain.resources[0].name)
     self.assertThat(
         domain.resources[0].records,
         MatchesSetwise(
             Equals(
                 InternalDomainResourseRecord(rrtype="AAAA",
                                              rrdata=static_ip1.ip)),
             Equals(
                 InternalDomainResourseRecord(rrtype="AAAA",
                                              rrdata=static_ip2.ip)),
         ),
     )
Ejemplo n.º 2
0
 def test_prefers_static_ip_over_dhcp(self):
     rack = factory.make_RackController()
     factory.make_RegionRackRPCConnection(rack)
     nic = rack.get_boot_interface()
     network = factory.make_ipv4_network()
     subnet = factory.make_Subnet(cidr=str(network.cidr))
     static_ip = factory.make_StaticIPAddress(
         alloc_type=IPADDRESS_TYPE.AUTO,
         ip=factory.pick_ip_in_Subnet(subnet),
         subnet=subnet,
         interface=nic,
     )
     factory.make_StaticIPAddress(
         alloc_type=IPADDRESS_TYPE.DHCP,
         ip=factory.pick_ip_in_Subnet(subnet),
         subnet=subnet,
         interface=nic,
     )
     domain = get_internal_domain()
     self.assertThat(domain.resources, HasLength(1))
     self.assertEqual(get_resource_name_for_subnet(subnet),
                      domain.resources[0].name)
     self.assertEqual(
         InternalDomainResourseRecord(rrtype="A", rrdata=static_ip.ip),
         domain.resources[0].records[0],
     )
Ejemplo n.º 3
0
def get_apt_proxy(request, rack_controller=None, node=None):
    """Return the APT proxy for the `rack_controller`."""
    config = Config.objects.get_configs([
        "enable_http_proxy",
        "http_proxy",
        "use_peer_proxy",
        "maas_proxy_port",
        "maas_internal_domain",
        "use_rack_proxy",
    ])
    if config["enable_http_proxy"]:
        http_proxy = config["http_proxy"]
        if http_proxy is not None:
            http_proxy = http_proxy.strip()
        use_peer_proxy = config["use_peer_proxy"]
        if http_proxy and not use_peer_proxy:
            return http_proxy
        else:
            # Ensure the proxy port is the default if not set.
            maas_proxy_port = config["maas_proxy_port"]
            if not maas_proxy_port:
                maas_proxy_port = 8000
            # Use the client requesting the preseed to determine how they
            # should access the APT proxy.
            subnet = None
            remote_ip = get_remote_ip(request)
            if remote_ip is not None:
                subnet = Subnet.objects.get_best_subnet_for_ip(remote_ip)
            use_dns = (subnet is not None and not subnet.dns_servers
                       and subnet.vlan.dhcp_on)
            if config["use_rack_proxy"] and use_dns:
                # Client can use the MAAS proxy on the rack controller with
                # DNS resolution providing better HA.
                return "http://%s.%s:%d/" % (
                    get_resource_name_for_subnet(subnet),
                    config["maas_internal_domain"],
                    maas_proxy_port,
                )
            elif (config["use_rack_proxy"] and node is not None
                  and node.boot_cluster_ip):
                # Client can use the MAAS proxy on the rack controller with
                # IP address, instead of DNS.
                return "http://%s:%d/" % (
                    node.boot_cluster_ip,
                    maas_proxy_port,
                )
            else:
                # Fallback to sending the APT directly to the
                # region controller.
                region_ip = get_default_region_ip(request)
                url = "http://:%d/" % maas_proxy_port
                return compose_URL(
                    url,
                    get_maas_facing_server_host(rack_controller,
                                                default_region_ip=region_ip),
                )
    else:
        return None
Ejemplo n.º 4
0
 def test_dns_update_all_zones_includes_internal_domain(self):
     self.patch(settings, 'DNS_CONNECT', True)
     rack, static = self.create_rack_with_static_ip()
     factory.make_RegionRackRPCConnection(rack)
     dns_update_all_zones()
     resource_name = get_resource_name_for_subnet(static.subnet)
     self.assertDNSMatches(
         resource_name, Config.objects.get_config('maas_internal_domain'),
         static.ip, reverse=False)
Ejemplo n.º 5
0
def get_base_url_for_local_ip(local_ip, internal_domain):
    """Get the base URL for the preseed using the `local_ip`."""
    subnet = Subnet.objects.get_best_subnet_for_ip(local_ip)
    if subnet is not None and not subnet.dns_servers:
        # Use the MAAS internal domain to resolve the IP address of
        # the rack controllers on the subnet.
        return 'http://%s.%s:5248/' % (
            get_resource_name_for_subnet(subnet), internal_domain)
    else:
        # Either no subnet or the subnet has DNS servers defined. In
        # that case fallback to using IP address only.
        return 'http://%s:5248/' % local_ip
Ejemplo n.º 6
0
def get_base_url_for_local_ip(local_ip, internal_domain):
    """Get the base URL for the preseed using the `local_ip`."""
    subnet = Subnet.objects.get_best_subnet_for_ip(local_ip)
    if subnet is not None and not subnet.dns_servers and subnet.vlan.dhcp_on:
        # Use the MAAS internal domain to resolve the IP address of
        # the rack controllers on the subnet.
        return "http://%s.%s:5248/" % (
            get_resource_name_for_subnet(subnet),
            internal_domain,
        )
    else:
        # Either no subnet, the subnet has DNS servers defined, or the VLAN
        # that the subnet belongs to doesn't have DHCP enabled. In
        # that case fallback to using IP address only.
        return "http://%s:5248/" % local_ip
Ejemplo n.º 7
0
 def test__adds_connected_rack_ipv6(self):
     rack = factory.make_RackController()
     factory.make_RegionRackRPCConnection(rack)
     nic = rack.get_boot_interface()
     network = factory.make_ipv6_network()
     subnet = factory.make_Subnet(cidr=str(network.cidr))
     static_ip = factory.make_StaticIPAddress(
         alloc_type=IPADDRESS_TYPE.AUTO,
         ip=factory.pick_ip_in_Subnet(subnet),
         subnet=subnet, interface=nic)
     domain = get_internal_domain()
     self.assertEqual(
         get_resource_name_for_subnet(subnet), domain.resources[0].name)
     self.assertEqual(
         InternalDomainResourseRecord(rrtype='AAAA', rrdata=static_ip.ip),
         domain.resources[0].records[0])
Ejemplo n.º 8
0
 def test__has_enlistment_preseed_url_internal_domain(self):
     rack_controller = factory.make_RackController()
     vlan = factory.make_VLAN(dhcp_on=True, primary_rack=rack_controller)
     subnet = factory.make_Subnet(vlan=vlan)
     subnet.dns_servers = []
     subnet.save()
     local_ip = factory.pick_ip_in_Subnet(subnet)
     remote_ip = factory.make_ip_address()
     factory.make_default_ubuntu_release_bootable()
     observed_config = get_config(
         rack_controller.system_id, local_ip, remote_ip)
     self.assertEqual(
         compose_enlistment_preseed_url(
             base_url='http://%s.%s:5248/' % (
                 get_resource_name_for_subnet(subnet),
                 Config.objects.get_config('maas_internal_domain'))),
         observed_config["preseed_url"])
Ejemplo n.º 9
0
def get_apt_proxy(request, rack_controller=None):
    """Return the APT proxy for the `rack_controller`."""
    config = Config.objects.get_configs([
        'enable_http_proxy', 'http_proxy', 'use_peer_proxy', 'maas_proxy_port',
        'maas_internal_domain', 'use_rack_proxy'
    ])
    if config["enable_http_proxy"]:
        http_proxy = config["http_proxy"]
        if http_proxy is not None:
            http_proxy = http_proxy.strip()
        use_peer_proxy = config["use_peer_proxy"]
        if http_proxy and not use_peer_proxy:
            return http_proxy
        else:
            # Ensure the proxy port is the default if not set.
            maas_proxy_port = config["maas_proxy_port"]
            if not maas_proxy_port:
                maas_proxy_port = 8000
            # Use the client requesting the preseed to determine how they
            # should access the APT proxy.
            subnet = None
            remote_ip = get_remote_ip(request)
            if remote_ip is not None:
                subnet = Subnet.objects.get_best_subnet_for_ip(remote_ip)
            if (config['use_rack_proxy'] and subnet is not None
                    and not subnet.dns_servers):
                # Client can use the MAAS proxy on the rack controller.
                return "http://%s.%s:%d/" % (get_resource_name_for_subnet(
                    subnet), config["maas_internal_domain"], maas_proxy_port)
            else:
                # Client cannot use the MAAS proxy on the rack controller
                # because rack proxy is disabled, the subnet the IP belongs to
                # is unknown or the subnet is using DNS servers that are not
                # MAAS. Fallback to using the old way pre MAAS 2.5.
                region_ip = get_default_region_ip(request)
                url = "http://:%d/" % maas_proxy_port
                return compose_URL(
                    url,
                    get_maas_facing_server_host(rack_controller,
                                                default_region_ip=region_ip))
    else:
        return None
Ejemplo n.º 10
0
 def test_dns_update_all_zones_includes_multiple_racks(self):
     self.patch(settings, "DNS_CONNECT", True)
     rack1, static1 = self.create_rack_with_static_ip()
     factory.make_RegionRackRPCConnection(rack1)
     rack2, static2 = self.create_rack_with_static_ip(subnet=static1.subnet)
     factory.make_RegionRackRPCConnection(rack2)
     dns_update_all_zones()
     resource_name = get_resource_name_for_subnet(static1.subnet)
     self.assertDNSMatches(
         resource_name,
         Config.objects.get_config("maas_internal_domain"),
         static1.ip,
         reverse=False,
     )
     self.assertDNSMatches(
         resource_name,
         Config.objects.get_config("maas_internal_domain"),
         static2.ip,
         reverse=False,
     )
Ejemplo n.º 11
0
 def test_preseed_url_for_known_node_internal_domain(self):
     rack_url = 'http://%s' % factory.make_name('host')
     rack_controller = factory.make_RackController(url=rack_url)
     vlan = factory.make_VLAN(dhcp_on=True, primary_rack=rack_controller)
     subnet = factory.make_Subnet(vlan=vlan)
     subnet.dns_servers = []
     subnet.save()
     local_ip = factory.pick_ip_in_Subnet(subnet)
     remote_ip = factory.make_ip_address()
     self.patch(
         server_address, 'resolve_hostname').return_value = {local_ip}
     node = self.make_node(primary_rack=rack_controller)
     mac = node.get_boot_interface().mac_address
     observed_config = get_config(
         rack_controller.system_id, local_ip, remote_ip, mac=mac)
     self.assertThat(
         observed_config["preseed_url"],
         StartsWith(
             'http://%s.%s:5248' % (
                 get_resource_name_for_subnet(subnet),
                 Config.objects.get_config('maas_internal_domain'))))
Ejemplo n.º 12
0
 def test_returns_valid(self):
     subnet = factory.make_Subnet(cidr=self.cidr)
     self.assertEqual(self.result, get_resource_name_for_subnet(subnet))