@@ -864,8 +864,8 @@ static void test_invalid_sts_creds_options(void) {
864
864
}
865
865
866
866
static void validate_sts_token_http_request (const grpc_httpcli_request* request,
867
- const char * body,
868
- size_t body_size ) {
867
+ const char * body, size_t body_size,
868
+ bool expect_actor_token ) {
869
869
// Check that the body is constructed properly.
870
870
GPR_ASSERT (body != nullptr );
871
871
GPR_ASSERT (body_size != 0 );
@@ -882,10 +882,15 @@ static void validate_sts_token_http_request(const grpc_httpcli_request* request,
882
882
test_signed_jwt) == 0 );
883
883
GPR_ASSERT (strcmp (grpc_uri_get_query_arg (url, " subject_token_type" ),
884
884
test_signed_jwt_token_type) == 0 );
885
- GPR_ASSERT (strcmp (grpc_uri_get_query_arg (url, " actor_token" ),
886
- test_signed_jwt2) == 0 );
887
- GPR_ASSERT (strcmp (grpc_uri_get_query_arg (url, " actor_token_type" ),
888
- test_signed_jwt_token_type2) == 0 );
885
+ if (expect_actor_token) {
886
+ GPR_ASSERT (strcmp (grpc_uri_get_query_arg (url, " actor_token" ),
887
+ test_signed_jwt2) == 0 );
888
+ GPR_ASSERT (strcmp (grpc_uri_get_query_arg (url, " actor_token_type" ),
889
+ test_signed_jwt_token_type2) == 0 );
890
+ } else {
891
+ GPR_ASSERT (grpc_uri_get_query_arg (url, " actor_token" ) == nullptr );
892
+ GPR_ASSERT (grpc_uri_get_query_arg (url, " actor_token_type" ) == nullptr );
893
+ }
889
894
grpc_uri_destroy (url);
890
895
gpr_free (get_url_equivalent);
891
896
@@ -903,7 +908,17 @@ static int sts_token_httpcli_post_success(const grpc_httpcli_request* request,
903
908
grpc_millis /* deadline*/ ,
904
909
grpc_closure* on_done,
905
910
grpc_httpcli_response* response) {
906
- validate_sts_token_http_request (request, body, body_size);
911
+ validate_sts_token_http_request (request, body, body_size, true );
912
+ *response = http_response (200 , valid_sts_json_response);
913
+ grpc_core::ExecCtx::Run (DEBUG_LOCATION, on_done, GRPC_ERROR_NONE);
914
+ return 1 ;
915
+ }
916
+
917
+ static int sts_token_httpcli_post_success_no_actor_token (
918
+ const grpc_httpcli_request* request, const char * body, size_t body_size,
919
+ grpc_millis /* deadline*/ , grpc_closure* on_done,
920
+ grpc_httpcli_response* response) {
921
+ validate_sts_token_http_request (request, body, body_size, false );
907
922
*response = http_response (200 , valid_sts_json_response);
908
923
grpc_core::ExecCtx::Run (DEBUG_LOCATION, on_done, GRPC_ERROR_NONE);
909
924
return 1 ;
@@ -967,6 +982,51 @@ static void test_sts_creds_success(void) {
967
982
gpr_free (actor_token_path);
968
983
}
969
984
985
+ static void test_sts_creds_no_actor_token_success (void ) {
986
+ grpc_core::ExecCtx exec_ctx;
987
+ expected_md emd[] = {
988
+ {" authorization" , " Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_" }};
989
+ grpc_auth_metadata_context auth_md_ctx = {test_service_url, test_method,
990
+ nullptr , nullptr };
991
+ char * subject_token_path = write_tmp_jwt_file (test_signed_jwt);
992
+ grpc_sts_credentials_options valid_options = {
993
+ test_sts_endpoint_url, // sts_endpoint_url
994
+ " resource" , // resource
995
+ " audience" , // audience
996
+ " scope" , // scope
997
+ " requested_token_type" , // requested_token_type
998
+ subject_token_path, // subject_token_path
999
+ test_signed_jwt_token_type, // subject_token_type
1000
+ " " , // actor_token_path
1001
+ " " // actor_token_type
1002
+ };
1003
+ grpc_call_credentials* creds =
1004
+ grpc_sts_credentials_create (&valid_options, nullptr );
1005
+
1006
+ /* Check security level. */
1007
+ GPR_ASSERT (creds->min_security_level () == GRPC_PRIVACY_AND_INTEGRITY);
1008
+
1009
+ /* First request: http put should be called. */
1010
+ request_metadata_state* state =
1011
+ make_request_metadata_state (GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE (emd));
1012
+ grpc_httpcli_set_override (httpcli_get_should_not_be_called,
1013
+ sts_token_httpcli_post_success_no_actor_token);
1014
+ run_request_metadata_test (creds, auth_md_ctx, state);
1015
+ grpc_core::ExecCtx::Get ()->Flush ();
1016
+
1017
+ /* Second request: the cached token should be served directly. */
1018
+ state =
1019
+ make_request_metadata_state (GRPC_ERROR_NONE, emd, GPR_ARRAY_SIZE (emd));
1020
+ grpc_httpcli_set_override (httpcli_get_should_not_be_called,
1021
+ httpcli_post_should_not_be_called);
1022
+ run_request_metadata_test (creds, auth_md_ctx, state);
1023
+ grpc_core::ExecCtx::Get ()->Flush ();
1024
+
1025
+ creds->Unref ();
1026
+ grpc_httpcli_set_override (nullptr , nullptr );
1027
+ gpr_free (subject_token_path);
1028
+ }
1029
+
970
1030
static void test_sts_creds_load_token_failure (void ) {
971
1031
grpc_core::ExecCtx exec_ctx;
972
1032
request_metadata_state* state = make_request_metadata_state (
@@ -1624,6 +1684,7 @@ int main(int argc, char** argv) {
1624
1684
test_valid_sts_creds_options ();
1625
1685
test_invalid_sts_creds_options ();
1626
1686
test_sts_creds_success ();
1687
+ test_sts_creds_no_actor_token_success ();
1627
1688
test_sts_creds_load_token_failure ();
1628
1689
test_sts_creds_http_failure ();
1629
1690
test_jwt_creds_lifetime ();
0 commit comments