grpc 1.42.0.pre1-x86_64-darwin

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (128) hide show
  1. checksums.yaml +7 -0
  2. data/etc/roots.pem +4337 -0
  3. data/grpc_c.32.ruby +0 -0
  4. data/grpc_c.64.ruby +0 -0
  5. data/src/ruby/bin/math_client.rb +140 -0
  6. data/src/ruby/bin/math_pb.rb +34 -0
  7. data/src/ruby/bin/math_server.rb +191 -0
  8. data/src/ruby/bin/math_services_pb.rb +51 -0
  9. data/src/ruby/bin/noproto_client.rb +93 -0
  10. data/src/ruby/bin/noproto_server.rb +97 -0
  11. data/src/ruby/ext/grpc/ext-export.clang +1 -0
  12. data/src/ruby/ext/grpc/ext-export.gcc +6 -0
  13. data/src/ruby/ext/grpc/extconf.rb +123 -0
  14. data/src/ruby/ext/grpc/rb_byte_buffer.c +65 -0
  15. data/src/ruby/ext/grpc/rb_byte_buffer.h +35 -0
  16. data/src/ruby/ext/grpc/rb_call.c +1051 -0
  17. data/src/ruby/ext/grpc/rb_call.h +57 -0
  18. data/src/ruby/ext/grpc/rb_call_credentials.c +341 -0
  19. data/src/ruby/ext/grpc/rb_call_credentials.h +31 -0
  20. data/src/ruby/ext/grpc/rb_channel.c +846 -0
  21. data/src/ruby/ext/grpc/rb_channel.h +34 -0
  22. data/src/ruby/ext/grpc/rb_channel_args.c +155 -0
  23. data/src/ruby/ext/grpc/rb_channel_args.h +38 -0
  24. data/src/ruby/ext/grpc/rb_channel_credentials.c +286 -0
  25. data/src/ruby/ext/grpc/rb_channel_credentials.h +37 -0
  26. data/src/ruby/ext/grpc/rb_completion_queue.c +101 -0
  27. data/src/ruby/ext/grpc/rb_completion_queue.h +36 -0
  28. data/src/ruby/ext/grpc/rb_compression_options.c +471 -0
  29. data/src/ruby/ext/grpc/rb_compression_options.h +29 -0
  30. data/src/ruby/ext/grpc/rb_enable_cpp.cc +22 -0
  31. data/src/ruby/ext/grpc/rb_event_thread.c +145 -0
  32. data/src/ruby/ext/grpc/rb_event_thread.h +21 -0
  33. data/src/ruby/ext/grpc/rb_grpc.c +333 -0
  34. data/src/ruby/ext/grpc/rb_grpc.h +77 -0
  35. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +605 -0
  36. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +913 -0
  37. data/src/ruby/ext/grpc/rb_loader.c +57 -0
  38. data/src/ruby/ext/grpc/rb_loader.h +25 -0
  39. data/src/ruby/ext/grpc/rb_server.c +385 -0
  40. data/src/ruby/ext/grpc/rb_server.h +32 -0
  41. data/src/ruby/ext/grpc/rb_server_credentials.c +259 -0
  42. data/src/ruby/ext/grpc/rb_server_credentials.h +37 -0
  43. data/src/ruby/ext/grpc/rb_xds_channel_credentials.c +218 -0
  44. data/src/ruby/ext/grpc/rb_xds_channel_credentials.h +37 -0
  45. data/src/ruby/ext/grpc/rb_xds_server_credentials.c +170 -0
  46. data/src/ruby/ext/grpc/rb_xds_server_credentials.h +37 -0
  47. data/src/ruby/lib/grpc/2.4/grpc_c.bundle +0 -0
  48. data/src/ruby/lib/grpc/2.5/grpc_c.bundle +0 -0
  49. data/src/ruby/lib/grpc/2.6/grpc_c.bundle +0 -0
  50. data/src/ruby/lib/grpc/2.7/grpc_c.bundle +0 -0
  51. data/src/ruby/lib/grpc/3.0/grpc_c.bundle +0 -0
  52. data/src/ruby/lib/grpc/core/status_codes.rb +135 -0
  53. data/src/ruby/lib/grpc/core/time_consts.rb +56 -0
  54. data/src/ruby/lib/grpc/errors.rb +277 -0
  55. data/src/ruby/lib/grpc/generic/active_call.rb +669 -0
  56. data/src/ruby/lib/grpc/generic/bidi_call.rb +233 -0
  57. data/src/ruby/lib/grpc/generic/client_stub.rb +503 -0
  58. data/src/ruby/lib/grpc/generic/interceptor_registry.rb +53 -0
  59. data/src/ruby/lib/grpc/generic/interceptors.rb +186 -0
  60. data/src/ruby/lib/grpc/generic/rpc_desc.rb +204 -0
  61. data/src/ruby/lib/grpc/generic/rpc_server.rb +551 -0
  62. data/src/ruby/lib/grpc/generic/service.rb +211 -0
  63. data/src/ruby/lib/grpc/google_rpc_status_utils.rb +40 -0
  64. data/src/ruby/lib/grpc/grpc.rb +24 -0
  65. data/src/ruby/lib/grpc/logconfig.rb +44 -0
  66. data/src/ruby/lib/grpc/notifier.rb +45 -0
  67. data/src/ruby/lib/grpc/structs.rb +15 -0
  68. data/src/ruby/lib/grpc/version.rb +18 -0
  69. data/src/ruby/lib/grpc.rb +37 -0
  70. data/src/ruby/pb/README.md +42 -0
  71. data/src/ruby/pb/generate_proto_ruby.sh +51 -0
  72. data/src/ruby/pb/grpc/health/checker.rb +75 -0
  73. data/src/ruby/pb/grpc/health/v1/health_pb.rb +31 -0
  74. data/src/ruby/pb/grpc/health/v1/health_services_pb.rb +62 -0
  75. data/src/ruby/pb/grpc/testing/duplicate/echo_duplicate_services_pb.rb +44 -0
  76. data/src/ruby/pb/grpc/testing/metrics_pb.rb +28 -0
  77. data/src/ruby/pb/grpc/testing/metrics_services_pb.rb +49 -0
  78. data/src/ruby/pb/src/proto/grpc/testing/empty_pb.rb +17 -0
  79. data/src/ruby/pb/src/proto/grpc/testing/messages_pb.rb +145 -0
  80. data/src/ruby/pb/src/proto/grpc/testing/test_pb.rb +16 -0
  81. data/src/ruby/pb/src/proto/grpc/testing/test_services_pb.rb +152 -0
  82. data/src/ruby/pb/test/client.rb +769 -0
  83. data/src/ruby/pb/test/server.rb +252 -0
  84. data/src/ruby/pb/test/xds_client.rb +415 -0
  85. data/src/ruby/spec/call_credentials_spec.rb +42 -0
  86. data/src/ruby/spec/call_spec.rb +180 -0
  87. data/src/ruby/spec/channel_connection_spec.rb +126 -0
  88. data/src/ruby/spec/channel_credentials_spec.rb +124 -0
  89. data/src/ruby/spec/channel_spec.rb +245 -0
  90. data/src/ruby/spec/client_auth_spec.rb +152 -0
  91. data/src/ruby/spec/client_server_spec.rb +664 -0
  92. data/src/ruby/spec/compression_options_spec.rb +149 -0
  93. data/src/ruby/spec/debug_message_spec.rb +134 -0
  94. data/src/ruby/spec/error_sanity_spec.rb +49 -0
  95. data/src/ruby/spec/errors_spec.rb +142 -0
  96. data/src/ruby/spec/generic/active_call_spec.rb +683 -0
  97. data/src/ruby/spec/generic/client_interceptors_spec.rb +153 -0
  98. data/src/ruby/spec/generic/client_stub_spec.rb +1083 -0
  99. data/src/ruby/spec/generic/interceptor_registry_spec.rb +65 -0
  100. data/src/ruby/spec/generic/rpc_desc_spec.rb +374 -0
  101. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +127 -0
  102. data/src/ruby/spec/generic/rpc_server_spec.rb +748 -0
  103. data/src/ruby/spec/generic/server_interceptors_spec.rb +218 -0
  104. data/src/ruby/spec/generic/service_spec.rb +263 -0
  105. data/src/ruby/spec/google_rpc_status_utils_spec.rb +282 -0
  106. data/src/ruby/spec/pb/codegen/grpc/testing/package_options.proto +28 -0
  107. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_import.proto +22 -0
  108. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_import2.proto +23 -0
  109. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_ruby_style.proto +41 -0
  110. data/src/ruby/spec/pb/codegen/grpc/testing/same_package_service_name.proto +27 -0
  111. data/src/ruby/spec/pb/codegen/grpc/testing/same_ruby_package_service_name.proto +29 -0
  112. data/src/ruby/spec/pb/codegen/package_option_spec.rb +98 -0
  113. data/src/ruby/spec/pb/duplicate/codegen_spec.rb +57 -0
  114. data/src/ruby/spec/pb/health/checker_spec.rb +236 -0
  115. data/src/ruby/spec/server_credentials_spec.rb +104 -0
  116. data/src/ruby/spec/server_spec.rb +231 -0
  117. data/src/ruby/spec/spec_helper.rb +61 -0
  118. data/src/ruby/spec/support/helpers.rb +107 -0
  119. data/src/ruby/spec/support/services.rb +160 -0
  120. data/src/ruby/spec/testdata/README +1 -0
  121. data/src/ruby/spec/testdata/ca.pem +20 -0
  122. data/src/ruby/spec/testdata/client.key +28 -0
  123. data/src/ruby/spec/testdata/client.pem +20 -0
  124. data/src/ruby/spec/testdata/server1.key +28 -0
  125. data/src/ruby/spec/testdata/server1.pem +22 -0
  126. data/src/ruby/spec/time_consts_spec.rb +74 -0
  127. data/src/ruby/spec/user_agent_spec.rb +74 -0
  128. metadata +404 -0
