Пример #1
0
/*
  call-seq:
    creds = Credentials.compute_engine()
    Creates the default credential instances. */
static VALUE grpc_rb_compute_engine_credentials_create(VALUE cls) {
  grpc_rb_credentials *wrapper = ALLOC(grpc_rb_credentials);
  wrapper->wrapped = grpc_compute_engine_credentials_create();
  if (wrapper->wrapped == NULL) {
    rb_raise(rb_eRuntimeError,
             "could not create composite engine credentials, not sure why");
    return Qnil;
  }

  wrapper->mark = Qnil;
  return Data_Wrap_Struct(cls, grpc_rb_credentials_mark,
                          grpc_rb_credentials_free, wrapper);
}
Пример #2
0
static void test_compute_engine_creds_failure(void) {
    grpc_credentials *compute_engine_creds =
        grpc_compute_engine_credentials_create();
    grpc_httpcli_set_override(compute_engine_httpcli_get_failure_override,
                              httpcli_post_should_not_be_called);
    GPR_ASSERT(grpc_credentials_has_request_metadata(compute_engine_creds));
    GPR_ASSERT(grpc_credentials_has_request_metadata_only(compute_engine_creds));
    grpc_credentials_get_request_metadata(
        compute_engine_creds, NULL, test_service_url,
        on_oauth2_creds_get_metadata_failure, (void *)test_user_data);
    grpc_credentials_unref(compute_engine_creds);
    grpc_httpcli_set_override(NULL, NULL);
}
Пример #3
0
grpc_credentials *grpc_google_default_credentials_create(void) {
  grpc_credentials *result = NULL;
  int serving_cached_credentials = 0;
  gpr_once_init(&g_once, init_default_credentials);

  gpr_mu_lock(&g_mu);

  if (default_credentials != NULL) {
    result = grpc_credentials_ref(default_credentials);
    serving_cached_credentials = 1;
    goto end;
  }

  /* First, try the environment variable. */
  result =
      create_jwt_creds_from_path(gpr_getenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR));
  if (result != NULL) goto end;

  /* Then the well-known file. */
  result = create_refresh_token_creds_from_path(
      grpc_get_well_known_google_credentials_file_path());
  if (result != NULL) goto end;

  /* At last try to see if we're on compute engine (do the detection only once
     since it requires a network test). */
  if (!compute_engine_detection_done) {
    int need_compute_engine_creds = is_stack_running_on_compute_engine();
    compute_engine_detection_done = 1;
    if (need_compute_engine_creds) {
      result = grpc_compute_engine_credentials_create();
    }
  }

end:
  if (!serving_cached_credentials && result != NULL) {
    /* Blend with default ssl credentials and add a global reference so that it
       can be cached and re-served. */
    result = grpc_composite_credentials_create(
        grpc_ssl_credentials_create(NULL, NULL), result);
    GPR_ASSERT(result != NULL);
    default_credentials = grpc_credentials_ref(result);
  }
  gpr_mu_unlock(&g_mu);
  return result;
}
Пример #4
0
static void test_compute_engine_creds_success(void) {
    grpc_credentials *compute_engine_creds =
        grpc_compute_engine_credentials_create();
    GPR_ASSERT(grpc_credentials_has_request_metadata(compute_engine_creds));
    GPR_ASSERT(grpc_credentials_has_request_metadata_only(compute_engine_creds));

    /* First request: http get should be called. */
    grpc_httpcli_set_override(compute_engine_httpcli_get_success_override,
                              httpcli_post_should_not_be_called);
    grpc_credentials_get_request_metadata(
        compute_engine_creds, NULL, test_service_url,
        on_oauth2_creds_get_metadata_success, (void *)test_user_data);

    /* Second request: the cached token should be served directly. */
    grpc_httpcli_set_override(httpcli_get_should_not_be_called,
                              httpcli_post_should_not_be_called);
    grpc_credentials_get_request_metadata(
        compute_engine_creds, NULL, test_service_url,
        on_oauth2_creds_get_metadata_success, (void *)test_user_data);

    grpc_credentials_unref(compute_engine_creds);
    grpc_httpcli_set_override(NULL, NULL);
}