zonesync 0.14.5 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93b9f7c02475e037aad7c07c9ea6268f9ec0ab8fe87fe8e17a89265ff2baabe9
4
- data.tar.gz: 29d5558b0f695daa7f5e7a9296a5ded364ffbb320161bd0c2fbf077db6aa1546
3
+ metadata.gz: dc35acf531c5359239616d9009abb61ade3c711c83c687a124ebeb0c3f6519ce
4
+ data.tar.gz: a5dbb4528cbc1f0aa430009ab0d730147e57e59b868dd1b446fc529e7e35e38a
5
5
  SHA512:
6
- metadata.gz: 85884ce3351196bdee5adf6b8c0cda689ccc41ea17e5275022313908da04fef06c2cb6d92d4b33d56579a6ab2aa9d66dd57cec55ad3956aec5a8faa6097ac4aa
7
- data.tar.gz: 290dcc4a9820473ef6404d1f23fff7755e61677fcc00df4d58eb85cd3ca1f2aba9204075d20caa770331145ca4c4cda7142f66660a17e61843b5192933eed2e7
6
+ metadata.gz: 4f4e7910d91a2b83617af915cae0f21aa93fb0404e211a604903a6f5da39827744032dcdd5c797d7e915bb7c16e2135ea73f4cba6af88f490236319f6f8e876c
7
+ data.tar.gz: 6885221a705a0d39fe0045494511736446b3563f90776389472f811a9b0e21662531a47a7f9bad8ab50dcb286d8e3e5de37a2b38221b849284ad8067852690a1
data/README.md CHANGED
@@ -126,6 +126,22 @@ api IN A 192.0.2.3
126
126
 
127
127
  When generating a zone file with `zonesync generate`, proxied records will automatically include the `cf_tags=cf-proxied:true` tag in their comments.
128
128
 
129
+ ### Route53 alias records
130
+
131
+ When using the Route53 provider, an A or AAAA record can be an alias to another AWS resource. Write its target as a fully qualified name and tag it with a `route53-alias` comment:
132
+
133
+ ```
134
+ @ A d111abcdef8.cloudfront.net. ; route53-alias
135
+ @ AAAA d111abcdef8.cloudfront.net. ; route53-alias
136
+ api A dualstack.my-lb-123.us-east-1.elb.amazonaws.com. ; route53-alias=Z35SXDOTRQ7X7K,evaluate-target-health
137
+ ```
138
+
139
+ - `route53-alias` — CloudFront targets need nothing more, since their hosted zone is always `Z2FDTNDATAQYW2`
140
+ - `route53-alias=<HostedZoneId>` — the alias hosted zone of any other target
141
+ - `,evaluate-target-health` — have Route53 check the target's health
142
+
143
+ Route53 serves an alias with its target's TTL, so any TTL on the line is ignored. Other DNS providers can't hold an A or AAAA record that names a host, and refuse to sync one.
144
+
129
145
  ### Managing or avoiding conflicts with other people making edits to the DNS records
130
146
 
131
147
  Zonesync writes two additional TXT records: `zonesync_manifest` and `zonesync_checksum`. These two records together try to handle the situation where someone else makes edits directly to the DNS records managed by zonesync.
data/lib/zonesync/cli.rb CHANGED
@@ -13,7 +13,7 @@ module Zonesync
13
13
  def sync
14
14
  kwargs = options.to_hash.transform_keys(&:to_sym)
15
15
  Zonesync.call(**kwargs)
16
- rescue ConflictError, MissingManifestError, ChecksumMismatchError => e
16
+ rescue ConflictError, MissingManifestError, ChecksumMismatchError, UnsupportedRecordError => e
17
17
  puts e.message
18
18
  exit 1
19
19
  end
@@ -19,6 +19,13 @@ module Zonesync
19
19
  )
20
20
  end
21
21
 
22
+ # Cloudflare flattens a CNAME at the apex, so it can sit beside the apex's other records
23
+ def excludes?(existing, added)
24
+ apex = records.find { |r| r.type == "SOA" }.name
25
+ return false if [existing, added].any? { |r| r.type == "CNAME" && r.name == apex }
26
+ super
27
+ end
28
+
22
29
  def remove(record)
23
30
  id = find_record_id(record)
