From 6b1130323048dc867b0a391f7a356baf3dbf1f37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20B=C3=BChlmann?= Date: Thu, 9 Mar 2023 19:35:30 +0100 Subject: [PATCH] Prepare auditor to print a present role --- lib/mrsk/commander.rb | 4 ++-- lib/mrsk/commands/auditor.rb | 19 +++++++++++++++++-- test/commands/auditor_test.rb | 10 +++++++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/mrsk/commander.rb b/lib/mrsk/commander.rb index ad61e895..d49243c3 100644 --- a/lib/mrsk/commander.rb +++ b/lib/mrsk/commander.rb @@ -69,8 +69,8 @@ class Mrsk::Commander Mrsk::Commands::Accessory.new(config, name: name) end - def auditor - @auditor ||= Mrsk::Commands::Auditor.new(config) + def auditor(role: nil) + Mrsk::Commands::Auditor.new(config, role: role) end def builder diff --git a/lib/mrsk/commands/auditor.rb b/lib/mrsk/commands/auditor.rb index a0bc0076..9d6cf414 100644 --- a/lib/mrsk/commands/auditor.rb +++ b/lib/mrsk/commands/auditor.rb @@ -1,6 +1,13 @@ require "active_support/core_ext/time/conversions" class Mrsk::Commands::Auditor < Mrsk::Commands::Base + attr_reader :role + + def initialize(config, role: nil) + super(config) + @role = role + end + # Runs remotely def record(line) append \ @@ -25,11 +32,15 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base end def tagged_record_line(line) - "'#{recorded_at_tag} #{performer_tag} #{line}'" + quote [recorded_at_tag, performer_tag, role_tag, line].compact.join(" ") end def tagged_broadcast_line(line) - "'#{performer_tag} #{line}'" + quote [performer_tag, role_tag, line].compact.join(" ") + end + + def role_tag + "[#{role}]" if role end def performer_tag @@ -39,4 +50,8 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base def recorded_at_tag "[#{Time.now.to_fs(:db)}]" end + + def quote(tagged_line) + "'#{tagged_line}'" + end end diff --git a/test/commands/auditor_test.rb b/test/commands/auditor_test.rb index f35e110c..55c3a32e 100644 --- a/test/commands/auditor_test.rb +++ b/test/commands/auditor_test.rb @@ -22,6 +22,14 @@ class CommandsAuditorTest < ActiveSupport::TestCase new_command.record("app removed container").join(" ") end + test "record with role" do + @role = "web" + + assert_match \ + /echo '.* \[web\] app removed container' >> mrsk-app-audit.log/, + new_command.record("app removed container").join(" ") + end + test "broadcast" do assert_match \ /bin\/audit_broadcast '\[.*\] app removed container'/, @@ -30,6 +38,6 @@ class CommandsAuditorTest < ActiveSupport::TestCase private def new_command - Mrsk::Commands::Auditor.new(Mrsk::Configuration.new(@config, destination: @destination, version: "123")) + Mrsk::Commands::Auditor.new(Mrsk::Configuration.new(@config, destination: @destination, version: "123"), role: @role) end end