fake_security_connector.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 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 
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include <utility>
27 
28 #include "absl/status/status.h"
29 #include "absl/strings/str_cat.h"
30 #include "absl/strings/string_view.h"
31 
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/log.h>
36 
59 
60 namespace {
61 class grpc_fake_channel_security_connector final
63  public:
64  grpc_fake_channel_security_connector(
67  const char* target, const grpc_channel_args* args)
69  std::move(channel_creds),
70  std::move(request_metadata_creds)),
72  expected_targets_(
74  is_lb_channel_(grpc_channel_args_find(
76  nullptr) {
77  const grpc_arg* target_name_override_arg =
79  if (target_name_override_arg != nullptr) {
80  target_name_override_ =
81  gpr_strdup(grpc_channel_arg_get_string(target_name_override_arg));
82  } else {
83  target_name_override_ = nullptr;
84  }
85  }
86 
87  ~grpc_fake_channel_security_connector() override {
89  gpr_free(expected_targets_);
90  if (target_name_override_ != nullptr) gpr_free(target_name_override_);
91  }
92 
93  void check_peer(tsi_peer peer, grpc_endpoint* ep,
95  grpc_closure* on_peer_checked) override;
96 
97  void cancel_check_peer(grpc_closure* /*on_peer_checked*/,
98  grpc_error_handle error) override {
100  }
101 
102  int cmp(const grpc_security_connector* other_sc) const override {
103  auto* other =
104  reinterpret_cast<const grpc_fake_channel_security_connector*>(other_sc);
105  int c = channel_security_connector_cmp(other);
106  if (c != 0) return c;
107  c = strcmp(target_, other->target_);
108  if (c != 0) return c;
109  if (expected_targets_ == nullptr || other->expected_targets_ == nullptr) {
110  c = grpc_core::QsortCompare(expected_targets_, other->expected_targets_);
111  } else {
112  c = strcmp(expected_targets_, other->expected_targets_);
113  }
114  if (c != 0) return c;
115  return grpc_core::QsortCompare(is_lb_channel_, other->is_lb_channel_);
116  }
117 
119  grpc_pollset_set* /*interested_parties*/,
120  grpc_core::HandshakeManager* handshake_mgr) override {
122  tsi_create_fake_handshaker(/*is_client=*/true), this, args));
123  }
124 
126  absl::string_view host, grpc_auth_context*) override {
127  absl::string_view authority_hostname;
128  absl::string_view authority_ignored_port;
129  absl::string_view target_hostname;
130  absl::string_view target_ignored_port;
131  grpc_core::SplitHostPort(host, &authority_hostname,
132  &authority_ignored_port);
133  grpc_core::SplitHostPort(target_, &target_hostname, &target_ignored_port);
134  if (target_name_override_ != nullptr) {
135  absl::string_view fake_security_target_name_override_hostname;
136  absl::string_view fake_security_target_name_override_ignored_port;
138  target_name_override_, &fake_security_target_name_override_hostname,
139  &fake_security_target_name_override_ignored_port);
140  if (authority_hostname != fake_security_target_name_override_hostname) {
142  "Authority (host) '%s' != Fake Security Target override '%s'",
143  host.data(),
144  fake_security_target_name_override_hostname.data());
145  abort();
146  }
147  } else if (authority_hostname != target_hostname) {
148  gpr_log(GPR_ERROR, "Authority (host) '%s' != Target '%s'", host.data(),
149  target_);
150  abort();
151  }
153  }
154 
155  char* target() const { return target_; }
156  char* expected_targets() const { return expected_targets_; }
157  bool is_lb_channel() const { return is_lb_channel_; }
158  char* target_name_override() const { return target_name_override_; }
159 
160  private:
161  bool fake_check_target(const char* target, const char* set_str) const {
162  GPR_ASSERT(target != nullptr);
163  char** set = nullptr;
164  size_t set_size = 0;
165  gpr_string_split(set_str, ",", &set, &set_size);
166  bool found = false;
167  for (size_t i = 0; i < set_size; ++i) {
168  if (set[i] != nullptr && strcmp(target, set[i]) == 0) found = true;
169  }
170  for (size_t i = 0; i < set_size; ++i) {
171  gpr_free(set[i]);
172  }
173  gpr_free(set);
174  return found;
175  }
176 
177  void fake_secure_name_check() const {
178  if (expected_targets_ == nullptr) return;
179  char** lbs_and_backends = nullptr;
180  size_t lbs_and_backends_size = 0;
181  bool success = false;
182  gpr_string_split(expected_targets_, ";", &lbs_and_backends,
183  &lbs_and_backends_size);
184  if (lbs_and_backends_size > 2 || lbs_and_backends_size == 0) {
185  gpr_log(GPR_ERROR, "Invalid expected targets arg value: '%s'",
186  expected_targets_);
187  goto done;
188  }
189  if (is_lb_channel_) {
190  if (lbs_and_backends_size != 2) {
192  "Invalid expected targets arg value: '%s'. Expectations for LB "
193  "channels must be of the form 'be1,be2,be3,...;lb1,lb2,...",
194  expected_targets_);
195  goto done;
196  }
197  if (!fake_check_target(target_, lbs_and_backends[1])) {
198  gpr_log(GPR_ERROR, "LB target '%s' not found in expected set '%s'",
199  target_, lbs_and_backends[1]);
200  goto done;
201  }
202  success = true;
203  } else {
204  if (!fake_check_target(target_, lbs_and_backends[0])) {
205  gpr_log(GPR_ERROR, "Backend target '%s' not found in expected set '%s'",
206  target_, lbs_and_backends[0]);
207  goto done;
208  }
209  success = true;
210  }
211  done:
212  for (size_t i = 0; i < lbs_and_backends_size; ++i) {
213  gpr_free(lbs_and_backends[i]);
214  }
215  gpr_free(lbs_and_backends);
216  if (!success) abort();
217  }
218 
219  char* target_;
220  char* expected_targets_;
221  bool is_lb_channel_;
222  char* target_name_override_;
223 };
224 
225 void fake_check_peer(grpc_security_connector* /*sc*/, tsi_peer peer,
227  grpc_closure* on_peer_checked) {
228  const char* prop_name;
230  *auth_context = nullptr;
231  if (peer.property_count != 2) {
233  "Fake peers should only have 2 properties.");
234  goto end;
235  }
236  prop_name = peer.properties[0].name;
237  if (prop_name == nullptr ||
238  strcmp(prop_name, TSI_CERTIFICATE_TYPE_PEER_PROPERTY) != 0) {
240  absl::StrCat("Unexpected property in fake peer: ",
241  prop_name == nullptr ? "<EMPTY>" : prop_name));
242  goto end;
243  }
244  if (strncmp(peer.properties[0].value.data, TSI_FAKE_CERTIFICATE_TYPE,
245  peer.properties[0].value.length) != 0) {
247  "Invalid value for cert type property.");
248  goto end;
249  }
250  prop_name = peer.properties[1].name;
251  if (prop_name == nullptr ||
252  strcmp(prop_name, TSI_SECURITY_LEVEL_PEER_PROPERTY) != 0) {
254  absl::StrCat("Unexpected property in fake peer: ",
255  prop_name == nullptr ? "<EMPTY>" : prop_name));
256  goto end;
257  }
258  if (strncmp(peer.properties[1].value.data, TSI_FAKE_SECURITY_LEVEL,
259  peer.properties[1].value.length) != 0) {
261  "Invalid value for security level property.");
262  goto end;
263  }
264 
265  *auth_context = grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
267  auth_context->get(), GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME,
270  auth_context->get(), GRPC_TRANSPORT_SECURITY_LEVEL_PROPERTY_NAME,
272 end:
273  grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_peer_checked, error);
274  tsi_peer_destruct(&peer);
275 }
276 
278  tsi_peer peer, grpc_endpoint* /*ep*/,
280  grpc_closure* on_peer_checked) {
281  fake_check_peer(this, peer, auth_context, on_peer_checked);
282  fake_secure_name_check();
283 }
284 
285 class grpc_fake_server_security_connector
287  public:
288  explicit grpc_fake_server_security_connector(
291  std::move(server_creds)) {}
292  ~grpc_fake_server_security_connector() override = default;
293 
294  void check_peer(tsi_peer peer, grpc_endpoint* /*ep*/,
296  grpc_closure* on_peer_checked) override {
297  fake_check_peer(this, peer, auth_context, on_peer_checked);
298  }
299 
300  void cancel_check_peer(grpc_closure* /*on_peer_checked*/,
301  grpc_error_handle error) override {
303  }
304 
306  grpc_pollset_set* /*interested_parties*/,
307  grpc_core::HandshakeManager* handshake_mgr) override {
309  tsi_create_fake_handshaker(/*=is_client*/ false), this, args));
310  }
311 
312  int cmp(const grpc_security_connector* other) const override {
314  static_cast<const grpc_server_security_connector*>(other));
315  }
316 };
317 } // namespace
318 
323  const char* target, const grpc_channel_args* args) {
324  return grpc_core::MakeRefCounted<grpc_fake_channel_security_connector>(
325  std::move(channel_creds), std::move(request_metadata_creds), target,
326  args);
327 }
328 
332  return grpc_core::MakeRefCounted<grpc_fake_server_security_connector>(
333  std::move(server_creds));
334 }
grpc_arg
Definition: grpc_types.h:103
GRPC_TRANSPORT_SECURITY_LEVEL_PROPERTY_NAME
#define GRPC_TRANSPORT_SECURITY_LEVEL_PROPERTY_NAME
Definition: grpc_security_constants.h:47
grpc_channel_arg_get_string
char * grpc_channel_arg_get_string(const grpc_arg *arg)
Definition: channel_args.cc:432
grpc_auth_context
Definition: security_context.h:63
fake_credentials.h
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
tsi_peer::properties
tsi_peer_property * properties
Definition: transport_security_interface.h:239
tsi_peer_property::value
struct tsi_peer_property::@48 value
gpr_string_split
void gpr_string_split(const char *input, const char *sep, char ***strs, size_t *nstrs)
Definition: string.cc:300
grpc_security_connector::check_peer
virtual void check_peer(tsi_peer peer, grpc_endpoint *ep, grpc_core::RefCountedPtr< grpc_auth_context > *auth_context, grpc_closure *on_peer_checked)=0
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
grpc_server_security_connector::add_handshakers
virtual void add_handshakers(const grpc_channel_args *args, grpc_pollset_set *interested_parties, grpc_core::HandshakeManager *handshake_mgr)=0
grpc_server_security_connector
Definition: security_connector.h:171
grpc_core::SecurityHandshakerCreate
RefCountedPtr< Handshaker > SecurityHandshakerCreate(tsi_handshaker *handshaker, grpc_security_connector *connector, const grpc_channel_args *args)
Creates a security handshaker using handshaker.
Definition: security_handshaker.cc:658
TSI_FAKE_SECURITY_LEVEL
#define TSI_FAKE_SECURITY_LEVEL
Definition: fake_transport_security.h:30
TSI_SECURITY_LEVEL_PEER_PROPERTY
#define TSI_SECURITY_LEVEL_PEER_PROPERTY
Definition: transport_security_interface.h:226
grpc_pollset_set
struct grpc_pollset_set grpc_pollset_set
Definition: iomgr_fwd.h:23
tsi_peer_property::length
size_t length
Definition: transport_security_interface.h:234
string.h
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
error
grpc_error_handle error
Definition: retry_filter.cc:499
closure.h
grpc_core::SplitHostPort
bool SplitHostPort(absl::string_view name, absl::string_view *host, absl::string_view *port)
Definition: host_port.cc:88
tsi_create_fake_handshaker
tsi_handshaker * tsi_create_fake_handshaker(int is_client)
Definition: fake_transport_security.cc:778
credentials.h
grpc_channel_args
Definition: grpc_types.h:132
grpclb.h
TSI_FAKE_CERTIFICATE_TYPE
#define TSI_FAKE_CERTIFICATE_TYPE
Definition: fake_transport_security.h:28
grpc_security_connector
Definition: security_connector.h:61
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
grpc_security_connector::cancel_check_peer
virtual void cancel_check_peer(grpc_closure *on_peer_checked, grpc_error_handle error)=0
string_util.h
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME
#define GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME
Definition: grpc_security_constants.h:26
grpc_core::RefCountedPtr< grpc_channel_credentials >
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
GRPC_SSL_TARGET_NAME_OVERRIDE_ARG
#define GRPC_SSL_TARGET_NAME_OVERRIDE_ARG
Definition: grpc_types.h:278
gen_stats_data.found
bool found
Definition: gen_stats_data.py:61
GRPC_ARG_ADDRESS_IS_GRPCLB_LOAD_BALANCER
#define GRPC_ARG_ADDRESS_IS_GRPCLB_LOAD_BALANCER
Definition: grpclb.h:27
grpc_fake_channel_security_connector_create
grpc_core::RefCountedPtr< grpc_channel_security_connector > grpc_fake_channel_security_connector_create(grpc_core::RefCountedPtr< grpc_channel_credentials > channel_creds, grpc_core::RefCountedPtr< grpc_call_credentials > request_metadata_creds, const char *target, const grpc_channel_args *args)
Definition: fake_security_connector.cc:320
grpc_channel_security_connector::CheckCallHost
virtual grpc_core::ArenaPromise< absl::Status > CheckCallHost(absl::string_view host, grpc_auth_context *auth_context)=0
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
grpc_core::HandshakeManager::Add
void Add(RefCountedPtr< Handshaker > handshaker)
Definition: src/core/lib/transport/handshaker.cc:63
arena_promise.h
grpc_auth_context_add_cstring_property
GRPCAPI void grpc_auth_context_add_cstring_property(grpc_auth_context *ctx, const char *name, const char *value)
Definition: security_context.cc:268
tsi_peer_property::data
char * data
Definition: transport_security_interface.h:233
error.h
GRPC_FAKE_SECURITY_URL_SCHEME
#define GRPC_FAKE_SECURITY_URL_SCHEME
Definition: security_connector.h:50
host_port.h
grpc_channel_security_connector::add_handshakers
virtual void add_handshakers(const grpc_channel_args *args, grpc_pollset_set *interested_parties, grpc_core::HandshakeManager *handshake_mgr)=0
Registers handshakers with handshake_mgr.
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_fake_transport_get_expected_targets
const char * grpc_fake_transport_get_expected_targets(const grpc_channel_args *args)
Definition: fake_credentials.cc:94
tsi_peer_property::name
char * name
Definition: transport_security_interface.h:231
transport_security_interface.h
promise.h
GRPC_ERROR_CREATE_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
Definition: error.h:291
grpc_channel_security_connector::channel_security_connector_cmp
int channel_security_connector_cmp(const grpc_channel_security_connector *other) const
Definition: security_connector.cc:45
security_handshaker.h
tsi_peer
Definition: transport_security_interface.h:238
security_context.h
grpc_core::ImmediateOkStatus
Definition: promise/promise.h:78
debug_location.h
grpc_core::ArenaPromise
Definition: arena_promise.h:152
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
poll.h
check_peer
static void check_peer(char *peer_name)
Definition: end2end/tests/simple_request.cc:91
GRPC_ERROR_CREATE_FROM_CPP_STRING
#define GRPC_ERROR_CREATE_FROM_CPP_STRING(desc)
Definition: error.h:297
alloc.h
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
cpp.gmock_class.set
set
Definition: bloaty/third_party/googletest/googlemock/scripts/generator/cpp/gmock_class.py:44
grpc_security_constants.h
exec_ctx.h
handshaker.h
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
ref_counted_ptr.h
channel_args.h
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
grpc_channel_security_connector
Definition: security_connector.h:118
grpc_core::HandshakeManager
Definition: handshaker.h:98
iomgr_fwd.h
endpoint.h
TSI_CERTIFICATE_TYPE_PEER_PROPERTY
#define TSI_CERTIFICATE_TYPE_PEER_PROPERTY
Definition: transport_security_interface.h:223
grpc_fake_server_security_connector_create
grpc_core::RefCountedPtr< grpc_server_security_connector > grpc_fake_server_security_connector_create(grpc_core::RefCountedPtr< grpc_server_credentials > server_creds)
Definition: fake_security_connector.cc:330
grpc_error
Definition: error_internal.h:42
grpc_security_connector::cmp
virtual int cmp(const grpc_security_connector *other) const =0
fake_transport_security.h
absl::string_view::data
constexpr const_pointer data() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:336
grpc_closure
Definition: closure.h:56
grpc_channel_args_find
const grpc_arg * grpc_channel_args_find(const grpc_channel_args *args, const char *name)
Definition: channel_args.cc:393
tsi_peer_destruct
void tsi_peer_destruct(tsi_peer *self)
Definition: transport_security.cc:320
grpc_endpoint
Definition: endpoint.h:105
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
target_
std::string target_
Definition: rls.cc:342
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
fake_security_connector.h
GRPC_FAKE_TRANSPORT_SECURITY_TYPE
#define GRPC_FAKE_TRANSPORT_SECURITY_TYPE
Definition: src/core/lib/security/credentials/credentials.h:53
tsi_peer::property_count
size_t property_count
Definition: transport_security_interface.h:240
grpc_server_security_connector::server_security_connector_cmp
int server_security_connector_cmp(const grpc_server_security_connector *other) const
Definition: security_connector.cc:67
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:22