aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/security/credentials/oauth2
diff options
context:
space:
mode:
authorGravatar Yash Tibrewal <yashkt@google.com>2017-10-13 16:07:13 -0700
committerGravatar Yash Tibrewal <yashkt@google.com>2017-10-18 17:12:19 -0700
commit0ee7574732a06e8cace4e099a678f4bd5dbff679 (patch)
treee43d5de442fdcc3d39cd5af687f319fa39612d3f /src/core/lib/security/credentials/oauth2
parent6bf5f833efe2cb9e2ecc14358dd9699cd5d05263 (diff)
Removing instances of exec_ctx being passed around in functions in
src/core. exec_ctx is now a thread_local pointer of type ExecCtx instead of grpc_exec_ctx which is initialized whenever ExecCtx is instantiated. ExecCtx also keeps track of the previous exec_ctx so that nesting of exec_ctx is allowed. This means that there is only one exec_ctx being used at any time. Also, grpc_exec_ctx_finish is called in the destructor of the object, and the previous exec_ctx is restored to avoid breaking current functionality. The code still explicitly calls grpc_exec_ctx_finish because removing all such instances causes the code to break.
Diffstat (limited to 'src/core/lib/security/credentials/oauth2')
-rw-r--r--src/core/lib/security/credentials/oauth2/oauth2_credentials.cc104
-rw-r--r--src/core/lib/security/credentials/oauth2/oauth2_credentials.h7
2 files changed, 50 insertions, 61 deletions
diff --git a/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc b/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc
index f52a424e36..7dd75ed4e3 100644
--- a/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc
+++ b/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc
@@ -103,21 +103,19 @@ void grpc_auth_refresh_token_destruct(grpc_auth_refresh_token *refresh_token) {
// Oauth2 Token Fetcher credentials.
//
-static void oauth2_token_fetcher_destruct(grpc_exec_ctx *exec_ctx,
- grpc_call_credentials *creds) {
+static void oauth2_token_fetcher_destruct(grpc_call_credentials *creds) {
grpc_oauth2_token_fetcher_credentials *c =
(grpc_oauth2_token_fetcher_credentials *)creds;
- GRPC_MDELEM_UNREF(exec_ctx, c->access_token_md);
+ GRPC_MDELEM_UNREF(c->access_token_md);
gpr_mu_destroy(&c->mu);
- grpc_pollset_set_destroy(exec_ctx,
- grpc_polling_entity_pollset_set(&c->pollent));
- grpc_httpcli_context_destroy(exec_ctx, &c->httpcli_context);
+ grpc_pollset_set_destroy(grpc_polling_entity_pollset_set(&c->pollent));
+ grpc_httpcli_context_destroy(&c->httpcli_context);
}
grpc_credentials_status
grpc_oauth2_token_fetcher_credentials_parse_server_response(
- grpc_exec_ctx *exec_ctx, const grpc_http_response *response,
- grpc_mdelem *token_md, grpc_millis *token_lifetime) {
+ const grpc_http_response *response, grpc_mdelem *token_md,
+ grpc_millis *token_lifetime) {
char *null_terminated_body = NULL;
char *new_access_token = NULL;
grpc_credentials_status status = GRPC_CREDENTIALS_OK;
@@ -184,9 +182,8 @@ grpc_oauth2_token_fetcher_credentials_parse_server_response(
gpr_asprintf(&new_access_token, "%s %s", token_type->value,
access_token->value);
*token_lifetime = strtol(expires_in->value, NULL, 10) * GPR_MS_PER_SEC;
- if (!GRPC_MDISNULL(*token_md)) GRPC_MDELEM_UNREF(exec_ctx, *token_md);
+ if (!GRPC_MDISNULL(*token_md)) GRPC_MDELEM_UNREF(*token_md);
*token_md = grpc_mdelem_from_slices(
- exec_ctx,
grpc_slice_from_static_string(GRPC_AUTHORIZATION_METADATA_KEY),
grpc_slice_from_copied_string(new_access_token));
status = GRPC_CREDENTIALS_OK;
@@ -194,7 +191,7 @@ grpc_oauth2_token_fetcher_credentials_parse_server_response(
end:
if (status != GRPC_CREDENTIALS_OK && !GRPC_MDISNULL(*token_md)) {
- GRPC_MDELEM_UNREF(exec_ctx, *token_md);
+ GRPC_MDELEM_UNREF(*token_md);
*token_md = GRPC_MDNULL;
}
if (null_terminated_body != NULL) gpr_free(null_terminated_body);
@@ -203,8 +200,7 @@ end:
return status;
}
-static void on_oauth2_token_fetcher_http_response(grpc_exec_ctx *exec_ctx,
- void *user_data,
+static void on_oauth2_token_fetcher_http_response(void *user_data,
grpc_error *error) {
GRPC_LOG_IF_ERROR("oauth_fetch", GRPC_ERROR_REF(error));
grpc_credentials_metadata_request *r =
@@ -215,14 +211,13 @@ static void on_oauth2_token_fetcher_http_response(grpc_exec_ctx *exec_ctx,
grpc_millis token_lifetime;
grpc_credentials_status status =
grpc_oauth2_token_fetcher_credentials_parse_server_response(
- exec_ctx, &r->response, &access_token_md, &token_lifetime);
+ &r->response, &access_token_md, &token_lifetime);
// Update cache and grab list of pending requests.
gpr_mu_lock(&c->mu);
c->token_fetch_pending = false;
c->access_token_md = GRPC_MDELEM_REF(access_token_md);
- c->token_expiration = status == GRPC_CREDENTIALS_OK
- ? grpc_exec_ctx_now(exec_ctx) + token_lifetime
- : 0;
+ c->token_expiration =
+ status == GRPC_CREDENTIALS_OK ? grpc_exec_ctx_now() + token_lifetime : 0;
grpc_oauth2_pending_get_request_metadata *pending_request =
c->pending_requests;
c->pending_requests = NULL;
@@ -236,24 +231,22 @@ static void on_oauth2_token_fetcher_http_response(grpc_exec_ctx *exec_ctx,
error = GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
"Error occured when fetching oauth2 token.", &error, 1);
}
- GRPC_CLOSURE_SCHED(exec_ctx, pending_request->on_request_metadata, error);
+ GRPC_CLOSURE_SCHED(pending_request->on_request_metadata, error);
grpc_polling_entity_del_from_pollset_set(
- exec_ctx, pending_request->pollent,
- grpc_polling_entity_pollset_set(&c->pollent));
+ pending_request->pollent, grpc_polling_entity_pollset_set(&c->pollent));
grpc_oauth2_pending_get_request_metadata *prev = pending_request;
pending_request = pending_request->next;
gpr_free(prev);
}
- GRPC_MDELEM_UNREF(exec_ctx, access_token_md);
- grpc_call_credentials_unref(exec_ctx, r->creds);
- grpc_credentials_metadata_request_destroy(exec_ctx, r);
+ GRPC_MDELEM_UNREF(access_token_md);
+ grpc_call_credentials_unref(r->creds);
+ grpc_credentials_metadata_request_destroy(r);
}
static bool oauth2_token_fetcher_get_request_metadata(
- grpc_exec_ctx *exec_ctx, grpc_call_credentials *creds,
- grpc_polling_entity *pollent, grpc_auth_metadata_context context,
- grpc_credentials_mdelem_array *md_array, grpc_closure *on_request_metadata,
- grpc_error **error) {
+ grpc_call_credentials *creds, grpc_polling_entity *pollent,
+ grpc_auth_metadata_context context, grpc_credentials_mdelem_array *md_array,
+ grpc_closure *on_request_metadata, grpc_error **error) {
grpc_oauth2_token_fetcher_credentials *c =
(grpc_oauth2_token_fetcher_credentials *)creds;
// Check if we can use the cached token.
@@ -262,13 +255,13 @@ static bool oauth2_token_fetcher_get_request_metadata(
grpc_mdelem cached_access_token_md = GRPC_MDNULL;
gpr_mu_lock(&c->mu);
if (!GRPC_MDISNULL(c->access_token_md) &&
- (c->token_expiration + grpc_exec_ctx_now(exec_ctx) > refresh_threshold)) {
+ (c->token_expiration + grpc_exec_ctx_now() > refresh_threshold)) {
cached_access_token_md = GRPC_MDELEM_REF(c->access_token_md);
}
if (!GRPC_MDISNULL(cached_access_token_md)) {
gpr_mu_unlock(&c->mu);
grpc_credentials_mdelem_array_add(md_array, cached_access_token_md);
- GRPC_MDELEM_UNREF(exec_ctx, cached_access_token_md);
+ GRPC_MDELEM_UNREF(cached_access_token_md);
return true;
}
// Couldn't get the token from the cache.
@@ -280,7 +273,7 @@ static bool oauth2_token_fetcher_get_request_metadata(
pending_request->on_request_metadata = on_request_metadata;
pending_request->pollent = pollent;
grpc_polling_entity_add_to_pollset_set(
- exec_ctx, pollent, grpc_polling_entity_pollset_set(&c->pollent));
+ pollent, grpc_polling_entity_pollset_set(&c->pollent));
pending_request->next = c->pending_requests;
c->pending_requests = pending_request;
bool start_fetch = false;
@@ -291,17 +284,17 @@ static bool oauth2_token_fetcher_get_request_metadata(
gpr_mu_unlock(&c->mu);
if (start_fetch) {
grpc_call_credentials_ref(creds);
- c->fetch_func(exec_ctx, grpc_credentials_metadata_request_create(creds),
+ c->fetch_func(grpc_credentials_metadata_request_create(creds),
&c->httpcli_context, &c->pollent,
on_oauth2_token_fetcher_http_response,
- grpc_exec_ctx_now(exec_ctx) + refresh_threshold);
+ grpc_exec_ctx_now() + refresh_threshold);
}
return false;
}
static void oauth2_token_fetcher_cancel_get_request_metadata(
- grpc_exec_ctx *exec_ctx, grpc_call_credentials *creds,
- grpc_credentials_mdelem_array *md_array, grpc_error *error) {
+ grpc_call_credentials *creds, grpc_credentials_mdelem_array *md_array,
+ grpc_error *error) {
grpc_oauth2_token_fetcher_credentials *c =
(grpc_oauth2_token_fetcher_credentials *)creds;
gpr_mu_lock(&c->mu);
@@ -317,7 +310,7 @@ static void oauth2_token_fetcher_cancel_get_request_metadata(
c->pending_requests = pending_request->next;
}
// Invoke the callback immediately with an error.
- GRPC_CLOSURE_SCHED(exec_ctx, pending_request->on_request_metadata,
+ GRPC_CLOSURE_SCHED(pending_request->on_request_metadata,
GRPC_ERROR_REF(error));
gpr_free(pending_request);
break;
@@ -351,7 +344,7 @@ static grpc_call_credentials_vtable compute_engine_vtable = {
oauth2_token_fetcher_cancel_get_request_metadata};
static void compute_engine_fetch_oauth2(
- grpc_exec_ctx *exec_ctx, grpc_credentials_metadata_request *metadata_req,
+ grpc_credentials_metadata_request *metadata_req,
grpc_httpcli_context *httpcli_context, grpc_polling_entity *pollent,
grpc_iomgr_cb_func response_cb, grpc_millis deadline) {
grpc_http_header header = {(char *)"Metadata-Flavor", (char *)"Google"};
@@ -367,10 +360,10 @@ static void compute_engine_fetch_oauth2(
grpc_resource_quota *resource_quota =
grpc_resource_quota_create("oauth2_credentials");
grpc_httpcli_get(
- exec_ctx, httpcli_context, pollent, resource_quota, &request, deadline,
+ httpcli_context, pollent, resource_quota, &request, deadline,
GRPC_CLOSURE_CREATE(response_cb, metadata_req, grpc_schedule_on_exec_ctx),
&metadata_req->response);
- grpc_resource_quota_unref_internal(exec_ctx, resource_quota);
+ grpc_resource_quota_unref_internal(resource_quota);
}
grpc_call_credentials *grpc_google_compute_engine_credentials_create(
@@ -390,12 +383,11 @@ grpc_call_credentials *grpc_google_compute_engine_credentials_create(
// Google Refresh Token credentials.
//
-static void refresh_token_destruct(grpc_exec_ctx *exec_ctx,
- grpc_call_credentials *creds) {
+static void refresh_token_destruct(grpc_call_credentials *creds) {
grpc_google_refresh_token_credentials *c =
(grpc_google_refresh_token_credentials *)creds;
grpc_auth_refresh_token_destruct(&c->refresh_token);
- oauth2_token_fetcher_destruct(exec_ctx, &c->base.base);
+ oauth2_token_fetcher_destruct(&c->base.base);
}
static grpc_call_credentials_vtable refresh_token_vtable = {
@@ -403,7 +395,7 @@ static grpc_call_credentials_vtable refresh_token_vtable = {
oauth2_token_fetcher_cancel_get_request_metadata};
static void refresh_token_fetch_oauth2(
- grpc_exec_ctx *exec_ctx, grpc_credentials_metadata_request *metadata_req,
+ grpc_credentials_metadata_request *metadata_req,
grpc_httpcli_context *httpcli_context, grpc_polling_entity *pollent,
grpc_iomgr_cb_func response_cb, grpc_millis deadline) {
grpc_google_refresh_token_credentials *c =
@@ -427,11 +419,11 @@ static void refresh_token_fetch_oauth2(
grpc_resource_quota *resource_quota =
grpc_resource_quota_create("oauth2_credentials_refresh");
grpc_httpcli_post(
- exec_ctx, httpcli_context, pollent, resource_quota, &request, body,
- strlen(body), deadline,
+ httpcli_context, pollent, resource_quota, &request, body, strlen(body),
+ deadline,
GRPC_CLOSURE_CREATE(response_cb, metadata_req, grpc_schedule_on_exec_ctx),
&metadata_req->response);
- grpc_resource_quota_unref_internal(exec_ctx, resource_quota);
+ grpc_resource_quota_unref_internal(resource_quota);
gpr_free(body);
}
@@ -483,25 +475,23 @@ grpc_call_credentials *grpc_google_refresh_token_credentials_create(
// Oauth2 Access Token credentials.
//
-static void access_token_destruct(grpc_exec_ctx *exec_ctx,
- grpc_call_credentials *creds) {
+static void access_token_destruct(grpc_call_credentials *creds) {
grpc_access_token_credentials *c = (grpc_access_token_credentials *)creds;
- GRPC_MDELEM_UNREF(exec_ctx, c->access_token_md);
+ GRPC_MDELEM_UNREF(c->access_token_md);
}
static bool access_token_get_request_metadata(
- grpc_exec_ctx *exec_ctx, grpc_call_credentials *creds,
- grpc_polling_entity *pollent, grpc_auth_metadata_context context,
- grpc_credentials_mdelem_array *md_array, grpc_closure *on_request_metadata,
- grpc_error **error) {
+ grpc_call_credentials *creds, grpc_polling_entity *pollent,
+ grpc_auth_metadata_context context, grpc_credentials_mdelem_array *md_array,
+ grpc_closure *on_request_metadata, grpc_error **error) {
grpc_access_token_credentials *c = (grpc_access_token_credentials *)creds;
grpc_credentials_mdelem_array_add(md_array, c->access_token_md);
return true;
}
static void access_token_cancel_get_request_metadata(
- grpc_exec_ctx *exec_ctx, grpc_call_credentials *c,
- grpc_credentials_mdelem_array *md_array, grpc_error *error) {
+ grpc_call_credentials *c, grpc_credentials_mdelem_array *md_array,
+ grpc_error *error) {
GRPC_ERROR_UNREF(error);
}
@@ -524,11 +514,11 @@ grpc_call_credentials *grpc_access_token_credentials_create(
gpr_ref_init(&c->base.refcount, 1);
char *token_md_value;
gpr_asprintf(&token_md_value, "Bearer %s", access_token);
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ ExecCtx _local_exec_ctx;
c->access_token_md = grpc_mdelem_from_slices(
- &exec_ctx, grpc_slice_from_static_string(GRPC_AUTHORIZATION_METADATA_KEY),
+ grpc_slice_from_static_string(GRPC_AUTHORIZATION_METADATA_KEY),
grpc_slice_from_copied_string(token_md_value));
- grpc_exec_ctx_finish(&exec_ctx);
+ grpc_exec_ctx_finish();
gpr_free(token_md_value);
return &c->base;
}
diff --git a/src/core/lib/security/credentials/oauth2/oauth2_credentials.h b/src/core/lib/security/credentials/oauth2/oauth2_credentials.h
index c12db896f3..b7b5f58746 100644
--- a/src/core/lib/security/credentials/oauth2/oauth2_credentials.h
+++ b/src/core/lib/security/credentials/oauth2/oauth2_credentials.h
@@ -56,8 +56,7 @@ void grpc_auth_refresh_token_destruct(grpc_auth_refresh_token *refresh_token);
// This object is a base for credentials that need to acquire an oauth2 token
// from an http service.
-typedef void (*grpc_fetch_oauth2_func)(grpc_exec_ctx *exec_ctx,
- grpc_credentials_metadata_request *req,
+typedef void (*grpc_fetch_oauth2_func)(grpc_credentials_metadata_request *req,
grpc_httpcli_context *http_context,
grpc_polling_entity *pollent,
grpc_iomgr_cb_func cb,
@@ -103,8 +102,8 @@ grpc_refresh_token_credentials_create_from_auth_refresh_token(
// Exposed for testing only.
grpc_credentials_status
grpc_oauth2_token_fetcher_credentials_parse_server_response(
- grpc_exec_ctx *exec_ctx, const struct grpc_http_response *response,
- grpc_mdelem *token_md, grpc_millis *token_lifetime);
+ const struct grpc_http_response *response, grpc_mdelem *token_md,
+ grpc_millis *token_lifetime);
#ifdef __cplusplus
}