dns: dual-write toggles + drift fact
ci/woodpecker/pr/ruby-validate Pipeline was successful
ci/woodpecker/pr/puppet-lint Pipeline was successful
ci/woodpecker/pr/yamllint Pipeline was successful
ci/woodpecker/pr/bolt-validate Pipeline was successful
ci/woodpecker/pr/erb-validate Pipeline was successful
ci/woodpecker/pr/epp-validate Pipeline was successful
ci/woodpecker/pr/ruby-check Pipeline was successful
ci/woodpecker/pr/puppet-validate Pipeline was successful
ci/woodpecker/pr/ruby-validate Pipeline was successful
ci/woodpecker/pr/puppet-lint Pipeline was successful
ci/woodpecker/pr/yamllint Pipeline was successful
ci/woodpecker/pr/bolt-validate Pipeline was successful
ci/woodpecker/pr/erb-validate Pipeline was successful
ci/woodpecker/pr/epp-validate Pipeline was successful
ci/woodpecker/pr/ruby-check Pipeline was successful
ci/woodpecker/pr/puppet-validate Pipeline was successful
Publish records both ways during the k8s cutover, and expose expected vs deployed records for drift detection. - profiles::dns::updater + ::record: manage_nsupdate and manage_export booleans (both default on); export keeps the legacy master flow, so disable it once k8s is authoritative - dns_records fact: parses the expected records file and digs the authoritative server for each, reporting expected / in_sync / drift (plus dns_records_insync boolean); updater writes the server address to /var/lib/dns-updater/server for the fact - hiera: manage_export/manage_nsupdate = true (cutover)
This commit is contained in:
+10
-4
@@ -209,10 +209,16 @@ profiles::dns::base::nameservers:
|
|||||||
- 198.18.19.16
|
- 198.18.19.16
|
||||||
profiles::dns::master::basedir: '/var/named/sources'
|
profiles::dns::master::basedir: '/var/named/sources'
|
||||||
|
|
||||||
# dns::updater nsupdates host records to the k8s authoritative write endpoint
|
# dns record publishing. During the k8s cutover both methods run; set
|
||||||
# (bind-authoritative-primary). Inert until the TSIG key is set in eyaml:
|
# manage_export false once k8s is authoritative.
|
||||||
# profiles::dns::updater::key_secret: ENC[...] (must match the key the
|
# - export: legacy exported-resources -> puppet DNS master
|
||||||
# bind-authoritative zones allow-update with; algorithm hmac-sha256)
|
# - nsupdate: RFC2136 to the k8s bind-authoritative write endpoint (.9),
|
||||||
|
# inert until the TSIG key is set in eyaml:
|
||||||
|
# profiles::dns::updater::key_secret: ENC[...]
|
||||||
|
# (must match the key the bind-authoritative zones allow-update
|
||||||
|
# with; algorithm hmac-sha256)
|
||||||
|
profiles::dns::updater::manage_export: true
|
||||||
|
profiles::dns::updater::manage_nsupdate: true
|
||||||
profiles::dns::updater::server: '198.18.200.9'
|
profiles::dns::updater::server: '198.18.200.9'
|
||||||
profiles::dns::updater::key_name: 'client-update'
|
profiles::dns::updater::key_name: 'client-update'
|
||||||
profiles::dns::updater::key_algorithm: 'hmac-sha256'
|
profiles::dns::updater::key_algorithm: 'hmac-sha256'
|
||||||
|
|||||||
@@ -0,0 +1,88 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# lib/facter/dns_records.rb
|
||||||
|
#
|
||||||
|
# Reports this host's expected DNS records (assembled by profiles::dns::updater
|
||||||
|
# into its records file) versus what is currently deployed on the authoritative
|
||||||
|
# server, so puppet can detect drift and re-apply.
|
||||||
|
#
|
||||||
|
# Structured value:
|
||||||
|
# { server, count, expected => [{zone,fqdn,type,ttl,value}], in_sync,
|
||||||
|
# drift => [{...,deployed => [...]}] }
|
||||||
|
|
||||||
|
# Helpers for the dns_records fact.
|
||||||
|
module DnsRecordsFact
|
||||||
|
RECORDS_FILE = '/var/lib/dns-updater/records'
|
||||||
|
SERVER_FILE = '/var/lib/dns-updater/server'
|
||||||
|
|
||||||
|
module_function
|
||||||
|
|
||||||
|
# normalise a value for comparison: strip, drop trailing dot, downcase
|
||||||
|
def norm(value)
|
||||||
|
value.to_s.strip.chomp('.').downcase
|
||||||
|
end
|
||||||
|
|
||||||
|
def server
|
||||||
|
File.exist?(SERVER_FILE) ? File.read(SERVER_FILE).strip : nil
|
||||||
|
end
|
||||||
|
|
||||||
|
# a name relative to a zone (or @) as a fully-qualified name
|
||||||
|
def to_fqdn(name, zone)
|
||||||
|
return "#{zone}." if name.to_s.empty? || name == '@'
|
||||||
|
|
||||||
|
"#{name}.#{zone}."
|
||||||
|
end
|
||||||
|
|
||||||
|
# parse one "zone|name|type|ttl|value" line into a record hash (nil to skip)
|
||||||
|
def parse_line(line)
|
||||||
|
line = line.strip
|
||||||
|
return nil if line.empty? || line.start_with?('#')
|
||||||
|
|
||||||
|
zone, name, type, ttl, value = line.split('|', 5)
|
||||||
|
return nil unless zone && type && value
|
||||||
|
|
||||||
|
{ 'zone' => zone, 'fqdn' => to_fqdn(name, zone), 'type' => type, 'ttl' => ttl, 'value' => value }
|
||||||
|
end
|
||||||
|
|
||||||
|
# parse the records file into record hashes
|
||||||
|
def expected
|
||||||
|
return [] unless File.exist?(RECORDS_FILE)
|
||||||
|
|
||||||
|
File.readlines(RECORDS_FILE).filter_map { |line| parse_line(line) }
|
||||||
|
end
|
||||||
|
|
||||||
|
# the values currently deployed for a record, per the authoritative server
|
||||||
|
def deployed(record, srv)
|
||||||
|
cmd = ['dig', '+short', '+time=2', '+tries=1']
|
||||||
|
cmd << "@#{srv}" if srv && !srv.empty?
|
||||||
|
cmd += [record['fqdn'], record['type']]
|
||||||
|
out = Facter::Core::Execution.execute(cmd.join(' '), on_fail: '')
|
||||||
|
out.to_s.split("\n").map { |line| norm(line) }.reject(&:empty?)
|
||||||
|
end
|
||||||
|
|
||||||
|
def report
|
||||||
|
srv = server
|
||||||
|
exp = expected
|
||||||
|
drift = exp.filter_map do |record|
|
||||||
|
dep = deployed(record, srv)
|
||||||
|
record.merge('deployed' => dep) unless dep.include?(norm(record['value']))
|
||||||
|
end
|
||||||
|
{ 'server' => srv, 'count' => exp.length, 'expected' => exp, 'in_sync' => drift.empty?, 'drift' => drift }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Facter.add(:dns_records) do
|
||||||
|
confine kernel: 'Linux'
|
||||||
|
setcode do
|
||||||
|
File.exist?(DnsRecordsFact::RECORDS_FILE) ? DnsRecordsFact.report : nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Convenience boolean for `if $facts['dns_records_insync']` guards.
|
||||||
|
Facter.add(:dns_records_insync) do
|
||||||
|
confine kernel: 'Linux'
|
||||||
|
setcode do
|
||||||
|
v = Facter.value(:dns_records)
|
||||||
|
v.nil? ? nil : v['in_sync']
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
# profiles::dns::record
|
# profiles::dns::record
|
||||||
#
|
#
|
||||||
# Declares a DNS record for this host. The record is written to the local
|
# Declares a DNS record for this host. Publishes it via either or both methods,
|
||||||
# dns-updater records file (profiles::dns::updater), which nsupdates it to the
|
# controlled by profiles::dns::updater's toggles (both on during cutover):
|
||||||
# authoritative DNS server. This replaces the old flow that exported a
|
# - nsupdate: a local concat fragment consumed by profiles::dns::updater,
|
||||||
# @@concat::fragment to the puppet DNS master.
|
# which nsupdates it to the authoritative server.
|
||||||
|
# - export: the legacy @@concat::fragment exported to the puppet DNS master.
|
||||||
define profiles::dns::record (
|
define profiles::dns::record (
|
||||||
String $record,
|
String $record,
|
||||||
Enum[
|
Enum[
|
||||||
@@ -22,10 +23,22 @@ define profiles::dns::record (
|
|||||||
) {
|
) {
|
||||||
include profiles::dns::updater
|
include profiles::dns::updater
|
||||||
|
|
||||||
# zone|name|type|ttl|value (parsed by the dns-update script)
|
# new: local records file consumed by the nsupdate service
|
||||||
concat::fragment { "dns-record-${name}":
|
if $profiles::dns::updater::manage_nsupdate {
|
||||||
target => $profiles::dns::updater::records_file,
|
# zone|name|type|ttl|value (parsed by the dns-update script)
|
||||||
content => "${zone}|${record}|${type}|${ttl}|${value}\n",
|
concat::fragment { "dns-record-${name}":
|
||||||
order => sprintf('%03d', $order),
|
target => $profiles::dns::updater::records_file,
|
||||||
|
content => "${zone}|${record}|${type}|${ttl}|${value}\n",
|
||||||
|
order => sprintf('%03d', $order),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# legacy: export the fragment to the puppet DNS master
|
||||||
|
if $profiles::dns::updater::manage_export {
|
||||||
|
@@concat::fragment { "${zone}_${name}":
|
||||||
|
target => "${profiles::dns::updater::master_basedir}/${zone}.conf",
|
||||||
|
content => "${record} IN ${type} ${value}\n",
|
||||||
|
order => $order,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,111 +1,127 @@
|
|||||||
# profiles::dns::updater
|
# profiles::dns::updater
|
||||||
#
|
#
|
||||||
# Applies this host's DNS records to the authoritative DNS server via TSIG
|
# Publishes this host's DNS records. Two methods, independently toggled so both
|
||||||
# nsupdate, replacing the old exported-resources -> master zone-file flow.
|
# can run during the k8s cutover (profiles::dns::record honours the same flags):
|
||||||
#
|
#
|
||||||
# profiles::dns::record fragments are assembled into $records_file; a systemd
|
# - nsupdate ($manage_nsupdate): assemble the records into a local file and
|
||||||
# .path unit watches that file and runs dns-update.service (nsupdate) whenever
|
# nsupdate them to the k8s authoritative write endpoint via a systemd .path
|
||||||
# it changes. nsupdate comes from bind-utils (installed via bind::updater in
|
# unit that watches the file. Inert until $key_secret (TSIG) is set.
|
||||||
|
# - export ($manage_export): the legacy exported-resources flow to the puppet
|
||||||
|
# DNS master. Kept during cutover; disable once k8s is authoritative.
|
||||||
|
#
|
||||||
|
# nsupdate comes from bind-utils (installed via bind::updater in
|
||||||
# profiles::dns::base).
|
# profiles::dns::base).
|
||||||
#
|
|
||||||
# Inert until $key_secret is set (the shared TSIG key that the k8s
|
|
||||||
# bind-authoritative zones allow-update with): the records file is still
|
|
||||||
# assembled, but the updater service is not managed, so nodes are safe before
|
|
||||||
# the key is provisioned.
|
|
||||||
class profiles::dns::updater (
|
class profiles::dns::updater (
|
||||||
String $server = '198.18.200.9',
|
Boolean $manage_nsupdate = true,
|
||||||
String $key_name = 'client-update',
|
Boolean $manage_export = true,
|
||||||
String $key_algorithm = 'hmac-sha256',
|
String $server = '198.18.200.9',
|
||||||
Optional[Sensitive[String]] $key_secret = undef,
|
String $key_name = 'client-update',
|
||||||
Integer $default_ttl = 300,
|
String $key_algorithm = 'hmac-sha256',
|
||||||
Stdlib::AbsolutePath $records_file = '/var/lib/dns-updater/records',
|
Optional[Sensitive[String]] $key_secret = undef,
|
||||||
Stdlib::AbsolutePath $state_dir = '/var/lib/dns-updater',
|
Integer $default_ttl = 300,
|
||||||
Stdlib::AbsolutePath $config_dir = '/etc/dns-updater',
|
Stdlib::AbsolutePath $records_file = '/var/lib/dns-updater/records',
|
||||||
|
Stdlib::AbsolutePath $state_dir = '/var/lib/dns-updater',
|
||||||
|
Stdlib::AbsolutePath $config_dir = '/etc/dns-updater',
|
||||||
|
Stdlib::AbsolutePath $master_basedir = lookup('profiles::dns::master::basedir'),
|
||||||
) {
|
) {
|
||||||
|
|
||||||
$state_file = "${state_dir}/applied"
|
$state_file = "${state_dir}/applied"
|
||||||
$key_file = "${config_dir}/key"
|
$server_file = "${state_dir}/server"
|
||||||
|
$key_file = "${config_dir}/key"
|
||||||
|
|
||||||
file { $state_dir:
|
if $manage_nsupdate {
|
||||||
ensure => directory,
|
|
||||||
owner => 'root',
|
|
||||||
group => 'root',
|
|
||||||
mode => '0755',
|
|
||||||
}
|
|
||||||
|
|
||||||
# Records file, assembled from profiles::dns::record fragments.
|
file { $state_dir:
|
||||||
concat { $records_file:
|
|
||||||
ensure => present,
|
|
||||||
owner => 'root',
|
|
||||||
group => 'root',
|
|
||||||
mode => '0644',
|
|
||||||
ensure_newline => true,
|
|
||||||
warn => false,
|
|
||||||
require => File[$state_dir],
|
|
||||||
}
|
|
||||||
|
|
||||||
concat::fragment { 'dns-update-header':
|
|
||||||
target => $records_file,
|
|
||||||
content => "# Managed by puppet (profiles::dns::record): zone|name|type|ttl|value\n",
|
|
||||||
order => '00',
|
|
||||||
}
|
|
||||||
|
|
||||||
if $key_secret =~ Undef {
|
|
||||||
notify { 'dns-updater-inert':
|
|
||||||
message => 'profiles::dns::updater: key_secret unset; records assembled but not applied.',
|
|
||||||
loglevel => 'info',
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
file { $config_dir:
|
|
||||||
ensure => directory,
|
ensure => directory,
|
||||||
owner => 'root',
|
owner => 'root',
|
||||||
group => 'root',
|
group => 'root',
|
||||||
mode => '0700',
|
mode => '0755',
|
||||||
}
|
}
|
||||||
|
|
||||||
file { $key_file:
|
# Server address, read by the dns_records fact for drift detection.
|
||||||
ensure => file,
|
file { $server_file:
|
||||||
owner => 'root',
|
|
||||||
group => 'root',
|
|
||||||
mode => '0600',
|
|
||||||
show_diff => false,
|
|
||||||
content => Sensitive(epp('profiles/dns/tsig-key.epp', {
|
|
||||||
'name' => $key_name,
|
|
||||||
'algorithm' => $key_algorithm,
|
|
||||||
'secret' => $key_secret.unwrap,
|
|
||||||
})),
|
|
||||||
}
|
|
||||||
|
|
||||||
file { '/usr/local/bin/dns-update':
|
|
||||||
ensure => file,
|
ensure => file,
|
||||||
owner => 'root',
|
owner => 'root',
|
||||||
group => 'root',
|
group => 'root',
|
||||||
mode => '0755',
|
mode => '0644',
|
||||||
content => epp('profiles/dns/dns-update.sh.epp', {
|
content => "${server}\n",
|
||||||
'server' => $server,
|
require => File[$state_dir],
|
||||||
'key_file' => $key_file,
|
|
||||||
'records_file' => $records_file,
|
|
||||||
'state_file' => $state_file,
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
systemd::unit_file { 'dns-update.service':
|
# Records file, assembled from profiles::dns::record fragments.
|
||||||
content => epp('profiles/dns/dns-update.service.epp', { 'script' => '/usr/local/bin/dns-update' }),
|
concat { $records_file:
|
||||||
|
ensure => present,
|
||||||
|
owner => 'root',
|
||||||
|
group => 'root',
|
||||||
|
mode => '0644',
|
||||||
|
ensure_newline => true,
|
||||||
|
warn => false,
|
||||||
|
require => File[$state_dir],
|
||||||
}
|
}
|
||||||
|
|
||||||
# The .path unit watches the records file and triggers the service.
|
concat::fragment { 'dns-update-header':
|
||||||
systemd::unit_file { 'dns-update.path':
|
target => $records_file,
|
||||||
content => epp('profiles/dns/dns-update.path.epp', { 'records_file' => $records_file }),
|
content => "# Managed by puppet (profiles::dns::record): zone|name|type|ttl|value\n",
|
||||||
active => true,
|
order => '00',
|
||||||
enable => true,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Also apply within the puppet run whenever the records change.
|
if $key_secret =~ Undef {
|
||||||
exec { 'dns-update-apply':
|
notify { 'dns-updater-inert':
|
||||||
command => '/usr/local/bin/dns-update',
|
message => 'profiles::dns::updater: key_secret unset; records assembled but not applied.',
|
||||||
refreshonly => true,
|
loglevel => 'info',
|
||||||
subscribe => Concat[$records_file],
|
}
|
||||||
require => [File['/usr/local/bin/dns-update'], File[$key_file]],
|
} else {
|
||||||
|
file { $config_dir:
|
||||||
|
ensure => directory,
|
||||||
|
owner => 'root',
|
||||||
|
group => 'root',
|
||||||
|
mode => '0700',
|
||||||
|
}
|
||||||
|
|
||||||
|
file { $key_file:
|
||||||
|
ensure => file,
|
||||||
|
owner => 'root',
|
||||||
|
group => 'root',
|
||||||
|
mode => '0600',
|
||||||
|
show_diff => false,
|
||||||
|
content => Sensitive(epp('profiles/dns/tsig-key.epp', {
|
||||||
|
'name' => $key_name,
|
||||||
|
'algorithm' => $key_algorithm,
|
||||||
|
'secret' => $key_secret.unwrap,
|
||||||
|
})),
|
||||||
|
}
|
||||||
|
|
||||||
|
file { '/usr/local/bin/dns-update':
|
||||||
|
ensure => file,
|
||||||
|
owner => 'root',
|
||||||
|
group => 'root',
|
||||||
|
mode => '0755',
|
||||||
|
content => epp('profiles/dns/dns-update.sh.epp', {
|
||||||
|
'server' => $server,
|
||||||
|
'key_file' => $key_file,
|
||||||
|
'records_file' => $records_file,
|
||||||
|
'state_file' => $state_file,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
systemd::unit_file { 'dns-update.service':
|
||||||
|
content => epp('profiles/dns/dns-update.service.epp', { 'script' => '/usr/local/bin/dns-update' }),
|
||||||
|
}
|
||||||
|
|
||||||
|
# The .path unit watches the records file and triggers the service.
|
||||||
|
systemd::unit_file { 'dns-update.path':
|
||||||
|
content => epp('profiles/dns/dns-update.path.epp', { 'records_file' => $records_file }),
|
||||||
|
active => true,
|
||||||
|
enable => true,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Also apply within the puppet run whenever the records change.
|
||||||
|
exec { 'dns-update-apply':
|
||||||
|
command => '/usr/local/bin/dns-update',
|
||||||
|
refreshonly => true,
|
||||||
|
subscribe => Concat[$records_file],
|
||||||
|
require => [File['/usr/local/bin/dns-update'], File[$key_file]],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user