From 3288b818942f460cb7909f22687787a2b77f5e37 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 27 Jul 2021 16:59:45 +1000 Subject: [PATCH] fix: log HTTP request for pacts retrieved by URL when requested with verbose=true --- lib/pact/consumer_contract/pact_file.rb | 11 ++++--- .../http/authorization_header_redactor.rb | 32 +++++++++++++++++++ .../authorization_header_redactor_spec.rb | 15 +++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 lib/pact/http/authorization_header_redactor.rb create mode 100644 spec/lib/pact/http/authorization_header_redactor_spec.rb diff --git a/lib/pact/consumer_contract/pact_file.rb b/lib/pact/consumer_contract/pact_file.rb index f855129..349d7b4 100644 --- a/lib/pact/consumer_contract/pact_file.rb +++ b/lib/pact/consumer_contract/pact_file.rb @@ -1,4 +1,6 @@ -require 'net/http' +require "net/http" +require "pact/configuration" +require "pact/http/authorization_header_redactor" module Pact module PactFile @@ -81,7 +83,7 @@ def get_remote(uri, options) request = Net::HTTP::Get.new(uri) request = prepare_auth(request, options) if options[:username] || options[:token] - http = prepare_request(uri) + http = prepare_request(uri, options) response = perform_http_request(http, request, options) if response.is_a?(Net::HTTPRedirection) @@ -89,7 +91,7 @@ def get_remote(uri, options) req = Net::HTTP::Get.new(uri) req = prepare_auth(req, options) if options[:username] || options[:token] - http = prepare_request(uri) + http = prepare_request(uri, options) response = perform_http_request(http, req, options) end response @@ -101,11 +103,12 @@ def prepare_auth(request, options) request end - def prepare_request(uri) + def prepare_request(uri, options) http = Net::HTTP.new(uri.host, uri.port, :ENV) http.use_ssl = (uri.scheme == 'https') http.ca_file = ENV['SSL_CERT_FILE'] if ENV['SSL_CERT_FILE'] && ENV['SSL_CERT_FILE'] != '' http.ca_path = ENV['SSL_CERT_DIR'] if ENV['SSL_CERT_DIR'] && ENV['SSL_CERT_DIR'] != '' + http.set_debug_output(Pact::Http::AuthorizationHeaderRedactor.new(Pact.configuration.output_stream)) if options[:verbose] http end diff --git a/lib/pact/http/authorization_header_redactor.rb b/lib/pact/http/authorization_header_redactor.rb new file mode 100644 index 0000000..148ab1f --- /dev/null +++ b/lib/pact/http/authorization_header_redactor.rb @@ -0,0 +1,32 @@ +require "delegate" + +module Pact + module Http + class AuthorizationHeaderRedactor < SimpleDelegator + def puts(*args) + __getobj__().puts(*redact_args(args)) + end + + def print(*args) + __getobj__().puts(*redact_args(args)) + end + + def <<(*args) + __getobj__().send(:<<, *redact_args(args)) + end + + private + + attr_reader :redactions + + def redact_args(args) + args.collect{ | s| redact(s) } + end + + def redact(string) + return string unless string.is_a?(String) + string.gsub(/Authorization: .*\\r\\n/, "Authorization: [redacted]\\r\\n") + end + end + end +end diff --git a/spec/lib/pact/http/authorization_header_redactor_spec.rb b/spec/lib/pact/http/authorization_header_redactor_spec.rb new file mode 100644 index 0000000..cd2ecc5 --- /dev/null +++ b/spec/lib/pact/http/authorization_header_redactor_spec.rb @@ -0,0 +1,15 @@ +require "pact/http/authorization_header_redactor" + +module Pact + module Http + describe AuthorizationHeaderRedactor do + let(:stream) { StringIO.new } + let(:stream_redactor) { AuthorizationHeaderRedactor.new(stream) } + + it "redacts the authorizaton header" do + stream_redactor << "\\r\\nAuthorization: Bearer TOKEN\\r\\n" + expect(stream.string).to eq "\\r\\nAuthorization: [redacted]\\r\\n" + end + end + end +end