24
31
  http.delete("/#{id}")
@@ -44,6 +51,9 @@ module Zonesync
44
51
  # Convert CloudFlare-specific duplicate error to standard exception
45
52
  if e.message.include?('"code":81058') && e.message.include?("An identical record already exists")
46
53
  raise DuplicateRecordError.new(record, "CloudFlare error 81058")
54
+ # Cloudflare reports an identical CNAME as 81053, the same as a conflicting A/AAAA/CNAME
55
+ elsif e.message.include?('"code":81053') && all.keys.any? { |existing| existing.identical_to?(record) }
56
+ raise DuplicateRecordError.new(record, "CloudFlare error 81053")
47
57
  else
48
58
  # Re-raise other errors
49
59
  raise
@@ -114,6 +114,20 @@ module Zonesync
114
114
  end
115
115
  end
116
116
 
117
+ class UnsupportedRecordError < StandardError
118
+ def initialize(record, reason)
119
+ @record = record
120
+ @reason = reason
121
+ end
122
+
123
+ def message
124
+ <<~MSG.chomp
125
+ #{@reason}:
126
+ #{@record}
127
+ MSG
128
+ end
129
+ end
130
+
117
131
  class DuplicateRecordError < StandardError
118
132
  def initialize(record, provider_message = nil)
119
133
  @record = record
@@ -17,6 +17,13 @@ module Zonesync
17
17
  Zonesync.const_get(config.fetch(:provider)).new(config)
18
18
  end
19
19
 
20
+ # The source as this provider would hold it; only a provider that gives meaning to an A or AAAA naming a host may accept one
21
+ def interpret(source)
22
+ named = source.records.find { |record| %w[A AAAA].include?(record.type) && record.rdata.end_with?(".") }
23
+ raise UnsupportedRecordError.new(named, "#{self.class.name.split("::").last} can't hold an A or AAAA record that names a host instead of an address") if named
24
+ source
25
+ end
26
+
20
27
  def diff!(other, force: false)
21
28
  operations = diff(other).call
22
29
  Validator.call(operations, self, other, force: force)
@@ -41,6 +48,8 @@ module Zonesync
41
48
  zonefile.records
42
49
  end
43
50
 
51
+ def excludes?(existing, added) = existing.excludes?(added)
52
+
44
53
  def diffable_records
45
54
  records.select do |record|
46
55
  manifest.diffable?(record)
@@ -3,7 +3,7 @@
3
3
  module Zonesync
4
4
  class Repair
5
5
  def initialize(local, remote)
6
- @local = local
6
+ @local = remote.interpret(local)
7
7
  @remote = remote
8
8
  @remote_config = remote.config
9
9
  end
@@ -161,8 +161,8 @@ module Zonesync
161
161
 
162
162
  def update_manifest(output)
163
163
  # Re-read both providers to get the updated records
164
- updated_local = Provider.from({ provider: "Filesystem", path: @local.config.fetch(:path) })
165
164
  updated_remote = Provider.from(@remote_config)
165
+ updated_local = updated_remote.interpret(Provider.from({ provider: "Filesystem", path: @local.config.fetch(:path) }))
166
166
 
167
167
  # Generate new manifest based on current local state
