Skip to content

Commit 458cbf4

Browse files
authored
impl(oauth2): check for valid RAB email address (googleapis#16134)
1 parent d9b5436 commit 458cbf4

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

google/cloud/internal/oauth2_compute_engine_credentials.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,12 @@ Credentials::AllowedLocationsRequestType
243243
ComputeEngineCredentials::AllowedLocationsRequest() const {
244244
// TODO(#16079): Remove conditional and else clause when GA.
245245
#ifdef GOOGLE_CLOUD_CPP_TESTING_ENABLE_RAB
246-
return ServiceAccountAllowedLocationsRequest{AccountEmail()};
246+
auto email = AccountEmail();
247+
// RAB only supports values that contain the '@' character.
248+
if (absl::StrContains(email, "@")) {
249+
return ServiceAccountAllowedLocationsRequest{std::move(email)};
250+
}
251+
return std::monostate{};
247252
#else
248253
return std::monostate{};
249254
#endif

google/cloud/internal/oauth2_compute_engine_credentials_test.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,44 @@ TEST(ComputeEngineCredentialsTest, AccountEmail) {
432432
#endif
433433
}
434434

435+
TEST(ComputeEngineCredentialsTest, AccountEmailInvalidForRAB) {
436+
auto const alias = std::string{"default"};
437+
auto const not_an_email = std::string{"not-an-email"};
438+
auto const svc_acct_info_resp = std::string{R"""({
439+
"email": ")""" + not_an_email + R"""(",
440+
"scopes": ["scope1","scope2"]
441+
})"""};
442+
443+
auto client = std::make_unique<MockRestClient>();
444+
EXPECT_CALL(*client, Get(_, expect_service_config(alias)))
445+
.WillOnce([&](RestContext&, RestRequest const&) {
446+
auto response = std::make_unique<MockRestResponse>();
447+
EXPECT_CALL(*response, StatusCode)
448+
.WillRepeatedly(Return(HttpStatusCode::kOk));
449+
EXPECT_CALL(std::move(*response), ExtractPayload).WillOnce([&] {
450+
return MakeMockHttpPayloadSuccess(svc_acct_info_resp);
451+
});
452+
return std::unique_ptr<RestResponse>(std::move(response));
453+
});
454+
455+
MockHttpClientFactory client_factory;
456+
EXPECT_CALL(client_factory, Call).WillOnce(Return(ByMove(std::move(client))));
457+
ComputeEngineCredentials credentials(alias, Options{},
458+
client_factory.AsStdFunction());
459+
EXPECT_EQ(credentials.service_account_email(), alias);
460+
auto refreshed_email = credentials.AccountEmail();
461+
EXPECT_EQ(not_an_email, refreshed_email);
462+
EXPECT_EQ(credentials.service_account_email(), refreshed_email);
463+
// TODO(#16079): Remove conditional and else clause when GA.
464+
#ifdef GOOGLE_CLOUD_CPP_TESTING_ENABLE_RAB
465+
EXPECT_THAT(credentials.AllowedLocationsRequest(),
466+
VariantWith<std::monostate>(std::monostate()));
467+
#else
468+
EXPECT_THAT(credentials.AllowedLocationsRequest(),
469+
VariantWith<std::monostate>(std::monostate()));
470+
#endif
471+
}
472+
435473
auto expected_universe_domain_request = []() {
436474
auto const expected_path = absl::StrCat(
437475
internal::GceMetadataScheme(), "://", internal::GceMetadataHostname(),

0 commit comments

Comments
 (0)