Skip to content

Commit 51aa13c

Browse files
committed
feat: update redact logs
1 parent 03a3b63 commit 51aa13c

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

lib/pact_broker/string_refinements.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module PactBroker
2+
module StringRefinements
3+
refine String do
4+
def not_blank?
5+
self && self.strip.size > 0
6+
end
7+
end
8+
end
9+
end

lib/pact_broker/webhooks/redact_logs.rb

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
require 'pact_broker/string_refinements'
2+
13
module PactBroker
24
module Webhooks
3-
class RedactLogs
4-
def self.call logs
5-
logs.gsub(/(Authorization: )(.*)/i,'\1[REDACTED]')
6-
.gsub(/(Token: )(.*)/i,'\1[REDACTED]')
5+
module RedactLogs
6+
HEADER_SUBSTITUTIONS = [[/(Authorization: )(.*)/i, '\1[REDACTED]'], [ /(Token: )(.*)/i, '\1[REDACTED]']]
7+
8+
using PactBroker::StringRefinements
9+
10+
def redact_logs(logs, values)
11+
RedactLogs.call(logs, values)
12+
end
13+
14+
def self.call logs, values
15+
substitutions = HEADER_SUBSTITUTIONS + value_substitutions(values)
16+
17+
substitutions.reduce(logs) do | logs, (find, replace) |
18+
logs.gsub(find, replace)
19+
end
20+
end
21+
22+
def self.value_substitutions(values)
23+
values.select(&:not_blank?).collect{ | value | [value, "********"] }
724
end
825
end
926
end

spec/lib/pact_broker/webhooks/redact_logs_spec.rb

+16-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module PactBroker
44
module Webhooks
55
describe RedactLogs do
66
describe ".call" do
7+
let(:values) { [] }
8+
79
let(:string) do
810
"Authorization: foo\nX-Thing: bar"
911
end
@@ -25,23 +27,32 @@ module Webhooks
2527
end
2628

2729
it "hides the value of the Authorization header" do
28-
expect(RedactLogs.call(string)).to eq "Authorization: [REDACTED]\nX-Thing: bar"
30+
expect(RedactLogs.call(string, values)).to eq "Authorization: [REDACTED]\nX-Thing: bar"
2931
end
3032

3133
it "hides the value of the X-Authorization header" do
32-
expect(RedactLogs.call(x_auth_string)).to eq "X-Authorization: [REDACTED]\nX-Thing: bar"
34+
expect(RedactLogs.call(x_auth_string, values)).to eq "X-Authorization: [REDACTED]\nX-Thing: bar"
3335
end
3436

3537
it "hides the value of the X-Auth-Token header" do
36-
expect(RedactLogs.call(x_auth_token)).to eq "X-Auth-Token: [REDACTED]\nX-Thing: bar"
38+
expect(RedactLogs.call(x_auth_token, values)).to eq "X-Auth-Token: [REDACTED]\nX-Thing: bar"
3739
end
3840

3941
it "hides the value of the X-Authorization-Token header" do
40-
expect(RedactLogs.call(x_authorization_token)).to eq "X-Authorization-Token: [REDACTED]\nX-Thing: bar"
42+
expect(RedactLogs.call(x_authorization_token, values)).to eq "X-Authorization-Token: [REDACTED]\nX-Thing: bar"
4143
end
4244

4345
it "hides the value of the authorization header" do
44-
expect(RedactLogs.call(string_lower)).to eq "authorization: [REDACTED]\nX-Thing: bar"
46+
expect(RedactLogs.call(string_lower, values)).to eq "authorization: [REDACTED]\nX-Thing: bar"
47+
end
48+
49+
context "with values" do
50+
let(:values) { %w[foo bar] }
51+
let(:string) { "blahfoo\nbar wiffle" }
52+
53+
it "hides the passed in values" do
54+
expect(RedactLogs.call(string, values)).to eq "blah********\n******** wiffle"
55+
end
4556
end
4657
end
4758
end

0 commit comments

Comments
 (0)