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 "grpcpp/security/credentials_impl.h"
30 #include "src/core/lib/iomgr/error.h"
31 #include "src/core/lib/iomgr/load_file.h"
32 #include "src/core/lib/security/credentials/credentials.h"
33 #include "src/core/lib/security/util/json_util.h"
34 #include "src/cpp/client/secure_credentials.h"
35 #include "test/core/security/oauth2_utils.h"
36 #include "test/core/util/cmdline.h"
37 
create_sts_creds(const char * json_file_path)38 static grpc_call_credentials* create_sts_creds(const char* json_file_path) {
39   grpc_impl::experimental::StsCredentialsOptions options;
40   if (strlen(json_file_path) == 0) {
41     auto status =
42         grpc_impl::experimental::StsCredentialsOptionsFromEnv(&options);
43     if (!status.ok()) {
44       gpr_log(GPR_ERROR, "%s", status.error_message().c_str());
45       return nullptr;
46     }
47   } else {
48     grpc_slice sts_options_slice;
49     GPR_ASSERT(GRPC_LOG_IF_ERROR(
50         "load_file", grpc_load_file(json_file_path, 1, &sts_options_slice)));
51     auto status = grpc_impl::experimental::StsCredentialsOptionsFromJson(
52         reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(sts_options_slice)),
53         &options);
54     gpr_slice_unref(sts_options_slice);
55     if (!status.ok()) {
56       gpr_log(GPR_ERROR, "%s", status.error_message().c_str());
57       return nullptr;
58     }
59   }
60   grpc_sts_credentials_options opts =
61       grpc_impl::experimental::StsCredentialsCppToCoreOptions(options);
62   grpc_call_credentials* result = grpc_sts_credentials_create(&opts, nullptr);
63   return result;
64 }
65 
create_refresh_token_creds(const char * json_refresh_token_file_path)66 static grpc_call_credentials* create_refresh_token_creds(
67     const char* json_refresh_token_file_path) {
68   grpc_slice refresh_token;
69   GPR_ASSERT(GRPC_LOG_IF_ERROR(
70       "load_file",
71       grpc_load_file(json_refresh_token_file_path, 1, &refresh_token)));
72   grpc_call_credentials* result = grpc_google_refresh_token_credentials_create(
73       reinterpret_cast<const char*> GRPC_SLICE_START_PTR(refresh_token),
74       nullptr);
75   gpr_slice_unref(refresh_token);
76   return result;
77 }
78 
main(int argc,char ** argv)79 int main(int argc, char** argv) {
80   grpc_call_credentials* creds = nullptr;
81   const char* json_sts_options_file_path = nullptr;
82   const char* json_refresh_token_file_path = nullptr;
83   char* token = nullptr;
84   int use_gce = 0;
85   gpr_cmdline* cl = gpr_cmdline_create("fetch_oauth2");
86   gpr_cmdline_add_string(cl, "json_refresh_token",
87                          "File path of the json refresh token.",
88                          &json_refresh_token_file_path);
89   gpr_cmdline_add_string(
90       cl, "json_sts_options",
91       "File path of the json sts options. If the path is empty, the program "
92       "will attempt to use the $STS_CREDENTIALS environment variable to access "
93       "a file containing the options.",
94       &json_sts_options_file_path);
95   gpr_cmdline_add_flag(
96       cl, "gce",
97       "Get a token from the GCE metadata server (only works in GCE).",
98       &use_gce);
99   gpr_cmdline_parse(cl, argc, argv);
100 
101   grpc_init();
102 
103   if (json_sts_options_file_path != nullptr &&
104       json_refresh_token_file_path != nullptr) {
105     gpr_log(
106         GPR_ERROR,
107         "--json_sts_options and --json_refresh_token are mutually exclusive.");
108     exit(1);
109   }
110 
111   if (use_gce) {
112     if (json_sts_options_file_path != nullptr ||
113         json_refresh_token_file_path != nullptr) {
114       gpr_log(GPR_INFO,
115               "Ignoring json refresh token or sts options to get a token from "
116               "the GCE metadata server.");
117     }
118     creds = grpc_google_compute_engine_credentials_create(nullptr);
119     if (creds == nullptr) {
120       gpr_log(GPR_ERROR, "Could not create gce credentials.");
121       exit(1);
122     }
123   } else if (json_refresh_token_file_path != nullptr) {
124     creds = create_refresh_token_creds(json_refresh_token_file_path);
125     if (creds == nullptr) {
126       gpr_log(GPR_ERROR,
127               "Could not create refresh token creds. %s does probably not "
128               "contain a valid json refresh token.",
129               json_refresh_token_file_path);
130       exit(1);
131     }
132   } else if (json_sts_options_file_path != nullptr) {
133     creds = create_sts_creds(json_sts_options_file_path);
134     if (creds == nullptr) {
135       gpr_log(GPR_ERROR,
136               "Could not create sts creds. %s does probably not contain a "
137               "valid json for sts options.",
138               json_sts_options_file_path);
139       exit(1);
140     }
141   } else {
142     gpr_log(
143         GPR_ERROR,
144         "Missing --gce, --json_sts_options, or --json_refresh_token option.");
145     exit(1);
146   }
147   GPR_ASSERT(creds != nullptr);
148 
149   token = grpc_test_fetch_oauth2_token_with_credentials(creds);
150   if (token != nullptr) {
151     printf("Got token: %s.\n", token);
152     gpr_free(token);
153   }
154   grpc_call_credentials_release(creds);
155   gpr_cmdline_destroy(cl);
156   grpc_shutdown();
157   return 0;
158 }
159