int main(int argc, char **argv) {
  int result = 0;
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  synchronizer sync;
  grpc_channel_credentials *creds = NULL;
  char *service_url = "https://test.foo.google.com/Foo";
  grpc_auth_metadata_context context;
  gpr_cmdline *cl = gpr_cmdline_create("print_google_default_creds_token");
  gpr_cmdline_add_string(cl, "service_url",
                         "Service URL for the token request.", &service_url);
  gpr_cmdline_parse(cl, argc, argv);
  memset(&context, 0, sizeof(context));
  context.service_url = service_url;

  grpc_init();

  creds = grpc_google_default_credentials_create();
  if (creds == NULL) {
    fprintf(stderr, "\nCould not find default credentials.\n\n");
    result = 1;
    goto end;
  }

  grpc_pollset *pollset = gpr_malloc(grpc_pollset_size());
  grpc_pollset_init(pollset, &sync.mu);
  sync.pops = grpc_polling_entity_create_from_pollset(pollset);
  sync.is_done = 0;

  grpc_call_credentials_get_request_metadata(
      &exec_ctx, ((grpc_composite_channel_credentials *)creds)->call_creds,
      &sync.pops, context, on_metadata_response, &sync);

  gpr_mu_lock(sync.mu);
  while (!sync.is_done) {
    grpc_pollset_worker *worker = NULL;
    if (!GRPC_LOG_IF_ERROR(
            "pollset_work",
            grpc_pollset_work(&exec_ctx,
                              grpc_polling_entity_pollset(&sync.pops), &worker,
                              gpr_now(GPR_CLOCK_MONOTONIC),
                              gpr_inf_future(GPR_CLOCK_MONOTONIC))))
      sync.is_done = 1;
    gpr_mu_unlock(sync.mu);
    grpc_exec_ctx_flush(&exec_ctx);
    gpr_mu_lock(sync.mu);
  }
  gpr_mu_unlock(sync.mu);

  grpc_exec_ctx_finish(&exec_ctx);

  grpc_channel_credentials_release(creds);
  gpr_free(grpc_polling_entity_pollset(&sync.pops));

end:
  gpr_cmdline_destroy(cl);
  grpc_shutdown();
  return result;
}
Example #2
0
/*
  call-seq:
    creds = Credentials.default()
    Creates the default credential instances. */
static VALUE grpc_rb_default_credentials_create(VALUE cls) {
  grpc_rb_credentials *wrapper = ALLOC(grpc_rb_credentials);
  wrapper->wrapped = grpc_google_default_credentials_create();
  if (wrapper->wrapped == NULL) {
    rb_raise(rb_eRuntimeError,
             "could not create default credentials, not sure why");
    return Qnil;
  }

  wrapper->mark = Qnil;
  return Data_Wrap_Struct(cls, grpc_rb_credentials_mark,
                          grpc_rb_credentials_free, wrapper);
}
Example #3
0
static void test_google_default_creds_access_token(void) {
    grpc_refresh_token_credentials *refresh;
    grpc_credentials *creds;
    grpc_flush_cached_google_default_credentials();
    set_google_default_creds_env_var_with_file_contents(
        "refresh_token_google_default_creds", test_refresh_token_str);
    creds = grpc_google_default_credentials_create();
    GPR_ASSERT(creds != NULL);
    refresh = (grpc_refresh_token_credentials *)composite_inner_creds(
                  creds, GRPC_CREDENTIALS_TYPE_OAUTH2);
    GPR_ASSERT(strcmp(refresh->refresh_token.client_id,
                      "32555999999.apps.googleusercontent.com") == 0);
    grpc_credentials_unref(creds);
    gpr_setenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */
}
int main(int argc, char **argv) {
  int result = 0;
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  synchronizer sync;
  grpc_channel_credentials *creds = NULL;
  char *service_url = "https://test.foo.google.com/Foo";
  gpr_cmdline *cl = gpr_cmdline_create("print_google_default_creds_token");
  gpr_cmdline_add_string(cl, "service_url",
                         "Service URL for the token request.", &service_url);
  gpr_cmdline_parse(cl, argc, argv);

  grpc_init();

  creds = grpc_google_default_credentials_create();
  if (creds == NULL) {
    fprintf(stderr, "\nCould not find default credentials.\n\n");
    result = 1;
    goto end;
  }

  grpc_pollset_init(&sync.pollset);
  sync.is_done = 0;

  grpc_call_credentials_get_request_metadata(
      &exec_ctx, ((grpc_composite_channel_credentials *)creds)->call_creds,
      &sync.pollset, service_url, on_metadata_response, &sync);

  gpr_mu_lock(GRPC_POLLSET_MU(&sync.pollset));
  while (!sync.is_done) {
    grpc_pollset_worker worker;
    grpc_pollset_work(&exec_ctx, &sync.pollset, &worker,
                      gpr_now(GPR_CLOCK_MONOTONIC),
                      gpr_inf_future(GPR_CLOCK_MONOTONIC));
    gpr_mu_unlock(GRPC_POLLSET_MU(&sync.pollset));
    grpc_exec_ctx_finish(&exec_ctx);
    gpr_mu_lock(GRPC_POLLSET_MU(&sync.pollset));
  }
  gpr_mu_unlock(GRPC_POLLSET_MU(&sync.pollset));

  grpc_channel_credentials_release(creds);

end:
  gpr_cmdline_destroy(cl);
  grpc_shutdown();
  return result;
}
Example #5
0
static void test_google_default_creds_auth_key(void) {
    grpc_service_account_jwt_access_credentials *jwt;
    grpc_credentials *creds;
    char *json_key = test_json_key_str();
    grpc_flush_cached_google_default_credentials();
    set_google_default_creds_env_var_with_file_contents(
        "json_key_google_default_creds", json_key);
    gpr_free(json_key);
    creds = grpc_google_default_credentials_create();
    GPR_ASSERT(creds != NULL);
    jwt = (grpc_service_account_jwt_access_credentials *)composite_inner_creds(
              creds, GRPC_CREDENTIALS_TYPE_JWT);
    GPR_ASSERT(
        strcmp(jwt->key.client_id,
               "777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent.com") ==
        0);
    grpc_credentials_unref(creds);
    gpr_setenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, ""); /* Reset. */
}