fetch_oauth2.cc
Go to the documentation of this file.
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>
29 
36 #include "test/core/util/cmdline.h"
37 
38 static grpc_call_credentials* create_sts_creds(const char* json_file_path) {
40  if (strlen(json_file_path) == 0) {
42  if (!status.ok()) {
43  gpr_log(GPR_ERROR, "%s", status.error_message().c_str());
44  return nullptr;
45  }
46  } else {
47  grpc_slice sts_options_slice;
49  "load_file", grpc_load_file(json_file_path, 1, &sts_options_slice)));
51  reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(sts_options_slice)),
52  &options);
53  gpr_slice_unref(sts_options_slice);
54  if (!status.ok()) {
55  gpr_log(GPR_ERROR, "%s", status.error_message().c_str());
56  return nullptr;
57  }
58  }
62  return result;
63 }
64 
66  const char* json_refresh_token_file_path) {
67  grpc_slice refresh_token;
69  "load_file",
70  grpc_load_file(json_refresh_token_file_path, 1, &refresh_token)));
72  reinterpret_cast<const char*> GRPC_SLICE_START_PTR(refresh_token),
73  nullptr);
74  gpr_slice_unref(refresh_token);
75  return result;
76 }
77 
78 int main(int argc, char** argv) {
79  grpc_call_credentials* creds = nullptr;
80  const char* json_sts_options_file_path = nullptr;
81  const char* json_refresh_token_file_path = nullptr;
82  char* token = nullptr;
83  int use_gce = 0;
84  gpr_cmdline* cl = gpr_cmdline_create("fetch_oauth2");
85  gpr_cmdline_add_string(cl, "json_refresh_token",
86  "File path of the json refresh token.",
87  &json_refresh_token_file_path);
89  cl, "json_sts_options",
90  "File path of the json sts options. If the path is empty, the program "
91  "will attempt to use the $STS_CREDENTIALS environment variable to access "
92  "a file containing the options.",
93  &json_sts_options_file_path);
95  cl, "gce",
96  "Get a token from the GCE metadata server (only works in GCE).",
97  &use_gce);
98  gpr_cmdline_parse(cl, argc, argv);
99 
100  grpc_init();
101 
102  if (json_sts_options_file_path != nullptr &&
103  json_refresh_token_file_path != nullptr) {
104  gpr_log(
105  GPR_ERROR,
106  "--json_sts_options and --json_refresh_token are mutually exclusive.");
107  exit(1);
108  }
109 
110  if (use_gce) {
111  if (json_sts_options_file_path != nullptr ||
112  json_refresh_token_file_path != nullptr) {
114  "Ignoring json refresh token or sts options to get a token from "
115  "the GCE metadata server.");
116  }
118  if (creds == nullptr) {
119  gpr_log(GPR_ERROR, "Could not create gce credentials.");
120  exit(1);
121  }
122  } else if (json_refresh_token_file_path != nullptr) {
123  creds = create_refresh_token_creds(json_refresh_token_file_path);
124  if (creds == nullptr) {
126  "Could not create refresh token creds. %s does probably not "
127  "contain a valid json refresh token.",
128  json_refresh_token_file_path);
129  exit(1);
130  }
131  } else if (json_sts_options_file_path != nullptr) {
132  creds = create_sts_creds(json_sts_options_file_path);
133  if (creds == nullptr) {
135  "Could not create sts creds. %s does probably not contain a "
136  "valid json for sts options.",
137  json_sts_options_file_path);
138  exit(1);
139  }
140  } else {
141  gpr_log(
142  GPR_ERROR,
143  "Missing --gce, --json_sts_options, or --json_refresh_token option.");
144  exit(1);
145  }
146  GPR_ASSERT(creds != nullptr);
147 
149  if (token != nullptr) {
150  printf("Got token: %s.\n", token);
151  gpr_free(token);
152  }
155  grpc_shutdown();
156  return 0;
157 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
grpc::experimental::StsCredentialsOptionsFromJson
grpc::Status StsCredentialsOptionsFromJson(const std::string &json_string, StsCredentialsOptions *options)
Definition: secure_credentials.cc:162
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
log.h
grpc_load_file
grpc_error_handle grpc_load_file(const char *filename, int add_null_terminator, grpc_slice *output)
Definition: load_file.cc:33
load_file.h
slice.h
grpc_sts_credentials_options
Definition: grpc_security.h:355
string.h
options
double_dict options[]
Definition: capstone_test.c:55
grpc::experimental::StsCredentialsOptionsFromEnv
grpc::Status StsCredentialsOptionsFromEnv(StsCredentialsOptions *options)
Definition: secure_credentials.cc:219
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
gpr_slice_unref
#define gpr_slice_unref
Definition: gpr_slice.h:39
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
gpr_cmdline_destroy
void gpr_cmdline_destroy(gpr_cmdline *cl)
Definition: cmdline.cc:79
main
int main(int argc, char **argv)
Definition: fetch_oauth2.cc:78
secure_credentials.h
cstest_report.opts
opts
Definition: cstest_report.py:81
grpc_call_credentials
Definition: src/core/lib/security/credentials/credentials.h:189
status
absl::Status status
Definition: rls.cc:251
grpc_google_compute_engine_credentials_create
GRPCAPI grpc_call_credentials * grpc_google_compute_engine_credentials_create(void *reserved)
Definition: oauth2_credentials.cc:421
grpc_call_credentials_release
GRPCAPI void grpc_call_credentials_release(grpc_call_credentials *creds)
Definition: credentials.cc:42
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
grpc_security.h
gpr_cmdline_add_flag
void gpr_cmdline_add_flag(gpr_cmdline *cl, const char *name, const char *help, int *value)
Definition: cmdline.cc:110
credentials.h
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_sts_credentials_create
GRPCAPI grpc_call_credentials * grpc_sts_credentials_create(const grpc_sts_credentials_options *options, void *reserved)
Definition: oauth2_credentials.cc:701
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
gpr_cmdline_add_string
void gpr_cmdline_add_string(gpr_cmdline *cl, const char *name, const char *help, const char **value)
Definition: cmdline.cc:115
grpc.h
grpc::experimental::StsCredentialsCppToCoreOptions
grpc_sts_credentials_options StsCredentialsCppToCoreOptions(const StsCredentialsOptions &options)
Definition: secure_credentials.cc:255
oauth2_utils.h
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
create_sts_creds
static grpc_call_credentials * create_sts_creds(const char *json_file_path)
Definition: fetch_oauth2.cc:38
error.h
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
credentials.h
grpc_google_refresh_token_credentials_create
GRPCAPI grpc_call_credentials * grpc_google_refresh_token_credentials_create(const char *json_refresh_token, void *reserved)
Definition: oauth2_credentials.cc:508
alloc.h
cmdline.h
gpr_cmdline_create
gpr_cmdline * gpr_cmdline_create(const char *description)
Definition: cmdline.cc:66
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
gpr_cmdline
Definition: cmdline.cc:48
grpc::experimental::StsCredentialsOptions
Definition: include/grpcpp/security/credentials.h:299
json_util.h
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
sync.h
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
gpr_cmdline_parse
int gpr_cmdline_parse(gpr_cmdline *cl, int argc, char **argv)
Definition: cmdline.cc:309
grpc_test_fetch_oauth2_token_with_credentials
char * grpc_test_fetch_oauth2_token_with_credentials(grpc_call_credentials *creds)
Definition: oauth2_utils.cc:42
create_refresh_token_creds
static grpc_call_credentials * create_refresh_token_creds(const char *json_refresh_token_file_path)
Definition: fetch_oauth2.cc:65


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:19