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 "absl/flags/flag.h"
23 #include <grpc/slice.h>
24 #include <grpc/support/log.h>
25 #include <grpcpp/impl/codegen/slice.h>
27 #include "src/core/lib/iomgr/load_file.h"
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>.");
36 std::string, ssl_target, "",
37 "If not empty, treat the server host name as this for ssl/tls certificate "
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 "
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.");
51 std::string, channel_creds_type, "",
52 "The channel creds type: insecure, ssl, gdc (Google Default Credentials), "
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.");
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;
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;
73 std::string AccessToken(const std::string& auth) {
74 if (!IsAccessToken(auth)) {
77 return std::string(auth, ACCESS_TOKEN_PREFIX_LEN);
82 std::string CliCredentials::GetDefaultChannelCredsType() const {
86 std::string CliCredentials::GetDefaultCallCreds() const { return "none"; }
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();
99 grpc_load_file(absl::GetFlag(FLAGS_ssl_client_cert).c_str(), 1,
101 ssl_creds_options.pem_cert_chain =
102 grpc::StringFromCopiedSlice(cert_slice);
103 grpc_slice_unref(cert_slice);
105 if (!absl::GetFlag(FLAGS_ssl_client_key).empty()) {
106 grpc_slice key_slice = grpc_empty_slice();
109 grpc_load_file(absl::GetFlag(FLAGS_ssl_client_key).c_str(), 1,
111 ssl_creds_options.pem_private_key =
112 grpc::StringFromCopiedSlice(key_slice);
113 grpc_slice_unref(key_slice);
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);
128 "--local_connect_type=%s invalid; must be local_tcp or uds.\n",
129 absl::GetFlag(FLAGS_local_connect_type).c_str());
133 "--channel_creds_type=%s invalid; must be insecure, ssl, gdc, "
135 absl::GetFlag(FLAGS_channel_creds_type).c_str());
136 return std::shared_ptr<grpc::ChannelCredentials>();
139 std::shared_ptr<grpc::CallCredentials> CliCredentials::GetCallCredentials()
141 if (IsAccessToken(absl::GetFlag(FLAGS_call_creds))) {
142 return grpc::AccessTokenCredentials(
143 AccessToken(absl::GetFlag(FLAGS_call_creds)));
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>();
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>();
156 std::shared_ptr<grpc::ChannelCredentials> CliCredentials::GetCredentials()
158 if (absl::GetFlag(FLAGS_call_creds).empty()) {
159 absl::SetFlag(&FLAGS_call_creds, GetDefaultCallCreds());
161 if (absl::GetFlag(FLAGS_channel_creds_type).empty()) {
162 absl::SetFlag(&FLAGS_channel_creds_type, GetDefaultChannelCredsType());
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)
170 : grpc::CompositeChannelCredentials(channel_creds, call_creds);
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 "
180 " --call_creds ; Set to none, or"
181 " access_token=<token>\n";
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) : "";
190 } // namespace testing