Debian Codegraph @

test/cpp/util/cli_credentials.cc

raw
1 /*
2 *
3 * Copyright 2016 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 "test/cpp/util/cli_credentials.h"
20
21 #include "absl/flags/flag.h"
22
23 #include <grpc/slice.h>
24 #include <grpc/support/log.h>
25 #include <grpcpp/support/slice.h>
26
27 #include "src/core/lib/iomgr/load_file.h"
28
29 ABSL_RETIRED_FLAG(bool, enable_ssl, false,
30 "Replaced by --channel_creds_type=ssl.");
31 ABSL_RETIRED_FLAG(bool, use_auth, false,
32 "Replaced by --channel_creds_type=gdc.");
33 ABSL_RETIRED_FLAG(std::string, access_token, "",
34 "Replaced by --call_creds=access_token=<token>.");
35 ABSL_FLAG(
36 std::string, ssl_target, "",
37 "If not empty, treat the server host name as this for ssl/tls certificate "
38 "validation.");
39 ABSL_FLAG(
40 std::string, ssl_client_cert, "",
41 "If not empty, load this PEM formatted client certificate file. Requires "
42 "use of --ssl_client_key.");
43 ABSL_FLAG(std::string, ssl_client_key, "",
44 "If not empty, load this PEM formatted private key. Requires use of "
45 "--ssl_client_cert");
46 ABSL_FLAG(
47 std::string, local_connect_type, "local_tcp",
48 "The type of local connections for which local channel credentials will "
49 "be applied. Should be local_tcp or uds.");
50 ABSL_FLAG(
51 std::string, channel_creds_type, "",
52 "The channel creds type: insecure, ssl, gdc (Google Default Credentials), "
53 "alts, or local.");
54 ABSL_FLAG(
55 std::string, call_creds, "",
56 "Call credentials to use: none (default), or access_token=<token>. If "
57 "provided, the call creds are composited on top of channel creds.");
58
59 namespace grpc {
60 namespace testing {
61
62 namespace {
63
64 const char ACCESS_TOKEN_PREFIX[] = "access_token=";
65 constexpr int ACCESS_TOKEN_PREFIX_LEN =
66 sizeof(ACCESS_TOKEN_PREFIX) / sizeof(*ACCESS_TOKEN_PREFIX) - 1;
67
68 bool IsAccessToken(const std::string& auth) {
69 return auth.length() > ACCESS_TOKEN_PREFIX_LEN &&
70 auth.compare(0, ACCESS_TOKEN_PREFIX_LEN, ACCESS_TOKEN_PREFIX) == 0;
71 }
72
73 std::string AccessToken(const std::string& auth) {
74 if (!IsAccessToken(auth)) {
75 return "";
76 }
77 return std::string(auth, ACCESS_TOKEN_PREFIX_LEN);
78 }
79
80 } // namespace
81
82 std::string CliCredentials::GetDefaultChannelCredsType() const {
83 return "insecure";
84 }
85
86 std::string CliCredentials::GetDefaultCallCreds() const { return "none"; }
87
88 std::shared_ptr<grpc::ChannelCredentials>
89 CliCredentials::GetChannelCredentials() const {
90 if (absl::GetFlag(FLAGS_channel_creds_type) == "insecure") {
91 return grpc::InsecureChannelCredentials();
92 } else if (absl::GetFlag(FLAGS_channel_creds_type) == "ssl") {
93 grpc::SslCredentialsOptions ssl_creds_options;
94 // TODO(@Capstan): This won't affect Google Default Credentials using SSL.
95 if (!absl::GetFlag(FLAGS_ssl_client_cert).empty()) {
96 grpc_slice cert_slice = grpc_empty_slice();
97 GRPC_LOG_IF_ERROR(
98 "load_file",
99 grpc_load_file(absl::GetFlag(FLAGS_ssl_client_cert).c_str(), 1,
100 &cert_slice));
101 ssl_creds_options.pem_cert_chain =
102 grpc::StringFromCopiedSlice(cert_slice);
103 grpc_slice_unref(cert_slice);
104 }
105 if (!absl::GetFlag(FLAGS_ssl_client_key).empty()) {
106 grpc_slice key_slice = grpc_empty_slice();
107 GRPC_LOG_IF_ERROR(
108 "load_file",
109 grpc_load_file(absl::GetFlag(FLAGS_ssl_client_key).c_str(), 1,
110 &key_slice));
111 ssl_creds_options.pem_private_key =
112 grpc::StringFromCopiedSlice(key_slice);
113 grpc_slice_unref(key_slice);
114 }
115 return grpc::SslCredentials(ssl_creds_options);
116 } else if (absl::GetFlag(FLAGS_channel_creds_type) == "gdc") {
117 return grpc::GoogleDefaultCredentials();
118 } else if (absl::GetFlag(FLAGS_channel_creds_type) == "alts") {
119 return grpc::experimental::AltsCredentials(
120 grpc::experimental::AltsCredentialsOptions());
121 } else if (absl::GetFlag(FLAGS_channel_creds_type) == "local") {
122 if (absl::GetFlag(FLAGS_local_connect_type) == "local_tcp") {
123 return grpc::experimental::LocalCredentials(LOCAL_TCP);
124 } else if (absl::GetFlag(FLAGS_local_connect_type) == "uds") {
125 return grpc::experimental::LocalCredentials(UDS);
126 } else {
127 fprintf(stderr,
128 "--local_connect_type=%s invalid; must be local_tcp or uds.\n",
129 absl::GetFlag(FLAGS_local_connect_type).c_str());
130 }
131 }
132 fprintf(stderr,
133 "--channel_creds_type=%s invalid; must be insecure, ssl, gdc, "
134 "alts, or local.\n",
135 absl::GetFlag(FLAGS_channel_creds_type).c_str());
136 return std::shared_ptr<grpc::ChannelCredentials>();
137 }
138
139 std::shared_ptr<grpc::CallCredentials> CliCredentials::GetCallCredentials()
140 const {
141 if (IsAccessToken(absl::GetFlag(FLAGS_call_creds))) {
142 return grpc::AccessTokenCredentials(
143 AccessToken(absl::GetFlag(FLAGS_call_creds)));
144 }
145 if (absl::GetFlag(FLAGS_call_creds) == "none") {
146 // Nothing to do; creds, if any, are baked into the channel.
147 return std::shared_ptr<grpc::CallCredentials>();
148 }
149 fprintf(stderr,
150 "--call_creds=%s invalid; must be none "
151 "or access_token=<token>.\n",
152 absl::GetFlag(FLAGS_call_creds).c_str());
153 return std::shared_ptr<grpc::CallCredentials>();
154 }
155
156 std::shared_ptr<grpc::ChannelCredentials> CliCredentials::GetCredentials()
157 const {
158 if (absl::GetFlag(FLAGS_call_creds).empty()) {
159 absl::SetFlag(&FLAGS_call_creds, GetDefaultCallCreds());
160 }
161 if (absl::GetFlag(FLAGS_channel_creds_type).empty()) {
162 absl::SetFlag(&FLAGS_channel_creds_type, GetDefaultChannelCredsType());
163 }
164 std::shared_ptr<grpc::ChannelCredentials> channel_creds =
165 GetChannelCredentials();
166 // Composite any call-type credentials on top of the base channel.
167 std::shared_ptr<grpc::CallCredentials> call_creds = GetCallCredentials();
168 return (channel_creds == nullptr || call_creds == nullptr)
169 ? channel_creds
170 : grpc::CompositeChannelCredentials(channel_creds, call_creds);
171 }
172
173 std::string CliCredentials::GetCredentialUsage() const {
174 return " --ssl_target ; Set server host for ssl validation\n"
175 " --ssl_client_cert ; Client cert for ssl\n"
176 " --ssl_client_key ; Client private key for ssl\n"
177 " --local_connect_type ; Set to local_tcp or uds\n"
178 " --channel_creds_type ; Set to insecure, ssl, gdc, alts, or "
179 "local\n"
180 " --call_creds ; Set to none, or"
181 " access_token=<token>\n";
182 }
183
184 std::string CliCredentials::GetSslTargetNameOverride() const {
185 bool use_ssl = absl::GetFlag(FLAGS_channel_creds_type) == "ssl" ||
186 absl::GetFlag(FLAGS_channel_creds_type) == "gdc";
187 return use_ssl ? absl::GetFlag(FLAGS_ssl_target) : "";
188 }
189
190 } // namespace testing
191 } // namespace grpc