Skip to content

Commit 8d79f18

Browse files
authored
impl(oauth2): omit RAB header when locations unbounded (googleapis#16136)
1 parent 458cbf4 commit 8d79f18

4 files changed

Lines changed: 34 additions & 2 deletions

google/cloud/internal/oauth2_minimal_iam_credentials_rest.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ std::string IamCredentialsEndpoint(
4545

4646
using ::google::cloud::internal::InvalidArgumentError;
4747

48+
bool AllowedLocationsResponse::IsUnbounded() const {
49+
return !encoded_locations.empty() && encoded_locations == "0x0";
50+
}
51+
4852
MinimalIamCredentialsRestStub::MinimalIamCredentialsRestStub(
4953
std::shared_ptr<oauth2_internal::Credentials> credentials, Options options,
5054
HttpClientFactory client_factory)

google/cloud/internal/oauth2_minimal_iam_credentials_rest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct GenerateAccessTokenRequest {
4040
};
4141

4242
struct AllowedLocationsResponse {
43+
bool IsUnbounded() const;
4344
std::vector<std::string> locations;
4445
std::string encoded_locations;
4546
};

google/cloud/internal/oauth2_regional_access_boundary_token_manager.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ class RegionalAccessBoundaryTokenManager
112112
if (IsTokenValid(lock, tp)) {
113113
// Check to see if we're near expiry and if so, start refresh process.
114114
if (tp > (expire_time_ - TtlGracePeriod())) RefreshToken(lock, request);
115-
return rest_internal::HttpHeader{"x-allowed-locations",
116-
allowed_locations_.encoded_locations};
115+
if (!allowed_locations_.IsUnbounded()) {
116+
return rest_internal::HttpHeader{"x-allowed-locations",
117+
allowed_locations_.encoded_locations};
118+
}
119+
return rest_internal::HttpHeader{};
117120
}
118121
RefreshToken(lock, request);
119122
// Don't wait for a valid token, just return an empty header.

google/cloud/internal/oauth2_regional_access_boundary_token_manager_test.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,30 @@ TEST_F(RegionalAccessBoundaryTokenManagerTest,
191191
refreshed_allowed_locations.encoded_locations}));
192192
}
193193

194+
TEST_F(RegionalAccessBoundaryTokenManagerTest,
195+
GetAllowedLocationsHeaderValidTokenUnbounded) {
196+
fake_clock_->SetTime(std::chrono::system_clock::now());
197+
MockFunction<std::unique_ptr<BackoffPolicy>()> backoff_fn;
198+
EXPECT_CALL(backoff_fn, Call).Times(0);
199+
200+
EXPECT_CALL(*mock_credentials_, AllowedLocationsRequest)
201+
.WillRepeatedly(Return(WorkforceIdentityAllowedLocationsRequest{}));
202+
203+
AllowedLocationsResponse allowed_locations;
204+
allowed_locations.locations = {""};
205+
allowed_locations.encoded_locations = "0x0";
206+
207+
auto manager = RegionalAccessBoundaryTokenManager::Create(
208+
mock_credentials_, mock_iam_stub_, {}, backoff_fn.AsStdFunction(),
209+
fake_clock_, allowed_locations);
210+
211+
fake_clock_->AdvanceTime(std::chrono::seconds(1));
212+
213+
auto header =
214+
manager->AllowedLocations(fake_clock_->Now(), "service.googleapis.com");
215+
EXPECT_THAT(header, IsOkAndHolds(IsEmpty()));
216+
}
217+
194218
TEST_F(RegionalAccessBoundaryTokenManagerTest,
195219
GetAllowedLocationsHeaderNoInitialValidTokenWithRetry) {
196220
EXPECT_CALL(*mock_credentials_, AllowedLocationsRequest)

0 commit comments

Comments
 (0)