credentials_test.cc 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  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. #include <grpc/support/port_platform.h>
  19. #include "src/core/lib/security/credentials/credentials.h"
  20. #include <openssl/rsa.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <grpc/grpc_security.h>
  24. #include <grpc/slice.h>
  25. #include <grpc/support/alloc.h>
  26. #include <grpc/support/log.h>
  27. #include <grpc/support/string_util.h>
  28. #include <grpc/support/time.h>
  29. #include "src/core/lib/gpr/env.h"
  30. #include "src/core/lib/gpr/string.h"
  31. #include "src/core/lib/gpr/tmpfile.h"
  32. #include "src/core/lib/gprpp/host_port.h"
  33. #include "src/core/lib/http/httpcli.h"
  34. #include "src/core/lib/iomgr/error.h"
  35. #include "src/core/lib/security/credentials/composite/composite_credentials.h"
  36. #include "src/core/lib/security/credentials/fake/fake_credentials.h"
  37. #include "src/core/lib/security/credentials/google_default/google_default_credentials.h"
  38. #include "src/core/lib/security/credentials/jwt/jwt_credentials.h"
  39. #include "src/core/lib/security/credentials/oauth2/oauth2_credentials.h"
  40. #include "src/core/lib/security/transport/auth_filters.h"
  41. #include "src/core/lib/uri/uri_parser.h"
  42. #include "test/core/util/test_config.h"
  43. using grpc_core::internal::grpc_flush_cached_google_default_credentials;
  44. using grpc_core::internal::set_gce_tenancy_checker_for_testing;
  45. /* -- Constants. -- */
  46. static const char test_google_iam_authorization_token[] = "blahblahblhahb";
  47. static const char test_google_iam_authority_selector[] = "respectmyauthoritah";
  48. static const char test_oauth2_bearer_token[] =
  49. "Bearer blaaslkdjfaslkdfasdsfasf";
  50. /* This JSON key was generated with the GCE console and revoked immediately.
  51. The identifiers have been changed as well.
  52. Maximum size for a string literal is 509 chars in C89, yay! */
  53. static const char test_json_key_str_part1[] =
  54. "{ \"private_key\": \"-----BEGIN PRIVATE KEY-----"
  55. "\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJE"
  56. "qg"
  57. "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/"
  58. "rWBQvS4hle4LfijkP3J5BG+"
  59. "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+"
  60. "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/"
  61. "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/"
  62. "8HpCqFYM9V8f34SBWfD4fRFT+n/"
  63. "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+";
  64. static const char test_json_key_str_part2[] =
  65. "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+"
  66. "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/"
  67. "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDA"
  68. "G"
  69. "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6Hz"
  70. "A"
  71. "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+"
  72. "6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/"
  73. "5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZ"
  74. "Y"
  75. "Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", ";
  76. static const char test_json_key_str_part3[] =
  77. "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", "
  78. "\"client_email\": "
  79. "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount."
  80. "com\", \"client_id\": "
  81. "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent."
  82. "com\", \"type\": \"service_account\" }";
  83. /* Test refresh token. */
  84. static const char test_refresh_token_str[] =
  85. "{ \"client_id\": \"32555999999.apps.googleusercontent.com\","
  86. " \"client_secret\": \"EmssLNjJy1332hD4KFsecret\","
  87. " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\","
  88. " \"type\": \"authorized_user\"}";
  89. static const char valid_oauth2_json_response[] =
  90. "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\","
  91. " \"expires_in\":3599, "
  92. " \"token_type\":\"Bearer\"}";
  93. static const char valid_sts_json_response[] =
  94. "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\","
  95. " \"expires_in\":3599, "
  96. " \"issued_token_type\":\"urn:ietf:params:oauth:token-type:access_token\", "
  97. " \"token_type\":\"Bearer\"}";
  98. static const char test_scope[] = "perm1 perm2";
  99. static const char test_signed_jwt[] =
  100. "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImY0OTRkN2M1YWU2MGRmOTcyNmM4YW"
  101. "U0MDcyZTViYTdmZDkwODg2YzcifQ";
  102. static const char test_signed_jwt_token_type[] =
  103. "urn:ietf:params:oauth:token-type:id_token";
  104. static const char test_signed_jwt2[] =
  105. "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImY0OTRkN2M1YWU2MGRmOTcyNmM5YW"
  106. "U2MDcyZTViYTdnZDkwODg5YzcifQ";
  107. static const char test_signed_jwt_token_type2[] =
  108. "urn:ietf:params:oauth:token-type:jwt";
  109. static const char test_signed_jwt_path_prefix[] = "test_sign_jwt";
  110. static const char test_service_url[] = "https://foo.com/foo.v1";
  111. static const char other_test_service_url[] = "https://bar.com/bar.v1";
  112. static const char test_sts_endpoint_url[] =
  113. "https://foo.com:5555/v1/token-exchange";
  114. static const char test_method[] = "ThisIsNotAMethod";
  115. /* -- Global state flags. -- */
  116. static bool g_test_is_on_gce = false;
  117. static bool g_test_gce_tenancy_checker_called = false;
  118. /* -- Utils. -- */
  119. static char* test_json_key_str(void) {
  120. size_t result_len = strlen(test_json_key_str_part1) +
  121. strlen(test_json_key_str_part2) +
  122. strlen(test_json_key_str_part3);
  123. char* result = static_cast<char*>(gpr_malloc(result_len + 1));
  124. char* current = result;
  125. strcpy(result, test_json_key_str_part1);
  126. current += strlen(test_json_key_str_part1);
  127. strcpy(current, test_json_key_str_part2);
  128. current += strlen(test_json_key_str_part2);
  129. strcpy(current, test_json_key_str_part3);
  130. return result;
  131. }
  132. static grpc_httpcli_response http_response(int status, const char* body) {
  133. grpc_httpcli_response response;
  134. response = {};
  135. response.status = status;
  136. response.body = gpr_strdup(const_cast<char*>(body));
  137. response.body_length = strlen(body);
  138. return response;
  139. }
  140. /* -- Tests. -- */
  141. static void test_empty_md_array(void) {
  142. grpc_core::ExecCtx exec_ctx;
  143. grpc_credentials_mdelem_array md_array;
  144. md_array = {};
  145. GPR_ASSERT(md_array.md == nullptr);
  146. GPR_ASSERT(md_array.size == 0);
  147. grpc_credentials_mdelem_array_destroy(&md_array);
  148. }
  149. static void test_add_to_empty_md_array(void) {
  150. grpc_core::ExecCtx exec_ctx;
  151. grpc_credentials_mdelem_array md_array;
  152. md_array = {};
  153. const char* key = "hello";
  154. const char* value = "there blah blah blah blah blah blah blah";
  155. grpc_mdelem md = grpc_mdelem_from_slices(
  156. grpc_slice_from_copied_string(key), grpc_slice_from_copied_string(value));
  157. grpc_credentials_mdelem_array_add(&md_array, md);
  158. GPR_ASSERT(md_array.size == 1);
  159. GPR_ASSERT(grpc_mdelem_eq(md, md_array.md[0]));
  160. GRPC_MDELEM_UNREF(md);
  161. grpc_credentials_mdelem_array_destroy(&md_array);
  162. }
  163. static void test_add_abunch_to_md_array(void) {
  164. grpc_core::ExecCtx exec_ctx;
  165. grpc_credentials_mdelem_array md_array;
  166. md_array = {};
  167. const char* key = "hello";
  168. const char* value = "there blah blah blah blah blah blah blah";
  169. grpc_mdelem md = grpc_mdelem_from_slices(
  170. grpc_slice_from_copied_string(key), grpc_slice_from_copied_string(value));
  171. size_t num_entries = 1000;
  172. for (size_t i = 0; i < num_entries; ++i) {
  173. grpc_credentials_mdelem_array_add(&md_array, md);
  174. }
  175. for (size_t i = 0; i < num_entries; ++i) {
  176. GPR_ASSERT(grpc_mdelem_eq(md_array.md[i], md));
  177. }
  178. GRPC_MDELEM_UNREF(md);
  179. grpc_credentials_mdelem_array_destroy(&md_array);
  180. }
  181. static void test_oauth2_token_fetcher_creds_parsing_ok(void) {
  182. grpc_core::ExecCtx exec_ctx;
  183. grpc_mdelem token_md = GRPC_MDNULL;
  184. grpc_millis token_lifetime;
  185. grpc_httpcli_response response =
  186. http_response(200, valid_oauth2_json_response);
  187. GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
  188. &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_OK);
  189. GPR_ASSERT(token_lifetime == 3599 * GPR_MS_PER_SEC);
  190. GPR_ASSERT(grpc_slice_str_cmp(GRPC_MDKEY(token_md), "authorization") == 0);
  191. GPR_ASSERT(grpc_slice_str_cmp(GRPC_MDVALUE(token_md),
  192. "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_") ==
  193. 0);
  194. GRPC_MDELEM_UNREF(token_md);
  195. grpc_http_response_destroy(&response);
  196. }
  197. static void test_oauth2_token_fetcher_creds_parsing_bad_http_status(void) {
  198. grpc_core::ExecCtx exec_ctx;
  199. grpc_mdelem token_md = GRPC_MDNULL;
  200. grpc_millis token_lifetime;
  201. grpc_httpcli_response response =
  202. http_response(401, valid_oauth2_json_response);
  203. GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
  204. &response, &token_md, &token_lifetime) ==
  205. GRPC_CREDENTIALS_ERROR);
  206. grpc_http_response_destroy(&response);
  207. }
  208. static void test_oauth2_token_fetcher_creds_parsing_empty_http_body(void) {
  209. grpc_core::ExecCtx exec_ctx;
  210. grpc_mdelem token_md = GRPC_MDNULL;
  211. grpc_millis token_lifetime;
  212. grpc_httpcli_response response = http_response(200, "");
  213. GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
  214. &response, &token_md, &token_lifetime) ==
  215. GRPC_CREDENTIALS_ERROR);
  216. grpc_http_response_destroy(&response);
  217. }
  218. static void test_oauth2_token_fetcher_creds_parsing_invalid_json(void) {
  219. grpc_core::ExecCtx exec_ctx;
  220. grpc_mdelem token_md = GRPC_MDNULL;
  221. grpc_millis token_lifetime;
  222. grpc_httpcli_response response =
  223. http_response(200,
  224. "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\","
  225. " \"expires_in\":3599, "
  226. " \"token_type\":\"Bearer\"");
  227. GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
  228. &response, &token_md, &token_lifetime) ==
  229. GRPC_CREDENTIALS_ERROR);
  230. grpc_http_response_destroy(&response);
  231. }
  232. static void test_oauth2_token_fetcher_creds_parsing_missing_token(void) {
  233. grpc_core::ExecCtx exec_ctx;
  234. grpc_mdelem token_md = GRPC_MDNULL;
  235. grpc_millis token_lifetime;
  236. grpc_httpcli_response response = http_response(200,
  237. "{"
  238. " \"expires_in\":3599, "
  239. " \"token_type\":\"Bearer\"}");
  240. GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
  241. &response, &token_md, &token_lifetime) ==
  242. GRPC_CREDENTIALS_ERROR);
  243. grpc_http_response_destroy(&response);
  244. }
  245. static void test_oauth2_token_fetcher_creds_parsing_missing_token_type(void) {
  246. grpc_core::ExecCtx exec_ctx;
  247. grpc_mdelem token_md = GRPC_MDNULL;
  248. grpc_millis token_lifetime;
  249. grpc_httpcli_response response =
  250. http_response(200,
  251. "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\","
  252. " \"expires_in\":3599, "
  253. "}");
  254. GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
  255. &response, &token_md, &token_lifetime) ==
  256. GRPC_CREDENTIALS_ERROR);
  257. grpc_http_response_destroy(&response);
  258. }
  259. static void test_oauth2_token_fetcher_creds_parsing_missing_token_lifetime(
  260. void) {
  261. grpc_core::ExecCtx exec_ctx;
  262. grpc_mdelem token_md = GRPC_MDNULL;
  263. grpc_millis token_lifetime;
  264. grpc_httpcli_response response =
  265. http_response(200,
  266. "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\","
  267. " \"token_type\":\"Bearer\"}");
  268. GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
  269. &response, &token_md, &token_lifetime) ==
  270. GRPC_CREDENTIALS_ERROR);
  271. grpc_http_response_destroy(&response);
  272. }
  273. typedef struct {
  274. const char* key;
  275. const char* value;
  276. } expected_md;
  277. typedef struct {
  278. grpc_error* expected_error;
  279. const expected_md* expected;
  280. size_t expected_size;
  281. grpc_credentials_mdelem_array md_array;
  282. grpc_closure on_request_metadata;
  283. grpc_call_credentials* creds;
  284. grpc_polling_entity pollent;
  285. } request_metadata_state;
  286. static void check_metadata(const expected_md* expected,
  287. grpc_credentials_mdelem_array* md_array) {
  288. for (size_t i = 0; i < md_array->size; ++i) {
  289. size_t j;
  290. for (j = 0; j < md_array->size; ++j) {
  291. if (0 ==
  292. grpc_slice_str_cmp(GRPC_MDKEY(md_array->md[j]), expected[i].key)) {
  293. GPR_ASSERT(grpc_slice_str_cmp(GRPC_MDVALUE(md_array->md[j]),
  294. expected[i].value) == 0);
  295. break;
  296. }
  297. }
  298. if (j == md_array->size) {
  299. gpr_log(GPR_ERROR, "key %s not found", expected[i].key);
  300. GPR_ASSERT(0);
  301. }
  302. }
  303. }
  304. static void check_request_metadata(void* arg, grpc_error* error) {
  305. request_metadata_state* state = static_cast<request_metadata_state*>(arg);
  306. gpr_log(GPR_INFO, "expected_error: %s",
  307. grpc_error_string(state->expected_error));
  308. gpr_log(GPR_INFO, "actual_error: %s", grpc_error_string(error));
  309. if (state->expected_error == GRPC_ERROR_NONE) {
  310. GPR_ASSERT(error == GRPC_ERROR_NONE);
  311. } else {
  312. grpc_slice expected_error;
  313. GPR_ASSERT(grpc_error_get_str(state->expected_error,
  314. GRPC_ERROR_STR_DESCRIPTION, &expected_error));
  315. grpc_slice actual_error;
  316. GPR_ASSERT(
  317. grpc_error_get_str(error, GRPC_ERROR_STR_DESCRIPTION, &actual_error));
  318. GPR_ASSERT(grpc_slice_cmp(expected_error, actual_error) == 0);
  319. GRPC_ERROR_UNREF(state->expected_error);
  320. }
  321. gpr_log(GPR_INFO, "expected_size=%" PRIdPTR " actual_size=%" PRIdPTR,
  322. state->expected_size, state->md_array.size);
  323. GPR_ASSERT(state->md_array.size == state->expected_size);
  324. check_metadata(state->expected, &state->md_array);
  325. grpc_credentials_mdelem_array_destroy(&state->md_array);
  326. grpc_pollset_set_destroy(grpc_polling_entity_pollset_set(&state->pollent));
  327. gpr_free(state);
  328. }
  329. static request_metadata_state* make_request_metadata_state(
  330. grpc_error* expected_error, const expected_md* expected,
  331. size_t expected_size) {
  332. request_metadata_state* state =
  333. static_cast<request_metadata_state*>(gpr_zalloc(sizeof(*state)));
  334. state->expected_error = expected_error;
  335. state->expected = expected;
  336. state->expected_size = expected_size;
  337. state->pollent =
  338. grpc_polling_entity_create_from_pollset_set(grpc_pollset_set_create());
  339. GRPC_CLOSURE_INIT(&state->on_request_metadata, check_request_metadata, state,
  340. grpc_schedule_on_exec_ctx);
  341. return state;
  342. }
  343. static void run_request_metadata_test(grpc_call_credentials* creds,
  344. grpc_auth_metadata_context auth_md_ctx,
  345. request_metadata_state* state) {
  346. grpc_error* error = GRPC_ERROR_NONE;
  347. if (creds->get_request_metadata(&state->pollent, auth_md_ctx,
  348. &state->md_array, &state->on_request_metadata,
  349. &error)) {
  350. // Synchronous result. Invoke the callback directly.
  351. check_request_metadata(state, error);
  352. GRPC_ERROR_UNREF(error);
  353. }
  354. }
  355. static void test_google_iam_creds(void) {
  356. grpc_core::ExecCtx exec_ctx;
  357. expected_md emd[] = {{GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
  358. test_google_iam_authorization_token},
  359. {GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
  360. test_google_iam_authority_selector}};
  361. request_metadata_state* state =
  362. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  363. grpc_call_credentials* creds = grpc_google_iam_credentials_create(
  364. test_google_iam_authorization_token, test_google_iam_authority_selector,
  365. nullptr);
  366. /* Check security level. */
  367. GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY);
  368. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  369. nullptr, nullptr};
  370. run_request_metadata_test(creds, auth_md_ctx, state);
  371. creds->Unref();
  372. }
  373. static void test_access_token_creds(void) {
  374. grpc_core::ExecCtx exec_ctx;
  375. expected_md emd[] = {{GRPC_AUTHORIZATION_METADATA_KEY, "Bearer blah"}};
  376. request_metadata_state* state =
  377. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  378. grpc_call_credentials* creds =
  379. grpc_access_token_credentials_create("blah", nullptr);
  380. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  381. nullptr, nullptr};
  382. GPR_ASSERT(strcmp(creds->type(), GRPC_CALL_CREDENTIALS_TYPE_OAUTH2) == 0);
  383. /* Check security level. */
  384. GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY);
  385. run_request_metadata_test(creds, auth_md_ctx, state);
  386. creds->Unref();
  387. }
  388. namespace {
  389. class check_channel_oauth2 final : public grpc_channel_credentials {
  390. public:
  391. check_channel_oauth2() : grpc_channel_credentials("mock") {}
  392. ~check_channel_oauth2() override = default;
  393. grpc_core::RefCountedPtr<grpc_channel_security_connector>
  394. create_security_connector(
  395. grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
  396. const char* /*target*/, const grpc_channel_args* /*args*/,
  397. grpc_channel_args** /*new_args*/) override {
  398. GPR_ASSERT(strcmp(type(), "mock") == 0);
  399. GPR_ASSERT(call_creds != nullptr);
  400. GPR_ASSERT(strcmp(call_creds->type(), GRPC_CALL_CREDENTIALS_TYPE_OAUTH2) ==
  401. 0);
  402. return nullptr;
  403. }
  404. };
  405. } // namespace
  406. static void test_channel_oauth2_composite_creds(void) {
  407. grpc_core::ExecCtx exec_ctx;
  408. grpc_channel_args* new_args;
  409. grpc_channel_credentials* channel_creds = new check_channel_oauth2();
  410. grpc_call_credentials* oauth2_creds =
  411. grpc_access_token_credentials_create("blah", nullptr);
  412. grpc_channel_credentials* channel_oauth2_creds =
  413. grpc_composite_channel_credentials_create(channel_creds, oauth2_creds,
  414. nullptr);
  415. grpc_channel_credentials_release(channel_creds);
  416. grpc_call_credentials_release(oauth2_creds);
  417. channel_oauth2_creds->create_security_connector(nullptr, nullptr, nullptr,
  418. &new_args);
  419. grpc_channel_credentials_release(channel_oauth2_creds);
  420. }
  421. static void test_oauth2_google_iam_composite_creds(void) {
  422. grpc_core::ExecCtx exec_ctx;
  423. expected_md emd[] = {
  424. {GRPC_AUTHORIZATION_METADATA_KEY, test_oauth2_bearer_token},
  425. {GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY,
  426. test_google_iam_authorization_token},
  427. {GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY,
  428. test_google_iam_authority_selector}};
  429. request_metadata_state* state =
  430. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  431. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  432. nullptr, nullptr};
  433. grpc_call_credentials* oauth2_creds = grpc_md_only_test_credentials_create(
  434. "authorization", test_oauth2_bearer_token, 0);
  435. /* Check security level of fake credentials. */
  436. GPR_ASSERT(oauth2_creds->min_security_level() == GRPC_SECURITY_NONE);
  437. grpc_call_credentials* google_iam_creds = grpc_google_iam_credentials_create(
  438. test_google_iam_authorization_token, test_google_iam_authority_selector,
  439. nullptr);
  440. grpc_call_credentials* composite_creds =
  441. grpc_composite_call_credentials_create(oauth2_creds, google_iam_creds,
  442. nullptr);
  443. /* Check security level of composite credentials. */
  444. GPR_ASSERT(composite_creds->min_security_level() ==
  445. GRPC_PRIVACY_AND_INTEGRITY);
  446. oauth2_creds->Unref();
  447. google_iam_creds->Unref();
  448. GPR_ASSERT(strcmp(composite_creds->type(),
  449. GRPC_CALL_CREDENTIALS_TYPE_COMPOSITE) == 0);
  450. const grpc_composite_call_credentials::CallCredentialsList& creds_list =
  451. static_cast<const grpc_composite_call_credentials*>(composite_creds)
  452. ->inner();
  453. GPR_ASSERT(creds_list.size() == 2);
  454. GPR_ASSERT(strcmp(creds_list[0]->type(), GRPC_CALL_CREDENTIALS_TYPE_OAUTH2) ==
  455. 0);
  456. GPR_ASSERT(strcmp(creds_list[1]->type(), GRPC_CALL_CREDENTIALS_TYPE_IAM) ==
  457. 0);
  458. run_request_metadata_test(composite_creds, auth_md_ctx, state);
  459. composite_creds->Unref();
  460. }
  461. namespace {
  462. class check_channel_oauth2_google_iam final : public grpc_channel_credentials {
  463. public:
  464. check_channel_oauth2_google_iam() : grpc_channel_credentials("mock") {}
  465. ~check_channel_oauth2_google_iam() override = default;
  466. grpc_core::RefCountedPtr<grpc_channel_security_connector>
  467. create_security_connector(
  468. grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
  469. const char* /*target*/, const grpc_channel_args* /*args*/,
  470. grpc_channel_args** /*new_args*/) override {
  471. GPR_ASSERT(strcmp(type(), "mock") == 0);
  472. GPR_ASSERT(call_creds != nullptr);
  473. GPR_ASSERT(
  474. strcmp(call_creds->type(), GRPC_CALL_CREDENTIALS_TYPE_COMPOSITE) == 0);
  475. const grpc_composite_call_credentials::CallCredentialsList& creds_list =
  476. static_cast<const grpc_composite_call_credentials*>(call_creds.get())
  477. ->inner();
  478. GPR_ASSERT(
  479. strcmp(creds_list[0]->type(), GRPC_CALL_CREDENTIALS_TYPE_OAUTH2) == 0);
  480. GPR_ASSERT(strcmp(creds_list[1]->type(), GRPC_CALL_CREDENTIALS_TYPE_IAM) ==
  481. 0);
  482. return nullptr;
  483. }
  484. };
  485. } // namespace
  486. static void test_channel_oauth2_google_iam_composite_creds(void) {
  487. grpc_core::ExecCtx exec_ctx;
  488. grpc_channel_args* new_args;
  489. grpc_channel_credentials* channel_creds =
  490. new check_channel_oauth2_google_iam();
  491. grpc_call_credentials* oauth2_creds =
  492. grpc_access_token_credentials_create("blah", nullptr);
  493. grpc_channel_credentials* channel_oauth2_creds =
  494. grpc_composite_channel_credentials_create(channel_creds, oauth2_creds,
  495. nullptr);
  496. grpc_call_credentials* google_iam_creds = grpc_google_iam_credentials_create(
  497. test_google_iam_authorization_token, test_google_iam_authority_selector,
  498. nullptr);
  499. grpc_channel_credentials* channel_oauth2_iam_creds =
  500. grpc_composite_channel_credentials_create(channel_oauth2_creds,
  501. google_iam_creds, nullptr);
  502. grpc_channel_credentials_release(channel_creds);
  503. grpc_call_credentials_release(oauth2_creds);
  504. grpc_channel_credentials_release(channel_oauth2_creds);
  505. grpc_call_credentials_release(google_iam_creds);
  506. channel_oauth2_iam_creds->create_security_connector(nullptr, nullptr, nullptr,
  507. &new_args);
  508. grpc_channel_credentials_release(channel_oauth2_iam_creds);
  509. }
  510. static void validate_compute_engine_http_request(
  511. const grpc_httpcli_request* request) {
  512. GPR_ASSERT(request->handshaker != &grpc_httpcli_ssl);
  513. GPR_ASSERT(strcmp(request->host, "metadata.google.internal.") == 0);
  514. GPR_ASSERT(
  515. strcmp(request->http.path,
  516. "/computeMetadata/v1/instance/service-accounts/default/token") ==
  517. 0);
  518. GPR_ASSERT(request->http.hdr_count == 1);
  519. GPR_ASSERT(strcmp(request->http.hdrs[0].key, "Metadata-Flavor") == 0);
  520. GPR_ASSERT(strcmp(request->http.hdrs[0].value, "Google") == 0);
  521. }
  522. static int compute_engine_httpcli_get_success_override(
  523. const grpc_httpcli_request* request, grpc_millis /*deadline*/,
  524. grpc_closure* on_done, grpc_httpcli_response* response) {
  525. validate_compute_engine_http_request(request);
  526. *response = http_response(200, valid_oauth2_json_response);
  527. grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, GRPC_ERROR_NONE);
  528. return 1;
  529. }
  530. static int compute_engine_httpcli_get_failure_override(
  531. const grpc_httpcli_request* request, grpc_millis /*deadline*/,
  532. grpc_closure* on_done, grpc_httpcli_response* response) {
  533. validate_compute_engine_http_request(request);
  534. *response = http_response(403, "Not Authorized.");
  535. grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, GRPC_ERROR_NONE);
  536. return 1;
  537. }
  538. static int httpcli_post_should_not_be_called(
  539. const grpc_httpcli_request* /*request*/, const char* /*body_bytes*/,
  540. size_t /*body_size*/, grpc_millis /*deadline*/, grpc_closure* /*on_done*/,
  541. grpc_httpcli_response* /*response*/) {
  542. GPR_ASSERT("HTTP POST should not be called" == nullptr);
  543. return 1;
  544. }
  545. static int httpcli_get_should_not_be_called(
  546. const grpc_httpcli_request* /*request*/, grpc_millis /*deadline*/,
  547. grpc_closure* /*on_done*/, grpc_httpcli_response* /*response*/) {
  548. GPR_ASSERT("HTTP GET should not be called" == nullptr);
  549. return 1;
  550. }
  551. static void test_compute_engine_creds_success() {
  552. grpc_core::ExecCtx exec_ctx;
  553. expected_md emd[] = {
  554. {"authorization", "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"}};
  555. const char expected_creds_debug_string[] =
  556. "GoogleComputeEngineTokenFetcherCredentials{"
  557. "OAuth2TokenFetcherCredentials}";
  558. grpc_call_credentials* creds =
  559. grpc_google_compute_engine_credentials_create(nullptr);
  560. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  561. nullptr, nullptr};
  562. /* Check security level. */
  563. GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY);
  564. /* First request: http get should be called. */
  565. request_metadata_state* state =
  566. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  567. grpc_httpcli_set_override(compute_engine_httpcli_get_success_override,
  568. httpcli_post_should_not_be_called);
  569. run_request_metadata_test(creds, auth_md_ctx, state);
  570. grpc_core::ExecCtx::Get()->Flush();
  571. /* Second request: the cached token should be served directly. */
  572. state =
  573. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  574. grpc_httpcli_set_override(httpcli_get_should_not_be_called,
  575. httpcli_post_should_not_be_called);
  576. run_request_metadata_test(creds, auth_md_ctx, state);
  577. grpc_core::ExecCtx::Get()->Flush();
  578. GPR_ASSERT(
  579. strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0);
  580. creds->Unref();
  581. grpc_httpcli_set_override(nullptr, nullptr);
  582. }
  583. static void test_compute_engine_creds_failure(void) {
  584. grpc_core::ExecCtx exec_ctx;
  585. const char expected_creds_debug_string[] =
  586. "GoogleComputeEngineTokenFetcherCredentials{"
  587. "OAuth2TokenFetcherCredentials}";
  588. request_metadata_state* state = make_request_metadata_state(
  589. GRPC_ERROR_CREATE_FROM_STATIC_STRING(
  590. "Error occurred when fetching oauth2 token."),
  591. nullptr, 0);
  592. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  593. nullptr, nullptr};
  594. grpc_call_credentials* creds =
  595. grpc_google_compute_engine_credentials_create(nullptr);
  596. grpc_httpcli_set_override(compute_engine_httpcli_get_failure_override,
  597. httpcli_post_should_not_be_called);
  598. run_request_metadata_test(creds, auth_md_ctx, state);
  599. GPR_ASSERT(
  600. strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0);
  601. creds->Unref();
  602. grpc_httpcli_set_override(nullptr, nullptr);
  603. }
  604. static void validate_refresh_token_http_request(
  605. const grpc_httpcli_request* request, const char* body, size_t body_size) {
  606. /* The content of the assertion is tested extensively in json_token_test. */
  607. char* expected_body = nullptr;
  608. GPR_ASSERT(body != nullptr);
  609. GPR_ASSERT(body_size != 0);
  610. gpr_asprintf(&expected_body, GRPC_REFRESH_TOKEN_POST_BODY_FORMAT_STRING,
  611. "32555999999.apps.googleusercontent.com",
  612. "EmssLNjJy1332hD4KFsecret",
  613. "1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42");
  614. GPR_ASSERT(strlen(expected_body) == body_size);
  615. GPR_ASSERT(memcmp(expected_body, body, body_size) == 0);
  616. gpr_free(expected_body);
  617. GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl);
  618. GPR_ASSERT(strcmp(request->host, GRPC_GOOGLE_OAUTH2_SERVICE_HOST) == 0);
  619. GPR_ASSERT(
  620. strcmp(request->http.path, GRPC_GOOGLE_OAUTH2_SERVICE_TOKEN_PATH) == 0);
  621. GPR_ASSERT(request->http.hdr_count == 1);
  622. GPR_ASSERT(strcmp(request->http.hdrs[0].key, "Content-Type") == 0);
  623. GPR_ASSERT(strcmp(request->http.hdrs[0].value,
  624. "application/x-www-form-urlencoded") == 0);
  625. }
  626. static int refresh_token_httpcli_post_success(
  627. const grpc_httpcli_request* request, const char* body, size_t body_size,
  628. grpc_millis /*deadline*/, grpc_closure* on_done,
  629. grpc_httpcli_response* response) {
  630. validate_refresh_token_http_request(request, body, body_size);
  631. *response = http_response(200, valid_oauth2_json_response);
  632. grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, GRPC_ERROR_NONE);
  633. return 1;
  634. }
  635. static int token_httpcli_post_failure(const grpc_httpcli_request* /*request*/,
  636. const char* /*body*/,
  637. size_t /*body_size*/,
  638. grpc_millis /*deadline*/,
  639. grpc_closure* on_done,
  640. grpc_httpcli_response* response) {
  641. *response = http_response(403, "Not Authorized.");
  642. grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, GRPC_ERROR_NONE);
  643. return 1;
  644. }
  645. static void test_refresh_token_creds_success(void) {
  646. grpc_core::ExecCtx exec_ctx;
  647. expected_md emd[] = {
  648. {"authorization", "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"}};
  649. const char expected_creds_debug_string[] =
  650. "GoogleRefreshToken{ClientID:32555999999.apps.googleusercontent.com,"
  651. "OAuth2TokenFetcherCredentials}";
  652. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  653. nullptr, nullptr};
  654. grpc_call_credentials* creds = grpc_google_refresh_token_credentials_create(
  655. test_refresh_token_str, nullptr);
  656. /* Check security level. */
  657. GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY);
  658. /* First request: http put should be called. */
  659. request_metadata_state* state =
  660. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  661. grpc_httpcli_set_override(httpcli_get_should_not_be_called,
  662. refresh_token_httpcli_post_success);
  663. run_request_metadata_test(creds, auth_md_ctx, state);
  664. grpc_core::ExecCtx::Get()->Flush();
  665. /* Second request: the cached token should be served directly. */
  666. state =
  667. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  668. grpc_httpcli_set_override(httpcli_get_should_not_be_called,
  669. httpcli_post_should_not_be_called);
  670. run_request_metadata_test(creds, auth_md_ctx, state);
  671. grpc_core::ExecCtx::Get()->Flush();
  672. GPR_ASSERT(
  673. strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0);
  674. creds->Unref();
  675. grpc_httpcli_set_override(nullptr, nullptr);
  676. }
  677. static void test_refresh_token_creds_failure(void) {
  678. grpc_core::ExecCtx exec_ctx;
  679. const char expected_creds_debug_string[] =
  680. "GoogleRefreshToken{ClientID:32555999999.apps.googleusercontent.com,"
  681. "OAuth2TokenFetcherCredentials}";
  682. request_metadata_state* state = make_request_metadata_state(
  683. GRPC_ERROR_CREATE_FROM_STATIC_STRING(
  684. "Error occurred when fetching oauth2 token."),
  685. nullptr, 0);
  686. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  687. nullptr, nullptr};
  688. grpc_call_credentials* creds = grpc_google_refresh_token_credentials_create(
  689. test_refresh_token_str, nullptr);
  690. grpc_httpcli_set_override(httpcli_get_should_not_be_called,
  691. token_httpcli_post_failure);
  692. run_request_metadata_test(creds, auth_md_ctx, state);
  693. GPR_ASSERT(
  694. strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0);
  695. creds->Unref();
  696. grpc_httpcli_set_override(nullptr, nullptr);
  697. }
  698. static void test_valid_sts_creds_options(void) {
  699. grpc_sts_credentials_options valid_options = {
  700. test_sts_endpoint_url, // sts_endpoint_url
  701. nullptr, // resource
  702. nullptr, // audience
  703. nullptr, // scope
  704. nullptr, // requested_token_type
  705. test_signed_jwt_path_prefix, // subject_token_path
  706. test_signed_jwt_token_type, // subject_token_type
  707. nullptr, // actor_token_path
  708. nullptr // actor_token_type
  709. };
  710. grpc_uri* sts_url;
  711. grpc_error* error =
  712. grpc_core::ValidateStsCredentialsOptions(&valid_options, &sts_url);
  713. GPR_ASSERT(error == GRPC_ERROR_NONE);
  714. GPR_ASSERT(sts_url != nullptr);
  715. absl::string_view host;
  716. absl::string_view port;
  717. GPR_ASSERT(grpc_core::SplitHostPort(sts_url->authority, &host, &port));
  718. GPR_ASSERT(host == "foo.com");
  719. GPR_ASSERT(port == "5555");
  720. grpc_uri_destroy(sts_url);
  721. }
  722. static void test_invalid_sts_creds_options(void) {
  723. grpc_sts_credentials_options invalid_options = {
  724. test_sts_endpoint_url, // sts_endpoint_url
  725. nullptr, // resource
  726. nullptr, // audience
  727. nullptr, // scope
  728. nullptr, // requested_token_type
  729. nullptr, // subject_token_path (Required)
  730. test_signed_jwt_token_type, // subject_token_type
  731. nullptr, // actor_token_path
  732. nullptr // actor_token_type
  733. };
  734. grpc_uri* url_should_be_null;
  735. grpc_error* error = grpc_core::ValidateStsCredentialsOptions(
  736. &invalid_options, &url_should_be_null);
  737. GPR_ASSERT(error != GRPC_ERROR_NONE);
  738. GRPC_ERROR_UNREF(error);
  739. GPR_ASSERT(url_should_be_null == nullptr);
  740. invalid_options = {
  741. test_sts_endpoint_url, // sts_endpoint_url
  742. nullptr, // resource
  743. nullptr, // audience
  744. nullptr, // scope
  745. nullptr, // requested_token_type
  746. test_signed_jwt_path_prefix, // subject_token_path
  747. nullptr, // subject_token_type (Required)
  748. nullptr, // actor_token_path
  749. nullptr // actor_token_type
  750. };
  751. error = grpc_core::ValidateStsCredentialsOptions(&invalid_options,
  752. &url_should_be_null);
  753. GPR_ASSERT(error != GRPC_ERROR_NONE);
  754. GRPC_ERROR_UNREF(error);
  755. GPR_ASSERT(url_should_be_null == nullptr);
  756. invalid_options = {
  757. nullptr, // sts_endpoint_url (Required)
  758. nullptr, // resource
  759. nullptr, // audience
  760. nullptr, // scope
  761. nullptr, // requested_token_type
  762. test_signed_jwt_path_prefix, // subject_token_path
  763. test_signed_jwt_token_type, // subject_token_type (Required)
  764. nullptr, // actor_token_path
  765. nullptr // actor_token_type
  766. };
  767. error = grpc_core::ValidateStsCredentialsOptions(&invalid_options,
  768. &url_should_be_null);
  769. GPR_ASSERT(error != GRPC_ERROR_NONE);
  770. GRPC_ERROR_UNREF(error);
  771. GPR_ASSERT(url_should_be_null == nullptr);
  772. invalid_options = {
  773. "not_a_valid_uri", // sts_endpoint_url
  774. nullptr, // resource
  775. nullptr, // audience
  776. nullptr, // scope
  777. nullptr, // requested_token_type
  778. test_signed_jwt_path_prefix, // subject_token_path
  779. test_signed_jwt_token_type, // subject_token_type (Required)
  780. nullptr, // actor_token_path
  781. nullptr // actor_token_type
  782. };
  783. error = grpc_core::ValidateStsCredentialsOptions(&invalid_options,
  784. &url_should_be_null);
  785. GPR_ASSERT(error != GRPC_ERROR_NONE);
  786. GRPC_ERROR_UNREF(error);
  787. GPR_ASSERT(url_should_be_null == nullptr);
  788. invalid_options = {
  789. "ftp://ftp.is.not.a.valid.scheme/bar", // sts_endpoint_url
  790. nullptr, // resource
  791. nullptr, // audience
  792. nullptr, // scope
  793. nullptr, // requested_token_type
  794. test_signed_jwt_path_prefix, // subject_token_path
  795. test_signed_jwt_token_type, // subject_token_type (Required)
  796. nullptr, // actor_token_path
  797. nullptr // actor_token_type
  798. };
  799. error = grpc_core::ValidateStsCredentialsOptions(&invalid_options,
  800. &url_should_be_null);
  801. GPR_ASSERT(error != GRPC_ERROR_NONE);
  802. GRPC_ERROR_UNREF(error);
  803. GPR_ASSERT(url_should_be_null == nullptr);
  804. }
  805. static void validate_sts_token_http_request(const grpc_httpcli_request* request,
  806. const char* body, size_t body_size,
  807. bool expect_actor_token) {
  808. // Check that the body is constructed properly.
  809. GPR_ASSERT(body != nullptr);
  810. GPR_ASSERT(body_size != 0);
  811. GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl);
  812. char* get_url_equivalent;
  813. gpr_asprintf(&get_url_equivalent, "%s?%s", test_sts_endpoint_url, body);
  814. grpc_uri* url = grpc_uri_parse(get_url_equivalent, false);
  815. GPR_ASSERT(strcmp(grpc_uri_get_query_arg(url, "resource"), "resource") == 0);
  816. GPR_ASSERT(strcmp(grpc_uri_get_query_arg(url, "audience"), "audience") == 0);
  817. GPR_ASSERT(strcmp(grpc_uri_get_query_arg(url, "scope"), "scope") == 0);
  818. GPR_ASSERT(strcmp(grpc_uri_get_query_arg(url, "requested_token_type"),
  819. "requested_token_type") == 0);
  820. GPR_ASSERT(strcmp(grpc_uri_get_query_arg(url, "subject_token"),
  821. test_signed_jwt) == 0);
  822. GPR_ASSERT(strcmp(grpc_uri_get_query_arg(url, "subject_token_type"),
  823. test_signed_jwt_token_type) == 0);
  824. if (expect_actor_token) {
  825. GPR_ASSERT(strcmp(grpc_uri_get_query_arg(url, "actor_token"),
  826. test_signed_jwt2) == 0);
  827. GPR_ASSERT(strcmp(grpc_uri_get_query_arg(url, "actor_token_type"),
  828. test_signed_jwt_token_type2) == 0);
  829. } else {
  830. GPR_ASSERT(grpc_uri_get_query_arg(url, "actor_token") == nullptr);
  831. GPR_ASSERT(grpc_uri_get_query_arg(url, "actor_token_type") == nullptr);
  832. }
  833. grpc_uri_destroy(url);
  834. gpr_free(get_url_equivalent);
  835. // Check the rest of the request.
  836. GPR_ASSERT(strcmp(request->host, "foo.com:5555") == 0);
  837. GPR_ASSERT(strcmp(request->http.path, "/v1/token-exchange") == 0);
  838. GPR_ASSERT(request->http.hdr_count == 1);
  839. GPR_ASSERT(strcmp(request->http.hdrs[0].key, "Content-Type") == 0);
  840. GPR_ASSERT(strcmp(request->http.hdrs[0].value,
  841. "application/x-www-form-urlencoded") == 0);
  842. }
  843. static int sts_token_httpcli_post_success(const grpc_httpcli_request* request,
  844. const char* body, size_t body_size,
  845. grpc_millis /*deadline*/,
  846. grpc_closure* on_done,
  847. grpc_httpcli_response* response) {
  848. validate_sts_token_http_request(request, body, body_size, true);
  849. *response = http_response(200, valid_sts_json_response);
  850. grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, GRPC_ERROR_NONE);
  851. return 1;
  852. }
  853. static int sts_token_httpcli_post_success_no_actor_token(
  854. const grpc_httpcli_request* request, const char* body, size_t body_size,
  855. grpc_millis /*deadline*/, grpc_closure* on_done,
  856. grpc_httpcli_response* response) {
  857. validate_sts_token_http_request(request, body, body_size, false);
  858. *response = http_response(200, valid_sts_json_response);
  859. grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, GRPC_ERROR_NONE);
  860. return 1;
  861. }
  862. static char* write_tmp_jwt_file(const char* jwt_contents) {
  863. char* path;
  864. FILE* tmp = gpr_tmpfile(test_signed_jwt_path_prefix, &path);
  865. GPR_ASSERT(path != nullptr);
  866. GPR_ASSERT(tmp != nullptr);
  867. size_t jwt_length = strlen(jwt_contents);
  868. GPR_ASSERT(fwrite(jwt_contents, 1, jwt_length, tmp) == jwt_length);
  869. fclose(tmp);
  870. return path;
  871. }
  872. static void test_sts_creds_success(void) {
  873. grpc_core::ExecCtx exec_ctx;
  874. expected_md emd[] = {
  875. {"authorization", "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"}};
  876. const char expected_creds_debug_string[] =
  877. "StsTokenFetcherCredentials{Path:/v1/"
  878. "token-exchange,Authority:foo.com:5555,OAuth2TokenFetcherCredentials}";
  879. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  880. nullptr, nullptr};
  881. char* subject_token_path = write_tmp_jwt_file(test_signed_jwt);
  882. char* actor_token_path = write_tmp_jwt_file(test_signed_jwt2);
  883. grpc_sts_credentials_options valid_options = {
  884. test_sts_endpoint_url, // sts_endpoint_url
  885. "resource", // resource
  886. "audience", // audience
  887. "scope", // scope
  888. "requested_token_type", // requested_token_type
  889. subject_token_path, // subject_token_path
  890. test_signed_jwt_token_type, // subject_token_type
  891. actor_token_path, // actor_token_path
  892. test_signed_jwt_token_type2 // actor_token_type
  893. };
  894. grpc_call_credentials* creds =
  895. grpc_sts_credentials_create(&valid_options, nullptr);
  896. /* Check security level. */
  897. GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY);
  898. /* First request: http put should be called. */
  899. request_metadata_state* state =
  900. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  901. grpc_httpcli_set_override(httpcli_get_should_not_be_called,
  902. sts_token_httpcli_post_success);
  903. run_request_metadata_test(creds, auth_md_ctx, state);
  904. grpc_core::ExecCtx::Get()->Flush();
  905. /* Second request: the cached token should be served directly. */
  906. state =
  907. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  908. grpc_httpcli_set_override(httpcli_get_should_not_be_called,
  909. httpcli_post_should_not_be_called);
  910. run_request_metadata_test(creds, auth_md_ctx, state);
  911. grpc_core::ExecCtx::Get()->Flush();
  912. GPR_ASSERT(
  913. strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0);
  914. creds->Unref();
  915. grpc_httpcli_set_override(nullptr, nullptr);
  916. gpr_free(subject_token_path);
  917. gpr_free(actor_token_path);
  918. }
  919. static void test_sts_creds_token_file_not_found(void) {
  920. grpc_core::ExecCtx exec_ctx;
  921. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  922. nullptr, nullptr};
  923. grpc_sts_credentials_options valid_options = {
  924. test_sts_endpoint_url, // sts_endpoint_url
  925. "resource", // resource
  926. "audience", // audience
  927. "scope", // scope
  928. "requested_token_type", // requested_token_type
  929. "/some/completely/random/path", // subject_token_path
  930. test_signed_jwt_token_type, // subject_token_type
  931. "", // actor_token_path
  932. "" // actor_token_type
  933. };
  934. grpc_call_credentials* creds =
  935. grpc_sts_credentials_create(&valid_options, nullptr);
  936. /* Check security level. */
  937. GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY);
  938. request_metadata_state* state = make_request_metadata_state(
  939. GRPC_ERROR_CREATE_FROM_STATIC_STRING(
  940. "Error occurred when fetching oauth2 token."),
  941. nullptr, 0);
  942. grpc_httpcli_set_override(httpcli_get_should_not_be_called,
  943. httpcli_post_should_not_be_called);
  944. run_request_metadata_test(creds, auth_md_ctx, state);
  945. grpc_core::ExecCtx::Get()->Flush();
  946. /* Cleanup. */
  947. creds->Unref();
  948. grpc_httpcli_set_override(nullptr, nullptr);
  949. }
  950. static void test_sts_creds_no_actor_token_success(void) {
  951. grpc_core::ExecCtx exec_ctx;
  952. expected_md emd[] = {
  953. {"authorization", "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"}};
  954. const char expected_creds_debug_string[] =
  955. "StsTokenFetcherCredentials{Path:/v1/"
  956. "token-exchange,Authority:foo.com:5555,OAuth2TokenFetcherCredentials}";
  957. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  958. nullptr, nullptr};
  959. char* subject_token_path = write_tmp_jwt_file(test_signed_jwt);
  960. grpc_sts_credentials_options valid_options = {
  961. test_sts_endpoint_url, // sts_endpoint_url
  962. "resource", // resource
  963. "audience", // audience
  964. "scope", // scope
  965. "requested_token_type", // requested_token_type
  966. subject_token_path, // subject_token_path
  967. test_signed_jwt_token_type, // subject_token_type
  968. "", // actor_token_path
  969. "" // actor_token_type
  970. };
  971. grpc_call_credentials* creds =
  972. grpc_sts_credentials_create(&valid_options, nullptr);
  973. /* Check security level. */
  974. GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY);
  975. /* First request: http put should be called. */
  976. request_metadata_state* state =
  977. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  978. grpc_httpcli_set_override(httpcli_get_should_not_be_called,
  979. sts_token_httpcli_post_success_no_actor_token);
  980. run_request_metadata_test(creds, auth_md_ctx, state);
  981. grpc_core::ExecCtx::Get()->Flush();
  982. /* Second request: the cached token should be served directly. */
  983. state =
  984. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  985. grpc_httpcli_set_override(httpcli_get_should_not_be_called,
  986. httpcli_post_should_not_be_called);
  987. run_request_metadata_test(creds, auth_md_ctx, state);
  988. grpc_core::ExecCtx::Get()->Flush();
  989. GPR_ASSERT(
  990. strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0);
  991. creds->Unref();
  992. grpc_httpcli_set_override(nullptr, nullptr);
  993. gpr_free(subject_token_path);
  994. }
  995. static void test_sts_creds_load_token_failure(void) {
  996. const char expected_creds_debug_string[] =
  997. "StsTokenFetcherCredentials{Path:/v1/"
  998. "token-exchange,Authority:foo.com:5555,OAuth2TokenFetcherCredentials}";
  999. grpc_core::ExecCtx exec_ctx;
  1000. request_metadata_state* state = make_request_metadata_state(
  1001. GRPC_ERROR_CREATE_FROM_STATIC_STRING(
  1002. "Error occurred when fetching oauth2 token."),
  1003. nullptr, 0);
  1004. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  1005. nullptr, nullptr};
  1006. char* test_signed_jwt_path = write_tmp_jwt_file(test_signed_jwt);
  1007. grpc_sts_credentials_options options = {
  1008. test_sts_endpoint_url, // sts_endpoint_url
  1009. "resource", // resource
  1010. "audience", // audience
  1011. "scope", // scope
  1012. "requested_token_type", // requested_token_type
  1013. "invalid_path", // subject_token_path
  1014. test_signed_jwt_token_type, // subject_token_type
  1015. nullptr, // actor_token_path
  1016. nullptr // actor_token_type
  1017. };
  1018. grpc_call_credentials* creds = grpc_sts_credentials_create(&options, nullptr);
  1019. grpc_httpcli_set_override(httpcli_get_should_not_be_called,
  1020. httpcli_post_should_not_be_called);
  1021. run_request_metadata_test(creds, auth_md_ctx, state);
  1022. GPR_ASSERT(
  1023. strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0);
  1024. creds->Unref();
  1025. grpc_httpcli_set_override(nullptr, nullptr);
  1026. gpr_free(test_signed_jwt_path);
  1027. }
  1028. static void test_sts_creds_http_failure(void) {
  1029. const char expected_creds_debug_string[] =
  1030. "StsTokenFetcherCredentials{Path:/v1/"
  1031. "token-exchange,Authority:foo.com:5555,OAuth2TokenFetcherCredentials}";
  1032. grpc_core::ExecCtx exec_ctx;
  1033. request_metadata_state* state = make_request_metadata_state(
  1034. GRPC_ERROR_CREATE_FROM_STATIC_STRING(
  1035. "Error occurred when fetching oauth2 token."),
  1036. nullptr, 0);
  1037. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  1038. nullptr, nullptr};
  1039. char* test_signed_jwt_path = write_tmp_jwt_file(test_signed_jwt);
  1040. grpc_sts_credentials_options valid_options = {
  1041. test_sts_endpoint_url, // sts_endpoint_url
  1042. "resource", // resource
  1043. "audience", // audience
  1044. "scope", // scope
  1045. "requested_token_type", // requested_token_type
  1046. test_signed_jwt_path, // subject_token_path
  1047. test_signed_jwt_token_type, // subject_token_type
  1048. nullptr, // actor_token_path
  1049. nullptr // actor_token_type
  1050. };
  1051. grpc_call_credentials* creds =
  1052. grpc_sts_credentials_create(&valid_options, nullptr);
  1053. grpc_httpcli_set_override(httpcli_get_should_not_be_called,
  1054. token_httpcli_post_failure);
  1055. run_request_metadata_test(creds, auth_md_ctx, state);
  1056. GPR_ASSERT(
  1057. strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0);
  1058. creds->Unref();
  1059. grpc_httpcli_set_override(nullptr, nullptr);
  1060. gpr_free(test_signed_jwt_path);
  1061. }
  1062. static void validate_jwt_encode_and_sign_params(
  1063. const grpc_auth_json_key* json_key, const char* scope,
  1064. gpr_timespec token_lifetime) {
  1065. GPR_ASSERT(grpc_auth_json_key_is_valid(json_key));
  1066. GPR_ASSERT(json_key->private_key != nullptr);
  1067. GPR_ASSERT(RSA_check_key(json_key->private_key));
  1068. GPR_ASSERT(json_key->type != nullptr &&
  1069. strcmp(json_key->type, "service_account") == 0);
  1070. GPR_ASSERT(json_key->private_key_id != nullptr &&
  1071. strcmp(json_key->private_key_id,
  1072. "e6b5137873db8d2ef81e06a47289e6434ec8a165") == 0);
  1073. GPR_ASSERT(json_key->client_id != nullptr &&
  1074. strcmp(json_key->client_id,
  1075. "777-abaslkan11hlb6nmim3bpspl31ud.apps."
  1076. "googleusercontent.com") == 0);
  1077. GPR_ASSERT(json_key->client_email != nullptr &&
  1078. strcmp(json_key->client_email,
  1079. "777-abaslkan11hlb6nmim3bpspl31ud@developer."
  1080. "gserviceaccount.com") == 0);
  1081. if (scope != nullptr) GPR_ASSERT(strcmp(scope, test_scope) == 0);
  1082. GPR_ASSERT(gpr_time_cmp(token_lifetime, grpc_max_auth_token_lifetime()) == 0);
  1083. }
  1084. static char* encode_and_sign_jwt_success(const grpc_auth_json_key* json_key,
  1085. const char* /*audience*/,
  1086. gpr_timespec token_lifetime,
  1087. const char* scope) {
  1088. validate_jwt_encode_and_sign_params(json_key, scope, token_lifetime);
  1089. return gpr_strdup(test_signed_jwt);
  1090. }
  1091. static char* encode_and_sign_jwt_failure(const grpc_auth_json_key* json_key,
  1092. const char* /*audience*/,
  1093. gpr_timespec token_lifetime,
  1094. const char* scope) {
  1095. validate_jwt_encode_and_sign_params(json_key, scope, token_lifetime);
  1096. return nullptr;
  1097. }
  1098. static char* encode_and_sign_jwt_should_not_be_called(
  1099. const grpc_auth_json_key* /*json_key*/, const char* /*audience*/,
  1100. gpr_timespec /*token_lifetime*/, const char* /*scope*/) {
  1101. GPR_ASSERT("grpc_jwt_encode_and_sign should not be called" == nullptr);
  1102. return nullptr;
  1103. }
  1104. static grpc_service_account_jwt_access_credentials* creds_as_jwt(
  1105. grpc_call_credentials* creds) {
  1106. GPR_ASSERT(creds != nullptr);
  1107. GPR_ASSERT(strcmp(creds->type(), GRPC_CALL_CREDENTIALS_TYPE_JWT) == 0);
  1108. return reinterpret_cast<grpc_service_account_jwt_access_credentials*>(creds);
  1109. }
  1110. static void test_jwt_creds_lifetime(void) {
  1111. char* json_key_string = test_json_key_str();
  1112. const char expected_creds_debug_string_prefix[] =
  1113. "JWTAccessCredentials{ExpirationTime:";
  1114. // Max lifetime.
  1115. grpc_call_credentials* jwt_creds =
  1116. grpc_service_account_jwt_access_credentials_create(
  1117. json_key_string, grpc_max_auth_token_lifetime(), nullptr);
  1118. GPR_ASSERT(gpr_time_cmp(creds_as_jwt(jwt_creds)->jwt_lifetime(),
  1119. grpc_max_auth_token_lifetime()) == 0);
  1120. /* Check security level. */
  1121. GPR_ASSERT(jwt_creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY);
  1122. GPR_ASSERT(strncmp(expected_creds_debug_string_prefix,
  1123. jwt_creds->debug_string().c_str(),
  1124. strlen(expected_creds_debug_string_prefix)) == 0);
  1125. grpc_call_credentials_release(jwt_creds);
  1126. // Shorter lifetime.
  1127. gpr_timespec token_lifetime = {10, 0, GPR_TIMESPAN};
  1128. GPR_ASSERT(gpr_time_cmp(grpc_max_auth_token_lifetime(), token_lifetime) > 0);
  1129. jwt_creds = grpc_service_account_jwt_access_credentials_create(
  1130. json_key_string, token_lifetime, nullptr);
  1131. GPR_ASSERT(gpr_time_cmp(creds_as_jwt(jwt_creds)->jwt_lifetime(),
  1132. token_lifetime) == 0);
  1133. GPR_ASSERT(strncmp(expected_creds_debug_string_prefix,
  1134. jwt_creds->debug_string().c_str(),
  1135. strlen(expected_creds_debug_string_prefix)) == 0);
  1136. grpc_call_credentials_release(jwt_creds);
  1137. // Cropped lifetime.
  1138. gpr_timespec add_to_max = {10, 0, GPR_TIMESPAN};
  1139. token_lifetime = gpr_time_add(grpc_max_auth_token_lifetime(), add_to_max);
  1140. jwt_creds = grpc_service_account_jwt_access_credentials_create(
  1141. json_key_string, token_lifetime, nullptr);
  1142. GPR_ASSERT(gpr_time_cmp(creds_as_jwt(jwt_creds)->jwt_lifetime(),
  1143. grpc_max_auth_token_lifetime()) == 0);
  1144. GPR_ASSERT(strncmp(expected_creds_debug_string_prefix,
  1145. jwt_creds->debug_string().c_str(),
  1146. strlen(expected_creds_debug_string_prefix)) == 0);
  1147. grpc_call_credentials_release(jwt_creds);
  1148. gpr_free(json_key_string);
  1149. }
  1150. static void test_jwt_creds_success(void) {
  1151. const char expected_creds_debug_string_prefix[] =
  1152. "JWTAccessCredentials{ExpirationTime:";
  1153. char* json_key_string = test_json_key_str();
  1154. grpc_core::ExecCtx exec_ctx;
  1155. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  1156. nullptr, nullptr};
  1157. char* expected_md_value;
  1158. gpr_asprintf(&expected_md_value, "Bearer %s", test_signed_jwt);
  1159. expected_md emd[] = {{"authorization", expected_md_value}};
  1160. grpc_call_credentials* creds =
  1161. grpc_service_account_jwt_access_credentials_create(
  1162. json_key_string, grpc_max_auth_token_lifetime(), nullptr);
  1163. /* First request: jwt_encode_and_sign should be called. */
  1164. request_metadata_state* state =
  1165. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  1166. grpc_jwt_encode_and_sign_set_override(encode_and_sign_jwt_success);
  1167. run_request_metadata_test(creds, auth_md_ctx, state);
  1168. grpc_core::ExecCtx::Get()->Flush();
  1169. /* Second request: the cached token should be served directly. */
  1170. state =
  1171. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  1172. grpc_jwt_encode_and_sign_set_override(
  1173. encode_and_sign_jwt_should_not_be_called);
  1174. run_request_metadata_test(creds, auth_md_ctx, state);
  1175. grpc_core::ExecCtx::Get()->Flush();
  1176. /* Third request: Different service url so jwt_encode_and_sign should be
  1177. called again (no caching). */
  1178. state =
  1179. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  1180. auth_md_ctx.service_url = other_test_service_url;
  1181. grpc_jwt_encode_and_sign_set_override(encode_and_sign_jwt_success);
  1182. run_request_metadata_test(creds, auth_md_ctx, state);
  1183. grpc_core::ExecCtx::Get()->Flush();
  1184. GPR_ASSERT(strncmp(expected_creds_debug_string_prefix,
  1185. creds->debug_string().c_str(),
  1186. strlen(expected_creds_debug_string_prefix)) == 0);
  1187. creds->Unref();
  1188. gpr_free(json_key_string);
  1189. gpr_free(expected_md_value);
  1190. grpc_jwt_encode_and_sign_set_override(nullptr);
  1191. }
  1192. static void test_jwt_creds_signing_failure(void) {
  1193. const char expected_creds_debug_string_prefix[] =
  1194. "JWTAccessCredentials{ExpirationTime:";
  1195. char* json_key_string = test_json_key_str();
  1196. grpc_core::ExecCtx exec_ctx;
  1197. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  1198. nullptr, nullptr};
  1199. request_metadata_state* state = make_request_metadata_state(
  1200. GRPC_ERROR_CREATE_FROM_STATIC_STRING("Could not generate JWT."), nullptr,
  1201. 0);
  1202. grpc_call_credentials* creds =
  1203. grpc_service_account_jwt_access_credentials_create(
  1204. json_key_string, grpc_max_auth_token_lifetime(), nullptr);
  1205. grpc_jwt_encode_and_sign_set_override(encode_and_sign_jwt_failure);
  1206. run_request_metadata_test(creds, auth_md_ctx, state);
  1207. gpr_free(json_key_string);
  1208. GPR_ASSERT(strncmp(expected_creds_debug_string_prefix,
  1209. creds->debug_string().c_str(),
  1210. strlen(expected_creds_debug_string_prefix)) == 0);
  1211. creds->Unref();
  1212. grpc_jwt_encode_and_sign_set_override(nullptr);
  1213. }
  1214. static void set_google_default_creds_env_var_with_file_contents(
  1215. const char* file_prefix, const char* contents) {
  1216. size_t contents_len = strlen(contents);
  1217. char* creds_file_name;
  1218. FILE* creds_file = gpr_tmpfile(file_prefix, &creds_file_name);
  1219. GPR_ASSERT(creds_file_name != nullptr);
  1220. GPR_ASSERT(creds_file != nullptr);
  1221. GPR_ASSERT(fwrite(contents, 1, contents_len, creds_file) == contents_len);
  1222. fclose(creds_file);
  1223. gpr_setenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, creds_file_name);
  1224. gpr_free(creds_file_name);
  1225. }
  1226. static void test_google_default_creds_auth_key(void) {
  1227. grpc_core::ExecCtx exec_ctx;
  1228. grpc_composite_channel_credentials* creds;
  1229. char* json_key = test_json_key_str();
  1230. grpc_flush_cached_google_default_credentials();
  1231. set_google_default_creds_env_var_with_file_contents(
  1232. "json_key_google_default_creds", json_key);
  1233. gpr_free(json_key);
  1234. creds = reinterpret_cast<grpc_composite_channel_credentials*>(
  1235. grpc_google_default_credentials_create(nullptr));
  1236. auto* default_creds =
  1237. reinterpret_cast<const grpc_google_default_channel_credentials*>(
  1238. creds->inner_creds());
  1239. GPR_ASSERT(default_creds->ssl_creds() != nullptr);
  1240. auto* jwt =
  1241. reinterpret_cast<const grpc_service_account_jwt_access_credentials*>(
  1242. creds->call_creds());
  1243. GPR_ASSERT(
  1244. strcmp(jwt->key().client_id,
  1245. "777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent.com") ==
  1246. 0);
  1247. creds->Unref();
  1248. gpr_setenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */
  1249. }
  1250. static void test_google_default_creds_refresh_token(void) {
  1251. grpc_core::ExecCtx exec_ctx;
  1252. grpc_composite_channel_credentials* creds;
  1253. grpc_flush_cached_google_default_credentials();
  1254. set_google_default_creds_env_var_with_file_contents(
  1255. "refresh_token_google_default_creds", test_refresh_token_str);
  1256. creds = reinterpret_cast<grpc_composite_channel_credentials*>(
  1257. grpc_google_default_credentials_create(nullptr));
  1258. auto* default_creds =
  1259. reinterpret_cast<const grpc_google_default_channel_credentials*>(
  1260. creds->inner_creds());
  1261. GPR_ASSERT(default_creds->ssl_creds() != nullptr);
  1262. auto* refresh =
  1263. reinterpret_cast<const grpc_google_refresh_token_credentials*>(
  1264. creds->call_creds());
  1265. GPR_ASSERT(strcmp(refresh->refresh_token().client_id,
  1266. "32555999999.apps.googleusercontent.com") == 0);
  1267. creds->Unref();
  1268. gpr_setenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */
  1269. }
  1270. static int default_creds_metadata_server_detection_httpcli_get_success_override(
  1271. const grpc_httpcli_request* request, grpc_millis /*deadline*/,
  1272. grpc_closure* on_done, grpc_httpcli_response* response) {
  1273. *response = http_response(200, "");
  1274. grpc_http_header* headers =
  1275. static_cast<grpc_http_header*>(gpr_malloc(sizeof(*headers) * 1));
  1276. headers[0].key = gpr_strdup("Metadata-Flavor");
  1277. headers[0].value = gpr_strdup("Google");
  1278. response->hdr_count = 1;
  1279. response->hdrs = headers;
  1280. GPR_ASSERT(strcmp(request->http.path, "/") == 0);
  1281. GPR_ASSERT(strcmp(request->host, "metadata.google.internal.") == 0);
  1282. grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, GRPC_ERROR_NONE);
  1283. return 1;
  1284. }
  1285. static char* null_well_known_creds_path_getter(void) { return nullptr; }
  1286. static bool test_gce_tenancy_checker(void) {
  1287. g_test_gce_tenancy_checker_called = true;
  1288. return g_test_is_on_gce;
  1289. }
  1290. static void test_google_default_creds_gce(void) {
  1291. grpc_core::ExecCtx exec_ctx;
  1292. expected_md emd[] = {
  1293. {"authorization", "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"}};
  1294. request_metadata_state* state =
  1295. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  1296. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  1297. nullptr, nullptr};
  1298. grpc_flush_cached_google_default_credentials();
  1299. gpr_setenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */
  1300. grpc_override_well_known_credentials_path_getter(
  1301. null_well_known_creds_path_getter);
  1302. set_gce_tenancy_checker_for_testing(test_gce_tenancy_checker);
  1303. g_test_gce_tenancy_checker_called = false;
  1304. g_test_is_on_gce = true;
  1305. /* Simulate a successful detection of GCE. */
  1306. grpc_composite_channel_credentials* creds =
  1307. reinterpret_cast<grpc_composite_channel_credentials*>(
  1308. grpc_google_default_credentials_create(nullptr));
  1309. /* Verify that the default creds actually embeds a GCE creds. */
  1310. GPR_ASSERT(creds != nullptr);
  1311. GPR_ASSERT(creds->call_creds() != nullptr);
  1312. grpc_httpcli_set_override(compute_engine_httpcli_get_success_override,
  1313. httpcli_post_should_not_be_called);
  1314. run_request_metadata_test(creds->mutable_call_creds(), auth_md_ctx, state);
  1315. grpc_core::ExecCtx::Get()->Flush();
  1316. GPR_ASSERT(g_test_gce_tenancy_checker_called == true);
  1317. /* Cleanup. */
  1318. creds->Unref();
  1319. grpc_httpcli_set_override(nullptr, nullptr);
  1320. grpc_override_well_known_credentials_path_getter(nullptr);
  1321. }
  1322. static void test_google_default_creds_non_gce(void) {
  1323. grpc_core::ExecCtx exec_ctx;
  1324. expected_md emd[] = {
  1325. {"authorization", "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_"}};
  1326. request_metadata_state* state =
  1327. make_request_metadata_state(GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE(emd));
  1328. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  1329. nullptr, nullptr};
  1330. grpc_flush_cached_google_default_credentials();
  1331. gpr_setenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */
  1332. grpc_override_well_known_credentials_path_getter(
  1333. null_well_known_creds_path_getter);
  1334. set_gce_tenancy_checker_for_testing(test_gce_tenancy_checker);
  1335. g_test_gce_tenancy_checker_called = false;
  1336. g_test_is_on_gce = false;
  1337. /* Simulate a successful detection of metadata server. */
  1338. grpc_httpcli_set_override(
  1339. default_creds_metadata_server_detection_httpcli_get_success_override,
  1340. httpcli_post_should_not_be_called);
  1341. grpc_composite_channel_credentials* creds =
  1342. reinterpret_cast<grpc_composite_channel_credentials*>(
  1343. grpc_google_default_credentials_create(nullptr));
  1344. /* Verify that the default creds actually embeds a GCE creds. */
  1345. GPR_ASSERT(creds != nullptr);
  1346. GPR_ASSERT(creds->call_creds() != nullptr);
  1347. grpc_httpcli_set_override(compute_engine_httpcli_get_success_override,
  1348. httpcli_post_should_not_be_called);
  1349. run_request_metadata_test(creds->mutable_call_creds(), auth_md_ctx, state);
  1350. grpc_core::ExecCtx::Get()->Flush();
  1351. GPR_ASSERT(g_test_gce_tenancy_checker_called == true);
  1352. /* Cleanup. */
  1353. creds->Unref();
  1354. grpc_httpcli_set_override(nullptr, nullptr);
  1355. grpc_override_well_known_credentials_path_getter(nullptr);
  1356. }
  1357. static int default_creds_gce_detection_httpcli_get_failure_override(
  1358. const grpc_httpcli_request* request, grpc_millis /*deadline*/,
  1359. grpc_closure* on_done, grpc_httpcli_response* response) {
  1360. /* No magic header. */
  1361. GPR_ASSERT(strcmp(request->http.path, "/") == 0);
  1362. GPR_ASSERT(strcmp(request->host, "metadata.google.internal.") == 0);
  1363. *response = http_response(200, "");
  1364. grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, GRPC_ERROR_NONE);
  1365. return 1;
  1366. }
  1367. static void test_no_google_default_creds(void) {
  1368. grpc_flush_cached_google_default_credentials();
  1369. gpr_setenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */
  1370. grpc_override_well_known_credentials_path_getter(
  1371. null_well_known_creds_path_getter);
  1372. set_gce_tenancy_checker_for_testing(test_gce_tenancy_checker);
  1373. g_test_gce_tenancy_checker_called = false;
  1374. g_test_is_on_gce = false;
  1375. grpc_httpcli_set_override(
  1376. default_creds_gce_detection_httpcli_get_failure_override,
  1377. httpcli_post_should_not_be_called);
  1378. /* Simulate a successful detection of GCE. */
  1379. GPR_ASSERT(grpc_google_default_credentials_create(nullptr) == nullptr);
  1380. /* Try a second one. GCE detection should occur again. */
  1381. g_test_gce_tenancy_checker_called = false;
  1382. GPR_ASSERT(grpc_google_default_credentials_create(nullptr) == nullptr);
  1383. GPR_ASSERT(g_test_gce_tenancy_checker_called == true);
  1384. /* Cleanup. */
  1385. grpc_override_well_known_credentials_path_getter(nullptr);
  1386. grpc_httpcli_set_override(nullptr, nullptr);
  1387. }
  1388. static void test_google_default_creds_call_creds_specified(void) {
  1389. }
  1390. typedef enum {
  1391. PLUGIN_INITIAL_STATE,
  1392. PLUGIN_GET_METADATA_CALLED_STATE,
  1393. PLUGIN_DESTROY_CALLED_STATE
  1394. } plugin_state;
  1395. static const expected_md plugin_md[] = {{"foo", "bar"}, {"hi", "there"}};
  1396. static int plugin_get_metadata_success(
  1397. void* state, grpc_auth_metadata_context context,
  1398. grpc_credentials_plugin_metadata_cb /*cb*/, void* /*user_data*/,
  1399. grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX],
  1400. size_t* num_creds_md, grpc_status_code* /*status*/,
  1401. const char** /*error_details*/) {
  1402. GPR_ASSERT(strcmp(context.service_url, test_service_url) == 0);
  1403. GPR_ASSERT(strcmp(context.method_name, test_method) == 0);
  1404. GPR_ASSERT(context.channel_auth_context == nullptr);
  1405. GPR_ASSERT(context.reserved == nullptr);
  1406. GPR_ASSERT(GPR_ARRAY_SIZE(plugin_md) <
  1407. GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX);
  1408. plugin_state* s = static_cast<plugin_state*>(state);
  1409. *s = PLUGIN_GET_METADATA_CALLED_STATE;
  1410. for (size_t i = 0; i < GPR_ARRAY_SIZE(plugin_md); ++i) {
  1411. memset(&creds_md[i], 0, sizeof(grpc_metadata));
  1412. creds_md[i].key = grpc_slice_from_copied_string(plugin_md[i].key);
  1413. creds_md[i].value = grpc_slice_from_copied_string(plugin_md[i].value);
  1414. }
  1415. *num_creds_md = GPR_ARRAY_SIZE(plugin_md);
  1416. return true; // Synchronous return.
  1417. }
  1418. static const char* plugin_error_details = "Could not get metadata for plugin.";
  1419. static int plugin_get_metadata_failure(
  1420. void* state, grpc_auth_metadata_context context,
  1421. grpc_credentials_plugin_metadata_cb /*cb*/, void* /*user_data*/,
  1422. grpc_metadata /*creds_md*/[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX],
  1423. size_t* /*num_creds_md*/, grpc_status_code* status,
  1424. const char** error_details) {
  1425. GPR_ASSERT(strcmp(context.service_url, test_service_url) == 0);
  1426. GPR_ASSERT(strcmp(context.method_name, test_method) == 0);
  1427. GPR_ASSERT(context.channel_auth_context == nullptr);
  1428. GPR_ASSERT(context.reserved == nullptr);
  1429. plugin_state* s = static_cast<plugin_state*>(state);
  1430. *s = PLUGIN_GET_METADATA_CALLED_STATE;
  1431. *status = GRPC_STATUS_UNAUTHENTICATED;
  1432. *error_details = gpr_strdup(plugin_error_details);
  1433. return true; // Synchronous return.
  1434. }
  1435. static void plugin_destroy(void* state) {
  1436. plugin_state* s = static_cast<plugin_state*>(state);
  1437. *s = PLUGIN_DESTROY_CALLED_STATE;
  1438. }
  1439. static char* plugin_debug_string(void* state) {
  1440. plugin_state* s = static_cast<plugin_state*>(state);
  1441. char* ret = nullptr;
  1442. switch (*s) {
  1443. case PLUGIN_INITIAL_STATE:
  1444. gpr_asprintf(&ret, "TestPluginCredentials{state:INITIAL}");
  1445. break;
  1446. case PLUGIN_GET_METADATA_CALLED_STATE:
  1447. gpr_asprintf(&ret, "TestPluginCredentials{state:GET_METADATA_CALLED}");
  1448. break;
  1449. case PLUGIN_DESTROY_CALLED_STATE:
  1450. gpr_asprintf(&ret, "TestPluginCredentials{state:DESTROY}");
  1451. break;
  1452. default:
  1453. gpr_asprintf(&ret, "TestPluginCredentials{state:UNKNOWN}");
  1454. break;
  1455. }
  1456. return ret;
  1457. }
  1458. static void test_metadata_plugin_success(void) {
  1459. const char expected_creds_debug_string[] =
  1460. "TestPluginCredentials{state:GET_METADATA_CALLED}";
  1461. plugin_state state = PLUGIN_INITIAL_STATE;
  1462. grpc_metadata_credentials_plugin plugin;
  1463. grpc_core::ExecCtx exec_ctx;
  1464. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  1465. nullptr, nullptr};
  1466. request_metadata_state* md_state = make_request_metadata_state(
  1467. GRPC_ERROR_NONE, plugin_md, GPR_ARRAY_SIZE(plugin_md));
  1468. plugin.state = &state;
  1469. plugin.get_metadata = plugin_get_metadata_success;
  1470. plugin.destroy = plugin_destroy;
  1471. plugin.debug_string = plugin_debug_string;
  1472. grpc_call_credentials* creds = grpc_metadata_credentials_create_from_plugin(
  1473. plugin, GRPC_PRIVACY_AND_INTEGRITY, nullptr);
  1474. /* Check security level. */
  1475. GPR_ASSERT(creds->min_security_level() == GRPC_PRIVACY_AND_INTEGRITY);
  1476. GPR_ASSERT(state == PLUGIN_INITIAL_STATE);
  1477. run_request_metadata_test(creds, auth_md_ctx, md_state);
  1478. GPR_ASSERT(state == PLUGIN_GET_METADATA_CALLED_STATE);
  1479. GPR_ASSERT(
  1480. strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0);
  1481. creds->Unref();
  1482. GPR_ASSERT(state == PLUGIN_DESTROY_CALLED_STATE);
  1483. }
  1484. static void test_metadata_plugin_failure(void) {
  1485. const char expected_creds_debug_string[] =
  1486. "TestPluginCredentials{state:GET_METADATA_CALLED}";
  1487. plugin_state state = PLUGIN_INITIAL_STATE;
  1488. grpc_metadata_credentials_plugin plugin;
  1489. grpc_core::ExecCtx exec_ctx;
  1490. grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
  1491. nullptr, nullptr};
  1492. char* expected_error;
  1493. gpr_asprintf(&expected_error,
  1494. "Getting metadata from plugin failed with error: %s",
  1495. plugin_error_details);
  1496. request_metadata_state* md_state = make_request_metadata_state(
  1497. GRPC_ERROR_CREATE_FROM_COPIED_STRING(expected_error), nullptr, 0);
  1498. gpr_free(expected_error);
  1499. plugin.state = &state;
  1500. plugin.get_metadata = plugin_get_metadata_failure;
  1501. plugin.destroy = plugin_destroy;
  1502. plugin.debug_string = plugin_debug_string;
  1503. grpc_call_credentials* creds = grpc_metadata_credentials_create_from_plugin(
  1504. plugin, GRPC_PRIVACY_AND_INTEGRITY, nullptr);
  1505. GPR_ASSERT(state == PLUGIN_INITIAL_STATE);
  1506. run_request_metadata_test(creds, auth_md_ctx, md_state);
  1507. GPR_ASSERT(state == PLUGIN_GET_METADATA_CALLED_STATE);
  1508. GPR_ASSERT(
  1509. strcmp(creds->debug_string().c_str(), expected_creds_debug_string) == 0);
  1510. creds->Unref();
  1511. GPR_ASSERT(state == PLUGIN_DESTROY_CALLED_STATE);
  1512. }
  1513. static void test_get_well_known_google_credentials_file_path(void) {
  1514. char* path;
  1515. char* home = gpr_getenv("HOME");
  1516. bool restore_home_env = false;
  1517. #if defined(GRPC_BAZEL_BUILD) && \
  1518. (defined(GPR_POSIX_ENV) || defined(GPR_LINUX_ENV))
  1519. // when running under bazel locally, the HOME variable is not set
  1520. // so we set it to some fake value
  1521. restore_home_env = true;
  1522. gpr_setenv("HOME", "/fake/home/for/bazel");
  1523. #endif /* defined(GRPC_BAZEL_BUILD) && (defined(GPR_POSIX_ENV) || \
  1524. defined(GPR_LINUX_ENV)) */
  1525. path = grpc_get_well_known_google_credentials_file_path();
  1526. GPR_ASSERT(path != nullptr);
  1527. gpr_free(path);
  1528. #if defined(GPR_POSIX_ENV) || defined(GPR_LINUX_ENV)
  1529. restore_home_env = true;
  1530. gpr_unsetenv("HOME");
  1531. path = grpc_get_well_known_google_credentials_file_path();
  1532. GPR_ASSERT(path == nullptr);
  1533. gpr_free(path);
  1534. #endif /* GPR_POSIX_ENV || GPR_LINUX_ENV */
  1535. if (restore_home_env) {
  1536. if (home) {
  1537. gpr_setenv("HOME", home);
  1538. } else {
  1539. gpr_unsetenv("HOME");
  1540. }
  1541. }
  1542. gpr_free(home);
  1543. }
  1544. static void test_channel_creds_duplicate_without_call_creds(void) {
  1545. const char expected_creds_debug_string[] =
  1546. "AccessTokenCredentials{Token:present}";
  1547. grpc_core::ExecCtx exec_ctx;
  1548. grpc_channel_credentials* channel_creds =
  1549. grpc_fake_transport_security_credentials_create();
  1550. grpc_core::RefCountedPtr<grpc_channel_credentials> dup =
  1551. channel_creds->duplicate_without_call_credentials();
  1552. GPR_ASSERT(dup == channel_creds);
  1553. dup.reset();
  1554. grpc_call_credentials* call_creds =
  1555. grpc_access_token_credentials_create("blah", nullptr);
  1556. grpc_channel_credentials* composite_creds =
  1557. grpc_composite_channel_credentials_create(channel_creds, call_creds,
  1558. nullptr);
  1559. GPR_ASSERT(strcmp(call_creds->debug_string().c_str(),
  1560. expected_creds_debug_string) == 0);
  1561. call_creds->Unref();
  1562. dup = composite_creds->duplicate_without_call_credentials();
  1563. GPR_ASSERT(dup == channel_creds);
  1564. dup.reset();
  1565. channel_creds->Unref();
  1566. composite_creds->Unref();
  1567. }
  1568. typedef struct {
  1569. const char* url_scheme;
  1570. const char* call_host;
  1571. const char* call_method;
  1572. const char* desired_service_url;
  1573. const char* desired_method_name;
  1574. } auth_metadata_context_test_case;
  1575. static void test_auth_metadata_context(void) {
  1576. auth_metadata_context_test_case test_cases[] = {
  1577. // No service nor method.
  1578. {"https", "www.foo.com", "", "https://www.foo.com", ""},
  1579. // No method.
  1580. {"https", "www.foo.com", "/Service", "https://www.foo.com/Service", ""},
  1581. // Empty service and method.
  1582. {"https", "www.foo.com", "//", "https://www.foo.com/", ""},
  1583. // Empty method.
  1584. {"https", "www.foo.com", "/Service/", "https://www.foo.com/Service", ""},
  1585. // Malformed url.
  1586. {"https", "www.foo.com:", "/Service/", "https://www.foo.com:/Service",
  1587. ""},
  1588. // https, default explicit port.
  1589. {"https", "www.foo.com:443", "/Service/FooMethod",
  1590. "https://www.foo.com/Service", "FooMethod"},
  1591. // https, default implicit port.
  1592. {"https", "www.foo.com", "/Service/FooMethod",
  1593. "https://www.foo.com/Service", "FooMethod"},
  1594. // https with ipv6 literal, default explicit port.
  1595. {"https", "[1080:0:0:0:8:800:200C:417A]:443", "/Service/FooMethod",
  1596. "https://[1080:0:0:0:8:800:200C:417A]/Service", "FooMethod"},
  1597. // https with ipv6 literal, default implicit port.
  1598. {"https", "[1080:0:0:0:8:800:200C:443]", "/Service/FooMethod",
  1599. "https://[1080:0:0:0:8:800:200C:443]/Service", "FooMethod"},
  1600. // https, custom port.
  1601. {"https", "www.foo.com:8888", "/Service/FooMethod",
  1602. "https://www.foo.com:8888/Service", "FooMethod"},
  1603. // https with ipv6 literal, custom port.
  1604. {"https", "[1080:0:0:0:8:800:200C:417A]:8888", "/Service/FooMethod",
  1605. "https://[1080:0:0:0:8:800:200C:417A]:8888/Service", "FooMethod"},
  1606. // custom url scheme, https default port.
  1607. {"blah", "www.foo.com:443", "/Service/FooMethod",
  1608. "blah://www.foo.com:443/Service", "FooMethod"}};
  1609. for (uint32_t i = 0; i < GPR_ARRAY_SIZE(test_cases); i++) {
  1610. const char* url_scheme = test_cases[i].url_scheme;
  1611. grpc_slice call_host =
  1612. grpc_slice_from_copied_string(test_cases[i].call_host);
  1613. grpc_slice call_method =
  1614. grpc_slice_from_copied_string(test_cases[i].call_method);
  1615. grpc_auth_metadata_context auth_md_context;
  1616. memset(&auth_md_context, 0, sizeof(auth_md_context));
  1617. grpc_auth_metadata_context_build(url_scheme, call_host, call_method,
  1618. nullptr, &auth_md_context);
  1619. if (strcmp(auth_md_context.service_url,
  1620. test_cases[i].desired_service_url) != 0) {
  1621. gpr_log(GPR_ERROR, "Invalid service url, want: %s, got %s.",
  1622. test_cases[i].desired_service_url, auth_md_context.service_url);
  1623. GPR_ASSERT(false);
  1624. }
  1625. if (strcmp(auth_md_context.method_name,
  1626. test_cases[i].desired_method_name) != 0) {
  1627. gpr_log(GPR_ERROR, "Invalid method name, want: %s, got %s.",
  1628. test_cases[i].desired_method_name, auth_md_context.method_name);
  1629. GPR_ASSERT(false);
  1630. }
  1631. GPR_ASSERT(auth_md_context.channel_auth_context == nullptr);
  1632. grpc_slice_unref(call_host);
  1633. grpc_slice_unref(call_method);
  1634. grpc_auth_metadata_context_reset(&auth_md_context);
  1635. }
  1636. }
  1637. int main(int argc, char** argv) {
  1638. grpc::testing::TestEnvironment env(argc, argv);
  1639. grpc_init();
  1640. test_empty_md_array();
  1641. test_add_to_empty_md_array();
  1642. test_add_abunch_to_md_array();
  1643. test_oauth2_token_fetcher_creds_parsing_ok();
  1644. test_oauth2_token_fetcher_creds_parsing_bad_http_status();
  1645. test_oauth2_token_fetcher_creds_parsing_empty_http_body();
  1646. test_oauth2_token_fetcher_creds_parsing_invalid_json();
  1647. test_oauth2_token_fetcher_creds_parsing_missing_token();
  1648. test_oauth2_token_fetcher_creds_parsing_missing_token_type();
  1649. test_oauth2_token_fetcher_creds_parsing_missing_token_lifetime();
  1650. test_google_iam_creds();
  1651. test_access_token_creds();
  1652. test_channel_oauth2_composite_creds();
  1653. test_oauth2_google_iam_composite_creds();
  1654. test_channel_oauth2_google_iam_composite_creds();
  1655. test_compute_engine_creds_success();
  1656. test_compute_engine_creds_failure();
  1657. test_refresh_token_creds_success();
  1658. test_refresh_token_creds_failure();
  1659. test_valid_sts_creds_options();
  1660. test_invalid_sts_creds_options();
  1661. test_sts_creds_success();
  1662. test_sts_creds_no_actor_token_success();
  1663. test_sts_creds_load_token_failure();
  1664. test_sts_creds_http_failure();
  1665. test_sts_creds_token_file_not_found();
  1666. test_jwt_creds_lifetime();
  1667. test_jwt_creds_success();
  1668. test_jwt_creds_signing_failure();
  1669. test_google_default_creds_auth_key();
  1670. test_google_default_creds_refresh_token();
  1671. test_google_default_creds_gce();
  1672. test_google_default_creds_non_gce();
  1673. test_no_google_default_creds();
  1674. test_google_default_creds_call_creds_specified();
  1675. test_metadata_plugin_success();
  1676. test_metadata_plugin_failure();
  1677. test_get_well_known_google_credentials_file_path();
  1678. test_channel_creds_duplicate_without_call_creds();
  1679. test_auth_metadata_context();
  1680. grpc_shutdown();
  1681. return 0;
  1682. }