| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/security/credentials/credentials.h" |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include <string> |
| 27 |
|
| 28 |
#include <gmock/gmock.h> |
| 29 |
#include <openssl/rsa.h> |
| 30 |
|
| 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" |
| 35 |
|
| 36 |
#include <grpc/grpc_security.h> |
| 37 |
#include <grpc/slice.h> |
| 38 |
#include <grpc/support/alloc.h> |
| 39 |
#include <grpc/support/log.h> |
| 40 |
#include <grpc/support/string_util.h> |
| 41 |
#include <grpc/support/time.h> |
| 42 |
|
| 43 |
#include "src/core/lib/channel/channel_args.h" |
| 44 |
#include "src/core/lib/gpr/string.h" |
| 45 |
#include "src/core/lib/gpr/tmpfile.h" |
| 46 |
#include "src/core/lib/gprpp/env.h" |
| 47 |
#include "src/core/lib/gprpp/host_port.h" |
| 48 |
#include "src/core/lib/gprpp/time.h" |
| 49 |
#include "src/core/lib/gprpp/unique_type_name.h" |
| 50 |
#include "src/core/lib/http/httpcli.h" |
| 51 |
#include "src/core/lib/http/httpcli_ssl_credentials.h" |
| 52 |
#include "src/core/lib/iomgr/error.h" |
| 53 |
#include "src/core/lib/promise/exec_ctx_wakeup_scheduler.h" |
| 54 |
#include "src/core/lib/promise/promise.h" |
| 55 |
#include "src/core/lib/promise/seq.h" |
| 56 |
#include "src/core/lib/security/context/security_context.h" |
| 57 |
#include "src/core/lib/security/credentials/composite/composite_credentials.h" |
| 58 |
#include "src/core/lib/security/credentials/external/aws_external_account_credentials.h" |
| 59 |
#include "src/core/lib/security/credentials/external/external_account_credentials.h" |
| 60 |
#include "src/core/lib/security/credentials/external/file_external_account_credentials.h" |
| 61 |
#include "src/core/lib/security/credentials/external/url_external_account_credentials.h" |
| 62 |
#include "src/core/lib/security/credentials/fake/fake_credentials.h" |
| 63 |
#include "src/core/lib/security/credentials/google_default/google_default_credentials.h" |
| 64 |
#include "src/core/lib/security/credentials/iam/iam_credentials.h" |
| 65 |
#include "src/core/lib/security/credentials/jwt/jwt_credentials.h" |
| 66 |
#include "src/core/lib/security/credentials/oauth2/oauth2_credentials.h" |
| 67 |
#include "src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h" |
| 68 |
#include "src/core/lib/security/credentials/xds/xds_credentials.h" |
| 69 |
#include "src/core/lib/security/transport/auth_filters.h" |
| 70 |
#include "src/core/lib/transport/error_utils.h" |
| 71 |
#include "src/core/lib/uri/uri_parser.h" |
| 72 |
#include "test/core/util/test_config.h" |
| 73 |
|
| 74 |
namespace grpc_core { |
| 75 |
|
| 76 |
using internal::grpc_flush_cached_google_default_credentials; |
| 77 |
using internal::set_gce_tenancy_checker_for_testing; |
| 78 |
|
| 79 |
namespace { |
| 80 |
|
| 81 |
auto* g_memory_allocator = new MemoryAllocator( |
| 82 |
ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test")); |
| 83 |
|
| 84 |
/* -- Constants. -- */ |
| 85 |
|
| 86 |
const char test_google_iam_authorization_token[] = "blahblahblhahb"; |
| 87 |
const char test_google_iam_authority_selector[] = "respectmyauthoritah"; |
| 88 |
const char test_oauth2_bearer_token[] = "Bearer blaaslkdjfaslkdfasdsfasf"; |
| 89 |
|
| 90 |
/* This JSON key was generated with the GCE console and revoked immediately. |
| 91 |
The identifiers have been changed as well. |
| 92 |
Maximum size for a string literal is 509 chars in C89, yay! */ |
| 93 |
const char test_json_key_str_part1[] = |
| 94 |
"{ \"private_key\": \"-----BEGIN PRIVATE KEY-----" |
| 95 |
"\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJE" |
| 96 |
"qg" |
| 97 |
"WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/" |
| 98 |
"rWBQvS4hle4LfijkP3J5BG+" |
| 99 |
"IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+" |
| 100 |
"uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/" |
| 101 |
"zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/" |
| 102 |
"8HpCqFYM9V8f34SBWfD4fRFT+n/" |
| 103 |
"73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+"; |
| 104 |
const char test_json_key_str_part2[] = |
| 105 |
"53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+" |
| 106 |
"dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/" |
| 107 |
"XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDA" |
| 108 |
"G" |
| 109 |
"W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6Hz" |
| 110 |
"A" |
| 111 |
"ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+" |
| 112 |
"6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/" |
| 113 |
"5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZ" |
| 114 |
"Y" |
| 115 |
"Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", "; |
| 116 |
const char test_json_key_str_part3[] = |
| 117 |
"\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " |
| 118 |
"\"client_email\": " |
| 119 |
"\"[email protected]." |
| 120 |
"com\", \"client_id\": " |
| 121 |
"\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." |
| 122 |
"com\", \"type\": \"service_account\" }"; |
| 123 |
|
| 124 |
/* Test refresh token. */ |
| 125 |
const char test_refresh_token_str[] = |
| 126 |
"{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," |
| 127 |
" \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," |
| 128 |
" \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"," |
| 129 |
" \"type\": \"authorized_user\"}"; |
| 130 |
|
| 131 |
/* Test external account credentials. */ |
| 132 |
const char test_external_account_credentials_str[] = |
| 133 |
"{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_" |
| 134 |
"token_type\":\"subject_token_type\",\"service_account_impersonation_" |
| 135 |
"url\":\"https://iamcredentials.googleapis.com:5555/" |
| 136 |
"service_account_impersonation_url\",\"token_url\":\"https://" |
| 137 |
"sts.googleapis.com:5555/" |
| 138 |
"token\",\"token_info_url\":\"https://sts.googleapis.com:5555/" |
| 139 |
"token_info\",\"credential_source\":{\"file\":" |
| 140 |
"\"credentials_file_path\"}," |
| 141 |
"\"quota_project_id\":\"quota_" |
| 142 |
"project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_" |
| 143 |
"secret\"}"; |
| 144 |
|
| 145 |
const char test_external_account_credentials_multi_pattern_sts_str[] = |
| 146 |
"{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_" |
| 147 |
"token_type\":\"subject_token_type\",\"service_account_impersonation_" |
| 148 |
"url\":\"https://sts.test.googleapis.com:5555/" |
| 149 |
"service_account_impersonation_url\",\"token_url\":\"https://" |
| 150 |
"test.sts.googleapis.com:5555/token\",\"token_info_url\":\"https://" |
| 151 |
"test-sts.googleapis.com:5555/" |
| 152 |
"token_info\",\"credential_source\":{\"file\":\"credentials_file_path\"}," |
| 153 |
"\"quota_project_id\":\"quota_" |
| 154 |
"project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_" |
| 155 |
"secret\"}"; |
| 156 |
|
| 157 |
const char test_external_account_credentials_multi_pattern_iam_str[] = |
| 158 |
"{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_" |
| 159 |
"token_type\":\"subject_token_type\",\"service_account_impersonation_" |
| 160 |
"url\":\"https://iamcredentials.test.googleapis.com:5555/" |
| 161 |
"service_account_impersonation_url\",\"token_url\":\"https://" |
| 162 |
"test.iamcredentials.googleapis.com:5555/" |
| 163 |
"token\",\"token_info_url\":\"https://" |
| 164 |
"test-iamcredentials.googleapis.com:5555/" |
| 165 |
"token_info\",\"credential_source\":{\"file\":\"credentials_file_path\"}," |
| 166 |
"\"quota_project_id\":\"quota_" |
| 167 |
"project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_" |
| 168 |
"secret\"}"; |
| 169 |
|
| 170 |
const char test_external_account_credentials_psc_sts_str[] = |
| 171 |
"{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_" |
| 172 |
"token_type\":\"subject_token_type\",\"service_account_impersonation_" |
| 173 |
"url\":\"https://sts-xyz.p.googleapis.com:5555/" |
| 174 |
"service_account_impersonation_url\",\"token_url\":\"https://" |
| 175 |
"sts-xyz-123.p.googleapis.com:5555/token\",\"token_info_url\":\"https://" |
| 176 |
"sts-xyz.p.googleapis.com:5555/introspect" |
| 177 |
"token_info\",\"credential_source\":{\"file\":\"credentials_file_path\"}," |
| 178 |
"\"quota_project_id\":\"quota_" |
| 179 |
"project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_" |
| 180 |
"secret\"}"; |
| 181 |
|
| 182 |
const char test_external_account_credentials_psc_iam_str[] = |
| 183 |
"{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_" |
| 184 |
"token_type\":\"subject_token_type\",\"service_account_impersonation_" |
| 185 |
"url\":\"https://iamcredentials-xyz.p.googleapis.com:5555/" |
| 186 |
"service_account_impersonation_url\",\"token_url\":\"https://" |
| 187 |
"iamcredentials-xyz-123.p.googleapis.com:5555/" |
| 188 |
"token\",\"token_info_url\":\"https://" |
| 189 |
"iamcredentials-xyz-123.p.googleapis.com:5555/introspect" |
| 190 |
"token_info\",\"credential_source\":{\"file\":\"credentials_file_path\"}," |
| 191 |
"\"quota_project_id\":\"quota_" |
| 192 |
"project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_" |
| 193 |
"secret\"}"; |
| 194 |
|
| 195 |
const char valid_oauth2_json_response[] = |
| 196 |
"{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\"," |
| 197 |
" \"expires_in\":3599, " |
| 198 |
" \"token_type\":\"Bearer\"}"; |
| 199 |
|
| 200 |
const char valid_sts_json_response[] = |
| 201 |
"{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\"," |
| 202 |
" \"expires_in\":3599, " |
| 203 |
" \"issued_token_type\":\"urn:ietf:params:oauth:token-type:access_token\", " |
| 204 |
" \"token_type\":\"Bearer\"}"; |
| 205 |
|
| 206 |
const char test_scope[] = "perm1 perm2"; |
| 207 |
|
| 208 |
const char test_signed_jwt[] = |
| 209 |
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImY0OTRkN2M1YWU2MGRmOTcyNmM4YW" |
| 210 |
"U0MDcyZTViYTdmZDkwODg2YzcifQ"; |
| 211 |
const char test_signed_jwt_token_type[] = |
| 212 |
"urn:ietf:params:oauth:token-type:id_token"; |
| 213 |
const char test_signed_jwt2[] = |
| 214 |
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImY0OTRkN2M1YWU2MGRmOTcyNmM5YW" |
| 215 |
"U2MDcyZTViYTdnZDkwODg5YzcifQ"; |
| 216 |
const char test_signed_jwt_token_type2[] = |
| 217 |
"urn:ietf:params:oauth:token-type:jwt"; |
| 218 |
const char test_signed_jwt_path_prefix[] = "test_sign_jwt"; |
| 219 |
|
| 220 |
const char test_service_url[] = "https://foo.com/foo.v1"; |
| 221 |
const char test_service_url_no_service_name[] = "https://foo.com/"; |
| 222 |
const char other_test_service_url_no_service_name[] = "https://bar.com/"; |
| 223 |
const char test_method[] = "ThisIsNotAMethod"; |
| 224 |
|
| 225 |
const char kTestUrlScheme[] = "https"; |
| 226 |
const char kTestAuthority[] = "foo.com"; |
| 227 |
const char kTestPath[] = "/foo.v1/ThisIsNotAMethod"; |
| 228 |
const char kTestOtherAuthority[] = "bar.com"; |
| 229 |
const char kTestOtherPath[] = "/bar.v1/ThisIsNotAMethod"; |
| 230 |
|
| 231 |
const char test_sts_endpoint_url[] = "https://foo.com:5555/v1/token-exchange"; |
| 232 |
|
| 233 |
const char valid_external_account_creds_token_exchange_response[] = |
| 234 |
"{\"access_token\":\"token_exchange_access_token\"," |
| 235 |
" \"expires_in\":3599," |
| 236 |
" \"token_type\":\"Bearer\"}"; |
| 237 |
|
| 238 |
const char |
| 239 |
valid_external_account_creds_service_account_impersonation_response[] = |
| 240 |
"{\"accessToken\":\"service_account_impersonation_access_token\"," |
| 241 |
" \"expireTime\":\"2050-01-01T00:00:00Z\"}"; |
| 242 |
|
| 243 |
const char |
| 244 |
valid_url_external_account_creds_options_credential_source_format_text[] = |
| 245 |
"{\"url\":\"https://foo.com:5555/generate_subject_token_format_text\"," |
| 246 |
"\"headers\":{\"Metadata-Flavor\":\"Google\"}}"; |
| 247 |
|
| 248 |
const char |
| 249 |
valid_url_external_account_creds_options_credential_source_with_qurey_params_format_text |
| 250 |
[] = "{\"url\":\"https://foo.com:5555/" |
| 251 |
"path/to/url/creds?p1=v1&p2=v2\"," |
| 252 |
"\"headers\":{\"Metadata-Flavor\":\"Google\"}}"; |
| 253 |
|
| 254 |
const char |
| 255 |
valid_url_external_account_creds_retrieve_subject_token_response_format_text |
| 256 |
[] = "test_subject_token"; |
| 257 |
|
| 258 |
const char |
| 259 |
valid_url_external_account_creds_options_credential_source_format_json[] = |
| 260 |
"{\"url\":\"https://foo.com:5555/generate_subject_token_format_json\"," |
| 261 |
"\"headers\":{\"Metadata-Flavor\":\"Google\"}," |
| 262 |
"\"format\":{\"type\":\"json\",\"subject_token_field_name\":\"access_" |
| 263 |
"token\"}}"; |
| 264 |
|
| 265 |
const char |
| 266 |
valid_url_external_account_creds_retrieve_subject_token_response_format_json |
| 267 |
[] = "{\"access_token\":\"test_subject_token\"}"; |
| 268 |
|
| 269 |
const char invalid_url_external_account_creds_options_credential_source[] = |
| 270 |
"{\"url\":\"invalid_credential_source_url\"," |
| 271 |
"\"headers\":{\"Metadata-Flavor\":\"Google\"}}"; |
| 272 |
|
| 273 |
const char valid_aws_external_account_creds_retrieve_signing_keys_response[] = |
| 274 |
"{\"AccessKeyId\":\"test_access_key_id\",\"SecretAccessKey\":" |
| 275 |
"\"test_secret_access_key\",\"Token\":\"test_token\"}"; |
| 276 |
|
| 277 |
const char aws_imdsv2_session_token[] = "imdsv2_session_token"; |
| 278 |
|
| 279 |
const char valid_aws_external_account_creds_options_credential_source[] = |
| 280 |
"{\"environment_id\":\"aws1\"," |
| 281 |
"\"region_url\":\"https://169.254.169.254:5555/region_url\"," |
| 282 |
"\"url\":\"https://169.254.169.254:5555/url\"," |
| 283 |
"\"regional_cred_verification_url\":\"https://foo.com:5555/" |
| 284 |
"regional_cred_verification_url_{region}\"}"; |
| 285 |
|
| 286 |
const char valid_aws_imdsv2_external_account_creds_options_credential_source[] = |
| 287 |
"{\"environment_id\":\"aws1\"," |
| 288 |
"\"region_url\":\"http://169.254.169.254:5555/region_url\"," |
| 289 |
"\"url\":\"https://169.254.169.254:5555/url\"," |
| 290 |
"\"imdsv2_session_token_url\":\"https://169.254.169.254/" |
| 291 |
"imdsv2_session_token_url\"," |
| 292 |
"\"regional_cred_verification_url\":\"https://foo.com:5555/" |
| 293 |
"regional_cred_verification_url_{region}\"}"; |
| 294 |
|
| 295 |
const char valid_aws_external_account_creds_options_credential_source_ipv6[] = |
| 296 |
"{\"environment_id\":\"aws1\"," |
| 297 |
"\"region_url\":\"https://[fd00:ec2::254]:5555/region_url\"," |
| 298 |
"\"url\":\"http://[fd00:ec2::254]:5555/url\"," |
| 299 |
"\"imdsv2_session_token_url\":\"https://[fd00:ec2::254]/" |
| 300 |
"imdsv2_session_token_url\"," |
| 301 |
"\"regional_cred_verification_url\":\"https://foo.com:5555/" |
| 302 |
"regional_cred_verification_url_{region}\"}"; |
| 303 |
|
| 304 |
const char |
| 305 |
invalid_aws_external_account_creds_options_credential_source_unmatched_environment_id |
| 306 |
[] = "{\"environment_id\":\"unsupported_aws_version\"," |
| 307 |
"\"region_url\":\"https://169.254.169.254:5555/region_url\"," |
| 308 |
"\"url\":\"https://169.254.169.254:5555/url\"," |
| 309 |
"\"regional_cred_verification_url\":\"https://foo.com:5555/" |
| 310 |
"regional_cred_verification_url_{region}\"}"; |
| 311 |
|
| 312 |
const char |
| 313 |
invalid_aws_external_account_creds_options_credential_source_invalid_region_url_host |
| 314 |
[] = "{\"environment_id\":\"aws1\"," |
| 315 |
"\"region_url\":\"https://fakeurl.com/url\"," |
| 316 |
"\"url\":\"https://169.254.169.254:5555/url\"," |
| 317 |
"\"regional_cred_verification_url\":\"https://foo.com:5555/" |
| 318 |
"regional_cred_verification_url_{region}\"}"; |
| 319 |
|
| 320 |
const char |
| 321 |
invalid_aws_external_account_creds_options_credential_source_invalid_url_host |
| 322 |
[] = "{\"environment_id\":\"aws1\"," |
| 323 |
"\"region_url\":\"https://169.254.169.254:5555/region_url\"," |
| 324 |
"\"url\":\"https://fake.com/url\"," |
| 325 |
"\"regional_cred_verification_url\":\"https://foo.com:5555/" |
| 326 |
"regional_cred_verification_url_{region}\"}"; |
| 327 |
|
| 328 |
const char |
| 329 |
invalid_aws_external_account_creds_options_credential_source_missing_role_name |
| 330 |
[] = "{\"environment_id\":\"aws1\"," |
| 331 |
"\"region_url\":\"https://169.254.169.254:5555/region_url\"," |
| 332 |
"\"url\":\"https://169.254.169.254:5555/url_no_role_name\"," |
| 333 |
"\"regional_cred_verification_url\":\"https://foo.com:5555/" |
| 334 |
"regional_cred_verification_url_{region}\"}"; |
| 335 |
|
| 336 |
const char |
| 337 |
invalid_aws_external_account_creds_options_credential_source_invalid_regional_cred_verification_url |
| 338 |
[] = "{\"environment_id\":\"aws1\"," |
| 339 |
"\"region_url\":\"https://169.254.169.254:5555/region_url\"," |
| 340 |
"\"url\":\"https://169.254.169.254:5555/url_no_role_name\"," |
| 341 |
"\"regional_cred_verification_url\":\"invalid_regional_cred_" |
| 342 |
"verification_url\"}"; |
| 343 |
|
| 344 |
const char |
| 345 |
invalid_aws_external_account_creds_options_credential_source_invalid_imdsv2_url_host |
| 346 |
[] = "{\"environment_id\":\"aws1\"," |
| 347 |
"\"region_url\":\"https://169.254.169.254:5555/region_url\"," |
| 348 |
"\"url\":\"https://169.254.169.254:5555/url\"," |
| 349 |
"\"imdsv2_session_token_url\":\"https://foo.com/" |
| 350 |
"imdsv2_session_token_url\"," |
| 351 |
"\"regional_cred_verification_url\":\"https://foo.com:5555/" |
| 352 |
"regional_cred_verification_url_{region}\"}"; |
| 353 |
|
| 354 |
/* -- Global state flags. -- */ |
| 355 |
|
| 356 |
bool g_test_is_on_gce = false; |
| 357 |
|
| 358 |
bool g_test_gce_tenancy_checker_called = false; |
| 359 |
|
| 360 |
/* -- Utils. -- */ |
| 361 |
|
| 362 |
char* test_json_key_str(void) { |
| 363 |
size_t result_len = strlen(test_json_key_str_part1) + |
| 364 |
strlen(test_json_key_str_part2) + |
| 365 |
strlen(test_json_key_str_part3); |
| 366 |
char* result = static_cast<char*>(gpr_malloc(result_len + 1)); |
| 367 |
char* current = result; |
| 368 |
strcpy(result, test_json_key_str_part1); |
| 369 |
current += strlen(test_json_key_str_part1); |
| 370 |
strcpy(current, test_json_key_str_part2); |
| 371 |
current += strlen(test_json_key_str_part2); |
| 372 |
strcpy(current, test_json_key_str_part3); |
| 373 |
return result; |
| 374 |
} |
| 375 |
|
| 376 |
grpc_http_response http_response(int status, const char* body) { |
| 377 |
grpc_http_response response; |
| 378 |
response = {}; |
| 379 |
response.status = status; |
| 380 |
response.body = gpr_strdup(const_cast<char*>(body)); |
| 381 |
response.body_length = strlen(body); |
| 382 |
return response; |
| 383 |
} |
| 384 |
|
| 385 |
/* -- Tests. -- */ |
| 386 |
|
| 387 |
TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingOk) { |
| 388 |
ExecCtx exec_ctx; |
| 389 |
absl::optional<Slice> token_value; |
| 390 |
Duration token_lifetime; |
| 391 |
grpc_http_response response = http_response(200, valid_oauth2_json_response); |
| 392 |
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( |
| 393 |
&response, &token_value, &token_lifetime) == |
| 394 |
GRPC_CREDENTIALS_OK); |
| 395 |
GPR_ASSERT(token_lifetime == Duration::Seconds(3599)); |
| 396 |
GPR_ASSERT(token_value->as_string_view() == |
| 397 |
"Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"); |
| 398 |
grpc_http_response_destroy(&response); |
| 399 |
} |
| 400 |
|
| 401 |
TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingBadHttpStatus) { |
| 402 |
ExecCtx exec_ctx; |
| 403 |
absl::optional<Slice> token_value; |
| 404 |
Duration token_lifetime; |
| 405 |
grpc_http_response response = http_response(401, valid_oauth2_json_response); |
| 406 |
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( |
| 407 |
&response, &token_value, &token_lifetime) == |
| 408 |
GRPC_CREDENTIALS_ERROR); |
| 409 |
grpc_http_response_destroy(&response); |
| 410 |
} |
| 411 |
|
| 412 |
TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingEmptyHttpBody) { |
| 413 |
ExecCtx exec_ctx; |
| 414 |
absl::optional<Slice> token_value; |
| 415 |
Duration token_lifetime; |
| 416 |
grpc_http_response response = http_response(200, ""); |
| 417 |
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( |
| 418 |
&response, &token_value, &token_lifetime) == |
| 419 |
GRPC_CREDENTIALS_ERROR); |
| 420 |
grpc_http_response_destroy(&response); |
| 421 |
} |
| 422 |
|
| 423 |
TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingInvalidJson) { |
| 424 |
ExecCtx exec_ctx; |
| 425 |
absl::optional<Slice> token_value; |
| 426 |
Duration token_lifetime; |
| 427 |
grpc_http_response response = |
| 428 |
http_response(200, |
| 429 |
"{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\"," |
| 430 |
" \"expires_in\":3599, " |
| 431 |
" \"token_type\":\"Bearer\""); |
| 432 |
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( |
| 433 |
&response, &token_value, &token_lifetime) == |
| 434 |
GRPC_CREDENTIALS_ERROR); |
| 435 |
grpc_http_response_destroy(&response); |
| 436 |
} |
| 437 |
|
| 438 |
TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingMissingToken) { |
| 439 |
ExecCtx exec_ctx; |
| 440 |
absl::optional<Slice> token_value; |
| 441 |
Duration token_lifetime; |
| 442 |
grpc_http_response response = http_response(200, |
| 443 |
"{" |
| 444 |
" \"expires_in\":3599, " |
| 445 |
" \"token_type\":\"Bearer\"}"); |
| 446 |
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( |
| 447 |
&response, &token_value, &token_lifetime) == |
| 448 |
GRPC_CREDENTIALS_ERROR); |
| 449 |
grpc_http_response_destroy(&response); |
| 450 |
} |
| 451 |
|
| 452 |
TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingMissingTokenType) { |
| 453 |
ExecCtx exec_ctx; |
| 454 |
absl::optional<Slice> token_value; |
| 455 |
Duration token_lifetime; |
| 456 |
grpc_http_response response = |
| 457 |
http_response(200, |
| 458 |
"{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\"," |
| 459 |
" \"expires_in\":3599, " |
| 460 |
"}"); |
| 461 |
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( |
| 462 |
&response, &token_value, &token_lifetime) == |
| 463 |
GRPC_CREDENTIALS_ERROR); |
| 464 |
grpc_http_response_destroy(&response); |
| 465 |
} |
| 466 |
|
| 467 |
TEST(CredentialsTest, TestOauth2TokenFetcherCredsParsingMissingTokenLifetime) { |
| 468 |
ExecCtx exec_ctx; |
| 469 |
absl::optional<Slice> token_value; |
| 470 |
Duration token_lifetime; |
| 471 |
grpc_http_response response = |
| 472 |
http_response(200, |
| 473 |
"{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\"," |
| 474 |
" \"token_type\":\"Bearer\"}"); |
| 475 |
GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response( |
| 476 |
&response, &token_value, &token_lifetime) == |
| 477 |
GRPC_CREDENTIALS_ERROR); |
| 478 |
grpc_http_response_destroy(&response); |
| 479 |
} |
| 480 |
|
| 481 |
class RequestMetadataState : public RefCounted<RequestMetadataState> { |
| 482 |
public: |
| 483 |
static RefCountedPtr<RequestMetadataState> NewInstance( |
| 484 |
grpc_error_handle expected_error, std::string expected) { |
| 485 |
return MakeRefCounted<RequestMetadataState>( |
| 486 |
expected_error, std::move(expected), |
| 487 |
grpc_polling_entity_create_from_pollset_set(grpc_pollset_set_create())); |
| 488 |
} |
| 489 |
|
| 490 |
RequestMetadataState(grpc_error_handle expected_error, std::string expected, |
| 491 |
grpc_polling_entity pollent) |
| 492 |
: expected_error_(expected_error), |
| 493 |
expected_(std::move(expected)), |
| 494 |
pollent_(pollent) {} |
| 495 |
|
| 496 |
~RequestMetadataState() override { |
| 497 |
grpc_pollset_set_destroy(grpc_polling_entity_pollset_set(&pollent_)); |
| 498 |
} |
| 499 |
|
| 500 |
void RunRequestMetadataTest(grpc_call_credentials* creds, |
| 501 |
const char* url_scheme, const char* authority, |
| 502 |
const char* path) { |
| 503 |
auto self = Ref(); |
| 504 |
get_request_metadata_args_.security_connector = |
| 505 |
MakeRefCounted<BogusSecurityConnector>(url_scheme); |
| 506 |
md_.Set(HttpAuthorityMetadata(), Slice::FromStaticString(authority)); |
| 507 |
md_.Set(HttpPathMetadata(), Slice::FromStaticString(path)); |
| 508 |
activity_ = MakeActivity( |
| 509 |
[this, creds] { |
| 510 |
return Seq( |
| 511 |
creds->GetRequestMetadata( |
| 512 |
ClientMetadataHandle(&md_, Arena::PooledDeleter(nullptr)), |
| 513 |
&get_request_metadata_args_), |
| 514 |
[this](absl::StatusOr<ClientMetadataHandle> metadata) { |
| 515 |
if (metadata.ok()) { |
| 516 |
GPR_ASSERT(metadata->get() == &md_); |
| 517 |
} |
| 518 |
return metadata.status(); |
| 519 |
}); |
| 520 |
}, |
| 521 |
ExecCtxWakeupScheduler(), |
| 522 |
[self](absl::Status status) mutable { |
| 523 |
self->CheckRequestMetadata( |
| 524 |
absl_status_to_grpc_error(std::move(status))); |
| 525 |
self.reset(); |
| 526 |
}, |
| 527 |
arena_.get(), &pollent_); |
| 528 |
} |
| 529 |
|
| 530 |
private: |
| 531 |
// No-op security connector, exists only to inject url_scheme. |
| 532 |
class BogusSecurityConnector : public grpc_channel_security_connector { |
| 533 |
public: |
| 534 |
explicit BogusSecurityConnector(absl::string_view url_scheme) |
| 535 |
: grpc_channel_security_connector(url_scheme, nullptr, nullptr) {} |
| 536 |
|
| 537 |
void check_peer(tsi_peer, grpc_endpoint*, const ChannelArgs&, |
| 538 |
RefCountedPtr<grpc_auth_context>*, grpc_closure*) override { |
| 539 |
GPR_ASSERT(false); |
| 540 |
} |
| 541 |
|
| 542 |
void cancel_check_peer(grpc_closure*, grpc_error_handle) override { |
| 543 |
GPR_ASSERT(false); |
| 544 |
} |
| 545 |
|
| 546 |
int cmp(const grpc_security_connector*) const override { |
| 547 |
GPR_UNREACHABLE_CODE(return 0); |
| 548 |
} |
| 549 |
|
| 550 |
ArenaPromise<absl::Status> CheckCallHost(absl::string_view, |
| 551 |
grpc_auth_context*) override { |
| 552 |
GPR_UNREACHABLE_CODE( |
| 553 |
return Immediate(absl::PermissionDeniedError("should never happen"))); |
| 554 |
} |
| 555 |
|
| 556 |
void add_handshakers(const ChannelArgs&, grpc_pollset_set*, |
| 557 |
HandshakeManager*) override { |
| 558 |
GPR_ASSERT(false); |
| 559 |
} |
| 560 |
}; |
| 561 |
|
| 562 |
void CheckRequestMetadata(grpc_error_handle error) { |
| 563 |
gpr_log(GPR_INFO, "expected_error: %s", |
| 564 |
StatusToString(expected_error_).c_str()); |
| 565 |
gpr_log(GPR_INFO, "actual_error: %s", StatusToString(error).c_str()); |
| 566 |
if (expected_error_.ok()) { |
| 567 |
GPR_ASSERT(error.ok()); |
| 568 |
} else { |
| 569 |
std::string expected_error; |
| 570 |
GPR_ASSERT(grpc_error_get_str( |
| 571 |
expected_error_, StatusStrProperty::kDescription, &expected_error)); |
| 572 |
std::string actual_error; |
| 573 |
GPR_ASSERT(grpc_error_get_str(error, StatusStrProperty::kDescription, |
| 574 |
&actual_error)); |
| 575 |
GPR_ASSERT(expected_error == actual_error); |
| 576 |
} |
| 577 |
md_.Remove(HttpAuthorityMetadata()); |
| 578 |
md_.Remove(HttpPathMetadata()); |
| 579 |
gpr_log(GPR_INFO, "expected metadata: %s", expected_.c_str()); |
| 580 |
gpr_log(GPR_INFO, "actual metadata: %s", md_.DebugString().c_str()); |
| 581 |
GPR_ASSERT(md_.DebugString() == expected_); |
| 582 |
} |
| 583 |
|
| 584 |
private: |
| 585 |
grpc_error_handle expected_error_; |
| 586 |
std::string expected_; |
| 587 |
ScopedArenaPtr arena_ = MakeScopedArena(1024, g_memory_allocator); |
| 588 |
grpc_metadata_batch md_{arena_.get()}; |
| 589 |
grpc_call_credentials::GetRequestMetadataArgs get_request_metadata_args_; |
| 590 |
grpc_polling_entity pollent_; |
| 591 |
ActivityPtr activity_; |
| 592 |
}; |
| 593 |
|
| 594 |
TEST(CredentialsTest, TestGoogleIamCreds) { |
| 595 |
ExecCtx exec_ctx; |
| 596 |
auto state = RequestMetadataState::NewInstance( |
| 597 |
absl::OkStatus(), |
| 598 |
absl::StrCat(GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, ": ", |
| 599 |
test_google_iam_authorization_token, ", ", |
| 600 |
GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, ": ", |
| 601 |
test_google_iam_authority_selector)); |
| 602 |
grpc_call_credentials* creds = grpc_google_iam_credentials_create( |
| 603 |
test_google_iam_authorization_token, test_google_iam_authority_selector, |
| 604 |
nullptr); |
| 605 |
/* Check security level. */ |
| 606 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 607 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 608 |
kTestPath); |
| 609 |
creds->Unref(); |
| 610 |
} |
| 611 |
|
| 612 |
TEST(CredentialsTest, TestAccessTokenCreds) { |
| 613 |
ExecCtx exec_ctx; |
| 614 |
auto state = RequestMetadataState::NewInstance(absl::OkStatus(), |
| 615 |
"authorization: Bearer blah"); |
| 616 |
grpc_call_credentials* creds = |
| 617 |
grpc_access_token_credentials_create("blah", nullptr); |
| 618 |
GPR_ASSERT(creds->type() == grpc_access_token_credentials::Type()); |
| 619 |
/* Check security level. */ |
| 620 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 621 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 622 |
kTestPath); |
| 623 |
creds->Unref(); |
| 624 |
} |
| 625 |
|
| 626 |
class check_channel_oauth2 final : public grpc_channel_credentials { |
| 627 |
public: |
| 628 |
RefCountedPtr<grpc_channel_security_connector> create_security_connector( |
| 629 |
RefCountedPtr<grpc_call_credentials> call_creds, const char* /*target*/, |
| 630 |
ChannelArgs* /*new_args*/) override { |
| 631 |
GPR_ASSERT(type() == Type()); |
| 632 |
GPR_ASSERT(call_creds != nullptr); |
| 633 |
GPR_ASSERT(call_creds->type() == grpc_access_token_credentials::Type()); |
| 634 |
return nullptr; |
| 635 |
} |
| 636 |
|
| 637 |
static UniqueTypeName Type() { |
| 638 |
static UniqueTypeName::Factory kFactory("check_channel_oauth2"); |
| 639 |
return kFactory.Create(); |
| 640 |
} |
| 641 |
|
| 642 |
UniqueTypeName type() const override { return Type(); } |
| 643 |
|
| 644 |
private: |
| 645 |
int cmp_impl(const grpc_channel_credentials* other) const override { |
| 646 |
// TODO(yashykt): Check if we can do something better here |
| 647 |
return QsortCompare(static_cast<const grpc_channel_credentials*>(this), |
| 648 |
other); |
| 649 |
} |
| 650 |
}; |
| 651 |
|
| 652 |
TEST(CredentialsTest, TestChannelOauth2CompositeCreds) { |
| 653 |
ExecCtx exec_ctx; |
| 654 |
ChannelArgs new_args; |
| 655 |
grpc_channel_credentials* channel_creds = new check_channel_oauth2(); |
| 656 |
grpc_call_credentials* oauth2_creds = |
| 657 |
grpc_access_token_credentials_create("blah", nullptr); |
| 658 |
grpc_channel_credentials* channel_oauth2_creds = |
| 659 |
grpc_composite_channel_credentials_create(channel_creds, oauth2_creds, |
| 660 |
nullptr); |
| 661 |
grpc_channel_credentials_release(channel_creds); |
| 662 |
grpc_call_credentials_release(oauth2_creds); |
| 663 |
channel_oauth2_creds->create_security_connector(nullptr, nullptr, &new_args); |
| 664 |
grpc_channel_credentials_release(channel_oauth2_creds); |
| 665 |
} |
| 666 |
|
| 667 |
TEST(CredentialsTest, TestOauth2GoogleIamCompositeCreds) { |
| 668 |
ExecCtx exec_ctx; |
| 669 |
auto state = RequestMetadataState::NewInstance( |
| 670 |
absl::OkStatus(), |
| 671 |
absl::StrCat(GRPC_AUTHORIZATION_METADATA_KEY, ": ", |
| 672 |
test_oauth2_bearer_token, ", ", |
| 673 |
GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, ": ", |
| 674 |
test_google_iam_authorization_token, ", ", |
| 675 |
GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, ": ", |
| 676 |
test_google_iam_authority_selector)); |
| 677 |
grpc_call_credentials* oauth2_creds = grpc_md_only_test_credentials_create( |
| 678 |
"authorization", test_oauth2_bearer_token); |
| 679 |
|
| 680 |
/* Check security level of fake credentials. */ |
| 681 |
GPR_ASSERT(oauth2_creds->min_security_level() == GRPC_SECURITY_NONE); |
| 682 |
|
| 683 |
grpc_call_credentials* google_iam_creds = grpc_google_iam_credentials_create( |
| 684 |
test_google_iam_authorization_token, test_google_iam_authority_selector, |
| 685 |
nullptr); |
| 686 |
grpc_call_credentials* composite_creds = |
| 687 |
grpc_composite_call_credentials_create(oauth2_creds, google_iam_creds, |
| 688 |
nullptr); |
| 689 |
/* Check security level of composite credentials. */ |
| 690 |
GPR_ASSERT(composite_creds->min_security_level() == |
| 691 |
GRPC_PRIVACY_AND_INTEGRITY); |
| 692 |
|
| 693 |
oauth2_creds->Unref(); |
| 694 |
google_iam_creds->Unref(); |
| 695 |
GPR_ASSERT(composite_creds->type() == |
| 696 |
grpc_composite_call_credentials::Type()); |
| 697 |
const grpc_composite_call_credentials::CallCredentialsList& creds_list = |
| 698 |
static_cast<const grpc_composite_call_credentials*>(composite_creds) |
| 699 |
->inner(); |
| 700 |
GPR_ASSERT(creds_list.size() == 2); |
| 701 |
GPR_ASSERT(creds_list[0]->type() == grpc_md_only_test_credentials::Type()); |
| 702 |
GPR_ASSERT(creds_list[1]->type() == grpc_google_iam_credentials::Type()); |
| 703 |
state->RunRequestMetadataTest(composite_creds, kTestUrlScheme, kTestAuthority, |
| 704 |
kTestPath); |
| 705 |
composite_creds->Unref(); |
| 706 |
} |
| 707 |
|
| 708 |
class check_channel_oauth2_google_iam final : public grpc_channel_credentials { |
| 709 |
public: |
| 710 |
RefCountedPtr<grpc_channel_security_connector> create_security_connector( |
| 711 |
RefCountedPtr<grpc_call_credentials> call_creds, const char* /*target*/, |
| 712 |
ChannelArgs* /*new_args*/) override { |
| 713 |
GPR_ASSERT(type() == Type()); |
| 714 |
GPR_ASSERT(call_creds != nullptr); |
| 715 |
GPR_ASSERT(call_creds->type() == grpc_composite_call_credentials::Type()); |
| 716 |
const grpc_composite_call_credentials::CallCredentialsList& creds_list = |
| 717 |
static_cast<const grpc_composite_call_credentials*>(call_creds.get()) |
| 718 |
->inner(); |
| 719 |
GPR_ASSERT(creds_list[0]->type() == grpc_access_token_credentials::Type()); |
| 720 |
GPR_ASSERT(creds_list[1]->type() == grpc_google_iam_credentials::Type()); |
| 721 |
return nullptr; |
| 722 |
} |
| 723 |
|
| 724 |
static UniqueTypeName Type() { |
| 725 |
static UniqueTypeName::Factory kFactory("check_channel_oauth2_google_iam"); |
| 726 |
return kFactory.Create(); |
| 727 |
} |
| 728 |
|
| 729 |
UniqueTypeName type() const override { return Type(); } |
| 730 |
|
| 731 |
private: |
| 732 |
int cmp_impl(const grpc_channel_credentials* other) const override { |
| 733 |
// TODO(yashykt): Check if we can do something better here |
| 734 |
return QsortCompare(static_cast<const grpc_channel_credentials*>(this), |
| 735 |
other); |
| 736 |
} |
| 737 |
}; |
| 738 |
|
| 739 |
TEST(CredentialsTest, TestChannelOauth2GoogleIamCompositeCreds) { |
| 740 |
ExecCtx exec_ctx; |
| 741 |
ChannelArgs new_args; |
| 742 |
grpc_channel_credentials* channel_creds = |
| 743 |
new check_channel_oauth2_google_iam(); |
| 744 |
grpc_call_credentials* oauth2_creds = |
| 745 |
grpc_access_token_credentials_create("blah", nullptr); |
| 746 |
grpc_channel_credentials* channel_oauth2_creds = |
| 747 |
grpc_composite_channel_credentials_create(channel_creds, oauth2_creds, |
| 748 |
nullptr); |
| 749 |
grpc_call_credentials* google_iam_creds = grpc_google_iam_credentials_create( |
| 750 |
test_google_iam_authorization_token, test_google_iam_authority_selector, |
| 751 |
nullptr); |
| 752 |
|
| 753 |
grpc_channel_credentials* channel_oauth2_iam_creds = |
| 754 |
grpc_composite_channel_credentials_create(channel_oauth2_creds, |
| 755 |
google_iam_creds, nullptr); |
| 756 |
grpc_channel_credentials_release(channel_creds); |
| 757 |
grpc_call_credentials_release(oauth2_creds); |
| 758 |
grpc_channel_credentials_release(channel_oauth2_creds); |
| 759 |
grpc_call_credentials_release(google_iam_creds); |
| 760 |
|
| 761 |
channel_oauth2_iam_creds->create_security_connector(nullptr, nullptr, |
| 762 |
&new_args); |
| 763 |
|
| 764 |
grpc_channel_credentials_release(channel_oauth2_iam_creds); |
| 765 |
} |
| 766 |
|
| 767 |
void validate_compute_engine_http_request(const grpc_http_request* request, |
| 768 |
const char* host, const char* path) { |
| 769 |
GPR_ASSERT(strcmp(host, "metadata.google.internal.") == 0); |
| 770 |
GPR_ASSERT( |
| 771 |
strcmp(path, |
| 772 |
"/computeMetadata/v1/instance/service-accounts/default/token") == |
| 773 |
0); |
| 774 |
GPR_ASSERT(request->hdr_count == 1); |
| 775 |
GPR_ASSERT(strcmp(request->hdrs[0].key, "Metadata-Flavor") == 0); |
| 776 |
GPR_ASSERT(strcmp(request->hdrs[0].value, "Google") == 0); |
| 777 |
} |
| 778 |
|
| 779 |
int compute_engine_httpcli_get_success_override( |
| 780 |
const grpc_http_request* request, const char* host, const char* path, |
| 781 |
Timestamp /*deadline*/, grpc_closure* on_done, |
| 782 |
grpc_http_response* response) { |
| 783 |
validate_compute_engine_http_request(request, host, path); |
| 784 |
*response = http_response(200, valid_oauth2_json_response); |
| 785 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 786 |
return 1; |
| 787 |
} |
| 788 |
|
| 789 |
int compute_engine_httpcli_get_failure_override( |
| 790 |
const grpc_http_request* request, const char* host, const char* path, |
| 791 |
Timestamp /*deadline*/, grpc_closure* on_done, |
| 792 |
grpc_http_response* response) { |
| 793 |
validate_compute_engine_http_request(request, host, path); |
| 794 |
*response = http_response(403, "Not Authorized."); |
| 795 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 796 |
return 1; |
| 797 |
} |
| 798 |
|
| 799 |
int httpcli_post_should_not_be_called( |
| 800 |
const grpc_http_request* /*request*/, const char* /*host*/, |
| 801 |
const char* /*path*/, const char* /*body_bytes*/, size_t /*body_size*/, |
| 802 |
Timestamp /*deadline*/, grpc_closure* /*on_done*/, |
| 803 |
grpc_http_response* /*response*/) { |
| 804 |
GPR_ASSERT("HTTP POST should not be called" == nullptr); |
| 805 |
return 1; |
| 806 |
} |
| 807 |
|
| 808 |
int httpcli_get_should_not_be_called(const grpc_http_request* /*request*/, |
| 809 |
const char* /*host*/, const char* /*path*/, |
| 810 |
Timestamp /*deadline*/, |
| 811 |
grpc_closure* /*on_done*/, |
| 812 |
grpc_http_response* /*response*/) { |
| 813 |
GPR_ASSERT("HTTP GET should not be called" == nullptr); |
| 814 |
return 1; |
| 815 |
} |
| 816 |
|
| 817 |
int httpcli_put_should_not_be_called(const grpc_http_request* /*request*/, |
| 818 |
const char* /*host*/, const char* /*path*/, |
| 819 |
const char* /*body_bytes*/, |
| 820 |
size_t /*body_size*/, |
| 821 |
Timestamp /*deadline*/, |
| 822 |
grpc_closure* /*on_done*/, |
| 823 |
grpc_http_response* /*response*/) { |
| 824 |
GPR_ASSERT("HTTP PUT should not be called" == nullptr); |
| 825 |
return 1; |
| 826 |
} |
| 827 |
|
| 828 |
TEST(CredentialsTest, TestComputeEngineCredsSuccess) { |
| 829 |
ExecCtx exec_ctx; |
| 830 |
std::string emd = "authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"; |
| 831 |
const char expected_creds_debug_string[] = |
| 832 |
"GoogleComputeEngineTokenFetcherCredentials{" |
| 833 |
"OAuth2TokenFetcherCredentials}"; |
| 834 |
grpc_call_credentials* creds = |
| 835 |
grpc_google_compute_engine_credentials_create(nullptr); |
| 836 |
/* Check security level. */ |
| 837 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 838 |
|
| 839 |
/* First request: http get should be called. */ |
| 840 |
auto state = RequestMetadataState::NewInstance(absl::OkStatus(), emd); |
| 841 |
HttpRequest::SetOverride(compute_engine_httpcli_get_success_override, |
| 842 |
httpcli_post_should_not_be_called, |
| 843 |
httpcli_put_should_not_be_called); |
| 844 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 845 |
kTestPath); |
| 846 |
ExecCtx::Get()->Flush(); |
| 847 |
|
| 848 |
/* Second request: the cached token should be served directly. */ |
| 849 |
state = RequestMetadataState::NewInstance(absl::OkStatus(), emd); |
| 850 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 851 |
httpcli_post_should_not_be_called, |
| 852 |
httpcli_put_should_not_be_called); |
| 853 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 854 |
kTestPath); |
| 855 |
ExecCtx::Get()->Flush(); |
| 856 |
|
| 857 |
GPR_ASSERT( |
| 858 |
strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0); |
| 859 |
creds->Unref(); |
| 860 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 861 |
} |
| 862 |
|
| 863 |
TEST(CredentialsTest, TestComputeEngineCredsFailure) { |
| 864 |
ExecCtx exec_ctx; |
| 865 |
const char expected_creds_debug_string[] = |
| 866 |
"GoogleComputeEngineTokenFetcherCredentials{" |
| 867 |
"OAuth2TokenFetcherCredentials}"; |
| 868 |
auto state = RequestMetadataState::NewInstance( |
| 869 |
GRPC_ERROR_CREATE("Error occurred when fetching oauth2 token."), {}); |
| 870 |
grpc_call_credentials* creds = |
| 871 |
grpc_google_compute_engine_credentials_create(nullptr); |
| 872 |
HttpRequest::SetOverride(compute_engine_httpcli_get_failure_override, |
| 873 |
httpcli_post_should_not_be_called, |
| 874 |
httpcli_put_should_not_be_called); |
| 875 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 876 |
kTestPath); |
| 877 |
GPR_ASSERT( |
| 878 |
strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0); |
| 879 |
creds->Unref(); |
| 880 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 881 |
} |
| 882 |
|
| 883 |
void validate_refresh_token_http_request(const grpc_http_request* request, |
| 884 |
const char* host, const char* path, |
| 885 |
const char* body, size_t body_size) { |
| 886 |
/* The content of the assertion is tested extensively in json_token_test. */ |
| 887 |
GPR_ASSERT(body != nullptr); |
| 888 |
GPR_ASSERT(body_size != 0); |
| 889 |
std::string expected_body = absl::StrFormat( |
| 890 |
GRPC_REFRESH_TOKEN_POST_BODY_FORMAT_STRING, |
| 891 |
"32555999999.apps.googleusercontent.com", "EmssLNjJy1332hD4KFsecret", |
| 892 |
"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42"); |
| 893 |
GPR_ASSERT(expected_body.size() == body_size); |
| 894 |
GPR_ASSERT(memcmp(expected_body.data(), body, body_size) == 0); |
| 895 |
GPR_ASSERT(strcmp(host, GRPC_GOOGLE_OAUTH2_SERVICE_HOST) == 0); |
| 896 |
GPR_ASSERT(strcmp(path, GRPC_GOOGLE_OAUTH2_SERVICE_TOKEN_PATH) == 0); |
| 897 |
GPR_ASSERT(request->hdr_count == 1); |
| 898 |
GPR_ASSERT(strcmp(request->hdrs[0].key, "Content-Type") == 0); |
| 899 |
GPR_ASSERT( |
| 900 |
strcmp(request->hdrs[0].value, "application/x-www-form-urlencoded") == 0); |
| 901 |
} |
| 902 |
|
| 903 |
int refresh_token_httpcli_post_success(const grpc_http_request* request, |
| 904 |
const char* host, const char* path, |
| 905 |
const char* body, size_t body_size, |
| 906 |
Timestamp /*deadline*/, |
| 907 |
grpc_closure* on_done, |
| 908 |
grpc_http_response* response) { |
| 909 |
validate_refresh_token_http_request(request, host, path, body, body_size); |
| 910 |
*response = http_response(200, valid_oauth2_json_response); |
| 911 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 912 |
return 1; |
| 913 |
} |
| 914 |
|
| 915 |
int token_httpcli_post_failure(const grpc_http_request* /*request*/, |
| 916 |
const char* /*host*/, const char* /*path*/, |
| 917 |
const char* /*body*/, size_t /*body_size*/, |
| 918 |
Timestamp /*deadline*/, grpc_closure* on_done, |
| 919 |
grpc_http_response* response) { |
| 920 |
*response = http_response(403, "Not Authorized."); |
| 921 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 922 |
return 1; |
| 923 |
} |
| 924 |
|
| 925 |
TEST(CredentialsTest, TestRefreshTokenCredsSuccess) { |
| 926 |
ExecCtx exec_ctx; |
| 927 |
std::string emd = "authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"; |
| 928 |
const char expected_creds_debug_string[] = |
| 929 |
"GoogleRefreshToken{ClientID:32555999999.apps.googleusercontent.com," |
| 930 |
"OAuth2TokenFetcherCredentials}"; |
| 931 |
grpc_call_credentials* creds = grpc_google_refresh_token_credentials_create( |
| 932 |
test_refresh_token_str, nullptr); |
| 933 |
|
| 934 |
/* Check security level. */ |
| 935 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 936 |
|
| 937 |
/* First request: http put should be called. */ |
| 938 |
auto state = RequestMetadataState::NewInstance(absl::OkStatus(), emd); |
| 939 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 940 |
refresh_token_httpcli_post_success, |
| 941 |
httpcli_put_should_not_be_called); |
| 942 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 943 |
kTestPath); |
| 944 |
ExecCtx::Get()->Flush(); |
| 945 |
|
| 946 |
/* Second request: the cached token should be served directly. */ |
| 947 |
state = RequestMetadataState::NewInstance(absl::OkStatus(), emd); |
| 948 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 949 |
httpcli_post_should_not_be_called, |
| 950 |
httpcli_put_should_not_be_called); |
| 951 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 952 |
kTestPath); |
| 953 |
ExecCtx::Get()->Flush(); |
| 954 |
GPR_ASSERT( |
| 955 |
strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0); |
| 956 |
|
| 957 |
creds->Unref(); |
| 958 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 959 |
} |
| 960 |
|
| 961 |
TEST(CredentialsTest, TestRefreshTokenCredsFailure) { |
| 962 |
ExecCtx exec_ctx; |
| 963 |
const char expected_creds_debug_string[] = |
| 964 |
"GoogleRefreshToken{ClientID:32555999999.apps.googleusercontent.com," |
| 965 |
"OAuth2TokenFetcherCredentials}"; |
| 966 |
auto state = RequestMetadataState::NewInstance( |
| 967 |
GRPC_ERROR_CREATE("Error occurred when fetching oauth2 token."), {}); |
| 968 |
grpc_call_credentials* creds = grpc_google_refresh_token_credentials_create( |
| 969 |
test_refresh_token_str, nullptr); |
| 970 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 971 |
token_httpcli_post_failure, |
| 972 |
httpcli_put_should_not_be_called); |
| 973 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 974 |
kTestPath); |
| 975 |
GPR_ASSERT( |
| 976 |
strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0); |
| 977 |
|
| 978 |
creds->Unref(); |
| 979 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 980 |
} |
| 981 |
|
| 982 |
TEST(CredentialsTest, TestValidStsCredsOptions) { |
| 983 |
grpc_sts_credentials_options valid_options = { |
| 984 |
test_sts_endpoint_url, // sts_endpoint_url |
| 985 |
nullptr, // resource |
| 986 |
nullptr, // audience |
| 987 |
nullptr, // scope |
| 988 |
nullptr, // requested_token_type |
| 989 |
test_signed_jwt_path_prefix, // subject_token_path |
| 990 |
test_signed_jwt_token_type, // subject_token_type |
| 991 |
nullptr, // actor_token_path |
| 992 |
nullptr // actor_token_type |
| 993 |
}; |
| 994 |
absl::StatusOr<URI> sts_url = ValidateStsCredentialsOptions(&valid_options); |
| 995 |
GPR_ASSERT(sts_url.ok()); |
| 996 |
absl::string_view host; |
| 997 |
absl::string_view port; |
| 998 |
GPR_ASSERT(SplitHostPort(sts_url->authority(), &host, &port)); |
| 999 |
GPR_ASSERT(host == "foo.com"); |
| 1000 |
GPR_ASSERT(port == "5555"); |
| 1001 |
} |
| 1002 |
|
| 1003 |
TEST(CredentialsTest, TestInvalidStsCredsOptions) { |
| 1004 |
grpc_sts_credentials_options invalid_options = { |
| 1005 |
test_sts_endpoint_url, // sts_endpoint_url |
| 1006 |
nullptr, // resource |
| 1007 |
nullptr, // audience |
| 1008 |
nullptr, // scope |
| 1009 |
nullptr, // requested_token_type |
| 1010 |
nullptr, // subject_token_path (Required) |
| 1011 |
test_signed_jwt_token_type, // subject_token_type |
| 1012 |
nullptr, // actor_token_path |
| 1013 |
nullptr // actor_token_type |
| 1014 |
}; |
| 1015 |
absl::StatusOr<URI> url_should_be_invalid = |
| 1016 |
ValidateStsCredentialsOptions(&invalid_options); |
| 1017 |
GPR_ASSERT(!url_should_be_invalid.ok()); |
| 1018 |
|
| 1019 |
invalid_options = { |
| 1020 |
test_sts_endpoint_url, // sts_endpoint_url |
| 1021 |
nullptr, // resource |
| 1022 |
nullptr, // audience |
| 1023 |
nullptr, // scope |
| 1024 |
nullptr, // requested_token_type |
| 1025 |
test_signed_jwt_path_prefix, // subject_token_path |
| 1026 |
nullptr, // subject_token_type (Required) |
| 1027 |
nullptr, // actor_token_path |
| 1028 |
nullptr // actor_token_type |
| 1029 |
}; |
| 1030 |
url_should_be_invalid = ValidateStsCredentialsOptions(&invalid_options); |
| 1031 |
GPR_ASSERT(!url_should_be_invalid.ok()); |
| 1032 |
|
| 1033 |
invalid_options = { |
| 1034 |
nullptr, // sts_endpoint_url (Required) |
| 1035 |
nullptr, // resource |
| 1036 |
nullptr, // audience |
| 1037 |
nullptr, // scope |
| 1038 |
nullptr, // requested_token_type |
| 1039 |
test_signed_jwt_path_prefix, // subject_token_path |
| 1040 |
test_signed_jwt_token_type, // subject_token_type (Required) |
| 1041 |
nullptr, // actor_token_path |
| 1042 |
nullptr // actor_token_type |
| 1043 |
}; |
| 1044 |
url_should_be_invalid = ValidateStsCredentialsOptions(&invalid_options); |
| 1045 |
GPR_ASSERT(!url_should_be_invalid.ok()); |
| 1046 |
|
| 1047 |
invalid_options = { |
| 1048 |
"not_a_valid_uri", // sts_endpoint_url |
| 1049 |
nullptr, // resource |
| 1050 |
nullptr, // audience |
| 1051 |
nullptr, // scope |
| 1052 |
nullptr, // requested_token_type |
| 1053 |
test_signed_jwt_path_prefix, // subject_token_path |
| 1054 |
test_signed_jwt_token_type, // subject_token_type (Required) |
| 1055 |
nullptr, // actor_token_path |
| 1056 |
nullptr // actor_token_type |
| 1057 |
}; |
| 1058 |
url_should_be_invalid = ValidateStsCredentialsOptions(&invalid_options); |
| 1059 |
GPR_ASSERT(!url_should_be_invalid.ok()); |
| 1060 |
|
| 1061 |
invalid_options = { |
| 1062 |
"ftp://ftp.is.not.a.valid.scheme/bar", // sts_endpoint_url |
| 1063 |
nullptr, // resource |
| 1064 |
nullptr, // audience |
| 1065 |
nullptr, // scope |
| 1066 |
nullptr, // requested_token_type |
| 1067 |
test_signed_jwt_path_prefix, // subject_token_path |
| 1068 |
test_signed_jwt_token_type, // subject_token_type (Required) |
| 1069 |
nullptr, // actor_token_path |
| 1070 |
nullptr // actor_token_type |
| 1071 |
}; |
| 1072 |
url_should_be_invalid = ValidateStsCredentialsOptions(&invalid_options); |
| 1073 |
GPR_ASSERT(!url_should_be_invalid.ok()); |
| 1074 |
} |
| 1075 |
|
| 1076 |
void assert_query_parameters(const URI& uri, absl::string_view expected_key, |
| 1077 |
absl::string_view expected_val) { |
| 1078 |
const auto it = uri.query_parameter_map().find(expected_key); |
| 1079 |
GPR_ASSERT(it != uri.query_parameter_map().end()); |
| 1080 |
if (it->second != expected_val) { |
| 1081 |
gpr_log(GPR_ERROR, "%s!=%s", std::string(it->second).c_str(), |
| 1082 |
std::string(expected_val).c_str()); |
| 1083 |
} |
| 1084 |
GPR_ASSERT(it->second == expected_val); |
| 1085 |
} |
| 1086 |
|
| 1087 |
void validate_sts_token_http_request(const grpc_http_request* request, |
| 1088 |
const char* host, const char* path, |
| 1089 |
const char* body, size_t body_size, |
| 1090 |
bool expect_actor_token) { |
| 1091 |
// Check that the body is constructed properly. |
| 1092 |
GPR_ASSERT(body != nullptr); |
| 1093 |
GPR_ASSERT(body_size != 0); |
| 1094 |
std::string get_url_equivalent = |
| 1095 |
absl::StrFormat("%s?%s", test_sts_endpoint_url, body); |
| 1096 |
absl::StatusOr<URI> url = URI::Parse(get_url_equivalent); |
| 1097 |
if (!url.ok()) { |
| 1098 |
gpr_log(GPR_ERROR, "%s", url.status().ToString().c_str()); |
| 1099 |
GPR_ASSERT(url.ok()); |
| 1100 |
} |
| 1101 |
assert_query_parameters(*url, "resource", "resource"); |
| 1102 |
assert_query_parameters(*url, "audience", "audience"); |
| 1103 |
assert_query_parameters(*url, "scope", "scope"); |
| 1104 |
assert_query_parameters(*url, "requested_token_type", "requested_token_type"); |
| 1105 |
assert_query_parameters(*url, "subject_token", test_signed_jwt); |
| 1106 |
assert_query_parameters(*url, "subject_token_type", |
| 1107 |
test_signed_jwt_token_type); |
| 1108 |
if (expect_actor_token) { |
| 1109 |
assert_query_parameters(*url, "actor_token", test_signed_jwt2); |
| 1110 |
assert_query_parameters(*url, "actor_token_type", |
| 1111 |
test_signed_jwt_token_type2); |
| 1112 |
} else { |
| 1113 |
GPR_ASSERT(url->query_parameter_map().find("actor_token") == |
| 1114 |
url->query_parameter_map().end()); |
| 1115 |
GPR_ASSERT(url->query_parameter_map().find("actor_token_type") == |
| 1116 |
url->query_parameter_map().end()); |
| 1117 |
} |
| 1118 |
|
| 1119 |
// Check the rest of the request. |
| 1120 |
GPR_ASSERT(strcmp(host, "foo.com:5555") == 0); |
| 1121 |
GPR_ASSERT(strcmp(path, "/v1/token-exchange") == 0); |
| 1122 |
GPR_ASSERT(request->hdr_count == 1); |
| 1123 |
GPR_ASSERT(strcmp(request->hdrs[0].key, "Content-Type") == 0); |
| 1124 |
GPR_ASSERT( |
| 1125 |
strcmp(request->hdrs[0].value, "application/x-www-form-urlencoded") == 0); |
| 1126 |
} |
| 1127 |
|
| 1128 |
int sts_token_httpcli_post_success(const grpc_http_request* request, |
| 1129 |
const char* host, const char* path, |
| 1130 |
const char* body, size_t body_size, |
| 1131 |
Timestamp /*deadline*/, |
| 1132 |
grpc_closure* on_done, |
| 1133 |
grpc_http_response* response) { |
| 1134 |
validate_sts_token_http_request(request, host, path, body, body_size, true); |
| 1135 |
*response = http_response(200, valid_sts_json_response); |
| 1136 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 1137 |
return 1; |
| 1138 |
} |
| 1139 |
|
| 1140 |
int sts_token_httpcli_post_success_no_actor_token( |
| 1141 |
const grpc_http_request* request, const char* host, const char* path, |
| 1142 |
const char* body, size_t body_size, Timestamp /*deadline*/, |
| 1143 |
grpc_closure* on_done, grpc_http_response* response) { |
| 1144 |
validate_sts_token_http_request(request, host, path, body, body_size, false); |
| 1145 |
*response = http_response(200, valid_sts_json_response); |
| 1146 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 1147 |
return 1; |
| 1148 |
} |
| 1149 |
|
| 1150 |
char* write_tmp_jwt_file(const char* jwt_contents) { |
| 1151 |
char* path; |
| 1152 |
FILE* tmp = gpr_tmpfile(test_signed_jwt_path_prefix, &path); |
| 1153 |
GPR_ASSERT(path != nullptr); |
| 1154 |
GPR_ASSERT(tmp != nullptr); |
| 1155 |
size_t jwt_length = strlen(jwt_contents); |
| 1156 |
GPR_ASSERT(fwrite(jwt_contents, 1, jwt_length, tmp) == jwt_length); |
| 1157 |
fclose(tmp); |
| 1158 |
return path; |
| 1159 |
} |
| 1160 |
|
| 1161 |
TEST(CredentialsTest, TestStsCredsSuccess) { |
| 1162 |
ExecCtx exec_ctx; |
| 1163 |
std::string emd = "authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"; |
| 1164 |
const char expected_creds_debug_string[] = |
| 1165 |
"StsTokenFetcherCredentials{Path:/v1/" |
| 1166 |
"token-exchange,Authority:foo.com:5555,OAuth2TokenFetcherCredentials}"; |
| 1167 |
char* subject_token_path = write_tmp_jwt_file(test_signed_jwt); |
| 1168 |
char* actor_token_path = write_tmp_jwt_file(test_signed_jwt2); |
| 1169 |
grpc_sts_credentials_options valid_options = { |
| 1170 |
test_sts_endpoint_url, // sts_endpoint_url |
| 1171 |
"resource", // resource |
| 1172 |
"audience", // audience |
| 1173 |
"scope", // scope |
| 1174 |
"requested_token_type", // requested_token_type |
| 1175 |
subject_token_path, // subject_token_path |
| 1176 |
test_signed_jwt_token_type, // subject_token_type |
| 1177 |
actor_token_path, // actor_token_path |
| 1178 |
test_signed_jwt_token_type2 // actor_token_type |
| 1179 |
}; |
| 1180 |
grpc_call_credentials* creds = |
| 1181 |
grpc_sts_credentials_create(&valid_options, nullptr); |
| 1182 |
|
| 1183 |
/* Check security level. */ |
| 1184 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 1185 |
|
| 1186 |
/* First request: http put should be called. */ |
| 1187 |
auto state = RequestMetadataState::NewInstance(absl::OkStatus(), emd); |
| 1188 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 1189 |
sts_token_httpcli_post_success, |
| 1190 |
httpcli_put_should_not_be_called); |
| 1191 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 1192 |
kTestPath); |
| 1193 |
ExecCtx::Get()->Flush(); |
| 1194 |
|
| 1195 |
/* Second request: the cached token should be served directly. */ |
| 1196 |
state = RequestMetadataState::NewInstance(absl::OkStatus(), emd); |
| 1197 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 1198 |
httpcli_post_should_not_be_called, |
| 1199 |
httpcli_put_should_not_be_called); |
| 1200 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 1201 |
kTestPath); |
| 1202 |
ExecCtx::Get()->Flush(); |
| 1203 |
GPR_ASSERT( |
| 1204 |
strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0); |
| 1205 |
|
| 1206 |
creds->Unref(); |
| 1207 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 1208 |
gpr_free(subject_token_path); |
| 1209 |
gpr_free(actor_token_path); |
| 1210 |
} |
| 1211 |
|
| 1212 |
TEST(CredentialsTest, TestStsCredsTokenFileNotFound) { |
| 1213 |
ExecCtx exec_ctx; |
| 1214 |
grpc_sts_credentials_options valid_options = { |
| 1215 |
test_sts_endpoint_url, // sts_endpoint_url |
| 1216 |
"resource", // resource |
| 1217 |
"audience", // audience |
| 1218 |
"scope", // scope |
| 1219 |
"requested_token_type", // requested_token_type |
| 1220 |
"/some/completely/random/path", // subject_token_path |
| 1221 |
test_signed_jwt_token_type, // subject_token_type |
| 1222 |
"", // actor_token_path |
| 1223 |
"" // actor_token_type |
| 1224 |
}; |
| 1225 |
grpc_call_credentials* creds = |
| 1226 |
grpc_sts_credentials_create(&valid_options, nullptr); |
| 1227 |
|
| 1228 |
/* Check security level. */ |
| 1229 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 1230 |
|
| 1231 |
auto state = RequestMetadataState::NewInstance( |
| 1232 |
GRPC_ERROR_CREATE("Error occurred when fetching oauth2 token."), {}); |
| 1233 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 1234 |
httpcli_post_should_not_be_called, |
| 1235 |
httpcli_put_should_not_be_called); |
| 1236 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 1237 |
kTestPath); |
| 1238 |
ExecCtx::Get()->Flush(); |
| 1239 |
|
| 1240 |
/* Cleanup. */ |
| 1241 |
creds->Unref(); |
| 1242 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 1243 |
} |
| 1244 |
|
| 1245 |
TEST(CredentialsTest, TestStsCredsNoActorTokenSuccess) { |
| 1246 |
ExecCtx exec_ctx; |
| 1247 |
std::string emd = "authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"; |
| 1248 |
const char expected_creds_debug_string[] = |
| 1249 |
"StsTokenFetcherCredentials{Path:/v1/" |
| 1250 |
"token-exchange,Authority:foo.com:5555,OAuth2TokenFetcherCredentials}"; |
| 1251 |
char* subject_token_path = write_tmp_jwt_file(test_signed_jwt); |
| 1252 |
grpc_sts_credentials_options valid_options = { |
| 1253 |
test_sts_endpoint_url, // sts_endpoint_url |
| 1254 |
"resource", // resource |
| 1255 |
"audience", // audience |
| 1256 |
"scope", // scope |
| 1257 |
"requested_token_type", // requested_token_type |
| 1258 |
subject_token_path, // subject_token_path |
| 1259 |
test_signed_jwt_token_type, // subject_token_type |
| 1260 |
"", // actor_token_path |
| 1261 |
"" // actor_token_type |
| 1262 |
}; |
| 1263 |
grpc_call_credentials* creds = |
| 1264 |
grpc_sts_credentials_create(&valid_options, nullptr); |
| 1265 |
|
| 1266 |
/* Check security level. */ |
| 1267 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 1268 |
|
| 1269 |
/* First request: http put should be called. */ |
| 1270 |
auto state = RequestMetadataState::NewInstance(absl::OkStatus(), emd); |
| 1271 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 1272 |
sts_token_httpcli_post_success_no_actor_token, |
| 1273 |
httpcli_put_should_not_be_called); |
| 1274 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 1275 |
kTestPath); |
| 1276 |
ExecCtx::Get()->Flush(); |
| 1277 |
|
| 1278 |
/* Second request: the cached token should be served directly. */ |
| 1279 |
state = RequestMetadataState::NewInstance(absl::OkStatus(), emd); |
| 1280 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 1281 |
httpcli_post_should_not_be_called, |
| 1282 |
httpcli_put_should_not_be_called); |
| 1283 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 1284 |
kTestPath); |
| 1285 |
ExecCtx::Get()->Flush(); |
| 1286 |
GPR_ASSERT( |
| 1287 |
strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0); |
| 1288 |
|
| 1289 |
creds->Unref(); |
| 1290 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 1291 |
gpr_free(subject_token_path); |
| 1292 |
} |
| 1293 |
|
| 1294 |
TEST(CredentialsTest, TestStsCredsLoadTokenFailure) { |
| 1295 |
const char expected_creds_debug_string[] = |
| 1296 |
"StsTokenFetcherCredentials{Path:/v1/" |
| 1297 |
"token-exchange,Authority:foo.com:5555,OAuth2TokenFetcherCredentials}"; |
| 1298 |
ExecCtx exec_ctx; |
| 1299 |
auto state = RequestMetadataState::NewInstance( |
| 1300 |
GRPC_ERROR_CREATE("Error occurred when fetching oauth2 token."), {}); |
| 1301 |
char* test_signed_jwt_path = write_tmp_jwt_file(test_signed_jwt); |
| 1302 |
grpc_sts_credentials_options options = { |
| 1303 |
test_sts_endpoint_url, // sts_endpoint_url |
| 1304 |
"resource", // resource |
| 1305 |
"audience", // audience |
| 1306 |
"scope", // scope |
| 1307 |
"requested_token_type", // requested_token_type |
| 1308 |
"invalid_path", // subject_token_path |
| 1309 |
test_signed_jwt_token_type, // subject_token_type |
| 1310 |
nullptr, // actor_token_path |
| 1311 |
nullptr // actor_token_type |
| 1312 |
}; |
| 1313 |
grpc_call_credentials* creds = grpc_sts_credentials_create(&options, nullptr); |
| 1314 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 1315 |
httpcli_post_should_not_be_called, |
| 1316 |
httpcli_put_should_not_be_called); |
| 1317 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 1318 |
kTestPath); |
| 1319 |
GPR_ASSERT( |
| 1320 |
strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0); |
| 1321 |
|
| 1322 |
creds->Unref(); |
| 1323 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 1324 |
gpr_free(test_signed_jwt_path); |
| 1325 |
} |
| 1326 |
|
| 1327 |
TEST(CredentialsTest, TestStsCredsHttpFailure) { |
| 1328 |
const char expected_creds_debug_string[] = |
| 1329 |
"StsTokenFetcherCredentials{Path:/v1/" |
| 1330 |
"token-exchange,Authority:foo.com:5555,OAuth2TokenFetcherCredentials}"; |
| 1331 |
ExecCtx exec_ctx; |
| 1332 |
auto state = RequestMetadataState::NewInstance( |
| 1333 |
GRPC_ERROR_CREATE("Error occurred when fetching oauth2 token."), {}); |
| 1334 |
char* test_signed_jwt_path = write_tmp_jwt_file(test_signed_jwt); |
| 1335 |
grpc_sts_credentials_options valid_options = { |
| 1336 |
test_sts_endpoint_url, // sts_endpoint_url |
| 1337 |
"resource", // resource |
| 1338 |
"audience", // audience |
| 1339 |
"scope", // scope |
| 1340 |
"requested_token_type", // requested_token_type |
| 1341 |
test_signed_jwt_path, // subject_token_path |
| 1342 |
test_signed_jwt_token_type, // subject_token_type |
| 1343 |
nullptr, // actor_token_path |
| 1344 |
nullptr // actor_token_type |
| 1345 |
}; |
| 1346 |
grpc_call_credentials* creds = |
| 1347 |
grpc_sts_credentials_create(&valid_options, nullptr); |
| 1348 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 1349 |
token_httpcli_post_failure, |
| 1350 |
httpcli_put_should_not_be_called); |
| 1351 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 1352 |
kTestPath); |
| 1353 |
GPR_ASSERT( |
| 1354 |
strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0); |
| 1355 |
creds->Unref(); |
| 1356 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 1357 |
gpr_free(test_signed_jwt_path); |
| 1358 |
} |
| 1359 |
|
| 1360 |
void validate_jwt_encode_and_sign_params(const grpc_auth_json_key* json_key, |
| 1361 |
const char* scope, |
| 1362 |
gpr_timespec token_lifetime) { |
| 1363 |
GPR_ASSERT(grpc_auth_json_key_is_valid(json_key)); |
| 1364 |
GPR_ASSERT(json_key->private_key != nullptr); |
| 1365 |
GPR_ASSERT(RSA_check_key(json_key->private_key)); |
| 1366 |
GPR_ASSERT(json_key->type != nullptr && |
| 1367 |
strcmp(json_key->type, "service_account") == 0); |
| 1368 |
GPR_ASSERT(json_key->private_key_id != nullptr && |
| 1369 |
strcmp(json_key->private_key_id, |
| 1370 |
"e6b5137873db8d2ef81e06a47289e6434ec8a165") == 0); |
| 1371 |
GPR_ASSERT(json_key->client_id != nullptr && |
| 1372 |
strcmp(json_key->client_id, |
| 1373 |
"777-abaslkan11hlb6nmim3bpspl31ud.apps." |
| 1374 |
"googleusercontent.com") == 0); |
| 1375 |
GPR_ASSERT(json_key->client_email != nullptr && |
| 1376 |
strcmp(json_key->client_email, |
| 1377 |
"777-abaslkan11hlb6nmim3bpspl31ud@developer." |
| 1378 |
"gserviceaccount.com") == 0); |
| 1379 |
if (scope != nullptr) GPR_ASSERT(strcmp(scope, test_scope) == 0); |
| 1380 |
GPR_ASSERT(gpr_time_cmp(token_lifetime, grpc_max_auth_token_lifetime()) == 0); |
| 1381 |
} |
| 1382 |
|
| 1383 |
char* encode_and_sign_jwt_success(const grpc_auth_json_key* json_key, |
| 1384 |
const char* audience, |
| 1385 |
gpr_timespec token_lifetime, |
| 1386 |
const char* scope) { |
| 1387 |
if (strcmp(audience, test_service_url_no_service_name) != 0 && |
| 1388 |
strcmp(audience, other_test_service_url_no_service_name) != 0) { |
| 1389 |
return nullptr; |
| 1390 |
} |
| 1391 |
validate_jwt_encode_and_sign_params(json_key, scope, token_lifetime); |
| 1392 |
return gpr_strdup(test_signed_jwt); |
| 1393 |
} |
| 1394 |
|
| 1395 |
char* encode_and_sign_jwt_failure(const grpc_auth_json_key* json_key, |
| 1396 |
const char* /*audience*/, |
| 1397 |
gpr_timespec token_lifetime, |
| 1398 |
const char* scope) { |
| 1399 |
validate_jwt_encode_and_sign_params(json_key, scope, token_lifetime); |
| 1400 |
return nullptr; |
| 1401 |
} |
| 1402 |
|
| 1403 |
char* encode_and_sign_jwt_should_not_be_called( |
| 1404 |
const grpc_auth_json_key* /*json_key*/, const char* /*audience*/, |
| 1405 |
gpr_timespec /*token_lifetime*/, const char* /*scope*/) { |
| 1406 |
GPR_ASSERT("grpc_jwt_encode_and_sign should not be called" == nullptr); |
| 1407 |
return nullptr; |
| 1408 |
} |
| 1409 |
|
| 1410 |
grpc_service_account_jwt_access_credentials* creds_as_jwt( |
| 1411 |
grpc_call_credentials* creds) { |
| 1412 |
GPR_ASSERT(creds != nullptr); |
| 1413 |
GPR_ASSERT(creds->type() == |
| 1414 |
grpc_service_account_jwt_access_credentials::Type()); |
| 1415 |
return reinterpret_cast<grpc_service_account_jwt_access_credentials*>(creds); |
| 1416 |
} |
| 1417 |
|
| 1418 |
TEST(CredentialsTest, TestJwtCredsLifetime) { |
| 1419 |
char* json_key_string = test_json_key_str(); |
| 1420 |
const char expected_creds_debug_string_prefix[] = |
| 1421 |
"JWTAccessCredentials{ExpirationTime:"; |
| 1422 |
// Max lifetime. |
| 1423 |
grpc_call_credentials* jwt_creds = |
| 1424 |
grpc_service_account_jwt_access_credentials_create( |
| 1425 |
json_key_string, grpc_max_auth_token_lifetime(), nullptr); |
| 1426 |
GPR_ASSERT(gpr_time_cmp(creds_as_jwt(jwt_creds)->jwt_lifetime(), |
| 1427 |
grpc_max_auth_token_lifetime()) == 0); |
| 1428 |
/* Check security level. */ |
| 1429 |
GPR_ASSERT(jwt_creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 1430 |
GPR_ASSERT(strncmp(expected_creds_debug_string_prefix, |
| 1431 |
jwt_creds->debug_string().c_str(), |
| 1432 |
strlen(expected_creds_debug_string_prefix)) == 0); |
| 1433 |
grpc_call_credentials_release(jwt_creds); |
| 1434 |
|
| 1435 |
// Shorter lifetime. |
| 1436 |
gpr_timespec token_lifetime = {10, 0, GPR_TIMESPAN}; |
| 1437 |
GPR_ASSERT(gpr_time_cmp(grpc_max_auth_token_lifetime(), token_lifetime) > 0); |
| 1438 |
jwt_creds = grpc_service_account_jwt_access_credentials_create( |
| 1439 |
json_key_string, token_lifetime, nullptr); |
| 1440 |
GPR_ASSERT(gpr_time_cmp(creds_as_jwt(jwt_creds)->jwt_lifetime(), |
| 1441 |
token_lifetime) == 0); |
| 1442 |
GPR_ASSERT(strncmp(expected_creds_debug_string_prefix, |
| 1443 |
jwt_creds->debug_string().c_str(), |
| 1444 |
strlen(expected_creds_debug_string_prefix)) == 0); |
| 1445 |
grpc_call_credentials_release(jwt_creds); |
| 1446 |
|
| 1447 |
// Cropped lifetime. |
| 1448 |
gpr_timespec add_to_max = {10, 0, GPR_TIMESPAN}; |
| 1449 |
token_lifetime = gpr_time_add(grpc_max_auth_token_lifetime(), add_to_max); |
| 1450 |
jwt_creds = grpc_service_account_jwt_access_credentials_create( |
| 1451 |
json_key_string, token_lifetime, nullptr); |
| 1452 |
GPR_ASSERT(gpr_time_cmp(creds_as_jwt(jwt_creds)->jwt_lifetime(), |
| 1453 |
grpc_max_auth_token_lifetime()) == 0); |
| 1454 |
GPR_ASSERT(strncmp(expected_creds_debug_string_prefix, |
| 1455 |
jwt_creds->debug_string().c_str(), |
| 1456 |
strlen(expected_creds_debug_string_prefix)) == 0); |
| 1457 |
grpc_call_credentials_release(jwt_creds); |
| 1458 |
|
| 1459 |
gpr_free(json_key_string); |
| 1460 |
} |
| 1461 |
|
| 1462 |
TEST(CredentialsTest, TestRemoveServiceFromJwtUri) { |
| 1463 |
const char wrong_uri[] = "hello world"; |
| 1464 |
GPR_ASSERT(!RemoveServiceNameFromJwtUri(wrong_uri).ok()); |
| 1465 |
const char valid_uri[] = "https://foo.com/get/"; |
| 1466 |
const char expected_uri[] = "https://foo.com/"; |
| 1467 |
auto output = RemoveServiceNameFromJwtUri(valid_uri); |
| 1468 |
GPR_ASSERT(output.ok()); |
| 1469 |
GPR_ASSERT(strcmp(output->c_str(), expected_uri) == 0); |
| 1470 |
} |
| 1471 |
|
| 1472 |
TEST(CredentialsTest, TestJwtCredsSuccess) { |
| 1473 |
const char expected_creds_debug_string_prefix[] = |
| 1474 |
"JWTAccessCredentials{ExpirationTime:"; |
| 1475 |
|
| 1476 |
char* json_key_string = test_json_key_str(); |
| 1477 |
ExecCtx exec_ctx; |
| 1478 |
std::string expected_md_value = absl::StrCat("Bearer ", test_signed_jwt); |
| 1479 |
std::string emd = absl::StrCat("authorization: ", expected_md_value); |
| 1480 |
grpc_call_credentials* creds = |
| 1481 |
grpc_service_account_jwt_access_credentials_create( |
| 1482 |
json_key_string, grpc_max_auth_token_lifetime(), nullptr); |
| 1483 |
|
| 1484 |
/* First request: jwt_encode_and_sign should be called. */ |
| 1485 |
auto state = RequestMetadataState::NewInstance(absl::OkStatus(), emd); |
| 1486 |
grpc_jwt_encode_and_sign_set_override(encode_and_sign_jwt_success); |
| 1487 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 1488 |
kTestPath); |
| 1489 |
ExecCtx::Get()->Flush(); |
| 1490 |
|
| 1491 |
/* Second request: the cached token should be served directly. */ |
| 1492 |
state = RequestMetadataState::NewInstance(absl::OkStatus(), emd); |
| 1493 |
grpc_jwt_encode_and_sign_set_override( |
| 1494 |
encode_and_sign_jwt_should_not_be_called); |
| 1495 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 1496 |
kTestPath); |
| 1497 |
ExecCtx::Get()->Flush(); |
| 1498 |
|
| 1499 |
/* Third request: Different service url so jwt_encode_and_sign should be |
| 1500 |
called again (no caching). */ |
| 1501 |
state = RequestMetadataState::NewInstance(absl::OkStatus(), emd); |
| 1502 |
grpc_jwt_encode_and_sign_set_override(encode_and_sign_jwt_success); |
| 1503 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestOtherAuthority, |
| 1504 |
kTestOtherPath); |
| 1505 |
ExecCtx::Get()->Flush(); |
| 1506 |
GPR_ASSERT(strncmp(expected_creds_debug_string_prefix, |
| 1507 |
creds->debug_string().c_str(), |
| 1508 |
strlen(expected_creds_debug_string_prefix)) == 0); |
| 1509 |
|
| 1510 |
creds->Unref(); |
| 1511 |
gpr_free(json_key_string); |
| 1512 |
grpc_jwt_encode_and_sign_set_override(nullptr); |
| 1513 |
} |
| 1514 |
|
| 1515 |
TEST(CredentialsTest, TestJwtCredsSigningFailure) { |
| 1516 |
const char expected_creds_debug_string_prefix[] = |
| 1517 |
"JWTAccessCredentials{ExpirationTime:"; |
| 1518 |
char* json_key_string = test_json_key_str(); |
| 1519 |
ExecCtx exec_ctx; |
| 1520 |
auto state = RequestMetadataState::NewInstance( |
| 1521 |
GRPC_ERROR_CREATE("Could not generate JWT."), {}); |
| 1522 |
grpc_call_credentials* creds = |
| 1523 |
grpc_service_account_jwt_access_credentials_create( |
| 1524 |
json_key_string, grpc_max_auth_token_lifetime(), nullptr); |
| 1525 |
|
| 1526 |
grpc_jwt_encode_and_sign_set_override(encode_and_sign_jwt_failure); |
| 1527 |
state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 1528 |
kTestPath); |
| 1529 |
|
| 1530 |
gpr_free(json_key_string); |
| 1531 |
GPR_ASSERT(strncmp(expected_creds_debug_string_prefix, |
| 1532 |
creds->debug_string().c_str(), |
| 1533 |
strlen(expected_creds_debug_string_prefix)) == 0); |
| 1534 |
|
| 1535 |
creds->Unref(); |
| 1536 |
grpc_jwt_encode_and_sign_set_override(nullptr); |
| 1537 |
} |
| 1538 |
|
| 1539 |
void set_google_default_creds_env_var_with_file_contents( |
| 1540 |
const char* file_prefix, const char* contents) { |
| 1541 |
size_t contents_len = strlen(contents); |
| 1542 |
char* creds_file_name; |
| 1543 |
FILE* creds_file = gpr_tmpfile(file_prefix, &creds_file_name); |
| 1544 |
GPR_ASSERT(creds_file_name != nullptr); |
| 1545 |
GPR_ASSERT(creds_file != nullptr); |
| 1546 |
GPR_ASSERT(fwrite(contents, 1, contents_len, creds_file) == contents_len); |
| 1547 |
fclose(creds_file); |
| 1548 |
SetEnv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, creds_file_name); |
| 1549 |
gpr_free(creds_file_name); |
| 1550 |
} |
| 1551 |
|
| 1552 |
bool test_gce_tenancy_checker(void) { |
| 1553 |
g_test_gce_tenancy_checker_called = true; |
| 1554 |
return g_test_is_on_gce; |
| 1555 |
} |
| 1556 |
|
| 1557 |
std::string null_well_known_creds_path_getter(void) { return ""; } |
| 1558 |
|
| 1559 |
TEST(CredentialsTest, TestGoogleDefaultCredsAuthKey) { |
| 1560 |
ExecCtx exec_ctx; |
| 1561 |
grpc_composite_channel_credentials* creds; |
| 1562 |
char* json_key = test_json_key_str(); |
| 1563 |
grpc_flush_cached_google_default_credentials(); |
| 1564 |
set_gce_tenancy_checker_for_testing(test_gce_tenancy_checker); |
| 1565 |
g_test_gce_tenancy_checker_called = false; |
| 1566 |
g_test_is_on_gce = true; |
| 1567 |
set_google_default_creds_env_var_with_file_contents( |
| 1568 |
"json_key_google_default_creds", json_key); |
| 1569 |
grpc_override_well_known_credentials_path_getter( |
| 1570 |
null_well_known_creds_path_getter); |
| 1571 |
gpr_free(json_key); |
| 1572 |
creds = reinterpret_cast<grpc_composite_channel_credentials*>( |
| 1573 |
grpc_google_default_credentials_create(nullptr)); |
| 1574 |
auto* default_creds = |
| 1575 |
reinterpret_cast<const grpc_google_default_channel_credentials*>( |
| 1576 |
creds->inner_creds()); |
| 1577 |
GPR_ASSERT(default_creds->ssl_creds() != nullptr); |
| 1578 |
auto* jwt = |
| 1579 |
reinterpret_cast<const grpc_service_account_jwt_access_credentials*>( |
| 1580 |
creds->call_creds()); |
| 1581 |
GPR_ASSERT( |
| 1582 |
strcmp(jwt->key().client_id, |
| 1583 |
"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent.com") == |
| 1584 |
0); |
| 1585 |
GPR_ASSERT(g_test_gce_tenancy_checker_called == false); |
| 1586 |
creds->Unref(); |
| 1587 |
SetEnv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ |
| 1588 |
grpc_override_well_known_credentials_path_getter(nullptr); |
| 1589 |
} |
| 1590 |
|
| 1591 |
TEST(CredentialsTest, TestGoogleDefaultCredsRefreshToken) { |
| 1592 |
ExecCtx exec_ctx; |
| 1593 |
grpc_composite_channel_credentials* creds; |
| 1594 |
grpc_flush_cached_google_default_credentials(); |
| 1595 |
set_google_default_creds_env_var_with_file_contents( |
| 1596 |
"refresh_token_google_default_creds", test_refresh_token_str); |
| 1597 |
grpc_override_well_known_credentials_path_getter( |
| 1598 |
null_well_known_creds_path_getter); |
| 1599 |
creds = reinterpret_cast<grpc_composite_channel_credentials*>( |
| 1600 |
grpc_google_default_credentials_create(nullptr)); |
| 1601 |
auto* default_creds = |
| 1602 |
reinterpret_cast<const grpc_google_default_channel_credentials*>( |
| 1603 |
creds->inner_creds()); |
| 1604 |
GPR_ASSERT(default_creds->ssl_creds() != nullptr); |
| 1605 |
auto* refresh = |
| 1606 |
reinterpret_cast<const grpc_google_refresh_token_credentials*>( |
| 1607 |
creds->call_creds()); |
| 1608 |
GPR_ASSERT(strcmp(refresh->refresh_token().client_id, |
| 1609 |
"32555999999.apps.googleusercontent.com") == 0); |
| 1610 |
creds->Unref(); |
| 1611 |
SetEnv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ |
| 1612 |
grpc_override_well_known_credentials_path_getter(nullptr); |
| 1613 |
} |
| 1614 |
|
| 1615 |
TEST(CredentialsTest, TestGoogleDefaultCredsExternalAccountCredentials) { |
| 1616 |
ExecCtx exec_ctx; |
| 1617 |
grpc_composite_channel_credentials* creds; |
| 1618 |
grpc_flush_cached_google_default_credentials(); |
| 1619 |
set_google_default_creds_env_var_with_file_contents( |
| 1620 |
"google_default_creds_external_account_credentials", |
| 1621 |
test_external_account_credentials_str); |
| 1622 |
grpc_override_well_known_credentials_path_getter( |
| 1623 |
null_well_known_creds_path_getter); |
| 1624 |
creds = reinterpret_cast<grpc_composite_channel_credentials*>( |
| 1625 |
grpc_google_default_credentials_create(nullptr)); |
| 1626 |
auto* default_creds = |
| 1627 |
reinterpret_cast<const grpc_google_default_channel_credentials*>( |
| 1628 |
creds->inner_creds()); |
| 1629 |
GPR_ASSERT(default_creds->ssl_creds() != nullptr); |
| 1630 |
auto* external = |
| 1631 |
reinterpret_cast<const ExternalAccountCredentials*>(creds->call_creds()); |
| 1632 |
GPR_ASSERT(external != nullptr); |
| 1633 |
creds->Unref(); |
| 1634 |
SetEnv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ |
| 1635 |
grpc_override_well_known_credentials_path_getter(nullptr); |
| 1636 |
} |
| 1637 |
|
| 1638 |
TEST(CredentialsTest, |
| 1639 |
TestGoogleDefaultCredsExternalAccountCredentialsMultiPatternSts) { |
| 1640 |
ExecCtx exec_ctx; |
| 1641 |
grpc_composite_channel_credentials* creds; |
| 1642 |
grpc_flush_cached_google_default_credentials(); |
| 1643 |
set_google_default_creds_env_var_with_file_contents( |
| 1644 |
"google_default_creds_external_account_credentials_multi_pattern_sts", |
| 1645 |
test_external_account_credentials_multi_pattern_sts_str); |
| 1646 |
grpc_override_well_known_credentials_path_getter( |
| 1647 |
null_well_known_creds_path_getter); |
| 1648 |
creds = reinterpret_cast<grpc_composite_channel_credentials*>( |
| 1649 |
grpc_google_default_credentials_create(nullptr)); |
| 1650 |
auto* default_creds = |
| 1651 |
reinterpret_cast<const grpc_google_default_channel_credentials*>( |
| 1652 |
creds->inner_creds()); |
| 1653 |
GPR_ASSERT(default_creds->ssl_creds() != nullptr); |
| 1654 |
auto* external = |
| 1655 |
reinterpret_cast<const ExternalAccountCredentials*>(creds->call_creds()); |
| 1656 |
GPR_ASSERT(external != nullptr); |
| 1657 |
creds->Unref(); |
| 1658 |
SetEnv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ |
| 1659 |
grpc_override_well_known_credentials_path_getter(nullptr); |
| 1660 |
} |
| 1661 |
|
| 1662 |
TEST(CredentialsTest, |
| 1663 |
TestGoogleDefaultCredsExternalAccountCredentialsMultiPatternIam) { |
| 1664 |
ExecCtx exec_ctx; |
| 1665 |
grpc_composite_channel_credentials* creds; |
| 1666 |
grpc_flush_cached_google_default_credentials(); |
| 1667 |
set_google_default_creds_env_var_with_file_contents( |
| 1668 |
"google_default_creds_external_account_credentials_multi_pattern_iam", |
| 1669 |
test_external_account_credentials_multi_pattern_iam_str); |
| 1670 |
grpc_override_well_known_credentials_path_getter( |
| 1671 |
null_well_known_creds_path_getter); |
| 1672 |
creds = reinterpret_cast<grpc_composite_channel_credentials*>( |
| 1673 |
grpc_google_default_credentials_create(nullptr)); |
| 1674 |
auto* default_creds = |
| 1675 |
reinterpret_cast<const grpc_google_default_channel_credentials*>( |
| 1676 |
creds->inner_creds()); |
| 1677 |
GPR_ASSERT(default_creds->ssl_creds() != nullptr); |
| 1678 |
auto* external = |
| 1679 |
reinterpret_cast<const ExternalAccountCredentials*>(creds->call_creds()); |
| 1680 |
GPR_ASSERT(external != nullptr); |
| 1681 |
creds->Unref(); |
| 1682 |
SetEnv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ |
| 1683 |
grpc_override_well_known_credentials_path_getter(nullptr); |
| 1684 |
} |
| 1685 |
|
| 1686 |
TEST(CredentialsTest, TestGoogleDefaultCredsExternalAccountCredentialsPscSts) { |
| 1687 |
ExecCtx exec_ctx; |
| 1688 |
grpc_composite_channel_credentials* creds; |
| 1689 |
grpc_flush_cached_google_default_credentials(); |
| 1690 |
set_google_default_creds_env_var_with_file_contents( |
| 1691 |
"google_default_creds_external_account_credentials_psc_sts", |
| 1692 |
test_external_account_credentials_psc_sts_str); |
| 1693 |
grpc_override_well_known_credentials_path_getter( |
| 1694 |
null_well_known_creds_path_getter); |
| 1695 |
creds = reinterpret_cast<grpc_composite_channel_credentials*>( |
| 1696 |
grpc_google_default_credentials_create(nullptr)); |
| 1697 |
auto* default_creds = |
| 1698 |
reinterpret_cast<const grpc_google_default_channel_credentials*>( |
| 1699 |
creds->inner_creds()); |
| 1700 |
GPR_ASSERT(default_creds->ssl_creds() != nullptr); |
| 1701 |
auto* external = |
| 1702 |
reinterpret_cast<const ExternalAccountCredentials*>(creds->call_creds()); |
| 1703 |
GPR_ASSERT(external != nullptr); |
| 1704 |
creds->Unref(); |
| 1705 |
SetEnv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ |
| 1706 |
grpc_override_well_known_credentials_path_getter(nullptr); |
| 1707 |
} |
| 1708 |
|
| 1709 |
TEST(CredentialsTest, TestGoogleDefaultCredsExternalAccountCredentialsPscIam) { |
| 1710 |
ExecCtx exec_ctx; |
| 1711 |
grpc_composite_channel_credentials* creds; |
| 1712 |
grpc_flush_cached_google_default_credentials(); |
| 1713 |
set_google_default_creds_env_var_with_file_contents( |
| 1714 |
"google_default_creds_external_account_credentials_psc_iam", |
| 1715 |
test_external_account_credentials_psc_iam_str); |
| 1716 |
grpc_override_well_known_credentials_path_getter( |
| 1717 |
null_well_known_creds_path_getter); |
| 1718 |
creds = reinterpret_cast<grpc_composite_channel_credentials*>( |
| 1719 |
grpc_google_default_credentials_create(nullptr)); |
| 1720 |
auto* default_creds = |
| 1721 |
reinterpret_cast<const grpc_google_default_channel_credentials*>( |
| 1722 |
creds->inner_creds()); |
| 1723 |
GPR_ASSERT(default_creds->ssl_creds() != nullptr); |
| 1724 |
auto* external = |
| 1725 |
reinterpret_cast<const ExternalAccountCredentials*>(creds->call_creds()); |
| 1726 |
GPR_ASSERT(external != nullptr); |
| 1727 |
creds->Unref(); |
| 1728 |
SetEnv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ |
| 1729 |
grpc_override_well_known_credentials_path_getter(nullptr); |
| 1730 |
} |
| 1731 |
|
| 1732 |
int default_creds_metadata_server_detection_httpcli_get_success_override( |
| 1733 |
const grpc_http_request* /*request*/, const char* host, const char* path, |
| 1734 |
Timestamp /*deadline*/, grpc_closure* on_done, |
| 1735 |
grpc_http_response* response) { |
| 1736 |
*response = http_response(200, ""); |
| 1737 |
grpc_http_header* headers = |
| 1738 |
static_cast<grpc_http_header*>(gpr_malloc(sizeof(*headers) * 1)); |
| 1739 |
headers[0].key = gpr_strdup("Metadata-Flavor"); |
| 1740 |
headers[0].value = gpr_strdup("Google"); |
| 1741 |
response->hdr_count = 1; |
| 1742 |
response->hdrs = headers; |
| 1743 |
GPR_ASSERT(strcmp(path, "/") == 0); |
| 1744 |
GPR_ASSERT(strcmp(host, "metadata.google.internal.") == 0); |
| 1745 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 1746 |
return 1; |
| 1747 |
} |
| 1748 |
|
| 1749 |
TEST(CredentialsTest, TestGoogleDefaultCredsGce) { |
| 1750 |
ExecCtx exec_ctx; |
| 1751 |
auto state = RequestMetadataState::NewInstance( |
| 1752 |
absl::OkStatus(), |
| 1753 |
"authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"); |
| 1754 |
grpc_flush_cached_google_default_credentials(); |
| 1755 |
SetEnv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ |
| 1756 |
grpc_override_well_known_credentials_path_getter( |
| 1757 |
null_well_known_creds_path_getter); |
| 1758 |
set_gce_tenancy_checker_for_testing(test_gce_tenancy_checker); |
| 1759 |
g_test_gce_tenancy_checker_called = false; |
| 1760 |
g_test_is_on_gce = true; |
| 1761 |
|
| 1762 |
/* Simulate a successful detection of GCE. */ |
| 1763 |
grpc_composite_channel_credentials* creds = |
| 1764 |
reinterpret_cast<grpc_composite_channel_credentials*>( |
| 1765 |
grpc_google_default_credentials_create(nullptr)); |
| 1766 |
|
| 1767 |
/* Verify that the default creds actually embeds a GCE creds. */ |
| 1768 |
GPR_ASSERT(creds != nullptr); |
| 1769 |
GPR_ASSERT(creds->call_creds() != nullptr); |
| 1770 |
HttpRequest::SetOverride(compute_engine_httpcli_get_success_override, |
| 1771 |
httpcli_post_should_not_be_called, |
| 1772 |
httpcli_put_should_not_be_called); |
| 1773 |
state->RunRequestMetadataTest(creds->mutable_call_creds(), kTestUrlScheme, |
| 1774 |
kTestAuthority, kTestPath); |
| 1775 |
ExecCtx::Get()->Flush(); |
| 1776 |
|
| 1777 |
GPR_ASSERT(g_test_gce_tenancy_checker_called == true); |
| 1778 |
|
| 1779 |
/* Cleanup. */ |
| 1780 |
creds->Unref(); |
| 1781 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 1782 |
grpc_override_well_known_credentials_path_getter(nullptr); |
| 1783 |
} |
| 1784 |
|
| 1785 |
TEST(CredentialsTest, TestGoogleDefaultCredsNonGce) { |
| 1786 |
ExecCtx exec_ctx; |
| 1787 |
auto state = RequestMetadataState::NewInstance( |
| 1788 |
absl::OkStatus(), |
| 1789 |
"authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"); |
| 1790 |
grpc_flush_cached_google_default_credentials(); |
| 1791 |
SetEnv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ |
| 1792 |
grpc_override_well_known_credentials_path_getter( |
| 1793 |
null_well_known_creds_path_getter); |
| 1794 |
set_gce_tenancy_checker_for_testing(test_gce_tenancy_checker); |
| 1795 |
g_test_gce_tenancy_checker_called = false; |
| 1796 |
g_test_is_on_gce = false; |
| 1797 |
/* Simulate a successful detection of metadata server. */ |
| 1798 |
HttpRequest::SetOverride( |
| 1799 |
default_creds_metadata_server_detection_httpcli_get_success_override, |
| 1800 |
httpcli_post_should_not_be_called, httpcli_put_should_not_be_called); |
| 1801 |
grpc_composite_channel_credentials* creds = |
| 1802 |
reinterpret_cast<grpc_composite_channel_credentials*>( |
| 1803 |
grpc_google_default_credentials_create(nullptr)); |
| 1804 |
/* Verify that the default creds actually embeds a GCE creds. */ |
| 1805 |
GPR_ASSERT(creds != nullptr); |
| 1806 |
GPR_ASSERT(creds->call_creds() != nullptr); |
| 1807 |
HttpRequest::SetOverride(compute_engine_httpcli_get_success_override, |
| 1808 |
httpcli_post_should_not_be_called, |
| 1809 |
httpcli_put_should_not_be_called); |
| 1810 |
state->RunRequestMetadataTest(creds->mutable_call_creds(), kTestUrlScheme, |
| 1811 |
kTestAuthority, kTestPath); |
| 1812 |
ExecCtx::Get()->Flush(); |
| 1813 |
GPR_ASSERT(g_test_gce_tenancy_checker_called == true); |
| 1814 |
/* Cleanup. */ |
| 1815 |
creds->Unref(); |
| 1816 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 1817 |
grpc_override_well_known_credentials_path_getter(nullptr); |
| 1818 |
} |
| 1819 |
|
| 1820 |
int default_creds_gce_detection_httpcli_get_failure_override( |
| 1821 |
const grpc_http_request* /*request*/, const char* host, const char* path, |
| 1822 |
Timestamp /*deadline*/, grpc_closure* on_done, |
| 1823 |
grpc_http_response* response) { |
| 1824 |
/* No magic header. */ |
| 1825 |
GPR_ASSERT(strcmp(path, "/") == 0); |
| 1826 |
GPR_ASSERT(strcmp(host, "metadata.google.internal.") == 0); |
| 1827 |
*response = http_response(200, ""); |
| 1828 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 1829 |
return 1; |
| 1830 |
} |
| 1831 |
|
| 1832 |
TEST(CredentialsTest, TestNoGoogleDefaultCreds) { |
| 1833 |
grpc_flush_cached_google_default_credentials(); |
| 1834 |
SetEnv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */ |
| 1835 |
grpc_override_well_known_credentials_path_getter( |
| 1836 |
null_well_known_creds_path_getter); |
| 1837 |
set_gce_tenancy_checker_for_testing(test_gce_tenancy_checker); |
| 1838 |
g_test_gce_tenancy_checker_called = false; |
| 1839 |
g_test_is_on_gce = false; |
| 1840 |
HttpRequest::SetOverride( |
| 1841 |
default_creds_gce_detection_httpcli_get_failure_override, |
| 1842 |
httpcli_post_should_not_be_called, httpcli_put_should_not_be_called); |
| 1843 |
/* Simulate a successful detection of GCE. */ |
| 1844 |
GPR_ASSERT(grpc_google_default_credentials_create(nullptr) == nullptr); |
| 1845 |
/* Try a second one. GCE detection should occur again. */ |
| 1846 |
g_test_gce_tenancy_checker_called = false; |
| 1847 |
GPR_ASSERT(grpc_google_default_credentials_create(nullptr) == nullptr); |
| 1848 |
GPR_ASSERT(g_test_gce_tenancy_checker_called == true); |
| 1849 |
/* Cleanup. */ |
| 1850 |
grpc_override_well_known_credentials_path_getter(nullptr); |
| 1851 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 1852 |
} |
| 1853 |
|
| 1854 |
TEST(CredentialsTest, TestGoogleDefaultCredsCallCredsSpecified) { |
| 1855 |
auto state = RequestMetadataState::NewInstance( |
| 1856 |
absl::OkStatus(), |
| 1857 |
"authorization: Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"); |
| 1858 |
ExecCtx exec_ctx; |
| 1859 |
grpc_flush_cached_google_default_credentials(); |
| 1860 |
grpc_call_credentials* call_creds = |
| 1861 |
grpc_google_compute_engine_credentials_create(nullptr); |
| 1862 |
set_gce_tenancy_checker_for_testing(test_gce_tenancy_checker); |
| 1863 |
g_test_gce_tenancy_checker_called = false; |
| 1864 |
g_test_is_on_gce = true; |
| 1865 |
HttpRequest::SetOverride( |
| 1866 |
default_creds_metadata_server_detection_httpcli_get_success_override, |
| 1867 |
httpcli_post_should_not_be_called, httpcli_put_should_not_be_called); |
| 1868 |
grpc_composite_channel_credentials* channel_creds = |
| 1869 |
reinterpret_cast<grpc_composite_channel_credentials*>( |
| 1870 |
grpc_google_default_credentials_create(call_creds)); |
| 1871 |
GPR_ASSERT(g_test_gce_tenancy_checker_called == false); |
| 1872 |
GPR_ASSERT(channel_creds != nullptr); |
| 1873 |
GPR_ASSERT(channel_creds->call_creds() != nullptr); |
| 1874 |
HttpRequest::SetOverride(compute_engine_httpcli_get_success_override, |
| 1875 |
httpcli_post_should_not_be_called, |
| 1876 |
httpcli_put_should_not_be_called); |
| 1877 |
state->RunRequestMetadataTest(channel_creds->mutable_call_creds(), |
| 1878 |
kTestUrlScheme, kTestAuthority, kTestPath); |
| 1879 |
ExecCtx::Get()->Flush(); |
| 1880 |
channel_creds->Unref(); |
| 1881 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 1882 |
} |
| 1883 |
|
| 1884 |
struct fake_call_creds : public grpc_call_credentials { |
| 1885 |
public: |
| 1886 |
ArenaPromise<absl::StatusOr<ClientMetadataHandle>> GetRequestMetadata( |
| 1887 |
ClientMetadataHandle initial_metadata, |
| 1888 |
const grpc_call_credentials::GetRequestMetadataArgs*) override { |
| 1889 |
initial_metadata->Append("foo", Slice::FromStaticString("oof"), |
| 1890 |
[](absl::string_view, const Slice&) { abort(); }); |
| 1891 |
return Immediate(std::move(initial_metadata)); |
| 1892 |
} |
| 1893 |
|
| 1894 |
UniqueTypeName type() const override { |
| 1895 |
static UniqueTypeName::Factory kFactory("fake"); |
| 1896 |
return kFactory.Create(); |
| 1897 |
} |
| 1898 |
|
| 1899 |
private: |
| 1900 |
int cmp_impl(const grpc_call_credentials* other) const override { |
| 1901 |
// TODO(yashykt): Check if we can do something better here |
| 1902 |
return QsortCompare(static_cast<const grpc_call_credentials*>(this), other); |
| 1903 |
} |
| 1904 |
}; |
| 1905 |
|
| 1906 |
TEST(CredentialsTest, TestGoogleDefaultCredsNotDefault) { |
| 1907 |
auto state = RequestMetadataState::NewInstance(absl::OkStatus(), "foo: oof"); |
| 1908 |
ExecCtx exec_ctx; |
| 1909 |
grpc_flush_cached_google_default_credentials(); |
| 1910 |
RefCountedPtr<grpc_call_credentials> call_creds = |
| 1911 |
MakeRefCounted<fake_call_creds>(); |
| 1912 |
set_gce_tenancy_checker_for_testing(test_gce_tenancy_checker); |
| 1913 |
g_test_gce_tenancy_checker_called = false; |
| 1914 |
g_test_is_on_gce = true; |
| 1915 |
HttpRequest::SetOverride( |
| 1916 |
default_creds_metadata_server_detection_httpcli_get_success_override, |
| 1917 |
httpcli_post_should_not_be_called, httpcli_put_should_not_be_called); |
| 1918 |
grpc_composite_channel_credentials* channel_creds = |
| 1919 |
reinterpret_cast<grpc_composite_channel_credentials*>( |
| 1920 |
grpc_google_default_credentials_create(call_creds.release())); |
| 1921 |
GPR_ASSERT(g_test_gce_tenancy_checker_called == false); |
| 1922 |
GPR_ASSERT(channel_creds != nullptr); |
| 1923 |
GPR_ASSERT(channel_creds->call_creds() != nullptr); |
| 1924 |
state->RunRequestMetadataTest(channel_creds->mutable_call_creds(), |
| 1925 |
kTestUrlScheme, kTestAuthority, kTestPath); |
| 1926 |
ExecCtx::Get()->Flush(); |
| 1927 |
channel_creds->Unref(); |
| 1928 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 1929 |
} |
| 1930 |
|
| 1931 |
typedef enum { |
| 1932 |
PLUGIN_INITIAL_STATE, |
| 1933 |
PLUGIN_GET_METADATA_CALLED_STATE, |
| 1934 |
PLUGIN_DESTROY_CALLED_STATE |
| 1935 |
} plugin_state; |
| 1936 |
|
| 1937 |
const std::map<std::string, std::string> plugin_md = {{"foo", "bar"}, |
| 1938 |
{"hi", "there"}}; |
| 1939 |
|
| 1940 |
int plugin_get_metadata_success( |
| 1941 |
void* state, grpc_auth_metadata_context context, |
| 1942 |
grpc_credentials_plugin_metadata_cb /*cb*/, void* /*user_data*/, |
| 1943 |
grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX], |
| 1944 |
size_t* num_creds_md, grpc_status_code* /*status*/, |
| 1945 |
const char** /*error_details*/) { |
| 1946 |
GPR_ASSERT(strcmp(context.service_url, test_service_url) == 0); |
| 1947 |
GPR_ASSERT(strcmp(context.method_name, test_method) == 0); |
| 1948 |
GPR_ASSERT(context.channel_auth_context == nullptr); |
| 1949 |
GPR_ASSERT(context.reserved == nullptr); |
| 1950 |
GPR_ASSERT(plugin_md.size() < GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX); |
| 1951 |
plugin_state* s = static_cast<plugin_state*>(state); |
| 1952 |
*s = PLUGIN_GET_METADATA_CALLED_STATE; |
| 1953 |
size_t i = 0; |
| 1954 |
for (auto const& md : plugin_md) { |
| 1955 |
memset(&creds_md[i], 0, sizeof(grpc_metadata)); |
| 1956 |
creds_md[i].key = grpc_slice_from_copied_string(md.first.c_str()); |
| 1957 |
creds_md[i].value = grpc_slice_from_copied_string(md.second.c_str()); |
| 1958 |
i += 1; |
| 1959 |
} |
| 1960 |
*num_creds_md = plugin_md.size(); |
| 1961 |
return true; // Synchronous return. |
| 1962 |
} |
| 1963 |
|
| 1964 |
const char* plugin_error_details = "Could not get metadata for plugin."; |
| 1965 |
|
| 1966 |
int plugin_get_metadata_failure( |
| 1967 |
void* state, grpc_auth_metadata_context context, |
| 1968 |
grpc_credentials_plugin_metadata_cb /*cb*/, void* /*user_data*/, |
| 1969 |
grpc_metadata /*creds_md*/[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX], |
| 1970 |
size_t* /*num_creds_md*/, grpc_status_code* status, |
| 1971 |
const char** error_details) { |
| 1972 |
GPR_ASSERT(strcmp(context.service_url, test_service_url) == 0); |
| 1973 |
GPR_ASSERT(strcmp(context.method_name, test_method) == 0); |
| 1974 |
GPR_ASSERT(context.channel_auth_context == nullptr); |
| 1975 |
GPR_ASSERT(context.reserved == nullptr); |
| 1976 |
plugin_state* s = static_cast<plugin_state*>(state); |
| 1977 |
*s = PLUGIN_GET_METADATA_CALLED_STATE; |
| 1978 |
*status = GRPC_STATUS_UNAUTHENTICATED; |
| 1979 |
*error_details = gpr_strdup(plugin_error_details); |
| 1980 |
return true; // Synchronous return. |
| 1981 |
} |
| 1982 |
|
| 1983 |
void plugin_destroy(void* state) { |
| 1984 |
plugin_state* s = static_cast<plugin_state*>(state); |
| 1985 |
*s = PLUGIN_DESTROY_CALLED_STATE; |
| 1986 |
} |
| 1987 |
|
| 1988 |
char* plugin_debug_string(void* state) { |
| 1989 |
plugin_state* s = static_cast<plugin_state*>(state); |
| 1990 |
char* ret = nullptr; |
| 1991 |
switch (*s) { |
| 1992 |
case PLUGIN_INITIAL_STATE: |
| 1993 |
gpr_asprintf(&ret, "TestPluginCredentials{state:INITIAL}"); |
| 1994 |
break; |
| 1995 |
case PLUGIN_GET_METADATA_CALLED_STATE: |
| 1996 |
gpr_asprintf(&ret, "TestPluginCredentials{state:GET_METADATA_CALLED}"); |
| 1997 |
break; |
| 1998 |
case PLUGIN_DESTROY_CALLED_STATE: |
| 1999 |
gpr_asprintf(&ret, "TestPluginCredentials{state:DESTROY}"); |
| 2000 |
break; |
| 2001 |
default: |
| 2002 |
gpr_asprintf(&ret, "TestPluginCredentials{state:UNKNOWN}"); |
| 2003 |
break; |
| 2004 |
} |
| 2005 |
return ret; |
| 2006 |
} |
| 2007 |
|
| 2008 |
TEST(CredentialsTest, TestMetadataPluginSuccess) { |
| 2009 |
const char expected_creds_debug_string[] = |
| 2010 |
"TestPluginCredentials{state:GET_METADATA_CALLED}"; |
| 2011 |
plugin_state state = PLUGIN_INITIAL_STATE; |
| 2012 |
grpc_metadata_credentials_plugin plugin; |
| 2013 |
ExecCtx exec_ctx; |
| 2014 |
auto md_state = RequestMetadataState::NewInstance(absl::OkStatus(), |
| 2015 |
"foo: bar, hi: there"); |
| 2016 |
|
| 2017 |
plugin.state = &state; |
| 2018 |
plugin.get_metadata = plugin_get_metadata_success; |
| 2019 |
plugin.destroy = plugin_destroy; |
| 2020 |
plugin.debug_string = plugin_debug_string; |
| 2021 |
|
| 2022 |
grpc_call_credentials* creds = grpc_metadata_credentials_create_from_plugin( |
| 2023 |
plugin, GRPC_PRIVACY_AND_INTEGRITY, nullptr); |
| 2024 |
/* Check security level. */ |
| 2025 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 2026 |
GPR_ASSERT(state == PLUGIN_INITIAL_STATE); |
| 2027 |
md_state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 2028 |
kTestPath); |
| 2029 |
GPR_ASSERT(state == PLUGIN_GET_METADATA_CALLED_STATE); |
| 2030 |
GPR_ASSERT( |
| 2031 |
strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0); |
| 2032 |
creds->Unref(); |
| 2033 |
|
| 2034 |
GPR_ASSERT(state == PLUGIN_DESTROY_CALLED_STATE); |
| 2035 |
} |
| 2036 |
|
| 2037 |
TEST(CredentialsTest, TestMetadataPluginFailure) { |
| 2038 |
const char expected_creds_debug_string[] = |
| 2039 |
"TestPluginCredentials{state:GET_METADATA_CALLED}"; |
| 2040 |
|
| 2041 |
plugin_state state = PLUGIN_INITIAL_STATE; |
| 2042 |
grpc_metadata_credentials_plugin plugin; |
| 2043 |
ExecCtx exec_ctx; |
| 2044 |
auto md_state = RequestMetadataState::NewInstance( |
| 2045 |
GRPC_ERROR_CREATE( |
| 2046 |
absl::StrCat("Getting metadata from plugin failed with error: ", |
| 2047 |
plugin_error_details)), |
| 2048 |
{}); |
| 2049 |
|
| 2050 |
plugin.state = &state; |
| 2051 |
plugin.get_metadata = plugin_get_metadata_failure; |
| 2052 |
plugin.destroy = plugin_destroy; |
| 2053 |
plugin.debug_string = plugin_debug_string; |
| 2054 |
|
| 2055 |
grpc_call_credentials* creds = grpc_metadata_credentials_create_from_plugin( |
| 2056 |
plugin, GRPC_PRIVACY_AND_INTEGRITY, nullptr); |
| 2057 |
GPR_ASSERT(state == PLUGIN_INITIAL_STATE); |
| 2058 |
md_state->RunRequestMetadataTest(creds, kTestUrlScheme, kTestAuthority, |
| 2059 |
kTestPath); |
| 2060 |
GPR_ASSERT(state == PLUGIN_GET_METADATA_CALLED_STATE); |
| 2061 |
GPR_ASSERT( |
| 2062 |
strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0); |
| 2063 |
creds->Unref(); |
| 2064 |
|
| 2065 |
GPR_ASSERT(state == PLUGIN_DESTROY_CALLED_STATE); |
| 2066 |
} |
| 2067 |
|
| 2068 |
TEST(CredentialsTest, TestGetWellKnownGoogleCredentialsFilePath) { |
| 2069 |
auto home = GetEnv("HOME"); |
| 2070 |
bool restore_home_env = false; |
| 2071 |
#if defined(GRPC_BAZEL_BUILD) && \ |
| 2072 |
(defined(GPR_POSIX_ENV) || defined(GPR_LINUX_ENV)) |
| 2073 |
// when running under bazel locally, the HOME variable is not set |
| 2074 |
// so we set it to some fake value |
| 2075 |
restore_home_env = true; |
| 2076 |
SetEnv("HOME", "/fake/home/for/bazel"); |
| 2077 |
#endif /* defined(GRPC_BAZEL_BUILD) && (defined(GPR_POSIX_ENV) || \ |
| 2078 |
defined(GPR_LINUX_ENV)) */ |
| 2079 |
std::string path = grpc_get_well_known_google_credentials_file_path(); |
| 2080 |
GPR_ASSERT(!path.empty()); |
| 2081 |
#if defined(GPR_POSIX_ENV) || defined(GPR_LINUX_ENV) |
| 2082 |
restore_home_env = true; |
| 2083 |
UnsetEnv("HOME"); |
| 2084 |
path = grpc_get_well_known_google_credentials_file_path(); |
| 2085 |
GPR_ASSERT(path.empty()); |
| 2086 |
#endif /* GPR_POSIX_ENV || GPR_LINUX_ENV */ |
| 2087 |
if (restore_home_env) { |
| 2088 |
SetOrUnsetEnv("HOME", home); |
| 2089 |
} |
| 2090 |
} |
| 2091 |
|
| 2092 |
TEST(CredentialsTest, TestChannelCredsDuplicateWithoutCallCreds) { |
| 2093 |
const char expected_creds_debug_string[] = |
| 2094 |
"AccessTokenCredentials{Token:present}"; |
| 2095 |
ExecCtx exec_ctx; |
| 2096 |
|
| 2097 |
grpc_channel_credentials* channel_creds = |
| 2098 |
grpc_fake_transport_security_credentials_create(); |
| 2099 |
|
| 2100 |
RefCountedPtr<grpc_channel_credentials> dup = |
| 2101 |
channel_creds->duplicate_without_call_credentials(); |
| 2102 |
GPR_ASSERT(dup == channel_creds); |
| 2103 |
dup.reset(); |
| 2104 |
|
| 2105 |
grpc_call_credentials* call_creds = |
| 2106 |
grpc_access_token_credentials_create("blah", nullptr); |
| 2107 |
grpc_channel_credentials* composite_creds = |
| 2108 |
grpc_composite_channel_credentials_create(channel_creds, call_creds, |
| 2109 |
nullptr); |
| 2110 |
GPR_ASSERT(strcmp(call_creds->debug_string().c_str(), |
| 2111 |
expected_creds_debug_string) == 0); |
| 2112 |
|
| 2113 |
call_creds->Unref(); |
| 2114 |
dup = composite_creds->duplicate_without_call_credentials(); |
| 2115 |
GPR_ASSERT(dup == channel_creds); |
| 2116 |
dup.reset(); |
| 2117 |
|
| 2118 |
channel_creds->Unref(); |
| 2119 |
composite_creds->Unref(); |
| 2120 |
} |
| 2121 |
|
| 2122 |
typedef struct { |
| 2123 |
const char* url_scheme; |
| 2124 |
const char* call_host; |
| 2125 |
const char* call_method; |
| 2126 |
const char* desired_service_url; |
| 2127 |
const char* desired_method_name; |
| 2128 |
} auth_metadata_context_test_case; |
| 2129 |
|
| 2130 |
void auth_metadata_context_build(const char* url_scheme, |
| 2131 |
const grpc_slice& call_host, |
| 2132 |
const grpc_slice& call_method, |
| 2133 |
grpc_auth_context* auth_context, |
| 2134 |
grpc_auth_metadata_context* auth_md_context) { |
| 2135 |
char* service = grpc_slice_to_c_string(call_method); |
| 2136 |
char* last_slash = strrchr(service, '/'); |
| 2137 |
char* method_name = nullptr; |
| 2138 |
char* service_url = nullptr; |
| 2139 |
grpc_auth_metadata_context_reset(auth_md_context); |
| 2140 |
if (last_slash == nullptr) { |
| 2141 |
gpr_log(GPR_ERROR, "No '/' found in fully qualified method name"); |
| 2142 |
service[0] = '\0'; |
| 2143 |
method_name = gpr_strdup(""); |
| 2144 |
} else if (last_slash == service) { |
| 2145 |
method_name = gpr_strdup(""); |
| 2146 |
} else { |
| 2147 |
*last_slash = '\0'; |
| 2148 |
method_name = gpr_strdup(last_slash + 1); |
| 2149 |
} |
| 2150 |
char* host_and_port = grpc_slice_to_c_string(call_host); |
| 2151 |
if (url_scheme != nullptr && strcmp(url_scheme, GRPC_SSL_URL_SCHEME) == 0) { |
| 2152 |
/* Remove the port if it is 443. */ |
| 2153 |
char* port_delimiter = strrchr(host_and_port, ':'); |
| 2154 |
if (port_delimiter != nullptr && strcmp(port_delimiter + 1, "443") == 0) { |
| 2155 |
*port_delimiter = '\0'; |
| 2156 |
} |
| 2157 |
} |
| 2158 |
gpr_asprintf(&service_url, "%s://%s%s", |
| 2159 |
url_scheme == nullptr ? "" : url_scheme, host_and_port, service); |
| 2160 |
auth_md_context->service_url = service_url; |
| 2161 |
auth_md_context->method_name = method_name; |
| 2162 |
auth_md_context->channel_auth_context = |
| 2163 |
auth_context == nullptr |
| 2164 |
? nullptr |
| 2165 |
: auth_context->Ref(DEBUG_LOCATION, "grpc_auth_metadata_context") |
| 2166 |
.release(); |
| 2167 |
gpr_free(service); |
| 2168 |
gpr_free(host_and_port); |
| 2169 |
} |
| 2170 |
|
| 2171 |
TEST(CredentialsTest, TestAuthMetadataContext) { |
| 2172 |
auth_metadata_context_test_case test_cases[] = { |
| 2173 |
// No service nor method. |
| 2174 |
{"https", "www.foo.com", "", "https://www.foo.com", ""}, |
| 2175 |
// No method. |
| 2176 |
{"https", "www.foo.com", "/Service", "https://www.foo.com/Service", ""}, |
| 2177 |
// Empty service and method. |
| 2178 |
{"https", "www.foo.com", "//", "https://www.foo.com/", ""}, |
| 2179 |
// Empty method. |
| 2180 |
{"https", "www.foo.com", "/Service/", "https://www.foo.com/Service", ""}, |
| 2181 |
// Malformed url. |
| 2182 |
{"https", "www.foo.com:", "/Service/", "https://www.foo.com:/Service", |
| 2183 |
""}, |
| 2184 |
// https, default explicit port. |
| 2185 |
{"https", "www.foo.com:443", "/Service/FooMethod", |
| 2186 |
"https://www.foo.com/Service", "FooMethod"}, |
| 2187 |
// https, default implicit port. |
| 2188 |
{"https", "www.foo.com", "/Service/FooMethod", |
| 2189 |
"https://www.foo.com/Service", "FooMethod"}, |
| 2190 |
// https with ipv6 literal, default explicit port. |
| 2191 |
{"https", "[1080:0:0:0:8:800:200C:417A]:443", "/Service/FooMethod", |
| 2192 |
"https://[1080:0:0:0:8:800:200C:417A]/Service", "FooMethod"}, |
| 2193 |
// https with ipv6 literal, default implicit port. |
| 2194 |
{"https", "[1080:0:0:0:8:800:200C:443]", "/Service/FooMethod", |
| 2195 |
"https://[1080:0:0:0:8:800:200C:443]/Service", "FooMethod"}, |
| 2196 |
// https, custom port. |
| 2197 |
{"https", "www.foo.com:8888", "/Service/FooMethod", |
| 2198 |
"https://www.foo.com:8888/Service", "FooMethod"}, |
| 2199 |
// https with ipv6 literal, custom port. |
| 2200 |
{"https", "[1080:0:0:0:8:800:200C:417A]:8888", "/Service/FooMethod", |
| 2201 |
"https://[1080:0:0:0:8:800:200C:417A]:8888/Service", "FooMethod"}, |
| 2202 |
// custom url scheme, https default port. |
| 2203 |
{"blah", "www.foo.com:443", "/Service/FooMethod", |
| 2204 |
"blah://www.foo.com:443/Service", "FooMethod"}}; |
| 2205 |
for (uint32_t i = 0; i < GPR_ARRAY_SIZE(test_cases); i++) { |
| 2206 |
const char* url_scheme = test_cases[i].url_scheme; |
| 2207 |
grpc_slice call_host = |
| 2208 |
grpc_slice_from_copied_string(test_cases[i].call_host); |
| 2209 |
grpc_slice call_method = |
| 2210 |
grpc_slice_from_copied_string(test_cases[i].call_method); |
| 2211 |
grpc_auth_metadata_context auth_md_context; |
| 2212 |
memset(&auth_md_context, 0, sizeof(auth_md_context)); |
| 2213 |
auth_metadata_context_build(url_scheme, call_host, call_method, nullptr, |
| 2214 |
&auth_md_context); |
| 2215 |
if (strcmp(auth_md_context.service_url, |
| 2216 |
test_cases[i].desired_service_url) != 0) { |
| 2217 |
gpr_log(GPR_ERROR, "Invalid service url, want: %s, got %s.", |
| 2218 |
test_cases[i].desired_service_url, auth_md_context.service_url); |
| 2219 |
GPR_ASSERT(false); |
| 2220 |
} |
| 2221 |
if (strcmp(auth_md_context.method_name, |
| 2222 |
test_cases[i].desired_method_name) != 0) { |
| 2223 |
gpr_log(GPR_ERROR, "Invalid method name, want: %s, got %s.", |
| 2224 |
test_cases[i].desired_method_name, auth_md_context.method_name); |
| 2225 |
GPR_ASSERT(false); |
| 2226 |
} |
| 2227 |
GPR_ASSERT(auth_md_context.channel_auth_context == nullptr); |
| 2228 |
grpc_slice_unref(call_host); |
| 2229 |
grpc_slice_unref(call_method); |
| 2230 |
grpc_auth_metadata_context_reset(&auth_md_context); |
| 2231 |
} |
| 2232 |
} |
| 2233 |
|
| 2234 |
void validate_external_account_creds_token_exchage_request( |
| 2235 |
const grpc_http_request* request, const char* host, const char* path, |
| 2236 |
const char* body, size_t body_size, bool /*expect_actor_token*/) { |
| 2237 |
// Check that the body is constructed properly. |
| 2238 |
GPR_ASSERT(body != nullptr); |
| 2239 |
GPR_ASSERT(body_size != 0); |
| 2240 |
std::string get_url_equivalent = |
| 2241 |
absl::StrFormat("%s?%s", "https://foo.com:5555/token", body); |
| 2242 |
absl::StatusOr<URI> uri = URI::Parse(get_url_equivalent); |
| 2243 |
if (!uri.ok()) { |
| 2244 |
gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str()); |
| 2245 |
GPR_ASSERT(uri.ok()); |
| 2246 |
} |
| 2247 |
assert_query_parameters(*uri, "audience", "audience"); |
| 2248 |
assert_query_parameters(*uri, "grant_type", |
| 2249 |
"urn:ietf:params:oauth:grant-type:token-exchange"); |
| 2250 |
assert_query_parameters(*uri, "requested_token_type", |
| 2251 |
"urn:ietf:params:oauth:token-type:access_token"); |
| 2252 |
assert_query_parameters(*uri, "subject_token", "test_subject_token"); |
| 2253 |
assert_query_parameters(*uri, "subject_token_type", "subject_token_type"); |
| 2254 |
assert_query_parameters(*uri, "scope", |
| 2255 |
"https://www.googleapis.com/auth/cloud-platform"); |
| 2256 |
|
| 2257 |
// Check the rest of the request. |
| 2258 |
GPR_ASSERT(strcmp(host, "foo.com:5555") == 0); |
| 2259 |
GPR_ASSERT(strcmp(path, "/token") == 0); |
| 2260 |
GPR_ASSERT(request->hdr_count == 2); |
| 2261 |
GPR_ASSERT(strcmp(request->hdrs[0].key, "Content-Type") == 0); |
| 2262 |
GPR_ASSERT( |
| 2263 |
strcmp(request->hdrs[0].value, "application/x-www-form-urlencoded") == 0); |
| 2264 |
GPR_ASSERT(strcmp(request->hdrs[1].key, "Authorization") == 0); |
| 2265 |
GPR_ASSERT(strcmp(request->hdrs[1].value, |
| 2266 |
"Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=") == 0); |
| 2267 |
} |
| 2268 |
|
| 2269 |
void validate_external_account_creds_token_exchage_request_with_url_encode( |
| 2270 |
const grpc_http_request* request, const char* host, const char* path, |
| 2271 |
const char* body, size_t body_size, bool /*expect_actor_token*/) { |
| 2272 |
// Check that the body is constructed properly. |
| 2273 |
GPR_ASSERT(body != nullptr); |
| 2274 |
GPR_ASSERT(body_size != 0); |
| 2275 |
GPR_ASSERT( |
| 2276 |
strcmp( |
| 2277 |
std::string(body, body_size).c_str(), |
| 2278 |
"audience=audience_!%40%23%24&grant_type=urn%3Aietf%3Aparams%3Aoauth%" |
| 2279 |
"3Agrant-type%3Atoken-exchange&requested_token_type=urn%3Aietf%" |
| 2280 |
"3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&subject_token_type=" |
| 2281 |
"subject_token_type_!%40%23%24&subject_token=test_subject_token&" |
| 2282 |
"scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform&" |
| 2283 |
"options=%7B%7D") == 0); |
| 2284 |
|
| 2285 |
// Check the rest of the request. |
| 2286 |
GPR_ASSERT(strcmp(host, "foo.com:5555") == 0); |
| 2287 |
GPR_ASSERT(strcmp(path, "/token_url_encode") == 0); |
| 2288 |
GPR_ASSERT(request->hdr_count == 2); |
| 2289 |
GPR_ASSERT(strcmp(request->hdrs[0].key, "Content-Type") == 0); |
| 2290 |
GPR_ASSERT( |
| 2291 |
strcmp(request->hdrs[0].value, "application/x-www-form-urlencoded") == 0); |
| 2292 |
GPR_ASSERT(strcmp(request->hdrs[1].key, "Authorization") == 0); |
| 2293 |
GPR_ASSERT(strcmp(request->hdrs[1].value, |
| 2294 |
"Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=") == 0); |
| 2295 |
} |
| 2296 |
|
| 2297 |
void validate_external_account_creds_service_account_impersonation_request( |
| 2298 |
const grpc_http_request* request, const char* host, const char* path, |
| 2299 |
const char* body, size_t body_size, bool /*expect_actor_token*/) { |
| 2300 |
// Check that the body is constructed properly. |
| 2301 |
GPR_ASSERT(body != nullptr); |
| 2302 |
GPR_ASSERT(body_size != 0); |
| 2303 |
GPR_ASSERT(strcmp(body, "scope=scope_1 scope_2") == 0); |
| 2304 |
// Check the rest of the request. |
| 2305 |
GPR_ASSERT(strcmp(host, "foo.com:5555") == 0); |
| 2306 |
GPR_ASSERT(strcmp(path, "/service_account_impersonation") == 0); |
| 2307 |
GPR_ASSERT(request->hdr_count == 2); |
| 2308 |
GPR_ASSERT(strcmp(request->hdrs[0].key, "Content-Type") == 0); |
| 2309 |
GPR_ASSERT( |
| 2310 |
strcmp(request->hdrs[0].value, "application/x-www-form-urlencoded") == 0); |
| 2311 |
GPR_ASSERT(strcmp(request->hdrs[1].key, "Authorization") == 0); |
| 2312 |
GPR_ASSERT(strcmp(request->hdrs[1].value, |
| 2313 |
"Bearer token_exchange_access_token") == 0); |
| 2314 |
} |
| 2315 |
|
| 2316 |
int external_account_creds_httpcli_post_success( |
| 2317 |
const grpc_http_request* request, const char* host, const char* path, |
| 2318 |
const char* body, size_t body_size, Timestamp /*deadline*/, |
| 2319 |
grpc_closure* on_done, grpc_http_response* response) { |
| 2320 |
if (strcmp(path, "/token") == 0) { |
| 2321 |
validate_external_account_creds_token_exchage_request( |
| 2322 |
request, host, path, body, body_size, true); |
| 2323 |
*response = http_response( |
| 2324 |
200, valid_external_account_creds_token_exchange_response); |
| 2325 |
} else if (strcmp(path, "/service_account_impersonation") == 0) { |
| 2326 |
validate_external_account_creds_service_account_impersonation_request( |
| 2327 |
request, host, path, body, body_size, true); |
| 2328 |
*response = http_response( |
| 2329 |
200, |
| 2330 |
valid_external_account_creds_service_account_impersonation_response); |
| 2331 |
} else if (strcmp(path, "/token_url_encode") == 0) { |
| 2332 |
validate_external_account_creds_token_exchage_request_with_url_encode( |
| 2333 |
request, host, path, body, body_size, true); |
| 2334 |
*response = http_response( |
| 2335 |
200, valid_external_account_creds_token_exchange_response); |
| 2336 |
} |
| 2337 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 2338 |
return 1; |
| 2339 |
} |
| 2340 |
|
| 2341 |
int external_account_creds_httpcli_post_failure_token_exchange_response_missing_access_token( |
| 2342 |
const grpc_http_request* /*request*/, const char* /*host*/, |
| 2343 |
const char* path, const char* /*body*/, size_t /*body_size*/, |
| 2344 |
Timestamp /*deadline*/, grpc_closure* on_done, |
| 2345 |
grpc_http_response* response) { |
| 2346 |
if (strcmp(path, "/token") == 0) { |
| 2347 |
*response = http_response(200, |
| 2348 |
"{\"not_access_token\":\"not_access_token\"," |
| 2349 |
"\"expires_in\":3599," |
| 2350 |
" \"token_type\":\"Bearer\"}"); |
| 2351 |
} else if (strcmp(path, "/service_account_impersonation") == 0) { |
| 2352 |
*response = http_response( |
| 2353 |
200, |
| 2354 |
valid_external_account_creds_service_account_impersonation_response); |
| 2355 |
} |
| 2356 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 2357 |
return 1; |
| 2358 |
} |
| 2359 |
|
| 2360 |
int url_external_account_creds_httpcli_get_success( |
| 2361 |
const grpc_http_request* /*request*/, const char* /*host*/, |
| 2362 |
const char* path, Timestamp /*deadline*/, grpc_closure* on_done, |
| 2363 |
grpc_http_response* response) { |
| 2364 |
if (strcmp(path, "/generate_subject_token_format_text") == 0) { |
| 2365 |
*response = http_response( |
| 2366 |
200, |
| 2367 |
valid_url_external_account_creds_retrieve_subject_token_response_format_text); |
| 2368 |
} else if (strcmp(path, "/path/to/url/creds?p1=v1&p2=v2") == 0) { |
| 2369 |
*response = http_response( |
| 2370 |
200, |
| 2371 |
valid_url_external_account_creds_retrieve_subject_token_response_format_text); |
| 2372 |
} else if (strcmp(path, "/generate_subject_token_format_json") == 0) { |
| 2373 |
*response = http_response( |
| 2374 |
200, |
| 2375 |
valid_url_external_account_creds_retrieve_subject_token_response_format_json); |
| 2376 |
} |
| 2377 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 2378 |
return 1; |
| 2379 |
} |
| 2380 |
|
| 2381 |
void validate_aws_external_account_creds_token_exchage_request( |
| 2382 |
const grpc_http_request* request, const char* host, const char* path, |
| 2383 |
const char* body, size_t body_size, bool /*expect_actor_token*/) { |
| 2384 |
// Check that the body is constructed properly. |
| 2385 |
GPR_ASSERT(body != nullptr); |
| 2386 |
GPR_ASSERT(body_size != 0); |
| 2387 |
// Check that the regional_cred_verification_url got constructed |
| 2388 |
// with the correct AWS Region ("test_regionz" or "test_region"). |
| 2389 |
GPR_ASSERT(strstr(body, "regional_cred_verification_url_test_region")); |
| 2390 |
std::string get_url_equivalent = |
| 2391 |
absl::StrFormat("%s?%s", "https://foo.com:5555/token", body); |
| 2392 |
absl::StatusOr<URI> uri = URI::Parse(get_url_equivalent); |
| 2393 |
GPR_ASSERT(uri.ok()); |
| 2394 |
assert_query_parameters(*uri, "audience", "audience"); |
| 2395 |
assert_query_parameters(*uri, "grant_type", |
| 2396 |
"urn:ietf:params:oauth:grant-type:token-exchange"); |
| 2397 |
assert_query_parameters(*uri, "requested_token_type", |
| 2398 |
"urn:ietf:params:oauth:token-type:access_token"); |
| 2399 |
assert_query_parameters(*uri, "subject_token_type", "subject_token_type"); |
| 2400 |
assert_query_parameters(*uri, "scope", |
| 2401 |
"https://www.googleapis.com/auth/cloud-platform"); |
| 2402 |
// Check the rest of the request. |
| 2403 |
GPR_ASSERT(strcmp(host, "foo.com:5555") == 0); |
| 2404 |
GPR_ASSERT(strcmp(path, "/token") == 0); |
| 2405 |
GPR_ASSERT(request->hdr_count == 2); |
| 2406 |
GPR_ASSERT(strcmp(request->hdrs[0].key, "Content-Type") == 0); |
| 2407 |
GPR_ASSERT( |
| 2408 |
strcmp(request->hdrs[0].value, "application/x-www-form-urlencoded") == 0); |
| 2409 |
GPR_ASSERT(strcmp(request->hdrs[1].key, "Authorization") == 0); |
| 2410 |
GPR_ASSERT(strcmp(request->hdrs[1].value, |
| 2411 |
"Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=") == 0); |
| 2412 |
} |
| 2413 |
|
| 2414 |
int aws_external_account_creds_httpcli_get_success( |
| 2415 |
const grpc_http_request* /*request*/, const char* /*host*/, |
| 2416 |
const char* path, Timestamp /*deadline*/, grpc_closure* on_done, |
| 2417 |
grpc_http_response* response) { |
| 2418 |
if (strcmp(path, "/region_url") == 0) { |
| 2419 |
*response = http_response(200, "test_regionz"); |
| 2420 |
} else if (strcmp(path, "/url") == 0) { |
| 2421 |
*response = http_response(200, "test_role_name"); |
| 2422 |
} else if (strcmp(path, "/url_no_role_name") == 0) { |
| 2423 |
*response = http_response(200, ""); |
| 2424 |
} else if (strcmp(path, "/url/test_role_name") == 0) { |
| 2425 |
*response = http_response( |
| 2426 |
200, valid_aws_external_account_creds_retrieve_signing_keys_response); |
| 2427 |
} |
| 2428 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 2429 |
return 1; |
| 2430 |
} |
| 2431 |
|
| 2432 |
int aws_imdsv2_external_account_creds_httpcli_get_success( |
| 2433 |
const grpc_http_request* request, const char* host, const char* path, |
| 2434 |
Timestamp deadline, grpc_closure* on_done, grpc_http_response* response) { |
| 2435 |
GPR_ASSERT(request->hdr_count == 1); |
| 2436 |
GPR_ASSERT(strcmp(request->hdrs[0].key, "x-aws-ec2-metadata-token") == 0); |
| 2437 |
GPR_ASSERT(strcmp(request->hdrs[0].value, aws_imdsv2_session_token) == 0); |
| 2438 |
return aws_external_account_creds_httpcli_get_success( |
| 2439 |
request, host, path, deadline, on_done, response); |
| 2440 |
} |
| 2441 |
|
| 2442 |
int aws_imdsv2_external_account_creds_httpcli_put_success( |
| 2443 |
const grpc_http_request* request, const char* /*host*/, const char* path, |
| 2444 |
const char* /*body*/, size_t /*body_size*/, Timestamp /*deadline*/, |
| 2445 |
grpc_closure* on_done, grpc_http_response* response) { |
| 2446 |
GPR_ASSERT(request->hdr_count == 1); |
| 2447 |
GPR_ASSERT(strcmp(request->hdrs[0].key, |
| 2448 |
"x-aws-ec2-metadata-token-ttl-seconds") == 0); |
| 2449 |
GPR_ASSERT(strcmp(request->hdrs[0].value, "300") == 0); |
| 2450 |
GPR_ASSERT(strcmp(path, "/imdsv2_session_token_url") == 0); |
| 2451 |
*response = http_response(200, aws_imdsv2_session_token); |
| 2452 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 2453 |
return 1; |
| 2454 |
} |
| 2455 |
|
| 2456 |
int aws_external_account_creds_httpcli_post_success( |
| 2457 |
const grpc_http_request* request, const char* host, const char* path, |
| 2458 |
const char* body, size_t body_size, Timestamp /*deadline*/, |
| 2459 |
grpc_closure* on_done, grpc_http_response* response) { |
| 2460 |
if (strcmp(path, "/token") == 0) { |
| 2461 |
validate_aws_external_account_creds_token_exchage_request( |
| 2462 |
request, host, path, body, body_size, true); |
| 2463 |
*response = http_response( |
| 2464 |
200, valid_external_account_creds_token_exchange_response); |
| 2465 |
} |
| 2466 |
ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 2467 |
return 1; |
| 2468 |
} |
| 2469 |
|
| 2470 |
// The subclass of ExternalAccountCredentials for testing. |
| 2471 |
// ExternalAccountCredentials is an abstract class so we can't directly test |
| 2472 |
// against it. |
| 2473 |
class TestExternalAccountCredentials final : public ExternalAccountCredentials { |
| 2474 |
public: |
| 2475 |
TestExternalAccountCredentials(Options options, |
| 2476 |
std::vector<std::string> scopes) |
| 2477 |
: ExternalAccountCredentials(std::move(options), std::move(scopes)) {} |
| 2478 |
|
| 2479 |
protected: |
| 2480 |
void RetrieveSubjectToken( |
| 2481 |
HTTPRequestContext* /*ctx*/, const Options& /*options*/, |
| 2482 |
std::function<void(std::string, grpc_error_handle)> cb) override { |
| 2483 |
cb("test_subject_token", absl::OkStatus()); |
| 2484 |
} |
| 2485 |
}; |
| 2486 |
|
| 2487 |
TEST(CredentialsTest, TestExternalAccountCredsSuccess) { |
| 2488 |
ExecCtx exec_ctx; |
| 2489 |
Json credential_source(""); |
| 2490 |
TestExternalAccountCredentials::Options options = { |
| 2491 |
"external_account", // type; |
| 2492 |
"audience", // audience; |
| 2493 |
"subject_token_type", // subject_token_type; |
| 2494 |
"", // service_account_impersonation_url; |
| 2495 |
"https://foo.com:5555/token", // token_url; |
| 2496 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2497 |
credential_source, // credential_source; |
| 2498 |
"quota_project_id", // quota_project_id; |
| 2499 |
"client_id", // client_id; |
| 2500 |
"client_secret", // client_secret; |
| 2501 |
"", // workforce_pool_user_project; |
| 2502 |
}; |
| 2503 |
TestExternalAccountCredentials creds(options, {}); |
| 2504 |
/* Check security level. */ |
| 2505 |
GPR_ASSERT(creds.min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 2506 |
/* First request: http put should be called. */ |
| 2507 |
auto state = RequestMetadataState::NewInstance( |
| 2508 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 2509 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 2510 |
external_account_creds_httpcli_post_success, |
| 2511 |
httpcli_put_should_not_be_called); |
| 2512 |
state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority, |
| 2513 |
kTestPath); |
| 2514 |
ExecCtx::Get()->Flush(); |
| 2515 |
/* Second request: the cached token should be served directly. */ |
| 2516 |
state = RequestMetadataState::NewInstance( |
| 2517 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 2518 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 2519 |
httpcli_post_should_not_be_called, |
| 2520 |
httpcli_put_should_not_be_called); |
| 2521 |
state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority, |
| 2522 |
kTestPath); |
| 2523 |
ExecCtx::Get()->Flush(); |
| 2524 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2525 |
} |
| 2526 |
|
| 2527 |
TEST(CredentialsTest, TestExternalAccountCredsSuccessWithUrlEncode) { |
| 2528 |
std::map<std::string, std::string> emd = { |
| 2529 |
{"authorization", "Bearer token_exchange_access_token"}}; |
| 2530 |
ExecCtx exec_ctx; |
| 2531 |
Json credential_source(""); |
| 2532 |
TestExternalAccountCredentials::Options options = { |
| 2533 |
"external_account", // type; |
| 2534 |
"audience_!@#$", // audience; |
| 2535 |
"subject_token_type_!@#$", // subject_token_type; |
| 2536 |
"", // service_account_impersonation_url; |
| 2537 |
"https://foo.com:5555/token_url_encode", // token_url; |
| 2538 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2539 |
credential_source, // credential_source; |
| 2540 |
"quota_project_id", // quota_project_id; |
| 2541 |
"client_id", // client_id; |
| 2542 |
"client_secret", // client_secret; |
| 2543 |
"", // workforce_pool_user_project; |
| 2544 |
}; |
| 2545 |
TestExternalAccountCredentials creds(options, {}); |
| 2546 |
auto state = RequestMetadataState::NewInstance( |
| 2547 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 2548 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 2549 |
external_account_creds_httpcli_post_success, |
| 2550 |
httpcli_put_should_not_be_called); |
| 2551 |
state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority, |
| 2552 |
kTestPath); |
| 2553 |
ExecCtx::Get()->Flush(); |
| 2554 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2555 |
} |
| 2556 |
|
| 2557 |
TEST(CredentialsTest, |
| 2558 |
TestExternalAccountCredsSuccessWithServiceAccountImpersonation) { |
| 2559 |
ExecCtx exec_ctx; |
| 2560 |
Json credential_source(""); |
| 2561 |
TestExternalAccountCredentials::Options options = { |
| 2562 |
"external_account", // type; |
| 2563 |
"audience", // audience; |
| 2564 |
"subject_token_type", // subject_token_type; |
| 2565 |
"https://foo.com:5555/service_account_impersonation", // service_account_impersonation_url; |
| 2566 |
"https://foo.com:5555/token", // token_url; |
| 2567 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2568 |
credential_source, // credential_source; |
| 2569 |
"quota_project_id", // quota_project_id; |
| 2570 |
"client_id", // client_id; |
| 2571 |
"client_secret", // client_secret; |
| 2572 |
"", // workforce_pool_user_project; |
| 2573 |
}; |
| 2574 |
TestExternalAccountCredentials creds(options, {"scope_1", "scope_2"}); |
| 2575 |
/* Check security level. */ |
| 2576 |
GPR_ASSERT(creds.min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 2577 |
/* First request: http put should be called. */ |
| 2578 |
auto state = RequestMetadataState::NewInstance( |
| 2579 |
absl::OkStatus(), |
| 2580 |
"authorization: Bearer service_account_impersonation_access_token"); |
| 2581 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 2582 |
external_account_creds_httpcli_post_success, |
| 2583 |
httpcli_put_should_not_be_called); |
| 2584 |
state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority, |
| 2585 |
kTestPath); |
| 2586 |
ExecCtx::Get()->Flush(); |
| 2587 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2588 |
} |
| 2589 |
|
| 2590 |
TEST(CredentialsTest, TestExternalAccountCredsFailureInvalidTokenUrl) { |
| 2591 |
ExecCtx exec_ctx; |
| 2592 |
Json credential_source(""); |
| 2593 |
TestExternalAccountCredentials::Options options = { |
| 2594 |
"external_account", // type; |
| 2595 |
"audience", // audience; |
| 2596 |
"subject_token_type", // subject_token_type; |
| 2597 |
"https://foo.com:5555/service_account_impersonation", // service_account_impersonation_url; |
| 2598 |
"invalid_token_url", // token_url; |
| 2599 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2600 |
credential_source, // credential_source; |
| 2601 |
"quota_project_id", // quota_project_id; |
| 2602 |
"client_id", // client_id; |
| 2603 |
"client_secret", // client_secret; |
| 2604 |
"", // workforce_pool_user_project; |
| 2605 |
}; |
| 2606 |
TestExternalAccountCredentials creds(options, {}); |
| 2607 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 2608 |
httpcli_post_should_not_be_called, |
| 2609 |
httpcli_put_should_not_be_called); |
| 2610 |
grpc_error_handle error = |
| 2611 |
GRPC_ERROR_CREATE("Invalid token url: invalid_token_url."); |
| 2612 |
grpc_error_handle expected_error = GRPC_ERROR_CREATE_REFERENCING( |
| 2613 |
"Error occurred when fetching oauth2 token.", &error, 1); |
| 2614 |
auto state = RequestMetadataState::NewInstance(expected_error, {}); |
| 2615 |
state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority, |
| 2616 |
kTestPath); |
| 2617 |
ExecCtx::Get()->Flush(); |
| 2618 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2619 |
} |
| 2620 |
|
| 2621 |
TEST(CredentialsTest, |
| 2622 |
TestExternalAccountCredsFailureInvalidServiceAccountImpersonationUrl) { |
| 2623 |
ExecCtx exec_ctx; |
| 2624 |
Json credential_source(""); |
| 2625 |
TestExternalAccountCredentials::Options options = { |
| 2626 |
"external_account", // type; |
| 2627 |
"audience", // audience; |
| 2628 |
"subject_token_type", // subject_token_type; |
| 2629 |
"invalid_service_account_impersonation_url", // service_account_impersonation_url; |
| 2630 |
"https://foo.com:5555/token", // token_url; |
| 2631 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2632 |
credential_source, // credential_source; |
| 2633 |
"quota_project_id", // quota_project_id; |
| 2634 |
"client_id", // client_id; |
| 2635 |
"client_secret", // client_secret; |
| 2636 |
"", // workforce_pool_user_project; |
| 2637 |
}; |
| 2638 |
TestExternalAccountCredentials creds(options, {}); |
| 2639 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 2640 |
external_account_creds_httpcli_post_success, |
| 2641 |
httpcli_put_should_not_be_called); |
| 2642 |
grpc_error_handle error = GRPC_ERROR_CREATE( |
| 2643 |
"Invalid service account impersonation url: " |
| 2644 |
"invalid_service_account_impersonation_url."); |
| 2645 |
grpc_error_handle expected_error = GRPC_ERROR_CREATE_REFERENCING( |
| 2646 |
"Error occurred when fetching oauth2 token.", &error, 1); |
| 2647 |
auto state = RequestMetadataState::NewInstance(expected_error, {}); |
| 2648 |
state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority, |
| 2649 |
kTestPath); |
| 2650 |
ExecCtx::Get()->Flush(); |
| 2651 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2652 |
} |
| 2653 |
|
| 2654 |
TEST(CredentialsTest, |
| 2655 |
TestExternalAccountCredsFailureTokenExchangeResponseMissingAccessToken) { |
| 2656 |
ExecCtx exec_ctx; |
| 2657 |
Json credential_source(""); |
| 2658 |
TestExternalAccountCredentials::Options options = { |
| 2659 |
"external_account", // type; |
| 2660 |
"audience", // audience; |
| 2661 |
"subject_token_type", // subject_token_type; |
| 2662 |
"https://foo.com:5555/service_account_impersonation", // service_account_impersonation_url; |
| 2663 |
"https://foo.com:5555/token", // token_url; |
| 2664 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2665 |
credential_source, // credential_source; |
| 2666 |
"quota_project_id", // quota_project_id; |
| 2667 |
"client_id", // client_id; |
| 2668 |
"client_secret", // client_secret; |
| 2669 |
"", // workforce_pool_user_project; |
| 2670 |
}; |
| 2671 |
TestExternalAccountCredentials creds(options, {}); |
| 2672 |
HttpRequest::SetOverride( |
| 2673 |
httpcli_get_should_not_be_called, |
| 2674 |
external_account_creds_httpcli_post_failure_token_exchange_response_missing_access_token, |
| 2675 |
httpcli_put_should_not_be_called); |
| 2676 |
grpc_error_handle error = GRPC_ERROR_CREATE( |
| 2677 |
"Missing or invalid access_token in " |
| 2678 |
"{\"not_access_token\":\"not_access_token\",\"expires_in\":3599,\"token_" |
| 2679 |
"type\":\"Bearer\"}."); |
| 2680 |
grpc_error_handle expected_error = GRPC_ERROR_CREATE_REFERENCING( |
| 2681 |
"Error occurred when fetching oauth2 token.", &error, 1); |
| 2682 |
auto state = RequestMetadataState::NewInstance(expected_error, {}); |
| 2683 |
state->RunRequestMetadataTest(&creds, kTestUrlScheme, kTestAuthority, |
| 2684 |
kTestPath); |
| 2685 |
ExecCtx::Get()->Flush(); |
| 2686 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2687 |
} |
| 2688 |
|
| 2689 |
TEST(CredentialsTest, TestUrlExternalAccountCredsSuccessFormatText) { |
| 2690 |
ExecCtx exec_ctx; |
| 2691 |
auto credential_source = Json::Parse( |
| 2692 |
valid_url_external_account_creds_options_credential_source_format_text); |
| 2693 |
GPR_ASSERT(credential_source.ok()); |
| 2694 |
ExternalAccountCredentials::Options options = { |
| 2695 |
"external_account", // type; |
| 2696 |
"audience", // audience; |
| 2697 |
"subject_token_type", // subject_token_type; |
| 2698 |
"", // service_account_impersonation_url; |
| 2699 |
"https://foo.com:5555/token", // token_url; |
| 2700 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2701 |
*credential_source, // credential_source; |
| 2702 |
"quota_project_id", // quota_project_id; |
| 2703 |
"client_id", // client_id; |
| 2704 |
"client_secret", // client_secret; |
| 2705 |
"", // workforce_pool_user_project; |
| 2706 |
}; |
| 2707 |
grpc_error_handle error; |
| 2708 |
auto creds = UrlExternalAccountCredentials::Create(options, {}, &error); |
| 2709 |
GPR_ASSERT(creds != nullptr); |
| 2710 |
GPR_ASSERT(error.ok()); |
| 2711 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 2712 |
auto state = RequestMetadataState::NewInstance( |
| 2713 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 2714 |
HttpRequest::SetOverride(url_external_account_creds_httpcli_get_success, |
| 2715 |
external_account_creds_httpcli_post_success, |
| 2716 |
httpcli_put_should_not_be_called); |
| 2717 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 2718 |
kTestPath); |
| 2719 |
ExecCtx::Get()->Flush(); |
| 2720 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2721 |
} |
| 2722 |
|
| 2723 |
TEST(CredentialsTest, |
| 2724 |
TestUrlExternalAccountCredsSuccessWithQureyParamsFormatText) { |
| 2725 |
std::map<std::string, std::string> emd = { |
| 2726 |
{"authorization", "Bearer token_exchange_access_token"}}; |
| 2727 |
ExecCtx exec_ctx; |
| 2728 |
auto credential_source = Json::Parse( |
| 2729 |
valid_url_external_account_creds_options_credential_source_with_qurey_params_format_text); |
| 2730 |
GPR_ASSERT(credential_source.ok()); |
| 2731 |
ExternalAccountCredentials::Options options = { |
| 2732 |
"external_account", // type; |
| 2733 |
"audience", // audience; |
| 2734 |
"subject_token_type", // subject_token_type; |
| 2735 |
"", // service_account_impersonation_url; |
| 2736 |
"https://foo.com:5555/token", // token_url; |
| 2737 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2738 |
*credential_source, // credential_source; |
| 2739 |
"quota_project_id", // quota_project_id; |
| 2740 |
"client_id", // client_id; |
| 2741 |
"client_secret", // client_secret; |
| 2742 |
"", // workforce_pool_user_project; |
| 2743 |
}; |
| 2744 |
grpc_error_handle error; |
| 2745 |
auto creds = UrlExternalAccountCredentials::Create(options, {}, &error); |
| 2746 |
GPR_ASSERT(creds != nullptr); |
| 2747 |
GPR_ASSERT(error.ok()); |
| 2748 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 2749 |
auto state = RequestMetadataState::NewInstance( |
| 2750 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 2751 |
HttpRequest::SetOverride(url_external_account_creds_httpcli_get_success, |
| 2752 |
external_account_creds_httpcli_post_success, |
| 2753 |
httpcli_put_should_not_be_called); |
| 2754 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 2755 |
kTestPath); |
| 2756 |
ExecCtx::Get()->Flush(); |
| 2757 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2758 |
} |
| 2759 |
|
| 2760 |
TEST(CredentialsTest, TestUrlExternalAccountCredsSuccessFormatJson) { |
| 2761 |
ExecCtx exec_ctx; |
| 2762 |
auto credential_source = Json::Parse( |
| 2763 |
valid_url_external_account_creds_options_credential_source_format_json); |
| 2764 |
GPR_ASSERT(credential_source.ok()); |
| 2765 |
ExternalAccountCredentials::Options options = { |
| 2766 |
"external_account", // type; |
| 2767 |
"audience", // audience; |
| 2768 |
"subject_token_type", // subject_token_type; |
| 2769 |
"", // service_account_impersonation_url; |
| 2770 |
"https://foo.com:5555/token", // token_url; |
| 2771 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2772 |
*credential_source, // credential_source; |
| 2773 |
"quota_project_id", // quota_project_id; |
| 2774 |
"client_id", // client_id; |
| 2775 |
"client_secret", // client_secret; |
| 2776 |
"", // workforce_pool_user_project; |
| 2777 |
}; |
| 2778 |
grpc_error_handle error; |
| 2779 |
auto creds = UrlExternalAccountCredentials::Create(options, {}, &error); |
| 2780 |
GPR_ASSERT(creds != nullptr); |
| 2781 |
GPR_ASSERT(error.ok()); |
| 2782 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 2783 |
auto state = RequestMetadataState::NewInstance( |
| 2784 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 2785 |
HttpRequest::SetOverride(url_external_account_creds_httpcli_get_success, |
| 2786 |
external_account_creds_httpcli_post_success, |
| 2787 |
httpcli_put_should_not_be_called); |
| 2788 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 2789 |
kTestPath); |
| 2790 |
ExecCtx::Get()->Flush(); |
| 2791 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2792 |
} |
| 2793 |
|
| 2794 |
TEST(CredentialsTest, |
| 2795 |
TestUrlExternalAccountCredsFailureInvalidCredentialSourceUrl) { |
| 2796 |
auto credential_source = |
| 2797 |
Json::Parse(invalid_url_external_account_creds_options_credential_source); |
| 2798 |
GPR_ASSERT(credential_source.ok()); |
| 2799 |
ExternalAccountCredentials::Options options = { |
| 2800 |
"external_account", // type; |
| 2801 |
"audience", // audience; |
| 2802 |
"subject_token_type", // subject_token_type; |
| 2803 |
"", // service_account_impersonation_url; |
| 2804 |
"https://foo.com:5555/token", // token_url; |
| 2805 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2806 |
*credential_source, // credential_source; |
| 2807 |
"quota_project_id", // quota_project_id; |
| 2808 |
"client_id", // client_id; |
| 2809 |
"client_secret", // client_secret; |
| 2810 |
"", // workforce_pool_user_project; |
| 2811 |
}; |
| 2812 |
grpc_error_handle error; |
| 2813 |
auto creds = UrlExternalAccountCredentials::Create(options, {}, &error); |
| 2814 |
GPR_ASSERT(creds == nullptr); |
| 2815 |
std::string actual_error; |
| 2816 |
GPR_ASSERT(grpc_error_get_str(error, StatusStrProperty::kDescription, |
| 2817 |
&actual_error)); |
| 2818 |
GPR_ASSERT(absl::StartsWith(actual_error, "Invalid credential source url.")); |
| 2819 |
} |
| 2820 |
|
| 2821 |
TEST(CredentialsTest, TestFileExternalAccountCredsSuccessFormatText) { |
| 2822 |
ExecCtx exec_ctx; |
| 2823 |
char* subject_token_path = write_tmp_jwt_file("test_subject_token"); |
| 2824 |
auto credential_source = Json::Parse(absl::StrFormat( |
| 2825 |
"{\"file\":\"%s\"}", |
| 2826 |
absl::StrReplaceAll(subject_token_path, {{"\\", "\\\\"}}))); |
| 2827 |
GPR_ASSERT(credential_source.ok()); |
| 2828 |
ExternalAccountCredentials::Options options = { |
| 2829 |
"external_account", // type; |
| 2830 |
"audience", // audience; |
| 2831 |
"subject_token_type", // subject_token_type; |
| 2832 |
"", // service_account_impersonation_url; |
| 2833 |
"https://foo.com:5555/token", // token_url; |
| 2834 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2835 |
*credential_source, // credential_source; |
| 2836 |
"quota_project_id", // quota_project_id; |
| 2837 |
"client_id", // client_id; |
| 2838 |
"client_secret", // client_secret; |
| 2839 |
"", // workforce_pool_user_project; |
| 2840 |
}; |
| 2841 |
grpc_error_handle error; |
| 2842 |
auto creds = FileExternalAccountCredentials::Create(options, {}, &error); |
| 2843 |
GPR_ASSERT(creds != nullptr); |
| 2844 |
GPR_ASSERT(error.ok()); |
| 2845 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 2846 |
auto state = RequestMetadataState::NewInstance( |
| 2847 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 2848 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 2849 |
external_account_creds_httpcli_post_success, |
| 2850 |
httpcli_put_should_not_be_called); |
| 2851 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 2852 |
kTestPath); |
| 2853 |
ExecCtx::Get()->Flush(); |
| 2854 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2855 |
gpr_free(subject_token_path); |
| 2856 |
} |
| 2857 |
|
| 2858 |
TEST(CredentialsTest, TestFileExternalAccountCredsSuccessFormatJson) { |
| 2859 |
ExecCtx exec_ctx; |
| 2860 |
char* subject_token_path = |
| 2861 |
write_tmp_jwt_file("{\"access_token\":\"test_subject_token\"}"); |
| 2862 |
auto credential_source = Json::Parse(absl::StrFormat( |
| 2863 |
"{\n" |
| 2864 |
"\"file\":\"%s\",\n" |
| 2865 |
"\"format\":\n" |
| 2866 |
"{\n" |
| 2867 |
"\"type\":\"json\",\n" |
| 2868 |
"\"subject_token_field_name\":\"access_token\"\n" |
| 2869 |
"}\n" |
| 2870 |
"}", |
| 2871 |
absl::StrReplaceAll(subject_token_path, {{"\\", "\\\\"}}))); |
| 2872 |
GPR_ASSERT(credential_source.ok()); |
| 2873 |
ExternalAccountCredentials::Options options = { |
| 2874 |
"external_account", // type; |
| 2875 |
"audience", // audience; |
| 2876 |
"subject_token_type", // subject_token_type; |
| 2877 |
"", // service_account_impersonation_url; |
| 2878 |
"https://foo.com:5555/token", // token_url; |
| 2879 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2880 |
*credential_source, // credential_source; |
| 2881 |
"quota_project_id", // quota_project_id; |
| 2882 |
"client_id", // client_id; |
| 2883 |
"client_secret", // client_secret; |
| 2884 |
"", // workforce_pool_user_project; |
| 2885 |
}; |
| 2886 |
grpc_error_handle error; |
| 2887 |
auto creds = FileExternalAccountCredentials::Create(options, {}, &error); |
| 2888 |
GPR_ASSERT(creds != nullptr); |
| 2889 |
GPR_ASSERT(error.ok()); |
| 2890 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 2891 |
auto state = RequestMetadataState::NewInstance( |
| 2892 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 2893 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 2894 |
external_account_creds_httpcli_post_success, |
| 2895 |
httpcli_put_should_not_be_called); |
| 2896 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 2897 |
kTestPath); |
| 2898 |
ExecCtx::Get()->Flush(); |
| 2899 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2900 |
gpr_free(subject_token_path); |
| 2901 |
} |
| 2902 |
|
| 2903 |
TEST(CredentialsTest, TestFileExternalAccountCredsFailureFileNotFound) { |
| 2904 |
ExecCtx exec_ctx; |
| 2905 |
auto credential_source = Json::Parse("{\"file\":\"non_exisiting_file\"}"); |
| 2906 |
GPR_ASSERT(credential_source.ok()); |
| 2907 |
ExternalAccountCredentials::Options options = { |
| 2908 |
"external_account", // type; |
| 2909 |
"audience", // audience; |
| 2910 |
"subject_token_type", // subject_token_type; |
| 2911 |
"", // service_account_impersonation_url; |
| 2912 |
"https://foo.com:5555/token", // token_url; |
| 2913 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2914 |
*credential_source, // credential_source; |
| 2915 |
"quota_project_id", // quota_project_id; |
| 2916 |
"client_id", // client_id; |
| 2917 |
"client_secret", // client_secret; |
| 2918 |
"", // workforce_pool_user_project; |
| 2919 |
}; |
| 2920 |
grpc_error_handle error; |
| 2921 |
auto creds = FileExternalAccountCredentials::Create(options, {}, &error); |
| 2922 |
GPR_ASSERT(creds != nullptr); |
| 2923 |
GPR_ASSERT(error.ok()); |
| 2924 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 2925 |
httpcli_post_should_not_be_called, |
| 2926 |
httpcli_put_should_not_be_called); |
| 2927 |
error = GRPC_ERROR_CREATE("Failed to load file"); |
| 2928 |
grpc_error_handle expected_error = GRPC_ERROR_CREATE_REFERENCING( |
| 2929 |
"Error occurred when fetching oauth2 token.", &error, 1); |
| 2930 |
auto state = RequestMetadataState::NewInstance(expected_error, {}); |
| 2931 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 2932 |
kTestPath); |
| 2933 |
ExecCtx::Get()->Flush(); |
| 2934 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2935 |
} |
| 2936 |
|
| 2937 |
TEST(CredentialsTest, TestFileExternalAccountCredsFailureInvalidJsonContent) { |
| 2938 |
ExecCtx exec_ctx; |
| 2939 |
char* subject_token_path = write_tmp_jwt_file("not_a_valid_json_file"); |
| 2940 |
auto credential_source = Json::Parse(absl::StrFormat( |
| 2941 |
"{\n" |
| 2942 |
"\"file\":\"%s\",\n" |
| 2943 |
"\"format\":\n" |
| 2944 |
"{\n" |
| 2945 |
"\"type\":\"json\",\n" |
| 2946 |
"\"subject_token_field_name\":\"access_token\"\n" |
| 2947 |
"}\n" |
| 2948 |
"}", |
| 2949 |
absl::StrReplaceAll(subject_token_path, {{"\\", "\\\\"}}))); |
| 2950 |
GPR_ASSERT(credential_source.ok()); |
| 2951 |
ExternalAccountCredentials::Options options = { |
| 2952 |
"external_account", // type; |
| 2953 |
"audience", // audience; |
| 2954 |
"subject_token_type", // subject_token_type; |
| 2955 |
"", // service_account_impersonation_url; |
| 2956 |
"https://foo.com:5555/token", // token_url; |
| 2957 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2958 |
*credential_source, // credential_source; |
| 2959 |
"quota_project_id", // quota_project_id; |
| 2960 |
"client_id", // client_id; |
| 2961 |
"client_secret", // client_secret; |
| 2962 |
"", // workforce_pool_user_project; |
| 2963 |
}; |
| 2964 |
grpc_error_handle error; |
| 2965 |
auto creds = FileExternalAccountCredentials::Create(options, {}, &error); |
| 2966 |
GPR_ASSERT(creds != nullptr); |
| 2967 |
GPR_ASSERT(error.ok()); |
| 2968 |
HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 2969 |
httpcli_post_should_not_be_called, |
| 2970 |
httpcli_put_should_not_be_called); |
| 2971 |
error = |
| 2972 |
GRPC_ERROR_CREATE("The content of the file is not a valid json object."); |
| 2973 |
grpc_error_handle expected_error = GRPC_ERROR_CREATE_REFERENCING( |
| 2974 |
"Error occurred when fetching oauth2 token.", &error, 1); |
| 2975 |
auto state = RequestMetadataState::NewInstance(expected_error, {}); |
| 2976 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 2977 |
kTestPath); |
| 2978 |
ExecCtx::Get()->Flush(); |
| 2979 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 2980 |
gpr_free(subject_token_path); |
| 2981 |
} |
| 2982 |
|
| 2983 |
TEST(CredentialsTest, TestAwsExternalAccountCredsSuccess) { |
| 2984 |
ExecCtx exec_ctx; |
| 2985 |
auto credential_source = |
| 2986 |
Json::Parse(valid_aws_external_account_creds_options_credential_source); |
| 2987 |
GPR_ASSERT(credential_source.ok()); |
| 2988 |
ExternalAccountCredentials::Options options = { |
| 2989 |
"external_account", // type; |
| 2990 |
"audience", // audience; |
| 2991 |
"subject_token_type", // subject_token_type; |
| 2992 |
"", // service_account_impersonation_url; |
| 2993 |
"https://foo.com:5555/token", // token_url; |
| 2994 |
"https://foo.com:5555/token_info", // token_info_url; |
| 2995 |
*credential_source, // credential_source; |
| 2996 |
"quota_project_id", // quota_project_id; |
| 2997 |
"client_id", // client_id; |
| 2998 |
"client_secret", // client_secret; |
| 2999 |
"", // workforce_pool_user_project; |
| 3000 |
}; |
| 3001 |
grpc_error_handle error; |
| 3002 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3003 |
GPR_ASSERT(creds != nullptr); |
| 3004 |
GPR_ASSERT(error.ok()); |
| 3005 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 3006 |
auto state = RequestMetadataState::NewInstance( |
| 3007 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 3008 |
HttpRequest::SetOverride(aws_external_account_creds_httpcli_get_success, |
| 3009 |
aws_external_account_creds_httpcli_post_success, |
| 3010 |
httpcli_put_should_not_be_called); |
| 3011 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 3012 |
kTestPath); |
| 3013 |
ExecCtx::Get()->Flush(); |
| 3014 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 3015 |
} |
| 3016 |
|
| 3017 |
TEST(CredentialsTest, TestAwsImdsv2ExternalAccountCredsSuccess) { |
| 3018 |
ExecCtx exec_ctx; |
| 3019 |
auto credential_source = Json::Parse( |
| 3020 |
valid_aws_imdsv2_external_account_creds_options_credential_source); |
| 3021 |
GPR_ASSERT(credential_source.ok()); |
| 3022 |
ExternalAccountCredentials::Options options = { |
| 3023 |
"external_account", // type; |
| 3024 |
"audience", // audience; |
| 3025 |
"subject_token_type", // subject_token_type; |
| 3026 |
"", // service_account_impersonation_url; |
| 3027 |
"https://foo.com:5555/token", // token_url; |
| 3028 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3029 |
*credential_source, // credential_source; |
| 3030 |
"quota_project_id", // quota_project_id; |
| 3031 |
"client_id", // client_id; |
| 3032 |
"client_secret", // client_secret; |
| 3033 |
"", // workforce_pool_user_project; |
| 3034 |
}; |
| 3035 |
grpc_error_handle error; |
| 3036 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3037 |
GPR_ASSERT(creds != nullptr); |
| 3038 |
GPR_ASSERT(error.ok()); |
| 3039 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 3040 |
auto state = RequestMetadataState::NewInstance( |
| 3041 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 3042 |
HttpRequest::SetOverride( |
| 3043 |
aws_imdsv2_external_account_creds_httpcli_get_success, |
| 3044 |
aws_external_account_creds_httpcli_post_success, |
| 3045 |
aws_imdsv2_external_account_creds_httpcli_put_success); |
| 3046 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 3047 |
kTestPath); |
| 3048 |
ExecCtx::Get()->Flush(); |
| 3049 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 3050 |
} |
| 3051 |
|
| 3052 |
TEST(CredentialsTest, TestAwsExternalAccountCredsSuccessIpv6) { |
| 3053 |
ExecCtx exec_ctx; |
| 3054 |
auto credential_source = Json::Parse( |
| 3055 |
valid_aws_external_account_creds_options_credential_source_ipv6); |
| 3056 |
GPR_ASSERT(credential_source.ok()); |
| 3057 |
ExternalAccountCredentials::Options options = { |
| 3058 |
"external_account", // type; |
| 3059 |
"audience", // audience; |
| 3060 |
"subject_token_type", // subject_token_type; |
| 3061 |
"", // service_account_impersonation_url; |
| 3062 |
"https://foo.com:5555/token", // token_url; |
| 3063 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3064 |
*credential_source, // credential_source; |
| 3065 |
"quota_project_id", // quota_project_id; |
| 3066 |
"client_id", // client_id; |
| 3067 |
"client_secret", // client_secret; |
| 3068 |
"", // workforce_pool_user_project; |
| 3069 |
}; |
| 3070 |
grpc_error_handle error; |
| 3071 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3072 |
GPR_ASSERT(creds != nullptr); |
| 3073 |
GPR_ASSERT(error.ok()); |
| 3074 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 3075 |
auto state = RequestMetadataState::NewInstance( |
| 3076 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 3077 |
HttpRequest::SetOverride( |
| 3078 |
aws_imdsv2_external_account_creds_httpcli_get_success, |
| 3079 |
aws_external_account_creds_httpcli_post_success, |
| 3080 |
aws_imdsv2_external_account_creds_httpcli_put_success); |
| 3081 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 3082 |
kTestPath); |
| 3083 |
ExecCtx::Get()->Flush(); |
| 3084 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 3085 |
} |
| 3086 |
|
| 3087 |
TEST(CredentialsTest, TestAwsExternalAccountCredsSuccessPathRegionEnvKeysUrl) { |
| 3088 |
ExecCtx exec_ctx; |
| 3089 |
SetEnv("AWS_REGION", "test_regionz"); |
| 3090 |
auto credential_source = |
| 3091 |
Json::Parse(valid_aws_external_account_creds_options_credential_source); |
| 3092 |
GPR_ASSERT(credential_source.ok()); |
| 3093 |
ExternalAccountCredentials::Options options = { |
| 3094 |
"external_account", // type; |
| 3095 |
"audience", // audience; |
| 3096 |
"subject_token_type", // subject_token_type; |
| 3097 |
"", // service_account_impersonation_url; |
| 3098 |
"https://foo.com:5555/token", // token_url; |
| 3099 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3100 |
*credential_source, // credential_source; |
| 3101 |
"quota_project_id", // quota_project_id; |
| 3102 |
"client_id", // client_id; |
| 3103 |
"client_secret", // client_secret; |
| 3104 |
"", // workforce_pool_user_project; |
| 3105 |
}; |
| 3106 |
grpc_error_handle error; |
| 3107 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3108 |
GPR_ASSERT(creds != nullptr); |
| 3109 |
GPR_ASSERT(error.ok()); |
| 3110 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 3111 |
auto state = RequestMetadataState::NewInstance( |
| 3112 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 3113 |
HttpRequest::SetOverride(aws_external_account_creds_httpcli_get_success, |
| 3114 |
aws_external_account_creds_httpcli_post_success, |
| 3115 |
httpcli_put_should_not_be_called); |
| 3116 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 3117 |
kTestPath); |
| 3118 |
ExecCtx::Get()->Flush(); |
| 3119 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 3120 |
UnsetEnv("AWS_REGION"); |
| 3121 |
} |
| 3122 |
|
| 3123 |
TEST(CredentialsTest, |
| 3124 |
TestAwsExternalAccountCredsSuccessPathDefaultRegionEnvKeysUrl) { |
| 3125 |
ExecCtx exec_ctx; |
| 3126 |
SetEnv("AWS_DEFAULT_REGION", "test_regionz"); |
| 3127 |
auto credential_source = |
| 3128 |
Json::Parse(valid_aws_external_account_creds_options_credential_source); |
| 3129 |
GPR_ASSERT(credential_source.ok()); |
| 3130 |
ExternalAccountCredentials::Options options = { |
| 3131 |
"external_account", // type; |
| 3132 |
"audience", // audience; |
| 3133 |
"subject_token_type", // subject_token_type; |
| 3134 |
"", // service_account_impersonation_url; |
| 3135 |
"https://foo.com:5555/token", // token_url; |
| 3136 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3137 |
*credential_source, // credential_source; |
| 3138 |
"quota_project_id", // quota_project_id; |
| 3139 |
"client_id", // client_id; |
| 3140 |
"client_secret", // client_secret; |
| 3141 |
"", // workforce_pool_user_project; |
| 3142 |
}; |
| 3143 |
grpc_error_handle error; |
| 3144 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3145 |
GPR_ASSERT(creds != nullptr); |
| 3146 |
GPR_ASSERT(error.ok()); |
| 3147 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 3148 |
auto state = RequestMetadataState::NewInstance( |
| 3149 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 3150 |
HttpRequest::SetOverride(aws_external_account_creds_httpcli_get_success, |
| 3151 |
aws_external_account_creds_httpcli_post_success, |
| 3152 |
httpcli_put_should_not_be_called); |
| 3153 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 3154 |
kTestPath); |
| 3155 |
ExecCtx::Get()->Flush(); |
| 3156 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 3157 |
UnsetEnv("AWS_DEFAULT_REGION"); |
| 3158 |
} |
| 3159 |
|
| 3160 |
TEST(CredentialsTest, |
| 3161 |
TestAwsExternalAccountCredsSuccessPathDuplicateRegionEnvKeysUrl) { |
| 3162 |
ExecCtx exec_ctx; |
| 3163 |
// Make sure that AWS_REGION gets used over AWS_DEFAULT_REGION |
| 3164 |
SetEnv("AWS_REGION", "test_regionz"); |
| 3165 |
SetEnv("AWS_DEFAULT_REGION", "ERROR_REGION"); |
| 3166 |
auto credential_source = |
| 3167 |
Json::Parse(valid_aws_external_account_creds_options_credential_source); |
| 3168 |
GPR_ASSERT(credential_source.ok()); |
| 3169 |
ExternalAccountCredentials::Options options = { |
| 3170 |
"external_account", // type; |
| 3171 |
"audience", // audience; |
| 3172 |
"subject_token_type", // subject_token_type; |
| 3173 |
"", // service_account_impersonation_url; |
| 3174 |
"https://foo.com:5555/token", // token_url; |
| 3175 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3176 |
*credential_source, // credential_source; |
| 3177 |
"quota_project_id", // quota_project_id; |
| 3178 |
"client_id", // client_id; |
| 3179 |
"client_secret", // client_secret; |
| 3180 |
"", // workforce_pool_user_project; |
| 3181 |
}; |
| 3182 |
grpc_error_handle error; |
| 3183 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3184 |
GPR_ASSERT(creds != nullptr); |
| 3185 |
GPR_ASSERT(error.ok()); |
| 3186 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 3187 |
auto state = RequestMetadataState::NewInstance( |
| 3188 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 3189 |
HttpRequest::SetOverride(aws_external_account_creds_httpcli_get_success, |
| 3190 |
aws_external_account_creds_httpcli_post_success, |
| 3191 |
httpcli_put_should_not_be_called); |
| 3192 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 3193 |
kTestPath); |
| 3194 |
ExecCtx::Get()->Flush(); |
| 3195 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 3196 |
UnsetEnv("AWS_REGION"); |
| 3197 |
UnsetEnv("AWS_DEFAULT_REGION"); |
| 3198 |
} |
| 3199 |
|
| 3200 |
TEST(CredentialsTest, TestAwsExternalAccountCredsSuccessPathRegionUrlKeysEnv) { |
| 3201 |
ExecCtx exec_ctx; |
| 3202 |
SetEnv("AWS_ACCESS_KEY_ID", "test_access_key_id"); |
| 3203 |
SetEnv("AWS_SECRET_ACCESS_KEY", "test_secret_access_key"); |
| 3204 |
SetEnv("AWS_SESSION_TOKEN", "test_token"); |
| 3205 |
auto credential_source = |
| 3206 |
Json::Parse(valid_aws_external_account_creds_options_credential_source); |
| 3207 |
GPR_ASSERT(credential_source.ok()); |
| 3208 |
ExternalAccountCredentials::Options options = { |
| 3209 |
"external_account", // type; |
| 3210 |
"audience", // audience; |
| 3211 |
"subject_token_type", // subject_token_type; |
| 3212 |
"", // service_account_impersonation_url; |
| 3213 |
"https://foo.com:5555/token", // token_url; |
| 3214 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3215 |
*credential_source, // credential_source; |
| 3216 |
"quota_project_id", // quota_project_id; |
| 3217 |
"client_id", // client_id; |
| 3218 |
"client_secret", // client_secret; |
| 3219 |
"", // workforce_pool_user_project; |
| 3220 |
}; |
| 3221 |
grpc_error_handle error; |
| 3222 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3223 |
GPR_ASSERT(creds != nullptr); |
| 3224 |
GPR_ASSERT(error.ok()); |
| 3225 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 3226 |
auto state = RequestMetadataState::NewInstance( |
| 3227 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 3228 |
HttpRequest::SetOverride(aws_external_account_creds_httpcli_get_success, |
| 3229 |
aws_external_account_creds_httpcli_post_success, |
| 3230 |
httpcli_put_should_not_be_called); |
| 3231 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 3232 |
kTestPath); |
| 3233 |
ExecCtx::Get()->Flush(); |
| 3234 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 3235 |
UnsetEnv("AWS_ACCESS_KEY_ID"); |
| 3236 |
UnsetEnv("AWS_SECRET_ACCESS_KEY"); |
| 3237 |
UnsetEnv("AWS_SESSION_TOKEN"); |
| 3238 |
} |
| 3239 |
|
| 3240 |
TEST(CredentialsTest, TestAwsExternalAccountCredsSuccessPathRegionEnvKeysEnv) { |
| 3241 |
ExecCtx exec_ctx; |
| 3242 |
SetEnv("AWS_REGION", "test_regionz"); |
| 3243 |
SetEnv("AWS_ACCESS_KEY_ID", "test_access_key_id"); |
| 3244 |
SetEnv("AWS_SECRET_ACCESS_KEY", "test_secret_access_key"); |
| 3245 |
SetEnv("AWS_SESSION_TOKEN", "test_token"); |
| 3246 |
auto credential_source = |
| 3247 |
Json::Parse(valid_aws_external_account_creds_options_credential_source); |
| 3248 |
GPR_ASSERT(credential_source.ok()); |
| 3249 |
ExternalAccountCredentials::Options options = { |
| 3250 |
"external_account", // type; |
| 3251 |
"audience", // audience; |
| 3252 |
"subject_token_type", // subject_token_type; |
| 3253 |
"", // service_account_impersonation_url; |
| 3254 |
"https://foo.com:5555/token", // token_url; |
| 3255 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3256 |
*credential_source, // credential_source; |
| 3257 |
"quota_project_id", // quota_project_id; |
| 3258 |
"client_id", // client_id; |
| 3259 |
"client_secret", // client_secret; |
| 3260 |
"", // workforce_pool_user_project; |
| 3261 |
}; |
| 3262 |
grpc_error_handle error; |
| 3263 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3264 |
GPR_ASSERT(creds != nullptr); |
| 3265 |
GPR_ASSERT(error.ok()); |
| 3266 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 3267 |
auto state = RequestMetadataState::NewInstance( |
| 3268 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 3269 |
HttpRequest::SetOverride(aws_external_account_creds_httpcli_get_success, |
| 3270 |
aws_external_account_creds_httpcli_post_success, |
| 3271 |
httpcli_put_should_not_be_called); |
| 3272 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 3273 |
kTestPath); |
| 3274 |
ExecCtx::Get()->Flush(); |
| 3275 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 3276 |
UnsetEnv("AWS_REGION"); |
| 3277 |
UnsetEnv("AWS_ACCESS_KEY_ID"); |
| 3278 |
UnsetEnv("AWS_SECRET_ACCESS_KEY"); |
| 3279 |
UnsetEnv("AWS_SESSION_TOKEN"); |
| 3280 |
} |
| 3281 |
|
| 3282 |
TEST(CredentialsTest, |
| 3283 |
TestAwsExternalAccountCredsSuccessPathDefaultRegionEnvKeysEnv) { |
| 3284 |
std::map<std::string, std::string> emd = { |
| 3285 |
{"authorization", "Bearer token_exchange_access_token"}}; |
| 3286 |
ExecCtx exec_ctx; |
| 3287 |
SetEnv("AWS_DEFAULT_REGION", "test_regionz"); |
| 3288 |
SetEnv("AWS_ACCESS_KEY_ID", "test_access_key_id"); |
| 3289 |
SetEnv("AWS_SECRET_ACCESS_KEY", "test_secret_access_key"); |
| 3290 |
SetEnv("AWS_SESSION_TOKEN", "test_token"); |
| 3291 |
auto credential_source = |
| 3292 |
Json::Parse(valid_aws_external_account_creds_options_credential_source); |
| 3293 |
GPR_ASSERT(credential_source.ok()); |
| 3294 |
ExternalAccountCredentials::Options options = { |
| 3295 |
"external_account", // type; |
| 3296 |
"audience", // audience; |
| 3297 |
"subject_token_type", // subject_token_type; |
| 3298 |
"", // service_account_impersonation_url; |
| 3299 |
"https://foo.com:5555/token", // token_url; |
| 3300 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3301 |
*credential_source, // credential_source; |
| 3302 |
"quota_project_id", // quota_project_id; |
| 3303 |
"client_id", // client_id; |
| 3304 |
"client_secret", // client_secret; |
| 3305 |
"", // workforce_pool_user_project; |
| 3306 |
}; |
| 3307 |
grpc_error_handle error; |
| 3308 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3309 |
GPR_ASSERT(creds != nullptr); |
| 3310 |
GPR_ASSERT(error.ok()); |
| 3311 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 3312 |
auto state = RequestMetadataState::NewInstance( |
| 3313 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 3314 |
HttpRequest::SetOverride(aws_external_account_creds_httpcli_get_success, |
| 3315 |
aws_external_account_creds_httpcli_post_success, |
| 3316 |
httpcli_put_should_not_be_called); |
| 3317 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 3318 |
kTestPath); |
| 3319 |
ExecCtx::Get()->Flush(); |
| 3320 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 3321 |
UnsetEnv("AWS_DEFAULT_REGION"); |
| 3322 |
UnsetEnv("AWS_ACCESS_KEY_ID"); |
| 3323 |
UnsetEnv("AWS_SECRET_ACCESS_KEY"); |
| 3324 |
UnsetEnv("AWS_SESSION_TOKEN"); |
| 3325 |
} |
| 3326 |
|
| 3327 |
TEST(CredentialsTest, |
| 3328 |
TestAwsExternalAccountCredsSuccessPathDuplicateRegionEnvKeysEnv) { |
| 3329 |
ExecCtx exec_ctx; |
| 3330 |
// Make sure that AWS_REGION gets used over AWS_DEFAULT_REGION |
| 3331 |
SetEnv("AWS_REGION", "test_regionz"); |
| 3332 |
SetEnv("AWS_DEFAULT_REGION", "ERROR_REGION"); |
| 3333 |
SetEnv("AWS_ACCESS_KEY_ID", "test_access_key_id"); |
| 3334 |
SetEnv("AWS_SECRET_ACCESS_KEY", "test_secret_access_key"); |
| 3335 |
SetEnv("AWS_SESSION_TOKEN", "test_token"); |
| 3336 |
auto credential_source = |
| 3337 |
Json::Parse(valid_aws_external_account_creds_options_credential_source); |
| 3338 |
GPR_ASSERT(credential_source.ok()); |
| 3339 |
ExternalAccountCredentials::Options options = { |
| 3340 |
"external_account", // type; |
| 3341 |
"audience", // audience; |
| 3342 |
"subject_token_type", // subject_token_type; |
| 3343 |
"", // service_account_impersonation_url; |
| 3344 |
"https://foo.com:5555/token", // token_url; |
| 3345 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3346 |
*credential_source, // credential_source; |
| 3347 |
"quota_project_id", // quota_project_id; |
| 3348 |
"client_id", // client_id; |
| 3349 |
"client_secret", // client_secret; |
| 3350 |
"", // workforce_pool_user_project; |
| 3351 |
}; |
| 3352 |
grpc_error_handle error; |
| 3353 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3354 |
GPR_ASSERT(creds != nullptr); |
| 3355 |
GPR_ASSERT(error.ok()); |
| 3356 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 3357 |
auto state = RequestMetadataState::NewInstance( |
| 3358 |
absl::OkStatus(), "authorization: Bearer token_exchange_access_token"); |
| 3359 |
HttpRequest::SetOverride(aws_external_account_creds_httpcli_get_success, |
| 3360 |
aws_external_account_creds_httpcli_post_success, |
| 3361 |
httpcli_put_should_not_be_called); |
| 3362 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 3363 |
kTestPath); |
| 3364 |
ExecCtx::Get()->Flush(); |
| 3365 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 3366 |
UnsetEnv("AWS_REGION"); |
| 3367 |
UnsetEnv("AWS_DEFAULT_REGION"); |
| 3368 |
UnsetEnv("AWS_ACCESS_KEY_ID"); |
| 3369 |
UnsetEnv("AWS_SECRET_ACCESS_KEY"); |
| 3370 |
UnsetEnv("AWS_SESSION_TOKEN"); |
| 3371 |
} |
| 3372 |
|
| 3373 |
TEST(CredentialsTest, |
| 3374 |
TestAwsExternalAccountCredsFailureUnmatchedEnvironmentId) { |
| 3375 |
auto credential_source = Json::Parse( |
| 3376 |
invalid_aws_external_account_creds_options_credential_source_unmatched_environment_id); |
| 3377 |
GPR_ASSERT(credential_source.ok()); |
| 3378 |
ExternalAccountCredentials::Options options = { |
| 3379 |
"external_account", // type; |
| 3380 |
"audience", // audience; |
| 3381 |
"subject_token_type", // subject_token_type; |
| 3382 |
"", // service_account_impersonation_url; |
| 3383 |
"https://foo.com:5555/token", // token_url; |
| 3384 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3385 |
*credential_source, // credential_source; |
| 3386 |
"quota_project_id", // quota_project_id; |
| 3387 |
"client_id", // client_id; |
| 3388 |
"client_secret", // client_secret; |
| 3389 |
"", // workforce_pool_user_project; |
| 3390 |
}; |
| 3391 |
grpc_error_handle error; |
| 3392 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3393 |
GPR_ASSERT(creds == nullptr); |
| 3394 |
std::string expected_error = "environment_id does not match."; |
| 3395 |
std::string actual_error; |
| 3396 |
GPR_ASSERT(grpc_error_get_str(error, StatusStrProperty::kDescription, |
| 3397 |
&actual_error)); |
| 3398 |
GPR_ASSERT(expected_error == actual_error); |
| 3399 |
} |
| 3400 |
|
| 3401 |
TEST(CredentialsTest, TestAwsExternalAccountCredsFailureMissingRoleName) { |
| 3402 |
ExecCtx exec_ctx; |
| 3403 |
auto credential_source = Json::Parse( |
| 3404 |
invalid_aws_external_account_creds_options_credential_source_missing_role_name); |
| 3405 |
GPR_ASSERT(credential_source.ok()); |
| 3406 |
ExternalAccountCredentials::Options options = { |
| 3407 |
"external_account", // type; |
| 3408 |
"audience", // audience; |
| 3409 |
"subject_token_type", // subject_token_type; |
| 3410 |
"", // service_account_impersonation_url; |
| 3411 |
"https://foo.com:5555/token", // token_url; |
| 3412 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3413 |
*credential_source, // credential_source; |
| 3414 |
"quota_project_id", // quota_project_id; |
| 3415 |
"client_id", // client_id; |
| 3416 |
"client_secret", // client_secret; |
| 3417 |
"", // workforce_pool_user_project; |
| 3418 |
}; |
| 3419 |
grpc_error_handle error; |
| 3420 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3421 |
GPR_ASSERT(creds != nullptr); |
| 3422 |
GPR_ASSERT(error.ok()); |
| 3423 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 3424 |
error = GRPC_ERROR_CREATE("Missing role name when retrieving signing keys."); |
| 3425 |
grpc_error_handle expected_error = GRPC_ERROR_CREATE_REFERENCING( |
| 3426 |
"Error occurred when fetching oauth2 token.", &error, 1); |
| 3427 |
auto state = RequestMetadataState::NewInstance(expected_error, {}); |
| 3428 |
HttpRequest::SetOverride(aws_external_account_creds_httpcli_get_success, |
| 3429 |
aws_external_account_creds_httpcli_post_success, |
| 3430 |
httpcli_put_should_not_be_called); |
| 3431 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 3432 |
kTestPath); |
| 3433 |
ExecCtx::Get()->Flush(); |
| 3434 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 3435 |
} |
| 3436 |
|
| 3437 |
TEST(CredentialsTest, |
| 3438 |
TestAwsExternalAccountCredsFailureInvalidRegionalCredVerificationUrl) { |
| 3439 |
ExecCtx exec_ctx; |
| 3440 |
auto credential_source = Json::Parse( |
| 3441 |
invalid_aws_external_account_creds_options_credential_source_invalid_regional_cred_verification_url); |
| 3442 |
GPR_ASSERT(credential_source.ok()); |
| 3443 |
ExternalAccountCredentials::Options options = { |
| 3444 |
"external_account", // type; |
| 3445 |
"audience", // audience; |
| 3446 |
"subject_token_type", // subject_token_type; |
| 3447 |
"", // service_account_impersonation_url; |
| 3448 |
"https://foo.com:5555/token", // token_url; |
| 3449 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3450 |
*credential_source, // credential_source; |
| 3451 |
"quota_project_id", // quota_project_id; |
| 3452 |
"client_id", // client_id; |
| 3453 |
"client_secret", // client_secret; |
| 3454 |
"", // workforce_pool_user_project; |
| 3455 |
}; |
| 3456 |
grpc_error_handle error; |
| 3457 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3458 |
GPR_ASSERT(creds != nullptr); |
| 3459 |
GPR_ASSERT(error.ok()); |
| 3460 |
GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY); |
| 3461 |
error = GRPC_ERROR_CREATE("Creating aws request signer failed."); |
| 3462 |
grpc_error_handle expected_error = GRPC_ERROR_CREATE_REFERENCING( |
| 3463 |
"Error occurred when fetching oauth2 token.", &error, 1); |
| 3464 |
auto state = RequestMetadataState::NewInstance(expected_error, {}); |
| 3465 |
HttpRequest::SetOverride(aws_external_account_creds_httpcli_get_success, |
| 3466 |
aws_external_account_creds_httpcli_post_success, |
| 3467 |
httpcli_put_should_not_be_called); |
| 3468 |
state->RunRequestMetadataTest(creds.get(), kTestUrlScheme, kTestAuthority, |
| 3469 |
kTestPath); |
| 3470 |
ExecCtx::Get()->Flush(); |
| 3471 |
HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 3472 |
} |
| 3473 |
|
| 3474 |
TEST(CredentialsTest, TestAwsExternalAccountCredsFailureInvalidUrlHost) { |
| 3475 |
auto credential_source = Json::Parse( |
| 3476 |
invalid_aws_external_account_creds_options_credential_source_invalid_url_host); |
| 3477 |
GPR_ASSERT(credential_source.ok()); |
| 3478 |
ExternalAccountCredentials::Options options = { |
| 3479 |
"external_account", // type; |
| 3480 |
"audience", // audience; |
| 3481 |
"subject_token_type", // subject_token_type; |
| 3482 |
"", // service_account_impersonation_url; |
| 3483 |
"https://foo.com:5555/token", // token_url; |
| 3484 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3485 |
*credential_source, // credential_source; |
| 3486 |
"quota_project_id", // quota_project_id; |
| 3487 |
"client_id", // client_id; |
| 3488 |
"client_secret", // client_secret; |
| 3489 |
"", // workforce_pool_user_project; |
| 3490 |
}; |
| 3491 |
grpc_error_handle error; |
| 3492 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3493 |
GPR_ASSERT(creds == nullptr); |
| 3494 |
std::string expected_error = |
| 3495 |
"Invalid host for url field, expecting 169.254.169.254 or fd00:ec2::254."; |
| 3496 |
std::string actual_error; |
| 3497 |
GPR_ASSERT(grpc_error_get_str(error, StatusStrProperty::kDescription, |
| 3498 |
&actual_error)); |
| 3499 |
GPR_ASSERT(expected_error == actual_error); |
| 3500 |
} |
| 3501 |
|
| 3502 |
TEST(CredentialsTest, TestAwsExternalAccountCredsFailureInvalidRegionUrlHost) { |
| 3503 |
auto credential_source = Json::Parse( |
| 3504 |
invalid_aws_external_account_creds_options_credential_source_invalid_region_url_host); |
| 3505 |
GPR_ASSERT(credential_source.ok()); |
| 3506 |
ExternalAccountCredentials::Options options = { |
| 3507 |
"external_account", // type; |
| 3508 |
"audience", // audience; |
| 3509 |
"subject_token_type", // subject_token_type; |
| 3510 |
"", // service_account_impersonation_url; |
| 3511 |
"https://foo.com:5555/token", // token_url; |
| 3512 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3513 |
*credential_source, // credential_source; |
| 3514 |
"quota_project_id", // quota_project_id; |
| 3515 |
"client_id", // client_id; |
| 3516 |
"client_secret", // client_secret; |
| 3517 |
"", // workforce_pool_user_project; |
| 3518 |
}; |
| 3519 |
grpc_error_handle error; |
| 3520 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3521 |
GPR_ASSERT(creds == nullptr); |
| 3522 |
std::string expected_error = |
| 3523 |
"Invalid host for region_url field, expecting 169.254.169.254 or " |
| 3524 |
"fd00:ec2::254."; |
| 3525 |
std::string actual_error; |
| 3526 |
GPR_ASSERT(grpc_error_get_str(error, StatusStrProperty::kDescription, |
| 3527 |
&actual_error)); |
| 3528 |
GPR_ASSERT(expected_error == actual_error); |
| 3529 |
} |
| 3530 |
|
| 3531 |
TEST(CredentialsTest, TestAwsExternalAccountCredsFailureInvalidImdsv2UrlHost) { |
| 3532 |
auto credential_source = Json::Parse( |
| 3533 |
invalid_aws_external_account_creds_options_credential_source_invalid_imdsv2_url_host); |
| 3534 |
GPR_ASSERT(credential_source.ok()); |
| 3535 |
ExternalAccountCredentials::Options options = { |
| 3536 |
"external_account", // type; |
| 3537 |
"audience", // audience; |
| 3538 |
"subject_token_type", // subject_token_type; |
| 3539 |
"", // service_account_impersonation_url; |
| 3540 |
"https://foo.com:5555/token", // token_url; |
| 3541 |
"https://foo.com:5555/token_info", // token_info_url; |
| 3542 |
*credential_source, // credential_source; |
| 3543 |
"quota_project_id", // quota_project_id; |
| 3544 |
"client_id", // client_id; |
| 3545 |
"client_secret", // client_secret; |
| 3546 |
"", // workforce_pool_user_project; |
| 3547 |
}; |
| 3548 |
grpc_error_handle error; |
| 3549 |
auto creds = AwsExternalAccountCredentials::Create(options, {}, &error); |
| 3550 |
GPR_ASSERT(creds == nullptr); |
| 3551 |
std::string expected_error = |
| 3552 |
"Invalid host for imdsv2_session_token_url field, expecting " |
| 3553 |
"169.254.169.254 or fd00:ec2::254."; |
| 3554 |
std::string actual_error; |
| 3555 |
GPR_ASSERT(grpc_error_get_str(error, StatusStrProperty::kDescription, |
| 3556 |
&actual_error)); |
| 3557 |
GPR_ASSERT(expected_error == actual_error); |
| 3558 |
} |
| 3559 |
|
| 3560 |
TEST(CredentialsTest, TestExternalAccountCredentialsCreateSuccess) { |
| 3561 |
// url credentials |
| 3562 |
const char* url_options_string = |
| 3563 |
"{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_" |
| 3564 |
"token_type\":\"subject_token_type\",\"service_account_impersonation_" |
| 3565 |
"url\":\"service_account_impersonation_url\",\"token_url\":\"https://" |
| 3566 |
"foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/" |
| 3567 |
"token_info\",\"credential_source\":{\"url\":\"https://foo.com:5555/" |
| 3568 |
"generate_subject_token_format_json\",\"headers\":{\"Metadata-Flavor\":" |
| 3569 |
"\"Google\"},\"format\":{\"type\":\"json\",\"subject_token_field_name\":" |
| 3570 |
"\"access_token\"}},\"quota_project_id\":\"quota_" |
| 3571 |
"project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_" |
| 3572 |
"secret\"}"; |
| 3573 |
const char* url_scopes_string = "scope1,scope2"; |
| 3574 |
grpc_call_credentials* url_creds = grpc_external_account_credentials_create( |
| 3575 |
url_options_string, url_scopes_string); |
| 3576 |
GPR_ASSERT(url_creds != nullptr); |
| 3577 |
url_creds->Unref(); |
| 3578 |
// file credentials |
| 3579 |
const char* file_options_string = |
| 3580 |
"{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_" |
| 3581 |
"token_type\":\"subject_token_type\",\"service_account_impersonation_" |
| 3582 |
"url\":\"service_account_impersonation_url\",\"token_url\":\"https://" |
| 3583 |
"foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/" |
| 3584 |
"token_info\",\"credential_source\":{\"file\":\"credentials_file_path\"}," |
| 3585 |
"\"quota_project_id\":\"quota_" |
| 3586 |
"project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_" |
| 3587 |
"secret\"}"; |
| 3588 |
const char* file_scopes_string = "scope1,scope2"; |
| 3589 |
grpc_call_credentials* file_creds = grpc_external_account_credentials_create( |
| 3590 |
file_options_string, file_scopes_string); |
| 3591 |
GPR_ASSERT(file_creds != nullptr); |
| 3592 |
file_creds->Unref(); |
| 3593 |
// aws credentials |
| 3594 |
const char* aws_options_string = |
| 3595 |
"{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_" |
| 3596 |
"token_type\":\"subject_token_type\",\"service_account_impersonation_" |
| 3597 |
"url\":\"service_account_impersonation_url\",\"token_url\":\"https://" |
| 3598 |
"foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/" |
| 3599 |
"token_info\",\"credential_source\":{\"environment_id\":\"aws1\"," |
| 3600 |
"\"region_url\":\"https://169.254.169.254:5555/" |
| 3601 |
"region_url\",\"url\":\"https://" |
| 3602 |
"169.254.169.254:5555/url\",\"regional_cred_verification_url\":\"https://" |
| 3603 |
"foo.com:5555/regional_cred_verification_url_{region}\"}," |
| 3604 |
"\"quota_project_id\":\"quota_" |
| 3605 |
"project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_" |
| 3606 |
"secret\"}"; |
| 3607 |
const char* aws_scopes_string = "scope1,scope2"; |
| 3608 |
grpc_call_credentials* aws_creds = grpc_external_account_credentials_create( |
| 3609 |
aws_options_string, aws_scopes_string); |
| 3610 |
GPR_ASSERT(aws_creds != nullptr); |
| 3611 |
aws_creds->Unref(); |
| 3612 |
} |
| 3613 |
|
| 3614 |
TEST(CredentialsTest, |
| 3615 |
TestExternalAccountCredentialsCreateFailureInvalidJsonFormat) { |
| 3616 |
const char* options_string = "invalid_json"; |
| 3617 |
grpc_call_credentials* creds = |
| 3618 |
grpc_external_account_credentials_create(options_string, ""); |
| 3619 |
GPR_ASSERT(creds == nullptr); |
| 3620 |
} |
| 3621 |
|
| 3622 |
TEST(CredentialsTest, |
| 3623 |
TestExternalAccountCredentialsCreateFailureInvalidOptionsFormat) { |
| 3624 |
const char* options_string = "{\"random_key\":\"random_value\"}"; |
| 3625 |
grpc_call_credentials* creds = |
| 3626 |
grpc_external_account_credentials_create(options_string, ""); |
| 3627 |
GPR_ASSERT(creds == nullptr); |
| 3628 |
} |
| 3629 |
|
| 3630 |
TEST( |
| 3631 |
CredentialsTest, |
| 3632 |
TestExternalAccountCredentialsCreateFailureInvalidOptionsCredentialSource) { |
| 3633 |
const char* options_string = |
| 3634 |
"{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_" |
| 3635 |
"token_type\":\"subject_token_type\",\"service_account_impersonation_" |
| 3636 |
"url\":\"service_account_impersonation_url\",\"token_url\":\"https://" |
| 3637 |
"foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/" |
| 3638 |
"token_info\",\"credential_source\":{\"random_key\":\"random_value\"}," |
| 3639 |
"\"quota_project_id\":\"quota_" |
| 3640 |
"project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_" |
| 3641 |
"secret\"}"; |
| 3642 |
grpc_call_credentials* creds = |
| 3643 |
grpc_external_account_credentials_create(options_string, ""); |
| 3644 |
GPR_ASSERT(creds == nullptr); |
| 3645 |
} |
| 3646 |
|
| 3647 |
TEST(CredentialsTest, |
| 3648 |
TestExternalAccountCredentialsCreateSuccessWorkforcePool) { |
| 3649 |
const char* url_options_string = |
| 3650 |
"{\"type\":\"external_account\",\"audience\":\"//iam.googleapis.com/" |
| 3651 |
"locations/location/workforcePools/pool/providers/provider\",\"subject_" |
| 3652 |
"token_type\":\"subject_token_type\",\"service_account_impersonation_" |
| 3653 |
"url\":\"service_account_impersonation_url\",\"token_url\":\"https://" |
| 3654 |
"foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/" |
| 3655 |
"token_info\",\"credential_source\":{\"url\":\"https://foo.com:5555/" |
| 3656 |
"generate_subject_token_format_json\",\"headers\":{\"Metadata-Flavor\":" |
| 3657 |
"\"Google\"},\"format\":{\"type\":\"json\",\"subject_token_field_name\":" |
| 3658 |
"\"access_token\"}},\"quota_project_id\":\"quota_" |
| 3659 |
"project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_" |
| 3660 |
"secret\",\"workforce_pool_user_project\":\"workforce_pool_user_" |
| 3661 |
"project\"}"; |
| 3662 |
const char* url_scopes_string = "scope1,scope2"; |
| 3663 |
grpc_call_credentials* url_creds = grpc_external_account_credentials_create( |
| 3664 |
url_options_string, url_scopes_string); |
| 3665 |
GPR_ASSERT(url_creds != nullptr); |
| 3666 |
url_creds->Unref(); |
| 3667 |
} |
| 3668 |
|
| 3669 |
TEST(CredentialsTest, |
| 3670 |
TestExternalAccountCredentialsCreateFailureInvalidWorkforcePoolAudience) { |
| 3671 |
const char* url_options_string = |
| 3672 |
"{\"type\":\"external_account\",\"audience\":\"invalid_workforce_pool_" |
| 3673 |
"audience\",\"subject_" |
| 3674 |
"token_type\":\"subject_token_type\",\"service_account_impersonation_" |
| 3675 |
"url\":\"service_account_impersonation_url\",\"token_url\":\"https://" |
| 3676 |
"foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/" |
| 3677 |
"token_info\",\"credential_source\":{\"url\":\"https://foo.com:5555/" |
| 3678 |
"generate_subject_token_format_json\",\"headers\":{\"Metadata-Flavor\":" |
| 3679 |
"\"Google\"},\"format\":{\"type\":\"json\",\"subject_token_field_name\":" |
| 3680 |
"\"access_token\"}},\"quota_project_id\":\"quota_" |
| 3681 |
"project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_" |
| 3682 |
"secret\",\"workforce_pool_user_project\":\"workforce_pool_user_" |
| 3683 |
"project\"}"; |
| 3684 |
const char* url_scopes_string = "scope1,scope2"; |
| 3685 |
grpc_call_credentials* url_creds = grpc_external_account_credentials_create( |
| 3686 |
url_options_string, url_scopes_string); |
| 3687 |
GPR_ASSERT(url_creds == nullptr); |
| 3688 |
} |
| 3689 |
|
| 3690 |
TEST(CredentialsTest, TestInsecureCredentialsCompareSuccess) { |
| 3691 |
auto insecure_creds_1 = grpc_insecure_credentials_create(); |
| 3692 |
auto insecure_creds_2 = grpc_insecure_credentials_create(); |
| 3693 |
ASSERT_EQ(insecure_creds_1->cmp(insecure_creds_2), 0); |
| 3694 |
grpc_arg arg_1 = grpc_channel_credentials_to_arg(insecure_creds_1); |
| 3695 |
grpc_channel_args args_1 = {1, &arg_1}; |
| 3696 |
grpc_arg arg_2 = grpc_channel_credentials_to_arg(insecure_creds_2); |
| 3697 |
grpc_channel_args args_2 = {1, &arg_2}; |
| 3698 |
EXPECT_EQ(grpc_channel_args_compare(&args_1, &args_2), 0); |
| 3699 |
grpc_channel_credentials_release(insecure_creds_1); |
| 3700 |
grpc_channel_credentials_release(insecure_creds_2); |
| 3701 |
} |
| 3702 |
|
| 3703 |
TEST(CredentialsTest, TestInsecureCredentialsCompareFailure) { |
| 3704 |
auto* insecure_creds = grpc_insecure_credentials_create(); |
| 3705 |
auto* fake_creds = grpc_fake_transport_security_credentials_create(); |
| 3706 |
ASSERT_NE(insecure_creds->cmp(fake_creds), 0); |
| 3707 |
ASSERT_NE(fake_creds->cmp(insecure_creds), 0); |
| 3708 |
grpc_arg arg_1 = grpc_channel_credentials_to_arg(insecure_creds); |
| 3709 |
grpc_channel_args args_1 = {1, &arg_1}; |
| 3710 |
grpc_arg arg_2 = grpc_channel_credentials_to_arg(fake_creds); |
| 3711 |
grpc_channel_args args_2 = {1, &arg_2}; |
| 3712 |
EXPECT_NE(grpc_channel_args_compare(&args_1, &args_2), 0); |
| 3713 |
grpc_channel_credentials_release(fake_creds); |
| 3714 |
grpc_channel_credentials_release(insecure_creds); |
| 3715 |
} |
| 3716 |
|
| 3717 |
TEST(CredentialsTest, TestInsecureCredentialsSingletonCreate) { |
| 3718 |
auto* insecure_creds_1 = grpc_insecure_credentials_create(); |
| 3719 |
auto* insecure_creds_2 = grpc_insecure_credentials_create(); |
| 3720 |
EXPECT_EQ(insecure_creds_1, insecure_creds_2); |
| 3721 |
} |
| 3722 |
|
| 3723 |
TEST(CredentialsTest, TestFakeCallCredentialsCompareSuccess) { |
| 3724 |
auto call_creds = MakeRefCounted<fake_call_creds>(); |
| 3725 |
GPR_ASSERT(call_creds->cmp(call_creds.get()) == 0); |
| 3726 |
} |
| 3727 |
|
| 3728 |
TEST(CredentialsTest, TestFakeCallCredentialsCompareFailure) { |
| 3729 |
auto fake_creds = MakeRefCounted<fake_call_creds>(); |
| 3730 |
auto* md_creds = grpc_md_only_test_credentials_create("key", "value"); |
| 3731 |
GPR_ASSERT(fake_creds->cmp(md_creds) != 0); |
| 3732 |
GPR_ASSERT(md_creds->cmp(fake_creds.get()) != 0); |
| 3733 |
grpc_call_credentials_release(md_creds); |
| 3734 |
} |
| 3735 |
|
| 3736 |
TEST(CredentialsTest, TestHttpRequestSSLCredentialsCompare) { |
| 3737 |
auto creds_1 = CreateHttpRequestSSLCredentials(); |
| 3738 |
auto creds_2 = CreateHttpRequestSSLCredentials(); |
| 3739 |
EXPECT_EQ(creds_1->cmp(creds_2.get()), 0); |
| 3740 |
EXPECT_EQ(creds_2->cmp(creds_1.get()), 0); |
| 3741 |
} |
| 3742 |
|
| 3743 |
TEST(CredentialsTest, TestHttpRequestSSLCredentialsSingleton) { |
| 3744 |
auto creds_1 = CreateHttpRequestSSLCredentials(); |
| 3745 |
auto creds_2 = CreateHttpRequestSSLCredentials(); |
| 3746 |
EXPECT_EQ(creds_1, creds_2); |
| 3747 |
} |
| 3748 |
|
| 3749 |
TEST(CredentialsTest, TestCompositeChannelCredsCompareSuccess) { |
| 3750 |
auto* insecure_creds = grpc_insecure_credentials_create(); |
| 3751 |
auto fake_creds = MakeRefCounted<fake_call_creds>(); |
| 3752 |
auto* composite_creds_1 = grpc_composite_channel_credentials_create( |
| 3753 |
insecure_creds, fake_creds.get(), nullptr); |
| 3754 |
auto* composite_creds_2 = grpc_composite_channel_credentials_create( |
| 3755 |
insecure_creds, fake_creds.get(), nullptr); |
| 3756 |
EXPECT_EQ(composite_creds_1->cmp(composite_creds_2), 0); |
| 3757 |
EXPECT_EQ(composite_creds_2->cmp(composite_creds_1), 0); |
| 3758 |
grpc_channel_credentials_release(insecure_creds); |
| 3759 |
grpc_channel_credentials_release(composite_creds_1); |
| 3760 |
grpc_channel_credentials_release(composite_creds_2); |
| 3761 |
} |
| 3762 |
|
| 3763 |
TEST(CredentialsTest, |
| 3764 |
TestCompositeChannelCredsCompareFailureDifferentChannelCreds) { |
| 3765 |
auto* insecure_creds = grpc_insecure_credentials_create(); |
| 3766 |
auto* fake_channel_creds = grpc_fake_transport_security_credentials_create(); |
| 3767 |
auto fake_creds = MakeRefCounted<fake_call_creds>(); |
| 3768 |
auto* composite_creds_1 = grpc_composite_channel_credentials_create( |
| 3769 |
insecure_creds, fake_creds.get(), nullptr); |
| 3770 |
auto* composite_creds_2 = grpc_composite_channel_credentials_create( |
| 3771 |
fake_channel_creds, fake_creds.get(), nullptr); |
| 3772 |
EXPECT_NE(composite_creds_1->cmp(composite_creds_2), 0); |
| 3773 |
EXPECT_NE(composite_creds_2->cmp(composite_creds_1), 0); |
| 3774 |
grpc_channel_credentials_release(insecure_creds); |
| 3775 |
grpc_channel_credentials_release(fake_channel_creds); |
| 3776 |
grpc_channel_credentials_release(composite_creds_1); |
| 3777 |
grpc_channel_credentials_release(composite_creds_2); |
| 3778 |
} |
| 3779 |
|
| 3780 |
TEST(CredentialsTest, |
| 3781 |
TestCompositeChannelCredsCompareFailureDifferentCallCreds) { |
| 3782 |
auto* insecure_creds = grpc_insecure_credentials_create(); |
| 3783 |
auto fake_creds = MakeRefCounted<fake_call_creds>(); |
| 3784 |
auto* md_creds = grpc_md_only_test_credentials_create("key", "value"); |
| 3785 |
auto* composite_creds_1 = grpc_composite_channel_credentials_create( |
| 3786 |
insecure_creds, fake_creds.get(), nullptr); |
| 3787 |
auto* composite_creds_2 = grpc_composite_channel_credentials_create( |
| 3788 |
insecure_creds, md_creds, nullptr); |
| 3789 |
EXPECT_NE(composite_creds_1->cmp(composite_creds_2), 0); |
| 3790 |
EXPECT_NE(composite_creds_2->cmp(composite_creds_1), 0); |
| 3791 |
grpc_channel_credentials_release(insecure_creds); |
| 3792 |
grpc_call_credentials_release(md_creds); |
| 3793 |
grpc_channel_credentials_release(composite_creds_1); |
| 3794 |
grpc_channel_credentials_release(composite_creds_2); |
| 3795 |
} |
| 3796 |
|
| 3797 |
TEST(CredentialsTest, TestTlsCredentialsCompareSuccess) { |
| 3798 |
auto* tls_creds_1 = |
| 3799 |
grpc_tls_credentials_create(grpc_tls_credentials_options_create()); |
| 3800 |
auto* tls_creds_2 = |
| 3801 |
grpc_tls_credentials_create(grpc_tls_credentials_options_create()); |
| 3802 |
EXPECT_EQ(tls_creds_1->cmp(tls_creds_2), 0); |
| 3803 |
EXPECT_EQ(tls_creds_2->cmp(tls_creds_1), 0); |
| 3804 |
grpc_channel_credentials_release(tls_creds_1); |
| 3805 |
grpc_channel_credentials_release(tls_creds_2); |
| 3806 |
} |
| 3807 |
|
| 3808 |
TEST(CredentialsTest, TestTlsCredentialsWithVerifierCompareSuccess) { |
| 3809 |
auto* options_1 = grpc_tls_credentials_options_create(); |
| 3810 |
options_1->set_certificate_verifier( |
| 3811 |
MakeRefCounted<HostNameCertificateVerifier>()); |
| 3812 |
auto* tls_creds_1 = grpc_tls_credentials_create(options_1); |
| 3813 |
auto* options_2 = grpc_tls_credentials_options_create(); |
| 3814 |
options_2->set_certificate_verifier( |
| 3815 |
MakeRefCounted<HostNameCertificateVerifier>()); |
| 3816 |
auto* tls_creds_2 = grpc_tls_credentials_create(options_2); |
| 3817 |
EXPECT_EQ(tls_creds_1->cmp(tls_creds_2), 0); |
| 3818 |
EXPECT_EQ(tls_creds_2->cmp(tls_creds_1), 0); |
| 3819 |
grpc_channel_credentials_release(tls_creds_1); |
| 3820 |
grpc_channel_credentials_release(tls_creds_2); |
| 3821 |
} |
| 3822 |
|
| 3823 |
TEST(CredentialsTest, TestTlsCredentialsCompareFailure) { |
| 3824 |
auto* options_1 = grpc_tls_credentials_options_create(); |
| 3825 |
options_1->set_check_call_host(true); |
| 3826 |
auto* tls_creds_1 = grpc_tls_credentials_create(options_1); |
| 3827 |
auto* options_2 = grpc_tls_credentials_options_create(); |
| 3828 |
options_2->set_check_call_host(false); |
| 3829 |
auto* tls_creds_2 = grpc_tls_credentials_create(options_2); |
| 3830 |
EXPECT_NE(tls_creds_1->cmp(tls_creds_2), 0); |
| 3831 |
EXPECT_NE(tls_creds_2->cmp(tls_creds_1), 0); |
| 3832 |
grpc_channel_credentials_release(tls_creds_1); |
| 3833 |
grpc_channel_credentials_release(tls_creds_2); |
| 3834 |
} |
| 3835 |
|
| 3836 |
TEST(CredentialsTest, TestTlsCredentialsWithVerifierCompareFailure) { |
| 3837 |
auto* options_1 = grpc_tls_credentials_options_create(); |
| 3838 |
options_1->set_certificate_verifier( |
| 3839 |
MakeRefCounted<HostNameCertificateVerifier>()); |
| 3840 |
auto* tls_creds_1 = grpc_tls_credentials_create(options_1); |
| 3841 |
auto* options_2 = grpc_tls_credentials_options_create(); |
| 3842 |
grpc_tls_certificate_verifier_external verifier = {nullptr, nullptr, nullptr, |
| 3843 |
nullptr}; |
| 3844 |
options_2->set_certificate_verifier( |
| 3845 |
MakeRefCounted<ExternalCertificateVerifier>(&verifier)); |
| 3846 |
auto* tls_creds_2 = grpc_tls_credentials_create(options_2); |
| 3847 |
EXPECT_NE(tls_creds_1->cmp(tls_creds_2), 0); |
| 3848 |
EXPECT_NE(tls_creds_2->cmp(tls_creds_1), 0); |
| 3849 |
grpc_channel_credentials_release(tls_creds_1); |
| 3850 |
grpc_channel_credentials_release(tls_creds_2); |
| 3851 |
} |
| 3852 |
|
| 3853 |
TEST(CredentialsTest, TestXdsCredentialsCompareSucces) { |
| 3854 |
auto* insecure_creds = grpc_insecure_credentials_create(); |
| 3855 |
auto* xds_creds_1 = grpc_xds_credentials_create(insecure_creds); |
| 3856 |
auto* xds_creds_2 = grpc_xds_credentials_create(insecure_creds); |
| 3857 |
EXPECT_EQ(xds_creds_1->cmp(xds_creds_2), 0); |
| 3858 |
EXPECT_EQ(xds_creds_2->cmp(xds_creds_1), 0); |
| 3859 |
grpc_channel_credentials_release(insecure_creds); |
| 3860 |
grpc_channel_credentials_release(xds_creds_1); |
| 3861 |
grpc_channel_credentials_release(xds_creds_2); |
| 3862 |
} |
| 3863 |
|
| 3864 |
TEST(CredentialsTest, TestXdsCredentialsCompareFailure) { |
| 3865 |
auto* insecure_creds = grpc_insecure_credentials_create(); |
| 3866 |
auto* fake_creds = grpc_fake_transport_security_credentials_create(); |
| 3867 |
auto* xds_creds_1 = grpc_xds_credentials_create(insecure_creds); |
| 3868 |
auto* xds_creds_2 = grpc_xds_credentials_create(fake_creds); |
| 3869 |
EXPECT_NE(xds_creds_1->cmp(xds_creds_2), 0); |
| 3870 |
EXPECT_NE(xds_creds_2->cmp(xds_creds_1), 0); |
| 3871 |
grpc_channel_credentials_release(insecure_creds); |
| 3872 |
grpc_channel_credentials_release(fake_creds); |
| 3873 |
grpc_channel_credentials_release(xds_creds_1); |
| 3874 |
grpc_channel_credentials_release(xds_creds_2); |
| 3875 |
} |
| 3876 |
|
| 3877 |
} // namespace |
| 3878 |
} // namespace grpc_core |
| 3879 |
|
| 3880 |
int main(int argc, char** argv) { |
| 3881 |
testing::InitGoogleTest(&argc, argv); |
| 3882 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 3883 |
grpc_init(); |
| 3884 |
auto result = RUN_ALL_TESTS(); |
| 3885 |
grpc_shutdown(); |
| 3886 |
return result; |
| 3887 |
} |