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 #include <grpcpp/security/credentials.h>
29 
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::experimental::StsCredentialsOptions options;
40   if (strlen(json_file_path) == 0) {
41     auto status = grpc::experimental::StsCredentialsOptionsFromEnv(&options);
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;
48     GPR_ASSERT(GRPC_LOG_IF_ERROR(
49         "load_file", grpc_load_file(json_file_path, 1, &sts_options_slice)));
50     auto status = grpc::experimental::StsCredentialsOptionsFromJson(
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   }
59   grpc_sts_credentials_options opts =
60       grpc::experimental::StsCredentialsCppToCoreOptions(options);
61   grpc_call_credentials* result = grpc_sts_credentials_create(&opts, nullptr);
62   return result;
63 }
64 
create_refresh_token_creds(const char * json_refresh_token_file_path)65 static grpc_call_credentials* create_refresh_token_creds(
66     const char* json_refresh_token_file_path) {
67   grpc_slice refresh_token;
68   GPR_ASSERT(GRPC_LOG_IF_ERROR(
69       "load_file",
70       grpc_load_file(json_refresh_token_file_path, 1, &refresh_token)));
71   grpc_call_credentials* result = grpc_google_refresh_token_credentials_create(
72       reinterpret_cast<const char*> GRPC_SLICE_START_PTR(refresh_token),
73       nullptr);
74   gpr_slice_unref(refresh_token);
75   return result;
76 }
77 
main(int argc,char ** argv)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);
88   gpr_cmdline_add_string(
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);
94   gpr_cmdline_add_flag(
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) {
113       gpr_log(GPR_INFO,
114               "Ignoring json refresh token or sts options to get a token from "
115               "the GCE metadata server.");
116     }
117     creds = grpc_google_compute_engine_credentials_create(nullptr);
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) {
125       gpr_log(GPR_ERROR,
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) {
134       gpr_log(GPR_ERROR,
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 
148   token = grpc_test_fetch_oauth2_token_with_credentials(creds);
149   if (token != nullptr) {
150     printf("Got token: %s.\n", token);
151     gpr_free(token);
152   }
153   grpc_call_credentials_release(creds);
154   gpr_cmdline_destroy(cl);
155   grpc_shutdown();
156   return 0;
157 }
158