-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move audit log to database #96
Changes from 10 commits
8ab5d28
1f5dcf5
7f0c478
699c101
d6563af
aebd7de
393907a
9e576f1
e35cdd1
5cd9003
749a464
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class SqlAuditLog < ApplicationRecord | ||
validates :request_id, :action, :result, :subject, presence: true | ||
|
||
if Config[:db_encryption] | ||
encrypts :request_id, :action, :result, :error, :subject, :cert_common_name, :kv_path | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class CreateSqlAuditLogs < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table :sql_audit_logs do |t| | ||
t.string :request_id, null: false | ||
t.string :action, null: false | ||
t.string :result, null: false | ||
t.string :error, null: true | ||
t.string :subject, null: false | ||
t.string :cert_common_name, null: true | ||
t.string :kv_path, null: true | ||
t.timestamps | ||
end | ||
add_index :sql_audit_logs, [ :subject, :created_at ] | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,7 @@ def setup | |
@domain = domains(:owner_match) | ||
@identity = Identity.new(subject: @domain.users.first) | ||
@cr = Requests::CertIssueRequest.new(common_name: @domain.fqdn) | ||
@log = Tempfile.new("log-test") | ||
Config[:audit_log_file] = @log.path | ||
end | ||
|
||
def teardown | ||
@log.close | ||
@log.unlink | ||
Thread.current[:request_id] = "request_id" | ||
end | ||
|
||
test ".call will be logged as success" do | ||
|
@@ -23,7 +17,9 @@ def call | |
end) | ||
rslt = SuccessAction.call(identity: @identity, request: @cr) | ||
assert rslt.success? | ||
assert_match %Q("action":"SuccessAction","result":"success","subject":"[email protected]","cert_common_name":"example.com"), @log.readlines.last | ||
log = SqlAuditLog.last | ||
expected = { "action"=>"SuccessAction", "result"=>"success", "subject"=>"[email protected]", "cert_common_name"=>"example.com" } | ||
assert expected <= log.attributes | ||
end | ||
|
||
test ".call will be logged as failure" do | ||
|
@@ -36,6 +32,8 @@ def call | |
end) | ||
rslt = FailAction.call(identity: @identity, request: @cr) | ||
assert_not rslt.success? | ||
assert_match %Q("action":"FailAction","result":"failure","subject":"[email protected]","cert_common_name":"example.com"), @log.readlines.last | ||
log = SqlAuditLog.last | ||
expected = { "action"=>"FailAction", "result"=>"failure", "subject"=>"[email protected]", "cert_common_name"=>"example.com" } | ||
assert expected <= log.attributes | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ class AuthenticateIdentityTest < ActiveSupport::TestCase | |
def setup | ||
@interactor = AuthenticateIdentity | ||
@identity = Identity.new(subject: "[email protected]", groups: [ "admin_group" ]) | ||
Thread.current[:request_id] = "request_id" | ||
end | ||
|
||
test ".call success" do | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require "test_helper" | ||
|
||
class SqlAuditLogTest < ActiveSupport::TestCase | ||
def setup | ||
@attributes = { | ||
request_id: "uuid1", | ||
action: "string1", | ||
result: "string2", | ||
subject: "string3", | ||
cert_common_name: "string4" | ||
} | ||
@sql_audit_log = SqlAuditLog.new(@attributes) | ||
end | ||
|
||
test "#new should set attributes from attributes argument" do | ||
@attributes.each do |key, value| | ||
assert_equal value, @sql_audit_log.send(key), "Attribute #{key} was not set correctly" | ||
end | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe test the negative case too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops, I merged before I saw this comment. I'll add it as a separate PR. |
||
test "#valid? should be valid with valid attributes" do | ||
assert @sql_audit_log.valid? | ||
end | ||
|
||
test "#valid? should require an result" do | ||
@sql_audit_log.result = nil | ||
assert_not @sql_audit_log.valid? | ||
assert_includes @sql_audit_log.errors[:result], "can't be blank" | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we name this (and the table)
AuditLog
-- since theSql
part is kind of implicit?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea. will fix.