Beispiel #1
0
def test_create_resolver_with_default_arguments():
    resolver = dnsq.create_resolver()

    assert isinstance(resolver, dns.resolver.Resolver)
    assert isinstance(resolver.nameservers, list)
    assert resolver.nameservers != []
    assert isinstance(resolver.domain, dns.name.Name)
Beispiel #2
0
def test_create_resolver_with_custom_nameservers_argument():
    resolver = None
    resolver = dnsq.create_resolver(nameservers=['1.2.3.4', '2.3.4.5'])

    assert isinstance(resolver, dns.resolver.Resolver)
    assert isinstance(resolver.domain, dns.name.Name)
    assert resolver.domain.to_text() != ''
    assert resolver.nameservers == ['1.2.3.4', '2.3.4.5']
    assert resolver.lifetime == 20.0
    assert resolver.timeout == 10.0
Beispiel #3
0
def test_ns_type_nameserver_domain_long_arguments():
    assert_cli(
        'dnsq --type ns --nameserver {nameserver} --domain {domain} --timeout {timeout}'
        .format(nameserver=DEFAULT_NAMESERVER,
                domain=DEFAULT_DOMAIN,
                timeout=DEFAULT_TIMEOUT),
        expected='ns1.foo-domain.com. ns2.foo-domain.com.',
        rc=0,
        stdout=True,
        stderr=True)

    resolver = dnsq.create_resolver(DEFAULT_DOMAIN, DEFAULT_NAMESERVER)
    assert dnsq.ns_records(resolver=resolver, domain=DEFAULT_DOMAIN)
Beispiel #4
0
def test_create_resolver_with_custom_search_should_invoke_get_resolver_domain_type():
    resolver = None
    domain = 'example.com'

    def mocked_result(*args, **kwargs):
        return dns.name.from_text(domain)

    # we mock get_resolver_domain_type to return a custom mock each time get_resolver_domain_type is called
    with mock.patch('dnsq.get_resolver_domain_type', autospec=True, side_effect=mocked_result) as get_resolver_domain_type_mock:
        resolver = dnsq.create_resolver(search=domain)

    assert isinstance(resolver, dns.resolver.Resolver)
    get_resolver_domain_type_mock.assert_called_once_with(domain=domain)
    assert isinstance(resolver.domain, dns.name.Name)
    assert resolver.search == [dns.name.from_text(domain)]
    assert resolver.lifetime == 20.0
    assert resolver.timeout == 10.0
from tests import conftest

import dns.name
import dnsq
import tasks

SEARCH_DOMAIN = 'foo-domain.com'
NAMESERVERS = ['67.77.255.142']
LIFETIME = 5.0
SOA_SERIAL = '2018070500'
SOA_REFRESH = '28800'
SOA_RETRY = '3600'
SOA_EXPIRE = '604800'
SOA_TTL = '86400'
resolver = dnsq.create_resolver(search=SEARCH_DOMAIN,
                                nameservers=NAMESERVERS,
                                lifetime=LIFETIME)

# before running any of these tests, fire up the test environment if it  is not running
if conftest.is_box_running(box='default') is False:
    tasks.prep(conftest.CONTEXT, pty=True, box='default', skip_if_built=False)


def test_resolver():
    assert resolver.search == [dns.name.from_text(SEARCH_DOMAIN)]
    assert resolver.nameservers == NAMESERVERS


