blob: b6421f19ab23245533c880c9285aa3a60badb3ed [file] [log] [blame]
Julien Boeufcd9b1c82015-02-20 17:40:41 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Julien Boeufcd9b1c82015-02-20 17:40:41 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <stdio.h>
35#include <string.h>
36
Julien Boeufcd9b1c82015-02-20 17:40:41 -080037#include <grpc/grpc.h>
38#include <grpc/grpc_security.h>
Craig Tiller28b72422016-10-26 21:15:29 -070039#include <grpc/slice.h>
Julien Boeufcd9b1c82015-02-20 17:40:41 -080040#include <grpc/support/alloc.h>
41#include <grpc/support/cmdline.h>
42#include <grpc/support/log.h>
Julien Boeufcd9b1c82015-02-20 17:40:41 -080043#include <grpc/support/sync.h>
44
Julien Boeuf8ca294e2016-05-02 14:56:30 -070045#include "src/core/lib/security/credentials/composite/composite_credentials.h"
Craig Tillerf707d622016-05-06 14:26:12 -070046#include "src/core/lib/security/credentials/credentials.h"
Craig Tillera9b09dd2016-10-28 09:05:27 -070047#include "src/core/lib/slice/slice_string_helpers.h"
Craig Tillerc650fb32016-10-28 09:05:50 -070048#include "src/core/lib/support/string.h"
Craig Tiller69b093b2016-02-25 19:04:07 -080049
Craig Tillera82950e2015-09-22 12:33:20 -070050typedef struct {
Craig Tiller69b093b2016-02-25 19:04:07 -080051 gpr_mu *mu;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070052 grpc_polling_entity pops;
Julien Boeufcd9b1c82015-02-20 17:40:41 -080053 int is_done;
54} synchronizer;
55
Craig Tillera82950e2015-09-22 12:33:20 -070056static void on_metadata_response(grpc_exec_ctx *exec_ctx, void *user_data,
57 grpc_credentials_md *md_elems, size_t num_md,
Julien Boeuf2e3c9ad2016-01-19 17:14:38 -080058 grpc_credentials_status status,
59 const char *error_details) {
Julien Boeufcd9b1c82015-02-20 17:40:41 -080060 synchronizer *sync = user_data;
Craig Tillera82950e2015-09-22 12:33:20 -070061 if (status == GRPC_CREDENTIALS_ERROR) {
62 fprintf(stderr, "Fetching token failed.\n");
63 } else {
64 char *token;
65 GPR_ASSERT(num_md == 1);
Craig Tiller7c70b6c2017-01-23 07:48:42 -080066 token = grpc_slice_to_c_string(md_elems[0].value);
Craig Tillera82950e2015-09-22 12:33:20 -070067 printf("\nGot token: %s\n\n", token);
68 gpr_free(token);
69 }
Craig Tiller69b093b2016-02-25 19:04:07 -080070 gpr_mu_lock(sync->mu);
Julien Boeufcd9b1c82015-02-20 17:40:41 -080071 sync->is_done = 1;
Craig Tillerc97065d2016-06-07 13:00:14 -070072 GRPC_LOG_IF_ERROR(
73 "pollset_kick",
74 grpc_pollset_kick(grpc_polling_entity_pollset(&sync->pops), NULL));
Craig Tiller69b093b2016-02-25 19:04:07 -080075 gpr_mu_unlock(sync->mu);
Julien Boeufcd9b1c82015-02-20 17:40:41 -080076}
77
Craig Tillera82950e2015-09-22 12:33:20 -070078int main(int argc, char **argv) {
Julien Boeufcd9b1c82015-02-20 17:40:41 -080079 int result = 0;
Craig Tillerf5768a62015-09-22 10:54:34 -070080 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Julien Boeufcd9b1c82015-02-20 17:40:41 -080081 synchronizer sync;
Julien Boeuf4e02e842015-10-09 22:49:42 -070082 grpc_channel_credentials *creds = NULL;
Julien Boeufcd9b1c82015-02-20 17:40:41 -080083 char *service_url = "https://test.foo.google.com/Foo";
Julien Boeuf3c957e62015-11-18 21:33:58 -080084 grpc_auth_metadata_context context;
Craig Tillera82950e2015-09-22 12:33:20 -070085 gpr_cmdline *cl = gpr_cmdline_create("print_google_default_creds_token");
86 gpr_cmdline_add_string(cl, "service_url",
87 "Service URL for the token request.", &service_url);
88 gpr_cmdline_parse(cl, argc, argv);
Julien Boeuf3c957e62015-11-18 21:33:58 -080089 memset(&context, 0, sizeof(context));
90 context.service_url = service_url;
Julien Boeufcd9b1c82015-02-20 17:40:41 -080091
Craig Tillera82950e2015-09-22 12:33:20 -070092 grpc_init();
Julien Boeufcd9b1c82015-02-20 17:40:41 -080093
Craig Tillera82950e2015-09-22 12:33:20 -070094 creds = grpc_google_default_credentials_create();
95 if (creds == NULL) {
96 fprintf(stderr, "\nCould not find default credentials.\n\n");
97 result = 1;
98 goto end;
99 }
Julien Boeufcd9b1c82015-02-20 17:40:41 -0800100
Yuchen Zeng47de64c2017-02-22 19:04:38 -0800101 grpc_pollset *pollset = gpr_zalloc(grpc_pollset_size());
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700102 grpc_pollset_init(pollset, &sync.mu);
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700103 sync.pops = grpc_polling_entity_create_from_pollset(pollset);
Julien Boeufcd9b1c82015-02-20 17:40:41 -0800104 sync.is_done = 0;
105
Julien Boeufb73cbc22015-10-20 21:52:54 -0700106 grpc_call_credentials_get_request_metadata(
107 &exec_ctx, ((grpc_composite_channel_credentials *)creds)->call_creds,
David Garcia Quintas60449092016-05-04 20:20:04 -0700108 &sync.pops, context, on_metadata_response, &sync);
Julien Boeufcd9b1c82015-02-20 17:40:41 -0800109
Craig Tiller69b093b2016-02-25 19:04:07 -0800110 gpr_mu_lock(sync.mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700111 while (!sync.is_done) {
Craig Tiller3633ce42016-02-18 08:08:53 -0800112 grpc_pollset_worker *worker = NULL;
Craig Tiller1aee5362016-05-07 11:26:50 -0700113 if (!GRPC_LOG_IF_ERROR(
114 "pollset_work",
Craig Tillerc97065d2016-06-07 13:00:14 -0700115 grpc_pollset_work(&exec_ctx,
116 grpc_polling_entity_pollset(&sync.pops), &worker,
Craig Tiller1aee5362016-05-07 11:26:50 -0700117 gpr_now(GPR_CLOCK_MONOTONIC),
118 gpr_inf_future(GPR_CLOCK_MONOTONIC))))
119 sync.is_done = 1;
Craig Tiller69b093b2016-02-25 19:04:07 -0800120 gpr_mu_unlock(sync.mu);
121 grpc_exec_ctx_flush(&exec_ctx);
122 gpr_mu_lock(sync.mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700123 }
Craig Tiller69b093b2016-02-25 19:04:07 -0800124 gpr_mu_unlock(sync.mu);
125
126 grpc_exec_ctx_finish(&exec_ctx);
Julien Boeufcd9b1c82015-02-20 17:40:41 -0800127
Julien Boeuf4e02e842015-10-09 22:49:42 -0700128 grpc_channel_credentials_release(creds);
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700129 gpr_free(grpc_polling_entity_pollset(&sync.pops));
Julien Boeufcd9b1c82015-02-20 17:40:41 -0800130
131end:
Craig Tillera82950e2015-09-22 12:33:20 -0700132 gpr_cmdline_destroy(cl);
133 grpc_shutdown();
Julien Boeufcd9b1c82015-02-20 17:40:41 -0800134 return result;
135}