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