Imported Upstream version 1.34.0
[platform/upstream/grpc.git] / test / cpp / util / cli_credentials.cc
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 <grpc/slice.h>
22 #include <grpc/support/log.h>
23 #include <grpcpp/impl/codegen/slice.h>
24
25 #include "absl/flags/flag.h"
26 #include "src/core/lib/iomgr/load_file.h"
27
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>.");
34 ABSL_FLAG(
35     std::string, ssl_target, "",
36     "If not empty, treat the server host name as this for ssl/tls certificate "
37     "validation.");
38 ABSL_FLAG(
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 "
44           "--ssl_client_cert");
45 ABSL_FLAG(
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.");
49 ABSL_FLAG(
50     std::string, channel_creds_type, "",
51     "The channel creds type: insecure, ssl, gdc (Google Default Credentials), "
52     "alts, or local.");
53 ABSL_FLAG(
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.");
57
58 namespace grpc {
59 namespace testing {
60
61 namespace {
62
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;
66
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;
70 }
71
72 std::string AccessToken(const std::string& auth) {
73   if (!IsAccessToken(auth)) {
74     return "";
75   }
76   return std::string(auth, ACCESS_TOKEN_PREFIX_LEN);
77 }
78
79 }  // namespace
80
81 std::string CliCredentials::GetDefaultChannelCredsType() const {
82   return "insecure";
83 }
84
85 std::string CliCredentials::GetDefaultCallCreds() const { return "none"; }
86
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();
96       GRPC_LOG_IF_ERROR(
97           "load_file",
98           grpc_load_file(absl::GetFlag(FLAGS_ssl_client_cert).c_str(), 1,
99                          &cert_slice));
100       ssl_creds_options.pem_cert_chain =
101           grpc::StringFromCopiedSlice(cert_slice);
102       grpc_slice_unref(cert_slice);
103     }
104     if (!absl::GetFlag(FLAGS_ssl_client_key).empty()) {
105       grpc_slice key_slice = grpc_empty_slice();
106       GRPC_LOG_IF_ERROR(
107           "load_file",
108           grpc_load_file(absl::GetFlag(FLAGS_ssl_client_key).c_str(), 1,
109                          &key_slice));
110       ssl_creds_options.pem_private_key =
111           grpc::StringFromCopiedSlice(key_slice);
112       grpc_slice_unref(key_slice);
113     }
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);
125     } else {
126       fprintf(stderr,
127               "--local_connect_type=%s invalid; must be local_tcp or uds.\n",
128               absl::GetFlag(FLAGS_local_connect_type).c_str());
129     }
130   }
131   fprintf(stderr,
132           "--channel_creds_type=%s invalid; must be insecure, ssl, gdc, "
133           "alts, or local.\n",
134           absl::GetFlag(FLAGS_channel_creds_type).c_str());
135   return std::shared_ptr<grpc::ChannelCredentials>();
136 }
137
138 std::shared_ptr<grpc::CallCredentials> CliCredentials::GetCallCredentials()
139     const {
140   if (IsAccessToken(absl::GetFlag(FLAGS_call_creds))) {
141     return grpc::AccessTokenCredentials(
142         AccessToken(absl::GetFlag(FLAGS_call_creds)));
143   }
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>();
147   }
148   fprintf(stderr,
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>();
153 }
154
155 std::shared_ptr<grpc::ChannelCredentials> CliCredentials::GetCredentials()
156     const {
157   if (absl::GetFlag(FLAGS_call_creds).empty()) {
158     absl::SetFlag(&FLAGS_call_creds, GetDefaultCallCreds());
159   }
160   if (absl::GetFlag(FLAGS_channel_creds_type).empty()) {
161     absl::SetFlag(&FLAGS_channel_creds_type, GetDefaultChannelCredsType());
162   }
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)
168              ? channel_creds
169              : grpc::CompositeChannelCredentials(channel_creds, call_creds);
170 }
171
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 "
178          "local\n"
179          "    --call_creds             ; Set to none, or"
180          " access_token=<token>\n";
181 }
182
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) : "";
187 }
188
189 }  // namespace testing
190 }  // namespace grpc