Prepare auditor to print a present role

This commit is contained in:
Tobias Bühlmann
2023-03-09 19:35:30 +01:00
parent 901484d75d
commit 6b11303230
3 changed files with 28 additions and 5 deletions

View File

@@ -69,8 +69,8 @@ class Mrsk::Commander
Mrsk::Commands::Accessory.new(config, name: name) Mrsk::Commands::Accessory.new(config, name: name)
end end
def auditor def auditor(role: nil)
@auditor ||= Mrsk::Commands::Auditor.new(config) Mrsk::Commands::Auditor.new(config, role: role)
end end
def builder def builder

View File

@@ -1,6 +1,13 @@
require "active_support/core_ext/time/conversions" require "active_support/core_ext/time/conversions"
class Mrsk::Commands::Auditor < Mrsk::Commands::Base class Mrsk::Commands::Auditor < Mrsk::Commands::Base
attr_reader :role
def initialize(config, role: nil)
super(config)
@role = role
end
# Runs remotely # Runs remotely
def record(line) def record(line)
append \ append \
@@ -25,11 +32,15 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base
end end
def tagged_record_line(line) def tagged_record_line(line)
"'#{recorded_at_tag} #{performer_tag} #{line}'" quote [recorded_at_tag, performer_tag, role_tag, line].compact.join(" ")
end end
def tagged_broadcast_line(line) def tagged_broadcast_line(line)
"'#{performer_tag} #{line}'" quote [performer_tag, role_tag, line].compact.join(" ")
end
def role_tag
"[#{role}]" if role
end end
def performer_tag def performer_tag
@@ -39,4 +50,8 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base
def recorded_at_tag def recorded_at_tag
"[#{Time.now.to_fs(:db)}]" "[#{Time.now.to_fs(:db)}]"
end end
def quote(tagged_line)
"'#{tagged_line}'"
end
end end

View File

@@ -22,6 +22,14 @@ class CommandsAuditorTest < ActiveSupport::TestCase
new_command.record("app removed container").join(" ") new_command.record("app removed container").join(" ")
end 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 test "broadcast" do
assert_match \ assert_match \
/bin\/audit_broadcast '\[.*\] app removed container'/, /bin\/audit_broadcast '\[.*\] app removed container'/,
@@ -30,6 +38,6 @@ class CommandsAuditorTest < ActiveSupport::TestCase
private private
def new_command 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
end end