Ejemplo n.º 1
0
 def test_write_config_writes_config_if_no_existing_file(self):
     # If DNSConfig is created with overwrite=False, the config file
     # will be written if no config file exists.
     target_dir = patch_dns_config_path(self)
     dnsconfig = DNSConfig()
     dnsconfig.write_config(overwrite=False)
     self.assertThat(os.path.join(target_dir, MAAS_NAMED_CONF_NAME),
                     FileExists())
Ejemplo n.º 2
0
 def handle(self, *args, **options):
     no_clobber = options.get('no_clobber')
     set_up_rndc()
     set_up_options_conf(overwrite=not no_clobber)
     config = DNSConfig()
     config.write_config(overwrite=not no_clobber,
                         zone_names=(),
                         reverse_zone_names=())
Ejemplo n.º 3
0
 def handle(self, *args, **options):
     no_clobber = options.get('no_clobber')
     setup_rndc()
     upstream_dns = Config.objects.get_config("upstream_dns")
     set_up_options_conf(
         overwrite=not no_clobber, upstream_dns=upstream_dns)
     config = DNSConfig()
     config.write_config(
         overwrite=not no_clobber, zone_names=(), reverse_zone_names=())
Ejemplo n.º 4
0
 def test_write_config_writes_config_if_no_existing_file(self):
     # If DNSConfig is created with overwrite=False, the config file
     # will be written if no config file exists.
     target_dir = self.make_dir()
     self.patch(DNSConfig, 'target_dir', target_dir)
     dnsconfig = DNSConfig()
     dnsconfig.write_config(overwrite=False)
     self.assertThat(
         os.path.join(target_dir, MAAS_NAMED_CONF_NAME),
         FileExists())
Ejemplo n.º 5
0
 def test_get_include_snippet_returns_snippet(self):
     target_dir = self.make_dir()
     self.patch(DNSConfig, 'target_dir', target_dir)
     dnsconfig = DNSConfig()
     snippet = dnsconfig.get_include_snippet()
     self.assertThat(
         snippet,
         MatchesAll(
             Not(StartsWith('\n')),
             EndsWith('\n'),
             Contains(target_dir),
             Contains('include "%s"' % dnsconfig.target_path)))
Ejemplo n.º 6
0
 def test_write_config_skips_writing_if_overwrite_false(self):
     # If DNSConfig is created with overwrite=False, it won't
     # overwrite an existing config file.
     target_dir = patch_dns_config_path(self)
     random_content = factory.make_string()
     factory.make_file(location=target_dir,
                       name=MAAS_NAMED_CONF_NAME,
                       contents=random_content)
     dnsconfig = DNSConfig()
     dnsconfig.write_config(overwrite=False)
     self.assertThat(os.path.join(target_dir, MAAS_NAMED_CONF_NAME),
                     FileContains(random_content))
Ejemplo n.º 7
0
def run(args, stdout=sys.stdout, stderr=sys.stderr):
    """Setup MAAS DNS configuration.

    :param args: Parsed output of the arguments added in `add_arguments()`.
    :param stdout: Standard output stream to write to.
    :param stderr: Standard error stream to write to.
    """
    set_up_rndc()
    set_up_options_conf(overwrite=not args.no_clobber)
    config = DNSConfig()
    config.write_config(overwrite=not args.no_clobber,
                        zone_names=(),
                        reverse_zone_names=())
Ejemplo n.º 8
0
def write_dns_config(zones=(), callback=None, **kwargs):
    """Write out the DNS configuration file.

    :param zones: List of zones to include as part of the main
        config.
    :type zones: list of :class:`DNSZoneData`
    :param callback: Callback subtask.
    :type callback: callable
    :param **kwargs: Keyword args passed to DNSConfig.write_config()
    """
    dns_config = DNSConfig(zones=zones)
    dns_config.write_config(**kwargs)
    if callback is not None:
        callback.delay()
Ejemplo n.º 9
0
 def test_write_config_skips_writing_if_overwrite_false(self):
     # If DNSConfig is created with overwrite=False, it won't
     # overwrite an existing config file.
     target_dir = self.make_dir()
     self.patch(DNSConfig, 'target_dir', target_dir)
     random_content = factory.getRandomString()
     factory.make_file(
         location=target_dir, name=MAAS_NAMED_CONF_NAME,
         contents=random_content)
     dnsconfig = DNSConfig()
     dnsconfig.write_config(overwrite=False)
     self.assertThat(
         os.path.join(target_dir, MAAS_NAMED_CONF_NAME),
         FileContains(random_content))
