28 #include <gmock/gmock.h>
31 #include "absl/strings/match.h"
32 #include "absl/strings/str_cat.h"
33 #include "absl/strings/str_format.h"
34 #include "absl/strings/str_replace.h"
85 const char test_google_iam_authorization_token[] =
"blahblahblhahb";
86 const char test_google_iam_authority_selector[] =
"respectmyauthoritah";
87 const char test_oauth2_bearer_token[] =
"Bearer blaaslkdjfaslkdfasdsfasf";
93 "{ \"private_key\": \"-----BEGIN PRIVATE KEY-----"
94 "\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJE"
96 "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/"
97 "rWBQvS4hle4LfijkP3J5BG+"
98 "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+"
99 "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/"
100 "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/"
101 "8HpCqFYM9V8f34SBWfD4fRFT+n/"
102 "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+";
104 "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+"
105 "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/"
106 "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDA"
108 "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6Hz"
110 "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+"
111 "6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/"
112 "5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZ"
114 "Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", ";
116 "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", "
118 "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount."
119 "com\", \"client_id\": "
120 "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent."
121 "com\", \"type\": \"service_account\" }";
125 "{ \"client_id\": \"32555999999.apps.googleusercontent.com\","
126 " \"client_secret\": \"EmssLNjJy1332hD4KFsecret\","
127 " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\","
128 " \"type\": \"authorized_user\"}";
131 const char test_external_account_credentials_str[] =
132 "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
133 "token_type\":\"subject_token_type\",\"service_account_impersonation_"
134 "url\":\"https://iamcredentials.googleapis.com:5555/"
135 "service_account_impersonation_url\",\"token_url\":\"https://"
136 "sts.googleapis.com:5555/"
137 "token\",\"token_info_url\":\"https://sts.googleapis.com:5555/"
138 "token_info\",\"credential_source\":{\"file\":"
139 "\"credentials_file_path\"},"
140 "\"quota_project_id\":\"quota_"
141 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
144 const char test_external_account_credentials_multi_pattern_sts_str[] =
145 "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
146 "token_type\":\"subject_token_type\",\"service_account_impersonation_"
147 "url\":\"https://sts.test.googleapis.com:5555/"
148 "service_account_impersonation_url\",\"token_url\":\"https://"
149 "test.sts.googleapis.com:5555/token\",\"token_info_url\":\"https://"
150 "test-sts.googleapis.com:5555/"
151 "token_info\",\"credential_source\":{\"file\":\"credentials_file_path\"},"
152 "\"quota_project_id\":\"quota_"
153 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
156 const char test_external_account_credentials_multi_pattern_iam_str[] =
157 "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
158 "token_type\":\"subject_token_type\",\"service_account_impersonation_"
159 "url\":\"https://iamcredentials.test.googleapis.com:5555/"
160 "service_account_impersonation_url\",\"token_url\":\"https://"
161 "test.iamcredentials.googleapis.com:5555/"
162 "token\",\"token_info_url\":\"https://"
163 "test-iamcredentials.googleapis.com:5555/"
164 "token_info\",\"credential_source\":{\"file\":\"credentials_file_path\"},"
165 "\"quota_project_id\":\"quota_"
166 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
169 const char valid_oauth2_json_response[] =
170 "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\","
171 " \"expires_in\":3599, "
172 " \"token_type\":\"Bearer\"}";
174 const char valid_sts_json_response[] =
175 "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\","
176 " \"expires_in\":3599, "
177 " \"issued_token_type\":\"urn:ietf:params:oauth:token-type:access_token\", "
178 " \"token_type\":\"Bearer\"}";
182 const char test_signed_jwt[] =
183 "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImY0OTRkN2M1YWU2MGRmOTcyNmM4YW"
184 "U0MDcyZTViYTdmZDkwODg2YzcifQ";
185 const char test_signed_jwt_token_type[] =
186 "urn:ietf:params:oauth:token-type:id_token";
187 const char test_signed_jwt2[] =
188 "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImY0OTRkN2M1YWU2MGRmOTcyNmM5YW"
189 "U2MDcyZTViYTdnZDkwODg5YzcifQ";
190 const char test_signed_jwt_token_type2[] =
191 "urn:ietf:params:oauth:token-type:jwt";
192 const char test_signed_jwt_path_prefix[] =
"test_sign_jwt";
195 const char test_service_url_no_service_name[] =
"https://foo.com/";
196 const char other_test_service_url_no_service_name[] =
"https://bar.com/";
197 const char test_method[] =
"ThisIsNotAMethod";
199 const char kTestUrlScheme[] =
"https";
200 const char kTestAuthority[] =
"foo.com";
201 const char kTestPath[] =
"/foo.v1/ThisIsNotAMethod";
202 const char kTestOtherAuthority[] =
"bar.com";
203 const char kTestOtherPath[] =
"/bar.v1/ThisIsNotAMethod";
205 const char test_sts_endpoint_url[] =
"https://foo.com:5555/v1/token-exchange";
207 const char valid_external_account_creds_token_exchange_response[] =
208 "{\"access_token\":\"token_exchange_access_token\","
209 " \"expires_in\":3599,"
210 " \"token_type\":\"Bearer\"}";
213 valid_external_account_creds_service_account_impersonation_response[] =
214 "{\"accessToken\":\"service_account_impersonation_access_token\","
215 " \"expireTime\":\"2050-01-01T00:00:00Z\"}";
218 valid_url_external_account_creds_options_credential_source_format_text[] =
219 "{\"url\":\"https://foo.com:5555/generate_subject_token_format_text\","
220 "\"headers\":{\"Metadata-Flavor\":\"Google\"}}";
223 valid_url_external_account_creds_options_credential_source_with_qurey_params_format_text
224 [] =
"{\"url\":\"https://foo.com:5555/"
225 "path/to/url/creds?p1=v1&p2=v2\","
226 "\"headers\":{\"Metadata-Flavor\":\"Google\"}}";
229 valid_url_external_account_creds_retrieve_subject_token_response_format_text
230 [] =
"test_subject_token";
233 valid_url_external_account_creds_options_credential_source_format_json[] =
234 "{\"url\":\"https://foo.com:5555/generate_subject_token_format_json\","
235 "\"headers\":{\"Metadata-Flavor\":\"Google\"},"
236 "\"format\":{\"type\":\"json\",\"subject_token_field_name\":\"access_"
240 valid_url_external_account_creds_retrieve_subject_token_response_format_json
241 [] =
"{\"access_token\":\"test_subject_token\"}";
243 const char invalid_url_external_account_creds_options_credential_source[] =
244 "{\"url\":\"invalid_credential_source_url\","
245 "\"headers\":{\"Metadata-Flavor\":\"Google\"}}";
247 const char valid_aws_external_account_creds_retrieve_signing_keys_response[] =
248 "{\"AccessKeyId\":\"test_access_key_id\",\"SecretAccessKey\":"
249 "\"test_secret_access_key\",\"Token\":\"test_token\"}";
251 const char aws_imdsv2_session_token[] =
"imdsv2_session_token";
253 const char valid_aws_external_account_creds_options_credential_source[] =
254 "{\"environment_id\":\"aws1\","
255 "\"region_url\":\"https://foo.com:5555/region_url\","
256 "\"url\":\"https://foo.com:5555/url\","
257 "\"regional_cred_verification_url\":\"https://foo.com:5555/"
258 "regional_cred_verification_url_{region}\"}";
260 const char valid_aws_imdsv2_external_account_creds_options_credential_source[] =
261 "{\"environment_id\":\"aws1\","
262 "\"region_url\":\"https://foo.com:5555/region_url\","
263 "\"url\":\"https://foo.com:5555/url\","
264 "\"imdsv2_session_token_url\":\"https://foo.com:5555/"
265 "imdsv2_session_token_url\","
266 "\"regional_cred_verification_url\":\"https://foo.com:5555/"
267 "regional_cred_verification_url_{region}\"}";
270 invalid_aws_external_account_creds_options_credential_source_unmatched_environment_id
271 [] =
"{\"environment_id\":\"unsupported_aws_version\","
272 "\"region_url\":\"https://foo.com:5555/region_url\","
273 "\"url\":\"https://foo.com:5555/url\","
274 "\"regional_cred_verification_url\":\"https://foo.com:5555/"
275 "regional_cred_verification_url_{region}\"}";
278 invalid_aws_external_account_creds_options_credential_source_invalid_region_url
279 [] =
"{\"environment_id\":\"aws1\","
280 "\"region_url\":\"invalid_region_url\","
281 "\"url\":\"https://foo.com:5555/url\","
282 "\"regional_cred_verification_url\":\"https://foo.com:5555/"
283 "regional_cred_verification_url_{region}\"}";
286 invalid_aws_external_account_creds_options_credential_source_invalid_url[] =
287 "{\"environment_id\":\"aws1\","
288 "\"region_url\":\"https://foo.com:5555/region_url\","
289 "\"url\":\"invalid_url\","
290 "\"regional_cred_verification_url\":\"https://foo.com:5555/"
291 "regional_cred_verification_url_{region}\"}";
294 invalid_aws_external_account_creds_options_credential_source_missing_role_name
295 [] =
"{\"environment_id\":\"aws1\","
296 "\"region_url\":\"https://foo.com:5555/region_url\","
297 "\"url\":\"https://foo.com:5555/url_no_role_name\","
298 "\"regional_cred_verification_url\":\"https://foo.com:5555/"
299 "regional_cred_verification_url_{region}\"}";
302 invalid_aws_external_account_creds_options_credential_source_invalid_regional_cred_verification_url
303 [] =
"{\"environment_id\":\"aws1\","
304 "\"region_url\":\"https://foo.com:5555/region_url\","
305 "\"url\":\"https://foo.com:5555/url_no_role_name\","
306 "\"regional_cred_verification_url\":\"invalid_regional_cred_"
307 "verification_url\"}";
311 bool g_test_is_on_gce =
false;
313 bool g_test_gce_tenancy_checker_called =
false;
336 response.body_length = strlen(body);
342 TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingOk) {
348 &
response, &token_value, &token_lifetime) ==
352 "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_");
356 TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingBadHttpStatus) {
362 &
response, &token_value, &token_lifetime) ==
367 TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingEmptyHttpBody) {
373 &
response, &token_value, &token_lifetime) ==
378 TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingInvalidJson) {
384 "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\","
385 " \"expires_in\":3599, "
386 " \"token_type\":\"Bearer\"");
388 &
response, &token_value, &token_lifetime) ==
393 TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingMissingToken) {
399 " \"expires_in\":3599, "
400 " \"token_type\":\"Bearer\"}");
402 &
response, &token_value, &token_lifetime) ==
407 TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingMissingTokenType) {
413 "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\","
414 " \"expires_in\":3599, "
417 &
response, &token_value, &token_lifetime) ==
422 TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingMissingTokenLifetime) {
428 "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\","
429 " \"token_type\":\"Bearer\"}");
431 &
response, &token_value, &token_lifetime) ==
436 class RequestMetadataState :
public RefCounted<RequestMetadataState> {
438 static RefCountedPtr<RequestMetadataState> NewInstance(
440 return MakeRefCounted<RequestMetadataState>(
451 ~RequestMetadataState()
override {
456 const char*
url_scheme,
const char* authority,
460 MakeRefCounted<BogusSecurityConnector>(
url_scheme);
470 GPR_ASSERT(metadata->get() == &md_);
475 ExecCtxWakeupScheduler(),
477 self->CheckRequestMetadata(
511 HandshakeManager*)
override {
550 TEST(CredentialsTest, TestGoogleIamCreds) {
552 auto state = RequestMetadataState::NewInstance(
555 test_google_iam_authorization_token,
", ",
557 test_google_iam_authority_selector));
559 test_google_iam_authorization_token, test_google_iam_authority_selector,
563 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
568 TEST(CredentialsTest, TestAccessTokenCreds) {
571 "authorization: Bearer blah");
577 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
584 RefCountedPtr<grpc_channel_security_connector> create_security_connector(
585 RefCountedPtr<grpc_call_credentials>
call_creds,
const char* ,
594 static UniqueTypeName
Type() {
595 static UniqueTypeName::Factory kFactory(
"check_channel_oauth2");
596 return kFactory.Create();
599 UniqueTypeName
type()
const override {
return Type(); }
609 TEST(CredentialsTest, TestChannelOauth2CompositeCreds) {
625 TEST(CredentialsTest, TestOauth2GoogleIamCompositeCreds) {
627 auto state = RequestMetadataState::NewInstance(
630 test_oauth2_bearer_token,
", ",
632 test_google_iam_authorization_token,
", ",
634 test_google_iam_authority_selector));
636 "authorization", test_oauth2_bearer_token);
642 test_google_iam_authorization_token, test_google_iam_authority_selector,
651 oauth2_creds->
Unref();
652 google_iam_creds->
Unref();
661 state->RunRequestMetadataTest(composite_creds, kTestUrlScheme, kTestAuthority,
663 composite_creds->
Unref();
668 RefCountedPtr<grpc_channel_security_connector> create_security_connector(
669 RefCountedPtr<grpc_call_credentials>
call_creds,
const char* ,
683 static UniqueTypeName
Type() {
684 static UniqueTypeName::Factory kFactory(
"check_channel_oauth2_google_iam");
685 return kFactory.Create();
688 UniqueTypeName
type()
const override {
return Type(); }
698 TEST(CredentialsTest, TestChannelOauth2GoogleIamCompositeCreds) {
702 new check_channel_oauth2_google_iam();
709 test_google_iam_authorization_token, test_google_iam_authority_selector,
714 google_iam_creds,
nullptr);
727 const char* host,
const char*
path) {
728 GPR_ASSERT(strcmp(host,
"metadata.google.internal.") == 0);
731 "/computeMetadata/v1/instance/service-accounts/default/token") ==
738 int compute_engine_httpcli_get_success_override(
742 validate_compute_engine_http_request(
request, host,
path);
748 int compute_engine_httpcli_get_failure_override(
752 validate_compute_engine_http_request(
request, host,
path);
760 const char* ,
const char* ,
size_t ,
763 GPR_ASSERT(
"HTTP POST should not be called" ==
nullptr);
768 const char* ,
const char* ,
772 GPR_ASSERT(
"HTTP GET should not be called" ==
nullptr);
777 const char* ,
const char* ,
783 GPR_ASSERT(
"HTTP PUT should not be called" ==
nullptr);
787 TEST(CredentialsTest, TestComputeEngineCredsSuccess) {
789 std::string emd =
"authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_";
790 const char expected_creds_debug_string[] =
791 "GoogleComputeEngineTokenFetcherCredentials{"
792 "OAuth2TokenFetcherCredentials}";
803 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
812 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
817 strcmp(creds->
debug_string().c_str(), expected_creds_debug_string) == 0);
822 TEST(CredentialsTest, TestComputeEngineCredsFailure) {
824 const char expected_creds_debug_string[] =
825 "GoogleComputeEngineTokenFetcherCredentials{"
826 "OAuth2TokenFetcherCredentials}";
827 auto state = RequestMetadataState::NewInstance(
829 "Error occurred when fetching oauth2 token."),
836 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
839 strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0);
845 const char* host,
const char*
path,
846 const char* body,
size_t body_size) {
852 "32555999999.apps.googleusercontent.com",
"EmssLNjJy1332hD4KFsecret",
853 "1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42");
854 GPR_ASSERT(expected_body.size() == body_size);
855 GPR_ASSERT(memcmp(expected_body.data(), body, body_size) == 0);
861 strcmp(
request->hdrs[0].value,
"application/x-www-form-urlencoded") == 0);
865 const char* host,
const char*
path,
866 const char* body,
size_t body_size,
870 validate_refresh_token_http_request(
request, host,
path, body, body_size);
877 const char* ,
const char* ,
878 const char* ,
size_t ,
886 TEST(CredentialsTest, TestRefreshTokenCredsSuccess) {
888 std::string emd =
"authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_";
889 const char expected_creds_debug_string[] =
890 "GoogleRefreshToken{ClientID:32555999999.apps.googleusercontent.com,"
891 "OAuth2TokenFetcherCredentials}";
901 refresh_token_httpcli_post_success,
903 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
912 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
916 strcmp(creds->
debug_string().c_str(), expected_creds_debug_string) == 0);
922 TEST(CredentialsTest, TestRefreshTokenCredsFailure) {
924 const char expected_creds_debug_string[] =
925 "GoogleRefreshToken{ClientID:32555999999.apps.googleusercontent.com,"
926 "OAuth2TokenFetcherCredentials}";
927 auto state = RequestMetadataState::NewInstance(
929 "Error occurred when fetching oauth2 token."),
934 token_httpcli_post_failure,
936 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
939 strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0);
945 TEST(CredentialsTest, TestValidStsCredsOptions) {
947 test_sts_endpoint_url,
952 test_signed_jwt_path_prefix,
953 test_signed_jwt_token_type,
966 TEST(CredentialsTest, TestInvalidStsCredsOptions) {
968 test_sts_endpoint_url,
974 test_signed_jwt_token_type,
983 test_sts_endpoint_url,
988 test_signed_jwt_path_prefix,
1002 test_signed_jwt_path_prefix,
1003 test_signed_jwt_token_type,
1016 test_signed_jwt_path_prefix,
1017 test_signed_jwt_token_type,
1025 "ftp://ftp.is.not.a.valid.scheme/bar",
1030 test_signed_jwt_path_prefix,
1031 test_signed_jwt_token_type,
1041 const auto it = uri.query_parameter_map().find(expected_key);
1043 if (
it->second != expected_val) {
1051 const char* host,
const char*
path,
1052 const char* body,
size_t body_size,
1053 bool expect_actor_token) {
1064 assert_query_parameters(*
url,
"resource",
"resource");
1065 assert_query_parameters(*
url,
"audience",
"audience");
1066 assert_query_parameters(*
url,
"scope",
"scope");
1067 assert_query_parameters(*
url,
"requested_token_type",
"requested_token_type");
1068 assert_query_parameters(*
url,
"subject_token", test_signed_jwt);
1069 assert_query_parameters(*
url,
"subject_token_type",
1070 test_signed_jwt_token_type);
1071 if (expect_actor_token) {
1072 assert_query_parameters(*
url,
"actor_token", test_signed_jwt2);
1073 assert_query_parameters(*
url,
"actor_token_type",
1074 test_signed_jwt_token_type2);
1076 GPR_ASSERT(
url->query_parameter_map().find(
"actor_token") ==
1077 url->query_parameter_map().end());
1078 GPR_ASSERT(
url->query_parameter_map().find(
"actor_token_type") ==
1079 url->query_parameter_map().end());
1083 GPR_ASSERT(strcmp(host,
"foo.com:5555") == 0);
1088 strcmp(
request->hdrs[0].value,
"application/x-www-form-urlencoded") == 0);
1092 const char* host,
const char*
path,
1093 const char* body,
size_t body_size,
1097 validate_sts_token_http_request(
request, host,
path, body, body_size,
true);
1103 int sts_token_httpcli_post_success_no_actor_token(
1105 const char* body,
size_t body_size,
Timestamp ,
1107 validate_sts_token_http_request(
request, host,
path, body, body_size,
false);
1113 char* write_tmp_jwt_file(
const char* jwt_contents) {
1118 size_t jwt_length = strlen(jwt_contents);
1119 GPR_ASSERT(fwrite(jwt_contents, 1, jwt_length,
tmp) == jwt_length);
1124 TEST(CredentialsTest, TestStsCredsSuccess) {
1126 std::string emd =
"authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_";
1127 const char expected_creds_debug_string[] =
1128 "StsTokenFetcherCredentials{Path:/v1/"
1129 "token-exchange,Authority:foo.com:5555,OAuth2TokenFetcherCredentials}";
1130 char* subject_token_path = write_tmp_jwt_file(test_signed_jwt);
1131 char* actor_token_path = write_tmp_jwt_file(test_signed_jwt2);
1133 test_sts_endpoint_url,
1137 "requested_token_type",
1139 test_signed_jwt_token_type,
1141 test_signed_jwt_token_type2
1152 sts_token_httpcli_post_success,
1154 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
1163 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
1167 strcmp(creds->
debug_string().c_str(), expected_creds_debug_string) == 0);
1175 TEST(CredentialsTest, TestStsCredsTokenFileNotFound) {
1178 test_sts_endpoint_url,
1182 "requested_token_type",
1183 "/some/completely/random/path",
1184 test_signed_jwt_token_type,
1194 auto state = RequestMetadataState::NewInstance(
1196 "Error occurred when fetching oauth2 token."),
1201 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
1210 TEST(CredentialsTest, TestStsCredsNoActorTokenSuccess) {
1212 std::string emd =
"authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_";
1213 const char expected_creds_debug_string[] =
1214 "StsTokenFetcherCredentials{Path:/v1/"
1215 "token-exchange,Authority:foo.com:5555,OAuth2TokenFetcherCredentials}";
1216 char* subject_token_path = write_tmp_jwt_file(test_signed_jwt);
1218 test_sts_endpoint_url,
1222 "requested_token_type",
1224 test_signed_jwt_token_type,
1237 sts_token_httpcli_post_success_no_actor_token,
1239 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
1248 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
1252 strcmp(creds->
debug_string().c_str(), expected_creds_debug_string) == 0);
1259 TEST(CredentialsTest, TestStsCredsLoadTokenFailure) {
1260 const char expected_creds_debug_string[] =
1261 "StsTokenFetcherCredentials{Path:/v1/"
1262 "token-exchange,Authority:foo.com:5555,OAuth2TokenFetcherCredentials}";
1264 auto state = RequestMetadataState::NewInstance(
1266 "Error occurred when fetching oauth2 token."),
1268 char* test_signed_jwt_path = write_tmp_jwt_file(test_signed_jwt);
1270 test_sts_endpoint_url,
1274 "requested_token_type",
1276 test_signed_jwt_token_type,
1284 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
1287 strcmp(creds->
debug_string().c_str(), expected_creds_debug_string) == 0);
1294 TEST(CredentialsTest, TestStsCredsHttpFailure) {
1295 const char expected_creds_debug_string[] =
1296 "StsTokenFetcherCredentials{Path:/v1/"
1297 "token-exchange,Authority:foo.com:5555,OAuth2TokenFetcherCredentials}";
1299 auto state = RequestMetadataState::NewInstance(
1301 "Error occurred when fetching oauth2 token."),
1303 char* test_signed_jwt_path = write_tmp_jwt_file(test_signed_jwt);
1305 test_sts_endpoint_url,
1309 "requested_token_type",
1310 test_signed_jwt_path,
1311 test_signed_jwt_token_type,
1318 token_httpcli_post_failure,
1320 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
1323 strcmp(creds->
debug_string().c_str(), expected_creds_debug_string) == 0);
1336 strcmp(json_key->
type,
"service_account") == 0);
1339 "e6b5137873db8d2ef81e06a47289e6434ec8a165") == 0);
1342 "777-abaslkan11hlb6nmim3bpspl31ud.apps."
1343 "googleusercontent.com") == 0);
1346 "777-abaslkan11hlb6nmim3bpspl31ud@developer."
1347 "gserviceaccount.com") == 0);
1353 const char* audience,
1355 const char* scope) {
1356 if (strcmp(audience, test_service_url_no_service_name) != 0 &&
1357 strcmp(audience, other_test_service_url_no_service_name) != 0) {
1360 validate_jwt_encode_and_sign_params(json_key, scope, token_lifetime);
1367 const char* scope) {
1368 validate_jwt_encode_and_sign_params(json_key, scope, token_lifetime);
1372 char* encode_and_sign_jwt_should_not_be_called(
1375 GPR_ASSERT(
"grpc_jwt_encode_and_sign should not be called" ==
nullptr);
1387 TEST(CredentialsTest, TestJwtCredsLifetime) {
1389 const char expected_creds_debug_string_prefix[] =
1390 "JWTAccessCredentials{ExpirationTime:";
1399 GPR_ASSERT(strncmp(expected_creds_debug_string_prefix,
1401 strlen(expected_creds_debug_string_prefix)) == 0);
1408 json_key_string, token_lifetime,
nullptr);
1410 token_lifetime) == 0);
1411 GPR_ASSERT(strncmp(expected_creds_debug_string_prefix,
1413 strlen(expected_creds_debug_string_prefix)) == 0);
1420 json_key_string, token_lifetime,
nullptr);
1423 GPR_ASSERT(strncmp(expected_creds_debug_string_prefix,
1425 strlen(expected_creds_debug_string_prefix)) == 0);
1431 TEST(CredentialsTest, TestRemoveServiceFromJwtUri) {
1432 const char wrong_uri[] =
"hello world";
1434 const char valid_uri[] =
"https://foo.com/get/";
1435 const char expected_uri[] =
"https://foo.com/";
1441 TEST(CredentialsTest, TestJwtCredsSuccess) {
1442 const char expected_creds_debug_string_prefix[] =
1443 "JWTAccessCredentials{ExpirationTime:";
1456 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
1463 encode_and_sign_jwt_should_not_be_called);
1464 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
1472 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestOtherAuthority,
1475 GPR_ASSERT(strncmp(expected_creds_debug_string_prefix,
1477 strlen(expected_creds_debug_string_prefix)) == 0);
1484 TEST(CredentialsTest, TestJwtCredsSigningFailure) {
1485 const char expected_creds_debug_string_prefix[] =
1486 "JWTAccessCredentials{ExpirationTime:";
1489 auto state = RequestMetadataState::NewInstance(
1496 state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
1500 GPR_ASSERT(strncmp(expected_creds_debug_string_prefix,
1501 creds->debug_string().c_str(),
1502 strlen(expected_creds_debug_string_prefix)) == 0);
1508 void set_google_default_creds_env_var_with_file_contents(
1510 size_t contents_len = strlen(
contents);
1521 bool test_gce_tenancy_checker(
void) {
1522 g_test_gce_tenancy_checker_called =
true;
1523 return g_test_is_on_gce;
1526 std::string null_well_known_creds_path_getter(
void) {
return ""; }
1528 TEST(CredentialsTest, TestGoogleDefaultCredsAuthKey) {
1534 g_test_gce_tenancy_checker_called =
false;
1535 g_test_is_on_gce =
true;
1536 set_google_default_creds_env_var_with_file_contents(
1537 "json_key_google_default_creds", json_key);
1539 null_well_known_creds_path_getter);
1543 auto* default_creds =
1546 GPR_ASSERT(default_creds->ssl_creds() !=
nullptr);
1551 strcmp(jwt->key().client_id,
1552 "777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent.com") ==
1554 GPR_ASSERT(g_test_gce_tenancy_checker_called ==
false);
1560 TEST(CredentialsTest, TestGoogleDefaultCredsRefreshToken) {
1564 set_google_default_creds_env_var_with_file_contents(
1567 null_well_known_creds_path_getter);
1570 auto* default_creds =
1573 GPR_ASSERT(default_creds->ssl_creds() !=
nullptr);
1577 GPR_ASSERT(strcmp(refresh->refresh_token().client_id,
1578 "32555999999.apps.googleusercontent.com") == 0);
1584 TEST(CredentialsTest, TestGoogleDefaultCredsExternalAccountCredentials) {
1588 set_google_default_creds_env_var_with_file_contents(
1589 "google_default_creds_external_account_credentials",
1590 test_external_account_credentials_str);
1592 null_well_known_creds_path_getter);
1595 auto* default_creds =
1598 GPR_ASSERT(default_creds->ssl_creds() !=
nullptr);
1607 TEST(CredentialsTest,
1608 TestGoogleDefaultCredsExternalAccountCredentialsMultiPatternSts) {
1612 set_google_default_creds_env_var_with_file_contents(
1613 "google_default_creds_external_account_credentials_multi_pattern_sts",
1614 test_external_account_credentials_multi_pattern_sts_str);
1616 null_well_known_creds_path_getter);
1619 auto* default_creds =
1622 GPR_ASSERT(default_creds->ssl_creds() !=
nullptr);
1631 TEST(CredentialsTest,
1632 TestGoogleDefaultCredsExternalAccountCredentialsMultiPatternIam) {
1636 set_google_default_creds_env_var_with_file_contents(
1637 "google_default_creds_external_account_credentials_multi_pattern_iam",
1638 test_external_account_credentials_multi_pattern_iam_str);
1640 null_well_known_creds_path_getter);
1643 auto* default_creds =
1646 GPR_ASSERT(default_creds->ssl_creds() !=
nullptr);
1655 int default_creds_metadata_server_detection_httpcli_get_success_override(
1667 GPR_ASSERT(strcmp(host,
"metadata.google.internal.") == 0);
1672 TEST(CredentialsTest, TestGoogleDefaultCredsGce) {
1674 auto state = RequestMetadataState::NewInstance(
1676 "authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_");
1680 null_well_known_creds_path_getter);
1682 g_test_gce_tenancy_checker_called =
false;
1683 g_test_is_on_gce =
true;
1697 kTestAuthority, kTestPath);
1700 GPR_ASSERT(g_test_gce_tenancy_checker_called ==
true);
1708 TEST(CredentialsTest, TestGoogleDefaultCredsNonGce) {
1710 auto state = RequestMetadataState::NewInstance(
1712 "authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_");
1716 null_well_known_creds_path_getter);
1718 g_test_gce_tenancy_checker_called =
false;
1719 g_test_is_on_gce =
false;
1722 default_creds_metadata_server_detection_httpcli_get_success_override,
1734 kTestAuthority, kTestPath);
1736 GPR_ASSERT(g_test_gce_tenancy_checker_called ==
true);
1743 int default_creds_gce_detection_httpcli_get_failure_override(
1749 GPR_ASSERT(strcmp(host,
"metadata.google.internal.") == 0);
1755 TEST(CredentialsTest, TestNoGoogleDefaultCreds) {
1759 null_well_known_creds_path_getter);
1761 g_test_gce_tenancy_checker_called =
false;
1762 g_test_is_on_gce =
false;
1764 default_creds_gce_detection_httpcli_get_failure_override,
1769 g_test_gce_tenancy_checker_called =
false;
1771 GPR_ASSERT(g_test_gce_tenancy_checker_called ==
true);
1777 TEST(CredentialsTest, TestGoogleDefaultCredsCallCredsSpecified) {
1778 auto state = RequestMetadataState::NewInstance(
1780 "authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_");
1786 g_test_gce_tenancy_checker_called =
false;
1787 g_test_is_on_gce =
true;
1789 default_creds_metadata_server_detection_httpcli_get_success_override,
1794 GPR_ASSERT(g_test_gce_tenancy_checker_called ==
false);
1801 kTestUrlScheme, kTestAuthority, kTestPath);
1803 channel_creds->
Unref();
1809 ArenaPromise<absl::StatusOr<ClientMetadataHandle>> GetRequestMetadata(
1817 UniqueTypeName
type()
const override {
1818 static UniqueTypeName::Factory kFactory(
"fake");
1819 return kFactory.Create();
1829 TEST(CredentialsTest, TestGoogleDefaultCredsNotDefault) {
1833 RefCountedPtr<grpc_call_credentials>
call_creds =
1834 MakeRefCounted<fake_call_creds>();
1836 g_test_gce_tenancy_checker_called =
false;
1837 g_test_is_on_gce =
true;
1839 default_creds_metadata_server_detection_httpcli_get_success_override,
1844 GPR_ASSERT(g_test_gce_tenancy_checker_called ==
false);
1848 kTestUrlScheme, kTestAuthority, kTestPath);
1850 channel_creds->
Unref();
1855 PLUGIN_INITIAL_STATE,
1856 PLUGIN_GET_METADATA_CALLED_STATE,
1857 PLUGIN_DESTROY_CALLED_STATE
1860 const std::map<std::string, std::string> plugin_md = {{
"foo",
"bar"},
1863 int plugin_get_metadata_success(
1875 *
s = PLUGIN_GET_METADATA_CALLED_STATE;
1877 for (
auto const&
md : plugin_md) {
1883 *num_creds_md = plugin_md.size();
1887 const char* plugin_error_details =
"Could not get metadata for plugin.";
1889 int plugin_get_metadata_failure(
1894 const char** error_details) {
1900 *
s = PLUGIN_GET_METADATA_CALLED_STATE;
1902 *error_details =
gpr_strdup(plugin_error_details);
1908 *
s = PLUGIN_DESTROY_CALLED_STATE;
1911 char* plugin_debug_string(
void*
state) {
1913 char*
ret =
nullptr;
1915 case PLUGIN_INITIAL_STATE:
1918 case PLUGIN_GET_METADATA_CALLED_STATE:
1919 gpr_asprintf(&
ret,
"TestPluginCredentials{state:GET_METADATA_CALLED}");
1921 case PLUGIN_DESTROY_CALLED_STATE:
1931 TEST(CredentialsTest, TestMetadataPluginSuccess) {
1932 const char expected_creds_debug_string[] =
1933 "TestPluginCredentials{state:GET_METADATA_CALLED}";
1938 RequestMetadataState::NewInstance(
GRPC_ERROR_NONE,
"foo: bar, hi: there");
1950 md_state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
1954 strcmp(creds->
debug_string().c_str(), expected_creds_debug_string) == 0);
1960 TEST(CredentialsTest, TestMetadataPluginFailure) {
1961 const char expected_creds_debug_string[] =
1962 "TestPluginCredentials{state:GET_METADATA_CALLED}";
1967 auto md_state = RequestMetadataState::NewInstance(
1969 absl::StrCat(
"Getting metadata from plugin failed with error: ",
1970 plugin_error_details)),
1981 md_state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority,
1985 strcmp(creds->
debug_string().c_str(), expected_creds_debug_string) == 0);
1991 TEST(CredentialsTest, TestGetWellKnownGoogleCredentialsFilePath) {
1993 bool restore_home_env =
false;
1994 #if defined(GRPC_BAZEL_BUILD) && \
1995 (defined(GPR_POSIX_ENV) || defined(GPR_LINUX_ENV))
1998 restore_home_env =
true;
2004 #if defined(GPR_POSIX_ENV) || defined(GPR_LINUX_ENV)
2005 restore_home_env =
true;
2010 if (restore_home_env) {
2020 TEST(CredentialsTest, TestChannelCredsDuplicateWithoutCallCreds) {
2021 const char expected_creds_debug_string[] =
2022 "AccessTokenCredentials{Token:present}";
2028 RefCountedPtr<grpc_channel_credentials> dup =
2039 expected_creds_debug_string) == 0);
2046 channel_creds->
Unref();
2047 composite_creds->
Unref();
2056 } auth_metadata_context_test_case;
2058 void auth_metadata_context_build(
const char*
url_scheme,
2064 char* last_slash = strrchr(
service,
'/');
2068 if (last_slash ==
nullptr) {
2072 }
else if (last_slash ==
service) {
2081 char* port_delimiter = strrchr(host_and_port,
':');
2082 if (port_delimiter !=
nullptr && strcmp(port_delimiter + 1,
"443") == 0) {
2083 *port_delimiter =
'\0';
2091 auth_context ==
nullptr
2099 TEST(CredentialsTest, TestAuthMetadataContext) {
2100 auth_metadata_context_test_case
test_cases[] = {
2102 {
"https",
"www.foo.com",
"",
"https://www.foo.com",
""},
2104 {
"https",
"www.foo.com",
"/Service",
"https://www.foo.com/Service",
""},
2106 {
"https",
"www.foo.com",
"//",
"https://www.foo.com/",
""},
2108 {
"https",
"www.foo.com",
"/Service/",
"https://www.foo.com/Service",
""},
2110 {
"https",
"www.foo.com:",
"/Service/",
"https://www.foo.com:/Service",
2113 {
"https",
"www.foo.com:443",
"/Service/FooMethod",
2114 "https://www.foo.com/Service",
"FooMethod"},
2116 {
"https",
"www.foo.com",
"/Service/FooMethod",
2117 "https://www.foo.com/Service",
"FooMethod"},
2119 {
"https",
"[1080:0:0:0:8:800:200C:417A]:443",
"/Service/FooMethod",
2120 "https://[1080:0:0:0:8:800:200C:417A]/Service",
"FooMethod"},
2122 {
"https",
"[1080:0:0:0:8:800:200C:443]",
"/Service/FooMethod",
2123 "https://[1080:0:0:0:8:800:200C:443]/Service",
"FooMethod"},
2125 {
"https",
"www.foo.com:8888",
"/Service/FooMethod",
2126 "https://www.foo.com:8888/Service",
"FooMethod"},
2128 {
"https",
"[1080:0:0:0:8:800:200C:417A]:8888",
"/Service/FooMethod",
2129 "https://[1080:0:0:0:8:800:200C:417A]:8888/Service",
"FooMethod"},
2131 {
"blah",
"www.foo.com:443",
"/Service/FooMethod",
2132 "blah://www.foo.com:443/Service",
"FooMethod"}};
2140 memset(&auth_md_context, 0,
sizeof(auth_md_context));
2162 void validate_external_account_creds_token_exchage_request(
2164 const char* body,
size_t body_size,
bool ) {
2175 assert_query_parameters(*uri,
"audience",
"audience");
2176 assert_query_parameters(*uri,
"grant_type",
2177 "urn:ietf:params:oauth:grant-type:token-exchange");
2178 assert_query_parameters(*uri,
"requested_token_type",
2179 "urn:ietf:params:oauth:token-type:access_token");
2180 assert_query_parameters(*uri,
"subject_token",
"test_subject_token");
2181 assert_query_parameters(*uri,
"subject_token_type",
"subject_token_type");
2182 assert_query_parameters(*uri,
"scope",
2183 "https://www.googleapis.com/auth/cloud-platform");
2186 GPR_ASSERT(strcmp(host,
"foo.com:5555") == 0);
2191 strcmp(
request->hdrs[0].value,
"application/x-www-form-urlencoded") == 0);
2194 "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=") == 0);
2197 void validate_external_account_creds_token_exchage_request_with_url_encode(
2199 const char* body,
size_t body_size,
bool ) {
2206 "audience=audience_!%40%23%24&grant_type=urn%3Aietf%3Aparams%3Aoauth%"
2207 "3Agrant-type%3Atoken-exchange&requested_token_type=urn%3Aietf%"
2208 "3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&subject_token_type="
2209 "subject_token_type_!%40%23%24&subject_token=test_subject_token&"
2210 "scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform&"
2211 "options=%7B%7D") == 0);
2214 GPR_ASSERT(strcmp(host,
"foo.com:5555") == 0);
2219 strcmp(
request->hdrs[0].value,
"application/x-www-form-urlencoded") == 0);
2222 "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=") == 0);
2225 void validate_external_account_creds_service_account_impersonation_request(
2227 const char* body,
size_t body_size,
bool ) {
2231 GPR_ASSERT(strcmp(body,
"scope=scope_1 scope_2") == 0);
2233 GPR_ASSERT(strcmp(host,
"foo.com:5555") == 0);
2238 strcmp(
request->hdrs[0].value,
"application/x-www-form-urlencoded") == 0);
2241 "Bearer token_exchange_access_token") == 0);
2244 int external_account_creds_httpcli_post_success(
2246 const char* body,
size_t body_size,
Timestamp ,
2248 if (strcmp(
path,
"/token") == 0) {
2249 validate_external_account_creds_token_exchage_request(
2252 200, valid_external_account_creds_token_exchange_response);
2253 }
else if (strcmp(
path,
"/service_account_impersonation") == 0) {
2254 validate_external_account_creds_service_account_impersonation_request(
2258 valid_external_account_creds_service_account_impersonation_response);
2259 }
else if (strcmp(
path,
"/token_url_encode") == 0) {
2260 validate_external_account_creds_token_exchage_request_with_url_encode(
2263 200, valid_external_account_creds_token_exchange_response);
2269 int external_account_creds_httpcli_post_failure_token_exchange_response_missing_access_token(
2271 const char*
path,
const char* ,
size_t ,
2274 if (strcmp(
path,
"/token") == 0) {
2276 "{\"not_access_token\":\"not_access_token\","
2277 "\"expires_in\":3599,"
2278 " \"token_type\":\"Bearer\"}");
2279 }
else if (strcmp(
path,
"/service_account_impersonation") == 0) {
2282 valid_external_account_creds_service_account_impersonation_response);
2288 int url_external_account_creds_httpcli_get_success(
2292 if (strcmp(
path,
"/generate_subject_token_format_text") == 0) {
2295 valid_url_external_account_creds_retrieve_subject_token_response_format_text);
2296 }
else if (strcmp(
path,
"/path/to/url/creds?p1=v1&p2=v2") == 0) {
2299 valid_url_external_account_creds_retrieve_subject_token_response_format_text);
2300 }
else if (strcmp(
path,
"/generate_subject_token_format_json") == 0) {
2303 valid_url_external_account_creds_retrieve_subject_token_response_format_json);
2309 void validate_aws_external_account_creds_token_exchage_request(
2311 const char* body,
size_t body_size,
bool ) {
2317 GPR_ASSERT(strstr(body,
"regional_cred_verification_url_test_region"));
2322 assert_query_parameters(*uri,
"audience",
"audience");
2323 assert_query_parameters(*uri,
"grant_type",
2324 "urn:ietf:params:oauth:grant-type:token-exchange");
2325 assert_query_parameters(*uri,
"requested_token_type",
2326 "urn:ietf:params:oauth:token-type:access_token");
2327 assert_query_parameters(*uri,
"subject_token_type",
"subject_token_type");
2328 assert_query_parameters(*uri,
"scope",
2329 "https://www.googleapis.com/auth/cloud-platform");
2331 GPR_ASSERT(strcmp(host,
"foo.com:5555") == 0);
2336 strcmp(
request->hdrs[0].value,
"application/x-www-form-urlencoded") == 0);
2339 "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=") == 0);
2342 int aws_external_account_creds_httpcli_get_success(
2346 if (strcmp(
path,
"/region_url") == 0) {
2348 }
else if (strcmp(
path,
"/url") == 0) {
2350 }
else if (strcmp(
path,
"/url_no_role_name") == 0) {
2352 }
else if (strcmp(
path,
"/url/test_role_name") == 0) {
2354 200, valid_aws_external_account_creds_retrieve_signing_keys_response);
2360 int aws_imdsv2_external_account_creds_httpcli_get_success(
2366 return aws_external_account_creds_httpcli_get_success(
2370 int aws_imdsv2_external_account_creds_httpcli_put_success(
2376 "x-aws-ec2-metadata-token-ttl-seconds") == 0);
2384 int aws_external_account_creds_httpcli_post_success(
2386 const char* body,
size_t body_size,
Timestamp ,
2388 if (strcmp(
path,
"/token") == 0) {
2389 validate_aws_external_account_creds_token_exchage_request(
2392 200, valid_external_account_creds_token_exchange_response);
2403 TestExternalAccountCredentials(Options
options,
2404 std::vector<std::string> scopes)
2408 void RetrieveSubjectToken(
2409 HTTPRequestContext* ,
const Options& ,
2415 TEST(CredentialsTest, TestExternalAccountCredsSuccess) {
2417 Json credential_source(
"");
2418 TestExternalAccountCredentials::Options
options = {
2421 "subject_token_type",
2423 "https://foo.com:5555/token",
2424 "https://foo.com:5555/token_info",
2431 TestExternalAccountCredentials creds(
options, {});
2435 auto state = RequestMetadataState::NewInstance(
2436 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
2438 external_account_creds_httpcli_post_success,
2440 state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority,
2444 state = RequestMetadataState::NewInstance(
2445 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
2449 state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority,
2455 TEST(CredentialsTest, TestExternalAccountCredsSuccessWithUrlEncode) {
2456 std::map<std::string, std::string> emd = {
2457 {
"authorization",
"Bearer token_exchange_access_token"}};
2459 Json credential_source(
"");
2460 TestExternalAccountCredentials::Options
options = {
2463 "subject_token_type_!@#$",
2465 "https://foo.com:5555/token_url_encode",
2466 "https://foo.com:5555/token_info",
2473 TestExternalAccountCredentials creds(
options, {});
2474 auto state = RequestMetadataState::NewInstance(
2475 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
2477 external_account_creds_httpcli_post_success,
2479 state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority,
2485 TEST(CredentialsTest,
2486 TestExternalAccountCredsSuccessWithServiceAccountImpersonation) {
2488 Json credential_source(
"");
2489 TestExternalAccountCredentials::Options
options = {
2492 "subject_token_type",
2493 "https://foo.com:5555/service_account_impersonation",
2494 "https://foo.com:5555/token",
2495 "https://foo.com:5555/token_info",
2502 TestExternalAccountCredentials creds(
options, {
"scope_1",
"scope_2"});
2506 auto state = RequestMetadataState::NewInstance(
2508 "authorization: Bearer service_account_impersonation_access_token");
2510 external_account_creds_httpcli_post_success,
2512 state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority,
2518 TEST(CredentialsTest, TestExternalAccountCredsFailureInvalidTokenUrl) {
2520 Json credential_source(
"");
2521 TestExternalAccountCredentials::Options
options = {
2524 "subject_token_type",
2525 "https://foo.com:5555/service_account_impersonation",
2526 "invalid_token_url",
2527 "https://foo.com:5555/token_info",
2534 TestExternalAccountCredentials creds(
options, {});
2539 "Invalid token url: invalid_token_url.");
2542 "Error occurred when fetching oauth2 token.", &
error, 1);
2543 auto state = RequestMetadataState::NewInstance(expected_error, {});
2544 state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority,
2551 TEST(CredentialsTest,
2552 TestExternalAccountCredsFailureInvalidServiceAccountImpersonationUrl) {
2554 Json credential_source(
"");
2555 TestExternalAccountCredentials::Options
options = {
2558 "subject_token_type",
2559 "invalid_service_account_impersonation_url",
2560 "https://foo.com:5555/token",
2561 "https://foo.com:5555/token_info",
2568 TestExternalAccountCredentials creds(
options, {});
2570 external_account_creds_httpcli_post_success,
2573 "Invalid service account impersonation url: "
2574 "invalid_service_account_impersonation_url.");
2577 "Error occurred when fetching oauth2 token.", &
error, 1);
2578 auto state = RequestMetadataState::NewInstance(expected_error, {});
2579 state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority,
2586 TEST(CredentialsTest,
2587 TestExternalAccountCredsFailureTokenExchangeResponseMissingAccessToken) {
2589 Json credential_source(
"");
2590 TestExternalAccountCredentials::Options
options = {
2593 "subject_token_type",
2594 "https://foo.com:5555/service_account_impersonation",
2595 "https://foo.com:5555/token",
2596 "https://foo.com:5555/token_info",
2603 TestExternalAccountCredentials creds(
options, {});
2606 external_account_creds_httpcli_post_failure_token_exchange_response_missing_access_token,
2609 "Missing or invalid access_token in "
2610 "{\"not_access_token\":\"not_access_token\",\"expires_in\":3599,\"token_"
2611 "type\":\"Bearer\"}.");
2614 "Error occurred when fetching oauth2 token.", &
error, 1);
2615 auto state = RequestMetadataState::NewInstance(expected_error, {});
2616 state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority,
2623 TEST(CredentialsTest, TestUrlExternalAccountCredsSuccessFormatText) {
2627 valid_url_external_account_creds_options_credential_source_format_text,
2630 ExternalAccountCredentials::Options
options = {
2633 "subject_token_type",
2635 "https://foo.com:5555/token",
2636 "https://foo.com:5555/token_info",
2647 auto state = RequestMetadataState::NewInstance(
2648 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
2650 external_account_creds_httpcli_post_success,
2652 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
2658 TEST(CredentialsTest,
2659 TestUrlExternalAccountCredsSuccessWithQureyParamsFormatText) {
2660 std::map<std::string, std::string> emd = {
2661 {
"authorization",
"Bearer token_exchange_access_token"}};
2665 valid_url_external_account_creds_options_credential_source_with_qurey_params_format_text,
2668 ExternalAccountCredentials::Options
options = {
2671 "subject_token_type",
2673 "https://foo.com:5555/token",
2674 "https://foo.com:5555/token_info",
2685 auto state = RequestMetadataState::NewInstance(
2686 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
2688 external_account_creds_httpcli_post_success,
2690 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
2696 TEST(CredentialsTest, TestUrlExternalAccountCredsSuccessFormatJson) {
2700 valid_url_external_account_creds_options_credential_source_format_json,
2703 ExternalAccountCredentials::Options
options = {
2706 "subject_token_type",
2708 "https://foo.com:5555/token",
2709 "https://foo.com:5555/token_info",
2720 auto state = RequestMetadataState::NewInstance(
2721 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
2723 external_account_creds_httpcli_post_success,
2725 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
2731 TEST(CredentialsTest,
2732 TestUrlExternalAccountCredsFailureInvalidCredentialSourceUrl) {
2735 invalid_url_external_account_creds_options_credential_source, &
error);
2737 ExternalAccountCredentials::Options
options = {
2740 "subject_token_type",
2742 "https://foo.com:5555/token",
2743 "https://foo.com:5555/token_info",
2759 TEST(CredentialsTest, TestFileExternalAccountCredsSuccessFormatText) {
2762 char* subject_token_path = write_tmp_jwt_file(
"test_subject_token");
2765 "{\"file\":\"%s\"}",
2769 ExternalAccountCredentials::Options
options = {
2772 "subject_token_type",
2774 "https://foo.com:5555/token",
2775 "https://foo.com:5555/token_info",
2786 auto state = RequestMetadataState::NewInstance(
2787 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
2789 external_account_creds_httpcli_post_success,
2791 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
2799 TEST(CredentialsTest, TestFileExternalAccountCredsSuccessFormatJson) {
2802 char* subject_token_path =
2803 write_tmp_jwt_file(
"{\"access_token\":\"test_subject_token\"}");
2807 "\"file\":\"%s\",\n"
2810 "\"type\":\"json\",\n"
2811 "\"subject_token_field_name\":\"access_token\"\n"
2817 ExternalAccountCredentials::Options
options = {
2820 "subject_token_type",
2822 "https://foo.com:5555/token",
2823 "https://foo.com:5555/token_info",
2834 auto state = RequestMetadataState::NewInstance(
2835 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
2837 external_account_creds_httpcli_post_success,
2839 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
2847 TEST(CredentialsTest, TestFileExternalAccountCredsFailureFileNotFound) {
2850 Json credential_source =
2853 ExternalAccountCredentials::Options
options = {
2856 "subject_token_type",
2858 "https://foo.com:5555/token",
2859 "https://foo.com:5555/token_info",
2875 "Error occurred when fetching oauth2 token.", &
error, 1);
2876 auto state = RequestMetadataState::NewInstance(expected_error, {});
2877 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
2884 TEST(CredentialsTest, TestFileExternalAccountCredsFailureInvalidJsonContent) {
2887 char* subject_token_path = write_tmp_jwt_file(
"not_a_valid_json_file");
2891 "\"file\":\"%s\",\n"
2894 "\"type\":\"json\",\n"
2895 "\"subject_token_field_name\":\"access_token\"\n"
2901 ExternalAccountCredentials::Options
options = {
2904 "subject_token_type",
2906 "https://foo.com:5555/token",
2907 "https://foo.com:5555/token_info",
2921 "The content of the file is not a valid json object.");
2924 "Error occurred when fetching oauth2 token.", &
error, 1);
2925 auto state = RequestMetadataState::NewInstance(expected_error, {});
2926 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
2934 TEST(CredentialsTest, TestAwsExternalAccountCredsSuccess) {
2938 valid_aws_external_account_creds_options_credential_source, &
error);
2940 ExternalAccountCredentials::Options
options = {
2943 "subject_token_type",
2945 "https://foo.com:5555/token",
2946 "https://foo.com:5555/token_info",
2957 auto state = RequestMetadataState::NewInstance(
2958 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
2960 aws_external_account_creds_httpcli_post_success,
2962 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
2968 TEST(CredentialsTest, TestAwsImdsv2ExternalAccountCredsSuccess) {
2972 valid_aws_imdsv2_external_account_creds_options_credential_source,
2975 ExternalAccountCredentials::Options
options = {
2978 "subject_token_type",
2980 "https://foo.com:5555/token",
2981 "https://foo.com:5555/token_info",
2992 auto state = RequestMetadataState::NewInstance(
2993 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
2995 aws_imdsv2_external_account_creds_httpcli_get_success,
2996 aws_external_account_creds_httpcli_post_success,
2997 aws_imdsv2_external_account_creds_httpcli_put_success);
2998 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
3004 TEST(CredentialsTest, TestAwsExternalAccountCredsSuccessPathRegionEnvKeysUrl) {
3009 valid_aws_external_account_creds_options_credential_source, &
error);
3011 ExternalAccountCredentials::Options
options = {
3014 "subject_token_type",
3016 "https://foo.com:5555/token",
3017 "https://foo.com:5555/token_info",
3028 auto state = RequestMetadataState::NewInstance(
3029 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
3031 aws_external_account_creds_httpcli_post_success,
3033 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
3040 TEST(CredentialsTest,
3041 TestAwsExternalAccountCredsSuccessPathDefaultRegionEnvKeysUrl) {
3043 gpr_setenv(
"AWS_DEFAULT_REGION",
"test_regionz");
3046 valid_aws_external_account_creds_options_credential_source, &
error);
3048 ExternalAccountCredentials::Options
options = {
3051 "subject_token_type",
3053 "https://foo.com:5555/token",
3054 "https://foo.com:5555/token_info",
3065 auto state = RequestMetadataState::NewInstance(
3066 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
3068 aws_external_account_creds_httpcli_post_success,
3070 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
3077 TEST(CredentialsTest,
3078 TestAwsExternalAccountCredsSuccessPathDuplicateRegionEnvKeysUrl) {
3082 gpr_setenv(
"AWS_DEFAULT_REGION",
"ERROR_REGION");
3085 valid_aws_external_account_creds_options_credential_source, &
error);
3087 ExternalAccountCredentials::Options
options = {
3090 "subject_token_type",
3092 "https://foo.com:5555/token",
3093 "https://foo.com:5555/token_info",
3104 auto state = RequestMetadataState::NewInstance(
3105 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
3107 aws_external_account_creds_httpcli_post_success,
3109 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
3117 TEST(CredentialsTest, TestAwsExternalAccountCredsSuccessPathRegionUrlKeysEnv) {
3119 gpr_setenv(
"AWS_ACCESS_KEY_ID",
"test_access_key_id");
3120 gpr_setenv(
"AWS_SECRET_ACCESS_KEY",
"test_secret_access_key");
3121 gpr_setenv(
"AWS_SESSION_TOKEN",
"test_token");
3124 valid_aws_external_account_creds_options_credential_source, &
error);
3126 ExternalAccountCredentials::Options
options = {
3129 "subject_token_type",
3131 "https://foo.com:5555/token",
3132 "https://foo.com:5555/token_info",
3143 auto state = RequestMetadataState::NewInstance(
3144 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
3146 aws_external_account_creds_httpcli_post_success,
3148 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
3157 TEST(CredentialsTest, TestAwsExternalAccountCredsSuccessPathRegionEnvKeysEnv) {
3160 gpr_setenv(
"AWS_ACCESS_KEY_ID",
"test_access_key_id");
3161 gpr_setenv(
"AWS_SECRET_ACCESS_KEY",
"test_secret_access_key");
3162 gpr_setenv(
"AWS_SESSION_TOKEN",
"test_token");
3165 valid_aws_external_account_creds_options_credential_source, &
error);
3167 ExternalAccountCredentials::Options
options = {
3170 "subject_token_type",
3172 "https://foo.com:5555/token",
3173 "https://foo.com:5555/token_info",
3184 auto state = RequestMetadataState::NewInstance(
3185 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
3187 aws_external_account_creds_httpcli_post_success,
3189 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
3199 TEST(CredentialsTest,
3200 TestAwsExternalAccountCredsSuccessPathDefaultRegionEnvKeysEnv) {
3201 std::map<std::string, std::string> emd = {
3202 {
"authorization",
"Bearer token_exchange_access_token"}};
3204 gpr_setenv(
"AWS_DEFAULT_REGION",
"test_regionz");
3205 gpr_setenv(
"AWS_ACCESS_KEY_ID",
"test_access_key_id");
3206 gpr_setenv(
"AWS_SECRET_ACCESS_KEY",
"test_secret_access_key");
3207 gpr_setenv(
"AWS_SESSION_TOKEN",
"test_token");
3210 valid_aws_external_account_creds_options_credential_source, &
error);
3212 ExternalAccountCredentials::Options
options = {
3215 "subject_token_type",
3217 "https://foo.com:5555/token",
3218 "https://foo.com:5555/token_info",
3229 auto state = RequestMetadataState::NewInstance(
3230 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
3232 aws_external_account_creds_httpcli_post_success,
3234 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
3244 TEST(CredentialsTest,
3245 TestAwsExternalAccountCredsSuccessPathDuplicateRegionEnvKeysEnv) {
3249 gpr_setenv(
"AWS_DEFAULT_REGION",
"ERROR_REGION");
3250 gpr_setenv(
"AWS_ACCESS_KEY_ID",
"test_access_key_id");
3251 gpr_setenv(
"AWS_SECRET_ACCESS_KEY",
"test_secret_access_key");
3252 gpr_setenv(
"AWS_SESSION_TOKEN",
"test_token");
3255 valid_aws_external_account_creds_options_credential_source, &
error);
3257 ExternalAccountCredentials::Options
options = {
3260 "subject_token_type",
3262 "https://foo.com:5555/token",
3263 "https://foo.com:5555/token_info",
3274 auto state = RequestMetadataState::NewInstance(
3275 GRPC_ERROR_NONE,
"authorization: Bearer token_exchange_access_token");
3277 aws_external_account_creds_httpcli_post_success,
3279 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
3290 TEST(CredentialsTest,
3291 TestAwsExternalAccountCredsFailureUnmatchedEnvironmentId) {
3294 invalid_aws_external_account_creds_options_credential_source_unmatched_environment_id,
3297 ExternalAccountCredentials::Options
options = {
3300 "subject_token_type",
3302 "https://foo.com:5555/token",
3303 "https://foo.com:5555/token_info",
3312 std::string expected_error =
"environment_id does not match.";
3320 TEST(CredentialsTest, TestAwsExternalAccountCredsFailureInvalidRegionUrl) {
3324 invalid_aws_external_account_creds_options_credential_source_invalid_region_url,
3327 ExternalAccountCredentials::Options
options = {
3330 "subject_token_type",
3332 "https://foo.com:5555/token",
3333 "https://foo.com:5555/token_info",
3345 "Invalid region url: invalid_region_url.");
3348 "Error occurred when fetching oauth2 token.", &
error, 1);
3349 auto state = RequestMetadataState::NewInstance(expected_error, {});
3351 aws_external_account_creds_httpcli_post_success,
3353 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
3360 TEST(CredentialsTest, TestAwsExternalAccountCredsFailureInvalidUrl) {
3364 invalid_aws_external_account_creds_options_credential_source_invalid_url,
3367 ExternalAccountCredentials::Options
options = {
3370 "subject_token_type",
3372 "https://foo.com:5555/token",
3373 "https://foo.com:5555/token_info",
3387 "Error occurred when fetching oauth2 token.", &
error, 1);
3388 auto state = RequestMetadataState::NewInstance(expected_error, {});
3390 aws_external_account_creds_httpcli_post_success,
3392 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
3399 TEST(CredentialsTest, TestAwsExternalAccountCredsFailureMissingRoleName) {
3403 invalid_aws_external_account_creds_options_credential_source_missing_role_name,
3406 ExternalAccountCredentials::Options
options = {
3409 "subject_token_type",
3411 "https://foo.com:5555/token",
3412 "https://foo.com:5555/token_info",
3424 "Missing role name when retrieving signing keys.");
3427 "Error occurred when fetching oauth2 token.", &
error, 1);
3428 auto state = RequestMetadataState::NewInstance(expected_error, {});
3430 aws_external_account_creds_httpcli_post_success,
3432 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
3439 TEST(CredentialsTest,
3440 TestAwsExternalAccountCredsFailureInvalidRegionalCredVerificationUrl) {
3444 invalid_aws_external_account_creds_options_credential_source_invalid_regional_cred_verification_url,
3447 ExternalAccountCredentials::Options
options = {
3450 "subject_token_type",
3452 "https://foo.com:5555/token",
3453 "https://foo.com:5555/token_info",
3465 "Creating aws request signer failed.");
3468 "Error occurred when fetching oauth2 token.", &
error, 1);
3469 auto state = RequestMetadataState::NewInstance(expected_error, {});
3471 aws_external_account_creds_httpcli_post_success,
3473 state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority,
3480 TEST(CredentialsTest, TestExternalAccountCredentialsCreateSuccess) {
3482 const char* url_options_string =
3483 "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
3484 "token_type\":\"subject_token_type\",\"service_account_impersonation_"
3485 "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
3486 "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
3487 "token_info\",\"credential_source\":{\"url\":\"https://foo.com:5555/"
3488 "generate_subject_token_format_json\",\"headers\":{\"Metadata-Flavor\":"
3489 "\"Google\"},\"format\":{\"type\":\"json\",\"subject_token_field_name\":"
3490 "\"access_token\"}},\"quota_project_id\":\"quota_"
3491 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
3493 const char* url_scopes_string =
"scope1,scope2";
3495 url_options_string, url_scopes_string);
3499 const char* file_options_string =
3500 "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
3501 "token_type\":\"subject_token_type\",\"service_account_impersonation_"
3502 "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
3503 "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
3504 "token_info\",\"credential_source\":{\"file\":\"credentials_file_path\"},"
3505 "\"quota_project_id\":\"quota_"
3506 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
3508 const char* file_scopes_string =
"scope1,scope2";
3510 file_options_string, file_scopes_string);
3512 file_creds->
Unref();
3514 const char* aws_options_string =
3515 "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
3516 "token_type\":\"subject_token_type\",\"service_account_impersonation_"
3517 "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
3518 "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
3519 "token_info\",\"credential_source\":{\"environment_id\":\"aws1\","
3520 "\"region_url\":\"https://foo.com:5555/region_url\",\"url\":\"https://"
3521 "foo.com:5555/url\",\"regional_cred_verification_url\":\"https://"
3522 "foo.com:5555/regional_cred_verification_url_{region}\"},"
3523 "\"quota_project_id\":\"quota_"
3524 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
3526 const char* aws_scopes_string =
"scope1,scope2";
3528 aws_options_string, aws_scopes_string);
3533 TEST(CredentialsTest,
3534 TestExternalAccountCredentialsCreateFailureInvalidJsonFormat) {
3535 const char* options_string =
"invalid_json";
3541 TEST(CredentialsTest,
3542 TestExternalAccountCredentialsCreateFailureInvalidOptionsFormat) {
3543 const char* options_string =
"{\"random_key\":\"random_value\"}";
3551 TestExternalAccountCredentialsCreateFailureInvalidOptionsCredentialSource) {
3552 const char* options_string =
3553 "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
3554 "token_type\":\"subject_token_type\",\"service_account_impersonation_"
3555 "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
3556 "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
3557 "token_info\",\"credential_source\":{\"random_key\":\"random_value\"},"
3558 "\"quota_project_id\":\"quota_"
3559 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
3566 TEST(CredentialsTest,
3567 TestExternalAccountCredentialsCreateSuccessWorkforcePool) {
3568 const char* url_options_string =
3569 "{\"type\":\"external_account\",\"audience\":\"//iam.googleapis.com/"
3570 "locations/location/workforcePools/pool/providers/provider\",\"subject_"
3571 "token_type\":\"subject_token_type\",\"service_account_impersonation_"
3572 "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
3573 "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
3574 "token_info\",\"credential_source\":{\"url\":\"https://foo.com:5555/"
3575 "generate_subject_token_format_json\",\"headers\":{\"Metadata-Flavor\":"
3576 "\"Google\"},\"format\":{\"type\":\"json\",\"subject_token_field_name\":"
3577 "\"access_token\"}},\"quota_project_id\":\"quota_"
3578 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
3579 "secret\",\"workforce_pool_user_project\":\"workforce_pool_user_"
3581 const char* url_scopes_string =
"scope1,scope2";
3583 url_options_string, url_scopes_string);
3588 TEST(CredentialsTest,
3589 TestExternalAccountCredentialsCreateFailureInvalidWorkforcePoolAudience) {
3590 const char* url_options_string =
3591 "{\"type\":\"external_account\",\"audience\":\"invalid_workforce_pool_"
3592 "audience\",\"subject_"
3593 "token_type\":\"subject_token_type\",\"service_account_impersonation_"
3594 "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
3595 "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
3596 "token_info\",\"credential_source\":{\"url\":\"https://foo.com:5555/"
3597 "generate_subject_token_format_json\",\"headers\":{\"Metadata-Flavor\":"
3598 "\"Google\"},\"format\":{\"type\":\"json\",\"subject_token_field_name\":"
3599 "\"access_token\"}},\"quota_project_id\":\"quota_"
3600 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
3601 "secret\",\"workforce_pool_user_project\":\"workforce_pool_user_"
3603 const char* url_scopes_string =
"scope1,scope2";
3605 url_options_string, url_scopes_string);
3609 TEST(CredentialsTest, TestInsecureCredentialsCompareSuccess) {
3612 ASSERT_EQ(insecure_creds_1->cmp(insecure_creds_2), 0);
3622 TEST(CredentialsTest, TestInsecureCredentialsCompareFailure) {
3625 ASSERT_NE(insecure_creds->cmp(fake_creds), 0);
3626 ASSERT_NE(fake_creds->cmp(insecure_creds), 0);
3636 TEST(CredentialsTest, TestInsecureCredentialsSingletonCreate) {
3639 EXPECT_EQ(insecure_creds_1, insecure_creds_2);
3642 TEST(CredentialsTest, TestFakeCallCredentialsCompareSuccess) {
3643 auto call_creds = MakeRefCounted<fake_call_creds>();
3647 TEST(CredentialsTest, TestFakeCallCredentialsCompareFailure) {
3648 auto fake_creds = MakeRefCounted<fake_call_creds>();
3651 GPR_ASSERT(md_creds->cmp(fake_creds.get()) != 0);
3655 TEST(CredentialsTest, TestHttpRequestSSLCredentialsCompare) {
3658 EXPECT_EQ(creds_1->cmp(creds_2.get()), 0);
3659 EXPECT_EQ(creds_2->cmp(creds_1.get()), 0);
3662 TEST(CredentialsTest, TestHttpRequestSSLCredentialsSingleton) {
3668 TEST(CredentialsTest, TestCompositeChannelCredsCompareSuccess) {
3670 auto fake_creds = MakeRefCounted<fake_call_creds>();
3672 insecure_creds, fake_creds.get(),
nullptr);
3674 insecure_creds, fake_creds.get(),
nullptr);
3675 EXPECT_EQ(composite_creds_1->cmp(composite_creds_2), 0);
3676 EXPECT_EQ(composite_creds_2->cmp(composite_creds_1), 0);
3682 TEST(CredentialsTest,
3683 TestCompositeChannelCredsCompareFailureDifferentChannelCreds) {
3686 auto fake_creds = MakeRefCounted<fake_call_creds>();
3688 insecure_creds, fake_creds.get(),
nullptr);
3690 fake_channel_creds, fake_creds.get(),
nullptr);
3691 EXPECT_NE(composite_creds_1->cmp(composite_creds_2), 0);
3692 EXPECT_NE(composite_creds_2->cmp(composite_creds_1), 0);
3699 TEST(CredentialsTest,
3700 TestCompositeChannelCredsCompareFailureDifferentCallCreds) {
3702 auto fake_creds = MakeRefCounted<fake_call_creds>();
3705 insecure_creds, fake_creds.get(),
nullptr);
3707 insecure_creds, md_creds,
nullptr);
3708 EXPECT_NE(composite_creds_1->cmp(composite_creds_2), 0);
3709 EXPECT_NE(composite_creds_2->cmp(composite_creds_1), 0);
3716 TEST(CredentialsTest, TestTlsCredentialsCompareSuccess) {
3721 EXPECT_EQ(tls_creds_1->cmp(tls_creds_2), 0);
3722 EXPECT_EQ(tls_creds_2->cmp(tls_creds_1), 0);
3727 TEST(CredentialsTest, TestTlsCredentialsWithVerifierCompareSuccess) {
3729 options_1->set_certificate_verifier(
3730 MakeRefCounted<HostNameCertificateVerifier>());
3733 options_2->set_certificate_verifier(
3734 MakeRefCounted<HostNameCertificateVerifier>());
3736 EXPECT_EQ(tls_creds_1->cmp(tls_creds_2), 0);
3737 EXPECT_EQ(tls_creds_2->cmp(tls_creds_1), 0);
3742 TEST(CredentialsTest, TestTlsCredentialsCompareFailure) {
3744 options_1->set_check_call_host(
true);
3747 options_2->set_check_call_host(
false);
3749 EXPECT_NE(tls_creds_1->cmp(tls_creds_2), 0);
3750 EXPECT_NE(tls_creds_2->cmp(tls_creds_1), 0);
3755 TEST(CredentialsTest, TestTlsCredentialsWithVerifierCompareFailure) {
3757 options_1->set_certificate_verifier(
3758 MakeRefCounted<HostNameCertificateVerifier>());
3763 options_2->set_certificate_verifier(
3764 MakeRefCounted<ExternalCertificateVerifier>(&
verifier));
3766 EXPECT_NE(tls_creds_1->cmp(tls_creds_2), 0);
3767 EXPECT_NE(tls_creds_2->cmp(tls_creds_1), 0);
3772 TEST(CredentialsTest, TestXdsCredentialsCompareSucces) {
3776 EXPECT_EQ(xds_creds_1->cmp(xds_creds_2), 0);
3777 EXPECT_EQ(xds_creds_2->cmp(xds_creds_1), 0);
3783 TEST(CredentialsTest, TestXdsCredentialsCompareFailure) {
3788 EXPECT_NE(xds_creds_1->cmp(xds_creds_2), 0);
3789 EXPECT_NE(xds_creds_2->cmp(xds_creds_1), 0);