168
168
  new_manifest = updated_local.manifest.generate
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zonesync
4
+ class Route53 < Provider
5
+ class AliasRecord < Record
6
+ TAG = /\broute53-alias(?:=(?<hosted_zone_id>\w+))?(?<evaluate_target_health>,evaluate-target-health)?(?!\S)/
7
+ CLOUDFRONT_HOSTED_ZONE_ID = "Z2FDTNDATAQYW2"
8
+ private_constant :TAG, :CLOUDFRONT_HOSTED_ZONE_ID
9
+
10
+ def self.adopt(records)
11
+ records = records.map { |record| from(record) }
12
+ records.group_by { |record| [record.name, record.type] }.each_value { |rrset| of(rrset) }
13
+ records
14
+ end
15
+
16
+ def self.of(rrset)
17
+ alias_record = rrset.find { |record| record.is_a?(self) }
18
+ if alias_record && rrset.length > 1
19
+ raise UnsupportedRecordError.new(alias_record, "Route53 holds an alias as the only record of its name and type")
20
+ end
21
+ alias_record
22
+ end
23
+
24
+ def self.from(record)
25
+ return record if record.is_a?(self)
26
+ tag = record.comment&.match(TAG)
27
+ named = %w[A AAAA].include?(record.type) && record.rdata.end_with?(".")
28
+ return record unless tag || named
29
+ raise UnsupportedRecordError.new(record, "route53-alias only applies to an A or AAAA record whose target is a fully qualified name") unless named
30
+ raise UnsupportedRecordError.new(record, "Route53 holds an A or AAAA record that names a host only as an alias, tagged route53-alias") unless tag
31
+
32
+ dns_name = record.rdata.downcase
33
+ hosted_zone_id = tag[:hosted_zone_id] || (CLOUDFRONT_HOSTED_ZONE_ID if cloudfront?(dns_name))
34
+ raise UnsupportedRecordError.new(record, "Only CloudFront's alias hosted zone is inferred; state this target's with route53-alias=<HostedZoneId>") unless hosted_zone_id
35
+
36
+ build(
37
+ name: record.name,
38
+ type: record.type,
39
+ dns_name: dns_name,
40
+ hosted_zone_id: hosted_zone_id,
41
+ evaluate_target_health: !!tag[:evaluate_target_health],
42
+ )
43
+ end
44
+
45
+ def self.build(name:, type:, dns_name:, hosted_zone_id:, evaluate_target_health:)
46
+ tag = +"route53-alias"
47
+ tag << "=#{hosted_zone_id}" unless hosted_zone_id == CLOUDFRONT_HOSTED_ZONE_ID && cloudfront?(dns_name)
48
+ tag << ",evaluate-target-health" if evaluate_target_health
49
+ # Route53 holds no TTL for an alias, so the Zonefile's must not reach the manifest hash
50
+ new(name: name, type: type, ttl: nil, rdata: dns_name, comment: tag)
51
+ end
52
+
53
+ def self.cloudfront?(dns_name)
54
+ dns_name.end_with?(".cloudfront.net.")
55
+ end
56
+
57
+ def hosted_zone_id
58
+ comment[TAG, :hosted_zone_id] || CLOUDFRONT_HOSTED_ZONE_ID
59
+ end
60
+
61
+ def evaluate_target_health
62
+ !!comment[TAG, :evaluate_target_health]
63
+ end
64
+
65
+ def conflicts_with?(other)
66
+ name == other.name && type == other.type
67
+ end
68
+
69
+ def to_h
70
+ super.except(:ttl)
71
+ end
72
+
73
+ def to_s
74
+ "#{name} #{type} #{rdata} ; #{comment}"
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zonesync
4
+ class Route53 < Provider
5
+ class Source < Provider
6
+ def initialize(source)
7
+ super(source.config)
8
+ @source = source
9
+ end
10
+
11
+ def read
12
+ @source.read
13
+ end
14
+
15
+ def write(string)
16
+ @source.write(string)
17
+ end
18
+
19
+ def records
20
+ AliasRecord.adopt(super)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require "zonesync/record"
4
4
  require "zonesync/http"
5
+ require "zonesync/route53/alias_record"
6
+ require "zonesync/route53/source"
5
7
  require "rexml/document"
6
8
  require "erb"
7
9
 
@@ -17,6 +19,14 @@ module Zonesync
17
19
  end
18
20
  end
19
21
 
22
+ def interpret(source)
23
+ Source.new(source)
24
+ end
25
+
26
+ def records
27
+ AliasRecord.adopt(super)
28
+ end
29
+
20
30
  def remove(record)
21
31
  # Route53 requires all records with the same name/type to be managed together as a record set
22
32
  existing_records = records.select do |r|
@@ -132,6 +142,13 @@ module Zonesync
132
142
  <ResourceRecordSet>
133
143
  <Name><%= name %></Name>
134
144
  <Type><%= type %></Type>
145
+ <%- if (alias_record = AliasRecord.of(records_list.select { |r| r.name == name && r.type == type })) -%>
146
+ <AliasTarget>
147
+ <HostedZoneId><%= alias_record.hosted_zone_id %></HostedZoneId>
148
+ <DNSName><%= alias_record.rdata %></DNSName>
149
+ <EvaluateTargetHealth><%= alias_record.evaluate_target_health %></EvaluateTargetHealth>
150
+ </AliasTarget>
151
+ <%- else -%>
135
152
  <TTL><%= ttl %></TTL>
