processors - hook in to modify zones to hide stuff from octoDNS, modify things, add special records, ...pull/734/head
| @ -0,0 +1,6 @@ | |||
| # | |||
| # | |||
| # | |||
| from __future__ import absolute_import, division, print_function, \ | |||
| unicode_literals | |||
| @ -0,0 +1,30 @@ | |||
| # | |||
| # | |||
| # | |||
| from __future__ import absolute_import, division, print_function, \ | |||
| unicode_literals | |||
| from ..zone import Zone | |||
| class BaseProcessor(object): | |||
| def __init__(self, name): | |||
| self.name = name | |||
| def _clone_zone(self, zone): | |||
| return Zone(zone.name, sub_zones=zone.sub_zones) | |||
| def process_source_zone(self, zone, sources): | |||
| # sources may be empty, as will be the case for aliased zones | |||
| return zone | |||
| def process_target_zone(self, zone, target): | |||
| return zone | |||
| def process_plan(self, plan, sources, target): | |||
| # plan may be None if no changes were detected up until now, the | |||
| # process may still create a plan. | |||
| # sources may be empty, as will be the case for aliased zones | |||
| return plan | |||
| @ -0,0 +1,44 @@ | |||
| # | |||
| # | |||
| # | |||
| from __future__ import absolute_import, division, print_function, \ | |||
| unicode_literals | |||
| from .base import BaseProcessor | |||
| class TypeAllowlistFilter(BaseProcessor): | |||
| def __init__(self, name, allowlist): | |||
| super(TypeAllowlistFilter, self).__init__(name) | |||
| self.allowlist = set(allowlist) | |||
| def _process(self, zone, *args, **kwargs): | |||
| ret = self._clone_zone(zone) | |||
| for record in zone.records: | |||
| if record._type in self.allowlist: | |||
| ret.add_record(record) | |||
| return ret | |||
| process_source_zone = _process | |||
| process_target_zone = _process | |||
| class TypeRejectlistFilter(BaseProcessor): | |||
| def __init__(self, name, rejectlist): | |||
| super(TypeRejectlistFilter, self).__init__(name) | |||
| self.rejectlist = set(rejectlist) | |||
| def _process(self, zone, *args, **kwargs): | |||
| ret = self._clone_zone(zone) | |||
| for record in zone.records: | |||
| if record._type not in self.rejectlist: | |||
| ret.add_record(record) | |||
| return ret | |||
| process_source_zone = _process | |||
| process_target_zone = _process | |||
| @ -0,0 +1,103 @@ | |||
| # | |||
| # | |||
| # | |||
| from __future__ import absolute_import, division, print_function, \ | |||
| unicode_literals | |||
| from collections import defaultdict | |||
| from ..provider.plan import Plan | |||
| from ..record import Record | |||
| from .base import BaseProcessor | |||
| # Mark anything octoDNS is managing that way it can know it's safe to modify or | |||
| # delete. We'll take ownership of existing records that we're told to manage | |||
| # and thus "own" them going forward. | |||
| class OwnershipProcessor(BaseProcessor): | |||
| def __init__(self, name, txt_name='_owner', txt_value='*octodns*'): | |||
| super(OwnershipProcessor, self).__init__(name) | |||
| self.txt_name = txt_name | |||
| self.txt_value = txt_value | |||
| self._txt_values = [txt_value] | |||
| def process_source_zone(self, zone, *args, **kwargs): | |||
| ret = self._clone_zone(zone) | |||
| for record in zone.records: | |||
| # Always copy over the source records | |||
| ret.add_record(record) | |||
| # Then create and add an ownership TXT for each of them | |||
| record_name = record.name.replace('*', '_wildcard') | |||
| if record.name: | |||
| name = '{}.{}.{}'.format(self.txt_name, record._type, | |||
| record_name) | |||
| else: | |||
| name = '{}.{}'.format(self.txt_name, record._type) | |||
| txt = Record.new(zone, name, { | |||
| 'type': 'TXT', | |||
| 'ttl': 60, | |||
| 'value': self.txt_value, | |||
| }) | |||
| ret.add_record(txt) | |||
| return ret | |||
| def _is_ownership(self, record): | |||
| return record._type == 'TXT' and \ | |||
| record.name.startswith(self.txt_name) \ | |||
| and record.values == self._txt_values | |||
| def process_plan(self, plan, *args, **kwargs): | |||
| if not plan: | |||
| # If we don't have any change there's nothing to do | |||
| return plan | |||
| # First find all the ownership info | |||
| owned = defaultdict(dict) | |||
| # We need to look for ownership in both the desired and existing | |||
| # states, many things will show up in both, but that's fine. | |||
| for record in list(plan.existing.records) + list(plan.desired.records): | |||
| if self._is_ownership(record): | |||
| pieces = record.name.split('.', 2) | |||
| if len(pieces) > 2: | |||
| _, _type, name = pieces | |||
| name = name.replace('_wildcard', '*') | |||
| else: | |||
| _type = pieces[1] | |||
| name = '' | |||
| owned[name][_type.upper()] = True | |||
| # Cases: | |||
| # - Configured in source | |||
| # - We'll fully CRU/manage it adding ownership TXT, | |||
| # thanks to process_source_zone, if needed | |||
| # - Not in source | |||
| # - Has an ownership TXT - delete it & the ownership TXT | |||
| # - Does not have an ownership TXT - don't delete it | |||
| # - Special records like octodns-meta | |||
| # - Should be left alone and should not have ownerthis TXTs | |||
| filtered_changes = [] | |||
| for change in plan.changes: | |||
| record = change.record | |||
| if not self._is_ownership(record) and \ | |||
| record._type not in owned[record.name] and \ | |||
| record.name != 'octodns-meta': | |||
| # It's not an ownership TXT, it's not owned, and it's not | |||
| # special we're going to ignore it | |||
| continue | |||
| # We own this record or owned it up until now so whatever the | |||
| # change is we should do | |||
| filtered_changes.append(change) | |||
| if plan.changes != filtered_changes: | |||
| return Plan(plan.existing, plan.desired, filtered_changes, | |||
| plan.exists, plan.update_pcent_threshold, | |||
| plan.delete_pcent_threshold) | |||
| return plan | |||
| @ -0,0 +1,23 @@ | |||
| providers: | |||
| config: | |||
| class: octodns.provider.yaml.YamlProvider | |||
| directory: tests/config | |||
| dump: | |||
| class: octodns.provider.yaml.YamlProvider | |||
| directory: env/YAML_TMP_DIR | |||
| geo: | |||
| class: helpers.GeoProvider | |||
| nosshfp: | |||
| class: helpers.NoSshFpProvider | |||
| processors: | |||
| no-class: {} | |||
| zones: | |||
| unit.tests.: | |||
| processors: | |||
| - noop | |||
| sources: | |||
| - in | |||
| targets: | |||
| - dump | |||
| @ -0,0 +1,25 @@ | |||
| providers: | |||
| config: | |||
| class: octodns.provider.yaml.YamlProvider | |||
| directory: tests/config | |||
| dump: | |||
| class: octodns.provider.yaml.YamlProvider | |||
| directory: env/YAML_TMP_DIR | |||
| geo: | |||
| class: helpers.GeoProvider | |||
| nosshfp: | |||
| class: helpers.NoSshFpProvider | |||
| processors: | |||
| # valid class, but it wants a param and we're not passing it | |||
| wants-config: | |||
| class: helpers.WantsConfigProcessor | |||
| zones: | |||
| unit.tests.: | |||
| processors: | |||
| - noop | |||
| sources: | |||
| - in | |||
| targets: | |||
| - dump | |||
| @ -0,0 +1,33 @@ | |||
| providers: | |||
| config: | |||
| class: octodns.provider.yaml.YamlProvider | |||
| directory: tests/config | |||
| dump: | |||
| class: octodns.provider.yaml.YamlProvider | |||
| directory: env/YAML_TMP_DIR | |||
| geo: | |||
| class: helpers.GeoProvider | |||
| nosshfp: | |||
| class: helpers.NoSshFpProvider | |||
| processors: | |||
| # Just testing config so any processor will do | |||
| noop: | |||
| class: octodns.processor.base.BaseProcessor | |||
| zones: | |||
| unit.tests.: | |||
| processors: | |||
| - noop | |||
| sources: | |||
| - config | |||
| targets: | |||
| - dump | |||
| bad.unit.tests.: | |||
| processors: | |||
| - doesnt-exist | |||
| sources: | |||
| - in | |||
| targets: | |||
| - dump | |||
| @ -0,0 +1,17 @@ | |||
| manager: | |||
| max_workers: 2 | |||
| providers: | |||
| in: | |||
| class: octodns.provider.yaml.YamlProvider | |||
| directory: tests/config | |||
| dump: | |||
| class: octodns.provider.yaml.YamlProvider | |||
| directory: env/YAML_TMP_DIR | |||
| zones: | |||
| unit.tests.: | |||
| sources: | |||
| - in | |||
| processors: | |||
| - missing | |||
| targets: | |||
| - dump | |||
| @ -0,0 +1,90 @@ | |||
| # | |||
| # | |||
| # | |||
| from __future__ import absolute_import, division, print_function, \ | |||
| unicode_literals | |||
| from unittest import TestCase | |||
| from octodns.processor.filter import TypeAllowlistFilter, TypeRejectlistFilter | |||
| from octodns.record import Record | |||
| from octodns.zone import Zone | |||
| zone = Zone('unit.tests.', []) | |||
| for record in [ | |||
| Record.new(zone, 'a', { | |||
| 'ttl': 30, | |||
| 'type': 'A', | |||
| 'value': '1.2.3.4', | |||
| }), | |||
| Record.new(zone, 'aaaa', { | |||
| 'ttl': 30, | |||
| 'type': 'AAAA', | |||
| 'value': '::1', | |||
| }), | |||
| Record.new(zone, 'txt', { | |||
| 'ttl': 30, | |||
| 'type': 'TXT', | |||
| 'value': 'Hello World!', | |||
| }), | |||
| Record.new(zone, 'a2', { | |||
| 'ttl': 30, | |||
| 'type': 'A', | |||
| 'value': '2.3.4.5', | |||
| }), | |||
| Record.new(zone, 'txt2', { | |||
| 'ttl': 30, | |||
| 'type': 'TXT', | |||
| 'value': 'That will do', | |||
| }), | |||
| ]: | |||
| zone.add_record(record) | |||
| class TestTypeAllowListFilter(TestCase): | |||
| def test_basics(self): | |||
| filter_a = TypeAllowlistFilter('only-a', set(('A'))) | |||
| got = filter_a.process_source_zone(zone) | |||
| self.assertEquals(['a', 'a2'], sorted([r.name for r in got.records])) | |||
| filter_aaaa = TypeAllowlistFilter('only-aaaa', ('AAAA',)) | |||
| got = filter_aaaa.process_source_zone(zone) | |||
| self.assertEquals(['aaaa'], sorted([r.name for r in got.records])) | |||
| filter_txt = TypeAllowlistFilter('only-txt', ['TXT']) | |||
| got = filter_txt.process_target_zone(zone) | |||
| self.assertEquals(['txt', 'txt2'], | |||
| sorted([r.name for r in got.records])) | |||
| filter_a_aaaa = TypeAllowlistFilter('only-aaaa', set(('A', 'AAAA'))) | |||
| got = filter_a_aaaa.process_target_zone(zone) | |||
| self.assertEquals(['a', 'a2', 'aaaa'], | |||
| sorted([r.name for r in got.records])) | |||
| class TestTypeRejectListFilter(TestCase): | |||
| def test_basics(self): | |||
| filter_a = TypeRejectlistFilter('not-a', set(('A'))) | |||
| got = filter_a.process_source_zone(zone) | |||
| self.assertEquals(['aaaa', 'txt', 'txt2'], | |||
| sorted([r.name for r in got.records])) | |||
| filter_aaaa = TypeRejectlistFilter('not-aaaa', ('AAAA',)) | |||
| got = filter_aaaa.process_source_zone(zone) | |||
| self.assertEquals(['a', 'a2', 'txt', 'txt2'], | |||
| sorted([r.name for r in got.records])) | |||
| filter_txt = TypeRejectlistFilter('not-txt', ['TXT']) | |||
| got = filter_txt.process_target_zone(zone) | |||
| self.assertEquals(['a', 'a2', 'aaaa'], | |||
| sorted([r.name for r in got.records])) | |||
| filter_a_aaaa = TypeRejectlistFilter('not-a-aaaa', set(('A', 'AAAA'))) | |||
| got = filter_a_aaaa.process_target_zone(zone) | |||
| self.assertEquals(['txt', 'txt2'], | |||
| sorted([r.name for r in got.records])) | |||
| @ -0,0 +1,146 @@ | |||
| # | |||
| # | |||
| # | |||
| from __future__ import absolute_import, division, print_function, \ | |||
| unicode_literals | |||
| from unittest import TestCase | |||
| from octodns.processor.ownership import OwnershipProcessor | |||
| from octodns.record import Delete, Record | |||
| from octodns.zone import Zone | |||
| from helpers import PlannableProvider | |||
| zone = Zone('unit.tests.', []) | |||
| records = {} | |||
| for record in [ | |||
| Record.new(zone, '', { | |||
| 'ttl': 30, | |||
| 'type': 'A', | |||
| 'values': [ | |||
| '1.2.3.4', | |||
| '5.6.7.8', | |||
| ], | |||
| }), | |||
| Record.new(zone, 'the-a', { | |||
| 'ttl': 30, | |||
| 'type': 'A', | |||
| 'value': '1.2.3.4', | |||
| }), | |||
| Record.new(zone, 'the-aaaa', { | |||
| 'ttl': 30, | |||
| 'type': 'AAAA', | |||
| 'value': '::1', | |||
| }), | |||
| Record.new(zone, 'the-txt', { | |||
| 'ttl': 30, | |||
| 'type': 'TXT', | |||
| 'value': 'Hello World!', | |||
| }), | |||
| Record.new(zone, '*', { | |||
| 'ttl': 30, | |||
| 'type': 'A', | |||
| 'value': '4.3.2.1', | |||
| }), | |||
| ]: | |||
| records[record.name] = record | |||
| zone.add_record(record) | |||
| class TestOwnershipProcessor(TestCase): | |||
| def test_process_source_zone(self): | |||
| ownership = OwnershipProcessor('ownership') | |||
| got = ownership.process_source_zone(zone) | |||
| self.assertEquals([ | |||
| '', | |||
| '*', | |||
| '_owner.a', | |||
| '_owner.a._wildcard', | |||
| '_owner.a.the-a', | |||
| '_owner.aaaa.the-aaaa', | |||
| '_owner.txt.the-txt', | |||
| 'the-a', | |||
| 'the-aaaa', | |||
| 'the-txt', | |||
| ], sorted([r.name for r in got.records])) | |||
| found = False | |||
| for record in got.records: | |||
| if record.name.startswith(ownership.txt_name): | |||
| self.assertEquals([ownership.txt_value], record.values) | |||
| # test _is_ownership while we're in here | |||
| self.assertTrue(ownership._is_ownership(record)) | |||
| found = True | |||
| else: | |||
| self.assertFalse(ownership._is_ownership(record)) | |||
| self.assertTrue(found) | |||
| def test_process_plan(self): | |||
| ownership = OwnershipProcessor('ownership') | |||
| provider = PlannableProvider('helper') | |||
| # No plan, is a quick noop | |||
| self.assertFalse(ownership.process_plan(None)) | |||
| # Nothing exists create both records and ownership | |||
| ownership_added = ownership.process_source_zone(zone) | |||
| plan = provider.plan(ownership_added) | |||
| self.assertTrue(plan) | |||
| # Double the number of records | |||
| self.assertEquals(len(records) * 2, len(plan.changes)) | |||
| # Now process the plan, shouldn't make any changes, we're creating | |||
| # everything | |||
| got = ownership.process_plan(plan) | |||
| self.assertTrue(got) | |||
| self.assertEquals(len(records) * 2, len(got.changes)) | |||
| # Something extra exists and doesn't have ownership TXT, leave it | |||
| # alone, we don't own it. | |||
| extra_a = Record.new(zone, 'extra-a', { | |||
| 'ttl': 30, | |||
| 'type': 'A', | |||
| 'value': '4.4.4.4', | |||
| }) | |||
| plan.existing.add_record(extra_a) | |||
| # If we'd done a "real" plan we'd have a delete for the extra thing. | |||
| plan.changes.append(Delete(extra_a)) | |||
| # Process the plan, shouldn't make any changes since the extra bit is | |||
| # something we don't own | |||
| got = ownership.process_plan(plan) | |||
| self.assertTrue(got) | |||
| self.assertEquals(len(records) * 2, len(got.changes)) | |||
| # Something extra exists and does have an ownership record so we will | |||
| # delete it... | |||
| copy = Zone('unit.tests.', []) | |||
| for record in records.values(): | |||
| if record.name != 'the-a': | |||
| copy.add_record(record) | |||
| # New ownership, without the `the-a` | |||
| ownership_added = ownership.process_source_zone(copy) | |||
| self.assertEquals(len(records) * 2 - 2, len(ownership_added.records)) | |||
| plan = provider.plan(ownership_added) | |||
| # Fake the extra existing by adding the record, its ownership, and the | |||
| # two delete changes. | |||
| the_a = records['the-a'] | |||
| plan.existing.add_record(the_a) | |||
| name = '{}.a.the-a'.format(ownership.txt_name) | |||
| the_a_ownership = Record.new(zone, name, { | |||
| 'ttl': 30, | |||
| 'type': 'TXT', | |||
| 'value': ownership.txt_value, | |||
| }) | |||
| plan.existing.add_record(the_a_ownership) | |||
| plan.changes.append(Delete(the_a)) | |||
| plan.changes.append(Delete(the_a_ownership)) | |||
| # Finally process the plan, should be a noop and we should get the same | |||
| # plan out, meaning the planned deletes were allowed to happen. | |||
| got = ownership.process_plan(plan) | |||
| self.assertTrue(got) | |||
| self.assertEquals(plan, got) | |||
| self.assertEquals(len(plan.changes), len(got.changes)) | |||