Change logged representation of certain classes (#752)

This commit is contained in:
Daniel Azuma 2019-01-03 22:48:27 -08:00 committed by GitHub
parent 4ccc4ed71b
commit 55320753c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 1 deletions

View File

@ -253,7 +253,7 @@ module Google
# @return [Object] result if no block given
# @yield [result, nil] if block given
def success(result, &block)
logger.debug { sprintf('Success - %s', PP.pp(result, '')) }
logger.debug { sprintf('Success - %s', safe_object_representation(result)) }
block.call(result, nil) if block_given?
result
end
@ -333,6 +333,19 @@ module Google
private
UNSAFE_CLASS_NAMES = [
"Google::Apis::CloudkmsV1::DecryptResponse"
]
def safe_object_representation obj
name = obj.class.name
if UNSAFE_CLASS_NAMES.include? name
"#<#{name} (fields redacted)>"
else
PP.pp(obj, "")
end
end
def opencensus_begin_span
return unless OPENCENSUS_AVAILABLE && options.use_opencensus
return if @opencensus_span

View File

@ -15,6 +15,15 @@
require 'spec_helper'
require 'google/apis/core/http_command'
module Google
module Apis
module CloudkmsV1
class DecryptResponse
end
end
end
end
RSpec.describe Google::Apis::Core::HttpCommand do
include TestHelpers
include_context 'HTTP client'
@ -429,4 +438,23 @@ RSpec.describe Google::Apis::Core::HttpCommand do
command.execute(client)
end
describe "#safe_object_representation" do
let(:command) do
Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
end
it "should show fields in a normal object" do
obj = Object.new
obj.instance_variable_set(:@foobar, "hi")
str = command.send(:safe_object_representation, obj)
expect(str).to match /@foobar/
end
it "should not show fields in a restricted object" do
obj = Google::Apis::CloudkmsV1::DecryptResponse.new
obj.instance_variable_set(:@foobar, "hi")
str = command.send(:safe_object_representation, obj)
expect(str).not_to match /@foobar/
end
end
end