From 9449ba336f50a2626273501dee6fc0a9981c1eed Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 7 May 2023 21:05:53 +0200 Subject: [PATCH] Get a zone. --- src/client/lib/dnsmanager-client.cr | 7 +++++++ src/client/main.cr | 14 ++++++++++++++ src/client/parser.cr | 7 +++++++ src/requests/zone.cr | 16 +++++++++++++++- src/responses/zone.cr | 15 ++++++++++++++- src/storage.cr | 25 +++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/client/lib/dnsmanager-client.cr b/src/client/lib/dnsmanager-client.cr index f1b806f..f7c61ac 100644 --- a/src/client/lib/dnsmanager-client.cr +++ b/src/client/lib/dnsmanager-client.cr @@ -46,6 +46,13 @@ class DNSManager::Client < IPC parse_message [ Response::Success ], read end + # Get a zone. + def user_zone_get(domain : String) + request = DNSManager::Request::GetZone.new domain + send_now request + parse_message [ Response::Zone ], read + end + # Get user domain list. def user_domain_list() request = DNSManager::Request::UserDomains.new diff --git a/src/client/main.cr b/src/client/main.cr index 2dff6d0..2314cee 100644 --- a/src/client/main.cr +++ b/src/client/main.cr @@ -37,8 +37,10 @@ class Actions # Maintenance @the_call["admin-maintenance"] = ->admin_maintenance + @the_call["user-zone-add"] = ->user_zone_add @the_call["user-zone-del"] = ->user_zone_del + @the_call["user-zone-get"] = ->user_zone_get @the_call["user-domain-list"] = ->user_domain_list end @@ -93,6 +95,18 @@ class Actions end end + def user_zone_get + domains = Context.args.not_nil! + domains.each do |domain| + begin + pp! domain + pp! @dnsmanagerd.user_zone_get domain + rescue e + puts "error for user_zone_get: #{e.message}" + end + end + end + def user_domain_list response = @dnsmanagerd.user_domain_list case response diff --git a/src/client/parser.cr b/src/client/parser.cr index 565f57c..f2602e5 100644 --- a/src/client/parser.cr +++ b/src/client/parser.cr @@ -128,6 +128,13 @@ def parsing_cli(authd_config : Baguette::Configuration::Auth) unrecognized_args_to_context_args.call parser, nil, 1 end + parser.on("get", "Get a zone.") do + Baguette::Log.info "get zone." + Context.command = "user-zone-get" + parser.banner = "COMMAND: user zone get domain [domain...]" + unrecognized_args_to_context_args.call parser, nil, 1 + end + parser.on("list", "List all domains.") do Baguette::Log.info "List domains." Context.command = "user-domain-list" diff --git a/src/requests/zone.cr b/src/requests/zone.cr index 23e6f2b..315d73e 100644 --- a/src/requests/zone.cr +++ b/src/requests/zone.cr @@ -31,7 +31,21 @@ class DNSManager::Request end DNSManager.requests << DeleteZone - IPC::JSON.message UserDomains, 12 do + IPC::JSON.message GetZone, 12 do + property domain : String + + def initialize(@domain) + end + + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON + user = dnsmanagerd.get_logged_user event + raise NotLoggedException.new if user.nil? + dnsmanagerd.storage.get_zone user.uid, @domain + end + end + DNSManager.requests << GetZone + + IPC::JSON.message UserDomains, 13 do def initialize() end diff --git a/src/responses/zone.cr b/src/responses/zone.cr index 5e27305..ba5b33f 100644 --- a/src/responses/zone.cr +++ b/src/responses/zone.cr @@ -15,7 +15,20 @@ class DNSManager::Response end DNSManager.responses << DomainChanged - IPC::JSON.message DomainList, 12 do + IPC::JSON.message Zone, 12 do + property zone : Storage::Zone + def initialize(@zone) + end + end + DNSManager.responses << Zone + + IPC::JSON.message UnknownZone, 13 do + def initialize() + end + end + DNSManager.responses << UnknownZone + + IPC::JSON.message DomainList, 14 do property domains : Array(String) def initialize(@domains) end diff --git a/src/storage.cr b/src/storage.cr index 464f6ed..d78e93a 100644 --- a/src/storage.cr +++ b/src/storage.cr @@ -121,6 +121,31 @@ class DNSManager::Storage Response::Error.new "error while deleting the domain #{domain}" end + def get_zone(user_id : Int32, domain : String) : IPC::JSON + # User must exist. + user_data = user_data_by_uid.get? user_id.to_s + unless user_data + Baguette::Log.warning "unknown user #{user_id} tries to get zone #{domain}" + return Response::UnknownUser.new + end + + # User must own the domain. + unless user_data.domains.includes? domain + Baguette::Log.warning "user #{user_id} tries to get zone #{domain} but doesn't own it" + return Response::NoOwnership.new + end + + zone = zones_by_domain.get? domain + unless zone + return Response::UnknownZone.new + end + + Response::Zone.new zone + rescue e + Baguette::Log.error "trying to get a zone #{domain}: #{e}" + Response::Error.new "error while accessing a zone #{domain}" + end + def user_domains(user_id : Int32) : IPC::JSON # User must exist. user_data = user_data_by_uid.get? user_id.to_s