summaryrefslogtreecommitdiff
path: root/definate/dns_generator.py
blob: bb17c711b3cb569d2ba2befc7154f61209a17d58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python
#
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Generator for DNS based network definitions."""

__author__ = 'msu@google.com (Martin Suess)'


import logging
import socket

from third_party import ipaddr
import generator


class DnsGeneratorError(Exception):
  """Exception to use when DnsGenerator fails."""


class DnsGenerator(generator.Generator):
  """Generator implementation for network definitions based on DNS."""

  SUPPORTED_TYPES = ['A', 'AAAA']

  def GenerateDefinition(self, config, unused_global_config):
    """Generates a list of all nodes in a network definition.

    This method basically processes all the configuration which is
    hierarchically below "networks" in the "definitions" section in the
    configuration file to generate a list of all nodes in that definition.

    Args:
      config: YAML configuration structure (dictionaries, lists and strings)
        representing the "networks" section in "definitions" of the
        configuration file.
      unused_global_config: YAML configuration structure (dictionaries, lists
        and strings) representing the "global" section of the configuration
        file.

    Returns:
      Tuples of IPNetwork objects and string comments representing all the nodes
      in one definition.

    Raises:
      DefinateConfigError: The configuration is not well formed.
      DnsGeneratorError: There is a problem generating the output.
    """
    nodes = []
    yaml_structure = {
        'names': ['str'],
        'types': ['str'],
        }
    for network in config:
      self._yaml_validator.CheckConfiguration(network, yaml_structure)
      for typ in network['types']:
        if typ not in self.SUPPORTED_TYPES:
          raise DnsGeneratorError('Unsupported DNS type found: %s' % typ)
      for name in network['names']:
        try:
          addr_list = socket.getaddrinfo(name, None)
        except socket.gaierror:
          raise DnsGeneratorError('Hostname not found: %s' % name)
        for family, _, _, _, sockaddr in addr_list:
          ip_addr = None
          if family == socket.AF_INET and 'A' in network['types']:
            # sockaddr = (address, port)
            ip_addr = ipaddr.IPv4Network(sockaddr[0])
          elif family == socket.AF_INET6 and 'AAAA' in network['types']:
            # sockaddr = (address, port, flow info, scope id)
            ip_addr = ipaddr.IPv6Network(sockaddr[0])
          else:
            logging.debug('Skipping unknown AF \'%d\' for: %s', family, name)
          if ip_addr:
            nodes.append((ip_addr, name))
    return nodes