Ejemplo n.º 10
0
def write_full_dns_config(zones=None, callback=None, **kwargs):
    """Write out the DNS configuration files: the main configuration
    file and the zone files.
    :param zones: List of zones to write.
    :type zones: list of :class:`DNSZoneData`
    :param callback: Callback subtask.
    :type callback: callable
    :param **kwargs: Keyword args passed to DNSConfig.write_config()
    """
    if zones is not None:
        for zone in zones:
            zone.write_config()
    # Write main config file.
    dns_config = DNSConfig(zones=zones)
    dns_config.write_config(**kwargs)
    if callback is not None:
        callback.delay()
Ejemplo n.º 11
0
def bind_write_configuration(zones, trusted_networks, forwarded_zones=None):
    """Write BIND's configuration.

    :param zones: Those zones to include in main config.
    :type zones: Sequence of :py:class:`DomainData`.

    :param trusted_networks: A sequence of CIDR network specifications that
        are permitted to use the DNS server as a forwarder.
    """
    # trusted_networks was formerly specified as a single IP address with
    # netmask. These assertions are here to prevent code that assumes that
    # slipping through.
    assert not isinstance(trusted_networks, (bytes, str))
    assert isinstance(trusted_networks, Sequence)

    dns_config = DNSConfig(zones=zones, forwarded_zones=forwarded_zones)
    dns_config.write_config(trusted_networks=trusted_networks)
Ejemplo n.º 12
0
 def test_write_config_writes_config(self):
     target_dir = patch_dns_config_path(self)
     domain = factory.make_string()
     network = IPNetwork('192.168.0.3/24')
     ip = factory.pick_ip_in_network(network)
     forward_zone = DNSForwardZoneConfig(
         domain, mapping={factory.make_string(): ip})
     reverse_zone = DNSReverseZoneConfig(domain, network=network)
     dnsconfig = DNSConfig((forward_zone, reverse_zone))
     dnsconfig.write_config()
     self.assertThat(
         os.path.join(target_dir, MAAS_NAMED_CONF_NAME),
         FileContains(matcher=ContainsAll([
             'zone.%s' % domain,
             'zone.0.168.192.in-addr.arpa',
             MAAS_NAMED_RNDC_CONF_NAME,
         ])))
Ejemplo n.º 13
0
 def test_DNSConfig_defaults(self):
     dnsconfig = DNSConfig()
     self.assertEqual(
         (
             locate_config(TEMPLATES_DIR, 'named.conf.template'),
             os.path.join(conf.DNS_CONFIG_DIR, MAAS_NAMED_CONF_NAME)
         ),
         (dnsconfig.template_path, dnsconfig.target_path))
Ejemplo n.º 14
0
    def handle(self, *args, **options):
        edit = options.get('edit')
        config_path = options.get('config_path')
        include_snippet = DNSConfig().get_include_snippet()

        if edit is True:
            with open(config_path, "ab") as conf_file:
                conf_file.write(include_snippet)
        else:
            return INCLUDE_SNIPPET_COMMENT + include_snippet
Ejemplo n.º 15
0
 def test_get_include_snippet_returns_snippet(self):
     target_dir = patch_dns_config_path(self)
     snippet = DNSConfig.get_include_snippet()
     self.assertThat(
         snippet,
         MatchesAll(
             Not(StartsWith('\n')), EndsWith('\n'), Contains(target_dir),
             Contains('include "%s/%s"' % (
                 config.get_dns_config_dir(),
                 DNSConfig.target_file_name,
             ))))
Ejemplo n.º 16
0
    def handle(self, *args, **options):
        edit = options.get('edit')
        config_path = options.get('config_path')
        include_snippet = DNSConfig.get_include_snippet()

        if edit is True:
            # XXX: GavinPanella: I've not been able to discover what character
            # set BIND expects for its configuration, so I've gone with a safe
            # choice of ASCII. If we find that this fails we can revisit this
            # and experiment to discover a better choice.
            with open(config_path, "a", encoding="ascii") as conf_file:
                conf_file.write(include_snippet)
        else:
            return INCLUDE_SNIPPET_COMMENT + include_snippet
Ejemplo n.º 17
0
 def test_write_config_writes_config(self):
     target_dir = self.make_dir()
     self.patch(DNSConfig, 'target_dir', target_dir)
     domain = factory.getRandomString()
     network = IPNetwork('192.168.0.3/24')
     ip = factory.getRandomIPInNetwork(network)
     forward_zone = DNSForwardZoneConfig(
         domain, mapping={factory.getRandomString(): ip},
         networks=[network])
     reverse_zone = DNSReverseZoneConfig(
         domain, mapping={factory.getRandomString(): ip},
         network=network)
     dnsconfig = DNSConfig((forward_zone, reverse_zone))
     dnsconfig.write_config()
     self.assertThat(
         os.path.join(target_dir, MAAS_NAMED_CONF_NAME),
         FileContains(
             matcher=ContainsAll(
                 [
                     'zone.%s' % domain,
                     'zone.0.168.192.in-addr.arpa',
                     MAAS_NAMED_RNDC_CONF_NAME,
                 ])))
