Skip to content

Commit fb5b797

Browse files
fix: return response body from revoke! for logging pipeline (#562)
* fix: return response body from revoke! for logging pipeline * chore: speed up tests by stubbing gcloud and sleep * fix: ensure log_response actually logs the message --------- Co-authored-by: Viacheslav Rostovtsev <virost@google.com>
1 parent b0ec7d8 commit fb5b797

File tree

6 files changed

+80
-6
lines changed

6 files changed

+80
-6
lines changed

lib/googleauth/signet.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,12 @@ def log_response token_response
210210
digest = Digest::SHA256.hexdigest response_hash["id_token"]
211211
response_hash["id_token"] = "(sha256:#{digest})"
212212
end
213-
Google::Logging::Message.from(
214-
message: "Received auth token response: #{response_hash}",
215-
"credentialsId" => object_id
216-
)
213+
logger&.debug do
214+
Google::Logging::Message.from(
215+
message: "Received auth token response: #{response_hash}",
216+
"credentialsId" => object_id
217+
)
218+
end
217219
end
218220

219221
def log_auth_error err

lib/googleauth/user_refresh.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ def revoke! options = {}
146146
principal: principal
147147
)
148148
end
149+
150+
resp.body
149151
end
150152
end
151153

spec/googleauth/signet_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def make_auth_stubs opts
176176
response = mocked_responses.shift
177177
response == :raise ? raise(Signet::RemoteServerError) : response
178178
end
179+
expect(@client).to receive(:sleep).exactly(2).times.with(kind_of(Numeric))
179180
expect(@client.fetch_access_token!).to eq("success")
180181
end
181182

@@ -185,6 +186,7 @@ def make_auth_stubs opts
185186
response = mocked_responses.shift
186187
response == :raise ? raise(Signet::RemoteServerError) : response
187188
end
189+
expect(@client).to receive(:sleep).exactly(5).times.with(kind_of(Numeric))
188190
expect { @client.fetch_access_token! }.to raise_error Signet::AuthorizationError
189191
end
190192

spec/googleauth/user_refresh_spec.rb

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,37 +326,53 @@ def cred_json_text_with_universe_domain missing = nil
326326
end
327327

328328
describe "when revoking a refresh token" do
329+
let(:response_body) { "{}" }
329330
let :stub do
330331
stub_request(:post, "https://oauth2.googleapis.com/revoke")
331332
.with(body: hash_including("token" => "refreshtoken"))
332333
.to_return(status: 200,
334+
body: response_body,
333335
headers: { "Content-Type" => "application/json" })
334336
end
335337

336338
before :example do
337339
stub
338-
@client.revoke!
340+
@result = @client.revoke!
339341
end
340342

341343
it_behaves_like "revoked token"
344+
345+
# The return value is passed through retry_with_error's logging pipeline,
346+
# which expects a JSON-parseable string.
347+
it "returns the response body" do
348+
expect(@result).to eq(response_body)
349+
end
342350
end
343351

344352
describe "when revoking an access token" do
353+
let(:response_body) { "{}" }
345354
let :stub do
346355
stub_request(:post, "https://oauth2.googleapis.com/revoke")
347356
.with(body: hash_including("token" => "accesstoken"))
348357
.to_return(status: 200,
358+
body: response_body,
349359
headers: { "Content-Type" => "application/json" })
350360
end
351361

352362
before :example do
353363
stub
354364
@client.refresh_token = nil
355365
@client.access_token = "accesstoken"
356-
@client.revoke!
366+
@result = @client.revoke!
357367
end
358368

359369
it_behaves_like "revoked token"
370+
371+
# The return value is passed through retry_with_error's logging pipeline,
372+
# which expects a JSON-parseable string.
373+
it "returns the response body" do
374+
expect(@result).to eq(response_body)
375+
end
360376
end
361377

362378
describe "when revoking an invalid token" do
@@ -378,17 +394,54 @@ def cred_json_text_with_universe_domain missing = nil
378394
end
379395
end
380396

397+
describe "logging during revoke" do
398+
let(:response_body) { '{"foo": "bar"}' }
399+
let :stub do
400+
stub_request(:post, "https://oauth2.googleapis.com/revoke")
401+
.with(body: hash_including("token" => "refreshtoken"))
402+
.to_return(status: 200,
403+
body: response_body,
404+
headers: { "Content-Type" => "application/json" })
405+
end
406+
407+
it "logs the response body" do
408+
stub
409+
strio = StringIO.new
410+
logger = Logger.new strio
411+
logger.level = Logger::DEBUG
412+
@client.logger = logger
413+
@client.revoke!
414+
expect(strio.string).to include("Received auth token response")
415+
end
416+
417+
it "logs transient errors when they occur" do
418+
allow_any_instance_of(Faraday::Connection).to receive(:post).and_raise(Faraday::TimeoutError)
419+
strio = StringIO.new
420+
logger = Logger.new strio
421+
@client.logger = logger
422+
423+
# Stub sleep to avoid slow tests
424+
allow(@client).to receive(:sleep)
425+
426+
expect { @client.revoke! }.to raise_error Signet::AuthorizationError
427+
expect(strio.string).to include("Transient error when fetching auth token")
428+
expect(strio.string).to include("Exhausted retries when fetching auth token")
429+
end
430+
end
431+
381432
describe "when errors occurred with request" do
382433
it "should fail with Signet::AuthorizationError if request times out" do
383434
allow_any_instance_of(Faraday::Connection).to receive(:post)
384435
.and_raise(Faraday::TimeoutError)
436+
expect(@client).to receive(:sleep).exactly(5).times.with(kind_of(Numeric))
385437
expect { @client.revoke! }
386438
.to raise_error Signet::AuthorizationError
387439
end
388440

389441
it "should fail with Signet::AuthorizationError if request fails" do
390442
allow_any_instance_of(Faraday::Connection).to receive(:post)
391443
.and_raise(Faraday::ConnectionFailed, nil)
444+
expect(@client).to receive(:sleep).exactly(5).times.with(kind_of(Numeric))
392445
expect { @client.revoke! }
393446
.to raise_error Signet::AuthorizationError
394447
end

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
config.include WebMock::API
4343
config.filter_run focus: true
4444
config.run_all_when_everything_filtered = true
45+
46+
config.before(:each) do
47+
allow(Google::Auth::CredentialsLoader).to receive(:load_gcloud_project_id).and_return("my-project-id")
48+
end
4549
end
4650

4751
module TestHelpers

test/helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818

1919
require "googleauth"
2020

21+
# Proactively stub the gcloud CLI call for all future Minitest tests
22+
module Google
23+
module Auth
24+
module CredentialsLoader
25+
def load_gcloud_project_id
26+
"my-project-id"
27+
end
28+
end
29+
end
30+
end
31+
2132
##
2233
# A simple in-memory implementation of TokenStore
2334
# for UserAuthorizer initialization when testing

0 commit comments

Comments
 (0)