rb_xds_channel_credentials.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2021 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 <ruby/ruby.h>
20 
22 
23 #include <string.h>
24 
25 #include "rb_call_credentials.h"
26 #include "rb_channel_credentials.h"
27 #include "rb_grpc.h"
29 
30 #include <grpc/grpc.h>
31 #include <grpc/grpc_security.h>
32 #include <grpc/support/alloc.h>
33 #include <grpc/support/log.h>
34 
35 /* grpc_rb_cXdsChannelCredentials is the ruby class that proxies
36  grpc_channel_credentials. */
37 static VALUE grpc_rb_cXdsChannelCredentials = Qnil;
38 
39 /* grpc_rb_xds_channel_credentials wraps a grpc_channel_credentials. It
40  * provides a mark object that is used to hold references to any objects used to
41  * create the credentials. */
43  /* Holder of ruby objects involved in constructing the credentials */
44  VALUE mark;
45 
46  /* The actual credentials */
49 
52  if (p == NULL) {
53  return;
54  };
57  wrapper->wrapped = NULL;
58 
59  xfree(p);
60 }
61 
62 /* Destroys the credentials instances. */
66 }
67 
68 /* Protects the mark object from GC */
71  if (p == NULL) {
72  return;
73  }
75 
76  if (wrapper->mark != Qnil) {
77  rb_gc_mark(wrapper->mark);
78  }
79 }
80 
82  "grpc_xds_channel_credentials",
85  NULL,
86  NULL,
87 #ifdef RUBY_TYPED_FREE_IMMEDIATELY
88  RUBY_TYPED_FREE_IMMEDIATELY
89 #endif
90 };
91 
92 /* Allocates ChannelCredential instances.
93  Provides safe initial defaults for the instance fields. */
94 static VALUE grpc_rb_xds_channel_credentials_alloc(VALUE cls) {
98  wrapper->wrapped = NULL;
99  wrapper->mark = Qnil;
100  return TypedData_Wrap_Struct(cls, &grpc_rb_xds_channel_credentials_data_type,
101  wrapper);
102 }
103 
104 /* Creates a wrapping object for a given channel credentials. This should only
105  * be called with grpc_channel_credentials objects that are not already
106  * associated with any Ruby object. */
108  VALUE mark) {
110  if (c == NULL) {
111  return Qnil;
112  }
113  VALUE rb_wrapper =
115  TypedData_Get_Struct(rb_wrapper, grpc_rb_xds_channel_credentials,
117  wrapper->wrapped = c;
118  wrapper->mark = mark;
119  return rb_wrapper;
120 }
121 
122 /* The attribute used on the mark object to hold the fallback creds. */
124 
125 /*
126  call-seq:
127  fallback_creds: (ChannelCredentials) fallback credentials to create
128  XDS credentials
129  Initializes Credential instances. */
130 static VALUE grpc_rb_xds_channel_credentials_init(VALUE self,
131  VALUE fallback_creds) {
133  grpc_channel_credentials* grpc_fallback_creds =
135  grpc_channel_credentials* creds =
136  grpc_xds_credentials_create(grpc_fallback_creds);
137  if (creds == NULL) {
138  rb_raise(rb_eRuntimeError,
139  "the call to grpc_xds_credentials_create() failed, could not "
140  "create a credentials, , see "
141  "https://github.com/grpc/grpc/blob/master/TROUBLESHOOTING.md for "
142  "debugging tips");
143  return Qnil;
144  }
145 
146  TypedData_Get_Struct(self, grpc_rb_xds_channel_credentials,
148  wrapper->wrapped = creds;
149 
150  /* Add the input objects as hidden fields to preserve them. */
151  rb_ivar_set(self, id_fallback_creds, fallback_creds);
152 
153  return self;
154 }
155 
156 // TODO: de-duplicate this code with the similar method in
157 // rb_channel_credentials.c, after putting ChannelCredentials and
158 // XdsChannelCredentials under a common parent class
159 static VALUE grpc_rb_xds_channel_credentials_compose(int argc, VALUE* argv,
160  VALUE self) {
162  grpc_call_credentials* other;
163  grpc_channel_credentials* prev = NULL;
164  VALUE mark;
165  if (argc == 0) {
166  return self;
167  }
168  mark = rb_ary_new();
169  rb_ary_push(mark, self);
171  for (int i = 0; i < argc; i++) {
172  rb_ary_push(mark, argv[i]);
174  creds = grpc_composite_channel_credentials_create(creds, other, NULL);
175  if (prev != NULL) {
177  }
178  prev = creds;
179 
180  if (creds == NULL) {
181  rb_raise(rb_eRuntimeError,
182  "Failed to compose channel and call credentials");
183  }
184  }
185  return grpc_rb_xds_wrap_channel_credentials(creds, mark);
186 }
187 
189  grpc_rb_cXdsChannelCredentials = rb_define_class_under(
190  grpc_rb_mGrpcCore, "XdsChannelCredentials", rb_cObject);
191 
192  /* Allocates an object managed by the ruby runtime */
193  rb_define_alloc_func(grpc_rb_cXdsChannelCredentials,
195 
196  /* Provides a ruby constructor and support for dup/clone. */
197  rb_define_method(grpc_rb_cXdsChannelCredentials, "initialize",
199  rb_define_method(grpc_rb_cXdsChannelCredentials, "initialize_copy",
201  rb_define_method(grpc_rb_cXdsChannelCredentials, "compose",
203 
204  id_fallback_creds = rb_intern("__fallback_creds");
205 }
206 
207 /* Gets the wrapped grpc_channel_credentials from the ruby wrapper */
210  Check_TypedStruct(v, &grpc_rb_xds_channel_credentials_data_type);
211  TypedData_Get_Struct(v, grpc_rb_xds_channel_credentials,
213  return wrapper->wrapped;
214 }
215 
217  return rb_typeddata_is_kind_of(v, &grpc_rb_xds_channel_credentials_data_type);
218 }
log.h
grpc_rb_xds_channel_credentials_compose
static VALUE grpc_rb_xds_channel_credentials_compose(int argc, VALUE *argv, VALUE self)
Definition: rb_xds_channel_credentials.c:159
rb_grpc_imports.generated.h
grpc_rb_xds_channel_credentials_alloc
static VALUE grpc_rb_xds_channel_credentials_alloc(VALUE cls)
Definition: rb_xds_channel_credentials.c:94
grpc_rb_xds_channel_credentials
Definition: rb_xds_channel_credentials.c:42
GRPC_RB_MEMSIZE_UNAVAILABLE
#define GRPC_RB_MEMSIZE_UNAVAILABLE
Definition: rb_grpc.h:57
grpc_ruby_shutdown
void grpc_ruby_shutdown()
Definition: rb_grpc.c:296
string.h
grpc_call_credentials
Definition: src/core/lib/security/credentials/credentials.h:189
xds_manager.p
p
Definition: xds_manager.py:60
grpc_security.h
_grpc_channel_wrapper::wrapped
grpc_channel * wrapped
Definition: src/php/ext/grpc/channel.h:35
grpc_rb_xds_channel_credentials_data_type
static rb_data_type_t grpc_rb_xds_channel_credentials_data_type
Definition: rb_xds_channel_credentials.c:81
grpc_rb_xds_channel_credentials_init
static VALUE grpc_rb_xds_channel_credentials_init(VALUE self, VALUE fallback_creds)
Definition: rb_xds_channel_credentials.c:130
grpc_rb_get_wrapped_channel_credentials
grpc_channel_credentials * grpc_rb_get_wrapped_channel_credentials(VALUE v)
Definition: rb_channel_credentials.c:275
grpc_rb_xds_channel_credentials::mark
VALUE mark
Definition: rb_xds_channel_credentials.c:44
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
grpc_rb_xds_channel_credentials_free_internal
static void grpc_rb_xds_channel_credentials_free_internal(void *p)
Definition: rb_xds_channel_credentials.c:50
grpc_rb_get_wrapped_call_credentials
grpc_call_credentials * grpc_rb_get_wrapped_call_credentials(VALUE v)
Definition: rb_call_credentials.c:336
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
grpc.h
grpc_rb_xds_wrap_channel_credentials
VALUE grpc_rb_xds_wrap_channel_credentials(grpc_channel_credentials *c, VALUE mark)
Definition: rb_xds_channel_credentials.c:107
grpc_composite_channel_credentials_create
GRPCAPI grpc_channel_credentials * grpc_composite_channel_credentials_create(grpc_channel_credentials *channel_creds, grpc_call_credentials *call_creds, void *reserved)
Definition: composite_credentials.cc:164
wrapper
grpc_channel_wrapper * wrapper
Definition: src/php/ext/grpc/channel.h:48
rb_call_credentials.h
grpc_channel_credentials_release
GRPCAPI void grpc_channel_credentials_release(grpc_channel_credentials *creds)
Definition: credentials.cc:36
rb_xds_channel_credentials.h
rb_grpc.h
grpc_rb_get_wrapped_xds_channel_credentials
grpc_channel_credentials * grpc_rb_get_wrapped_xds_channel_credentials(VALUE v)
Definition: rb_xds_channel_credentials.c:208
grpc_rb_mGrpcCore
VALUE grpc_rb_mGrpcCore
Definition: rb_grpc.c:252
ALLOC
#define ALLOC(class_name)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1486
grpc_rb_cannot_init_copy
VALUE grpc_rb_cannot_init_copy(VALUE copy, VALUE self)
Definition: rb_grpc.c:80
grpc_rb_xds_channel_credentials::wrapped
grpc_channel_credentials * wrapped
Definition: rb_xds_channel_credentials.c:47
alloc.h
grpc_rb_xds_channel_credentials_mark
static void grpc_rb_xds_channel_credentials_mark(void *p)
Definition: rb_xds_channel_credentials.c:69
grpc_rb_cXdsChannelCredentials
static VALUE grpc_rb_cXdsChannelCredentials
Definition: rb_xds_channel_credentials.c:37
rb_channel_credentials.h
Init_grpc_xds_channel_credentials
void Init_grpc_xds_channel_credentials()
Definition: rb_xds_channel_credentials.c:188
grpc_rb_xds_channel_credentials
struct grpc_rb_xds_channel_credentials grpc_rb_xds_channel_credentials
grpc_xds_credentials_create
GRPCAPI grpc_channel_credentials * grpc_xds_credentials_create(grpc_channel_credentials *fallback_credentials)
Definition: core/lib/security/credentials/xds/xds_credentials.cc:243
grpc_rb_xds_channel_credentials_free
static void grpc_rb_xds_channel_credentials_free(void *p)
Definition: rb_xds_channel_credentials.c:63
id_fallback_creds
static ID id_fallback_creds
Definition: rb_xds_channel_credentials.c:123
grpc_channel_credentials
Definition: src/core/lib/security/credentials/credentials.h:96
grpc_ruby_init
void grpc_ruby_init()
Definition: rb_grpc.c:286
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_rb_is_xds_channel_credentials
bool grpc_rb_is_xds_channel_credentials(VALUE v)
Definition: rb_xds_channel_credentials.c:216


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