cli_credentials.cc
Go to the documentation of this file.
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 
20 
21 #include "absl/flags/flag.h"
22 
23 #include <grpc/slice.h>
24 #include <grpc/support/log.h>
26 
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(
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 
83  return "insecure";
84 }
85 
87 
88 std::shared_ptr<grpc::ChannelCredentials>
90  if (absl::GetFlag(FLAGS_channel_creds_type) == "insecure") {
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();
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();
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") {
118  } else if (absl::GetFlag(FLAGS_channel_creds_type) == "alts") {
121  } else if (absl::GetFlag(FLAGS_channel_creds_type) == "local") {
122  if (absl::GetFlag(FLAGS_local_connect_type) == "local_tcp") {
124  } else if (absl::GetFlag(FLAGS_local_connect_type) == "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))) {
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 =
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
171 }
172 
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 
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
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
testing
Definition: aws_request_signer_test.cc:25
absl::SetFlag
void SetFlag(absl::Flag< T > *flag, const T &v)
Definition: abseil-cpp/absl/flags/flag.h:110
log.h
grpc_load_file
grpc_error_handle grpc_load_file(const char *filename, int add_null_terminator, grpc_slice *output)
Definition: load_file.cc:33
grpc::testing::CliCredentials::GetCredentials
std::shared_ptr< grpc::ChannelCredentials > GetCredentials() const
Definition: cli_credentials.cc:156
load_file.h
grpc
Definition: grpcpp/alarm.h:33
slice.h
grpc::SslCredentials
std::shared_ptr< ChannelCredentials > SslCredentials(const SslCredentialsOptions &options)
Builds SSL Credentials given SSL specific options.
Definition: secure_credentials.cc:129
grpc::testing::CliCredentials::GetDefaultChannelCredsType
virtual std::string GetDefaultChannelCredsType() const
Definition: cli_credentials.cc:82
grpc::testing::CliCredentials::GetCallCredentials
virtual std::shared_ptr< grpc::CallCredentials > GetCallCredentials() const
Definition: cli_credentials.cc:139
ABSL_FLAG
ABSL_FLAG(std::string, ssl_target, "", "If not empty, treat the server host name as this for ssl/tls certificate " "validation.")
grpc::SslCredentialsOptions::pem_cert_chain
grpc::string pem_cert_chain
Definition: include/grpcpp/security/credentials.h:171
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::CompositeChannelCredentials
std::shared_ptr< ChannelCredentials > CompositeChannelCredentials(const std::shared_ptr< ChannelCredentials > &channel_creds, const std::shared_ptr< CallCredentials > &call_creds)
Definition: secure_credentials.cc:373
slice.h
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
grpc::GoogleDefaultCredentials
std::shared_ptr< ChannelCredentials > GoogleDefaultCredentials()
Definition: secure_credentials.cc:115
LOCAL_TCP
@ LOCAL_TCP
Definition: grpc_security_constants.h:143
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
grpc::testing::UDS
Definition: fullstack_fixtures.h:133
grpc::SslCredentialsOptions
Options used to build SslCredentials.
Definition: include/grpcpp/security/credentials.h:156
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
grpc::experimental::LocalCredentials
std::shared_ptr< ChannelCredentials > LocalCredentials(grpc_local_connect_type type)
Builds Local Credentials.
Definition: secure_credentials.cc:309
ABSL_RETIRED_FLAG
ABSL_RETIRED_FLAG(bool, enable_ssl, false, "Replaced by --channel_creds_type=ssl.")
grpc::testing::CliCredentials::GetDefaultCallCreds
virtual std::string GetDefaultCallCreds() const
Definition: cli_credentials.cc:86
grpc::testing::CliCredentials::GetCredentialUsage
virtual std::string GetCredentialUsage() const
Definition: cli_credentials.cc:173
call_creds
void call_creds(grpc_end2end_test_config config)
Definition: call_creds.cc:523
grpc_empty_slice
GPRAPI grpc_slice grpc_empty_slice(void)
Definition: slice/slice.cc:42
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
google_benchmark.example.empty
def empty(state)
Definition: example.py:31
grpc::SslCredentialsOptions::pem_private_key
grpc::string pem_private_key
Definition: include/grpcpp/security/credentials.h:166
grpc::testing::CliCredentials::GetSslTargetNameOverride
virtual std::string GetSslTargetNameOverride() const
Definition: cli_credentials.cc:184
grpc::testing::CliCredentials::GetChannelCredentials
virtual std::shared_ptr< grpc::ChannelCredentials > GetChannelCredentials() const
Definition: cli_credentials.cc:89
grpc::experimental::AltsCredentials
std::shared_ptr< ChannelCredentials > AltsCredentials(const AltsCredentialsOptions &options)
Builds ALTS Credentials given ALTS specific options.
Definition: secure_credentials.cc:294
grpc::AccessTokenCredentials
std::shared_ptr< CallCredentials > AccessTokenCredentials(const grpc::string &access_token)
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
grpc::experimental::AltsCredentialsOptions
Options used to build AltsCredentials.
Definition: include/grpcpp/security/credentials.h:327
grpc::StringFromCopiedSlice
std::string StringFromCopiedSlice(grpc_slice slice)
Definition: include/grpcpp/impl/codegen/slice.h:139
cli_credentials.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:46