Skip to content

Commit 8577fe8

Browse files
authored
Merge pull request grpc#21641 from jboeuf/sts_creds_fix_empty_actor_token
Fix STS credentials to ignore empty actor token path.
2 parents 99fdc58 + 323e23a commit 8577fe8

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

src/core/lib/security/credentials/oauth2/oauth2_credentials.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ class StsTokenFetcherCredentials
611611
MaybeAddToBody(&body_strvec, "scope", scope_.get());
612612
MaybeAddToBody(&body_strvec, "requested_token_type",
613613
requested_token_type_.get());
614-
if (actor_token_path_ != nullptr) {
614+
if ((actor_token_path_ != nullptr) && *actor_token_path_ != '\0') {
615615
err = LoadTokenFile(actor_token_path_.get(), &actor_token);
616616
if (err != GRPC_ERROR_NONE) return cleanup();
617617
MaybeAddToBody(

test/core/security/credentials_test.cc

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -864,8 +864,8 @@ static void test_invalid_sts_creds_options(void) {
864864
}
865865

866866
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) {
869869
// Check that the body is constructed properly.
870870
GPR_ASSERT(body != nullptr);
871871
GPR_ASSERT(body_size != 0);
@@ -882,10 +882,15 @@ static void validate_sts_token_http_request(const grpc_httpcli_request* request,
882882
test_signed_jwt) == 0);
883883
GPR_ASSERT(strcmp(grpc_uri_get_query_arg(url, "subject_token_type"),
884884
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+
}
889894
grpc_uri_destroy(url);
890895
gpr_free(get_url_equivalent);
891896

@@ -903,7 +908,17 @@ static int sts_token_httpcli_post_success(const grpc_httpcli_request* request,
903908
grpc_millis /*deadline*/,
904909
grpc_closure* on_done,
905910
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);
907922
*response = http_response(200, valid_sts_json_response);
908923
grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, GRPC_ERROR_NONE);
909924
return 1;
@@ -967,6 +982,51 @@ static void test_sts_creds_success(void) {
967982
gpr_free(actor_token_path);
968983
}
969984

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+
9701030
static void test_sts_creds_load_token_failure(void) {
9711031
grpc_core::ExecCtx exec_ctx;
9721032
request_metadata_state* state = make_request_metadata_state(
@@ -1624,6 +1684,7 @@ int main(int argc, char** argv) {
16241684
test_valid_sts_creds_options();
16251685
test_invalid_sts_creds_options();
16261686
test_sts_creds_success();
1687+
test_sts_creds_no_actor_token_success();
16271688
test_sts_creds_load_token_failure();
16281689
test_sts_creds_http_failure();
16291690
test_jwt_creds_lifetime();

0 commit comments

Comments
 (0)