136
153
  <ResourceRecords>
137
154
  <%- group_records.each do |group_record| -%>
@@ -140,6 +157,7 @@ module Zonesync
140
157
  </ResourceRecord>
141
158
  <%- end -%>
142
159
  </ResourceRecords>
160
+ <%- end -%>
143
161
  </ResourceRecordSet>
144
162
  </Change>
145
163
  <%- end -%>
@@ -151,62 +169,14 @@ module Zonesync
151
169
  end
152
170
 
153
171
  def change_record(action, record)
154
- http.post("", <<~XML)
155
- <?xml version="1.0" encoding="UTF-8"?>
156
- <ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
157
- <ChangeBatch>
158
- <Changes>
159
- <Change>
160
- <Action>#{action}</Action>
161
- <ResourceRecordSet>
162
- <Name>#{record.name}</Name>
163
- <Type>#{record.type}</Type>
164
- <TTL>#{record.ttl}</TTL>
165
- <ResourceRecords>
166
- <ResourceRecord>
167
- <Value>#{rdata_for_api(record)}</Value>
168
- </ResourceRecord>
169
- </ResourceRecords>
170
- </ResourceRecordSet>
171
- </Change>
172
- </Changes>
173
- </ChangeBatch>
174
- </ChangeResourceRecordSetsRequest>
175
- XML
172
+ post_changes([[[record], action]])
176
173
  end
177
174
 
178
175
  def change_records(action, records_list)
179
- # Group records by name and type to handle multiple values
180
- grouped = records_list.group_by { |r| [r.name, r.type, r.ttl] }
181
-
182
- http.post("", ERB.new(<<~XML, trim_mode: "-").result(binding))
183
- <?xml version="1.0" encoding="UTF-8"?>
184
- <ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
185
- <ChangeBatch>
186
- <Changes>
187
- <%- grouped.each do |(name, type, ttl), records| -%>
188
- <Change>
189
- <Action><%= action %></Action>
190
- <ResourceRecordSet>
191
- <Name><%= name %></Name>
192
- <Type><%= type %></Type>
193
- <TTL><%= ttl %></TTL>
194
- <ResourceRecords>
195
- <%- records.each do |record| -%>
196
- <ResourceRecord>
197
- <Value><%= rdata_for_api(record) %></Value>
198
- </ResourceRecord>
199
- <%- end -%>
200
- </ResourceRecords>
201
- </ResourceRecordSet>
202
- </Change>
203
- <%- end -%>
204
- </Changes>
205
- </ChangeBatch>
206
- </ChangeResourceRecordSetsRequest>
207
- XML
176
+ post_changes([[records_list, action]])
208
177
  end
209
178
 
179
+
210
180
  def rdata_for_api(record)
211
181
  return record.rdata unless record.type == "TXT"
212
182
 
@@ -218,9 +188,21 @@ module Zonesync
218
188
  end
219
189
 
220
190
  def to_records(el)
191
+ name = normalize_trailing_period(get_value(el, "Name"))
192
+ type = get_value(el, "Type")
193
+
194
+ alias_target = el.elements["AliasTarget"]
195
+ if alias_target && %w[A AAAA].include?(type)
196
+ return [AliasRecord.build(
197
+ name: name,
198
+ type: type,
199
+ dns_name: normalize_trailing_period(get_value(alias_target, "DNSName")).downcase,
200
+ hosted_zone_id: get_value(alias_target, "HostedZoneId"),
201
+ evaluate_target_health: get_value(alias_target, "EvaluateTargetHealth") == "true",
202
+ )]
203
+ end
204
+
221
205
  el.elements.collect("ResourceRecords/ResourceRecord") do |rr|
222
- name = normalize_trailing_period(get_value(el, "Name"))
223
- type = get_value(el, "Type")
224
206
  ttl = get_value(el, "TTL")
225
207
  rdata = get_value(rr, "Value")
226
208
  rdata = normalize_txt_rdata(rdata) if type == "TXT"
data/lib/zonesync/sync.rb CHANGED
@@ -5,6 +5,7 @@ require "zonesync/logger"
5
5
  module Zonesync
