Skip to content

Commit

Permalink
Backport: If a http with a payload returns a 401, then return a respo…
Browse files Browse the repository at this point in the history
…nse instead of an error

cherry-picked from: 9eeaf22

- This is to allow handling of 401 responses due to lack of user permissions

[#132849945]

Signed-off-by: Dennis Leon <[email protected]>
  • Loading branch information
tjvman authored and DennisDenuto committed Nov 1, 2016
1 parent 6d4aba5 commit 8b27e12
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion bosh_cli/lib/cli/client/director.rb
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,11 @@ def perform_http_request(method, uri, payload = nil, headers = {}, &block)
}, &block)

if !response.nil? && response.code == 401
if @credentials.nil? || !payload.nil? || !@credentials.refresh
if !payload.nil?
return response
end

if @credentials.nil? || !@credentials.refresh
raise AuthError
end

Expand Down
4 changes: 2 additions & 2 deletions bosh_cli/spec/unit/client/director_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
with(headers: { 'Content-Type' => 'application/json' }).
to_return(body: '{"version":258}', status: 401)

expect { @director.post('/path', 'application/json', 'binary data') }.to raise_error(Bosh::Cli::AuthError)
expect { @director.post('/path', 'application/json', 'binary data') }.to raise_error(Bosh::Cli::DirectorError, /HTTP 401/)
end
end

Expand Down Expand Up @@ -226,7 +226,7 @@

expect(credentials).to receive(:refresh).once

expect {@director.post('/path', 'application/json', 'binary data')}.to raise_error(Bosh::Cli::AuthError)
expect {@director.post('/path', 'application/json', 'binary data')}.to raise_error(Bosh::Cli::DirectorError, /HTTP 401/)
end

it 'should not refresh the token before making a request without a body' do
Expand Down

0 comments on commit 8b27e12

Please sign in to comment.