-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Audit Logging (json formatted) for user actions (#26)
* App registry client * Add json-server mock for AppRegistry * request_id logging; accept hash for log message
- Loading branch information
Showing
11 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module AuditLogging | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
around do |interactor| | ||
interactor.call | ||
log | ||
rescue => e | ||
log | ||
raise e | ||
end | ||
end | ||
|
||
private | ||
|
||
def log | ||
result = context.success? ? "success" : "failure" | ||
level = context.success? ? :info : :error | ||
payload = { | ||
action: "#{self.class.name}", | ||
result: result, | ||
error: context.error&.message, | ||
subject: context.identity&.subject, | ||
cert_common_name: context.request&.try(:common_name) | ||
} | ||
AuditLogger.new.send(level, payload) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class AuditLogFormatter < ActiveSupport::Logger::SimpleFormatter | ||
def call(severity, timestamp, _progname, message) | ||
# request_id is unique to the life of the api request | ||
request_id = Thread.current[:request_id] | ||
json = { | ||
type: severity, | ||
time: "#{timestamp}", | ||
request_id: request_id | ||
} | ||
if message.is_a? Hash | ||
json = json.merge(message) | ||
else | ||
json[:message] = message | ||
end | ||
"#{json.to_json}\n" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AuditLogger < ActiveSupport::Logger | ||
def initialize | ||
super(Rails.configuration.astral[:audit_log_file]) | ||
self.formatter = AuditLogFormatter.new | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "test_helper" | ||
|
||
class AuditLoggingTest < ActiveSupport::TestCase | ||
def setup | ||
@domain = domains(:owner_match) | ||
@identity = Identity.new(subject: @domain.users_array.first) | ||
@cr = CertIssueRequest.new(common_name: @domain.fqdn) | ||
@log = Tempfile.new("log-test") | ||
Rails.configuration.astral[:audit_log_file] = @log.path | ||
end | ||
|
||
def teardown | ||
@log.close | ||
@log.unlink | ||
end | ||
|
||
test ".call will be logged as success" do | ||
Object.const_set("SuccessAction", Class.new do | ||
include Interactor | ||
include AuditLogging | ||
|
||
def call | ||
end | ||
end) | ||
rslt = SuccessAction.call(identity: @identity, request: @cr) | ||
assert rslt.success? | ||
assert_match %Q("action":"SuccessAction","result":"success","error":null,"subject":"[email protected]","cert_common_name":"example.com"), @log.readlines.last | ||
end | ||
|
||
test ".call will be logged as failure" do | ||
Object.const_set("FailAction", Class.new do | ||
include Interactor | ||
include AuditLogging | ||
|
||
def call | ||
context.fail! | ||
end | ||
end) | ||
rslt = FailAction.call(identity: @identity, request: @cr) | ||
assert_not rslt.success? | ||
assert_match %Q("action":"FailAction","result":"failure","error":null,"subject":"[email protected]","cert_common_name":"example.com"), @log.readlines.last | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require "test_helper" | ||
|
||
class AuditLogFormatterTest < ActiveSupport::TestCase | ||
setup do | ||
Thread.current[:request_id] = nil | ||
end | ||
|
||
test "#call formats logformatter inputs as json" do | ||
t = Time.now | ||
result = AuditLogFormatter.new.call("info", t, nil, "some message") | ||
assert_equal %Q({"type":"info","time":"#{t}","request_id":null,"message":"some message"}\n), result | ||
end | ||
|
||
test "#call accepts and merges a Hash type for the message" do | ||
t = Time.now | ||
result = AuditLogFormatter.new.call("info", t, nil, { key: "some message", key2: "another" }) | ||
assert_equal %Q({"type":"info","time":"#{t}","request_id":null,"key":"some message","key2":"another"}\n), result | ||
end | ||
|
||
test "#call can render a thread local request_id" do | ||
t = Time.now | ||
req_id = SecureRandom.hex | ||
Thread.stub :current, { request_id: req_id } do | ||
result = AuditLogFormatter.new.call("info", t, nil, { key: "some message" }) | ||
assert_equal %Q({"type":"info","time":"#{t}","request_id":"#{req_id}","key":"some message"}\n), result | ||
end | ||
end | ||
end |