diff options
author | Julien Boeuf <jboeuf@google.com> | 2015-02-20 17:40:41 -0800 |
---|---|---|
committer | Julien Boeuf <jboeuf@google.com> | 2015-02-21 21:57:42 -0800 |
commit | cd9b1c850db35ad37669dc0a650712b1cd29527f (patch) | |
tree | b0782908327649376dd8485706130d8921a483e1 /test/core | |
parent | dc6abba60a2c4f3f5131814e5ee6b251b0188605 (diff) |
Added support for default credentials.
- Tested with new tool (print_default_creds_token) on:
- workstation for env var and well known place.
- GCE for compute engine default creds.
- I'd prefer the grpc_default_credentials_create() API to remain
synchronous even though there may be an async call for gce detection
on which we block.
Diffstat (limited to 'test/core')
-rw-r--r-- | test/core/security/print_default_creds_token.c | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/test/core/security/print_default_creds_token.c b/test/core/security/print_default_creds_token.c new file mode 100644 index 0000000000..b11acace40 --- /dev/null +++ b/test/core/security/print_default_creds_token.c @@ -0,0 +1,107 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include <stdio.h> +#include <string.h> + +#include "src/core/security/credentials.h" +#include <grpc/grpc.h> +#include <grpc/grpc_security.h> +#include <grpc/support/alloc.h> +#include <grpc/support/cmdline.h> +#include <grpc/support/log.h> +#include <grpc/support/slice.h> +#include <grpc/support/sync.h> + +typedef struct { + gpr_cv cv; + gpr_mu mu; + int is_done; +} synchronizer; + +static void on_metadata_response(void *user_data, grpc_mdelem **md_elems, + size_t num_md, + grpc_credentials_status status) { + synchronizer *sync = user_data; + if (status == GRPC_CREDENTIALS_ERROR) { + fprintf(stderr, "Fetching token failed.\n"); + } else { + GPR_ASSERT(num_md == 1); + printf("\nGot token: %s\n\n", + (const char *)GPR_SLICE_START_PTR(md_elems[0]->value->slice)); + } + gpr_mu_lock(&sync->mu); + sync->is_done = 1; + gpr_mu_unlock(&sync->mu); + gpr_cv_signal(&sync->cv); +} + +int main(int argc, char **argv) { + int result = 0; + synchronizer sync; + grpc_credentials *creds = NULL; + char *service_url = "https://test.foo.google.com/Foo"; + gpr_cmdline *cl = gpr_cmdline_create("print_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_default_credentials_create(); + if (creds == NULL) { + fprintf(stderr, "\nCould not find default credentials.\n\n"); + result = 1; + goto end; + } + + gpr_mu_init(&sync.mu); + gpr_cv_init(&sync.cv); + sync.is_done = 0; + + grpc_credentials_get_request_metadata(creds, "", on_metadata_response, &sync); + + gpr_mu_lock(&sync.mu); + while (!sync.is_done) gpr_cv_wait(&sync.cv, &sync.mu, gpr_inf_future); + gpr_mu_unlock(&sync.mu); + + gpr_mu_destroy(&sync.mu); + gpr_cv_destroy(&sync.cv); + grpc_credentials_release(creds); + +end: + gpr_cmdline_destroy(cl); + grpc_shutdown(); + return result; +} |