@@ -0,0 +1,259 @@
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
+
19
+ #include <ruby/ruby.h>
20
+
21
+ #include "rb_server_credentials.h"
22
+
23
+ #include "rb_grpc.h"
24
+ #include "rb_grpc_imports.generated.h"
25
+
26
+ #include <grpc/grpc.h>
27
+ #include <grpc/grpc_security.h>
28
+ #include <grpc/support/log.h>
29
+
30
+ /* grpc_rb_cServerCredentials is the ruby class that proxies
31
+ grpc_server_credentials. */
32
+ static VALUE grpc_rb_cServerCredentials = Qnil;
33
+
34
+ /* grpc_rb_server_credentials wraps a grpc_server_credentials. It provides a
35
+ peer ruby object, 'mark' to hold references to objects involved in
36
+ constructing the server credentials. */
37
+ typedef struct grpc_rb_server_credentials {
38
+ /* Holder of ruby objects involved in constructing the server credentials */
39
+ VALUE mark;
40
+ /* The actual server credentials */
41
+ grpc_server_credentials* wrapped;
42
+ } grpc_rb_server_credentials;
43
+
44
+ /* Destroys the server credentials instances. */
45
+ static void grpc_rb_server_credentials_free_internal(void* p) {
46
+ grpc_rb_server_credentials* wrapper = NULL;
47
+ if (p == NULL) {
48
+ return;
49
+ };
50
+ wrapper = (grpc_rb_server_credentials*)p;
51
+
52
+ /* Delete the wrapped object if the mark object is Qnil, which indicates that
53
+ no other object is the actual owner. */
54
+ if (wrapper->wrapped != NULL && wrapper->mark == Qnil) {
55
+ grpc_server_credentials_release(wrapper->wrapped);
56
+ wrapper->wrapped = NULL;
57
+ }
58
+
59
+ xfree(p);
60
+ }
61
+
62
+ /* Destroys the server credentials instances. */
63
+ static void grpc_rb_server_credentials_free(void* p) {
64
+ grpc_rb_server_credentials_free_internal(p);
65
+ grpc_ruby_shutdown();
66
+ }
67
+
68
+ /* Protects the mark object from GC */
69
+ static void grpc_rb_server_credentials_mark(void* p) {
70
+ grpc_rb_server_credentials* wrapper = NULL;
71
+ if (p == NULL) {
72
+ return;
73
+ }
74
+ wrapper = (grpc_rb_server_credentials*)p;
75
+
76
+ /* If it's not already cleaned up, mark the mark object */
77
+ if (wrapper->mark != Qnil) {
78
+ rb_gc_mark(wrapper->mark);
79
+ }
80
+ }
81
+
82
+ static const rb_data_type_t grpc_rb_server_credentials_data_type = {
83
+ "grpc_server_credentials",
84
+ {grpc_rb_server_credentials_mark,
85
+ grpc_rb_server_credentials_free,
86
+ GRPC_RB_MEMSIZE_UNAVAILABLE,
87
+ {NULL, NULL}},
88
+ NULL,
89
+ NULL,
90
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
91
+ RUBY_TYPED_FREE_IMMEDIATELY
92
+ #endif
93
+ };
94
+
95
+ /* Allocates ServerCredential instances.
96
+ Provides safe initial defaults for the instance fields. */
97
+ static VALUE grpc_rb_server_credentials_alloc(VALUE cls) {
98
+ grpc_ruby_init();
99
+ grpc_rb_server_credentials* wrapper = ALLOC(grpc_rb_server_credentials);
100
+ wrapper->wrapped = NULL;
101
+ wrapper->mark = Qnil;
102
+ return TypedData_Wrap_Struct(cls, &grpc_rb_server_credentials_data_type,
103
+ wrapper);
104
+ }
105
+
106
+ /* The attribute used on the mark object to preserve the pem_root_certs. */
107
+ static ID id_pem_root_certs;
108
+
109
+ /* The attribute used on the mark object to preserve the pem_key_certs */
110
+ static ID id_pem_key_certs;
111
+
112
+ /* The key used to access the pem cert in a key_cert pair hash */
113
+ static VALUE sym_cert_chain;
114
+
115
+ /* The key used to access the pem private key in a key_cert pair hash */
116
+ static VALUE sym_private_key;
117
+
118
+ /*
119
+ call-seq:
120
+ creds = ServerCredentials.new(nil,
121
+ [{private_key: <pem_private_key1>,
122
+ {cert_chain: <pem_cert_chain1>}],
123
+ force_client_auth)
124
+ creds = ServerCredentials.new(pem_root_certs,
125
+ [{private_key: <pem_private_key1>,
126
+ {cert_chain: <pem_cert_chain1>}],
127
+ force_client_auth)
128
+
129
+ pem_root_certs: (optional) PEM encoding of the server root certificate
130
+ pem_private_key: (required) PEM encoding of the server's private keys
131
+ force_client_auth: indicatees
132
+
133
+ Initializes ServerCredential instances. */
134
+ static VALUE grpc_rb_server_credentials_init(VALUE self, VALUE pem_root_certs,
135
+ VALUE pem_key_certs,
136
+ VALUE force_client_auth) {
137
+ grpc_rb_server_credentials* wrapper = NULL;
138
+ grpc_server_credentials* creds = NULL;
139
+ grpc_ssl_pem_key_cert_pair* key_cert_pairs = NULL;
140
+ VALUE cert = Qnil;
141
+ VALUE key = Qnil;
142
+ VALUE key_cert = Qnil;
143
+ int auth_client = 0;
144
+ long num_key_certs = 0;
145
+ int i;
146
+
147
+ if (NIL_P(force_client_auth) ||
148
+ !(force_client_auth == Qfalse || force_client_auth == Qtrue)) {
149
+ rb_raise(rb_eTypeError,
150
+ "bad force_client_auth: got:<%s> want: <True|False|nil>",
151
+ rb_obj_classname(force_client_auth));
152
+ return Qnil;
153
+ }
154
+ if (NIL_P(pem_key_certs) || TYPE(pem_key_certs) != T_ARRAY) {
155
+ rb_raise(rb_eTypeError, "bad pem_key_certs: got:<%s> want: <Array>",
156
+ rb_obj_classname(pem_key_certs));
157
+ return Qnil;
158
+ }
159
+ num_key_certs = RARRAY_LEN(pem_key_certs);
160
+ if (num_key_certs == 0) {
161
+ rb_raise(rb_eTypeError, "bad pem_key_certs: it had no elements");
162
+ return Qnil;
163
+ }
164
+ for (i = 0; i < num_key_certs; i++) {
165
+ key_cert = rb_ary_entry(pem_key_certs, i);
166
+ if (key_cert == Qnil) {
167
+ rb_raise(rb_eTypeError,
168
+ "could not create a server credential: nil key_cert");
169
+ return Qnil;
170
+ } else if (TYPE(key_cert) != T_HASH) {
171
+ rb_raise(rb_eTypeError,
172
+ "could not create a server credential: want <Hash>, got <%s>",
173
+ rb_obj_classname(key_cert));
174
+ return Qnil;
175
+ } else if (rb_hash_aref(key_cert, sym_private_key) == Qnil) {
176
+ rb_raise(rb_eTypeError,
177
+ "could not create a server credential: want nil private key");
178
+ return Qnil;
179
+ } else if (rb_hash_aref(key_cert, sym_cert_chain) == Qnil) {
180
+ rb_raise(rb_eTypeError,
181
+ "could not create a server credential: want nil cert chain");
182
+ return Qnil;
183
+ }
184
+ }
185
+
186
+ auth_client = TYPE(force_client_auth) == T_TRUE
187
+ ? GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
188
+ : GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE;
189
+ key_cert_pairs = ALLOC_N(grpc_ssl_pem_key_cert_pair, num_key_certs);
190
+ for (i = 0; i < num_key_certs; i++) {
191
+ key_cert = rb_ary_entry(pem_key_certs, i);
192
+ key = rb_hash_aref(key_cert, sym_private_key);
193
+ cert = rb_hash_aref(key_cert, sym_cert_chain);
194
+ key_cert_pairs[i].private_key = RSTRING_PTR(key);
195
+ key_cert_pairs[i].cert_chain = RSTRING_PTR(cert);
196
+ }
197
+
198
+ TypedData_Get_Struct(self, grpc_rb_server_credentials,
199
+ &grpc_rb_server_credentials_data_type, wrapper);
200
+
201
+ if (pem_root_certs == Qnil) {
202
+ creds = grpc_ssl_server_credentials_create_ex(
203
+ NULL, key_cert_pairs, num_key_certs, auth_client, NULL);
204
+ } else {
205
+ creds = grpc_ssl_server_credentials_create_ex(RSTRING_PTR(pem_root_certs),
206
+ key_cert_pairs, num_key_certs,
207
+ auth_client, NULL);
208
+ }
209
+ xfree(key_cert_pairs);
210
+ if (creds == NULL) {
211
+ rb_raise(rb_eRuntimeError,
212
+ "the call to grpc_ssl_server_credentials_create_ex() failed, "
213
+ "could not create a credentials, see "
214
+ "https://github.com/grpc/grpc/blob/master/TROUBLESHOOTING.md for "
215
+ "debugging tips");
216
+ return Qnil;
217
+ }
218
+ wrapper->wrapped = creds;
219
+
220
+ /* Add the input objects as hidden fields to preserve them. */
221
+ rb_ivar_set(self, id_pem_key_certs, pem_key_certs);
222
+ rb_ivar_set(self, id_pem_root_certs, pem_root_certs);
223
+
224
+ return self;
225
+ }
226
+
227
+ void Init_grpc_server_credentials() {
228
+ grpc_rb_cServerCredentials =
229
+ rb_define_class_under(grpc_rb_mGrpcCore, "ServerCredentials", rb_cObject);
230
+
231
+ /* Allocates an object managed by the ruby runtime */
232
+ rb_define_alloc_func(grpc_rb_cServerCredentials,
233
+ grpc_rb_server_credentials_alloc);
234
+
235
+ /* Provides a ruby constructor and support for dup/clone. */
236
+ rb_define_method(grpc_rb_cServerCredentials, "initialize",
237
+ grpc_rb_server_credentials_init, 3);
238
+ rb_define_method(grpc_rb_cServerCredentials, "initialize_copy",
239
+ grpc_rb_cannot_init_copy, 1);
240
+
241
+ id_pem_key_certs = rb_intern("__pem_key_certs");
242
+ id_pem_root_certs = rb_intern("__pem_root_certs");
243
+ sym_private_key = ID2SYM(rb_intern("private_key"));
244
+ sym_cert_chain = ID2SYM(rb_intern("cert_chain"));
245
+ }
246
+
247
+ /* Gets the wrapped grpc_server_credentials from the ruby wrapper */
248
+ grpc_server_credentials* grpc_rb_get_wrapped_server_credentials(VALUE v) {
249
+ grpc_rb_server_credentials* wrapper = NULL;
250
+ Check_TypedStruct(v, &grpc_rb_server_credentials_data_type);
251
+ TypedData_Get_Struct(v, grpc_rb_server_credentials,
252
+ &grpc_rb_server_credentials_data_type, wrapper);
253
+ return wrapper->wrapped;
254
+ }
255
+
256
+ /* Check if v is kind of ServerCredentials */
257
+ bool grpc_rb_is_server_credentials(VALUE v) {
258
+ return rb_typeddata_is_kind_of(v, &grpc_rb_server_credentials_data_type);
259
+ }
@@ -0,0 +1,37 @@
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
+
19
+ #ifndef GRPC_RB_SERVER_CREDENTIALS_H_
20
+ #define GRPC_RB_SERVER_CREDENTIALS_H_
21
+
22
+ #include <ruby/ruby.h>
23
+
24
+ #include <stdbool.h>
25
+
26
+ #include <grpc/grpc_security.h>
27
+
28
+ /* Initializes the ruby ServerCredentials class. */
29
+ void Init_grpc_server_credentials();
30
+
31
+ /* Gets the wrapped server_credentials from the ruby wrapper */
32
+ grpc_server_credentials* grpc_rb_get_wrapped_server_credentials(VALUE v);
33
+
34
+ /* Check if v is kind of ServerCredentials */
35
+ bool grpc_rb_is_server_credentials(VALUE v);
36
+
37
+ #endif /* GRPC_RB_SERVER_CREDENTIALS_H_ */
@@ -0,0 +1,218 @@
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
+
21
+ #include "rb_xds_channel_credentials.h"
22
+
23
+ #include <string.h>
24
+
25
+ #include "rb_call_credentials.h"
26
+ #include "rb_channel_credentials.h"
27
+ #include "rb_grpc.h"
28
+ #include "rb_grpc_imports.generated.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. */
42
+ typedef struct grpc_rb_xds_channel_credentials {
43
+ /* Holder of ruby objects involved in constructing the credentials */
44
+ VALUE mark;
45
+
46
+ /* The actual credentials */
47
+ grpc_channel_credentials* wrapped;
48
+ } grpc_rb_xds_channel_credentials;
49
+
50
+ static void grpc_rb_xds_channel_credentials_free_internal(void* p) {
51
+ grpc_rb_xds_channel_credentials* wrapper = NULL;
52
+ if (p == NULL) {
53
+ return;
54
+ };
55
+ wrapper = (grpc_rb_xds_channel_credentials*)p;
56
+ grpc_channel_credentials_release(wrapper->wrapped);
57
+ wrapper->wrapped = NULL;
58
+
59
+ xfree(p);
60
+ }
61
+
62
+ /* Destroys the credentials instances. */
63
+ static void grpc_rb_xds_channel_credentials_free(void* p) {
64
+ grpc_rb_xds_channel_credentials_free_internal(p);
65
+ grpc_ruby_shutdown();
66
+ }
67
+
68
+ /* Protects the mark object from GC */
69
+ static void grpc_rb_xds_channel_credentials_mark(void* p) {
70
+ grpc_rb_xds_channel_credentials* wrapper = NULL;
71
+ if (p == NULL) {
72
+ return;
73
+ }
74
+ wrapper = (grpc_rb_xds_channel_credentials*)p;
75
+
76
+ if (wrapper->mark != Qnil) {
77
+ rb_gc_mark(wrapper->mark);
78
+ }
79
+ }
80
+
81
+ static rb_data_type_t grpc_rb_xds_channel_credentials_data_type = {
82
+ "grpc_xds_channel_credentials",
83
+ {grpc_rb_xds_channel_credentials_mark, grpc_rb_xds_channel_credentials_free,
84
+ GRPC_RB_MEMSIZE_UNAVAILABLE, NULL},
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) {
95
+ grpc_ruby_init();
96
+ grpc_rb_xds_channel_credentials* wrapper =
97
+ ALLOC(grpc_rb_xds_channel_credentials);
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. */
107
+ VALUE grpc_rb_xds_wrap_channel_credentials(grpc_channel_credentials* c,
108
+ VALUE mark) {
109
+ grpc_rb_xds_channel_credentials* wrapper;
110
+ if (c == NULL) {
111
+ return Qnil;
112
+ }
113
+ VALUE rb_wrapper =
114
+ grpc_rb_xds_channel_credentials_alloc(grpc_rb_cXdsChannelCredentials);
115
+ TypedData_Get_Struct(rb_wrapper, grpc_rb_xds_channel_credentials,
116
+ &grpc_rb_xds_channel_credentials_data_type, wrapper);
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. */
123
+ static ID id_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) {
132
+ grpc_rb_xds_channel_credentials* wrapper = NULL;
133
+ grpc_channel_credentials* grpc_fallback_creds =
134
+ grpc_rb_get_wrapped_channel_credentials(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,
147
+ &grpc_rb_xds_channel_credentials_data_type, wrapper);
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) {
161
+ grpc_channel_credentials* creds;
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);
170
+ creds = grpc_rb_get_wrapped_xds_channel_credentials(self);
171
+ for (int i = 0; i < argc; i++) {
172
+ rb_ary_push(mark, argv[i]);
173
+ other = grpc_rb_get_wrapped_call_credentials(argv[i]);
174
+ creds = grpc_composite_channel_credentials_create(creds, other, NULL);
175
+ if (prev != NULL) {
176
+ grpc_channel_credentials_release(prev);
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
+
188
+ void Init_grpc_xds_channel_credentials() {
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,
194
+ grpc_rb_xds_channel_credentials_alloc);
195
+
196
+ /* Provides a ruby constructor and support for dup/clone. */
197
+ rb_define_method(grpc_rb_cXdsChannelCredentials, "initialize",
198
+ grpc_rb_xds_channel_credentials_init, 1);
199
+ rb_define_method(grpc_rb_cXdsChannelCredentials, "initialize_copy",
200
+ grpc_rb_cannot_init_copy, 1);
201
+ rb_define_method(grpc_rb_cXdsChannelCredentials, "compose",
202
+ grpc_rb_xds_channel_credentials_compose, -1);
203
+
204
+ id_fallback_creds = rb_intern("__fallback_creds");
205
+ }
206
+
207
+ /* Gets the wrapped grpc_channel_credentials from the ruby wrapper */
208
+ grpc_channel_credentials* grpc_rb_get_wrapped_xds_channel_credentials(VALUE v) {
209
+ grpc_rb_xds_channel_credentials* wrapper = NULL;
210
+ Check_TypedStruct(v, &grpc_rb_xds_channel_credentials_data_type);
211
+ TypedData_Get_Struct(v, grpc_rb_xds_channel_credentials,
212
+ &grpc_rb_xds_channel_credentials_data_type, wrapper);
213
+ return wrapper->wrapped;
214
+ }
215
+
216
+ bool grpc_rb_is_xds_channel_credentials(VALUE v) {
217
+ return rb_typeddata_is_kind_of(v, &grpc_rb_xds_channel_credentials_data_type);
218
+ }
@@ -0,0 +1,37 @@
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
+ #ifndef GRPC_RB_XDS_CHANNEL_CREDENTIALS_H_
20
+ #define GRPC_RB_XDS_CHANNEL_CREDENTIALS_H_
21
+
22
+ #include <ruby/ruby.h>
23
+
24
+ #include <stdbool.h>
25
+
26
+ #include <grpc/grpc_security.h>
27
+
28
+ /* Initializes the ruby ChannelCredentials class. */
29
+ void Init_grpc_xds_channel_credentials();
30
+
31
+ /* Gets the wrapped credentials from the ruby wrapper */
32
+ grpc_channel_credentials* grpc_rb_get_wrapped_xds_channel_credentials(VALUE v);
33
+
34
+ /* Check if v is kind of XdsChannelCredentials */
35
+ bool grpc_rb_is_xds_channel_credentials(VALUE v);
36
+
37
+ #endif /* GRPC_RB_XDS_CHANNEL_CREDENTIALS_H_ */