def test_ns_records():
    actual_ns_records = dnsq.ns_records(resolver, domain=SEARCH_DOMAIN)
    expected_ns_records = [
Beispiel #6
0
def create_parser():
    """Create a new `argparse.ArgumentParser`

    Returns:
        `argparse.ArgumentParser`

    """
    parser = argparse.ArgumentParser(prog=PROG, description=DESCRIPTION)
    resolver = dnsq.create_resolver()
    default_domain = resolver.search[0]
    default_nameserver = resolver.nameservers[0]
    default_timeout = dnsq.DEFAULT_LIFETIME
    default_sort_by = 'hostname'

    parser.add_argument('--version',
                        action='version',
                        version=dnsq.release.__version__,
                        help='Display dnsq version')

    parser.add_argument('-t',
                        '--type',
                        choices=['ns', 'soa', 'axfr', 'zone-transfer'],
                        required=False,
                        help='A type of query to make')

    parser.add_argument(
        '-d',
        '--domain',
        default=default_domain,
        required=False,
        help='The domain to query. Default "{}"'.format(default_domain))

    parser.add_argument('-n',
                        '--nameserver',
                        default=default_nameserver,
                        help='The nameserver to query. Default "{}"'.format(
                            default_nameserver))

    parser.add_argument(
        '-q',
        '--query',
        help='Query and filter the zone transfer for a particular record')

    parser.add_argument(
        '--sort-by',
        choices=['hostname', 'ip'],
        required=False,
        help=
        'Only used with QUERY option, allows sorting the results. Default "{}"'
        .format(default_sort_by))

    parser.add_argument(
        '-v',
        '--verbose',
        action='count',
        default=0,
        help='Turn on verbose logging, -v=INFO, -vv=DEBUG',
    )

    parser.add_argument(
        '--timeout',
        action='store',
        required=False,
        type=float,
        default=default_timeout,
        help='The timeout to wait for a response from the nameserver. Default {}'
        .format(default_timeout))

    parser.add_argument(
        '--supports-axfr',
        '--supports-zone-transfer',
        action='store_true',
        default=False,
        required=False,
        help='If domain supports a zone transfer exit 0, otherwise exit 1',
    )

    return parser
Beispiel #7
0
def execute(argv=None):
    """Execute the command line with argv

    Args:
        Argv `list` or `None` - When `None` reads sys.argv[1:]

    """
    parser = create_parser()
    options = parser.parse_args(argv)
    resolver = None

    if options.verbose == 1:
        dnsq.LOGGER.setLevel(logging.INFO)
    elif options.verbose >= 2:
        dnsq.LOGGER.setLevel(logging.DEBUG)

    if options.domain and options.nameserver:
        resolver = dnsq.create_resolver(search=options.domain,
                                        nameservers=options.nameserver,
                                        lifetime=options.timeout)
        options.resolver = resolver

    if options.query:
        regex = re.compile(r'{!s}'.format(options.query))
        dnsq.LOGGER.info(
            'Searching zone transfer for the following query: "{}"'.format(
                regex.pattern))
        err_msg = 'The query option requires the zone transfer capability for domain={} nameserver={}'.format(
            options.domain, options.nameserver)
        assert dnsq.supports_zone_transfer(domain=options.domain,
                                           nameserver=options.nameserver,
                                           lifetime=options.timeout), err_msg

        results = []
        for line in dnsq.zone_transfer(domain=options.domain,
                                       nameserver=options.nameserver,
                                       lifetime=options.timeout):
            for item in line:
                if regex.search(item):
                    hostname, rec_type, alias = (line[0], line[-2], line[-1])
                    if options.sort_by == 'ip':
                        result = ' '.join([alias, rec_type, hostname])
                    elif options.sort_by == 'hostname' or options.sort_by is None:
                        result = ' '.join([hostname, rec_type, alias])
                    results.append(result)

        # sort the results
        if options.sort_by is None or options.sort_by == 'hostname':
            sorted_results = '\n'.join(sorted(results))
            print(sorted_results)
        elif options.sort_by == 'ip':
            sorted_results = '\n'.join(dnsq.sort_ips(results))
            print(sorted_results)

        sys.exit(0)

    if options.supports_axfr:
        result = dnsq.supports_zone_transfer(domain=options.domain,
                                             nameserver=options.nameserver,
                                             lifetime=options.timeout)
        if result:
            sys.exit(0)
        sys.exit(1)

    if options.type == 'ns':
        recs = dnsq.ns_records(resolver, domain=options.domain)
        rec_str = ' '.join(recs)
        print(rec_str)
        sys.exit(0)
    elif options.type == 'soa':
        recs = dnsq.soa_records(resolver, domain=options.domain)
        print('\n'.join([' '.join(rec) for rec in recs]))
        sys.exit(0)
    elif options.type == 'axfr' or options.type == 'zone-transfer':
        err_msg = ('ERR: The domain: "{domain}" via nameserver: "{nameserver}"'
                   'does not support AXFR (zone-transfers)\n').format(
                       domain=options.domain, nameserver=options.nameserver)

        if dnsq.supports_zone_transfer(domain=options.domain,
                                       nameserver=options.nameserver,
                                       lifetime=options.timeout) is False:
            sys.stderr.write(err_msg)
            sys.exit(1)
        for line in dnsq.zone_transfer(domain=options.domain,
                                       nameserver=options.nameserver,
                                       lifetime=options.timeout):
            result = ' '.join(line)
            print(result)
        sys.exit(0)
Beispiel #8
0
import dnsq
import tasks

testinfra_hosts = (
    'ssh://default?ssh_config=ssh-config',
)


# before running any of these tests, fire up the test environment if it  is not running
if conftest.is_box_running(box='default') is False:
    tasks.prep(conftest.CONTEXT, pty=True, box='default', skip_if_built=False)

# create the ssh-config
conftest.refresh_vagrant_ssh_config(box='default')

resolver = dnsq.create_resolver(search='foo-domain.com', nameservers=['67.77.255.142'])


def test_bind9_is_installed(host):
    bind9 = host.package('bind9')

    assert bind9.is_installed
    assert bind9.version == '1:9.10.3.dfsg.P4-12.3+deb9u5'


def test_bind9_config_files_contain_no_syntax_errors(host):
    with host.sudo():
        named_checkconf = host.run('/usr/sbin/named-checkconf')
        named_cz_db = host.run('/usr/sbin/named-checkzone 1.168.192.in-addr.arpa /etc/bind/db.192.168.1')
        named_cz_fwd = host.run('named-checkzone foo-domain.com /etc/bind/db.foo-domain.com')