Ejemplo n.º 18
0
 def test_write_config_with_forwarded_zones(self):
     name = factory.make_name("domain")
     ip = factory.make_ip_address()
     forwarded_zones = [(name, [ip])]
     target_dir = patch_dns_config_path(self)
     DNSConfig(forwarded_zones=forwarded_zones).write_config()
     config_path = os.path.join(target_dir, MAAS_NAMED_CONF_NAME)
     expected_content = dedent(f"""
     zone "{name}" {{
         type forward;
         forward only;
         forwarders {{
             {ip};
         }};
     }};
     """)
     config = read_isc_file(config_path)
     expected = parse_isc_string(expected_content)
     self.assertEqual(expected[f'zone "{name}"'], config[f'zone "{name}"'])
Ejemplo n.º 19
0
def run(args, stdout=sys.stdout, stderr=sys.stderr):
    """Return the named configuration snippet.

    :param args: Parsed output of the arguments added in `add_arguments()`.
    :param stdout: Standard output stream to write to.
    :param stderr: Standard error stream to write to.
    """
    include_snippet = DNSConfig.get_include_snippet()

    if args.edit is True:
        # XXX: GavinPanella: I've not been able to discover what character
        # set BIND expects for its configuration, so I've gone with a safe
        # choice of ASCII. If we find that this fails we can revisit this
        # and experiment to discover a better choice.
        with open(args.config_path, "a", encoding="ascii") as conf_file:
            conf_file.write(include_snippet)
    else:
        stdout.write(INCLUDE_SNIPPET_COMMENT + include_snippet)
        stdout.write("\n")
        stdout.flush()
Ejemplo n.º 20
0
 def test_get_template_retrieves_template(self):
     dnsconfig = DNSConfig()
     template = dnsconfig.get_template()
     self.assertIsInstance(template, tempita.Template)
     self.assertThat(
         dnsconfig.template_path, FileContains(template.content))
Ejemplo n.º 21
0
 def test_render_template(self):
     dnsconfig = DNSConfig()
     random_content = factory.getRandomString()
     template = tempita.Template("{{test}}")
     rendered = dnsconfig.render_template(template, test=random_content)
     self.assertEqual(random_content, rendered)
Ejemplo n.º 22
0
 def test_write_config_makes_config_world_readable(self):
     target_dir = patch_dns_config_path(self)
     DNSConfig().write_config()
     config_file = FilePath(os.path.join(target_dir, MAAS_NAMED_CONF_NAME))
     self.assertTrue(config_file.getPermissions().other.read)
Ejemplo n.º 23
0
 def test_render_template_raises_DNSConfigFail(self):
     dnsconfig = DNSConfig()
     template = tempita.Template("template: {{test}}")
     exception = self.assertRaises(
         DNSConfigFail, dnsconfig.render_template, template)
     self.assertIn("'test' is not defined", unicode(exception))
Ejemplo n.º 24
0
 def test_write_config_errors_if_unexpected_exception(self):
     dnsconfig = DNSConfig()
     exception = IOError(errno.EBUSY, factory.make_string())
     self.patch(config, "atomic_write", Mock(side_effect=exception))
     self.assertRaises(IOError, dnsconfig.write_config)
Ejemplo n.º 25
0
 def test_write_config_DNSConfigDirectoryMissing_if_dir_missing(self):
     dnsconfig = DNSConfig()
     dir_name = patch_dns_config_path(self)
     os.rmdir(dir_name)
     self.assertRaises(DNSConfigDirectoryMissing, dnsconfig.write_config)
Ejemplo n.º 26
0
 def test_write_config_DNSConfigDirectoryMissing_if_dir_missing(self):
     dnsconfig = DNSConfig()
     dir_name = self.make_dir()
     os.rmdir(dir_name)
     self.patch(DNSConfig, 'target_dir', dir_name)
     self.assertRaises(DNSConfigDirectoryMissing, dnsconfig.write_config)
Ejemplo n.º 27
0
 def test_write_config_errors_if_unexpected_exception(self):
     dnsconfig = DNSConfig()
     exception = IOError(errno.EBUSY, factory.getRandomString())
     self.patch(
         DNSConfig, 'inner_write_config', Mock(side_effect=exception))
     self.assertRaises(IOError, dnsconfig.write_config)