6
6
  Sync = Struct.new(:source, :destination) do
7
7
  def call(dry_run: false, force: false)
8
+ source = destination.interpret(self.source)
8
9
  operations = destination.diff!(source, force: force)
9
10
 
10
11
  smanifest = source.manifest.generate
@@ -111,7 +111,7 @@ module Zonesync
111
111
  def find_excluded_record(record)
112
112
  removed = operations.filter_map { |method, (r)| r if method == :remove }
113
113
  destination.records.find do |r|
114
- r.excludes?(record) && removed.none? { |x| x.identical_to?(r) }
114
+ destination.excludes?(r, record) && removed.none? { |x| x.identical_to?(r) }
115
115
  end
116
116
  end
117
117
 
@@ -121,7 +121,7 @@ module Zonesync
121
121
  next if expected_hashes.include?(RecordHash.generate(r))
122
122
  next if r.identical_to?(record)
123
123
 
124
- r.conflicts_with?(record)
124
+ r.conflicts_with?(record) || record.conflicts_with?(r)
125
125
  end
126
126
  end
127
127
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zonesync
4
- VERSION = "0.14.5"
4
+ VERSION = "0.15.0"
5
5
  end
@@ -98,8 +98,8 @@ grammar Zonefile
98
98
 
99
99
  rule a_record
100
100
  (
101
- host space ms_age ttl klass "A" space ip_address /
102
- host space ms_age klass ttl "A" space ip_address
101
+ host space ms_age ttl klass "A" space ip_address:(fqdn / ip_address) /
102
+ host space ms_age klass ttl "A" space ip_address:(fqdn / ip_address)
103
103
  ) {
104
104
  def to_s
105
105
  "#{host} #{ms_age} #{ttl} #{klass} A #{ip_address}"
@@ -121,8 +121,8 @@ grammar Zonefile
121
121
 
122
122
  rule aaaa_record
123
123
  (
124
- host space ms_age ttl klass "AAAA" space ip_address:ip6_address /
125
- host space ms_age klass ttl "AAAA" space ip_address:ip6_address
124
+ host space ms_age ttl klass "AAAA" space ip_address:(fqdn / ip6_address) /
125
+ host space ms_age klass ttl "AAAA" space ip_address:(fqdn / ip6_address)
126
126
  ) {
127
127
  def to_s
128
128
  "#{host} #{ttl} #{klass} AAAA #{ip_address}"
@@ -134,6 +134,14 @@ grammar Zonefile
134
134
  }
135
135
  end
136
136
 
137
+ rule fqdn
138
+ '[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*\.(?![a-zA-Z0-9\-_])'r {
139
+ def to_s
140
+ text_value
141
+ end
142
+ }
143
+ end
144
+
137
145
  rule ip6_address
138
146
  [\da-fA-F:.] 2..39 {
139
147
  def to_s
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zonesync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.5
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  - James Ottaway
9
- autorequire:
10
9
  bindir: exe
11
10
  cert_chain: []
12
- date: 2026-11-14 00:00:00.000000000 Z
11
+ date: 1980-01-02 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: thor
@@ -115,6 +114,8 @@ files:
115
114
  - lib/zonesync/record_hash.rb
116
115
  - lib/zonesync/repair.rb
117
116
  - lib/zonesync/route53.rb
117
+ - lib/zonesync/route53/alias_record.rb
118
+ - lib/zonesync/route53/source.rb
118
119
  - lib/zonesync/sync.rb
119
120
  - lib/zonesync/validator.rb
120
121
  - lib/zonesync/version.rb
@@ -128,7 +129,6 @@ licenses:
128
129
  metadata:
129
130
  homepage_uri: https://github.com/botandrose/zonesync
130
131
  source_code_uri: https://github.com/botandrose/zonesync
131
- post_install_message:
132
132
  rdoc_options: []
133
133
  require_paths:
134
134
  - lib
@@ -143,8 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  - !ruby/object:Gem::Version
144
144
  version: '0'
145
145
  requirements: []
146
- rubygems_version: 3.5.11
147
- signing_key:
146
+ rubygems_version: 4.0.20
148
147
  specification_version: 4
149
148
  summary: Sync your Zone file with your DNS host
150
149
  test_files: []