1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <stdio.h>
20 #include <string.h>
21 
22 #include <grpc/grpc.h>
23 #include <grpc/grpc_security.h>
24 #include <grpc/slice.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/sync.h>
28 
29 #include "src/core/lib/gpr/string.h"
30 #include "src/core/lib/security/credentials/composite/composite_credentials.h"
31 #include "src/core/lib/security/credentials/credentials.h"
32 #include "src/core/lib/slice/slice_string_helpers.h"
33 #include "test/core/util/cmdline.h"
34 
35 typedef struct {
36   gpr_mu* mu;
37   grpc_polling_entity pops;
38   bool is_done;
39 
40   grpc_credentials_mdelem_array md_array;
41   grpc_closure on_request_metadata;
42 } synchronizer;
43 
on_metadata_response(void * arg,grpc_error_handle error)44 static void on_metadata_response(void* arg, grpc_error_handle error) {
45   synchronizer* sync = static_cast<synchronizer*>(arg);
46   if (error != GRPC_ERROR_NONE) {
47     fprintf(stderr, "Fetching token failed: %s\n",
48             grpc_error_std_string(error).c_str());
49     fflush(stderr);
50   } else {
51     char* token;
52     GPR_ASSERT(sync->md_array.size == 1);
53     token = grpc_slice_to_c_string(GRPC_MDVALUE(sync->md_array.md[0]));
54     printf("\nGot token: %s\n\n", token);
55     gpr_free(token);
56   }
57   gpr_mu_lock(sync->mu);
58   sync->is_done = true;
59   GRPC_LOG_IF_ERROR(
60       "pollset_kick",
61       grpc_pollset_kick(grpc_polling_entity_pollset(&sync->pops), nullptr));
62   gpr_mu_unlock(sync->mu);
63 }
64 
main(int argc,char ** argv)65 int main(int argc, char** argv) {
66   int result = 0;
67   grpc_core::ExecCtx exec_ctx;
68   synchronizer sync;
69   grpc_channel_credentials* creds = nullptr;
70   const char* service_url = "https://test.foo.google.com/Foo";
71   grpc_auth_metadata_context context;
72   gpr_cmdline* cl = gpr_cmdline_create("print_google_default_creds_token");
73   grpc_pollset* pollset = nullptr;
74   grpc_error_handle error = GRPC_ERROR_NONE;
75   gpr_cmdline_add_string(cl, "service_url",
76                          "Service URL for the token request.", &service_url);
77   gpr_cmdline_parse(cl, argc, argv);
78   memset(&context, 0, sizeof(context));
79   context.service_url = service_url;
80 
81   grpc_init();
82 
83   creds = grpc_google_default_credentials_create();
84   if (creds == nullptr) {
85     fprintf(stderr, "\nCould not find default credentials.\n\n");
86     fflush(stderr);
87     result = 1;
88     goto end;
89   }
90 
91   memset(&sync, 0, sizeof(sync));
92   pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
93   grpc_pollset_init(pollset, &sync.mu);
94   sync.pops = grpc_polling_entity_create_from_pollset(pollset);
95   sync.is_done = false;
96   GRPC_CLOSURE_INIT(&sync.on_request_metadata, on_metadata_response, &sync,
97                     grpc_schedule_on_exec_ctx);
98 
99   error = GRPC_ERROR_NONE;
100   if (reinterpret_cast<grpc_composite_channel_credentials*>(creds)
101           ->mutable_call_creds()
102           ->get_request_metadata(&sync.pops, context, &sync.md_array,
103                                  &sync.on_request_metadata, &error)) {
104     // Synchronous response.  Invoke callback directly.
105     on_metadata_response(&sync, error);
106     GRPC_ERROR_UNREF(error);
107   }
108 
109   gpr_mu_lock(sync.mu);
110   while (!sync.is_done) {
111     grpc_pollset_worker* worker = nullptr;
112     if (!GRPC_LOG_IF_ERROR(
113             "pollset_work",
114             grpc_pollset_work(grpc_polling_entity_pollset(&sync.pops), &worker,
115                               GRPC_MILLIS_INF_FUTURE)))
116       sync.is_done = true;
117     gpr_mu_unlock(sync.mu);
118     grpc_core::ExecCtx::Get()->Flush();
119     gpr_mu_lock(sync.mu);
120   }
121   gpr_mu_unlock(sync.mu);
122 
123   grpc_channel_credentials_release(creds);
124   gpr_free(grpc_polling_entity_pollset(&sync.pops));
125 
126 end:
127   gpr_cmdline_destroy(cl);
128   grpc_shutdown();
129   return result;
130 }
131