Wire up google_default_credentials/http_cli

pull/1888/head
Craig Tiller 10 years ago
parent 6e2b12972f
commit 00d5b5cf90
  1. 4
      src/core/httpcli/httpcli.h
  2. 30
      src/core/security/google_default_credentials.c

@ -38,6 +38,8 @@
#include <grpc/support/time.h>
#include "src/core/iomgr/pollset_set.h"
/* User agent this library reports */
#define GRPC_HTTPCLI_USER_AGENT "grpc-httpcli/0.0"
/* Maximum length of a header string of the form 'Key: Value\r\n' */
@ -90,6 +92,7 @@ typedef void (*grpc_httpcli_response_cb)(void *user_data,
supplied pointer to pass to said call) */
void grpc_httpcli_get(const grpc_httpcli_request *request,
gpr_timespec deadline,
grpc_pollset_set *interested_parties,
grpc_httpcli_response_cb on_response, void *user_data);
/* Asynchronously perform a HTTP POST.
@ -98,6 +101,7 @@ void grpc_httpcli_get(const grpc_httpcli_request *request,
void grpc_httpcli_post(const grpc_httpcli_request *request,
const char *body_bytes, size_t body_size,
gpr_timespec deadline,
grpc_pollset_set *interested_parties,
grpc_httpcli_response_cb on_response, void *user_data);
/* override functions return 1 if they handled the request, 0 otherwise */

@ -60,8 +60,7 @@ static void init_default_credentials(void) {
}
typedef struct {
gpr_cv cv;
gpr_mu mu;
grpc_pollset pollset;
int is_done;
int success;
} compute_engine_detector;
@ -82,22 +81,22 @@ static void on_compute_engine_detection_http_response(
}
}
}
gpr_mu_lock(&detector->mu);
gpr_mu_lock(GRPC_POLLSET_MU(&detector->pollset));
detector->is_done = 1;
gpr_mu_unlock(&detector->mu);
gpr_cv_signal(&detector->cv);
grpc_pollset_kick(&detector->pollset);
gpr_mu_unlock(GRPC_POLLSET_MU(&detector->pollset));
}
static int is_stack_running_on_compute_engine(void) {
compute_engine_detector detector;
grpc_httpcli_request request;
grpc_pollset_set pollset_set;
/* The http call is local. If it takes more than one sec, it is for sure not
on compute engine. */
gpr_timespec max_detection_delay = {1, 0};
gpr_mu_init(&detector.mu);
gpr_cv_init(&detector.cv);
grpc_pollset_init(&detector.pollset);
detector.is_done = 0;
detector.success = 0;
@ -105,19 +104,24 @@ static int is_stack_running_on_compute_engine(void) {
request.host = GRPC_COMPUTE_ENGINE_DETECTION_HOST;
request.path = "/";
grpc_pollset_set_init(&pollset_set);
grpc_pollset_set_add_pollset(&pollset_set, &detector.pollset);
grpc_httpcli_get(&request, gpr_time_add(gpr_now(), max_detection_delay),
on_compute_engine_detection_http_response, &detector);
&pollset_set, on_compute_engine_detection_http_response,
&detector);
/* Block until we get the response. This is not ideal but this should only be
called once for the lifetime of the process by the default credentials. */
gpr_mu_lock(&detector.mu);
gpr_mu_lock(GRPC_POLLSET_MU(&detector.pollset));
while (!detector.is_done) {
gpr_cv_wait(&detector.cv, &detector.mu, gpr_inf_future);
grpc_pollset_work(&detector.pollset, gpr_inf_future);
}
gpr_mu_unlock(&detector.mu);
gpr_mu_unlock(GRPC_POLLSET_MU(&detector.pollset));
grpc_pollset_set_destroy(&pollset_set);
grpc_pollset_destroy(&detector.pollset);
gpr_mu_destroy(&detector.mu);
gpr_cv_destroy(&detector.cv);
return detector.success;
}

Loading…
Cancel
Save