From 3580073fda5557af963351bd8e96c1a8307884b9 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Wed, 28 Jun 2023 17:59:13 +0200 Subject: [PATCH] Verify accepted domain templates at startup. --- src/config.cr | 3 +++ src/main.cr | 25 +++++++++++++++++++++++++ src/storage/zone.cr | 18 +++++++++--------- templates/example.com.json | 1 + 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 templates/example.com.json diff --git a/src/config.cr b/src/config.cr index 2f75706..8a461bc 100644 --- a/src/config.cr +++ b/src/config.cr @@ -7,6 +7,9 @@ class Baguette::Configuration property recreate_indexes : Bool = false property storage_directory : String = "storage" + # List of all accepted domains. Example: netlib.re. + property accepted_domains : Array(String)? = nil + # New domains require to load a template so the user won't have to enter some # necessary entries himself. For example, SOA and NS RRs should be pre-loaded # and updated by an administrator when required. diff --git a/src/main.cr b/src/main.cr index 17335e5..6bbe16a 100644 --- a/src/main.cr +++ b/src/main.cr @@ -213,6 +213,31 @@ def main end end + dir = configuration.template_directory + accepted_domains = configuration.accepted_domains + + unless accepted_domains + Baguette::Log.error "Not even a single accepted domain configured. Probably an error." + exit 1 + end + + accepted_domains.each do |domain| + template_file = "#{dir}/#{domain}.json" + Baguette::Log.error "TODO: check for #{template_file}" + zone = DNSManager::Storage::Zone.from_json File.read "#{template_file}" + puts "zone #{domain}: #{zone}" + rescue e + Baguette::Log.error "error reading template #{template_file}: #{e}" + exit 1 + #if File.exists? "./templates" + #else + # Baguette::Log.error "tried template directory './templates', but doesn't exist either" + # exit 1 + #end + end + + exit 1 + if simulation pp! configuration exit 0 diff --git a/src/storage/zone.cr b/src/storage/zone.cr index b075cde..4363603 100644 --- a/src/storage/zone.cr +++ b/src/storage/zone.cr @@ -21,15 +21,15 @@ class DNSManager::Storage::Zone include JSON::Serializable use_json_discriminator "rrtype", { - a: A, - aaaa: AAAA, - soa: SOA, - txt: TXT, - ptr: PTR, - ns: NS, - cname: CNAME, - mx: MX, - srv: SRV + A: A, + AAAA: AAAA, + SOA: SOA, + TXT: TXT, + PTR: PTR, + NS: NS, + CNAME: CNAME, + MX: MX, + SRV: SRV } # Used to discriminate between classes. diff --git a/templates/example.com.json b/templates/example.com.json new file mode 100644 index 0000000..2547972 --- /dev/null +++ b/templates/example.com.json @@ -0,0 +1 @@ +{"domain":"example.com","current_rrid":3,"resources":[{"rrtype":"SOA","rrid":0,"name":"example.com.","ttl":3600,"target":"","readonly":true,"mname":"ns0.example.com.","rname":"dnsmaster.example.com.","serial":2023070100,"refresh":3600,"retry":600,"expire":2419200,"minttl":600},{"rrtype":"NS","rrid":1,"name":"example.com.","ttl":3600,"target":"ns0.arn-fai.net.","readonly":true},{"rrtype":"NS","rrid":2,"name":"example.com.","ttl":3600,"target":"alsace.tetaneutral.net.","readonly":true}]} \ No newline at end of file