zone_config.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (C) 2012 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. from isc.acl.dns import REQUEST_LOADER
  16. import isc.dns
  17. from isc.datasrc import DataSourceClient
  18. # Constants representing zone types
  19. ZONE_NOTFOUND = -1 # Zone isn't found in find_zone()
  20. ZONE_PRIMARY = 0 # Primary zone
  21. ZONE_SECONDARY = 1 # Secondary zone
  22. class ZoneConfig:
  23. '''A temporary helper class to encapsulate zone related configuration.
  24. Its find_zone method will search the conceptual configuration for a
  25. given zone, and return a tuple of zone type (primary or secondary) and
  26. the client object to access the data source stroing the zone.
  27. It's very likely that details of zone related configurations like this
  28. will change in near future, so the main purpose of this class is to
  29. provide an independent interface for the main DDNS session module
  30. until the details are fixed.
  31. '''
  32. def __init__(self, secondaries, datasrc_class, datasrc_client, acl_map={}):
  33. '''Constructor.
  34. Parameters:
  35. - secondaries: a list of 2-element tuples. Each element is a pair
  36. of isc.dns.Name and isc.dns.RRClass, and identifies a single
  37. secondary zone.
  38. - datasrc_class: isc.dns.RRClass object. Specifies the RR class
  39. of datasrc_client.
  40. - datasrc_client: isc.dns.DataSourceClient object. A data source
  41. class for the RR class of datasrc_class. It's expected to contain
  42. a zone that is eventually updated in the ddns package.
  43. - acl_map: a dictionary that maps a tuple of
  44. (isc.dns.Name, isc.dns.RRClass) to an isc.dns.dns.RequestACL
  45. object. It defines an ACL to be applied to the zone defined
  46. by the tuple. If unspecified, or the map is empty, the default
  47. ACL will be applied to all zones, which is to reject any requests.
  48. '''
  49. self.__secondaries = set()
  50. for (zname, zclass) in secondaries:
  51. self.__secondaries.add((zname, zclass))
  52. self.__datasrc_class = datasrc_class
  53. self.__datasrc_client = datasrc_client
  54. self.__default_acl = REQUEST_LOADER.load([{"action": "REJECT"}])
  55. self.__acl_map = acl_map
  56. def find_zone(self, zone_name, zone_class):
  57. '''Return the type and accessor client object for given zone.'''
  58. if self.__datasrc_class == zone_class and \
  59. self.__datasrc_client.find_zone(zone_name)[0] == \
  60. DataSourceClient.SUCCESS:
  61. if (zone_name, zone_class) in self.__secondaries:
  62. return ZONE_SECONDARY, None
  63. return ZONE_PRIMARY, self.__datasrc_client
  64. return ZONE_NOTFOUND, None
  65. def get_update_acl(self, zone_name, zone_class):
  66. '''Return the update ACL for the given zone.
  67. This method searches the internally stored ACL map to see if
  68. there's an ACL to be applied to the given zone. If found, that
  69. ACL will be returned; otherwise the default ACL (see the constructor
  70. description) will be returned.
  71. Parameters:
  72. zone_name (isc.dns.Name): The zone name.
  73. zone_class (isc.dns.RRClass): The zone class.
  74. '''
  75. acl = self.__acl_map.get((zone_name, zone_class))
  76. if acl is not None:
  77. return acl
  78. return self.__default_acl
  79. def set_update_acl_map(self, new_map):
  80. '''Set a new ACL map.
  81. This replaces any stored ACL map, either at construction or
  82. by a previous call to this method, with the given new one.
  83. Parameter:
  84. new_map: same as the acl_map parameter of the constructor.
  85. '''
  86. self.__acl_map = new_map