aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/security/print_google_default_creds_token.cc
blob: 398c58c6e174f1d9fa13a9b0aa109d5703ebb844 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <string.h>

#include <grpc/grpc.h>
#include <grpc/grpc_security.h>
#include <grpc/slice.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/sync.h>

#include "src/core/lib/gpr/string.h"
#include "src/core/lib/security/credentials/composite/composite_credentials.h"
#include "src/core/lib/security/credentials/credentials.h"
#include "src/core/lib/slice/slice_string_helpers.h"
#include "test/core/util/cmdline.h"

typedef struct {
  gpr_mu* mu;
  grpc_polling_entity pops;
  bool is_done;

  grpc_credentials_mdelem_array md_array;
  grpc_closure on_request_metadata;
} synchronizer;

static void on_metadata_response(void* arg, grpc_error* error) {
  synchronizer* sync = static_cast<synchronizer*>(arg);
  if (error != GRPC_ERROR_NONE) {
    fprintf(stderr, "Fetching token failed: %s\n", grpc_error_string(error));
    fflush(stderr);
  } else {
    char* token;
    GPR_ASSERT(sync->md_array.size == 1);
    token = grpc_slice_to_c_string(GRPC_MDVALUE(sync->md_array.md[0]));
    printf("\nGot token: %s\n\n", token);
    gpr_free(token);
  }
  gpr_mu_lock(sync->mu);
  sync->is_done = true;
  GRPC_LOG_IF_ERROR(
      "pollset_kick",
      grpc_pollset_kick(grpc_polling_entity_pollset(&sync->pops), nullptr));
  gpr_mu_unlock(sync->mu);
}

int main(int argc, char** argv) {
  int result = 0;
  grpc_core::ExecCtx exec_ctx;
  synchronizer sync;
  grpc_channel_credentials* creds = nullptr;
  const char* service_url = "https://test.foo.google.com/Foo";
  grpc_auth_metadata_context context;
  gpr_cmdline* cl = gpr_cmdline_create("print_google_default_creds_token");
  grpc_pollset* pollset = nullptr;
  grpc_error* error = nullptr;
  gpr_cmdline_add_string(cl, "service_url",
                         "Service URL for the token request.", &service_url);
  gpr_cmdline_parse(cl, argc, argv);
  memset(&context, 0, sizeof(context));
  context.service_url = service_url;

  grpc_init();

  creds = grpc_google_default_credentials_create();
  if (creds == nullptr) {
    fprintf(stderr, "\nCould not find default credentials.\n\n");
    fflush(stderr);
    result = 1;
    goto end;
  }

  memset(&sync, 0, sizeof(sync));
  pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
  grpc_pollset_init(pollset, &sync.mu);
  sync.pops = grpc_polling_entity_create_from_pollset(pollset);
  sync.is_done = false;
  GRPC_CLOSURE_INIT(&sync.on_request_metadata, on_metadata_response, &sync,
                    grpc_schedule_on_exec_ctx);

  error = GRPC_ERROR_NONE;
  if (reinterpret_cast<grpc_composite_channel_credentials*>(creds)
          ->mutable_call_creds()
          ->get_request_metadata(&sync.pops, context, &sync.md_array,
                                 &sync.on_request_metadata, &error)) {
    // Synchronous response.  Invoke callback directly.
    on_metadata_response(&sync, error);
    GRPC_ERROR_UNREF(error);
  }

  gpr_mu_lock(sync.mu);
  while (!sync.is_done) {
    grpc_pollset_worker* worker = nullptr;
    if (!GRPC_LOG_IF_ERROR(
            "pollset_work",
            grpc_pollset_work(grpc_polling_entity_pollset(&sync.pops), &worker,
                              GRPC_MILLIS_INF_FUTURE)))
      sync.is_done = true;
    gpr_mu_unlock(sync.mu);
    grpc_core::ExecCtx::Get()->Flush();
    gpr_mu_lock(sync.mu);
  }
  gpr_mu_unlock(sync.mu);

  grpc_channel_credentials_release(creds);
  gpr_free(grpc_polling_entity_pollset(&sync.pops));

end:
  gpr_cmdline_destroy(cl);
  grpc_shutdown();
  return result;
}