-
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.
* 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 * Extract redacting to a `Redactor`, override `PierLogging::Logger`s log method * Test redactor for hash * Move require declaration to Redactor class * Test redaction for arrays * Fix identation * Double quotes strings * Rename var for clarity * Redacting all params Ougai is very flexible and data can be in any position. * Add logger test without assertions * Add assertions * Refactor test * Move sensitive_keyword to LoggerConfiguration * Override `_log` not `log`, fix Heisembug * version bump and fix log readability * Add squad to list of authors * Remove unused test * remove extraneous redaction from request logger, base logger already redacts everything * Bump version Co-authored-by: Leonardo Bighetti <[email protected]> Co-authored-by: Bruno Arakaki <[email protected]>
- Loading branch information
1 parent
33bb767
commit 8742fb7
Showing
11 changed files
with
205 additions
and
73 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
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,62 @@ | ||
# Requiring only the part that we need | ||
require 'facets/hash/traverse' | ||
|
||
module PierLogging | ||
module Helpers | ||
class Redactor | ||
REDACT_REPLACE_KEYS = [ | ||
/passw(or)?d/i, | ||
/^pw$/, | ||
/^pass$/i, | ||
/secret/i, | ||
/token/i, | ||
/api[-._]?key/i, | ||
/session[-._]?id/i, | ||
/^connect\.sid$/ | ||
].freeze | ||
REDACT_REPLACE_BY = '*'.freeze | ||
|
||
class << self | ||
def redact(obj, replace_keys = nil, replace_by = REDACT_REPLACE_BY) | ||
replace_keys ||= sensitive_keywords | ||
if obj.is_a?(Array) | ||
redact_array(obj, replace_keys, replace_by) | ||
elsif obj.is_a?(Hash) | ||
redact_hash(obj, replace_keys, replace_by) | ||
elsif obj.respond_to?(:to_hash) | ||
redact_hash(obj.to_hash, replace_keys, replace_by) | ||
else | ||
obj | ||
end | ||
end | ||
|
||
private | ||
|
||
def sensitive_keywords | ||
REDACT_REPLACE_KEYS + PierLogging.logger_configuration.sensitive_keywords | ||
end | ||
|
||
def redact_array(arr, replace_keys, replace_by = REDACT_REPLACE_BY) | ||
raise StandardError, 'Could not redact_array for non-array objects' unless arr.is_a? Array | ||
arr.map { |el| redact(el, replace_keys, replace_by) } | ||
end | ||
|
||
def redact_hash(hash, replace_keys, replace_by = REDACT_REPLACE_BY) | ||
raise StandardError, 'Could not redact_hash for non-hash objects' unless hash.is_a? Hash | ||
hash.traverse do |k,v| | ||
should_redact = replace_keys.any?{ |regex| k =~ regex } | ||
if (should_redact) | ||
[k, replace_by] | ||
else | ||
case v | ||
when Array then [k, redact_array(v, replace_keys, replace_by)] | ||
else | ||
[k, v] | ||
end | ||
end | ||
end | ||
end | ||
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module PierLogging | ||
VERSION = "0.3.3" | ||
VERSION = "0.4.1" | ||
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 |
---|---|---|
|
@@ -6,8 +6,8 @@ require "pier_logging/version" | |
Gem::Specification.new do |spec| | ||
spec.name = "pier_logging" | ||
spec.version = PierLogging::VERSION | ||
spec.authors = ["Mauricio Banduk"] | ||
spec.email = ["[email protected]"] | ||
spec.authors = ["Mauricio Banduk", "Bruno Arakaki", "Leonardo Bighetti", "Tiago Macedo"] | ||
spec.email = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"] | ||
|
||
spec.summary = %q{Structured log used on Pier Applications} | ||
spec.description = %q{Defines a basic structure for general and request logging} | ||
|
@@ -23,8 +23,8 @@ Gem::Specification.new do |spec| | |
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_dependency "ougai" | ||
spec.add_dependency "awesome_print" | ||
spec.add_dependency "ougai", ">=2.0.0" | ||
spec.add_dependency "amazing_print" | ||
spec.add_dependency "rails" | ||
spec.add_dependency "facets" | ||
|
||
|
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,44 @@ | ||
require "test_helper" | ||
|
||
class PierLogging::Helpers::RedactorTest < Minitest::Test | ||
subject { PierLogging::Helpers::Redactor } | ||
|
||
context ".redact" do | ||
setup do | ||
PierLogging.logger_configuration.sensitive_keywords = [:sensitive_key] | ||
end | ||
context "with a hash" do | ||
setup do | ||
@hash = { | ||
sensitive_key: "Amar é fogo que arde sem se ver", | ||
not_sensitive: "Vai Rabetão tão tão no chão", | ||
password: "Que não seja imortal, posto que é chama" | ||
} | ||
end | ||
should "redact only sensitive stuff" do | ||
response = subject.redact(@hash) | ||
|
||
assert_equal "*", response[:sensitive_key] | ||
assert_equal "Vai Rabetão tão tão no chão", response[:not_sensitive] | ||
assert_equal "*", response[:password] | ||
end | ||
end | ||
|
||
context "with an array of hashs" do | ||
setup do | ||
@array = [ | ||
{sensitive_key: "Amar é fogo que arde sem se ver"}, | ||
{not_sensitive: "Vai Rabetão tão tão no chão"}, | ||
{password: "Que não seja imortal, posto que é chama"} | ||
] | ||
end | ||
should "redact only sensitive stuff" do | ||
response = subject.redact(@array) | ||
|
||
assert_equal "*", response[0][:sensitive_key] | ||
assert_equal "Vai Rabetão tão tão no chão", response[1][:not_sensitive] | ||
assert_equal "*", response[2][:password] | ||
end | ||
end | ||
end | ||
end |
4 changes: 2 additions & 2 deletions
4
...ging/request_logger_configuration_test.rb → ...pier_logging/logger_configuration_test.rb
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
Oops, something went wrong.