From fac662a9ec09697e7c5a95724ab1255e7fff1a27 Mon Sep 17 00:00:00 2001 From: Tobias Mueller Date: Thu, 16 Oct 2025 22:03:09 -0700 Subject: [PATCH 1/7] processors: Added a simple TTL clamping processor This will come in handy for APIs not supporting TTL outside a certain range. The Spaceship API, for example, only allows TTLs in the range of 5..3600 but rewriting a whole zone for Spaceship only seems not as convenient as clamping the values as they flow through OctoDNS. The code is coming from Claude. My prompt was: Write a simple OctoDNS processor that clamps TTL values. I followed up with: AttributeError: 'TtlClampProcessor' object has no attribute 'log' Where should self.log come from? And finally: Hm. I suspect the code is not running, somehow. It doesn't seem to be getting control. I put a raise in the process_record method but it doesn't fail. And I don't see any clamped values nor log output. So it took three attempts to make it produce something useful. --- octodns/processor/clamp.py | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 octodns/processor/clamp.py diff --git a/octodns/processor/clamp.py b/octodns/processor/clamp.py new file mode 100644 index 0000000..4bfaeb5 --- /dev/null +++ b/octodns/processor/clamp.py @@ -0,0 +1,63 @@ +from logging import getLogger +from octodns.processor.base import BaseProcessor + + +class TtlClampProcessor(BaseProcessor): + """ + Processor that clamps TTL values to a specified range. + + Configuration: + min_ttl: Minimum TTL value (default: 300 seconds / 5 minutes) + max_ttl: Maximum TTL value (default: 86400 seconds / 24 hours) + + Example config.yaml: + processors: + clamp: + class: octodns.processor.clamp.TtlClampProcessor + min_ttl: 300 + max_ttl: 3600 + + zones: + example.com.: + sources: + - config + processors: + - clamp + targets: + - route53 + """ + + def __init__(self, id, min_ttl=300, max_ttl=86400): + super().__init__(id) + self.log = getLogger(f'{self.__class__.__module__}.{self.__class__.__name__}') + self.min_ttl = min_ttl + self.max_ttl = max_ttl + self.log.info( + f'TtlClampProcessor initialized: min={min_ttl}s, max={max_ttl}s' + ) + + def process_source_zone(self, desired, sources): + """ + Process records from source zone(s). + + Args: + desired: Zone object containing the desired records + sources: List of source names + + Returns: + The modified zone + """ + self.log.debug(f'Processing source zone: {desired.name}') + + for record in desired.records: + original_ttl = record.ttl + clamped_ttl = max(self.min_ttl, min(self.max_ttl, original_ttl)) + + if clamped_ttl != original_ttl: + self.log.info( + f'Clamping TTL for {record.fqdn} ({record._type}): ' + f'{original_ttl}s -> {clamped_ttl}s' + ) + record.ttl = clamped_ttl + + return desired From 6ebba0411132b805c1b28bf1dd372c5ff49fb4d8 Mon Sep 17 00:00:00 2001 From: Tobias Mueller Date: Fri, 17 Oct 2025 07:08:47 -0700 Subject: [PATCH 2/7] clamp: formatted I ran both isort and black. Let's hope that this makes the CI happy. --- octodns/processor/clamp.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/octodns/processor/clamp.py b/octodns/processor/clamp.py index 4bfaeb5..31c9e89 100644 --- a/octodns/processor/clamp.py +++ b/octodns/processor/clamp.py @@ -1,22 +1,23 @@ from logging import getLogger + from octodns.processor.base import BaseProcessor class TtlClampProcessor(BaseProcessor): """ Processor that clamps TTL values to a specified range. - + Configuration: min_ttl: Minimum TTL value (default: 300 seconds / 5 minutes) max_ttl: Maximum TTL value (default: 86400 seconds / 24 hours) - + Example config.yaml: processors: clamp: class: octodns.processor.clamp.TtlClampProcessor min_ttl: 300 max_ttl: 3600 - + zones: example.com.: sources: @@ -26,38 +27,40 @@ class TtlClampProcessor(BaseProcessor): targets: - route53 """ - + def __init__(self, id, min_ttl=300, max_ttl=86400): super().__init__(id) - self.log = getLogger(f'{self.__class__.__module__}.{self.__class__.__name__}') + self.log = getLogger( + f'{self.__class__.__module__}.{self.__class__.__name__}' + ) self.min_ttl = min_ttl self.max_ttl = max_ttl self.log.info( f'TtlClampProcessor initialized: min={min_ttl}s, max={max_ttl}s' ) - + def process_source_zone(self, desired, sources): """ Process records from source zone(s). - + Args: desired: Zone object containing the desired records sources: List of source names - + Returns: The modified zone """ self.log.debug(f'Processing source zone: {desired.name}') - + for record in desired.records: original_ttl = record.ttl clamped_ttl = max(self.min_ttl, min(self.max_ttl, original_ttl)) - + if clamped_ttl != original_ttl: self.log.info( f'Clamping TTL for {record.fqdn} ({record._type}): ' f'{original_ttl}s -> {clamped_ttl}s' ) record.ttl = clamped_ttl - + return desired From 890808464d809a8701dd9c8651e1136f8c784d96 Mon Sep 17 00:00:00 2001 From: Tobias Mueller Date: Fri, 17 Oct 2025 09:32:21 -0700 Subject: [PATCH 3/7] added changelog --- .changelog/2809d288040441ccb8e6633f514b09b0.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changelog/2809d288040441ccb8e6633f514b09b0.md diff --git a/.changelog/2809d288040441ccb8e6633f514b09b0.md b/.changelog/2809d288040441ccb8e6633f514b09b0.md new file mode 100644 index 0000000..91ca407 --- /dev/null +++ b/.changelog/2809d288040441ccb8e6633f514b09b0.md @@ -0,0 +1,5 @@ +--- +type: minor +--- + +Add processor for clamping TTLs From 5d9fd7e789c32657152b3795b39894973ae6203b Mon Sep 17 00:00:00 2001 From: Tobias Mueller Date: Fri, 17 Oct 2025 10:48:03 -0700 Subject: [PATCH 4/7] processor: clamp: Raise if TTL arguments are not logical We must not have min TTLs that are not smaller than the max TTL. --- octodns/processor/clamp.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/octodns/processor/clamp.py b/octodns/processor/clamp.py index 31c9e89..49dd241 100644 --- a/octodns/processor/clamp.py +++ b/octodns/processor/clamp.py @@ -1,6 +1,9 @@ from logging import getLogger -from octodns.processor.base import BaseProcessor +from .base import BaseProcessor, ProcessorException + +class TTLArgumentException(ProcessorException): + pass class TtlClampProcessor(BaseProcessor): @@ -33,6 +36,8 @@ class TtlClampProcessor(BaseProcessor): self.log = getLogger( f'{self.__class__.__module__}.{self.__class__.__name__}' ) + if not min_ttl <= max_ttl: + raise TTLArgumentException(f'Min TTL {min_ttl} is not lower than max TTL {max_ttl}') self.min_ttl = min_ttl self.max_ttl = max_ttl self.log.info( From d606318fdc2d9c59e169912cd37c0996631e4bf8 Mon Sep 17 00:00:00 2001 From: Tobias Mueller Date: Fri, 17 Oct 2025 10:50:50 -0700 Subject: [PATCH 5/7] clamp: Added test cases --- tests/test_octodns_processor_clamp.py | 120 ++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 tests/test_octodns_processor_clamp.py diff --git a/tests/test_octodns_processor_clamp.py b/tests/test_octodns_processor_clamp.py new file mode 100644 index 0000000..63c2de1 --- /dev/null +++ b/tests/test_octodns_processor_clamp.py @@ -0,0 +1,120 @@ +from unittest import TestCase + +from octodns.processor.clamp import TTLArgumentException, TtlClampProcessor +from octodns.record.base import Record +from octodns.zone import Zone + + +class TestClampProcessor(TestCase): + + def test_processor_min(self): + "Test the processor for clamping to the minimum" + min_ttl = 42 + processor = TtlClampProcessor('test', min_ttl=min_ttl) + + too_low_ttl = 23 + self.assertLess(too_low_ttl, min_ttl) + + zone = Zone('unit.tests.', []) + zone.add_record( + Record.new( + zone, '', {'type': 'TXT', 'ttl': too_low_ttl, 'value': 'foo'} + ) + ) + + processed_zone = processor.process_source_zone(zone.copy(), None) + self.assertNotEqual(zone, processed_zone) + + self.assertEqual(len(processed_zone.records), len(zone.records)) + self.assertEqual(len(processed_zone.records), 1) + self.assertEqual(processed_zone.records.pop().ttl, min_ttl) + + def test_processor_max(self): + "Test the processor for clamping to the maximum" + max_ttl = 4711 + processor = TtlClampProcessor('test', max_ttl=max_ttl) + + too_high_ttl = max_ttl + 1 + self.assertLess(max_ttl, too_high_ttl) + + zone = Zone('unit.tests.', []) + zone.add_record( + Record.new( + zone, '', {'type': 'TXT', 'ttl': too_high_ttl, 'value': 'foo'} + ) + ) + + processed_zone = processor.process_source_zone(zone.copy(), None) + self.assertNotEqual(zone, processed_zone) + + self.assertEqual(len(processed_zone.records), len(zone.records)) + self.assertEqual(len(processed_zone.records), 1) + self.assertEqual(processed_zone.records.pop().ttl, max_ttl) + + def test_processor_maxmin(self): + "Test the processor for unlogical arguments" + min_ttl = 42 + max_ttl = 23 + self.assertRaises( + TTLArgumentException, + TtlClampProcessor, + 'test', + min_ttl=min_ttl, + max_ttl=max_ttl, + ) + + def test_processor_minmax(self): + "Test the processor for clamping both min and max values" + min_ttl = 42 + max_ttl = 4711 + processor = TtlClampProcessor('test', min_ttl=min_ttl, max_ttl=max_ttl) + + too_low_ttl = min_ttl - 1 + too_high_ttl = max_ttl + 1 + self.assertLess(too_low_ttl, min_ttl) + self.assertLess(too_low_ttl, min_ttl) + self.assertLess(max_ttl, too_high_ttl) + + zone = Zone('unit.tests.', []) + zone.add_record( + Record.new( + zone, + 'high', + {'type': 'TXT', 'ttl': too_high_ttl, 'value': 'high'}, + ) + ) + zone.add_record( + Record.new( + zone, 'low', {'type': 'TXT', 'ttl': too_low_ttl, 'value': 'low'} + ) + ) + + processed_zone = processor.process_source_zone(zone.copy(), None) + self.assertNotEqual(zone, processed_zone) + + processed_records = sorted( + list(processed_zone.records), key=lambda r: r.ttl + ) + self.assertEqual(len(processed_records), 2) + + self.assertEqual(processed_records[0].ttl, min_ttl) + self.assertEqual(processed_records[1].ttl, max_ttl) + + def test_processor_noclamp(self): + "Test the processor for working with TTLs not requiring any clamping" + min_ttl = 23 + max_ttl = 4711 + processor = TtlClampProcessor('test', min_ttl=min_ttl, max_ttl=max_ttl) + + ttl = 42 + + self.assertLess(min_ttl, ttl) + self.assertLess(ttl, max_ttl) + + zone = Zone('unit.tests.', []) + zone.add_record( + Record.new(zone, '', {'type': 'TXT', 'ttl': ttl, 'value': 'foo'}) + ) + + processed_zone = processor.process_source_zone(zone.copy(), None) + self.assertEqual(processed_zone.records.pop().ttl, ttl) From 24881168c1b9504fbde6e306016cd6e1d4d2ef7e Mon Sep 17 00:00:00 2001 From: Tobias Mueller Date: Sat, 18 Oct 2025 12:04:24 -0700 Subject: [PATCH 6/7] clamp: let log format in place rather than f-strings --- octodns/processor/clamp.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/octodns/processor/clamp.py b/octodns/processor/clamp.py index 49dd241..acb3876 100644 --- a/octodns/processor/clamp.py +++ b/octodns/processor/clamp.py @@ -2,6 +2,7 @@ from logging import getLogger from .base import BaseProcessor, ProcessorException + class TTLArgumentException(ProcessorException): pass @@ -33,15 +34,17 @@ class TtlClampProcessor(BaseProcessor): def __init__(self, id, min_ttl=300, max_ttl=86400): super().__init__(id) - self.log = getLogger( - f'{self.__class__.__module__}.{self.__class__.__name__}' - ) + self.log = getLogger(self.__class__.__name__) if not min_ttl <= max_ttl: - raise TTLArgumentException(f'Min TTL {min_ttl} is not lower than max TTL {max_ttl}') + raise TTLArgumentException( + f'Min TTL {min_ttl} is not lower than max TTL {max_ttl}' + ) self.min_ttl = min_ttl self.max_ttl = max_ttl self.log.info( - f'TtlClampProcessor initialized: min={min_ttl}s, max={max_ttl}s' + 'TtlClampProcessor initialized: min=%ds, max=%ds', + self.min_ttl, + self.max_ttl, ) def process_source_zone(self, desired, sources): @@ -55,7 +58,7 @@ class TtlClampProcessor(BaseProcessor): Returns: The modified zone """ - self.log.debug(f'Processing source zone: {desired.name}') + self.log.debug('Processing source zone: %s', desired.name) for record in desired.records: original_ttl = record.ttl @@ -63,8 +66,11 @@ class TtlClampProcessor(BaseProcessor): if clamped_ttl != original_ttl: self.log.info( - f'Clamping TTL for {record.fqdn} ({record._type}): ' - f'{original_ttl}s -> {clamped_ttl}s' + 'Clamping TTL for %s (%s) %s -> %s', + record.fqdn, + record._type, + original_ttl, + clamped_ttl, ) record.ttl = clamped_ttl From 1a69d344b8c39e923909f3e593bebbc291242c6f Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Sat, 18 Oct 2025 13:36:50 -0700 Subject: [PATCH 7/7] std logging prefix, func names --- octodns/processor/clamp.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/octodns/processor/clamp.py b/octodns/processor/clamp.py index acb3876..68a3fe2 100644 --- a/octodns/processor/clamp.py +++ b/octodns/processor/clamp.py @@ -41,11 +41,7 @@ class TtlClampProcessor(BaseProcessor): ) self.min_ttl = min_ttl self.max_ttl = max_ttl - self.log.info( - 'TtlClampProcessor initialized: min=%ds, max=%ds', - self.min_ttl, - self.max_ttl, - ) + self.log.info('__init__: min=%ds, max=%ds', self.min_ttl, self.max_ttl) def process_source_zone(self, desired, sources): """ @@ -58,7 +54,7 @@ class TtlClampProcessor(BaseProcessor): Returns: The modified zone """ - self.log.debug('Processing source zone: %s', desired.name) + self.log.debug('process_source_zone: desired=%s', desired.name) for record in desired.records: original_ttl = record.ttl @@ -66,7 +62,7 @@ class TtlClampProcessor(BaseProcessor): if clamped_ttl != original_ttl: self.log.info( - 'Clamping TTL for %s (%s) %s -> %s', + 'process_source_zone: clamping TTL for %s (%s) %s -> %s', record.fqdn, record._type, original_ttl,