Skip to content

Commit

Permalink
Fixes #26957 - send request id instead session to proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap authored and tbrisker committed Jun 5, 2019
1 parent f19dc84 commit 48af620
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
20 changes: 16 additions & 4 deletions lib/proxy_api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ class Resource
def initialize(args)
raise("Must provide a protocol and host when initialising a smart-proxy connection") unless (url =~ /^http/)

@connect_params = {:timeout => Setting[:proxy_request_timeout], :open_timeout => 10,
:headers => { :accept => :json, :x_request_id => request_id },
:user => args[:user], :password => args[:password]}
@connect_params = {
:timeout => Setting[:proxy_request_timeout],
:open_timeout => 10,
:headers => {
:accept => :json,
:x_request_id => request_id,
:x_session_id => session_id
},
:user => args[:user],
:password => args[:password]
}

# We authenticate only if we are using SSL
@connect_params.merge!(ssl_auth_params) if url =~ /^https/i && !Rails.env.test?
Expand Down Expand Up @@ -108,7 +116,11 @@ def with_logger
end

def request_id
::Logging.mdc['session'] || SecureRandom.hex(32)
::Logging.mdc['request'] || SecureRandom.uuid
end

def session_id
::Logging.mdc['session'] || SecureRandom.uuid
end

def ssl_auth_params
Expand Down
13 changes: 11 additions & 2 deletions test/unit/proxy_api/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ class ProxyApiResourceTest < ActiveSupport::TestCase
assert ProxyAPI::Resource.new({}).send(:connect_params)[:headers][:x_request_id].present?
end

test "connect_params sets x_request_id to logger request ID" do
begin
::Logging.mdc['request'] = '850ca7f1-c6de-481f-86a9-dc379a04b445'
assert_equal '850ca7f1-c6de-481f-86a9-dc379a04b445', ProxyAPI::Resource.new({}).send(:connect_params)[:headers][:x_request_id]
ensure
::Logging.mdc.delete('request')
end
end

test "connect_params sets x_request_id to logger safe session ID" do
begin
::Logging.mdc['session'] = 'test'
assert_equal 'test', ProxyAPI::Resource.new({}).send(:connect_params)[:headers][:x_request_id]
::Logging.mdc['session'] = '850ca7f1-c6de-481f-86a9-dc379a04b446'
assert_equal '850ca7f1-c6de-481f-86a9-dc379a04b446', ProxyAPI::Resource.new({}).send(:connect_params)[:headers][:x_session_id]
ensure
::Logging.mdc.delete('session')
end
Expand Down

0 comments on commit 48af620

Please sign in to comment.