security_context.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 <string.h>
24 
25 #include <algorithm>
26 
27 #include <grpc/grpc_security.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
31 
40 
42  false, "auth_context_refcount");
43 
44 /* --- grpc_call --- */
45 
47  grpc_call_credentials* creds) {
50  GRPC_API_TRACE("grpc_call_set_credentials(call=%p, creds=%p)", 2,
51  (call, creds));
52  if (!grpc_call_is_client(call)) {
53  gpr_log(GPR_ERROR, "Method is client-side only.");
55  }
56  ctx = static_cast<grpc_client_security_context*>(
58  if (ctx == nullptr) {
62  } else {
63  ctx->creds = creds != nullptr ? creds->Ref() : nullptr;
64  }
65 
66  return GRPC_CALL_OK;
67 }
68 
71  GRPC_API_TRACE("grpc_call_auth_context(call=%p)", 1, (call));
72  if (sec_ctx == nullptr) return nullptr;
74  auto* sc = static_cast<grpc_client_security_context*>(sec_ctx);
75  if (sc->auth_context == nullptr) {
76  return nullptr;
77  } else {
78  return sc->auth_context
79  ->Ref(DEBUG_LOCATION, "grpc_call_auth_context client")
80  .release();
81  }
82  } else {
83  auto* sc = static_cast<grpc_server_security_context*>(sec_ctx);
84  if (sc->auth_context == nullptr) {
85  return nullptr;
86  } else {
87  return sc->auth_context
88  ->Ref(DEBUG_LOCATION, "grpc_call_auth_context server")
89  .release();
90  }
91  }
92 }
93 
95  GRPC_API_TRACE("grpc_auth_context_release(context=%p)", 1, (context));
96  if (context == nullptr) return;
97  context->Unref(DEBUG_LOCATION, "grpc_auth_context_unref");
98 }
99 
100 /* --- grpc_client_security_context --- */
102  auth_context.reset(DEBUG_LOCATION, "client_security_context");
103  if (extension.instance != nullptr && extension.destroy != nullptr) {
105  }
106 }
107 
110  return arena->New<grpc_client_security_context>(
111  creds != nullptr ? creds->Ref() : nullptr);
112 }
113 
117  static_cast<grpc_client_security_context*>(ctx);
118  c->~grpc_client_security_context();
119 }
120 
121 /* --- grpc_server_security_context --- */
123  auth_context.reset(DEBUG_LOCATION, "server_security_context");
124  if (extension.instance != nullptr && extension.destroy != nullptr) {
126  }
127 }
128 
131  return arena->New<grpc_server_security_context>();
132 }
133 
136  static_cast<grpc_server_security_context*>(ctx);
137  c->~grpc_server_security_context();
138 }
139 
140 /* --- grpc_auth_context --- */
141 
142 static grpc_auth_property_iterator empty_iterator = {nullptr, 0, nullptr};
143 
145  const grpc_auth_context* ctx) {
146  GRPC_API_TRACE("grpc_auth_context_peer_identity_property_name(ctx=%p)", 1,
147  (ctx));
148  return ctx->peer_identity_property_name();
149 }
150 
152  const char* name) {
157  "grpc_auth_context_set_peer_identity_property_name(ctx=%p, name=%s)", 2,
158  (ctx, name));
159  if (prop == nullptr) {
160  gpr_log(GPR_ERROR, "Property name %s not found in auth context.",
161  name != nullptr ? name : "NULL");
162  return 0;
163  }
164  ctx->set_peer_identity_property_name(prop->name);
165  return 1;
166 }
167 
169  GRPC_API_TRACE("grpc_auth_context_peer_is_authenticated(ctx=%p)", 1, (ctx));
170  return ctx->is_authenticated();
171 }
172 
174  const grpc_auth_context* ctx) {
176  GRPC_API_TRACE("grpc_auth_context_property_iterator(ctx=%p)", 1, (ctx));
177  if (ctx == nullptr) return it;
178  it.ctx = ctx;
179  return it;
180 }
181 
184  GRPC_API_TRACE("grpc_auth_property_iterator_next(it=%p)", 1, (it));
185  if (it == nullptr || it->ctx == nullptr) return nullptr;
186  while (it->index == it->ctx->properties().count) {
187  if (it->ctx->chained() == nullptr) return nullptr;
188  it->ctx = it->ctx->chained();
189  it->index = 0;
190  }
191  if (it->name == nullptr) {
192  return &it->ctx->properties().array[it->index++];
193  } else {
194  while (it->index < it->ctx->properties().count) {
195  const grpc_auth_property* prop =
196  &it->ctx->properties().array[it->index++];
197  GPR_ASSERT(prop->name != nullptr);
198  if (strcmp(it->name, prop->name) == 0) {
199  return prop;
200  }
201  }
202  /* We could not find the name, try another round. */
204  }
205 }
206 
208  const grpc_auth_context* ctx, const char* name) {
210  GRPC_API_TRACE("grpc_auth_context_find_properties_by_name(ctx=%p, name=%s)",
211  2, (ctx, name));
212  if (ctx == nullptr || name == nullptr) return empty_iterator;
213  it.ctx = ctx;
214  it.name = name;
215  return it;
216 }
217 
219  const grpc_auth_context* ctx) {
220  GRPC_API_TRACE("grpc_auth_context_peer_identity(ctx=%p)", 1, (ctx));
221  if (ctx == nullptr) return empty_iterator;
223  ctx, ctx->peer_identity_property_name());
224 }
225 
232  }
233 }
234 
235 void grpc_auth_context::add_property(const char* name, const char* value,
236  size_t value_length) {
237  ensure_capacity();
239  prop->name = gpr_strdup(name);
240  prop->value = static_cast<char*>(gpr_malloc(value_length + 1));
241  if (value != nullptr) {
242  memcpy(prop->value, value, value_length);
243  }
244  prop->value[value_length] = '\0';
245  prop->value_length = value_length;
246 }
247 
249  const char* value, size_t value_length) {
251  "grpc_auth_context_add_property(ctx=%p, name=%s, value=%*.*s, "
252  "value_length=%lu)",
253  6,
254  (ctx, name, (int)value_length, (int)value_length, value,
255  (unsigned long)value_length));
256  ctx->add_property(name, value, value_length);
257 }
258 
260  const char* value) {
261  ensure_capacity();
263  prop->name = gpr_strdup(name);
264  prop->value = gpr_strdup(value);
265  prop->value_length = strlen(value);
266 }
267 
269  const char* name,
270  const char* value) {
272  "grpc_auth_context_add_cstring_property(ctx=%p, name=%s, value=%s)", 3,
273  (ctx, name, value));
274  ctx->add_cstring_property(name, value);
275 }
276 
278  gpr_free(property->name);
279  gpr_free(property->value);
280  memset(property, 0, sizeof(grpc_auth_property));
281 }
282 
284  if (p != nullptr) {
285  static_cast<grpc_auth_context*>(p)->Unref(DEBUG_LOCATION,
286  "auth_context_pointer_arg");
287  }
288 }
289 
290 static void* auth_context_pointer_arg_copy(void* p) {
291  auto* ctx = static_cast<grpc_auth_context*>(p);
292  return ctx == nullptr
293  ? nullptr
294  : ctx->Ref(DEBUG_LOCATION, "auth_context_pointer_arg").release();
295 }
296 
297 static int auth_context_pointer_cmp(void* a, void* b) {
298  return grpc_core::QsortCompare(a, b);
299 }
300 
304 
307  const_cast<char*>(GRPC_AUTH_CONTEXT_ARG), c,
309 }
310 
312  if (strcmp(arg->key, GRPC_AUTH_CONTEXT_ARG) != 0) return nullptr;
313  if (arg->type != GRPC_ARG_POINTER) {
314  gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
316  return nullptr;
317  }
318  return static_cast<grpc_auth_context*>(arg->value.pointer.p);
319 }
320 
322  const grpc_channel_args* args) {
323  size_t i;
324  if (args == nullptr) return nullptr;
325  for (i = 0; i < args->num_args; i++) {
327  if (p != nullptr) return p;
328  }
329  return nullptr;
330 }
grpc_server_security_context_destroy
void grpc_server_security_context_destroy(void *ctx)
Definition: security_context.cc:134
grpc_arg
Definition: grpc_types.h:103
grpc_call_error
grpc_call_error
Definition: grpc_types.h:464
grpc_auth_context
Definition: security_context.h:63
regen-readme.it
it
Definition: regen-readme.py:15
log.h
ctx
Definition: benchmark-async.c:30
memset
return memset(p, 0, total)
grpc_auth_context::add_cstring_property
void add_cstring_property(const char *name, const char *value)
Definition: security_context.cc:259
grpc_security_context_extension::destroy
void(* destroy)(void *)
Definition: security_context.h:126
auth_context_pointer_arg_destroy
static void auth_context_pointer_arg_destroy(void *p)
Definition: security_context.cc:283
string.h
grpc_core::RefCountedPtr::reset
void reset(T *value=nullptr)
Definition: ref_counted_ptr.h:111
grpc_auth_context_add_property
void grpc_auth_context_add_property(grpc_auth_context *ctx, const char *name, const char *value, size_t value_length)
Definition: security_context.cc:248
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
grpc_auth_context_set_peer_identity_property_name
int grpc_auth_context_set_peer_identity_property_name(grpc_auth_context *ctx, const char *name)
Definition: security_context.cc:151
grpc_client_security_context::extension
grpc_security_context_extension extension
Definition: security_context.h:141
arg::value
void * value
Definition: cmdline.cc:44
arena.h
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
grpc_call_credentials
Definition: src/core/lib/security/credentials/credentials.h:189
GRPC_CALL_OK
@ GRPC_CALL_OK
Definition: grpc_types.h:466
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
setup.name
name
Definition: setup.py:542
grpc_core::RefCountedPtr::release
T * release()
Definition: ref_counted_ptr.h:140
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
xds_manager.p
p
Definition: xds_manager.py:60
grpc_security.h
GRPC_CALL_ERROR_NOT_ON_SERVER
@ GRPC_CALL_ERROR_NOT_ON_SERVER
Definition: grpc_types.h:470
grpc_core::Arena
Definition: src/core/lib/resource_quota/arena.h:45
credentials.h
grpc_arg_pointer_vtable
Definition: grpc_types.h:85
grpc_channel_args
Definition: grpc_types.h:132
grpc_auth_property_iterator_next
const grpc_auth_property * grpc_auth_property_iterator_next(grpc_auth_property_iterator *it)
Definition: security_context.cc:182
grpc_auth_context_to_arg
grpc_arg grpc_auth_context_to_arg(grpc_auth_context *c)
Definition: security_context.cc:305
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
call
FilterStackCall * call
Definition: call.cc:750
grpc_call_auth_context
grpc_auth_context * grpc_call_auth_context(grpc_call *call)
Definition: security_context.cc:69
GRPC_CONTEXT_SECURITY
@ GRPC_CONTEXT_SECURITY
Definition: core/lib/channel/context.h:34
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
grpc_auth_context_from_arg
grpc_auth_context * grpc_auth_context_from_arg(const grpc_arg *arg)
Definition: security_context.cc:311
string_util.h
context.h
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
grpc_auth_property_array::capacity
size_t capacity
Definition: security_context.h:53
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
arg::type
argtype type
Definition: cmdline.cc:43
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_client_security_context_create
grpc_client_security_context * grpc_client_security_context_create(grpc_core::Arena *arena, grpc_call_credentials *creds)
Definition: security_context.cc:108
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_realloc
GPRAPI void * gpr_realloc(void *p, size_t size)
Definition: alloc.cc:56
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_find_auth_context_in_args
grpc_auth_context * grpc_find_auth_context_in_args(const grpc_channel_args *args)
Definition: security_context.cc:321
grpc_auth_property::name
char * name
Definition: grpc_security.h:44
grpc_auth_property_iterator
Definition: grpc_security.h:36
grpc_call_set_credentials
grpc_call_error grpc_call_set_credentials(grpc_call *call, grpc_call_credentials *creds)
Definition: security_context.cc:46
grpc_call
struct grpc_call grpc_call
Definition: grpc_types.h:70
grpc_auth_context::properties_
grpc_auth_property_array properties_
Definition: security_context.h:115
grpc_auth_property_array::array
grpc_auth_property * array
Definition: security_context.h:51
grpc_auth_property::value_length
size_t value_length
Definition: grpc_security.h:46
auth_context_pointer_arg_copy
static void * auth_context_pointer_arg_copy(void *p)
Definition: security_context.cc:290
arg
Definition: cmdline.cc:40
grpc_auth_property::value
char * value
Definition: grpc_security.h:45
grpc_trace_auth_context_refcount
grpc_core::DebugOnlyTraceFlag grpc_trace_auth_context_refcount(false, "auth_context_refcount")
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_call_get_arena
grpc_core::Arena * grpc_call_get_arena(grpc_call *call)
Definition: call.cc:1823
grpc_server_security_context
Definition: security_context.h:152
grpc_core::TraceFlag
Definition: debug/trace.h:63
grpc_auth_property
Definition: grpc_security.h:43
grpc_client_security_context::~grpc_client_security_context
~grpc_client_security_context()
Definition: security_context.cc:101
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_client_security_context
Definition: security_context.h:133
grpc_auth_context_find_properties_by_name
grpc_auth_property_iterator grpc_auth_context_find_properties_by_name(const grpc_auth_context *ctx, const char *name)
Definition: security_context.cc:207
grpc_call_is_client
uint8_t grpc_call_is_client(grpc_call *call)
Definition: call.cc:1863
security_context.h
grpc_client_security_context::auth_context
grpc_core::RefCountedPtr< grpc_auth_context > auth_context
Definition: security_context.h:140
grpc_call_context_get
void * grpc_call_context_get(grpc_call *call, grpc_context_index elem)
Definition: call.cc:1859
grpc_call_context_set
void grpc_call_context_set(grpc_call *call, grpc_context_index elem, void *value, void(*destroy)(void *value))
Definition: call.cc:1854
GRPC_AUTH_CONTEXT_ARG
#define GRPC_AUTH_CONTEXT_ARG
Definition: security_context.h:58
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
grpc_auth_context_peer_is_authenticated
int grpc_auth_context_peer_is_authenticated(const grpc_auth_context *ctx)
Definition: security_context.cc:168
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_auth_property_reset
void grpc_auth_property_reset(grpc_auth_property *property)
Definition: security_context.cc:277
grpc_client_security_context_destroy
void grpc_client_security_context_destroy(void *ctx)
Definition: security_context.cc:114
alloc.h
grpc_auth_context_peer_identity
grpc_auth_property_iterator grpc_auth_context_peer_identity(const grpc_auth_context *ctx)
Definition: security_context.cc:218
grpc_auth_context::add_property
void add_property(const char *name, const char *value, size_t value_length)
Definition: security_context.cc:235
grpc_server_security_context_create
grpc_server_security_context * grpc_server_security_context_create(grpc_core::Arena *arena)
Definition: security_context.cc:129
exec_ctx.h
grpc_auth_context_peer_identity_property_name
const char * grpc_auth_context_peer_identity_property_name(const grpc_auth_context *ctx)
Definition: security_context.cc:144
ref_counted_ptr.h
grpc_security_context_extension::instance
void * instance
Definition: security_context.h:125
grpc_server_security_context::extension
grpc_security_context_extension extension
Definition: security_context.h:157
channel_args.h
api_trace.h
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
auth_context_pointer_cmp
static int auth_context_pointer_cmp(void *a, void *b)
Definition: security_context.cc:297
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
auth_context_pointer_vtable
static const grpc_arg_pointer_vtable auth_context_pointer_vtable
Definition: security_context.cc:301
GRPC_ARG_POINTER
@ GRPC_ARG_POINTER
Definition: grpc_types.h:82
grpc_auth_context_release
void grpc_auth_context_release(grpc_auth_context *context)
Definition: security_context.cc:94
empty_iterator
static grpc_auth_property_iterator empty_iterator
Definition: security_context.cc:142
grpc_auth_context_add_cstring_property
void grpc_auth_context_add_cstring_property(grpc_auth_context *ctx, const char *name, const char *value)
Definition: security_context.cc:268
grpc_channel_arg_pointer_create
grpc_arg grpc_channel_arg_pointer_create(char *name, void *value, const grpc_arg_pointer_vtable *vtable)
Definition: channel_args.cc:492
grpc_server_security_context::~grpc_server_security_context
~grpc_server_security_context()
Definition: security_context.cc:122
grpc_auth_property_array::count
size_t count
Definition: security_context.h:52
grpc_auth_context::ensure_capacity
void ensure_capacity()
Definition: security_context.cc:226
grpc_auth_context_property_iterator
grpc_auth_property_iterator grpc_auth_context_property_iterator(const grpc_auth_context *ctx)
Definition: security_context.cc:173
call.h
grpc_core::RefCounted::Ref
RefCountedPtr< Child > Ref() GRPC_MUST_USE_RESULT
Definition: ref_counted.h:287
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
GRPC_API_TRACE
#define GRPC_API_TRACE(fmt, nargs, args)
Definition: api_trace.h:48
port_platform.h
grpc_server_security_context::auth_context
grpc_core::RefCountedPtr< grpc_auth_context > auth_context
Definition: security_context.h:156


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:15