-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLAT-319] Configurable Redacted Keywords (#27)
* Add configurable secret keywords * add proper testing tools, add test for adding different types of sensitive keywords * wip * Add `sensitive_keywords` info to documentation * Remove extra args * Fix tests * Remove .github dir from gitignore * Reintroduce replace_keys args It is used in another context with a custom value to redact headers * Testing nested keys Co-authored-by: tiago-macedo <tiago.macedo@pier.digital> Co-authored-by: Leonardo Bighetti <leonardo.bighetti@gmail.com>
- Loading branch information
1 parent
ed2e1b6
commit 33bb767
Showing
10 changed files
with
139 additions
and
30 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ | |
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
*.gem | ||
*.gem | ||
.byebug_history |
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
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,25 @@ | ||
require "test_helper" | ||
|
||
class PierLogging::RequestLoggerConfigurationTest < Minitest::Test | ||
subject { PierLogging::RequestLoggerConfiguration.new } | ||
|
||
context "#sensitive_keywords" do | ||
should 'transform strings to regexps when adding them' do | ||
keyword = "blah" | ||
subject.sensitive_keywords = [keyword] | ||
assert_equal Regexp.new(keyword), subject.sensitive_keywords.first | ||
end | ||
|
||
should 'transform symbols to regexps when adding them' do | ||
keyword = :blah | ||
subject.sensitive_keywords = [keyword] | ||
assert_equal Regexp.new(keyword.to_s), subject.sensitive_keywords.first | ||
end | ||
|
||
should 'be able to add regexps' do | ||
keyword = /blah/ | ||
subject.sensitive_keywords = [keyword] | ||
assert_equal keyword, subject.sensitive_keywords.first | ||
end | ||
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,48 @@ | ||
require "test_helper" | ||
|
||
class PierLogging::RequestLoggerTest < Minitest::Test | ||
subject { PierLogging::RequestLogger.new(mock, @logger) } | ||
|
||
context "#log" do | ||
setup do | ||
PierLogging.request_logger_configuration.sensitive_keywords = [:blah] | ||
PierLogging.request_logger_configuration.enabled = true | ||
@logger = PierLogging::Logger.new($stdout) | ||
@env_mock = Rack::MockRequest.env_for | ||
body = {blah: "foo", bluh: "plaft"} | ||
@args = [@env_mock, "status", {"Content-type": "12"}, body, Time.now, Time.now] | ||
end | ||
|
||
teardown do | ||
PierLogging.request_logger_configuration.enabled = false | ||
end | ||
|
||
context 'when sensitive keyword is at root' do | ||
should "redact sensitive keywords" do | ||
@logger.expects(:info).with do |logged_args| | ||
redacted_body = logged_args[:response][:body] | ||
assert_equal "*", redacted_body[:blah] | ||
assert_equal "plaft", redacted_body[:bluh] | ||
end | ||
|
||
subject.log(@args) | ||
end | ||
end | ||
|
||
context 'when sensitive keyword is nested in array' do | ||
setup do | ||
body = {blirgh: [{blah: "foo"}, {bluh: "plaft"}]} | ||
@args = [@env_mock, "status", {"Content-type": "12"}, body, Time.now, Time.now] | ||
end | ||
should "redact sensitve keywords" do | ||
@logger.expects(:info).with do |logged_args| | ||
redacted_body = logged_args[:response][:body] | ||
assert_equal "*", redacted_body[:blirgh][0][:blah] | ||
assert_equal "plaft", redacted_body[:blirgh][1][:bluh] | ||
end | ||
|
||
subject.log(@args) | ||
end | ||
end | ||
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