diff --git a/src/network.cr b/src/network.cr index b714a0b..4146657 100644 --- a/src/network.cr +++ b/src/network.cr @@ -5,6 +5,10 @@ class IPC::JSON def handle(service : IPC, event : IPC::Event) raise "unimplemented" end + + def to_s(io : IO) + io << self.class.name.sub /[^:]+::[^:]+::/, "" + end end module DNSManager diff --git a/src/requests/admin.cr b/src/requests/admin.cr index 65f46ea..6cf0e0a 100644 --- a/src/requests/admin.cr +++ b/src/requests/admin.cr @@ -28,6 +28,11 @@ class DNSManager::Request def initialize(@subject) end + def to_s(io : IO) + super io + io << " (subject: #{@subject}, int: #{@int}, string: #{@string})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user @@ -72,6 +77,11 @@ class DNSManager::Request def initialize(@domain) end + def to_s(io : IO) + super io + io << " (domain: #{@domain})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user diff --git a/src/requests/migration.cr b/src/requests/migration.cr index b603f20..9b4801f 100644 --- a/src/requests/migration.cr +++ b/src/requests/migration.cr @@ -10,6 +10,11 @@ class DNSManager::Request def initialize(@login, @domain) end + def to_s(io : IO) + super io + io << " (login: #{@login}, domain: #{@domain})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user diff --git a/src/requests/ownership.cr b/src/requests/ownership.cr index c6076e0..2ac472e 100644 --- a/src/requests/ownership.cr +++ b/src/requests/ownership.cr @@ -5,6 +5,11 @@ class DNSManager::Request def initialize(@domain) end + def to_s(io : IO) + super io + io << " (domain: #{@domain})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user @@ -20,6 +25,11 @@ class DNSManager::Request def initialize(@domain) end + def to_s(io : IO) + super io + io << " (domain: #{@domain})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user @@ -35,6 +45,11 @@ class DNSManager::Request def initialize(@domain) end + def to_s(io : IO) + super io + io << " (domain: #{@domain})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user @@ -50,6 +65,11 @@ class DNSManager::Request def initialize(@uuid) end + def to_s(io : IO) + super io + io << " (uuid: #{@uuid})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user diff --git a/src/requests/token.cr b/src/requests/token.cr index 769f3a4..ad466a5 100644 --- a/src/requests/token.cr +++ b/src/requests/token.cr @@ -9,6 +9,11 @@ class DNSManager::Request def initialize(@domain, @rrid) end + def to_s(io : IO) + super io + io << " (domain: #{@domain}, rrid: #{@rrid})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user @@ -24,6 +29,11 @@ class DNSManager::Request def initialize(@token, @address) end + def to_s(io : IO) + super io + io << " (token: #{@token[0..15]}..., address: #{@address})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON dnsmanagerd.storage.use_token @token, @address end diff --git a/src/requests/user.cr b/src/requests/user.cr index ab195a7..8bc9dfd 100644 --- a/src/requests/user.cr +++ b/src/requests/user.cr @@ -44,6 +44,11 @@ class DNSManager::Request def initialize(@user_id = nil) end + def to_s(io : IO) + super io + io << " (user_id: #{@user_id})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user diff --git a/src/requests/zone.cr b/src/requests/zone.cr index 4128400..d389b05 100644 --- a/src/requests/zone.cr +++ b/src/requests/zone.cr @@ -9,6 +9,11 @@ class DNSManager::Request def initialize(@domain) end + def to_s(io : IO) + super io + io << " (domain: #{@domain})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user @@ -24,6 +29,11 @@ class DNSManager::Request def initialize(@domain) end + def to_s(io : IO) + super io + io << " (domain: #{@domain})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user @@ -38,6 +48,11 @@ class DNSManager::Request def initialize(@zone) end + def to_s(io : IO) + super io + io << " (zone: #{@zone})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user @@ -52,6 +67,11 @@ class DNSManager::Request def initialize(@domain) end + def to_s(io : IO) + super io + io << " (domain: #{@domain})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user @@ -77,6 +97,11 @@ class DNSManager::Request property domain : String property rr : DNSManager::Storage::Zone::ResourceRecord + def to_s(io : IO) + super io + io << " (domain: #{@domain}, rr: #{@rr.to_simple_s})" + end + def initialize(@domain, @rr) end @@ -95,6 +120,11 @@ class DNSManager::Request def initialize(@domain, @rr) end + def to_s(io : IO) + super io + io << " (domain: #{@domain}, rr: #{@rr.to_simple_s})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user @@ -110,6 +140,11 @@ class DNSManager::Request def initialize(@domain, @rrid) end + def to_s(io : IO) + super io + io << " (domain: #{@domain}, rrid: #{@rrid})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user @@ -124,6 +159,11 @@ class DNSManager::Request def initialize(@domain) end + def to_s(io : IO) + super io + io << " (domain: #{@domain})" + end + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user diff --git a/src/responses/error.cr b/src/responses/error.cr index 245b1d5..24f00bf 100644 --- a/src/responses/error.cr +++ b/src/responses/error.cr @@ -4,6 +4,11 @@ class DNSManager::Response property reason : String | Array(String) def initialize(@reason) end + + def to_s(io : IO) + super io + io << " (reason: #{@reason})" + end end DNSManager.responses << Error diff --git a/src/responses/zone.cr b/src/responses/zone.cr index 9dae82e..ec92c05 100644 --- a/src/responses/zone.cr +++ b/src/responses/zone.cr @@ -3,6 +3,11 @@ class DNSManager::Response property domain : String def initialize(@domain) end + + def to_s(io : IO) + super io + io << " (domain: #{@domain})" + end end DNSManager.responses << DomainDeleted @@ -11,6 +16,11 @@ class DNSManager::Response property errors : Array(Storage::Zone::Error) def initialize(@errors) end + + def to_s(io : IO) + super io + io << " (errors: #{@errors.join ","})" + end end DNSManager.responses << InvalidZone @@ -18,6 +28,11 @@ class DNSManager::Response property domain : DNSManager::Storage::Domain def initialize(@domain) end + + def to_s(io : IO) + super io + io << " (domain: #{@domain.name})" + end end DNSManager.responses << DomainChanged @@ -25,6 +40,11 @@ class DNSManager::Response property zone : Storage::Zone def initialize(@zone) end + + def to_s(io : IO) + super io + io << " (zone: #{@zone})" + end end DNSManager.responses << Zone @@ -38,6 +58,11 @@ class DNSManager::Response property domains : Array(String) def initialize(@domains) end + + def to_s(io : IO) + super io + io << " (domains: #{@domains.join ","})" + end end DNSManager.responses << DomainList @@ -45,6 +70,11 @@ class DNSManager::Response property domains : Array(String) def initialize(@domains) end + + def to_s(io : IO) + super io + io << " (domains: #{@domains.join ","})" + end end DNSManager.responses << AcceptedDomains @@ -54,6 +84,11 @@ class DNSManager::Response property my_domains : Array(DNSManager::Storage::Domain) def initialize(@admin, @accepted_domains, @my_domains) end + + def to_s(io : IO) + super io + io << " (admin: #{@admin})" + end end DNSManager.responses << Logged @@ -61,6 +96,11 @@ class DNSManager::Response property domain : String def initialize(@domain) end + + def to_s(io : IO) + super io + io << " (domain: #{@domain})" + end end DNSManager.responses << DomainAdded @@ -68,6 +108,11 @@ class DNSManager::Response property rrid : UInt32 def initialize(@rrid) end + + def to_s(io : IO) + super io + io << " (rrid: #{@rrid})" + end end DNSManager.responses << RRDeleted @@ -76,6 +121,11 @@ class DNSManager::Response property rr : Storage::Zone::ResourceRecord def initialize(@domain, @rr) end + + def to_s(io : IO) + super io + io << " (domain: #{@domain}, rr: #{@rr.to_simple_s})" + end end DNSManager.responses << RRAdded @@ -84,6 +134,11 @@ class DNSManager::Response property errors : Array(Storage::Zone::Error) def initialize(@errors) end + + def to_s(io : IO) + super io + io << " (errors: #{@errors.join ","})" + end end DNSManager.responses << InvalidRR @@ -92,6 +147,11 @@ class DNSManager::Response property rr : Storage::Zone::ResourceRecord def initialize(@domain, @rr) end + + def to_s(io : IO) + super io + io << " (domain: #{@domain}, rr: #{@rr.to_simple_s})" + end end DNSManager.responses << RRUpdated @@ -100,6 +160,11 @@ class DNSManager::Response property rr : Storage::Zone::ResourceRecord def initialize(@domain, @rr) end + + def to_s(io : IO) + super io + io << " (domain: #{@domain}, rr: #{@rr.to_simple_s})" + end end DNSManager.responses << RRReadOnly @@ -108,6 +173,11 @@ class DNSManager::Response property zonefile : String def initialize(@domain, @zonefile) end + + def to_s(io : IO) + super io + io << " (domain: #{@domain})" + end end DNSManager.responses << GeneratedZone @@ -115,6 +185,11 @@ class DNSManager::Response property domains : Array(String) def initialize(@domains) end + + def to_s(io : IO) + super io + io << " (domains: #{@domains.join ","})" + end end DNSManager.responses << OrphanDomainList end diff --git a/src/service.cr b/src/service.cr index 199db12..36f1e33 100644 --- a/src/service.cr +++ b/src/service.cr @@ -19,7 +19,6 @@ module DNSManager end end -alias IPCMESSAGE = Baguette::Configuration::IPC::MESSAGE alias DNSMESSAGE = DNSManager::MESSAGE class DNSManager::Service < IPC @@ -51,7 +50,7 @@ class DNSManager::Service < IPC when AuthD::Response::Login uid = response.uid token = response.token - Baguette::Log.info "Authenticated as #{@configuration.login} #{uid}, token: #{token}" + Baguette::Log.info "Authenticated as #{@configuration.login} #{uid}, token: #{token[0..15]}..." else @authd.close raise "Cannot authenticate to authd with login #{@configuration.login}: #{response}." @@ -110,34 +109,34 @@ class DNSManager::Service < IPC response = begin request.handle self, event rescue e : AuthorizationException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{reqname} authorization error" + Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} authorization error" Response::Error.new "authorization error" rescue e : DomainNotFoundException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{reqname} domain not found" + Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} domain not found" Response::DomainNotFound.new rescue e : CannotCheckPermissionsException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{reqname} cannot check permissions of user '#{e.uid}' on resource '#{e.resource}'" + Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} cannot check permissions of user '#{e.uid}' on resource '#{e.resource}'" Response::InsufficientRights.new rescue e : UnknownUserException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{reqname} unknown user" + Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} unknown user" Response::UnknownUser.new rescue e : NoOwnershipException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{reqname} no ownership error" + Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} no ownership error" Response::NoOwnership.new rescue e : NotLoggedException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{reqname} user not logged" + Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} user not logged" Response::Error.new "user not logged" rescue e : RRNotFoundException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{reqname} RR not found" + Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} RR not found" Response::RRNotFound.new rescue e : TokenNotFoundException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{reqname} Token not found" + Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} Token not found" Response::Error.new "token not found" rescue e : RRReadOnlyException - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{reqname} RR is read only" + Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} RR is read only" Response::RRReadOnly.new e.domain, e.rr rescue e # Generic case - Baguette::Log.error "(fd #{"%4d" % event.fd}) #{reqname} generic error #{e}" + Baguette::Log.error "(fd #{"%4d" % event.fd}) #{request} generic error #{e}" Response::Error.new "generic error" end @@ -149,13 +148,11 @@ class DNSManager::Service < IPC duration = Time.utc - request_start - response_name = response.class.name.sub /^DNSManager::Response::/, "" - if response.is_a? DNSManager::Response::Error - Baguette::Log.warning "fd #{"%4d" % event.fd} (#{duration}) #{reqname} >> #{response_name} (#{response.reason})" + Baguette::Log.warning "fd #{"%4d" % event.fd} (#{duration}) #{request} >> #{response}" else if reqname != "KeepAlive" || should_display? DNSMESSAGE::KEEPALIVE - Baguette::Log.debug "fd #{"%4d" % event.fd} (#{duration}) #{reqname} >> #{response_name}" + Baguette::Log.debug "fd #{"%4d" % event.fd} (#{duration}) #{request} >> #{response}" end end end diff --git a/src/storage/zone.cr b/src/storage/zone.cr index e2ef257..f38dffd 100644 --- a/src/storage/zone.cr +++ b/src/storage/zone.cr @@ -88,6 +88,10 @@ class DNSManager::Storage::Zone [] of Error end + def to_simple_s + "(rrid #{@rrid}, '#{@name}' #{@rrtype})" + end + def to_s(io : IO) io << "(#{ "%4d" % @rrid }) " io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} #{ "%.10s" % @rrtype } #{ "%.30s" % @target}\n" @@ -913,6 +917,10 @@ class DNSManager::Storage::Zone end def to_s(io : IO) + io << "ZONE #{@domain} (#{@resources.size} rr)" + end + + def pretty_print(io : IO) io << "DOMAIN #{@domain}.\n" @resources.each do |rr| io << rr