3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "test/cpp/util/cli_credentials.h"
21 #include <grpc/slice.h>
22 #include <grpc/support/log.h>
23 #include <grpcpp/impl/codegen/slice.h>
25 #include "absl/flags/flag.h"
26 #include "src/core/lib/iomgr/load_file.h"
28 ABSL_RETIRED_FLAG(bool, enable_ssl, false,
29 "Replaced by --channel_creds_type=ssl.");
30 ABSL_RETIRED_FLAG(bool, use_auth, false,
31 "Replaced by --channel_creds_type=gdc.");
32 ABSL_RETIRED_FLAG(std::string, access_token, "",
33 "Replaced by --call_creds=access_token=<token>.");
35 std::string, ssl_target, "",
36 "If not empty, treat the server host name as this for ssl/tls certificate "
39 std::string, ssl_client_cert, "",
40 "If not empty, load this PEM formatted client certificate file. Requires "
41 "use of --ssl_client_key.");
42 ABSL_FLAG(std::string, ssl_client_key, "",
43 "If not empty, load this PEM formatted private key. Requires use of "
46 std::string, local_connect_type, "local_tcp",
47 "The type of local connections for which local channel credentials will "
48 "be applied. Should be local_tcp or uds.");
50 std::string, channel_creds_type, "",
51 "The channel creds type: insecure, ssl, gdc (Google Default Credentials), "
54 std::string, call_creds, "",
55 "Call credentials to use: none (default), or access_token=<token>. If "
56 "provided, the call creds are composited on top of channel creds.");
63 const char ACCESS_TOKEN_PREFIX[] = "access_token=";
64 constexpr int ACCESS_TOKEN_PREFIX_LEN =
65 sizeof(ACCESS_TOKEN_PREFIX) / sizeof(*ACCESS_TOKEN_PREFIX) - 1;
67 bool IsAccessToken(const std::string& auth) {
68 return auth.length() > ACCESS_TOKEN_PREFIX_LEN &&
69 auth.compare(0, ACCESS_TOKEN_PREFIX_LEN, ACCESS_TOKEN_PREFIX) == 0;
72 std::string AccessToken(const std::string& auth) {
73 if (!IsAccessToken(auth)) {
76 return std::string(auth, ACCESS_TOKEN_PREFIX_LEN);
81 std::string CliCredentials::GetDefaultChannelCredsType() const {
85 std::string CliCredentials::GetDefaultCallCreds() const { return "none"; }
87 std::shared_ptr<grpc::ChannelCredentials>
88 CliCredentials::GetChannelCredentials() const {
89 if (absl::GetFlag(FLAGS_channel_creds_type) == "insecure") {
90 return grpc::InsecureChannelCredentials();
91 } else if (absl::GetFlag(FLAGS_channel_creds_type) == "ssl") {
92 grpc::SslCredentialsOptions ssl_creds_options;
93 // TODO(@Capstan): This won't affect Google Default Credentials using SSL.
94 if (!absl::GetFlag(FLAGS_ssl_client_cert).empty()) {
95 grpc_slice cert_slice = grpc_empty_slice();
98 grpc_load_file(absl::GetFlag(FLAGS_ssl_client_cert).c_str(), 1,
100 ssl_creds_options.pem_cert_chain =
101 grpc::StringFromCopiedSlice(cert_slice);
102 grpc_slice_unref(cert_slice);
104 if (!absl::GetFlag(FLAGS_ssl_client_key).empty()) {
105 grpc_slice key_slice = grpc_empty_slice();
108 grpc_load_file(absl::GetFlag(FLAGS_ssl_client_key).c_str(), 1,
110 ssl_creds_options.pem_private_key =
111 grpc::StringFromCopiedSlice(key_slice);
112 grpc_slice_unref(key_slice);
114 return grpc::SslCredentials(ssl_creds_options);
115 } else if (absl::GetFlag(FLAGS_channel_creds_type) == "gdc") {
116 return grpc::GoogleDefaultCredentials();
117 } else if (absl::GetFlag(FLAGS_channel_creds_type) == "alts") {
118 return grpc::experimental::AltsCredentials(
119 grpc::experimental::AltsCredentialsOptions());
120 } else if (absl::GetFlag(FLAGS_channel_creds_type) == "local") {
121 if (absl::GetFlag(FLAGS_local_connect_type) == "local_tcp") {
122 return grpc::experimental::LocalCredentials(LOCAL_TCP);
123 } else if (absl::GetFlag(FLAGS_local_connect_type) == "uds") {
124 return grpc::experimental::LocalCredentials(UDS);
127 "--local_connect_type=%s invalid; must be local_tcp or uds.\n",
128 absl::GetFlag(FLAGS_local_connect_type).c_str());
132 "--channel_creds_type=%s invalid; must be insecure, ssl, gdc, "
134 absl::GetFlag(FLAGS_channel_creds_type).c_str());
135 return std::shared_ptr<grpc::ChannelCredentials>();
138 std::shared_ptr<grpc::CallCredentials> CliCredentials::GetCallCredentials()
140 if (IsAccessToken(absl::GetFlag(FLAGS_call_creds))) {
141 return grpc::AccessTokenCredentials(
142 AccessToken(absl::GetFlag(FLAGS_call_creds)));
144 if (absl::GetFlag(FLAGS_call_creds) == "none") {
145 // Nothing to do; creds, if any, are baked into the channel.
146 return std::shared_ptr<grpc::CallCredentials>();
149 "--call_creds=%s invalid; must be none "
150 "or access_token=<token>.\n",
151 absl::GetFlag(FLAGS_call_creds).c_str());
152 return std::shared_ptr<grpc::CallCredentials>();
155 std::shared_ptr<grpc::ChannelCredentials> CliCredentials::GetCredentials()
157 if (absl::GetFlag(FLAGS_call_creds).empty()) {
158 absl::SetFlag(&FLAGS_call_creds, GetDefaultCallCreds());
160 if (absl::GetFlag(FLAGS_channel_creds_type).empty()) {
161 absl::SetFlag(&FLAGS_channel_creds_type, GetDefaultChannelCredsType());
163 std::shared_ptr<grpc::ChannelCredentials> channel_creds =
164 GetChannelCredentials();
165 // Composite any call-type credentials on top of the base channel.
166 std::shared_ptr<grpc::CallCredentials> call_creds = GetCallCredentials();
167 return (channel_creds == nullptr || call_creds == nullptr)
169 : grpc::CompositeChannelCredentials(channel_creds, call_creds);
172 const std::string CliCredentials::GetCredentialUsage() const {
173 return " --ssl_target ; Set server host for ssl validation\n"
174 " --ssl_client_cert ; Client cert for ssl\n"
175 " --ssl_client_key ; Client private key for ssl\n"
176 " --local_connect_type ; Set to local_tcp or uds\n"
177 " --channel_creds_type ; Set to insecure, ssl, gdc, alts, or "
179 " --call_creds ; Set to none, or"
180 " access_token=<token>\n";
183 const std::string CliCredentials::GetSslTargetNameOverride() const {
184 bool use_ssl = absl::GetFlag(FLAGS_channel_creds_type) == "ssl" ||
185 absl::GetFlag(FLAGS_channel_creds_type) == "gdc";
186 return use_ssl ? absl::GetFlag(FLAGS_ssl_target) : "";
189 } // namespace testing