Merge pull request #223 from basecamp/customizable-audit-broadcast

Allow customizing audit broadcast with env
This commit is contained in:
David Heinemeier Hansson
2023-05-05 14:30:04 +02:00
committed by GitHub
10 changed files with 148 additions and 48 deletions

View File

@@ -1,24 +1,24 @@
require "active_support/core_ext/time/conversions"
require "time"
class Mrsk::Commands::Auditor < Mrsk::Commands::Base
attr_reader :role
attr_reader :details
def initialize(config, role: nil)
def initialize(config, **details)
super(config)
@role = role
@details = default_details.merge(details)
end
# Runs remotely
def record(line)
def record(line, **details)
append \
[ :echo, tagged_record_line(line) ],
[ :echo, *audit_tags(**details), line ],
audit_log_file
end
# Runs locally
def broadcast(line)
def broadcast(line, **details)
if broadcast_cmd = config.audit_broadcast_cmd
[ broadcast_cmd, tagged_broadcast_line(line) ]
[ broadcast_cmd, *broadcast_args(line, **details), env: env_for(event: line, **details) ]
end
end
@@ -31,27 +31,29 @@ class Mrsk::Commands::Auditor < Mrsk::Commands::Base
[ "mrsk", config.service, config.destination, "audit.log" ].compact.join("-")
end
def tagged_record_line(line)
tagged_line recorded_at_tag, performer_tag, role_tag, line
def default_details
{ recorded_at: Time.now.utc.iso8601,
performer: `whoami`.chomp,
destination: config.destination }
end
def tagged_broadcast_line(line)
tagged_line performer_tag, role_tag, line
def audit_tags(**details)
tags_for **self.details.merge(details)
end
def tagged_line(*tags_and_line)
"'#{tags_and_line.compact.join(" ")}'"
def broadcast_args(line, **details)
"'#{broadcast_tags(**details).join(" ")} #{line}'"
end
def recorded_at_tag
"[#{Time.now.to_fs(:db)}]"
def broadcast_tags(**details)
tags_for **self.details.merge(details).except(:recorded_at)
end
def performer_tag
"[#{`whoami`.strip}]"
def tags_for(**details)
details.compact.values.map { |value| "[#{value}]" }
end
def role_tag
"[#{role}]" if role
def env_for(**details)
self.details.merge(details).compact.transform_keys { |detail| "MRSK_#{detail.upcase}" }
end
end

View File

@@ -1,5 +1,5 @@
require "active_support/duration"
require "active_support/core_ext/numeric/time"
require "time"
class Mrsk::Commands::Lock < Mrsk::Commands::Base
def acquire(message, version)
@@ -49,7 +49,7 @@ class Mrsk::Commands::Lock < Mrsk::Commands::Base
def lock_details(message, version)
<<~DETAILS.strip
Locked by: #{locked_by} at #{Time.now.gmtime}
Locked by: #{locked_by} at #{Time.now.utc.iso8601}
Version: #{version}
Message: #{message}
DETAILS