From e7d59e63573a357dd495358ed03b53c6a9ec6f21 Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Thu, 8 Feb 2024 20:00:46 +0000 Subject: [PATCH 1/7] Experimental processor for TTL proxy status --- octodns_cloudflare/processor/__init__.py | 3 ++ octodns_cloudflare/processor/ttl.py | 48 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 octodns_cloudflare/processor/__init__.py create mode 100644 octodns_cloudflare/processor/ttl.py diff --git a/octodns_cloudflare/processor/__init__.py b/octodns_cloudflare/processor/__init__.py new file mode 100644 index 0000000..9286583 --- /dev/null +++ b/octodns_cloudflare/processor/__init__.py @@ -0,0 +1,3 @@ +# +# +# \ No newline at end of file diff --git a/octodns_cloudflare/processor/ttl.py b/octodns_cloudflare/processor/ttl.py new file mode 100644 index 0000000..060ffc2 --- /dev/null +++ b/octodns_cloudflare/processor/ttl.py @@ -0,0 +1,48 @@ +# +# +# + +from octodns.processor.base import BaseProcessor, ProcessorException + + +class RestrictionException(ProcessorException): + pass + + +class TtlToProxy(BaseProcessor): + ''' + Ensure Cloudflare's proxy status is setup depending on the TTL set for the record. This + can be helpful for `octodns_bind.ZoneFileSource` or the like. + + Example usage: + + processors: + ttl-to-proxy: + class: octodns_cloudflare.processor.ttl.TtlToProxy + ttl: 0 + + zones: + exxampled.com.: + sources: + - config + processors: + - ttl-to-proxy + targets: + - cloudflare + ''' + + SEVEN_DAYS = 60 * 60 * 24 * 7 + + def __init__(self, name, ttl=0): + super().__init__(name) + self.ttl = ttl + + def process_source_zone(self, zone, *args, **kwargs): + for record in zone.records: + if record.ttl != self.ttl: + continue + else: + record._octodns['cloudflare'] = {'proxied': True, 'auto-ttl': True} + record.ttl = 1; # Ensure we set to valid TTL. + + return zone From 4493846444e1d834469a18d202650cc14bf0c848 Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Thu, 8 Feb 2024 20:11:06 +0000 Subject: [PATCH 2/7] Remove extraneous code --- octodns_cloudflare/processor/ttl.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/octodns_cloudflare/processor/ttl.py b/octodns_cloudflare/processor/ttl.py index 060ffc2..7769e2a 100644 --- a/octodns_cloudflare/processor/ttl.py +++ b/octodns_cloudflare/processor/ttl.py @@ -4,11 +4,6 @@ from octodns.processor.base import BaseProcessor, ProcessorException - -class RestrictionException(ProcessorException): - pass - - class TtlToProxy(BaseProcessor): ''' Ensure Cloudflare's proxy status is setup depending on the TTL set for the record. This @@ -31,8 +26,6 @@ class TtlToProxy(BaseProcessor): - cloudflare ''' - SEVEN_DAYS = 60 * 60 * 24 * 7 - def __init__(self, name, ttl=0): super().__init__(name) self.ttl = ttl From bad9bad13f3d727ca0bf99083c6f421d2f1ef4f7 Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Thu, 8 Feb 2024 21:15:22 +0000 Subject: [PATCH 3/7] Simplify if/else Co-authored-by: Ross McFarland --- octodns_cloudflare/processor/ttl.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/octodns_cloudflare/processor/ttl.py b/octodns_cloudflare/processor/ttl.py index 7769e2a..2d673bc 100644 --- a/octodns_cloudflare/processor/ttl.py +++ b/octodns_cloudflare/processor/ttl.py @@ -32,9 +32,7 @@ def __init__(self, name, ttl=0): def process_source_zone(self, zone, *args, **kwargs): for record in zone.records: - if record.ttl != self.ttl: - continue - else: + if record.ttl == self.ttl: record._octodns['cloudflare'] = {'proxied': True, 'auto-ttl': True} record.ttl = 1; # Ensure we set to valid TTL. From 0a7236dd873721e047b321238a144273a75372a5 Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Thu, 8 Feb 2024 21:17:32 +0000 Subject: [PATCH 4/7] Use copy of record during processing Co-authored-by: Ross McFarland --- octodns_cloudflare/processor/ttl.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/octodns_cloudflare/processor/ttl.py b/octodns_cloudflare/processor/ttl.py index 2d673bc..508d260 100644 --- a/octodns_cloudflare/processor/ttl.py +++ b/octodns_cloudflare/processor/ttl.py @@ -33,7 +33,9 @@ def __init__(self, name, ttl=0): def process_source_zone(self, zone, *args, **kwargs): for record in zone.records: if record.ttl == self.ttl: + record = record.copy() record._octodns['cloudflare'] = {'proxied': True, 'auto-ttl': True} record.ttl = 1; # Ensure we set to valid TTL. + desired.add_record(record, replace=True, lenient=True) return zone From 1af8d8829a142ec9cfd0374b85f00889f72b87b5 Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Thu, 8 Feb 2024 21:20:28 +0000 Subject: [PATCH 5/7] Linting & Testing --- octodns_cloudflare/processor/__init__.py | 2 +- octodns_cloudflare/processor/ttl.py | 11 +++-- ...todns_provider_cloudflare_processor_ttl.py | 42 +++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 tests/test_octodns_provider_cloudflare_processor_ttl.py diff --git a/octodns_cloudflare/processor/__init__.py b/octodns_cloudflare/processor/__init__.py index 9286583..407eb4e 100644 --- a/octodns_cloudflare/processor/__init__.py +++ b/octodns_cloudflare/processor/__init__.py @@ -1,3 +1,3 @@ # # -# \ No newline at end of file +# diff --git a/octodns_cloudflare/processor/ttl.py b/octodns_cloudflare/processor/ttl.py index 508d260..301ad14 100644 --- a/octodns_cloudflare/processor/ttl.py +++ b/octodns_cloudflare/processor/ttl.py @@ -4,6 +4,7 @@ from octodns.processor.base import BaseProcessor, ProcessorException + class TtlToProxy(BaseProcessor): ''' Ensure Cloudflare's proxy status is setup depending on the TTL set for the record. This @@ -34,8 +35,12 @@ def process_source_zone(self, zone, *args, **kwargs): for record in zone.records: if record.ttl == self.ttl: record = record.copy() - record._octodns['cloudflare'] = {'proxied': True, 'auto-ttl': True} - record.ttl = 1; # Ensure we set to valid TTL. - desired.add_record(record, replace=True, lenient=True) + record._octodns['cloudflare'] = { + 'proxied': True, + 'auto-ttl': True, + } + record.ttl = 1 + # Ensure we set to valid TTL. + zone.add_record(record, replace=True, lenient=True) return zone diff --git a/tests/test_octodns_provider_cloudflare_processor_ttl.py b/tests/test_octodns_provider_cloudflare_processor_ttl.py new file mode 100644 index 0000000..d86d7e4 --- /dev/null +++ b/tests/test_octodns_provider_cloudflare_processor_ttl.py @@ -0,0 +1,42 @@ +from unittest import TestCase + +from octodns.record import Record +from octodns.zone import Zone + +from octodns_cloudflare.processor.ttl import TtlToProxy + + +class TestTtlToProxy(TestCase): + def test_ttl_to_proxy(self): + processor = TtlToProxy('test', ttl=0) + + zone = Zone('unit.tests.', []) + zone_expected = Zone('unit.tests.', []) + + with_ttl = Record.new( + zone, 'good', {'type': 'A', 'ttl': 0, 'value': '1.2.3.4'} + ) + without_ttl = Record.new( + zone, 'bad', {'type': 'A', 'ttl': 10, 'value': '1.2.3.4'} + ) + zone.add_record(with_ttl) + zone.add_record(without_ttl) + + expected_with = Record.new( + zone, + 'good', + { + 'type': 'A', + 'ttl': 0, + 'value': '1.2.3.4', + '_octodns': {'cloudflare': {'proxied': True, 'auto-ttl': True}}, + }, + ) + expected_without = Record.new( + zone, 'bad', {'type': 'A', 'ttl': 10, 'value': '1.2.3.4'} + ) + zone_expected.add_record(expected_with) + zone_expected.add_record(expected_without) + + added_proxy = processor.process_source_zone(zone) + self.assertEqual(zone_expected.records, added_proxy.records) From 9e001cb1ee8bf8ff1a6ee82c040c8cebc00f8182 Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Fri, 9 Feb 2024 16:28:46 +0000 Subject: [PATCH 6/7] Correct testing format Co-authored-by: Ross McFarland --- tests/test_octodns_provider_cloudflare_processor_ttl.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_octodns_provider_cloudflare_processor_ttl.py b/tests/test_octodns_provider_cloudflare_processor_ttl.py index d86d7e4..47f442b 100644 --- a/tests/test_octodns_provider_cloudflare_processor_ttl.py +++ b/tests/test_octodns_provider_cloudflare_processor_ttl.py @@ -40,3 +40,11 @@ def test_ttl_to_proxy(self): added_proxy = processor.process_source_zone(zone) self.assertEqual(zone_expected.records, added_proxy.records) + good = next(r for r in added_proxy.records if r.name == 'good') + self.assertEqual(1, good.ttl) + self.assertEqual( + {'cloudflare': {'proxied': True, 'auto-ttl': True}}, good._octodns + ) + bad = next(r for r in added_proxy.records if r.name == 'bad') + self.assertEqual(10, bad.ttl) + self.assertFalse('cloudflare' in bad._octodns) From 7a7d41400de7e864f3f35f52796439bc18c5ac7d Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Fri, 9 Feb 2024 08:37:26 -0800 Subject: [PATCH 7/7] CHANGELOG.md entry for TtlToProxy --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08adec8..ba23a86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## v0.0.4 - 2024-??-?? - ??? + +* TtlToProxy processor added to enable the proxied flag based on a sentinel + ttl value. Useful when the source is not YamlProvider + ## v0.0.4 - 2024-02-08 - Know your zones * Support for Provider.list_zones to enable dynamic zone config when operating