grpc 1.32.0 → 1.33.0.pre1

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 (214) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +175 -376
  3. data/include/grpc/grpc.h +0 -5
  4. data/include/grpc/grpc_security.h +16 -0
  5. data/include/grpc/impl/codegen/grpc_types.h +0 -5
  6. data/src/core/ext/filters/client_channel/client_channel.cc +204 -170
  7. data/src/core/ext/filters/client_channel/config_selector.cc +0 -4
  8. data/src/core/ext/filters/client_channel/config_selector.h +34 -5
  9. data/src/core/ext/filters/client_channel/lb_policy.h +1 -1
  10. data/src/core/ext/filters/client_channel/lb_policy/address_filtering.cc +48 -35
  11. data/src/core/ext/filters/client_channel/lb_policy/address_filtering.h +7 -5
  12. data/src/core/ext/filters/client_channel/lb_policy/child_policy_handler.cc +3 -2
  13. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +106 -106
  14. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +2 -2
  15. data/src/core/ext/filters/client_channel/lb_policy/priority/priority.cc +3 -3
  16. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +3 -3
  17. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +9 -32
  18. data/src/core/ext/filters/client_channel/lb_policy/weighted_target/weighted_target.cc +3 -3
  19. data/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc +198 -126
  20. data/src/core/ext/filters/client_channel/lb_policy/xds/eds.cc +439 -249
  21. data/src/core/ext/filters/client_channel/lb_policy/xds/eds_drop.cc +571 -0
  22. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_manager.cc +727 -0
  23. data/src/core/ext/filters/client_channel/lb_policy_registry.cc +8 -1
  24. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +1 -1
  25. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +553 -358
  26. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h +28 -0
  27. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +8 -39
  28. data/src/core/ext/filters/client_channel/resolver_result_parsing.h +4 -2
  29. data/src/core/ext/filters/client_channel/resolving_lb_policy.cc +44 -43
  30. data/src/core/ext/filters/client_channel/resolving_lb_policy.h +5 -9
  31. data/src/core/ext/filters/client_channel/server_address.cc +80 -0
  32. data/src/core/ext/filters/client_channel/server_address.h +25 -36
  33. data/src/core/ext/filters/client_channel/service_config.cc +16 -13
  34. data/src/core/ext/filters/client_channel/service_config.h +7 -4
  35. data/src/core/ext/filters/client_channel/service_config_channel_arg_filter.cc +2 -2
  36. data/src/core/ext/filters/client_channel/service_config_parser.cc +8 -6
  37. data/src/core/ext/filters/client_channel/service_config_parser.h +8 -5
  38. data/src/core/ext/filters/client_channel/subchannel_interface.h +44 -0
  39. data/src/core/ext/filters/message_size/message_size_filter.cc +2 -1
  40. data/src/core/ext/filters/message_size/message_size_filter.h +2 -1
  41. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +17 -10
  42. data/src/core/ext/transport/chttp2/transport/flow_control.cc +10 -2
  43. data/src/core/ext/transport/chttp2/transport/flow_control.h +10 -0
  44. data/src/core/ext/transport/chttp2/transport/internal.h +5 -0
  45. data/src/core/ext/transport/chttp2/transport/parsing.cc +16 -2
  46. data/src/core/ext/upb-generated/envoy/config/accesslog/v3/accesslog.upb.c +29 -9
  47. data/src/core/ext/upb-generated/envoy/config/accesslog/v3/accesslog.upb.h +66 -0
  48. data/src/core/ext/upb-generated/envoy/config/cluster/v3/cluster.upb.c +123 -45
  49. data/src/core/ext/upb-generated/envoy/config/cluster/v3/cluster.upb.h +310 -53
  50. data/src/core/ext/upb-generated/envoy/config/core/v3/address.upb.c +17 -5
  51. data/src/core/ext/upb-generated/envoy/config/core/v3/address.upb.h +45 -0
  52. data/src/core/ext/upb-generated/envoy/config/core/v3/base.upb.c +1 -0
  53. data/src/core/ext/upb-generated/envoy/config/core/v3/config_source.upb.c +16 -9
  54. data/src/core/ext/upb-generated/envoy/config/core/v3/config_source.upb.h +38 -15
  55. data/src/core/ext/upb-generated/envoy/config/core/v3/extension.upb.c +53 -0
  56. data/src/core/ext/upb-generated/envoy/config/core/v3/extension.upb.h +133 -0
  57. data/src/core/ext/upb-generated/envoy/config/core/v3/grpc_service.upb.c +54 -8
  58. data/src/core/ext/upb-generated/envoy/config/core/v3/grpc_service.upb.h +123 -5
  59. data/src/core/ext/upb-generated/envoy/config/core/v3/protocol.upb.c +40 -16
  60. data/src/core/ext/upb-generated/envoy/config/core/v3/protocol.upb.h +114 -5
  61. data/src/core/ext/upb-generated/envoy/config/core/v3/substitution_format_string.upb.c +36 -0
  62. data/src/core/ext/upb-generated/envoy/config/core/v3/substitution_format_string.upb.h +85 -0
  63. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener.upb.c +36 -16
  64. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener.upb.h +86 -20
  65. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener_components.upb.c +23 -6
  66. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener_components.upb.h +54 -5
  67. data/src/core/ext/upb-generated/envoy/config/rbac/v3/rbac.upb.c +10 -6
  68. data/src/core/ext/upb-generated/envoy/config/rbac/v3/rbac.upb.h +28 -11
  69. data/src/core/ext/upb-generated/envoy/config/route/v3/route_components.upb.c +184 -57
  70. data/src/core/ext/upb-generated/envoy/config/route/v3/route_components.upb.h +504 -69
  71. data/src/core/ext/upb-generated/envoy/config/route/v3/scoped_route.upb.c +6 -5
  72. data/src/core/ext/upb-generated/envoy/config/route/v3/scoped_route.upb.h +11 -7
  73. data/src/core/ext/upb-generated/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upb.c +78 -26
  74. data/src/core/ext/upb-generated/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upb.h +236 -25
  75. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/common.upb.c +8 -9
  76. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/common.upb.h +19 -33
  77. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/secret.upb.c +7 -3
  78. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/secret.upb.h +16 -0
  79. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/tls.upb.c +65 -23
  80. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/tls.upb.h +229 -47
  81. data/src/core/ext/upb-generated/envoy/service/discovery/v3/discovery.upb.c +20 -10
  82. data/src/core/ext/upb-generated/envoy/service/discovery/v3/discovery.upb.h +67 -4
  83. data/src/core/ext/upb-generated/envoy/type/matcher/v3/string.upb.c +3 -2
  84. data/src/core/ext/upb-generated/envoy/type/matcher/v3/string.upb.h +6 -0
  85. data/src/core/ext/upb-generated/google/api/expr/v1alpha1/checked.upb.c +242 -0
  86. data/src/core/ext/upb-generated/google/api/expr/v1alpha1/checked.upb.h +753 -0
  87. data/src/core/ext/upb-generated/udpa/annotations/security.upb.c +31 -0
  88. data/src/core/ext/upb-generated/udpa/annotations/security.upb.h +57 -0
  89. data/src/core/ext/upb-generated/udpa/core/v1/authority.upb.c +28 -0
  90. data/src/core/ext/upb-generated/udpa/core/v1/authority.upb.h +53 -0
  91. data/src/core/ext/upb-generated/udpa/core/v1/collection_entry.upb.c +52 -0
  92. data/src/core/ext/upb-generated/udpa/core/v1/collection_entry.upb.h +129 -0
  93. data/src/core/ext/upb-generated/udpa/core/v1/context_params.upb.c +42 -0
  94. data/src/core/ext/upb-generated/udpa/core/v1/context_params.upb.h +77 -0
  95. data/src/core/ext/upb-generated/udpa/core/v1/resource.upb.c +36 -0
  96. data/src/core/ext/upb-generated/udpa/core/v1/resource.upb.h +85 -0
  97. data/src/core/ext/upb-generated/udpa/core/v1/resource_locator.upb.c +54 -0
  98. data/src/core/ext/upb-generated/udpa/core/v1/resource_locator.upb.h +160 -0
  99. data/src/core/ext/upb-generated/udpa/core/v1/resource_name.upb.c +36 -0
  100. data/src/core/ext/upb-generated/udpa/core/v1/resource_name.upb.h +84 -0
  101. data/src/core/ext/xds/certificate_provider_factory.h +59 -0
  102. data/src/core/ext/xds/certificate_provider_registry.cc +103 -0
  103. data/src/core/ext/xds/certificate_provider_registry.h +57 -0
  104. data/src/core/ext/xds/certificate_provider_store.h +50 -0
  105. data/src/core/ext/xds/google_mesh_ca_certificate_provider_factory.cc +377 -0
  106. data/src/core/ext/xds/google_mesh_ca_certificate_provider_factory.h +102 -0
  107. data/src/core/ext/xds/xds_api.cc +301 -93
  108. data/src/core/ext/xds/xds_api.h +129 -92
  109. data/src/core/ext/xds/xds_channel_args.h +6 -3
  110. data/src/core/ext/xds/xds_client.cc +498 -410
  111. data/src/core/ext/xds/xds_client.h +105 -51
  112. data/src/core/ext/xds/xds_client_stats.cc +18 -12
  113. data/src/core/ext/xds/xds_client_stats.h +33 -5
  114. data/src/core/lib/channel/channel_args.h +0 -1
  115. data/src/core/lib/channel/channelz.cc +10 -45
  116. data/src/core/lib/channel/channelz.h +11 -19
  117. data/src/core/lib/channel/channelz_registry.cc +12 -11
  118. data/src/core/lib/channel/channelz_registry.h +3 -0
  119. data/src/core/lib/gpr/time_precise.cc +2 -0
  120. data/src/core/lib/gpr/time_precise.h +6 -2
  121. data/src/core/lib/gprpp/dual_ref_counted.h +336 -0
  122. data/src/core/lib/gprpp/ref_counted.h +51 -22
  123. data/src/core/lib/gprpp/ref_counted_ptr.h +153 -0
  124. data/src/core/lib/iomgr/endpoint_cfstream.cc +9 -5
  125. data/src/core/lib/iomgr/exec_ctx.h +10 -8
  126. data/src/core/lib/json/json_util.cc +58 -0
  127. data/src/core/lib/json/json_util.h +37 -0
  128. data/src/core/lib/security/certificate_provider.h +60 -0
  129. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_distributor.cc +321 -0
  130. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_distributor.h +214 -0
  131. data/src/core/lib/security/credentials/xds/xds_credentials.cc +45 -0
  132. data/src/core/lib/security/credentials/xds/xds_credentials.h +51 -0
  133. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +6 -10
  134. data/src/core/lib/security/security_connector/ssl_utils.h +5 -0
  135. data/src/core/lib/surface/channel.cc +9 -31
  136. data/src/core/lib/surface/channel.h +6 -1
  137. data/src/core/lib/surface/init.cc +26 -9
  138. data/src/core/lib/surface/version.cc +2 -2
  139. data/src/core/lib/transport/bdp_estimator.h +2 -1
  140. data/src/core/lib/transport/connectivity_state.h +2 -2
  141. data/src/core/lib/transport/metadata.cc +11 -1
  142. data/src/core/plugin_registry/grpc_plugin_registry.cc +35 -20
  143. data/src/core/tsi/ssl_transport_security.cc +2 -2
  144. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +2 -2
  145. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +3 -3
  146. data/src/ruby/lib/grpc/version.rb +1 -1
  147. data/third_party/boringssl-with-bazel/err_data.c +465 -463
  148. data/third_party/boringssl-with-bazel/src/crypto/asn1/asn1_lib.c +0 -6
  149. data/third_party/boringssl-with-bazel/src/crypto/dsa/dsa.c +9 -43
  150. data/third_party/boringssl-with-bazel/src/crypto/dsa/dsa_asn1.c +55 -4
  151. data/third_party/boringssl-with-bazel/src/crypto/dsa/internal.h +34 -0
  152. data/third_party/boringssl-with-bazel/src/crypto/evp/evp.c +4 -0
  153. data/third_party/boringssl-with-bazel/src/crypto/evp/p_dsa_asn1.c +6 -2
  154. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/digest.c +2 -0
  155. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/internal.h +4 -0
  156. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa.c +30 -10
  157. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa_impl.c +10 -15
  158. data/third_party/boringssl-with-bazel/src/crypto/hpke/hpke.c +98 -11
  159. data/third_party/boringssl-with-bazel/src/crypto/hpke/internal.h +51 -6
  160. data/third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h +44 -2
  161. data/third_party/boringssl-with-bazel/src/crypto/trust_token/pmbtoken.c +221 -49
  162. data/third_party/boringssl-with-bazel/src/crypto/trust_token/trust_token.c +64 -20
  163. data/third_party/boringssl-with-bazel/src/crypto/x509/a_strex.c +3 -3
  164. data/third_party/boringssl-with-bazel/src/crypto/x509/algorithm.c +0 -8
  165. data/third_party/boringssl-with-bazel/src/crypto/x509/t_crl.c +3 -3
  166. data/third_party/boringssl-with-bazel/src/crypto/x509/t_x509.c +1 -1
  167. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_cmp.c +7 -2
  168. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_ext.c +21 -18
  169. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_obj.c +1 -1
  170. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_set.c +24 -3
  171. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_trs.c +3 -3
  172. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_txt.c +67 -67
  173. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_v3.c +3 -3
  174. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_vfy.c +29 -35
  175. data/third_party/boringssl-with-bazel/src/crypto/x509/x509cset.c +13 -2
  176. data/third_party/boringssl-with-bazel/src/crypto/x509/x509name.c +9 -8
  177. data/third_party/boringssl-with-bazel/src/crypto/x509/x_all.c +10 -10
  178. data/third_party/boringssl-with-bazel/src/crypto/x509/x_crl.c +2 -2
  179. data/third_party/boringssl-with-bazel/src/crypto/x509/x_name.c +28 -40
  180. data/third_party/boringssl-with-bazel/src/crypto/x509/x_x509.c +3 -1
  181. data/third_party/boringssl-with-bazel/src/crypto/x509v3/ext_dat.h +1 -4
  182. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_conf.c +7 -3
  183. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_genn.c +2 -2
  184. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_info.c +1 -1
  185. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_purp.c +55 -8
  186. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_skey.c +1 -1
  187. data/third_party/boringssl-with-bazel/src/include/openssl/asn1.h +0 -1
  188. data/third_party/boringssl-with-bazel/src/include/openssl/base.h +1 -1
  189. data/third_party/boringssl-with-bazel/src/include/openssl/cipher.h +6 -0
  190. data/third_party/boringssl-with-bazel/src/include/openssl/crypto.h +1 -1
  191. data/third_party/boringssl-with-bazel/src/include/openssl/dh.h +12 -0
  192. data/third_party/boringssl-with-bazel/src/include/openssl/digest.h +9 -0
  193. data/third_party/boringssl-with-bazel/src/include/openssl/evp.h +4 -1
  194. data/third_party/boringssl-with-bazel/src/include/openssl/ssl.h +9 -2
  195. data/third_party/boringssl-with-bazel/src/include/openssl/trust_token.h +26 -6
  196. data/third_party/boringssl-with-bazel/src/include/openssl/x509.h +188 -78
  197. data/third_party/boringssl-with-bazel/src/include/openssl/x509v3.h +52 -43
  198. data/third_party/boringssl-with-bazel/src/ssl/handshake.cc +18 -18
  199. data/third_party/boringssl-with-bazel/src/ssl/handshake_client.cc +2 -3
  200. data/third_party/boringssl-with-bazel/src/ssl/handshake_server.cc +1 -1
  201. data/third_party/boringssl-with-bazel/src/ssl/internal.h +9 -9
  202. data/third_party/boringssl-with-bazel/src/ssl/ssl_cipher.cc +8 -9
  203. data/third_party/boringssl-with-bazel/src/ssl/tls13_both.cc +1 -2
  204. data/third_party/boringssl-with-bazel/src/ssl/tls13_client.cc +4 -8
  205. data/third_party/boringssl-with-bazel/src/ssl/tls13_server.cc +2 -2
  206. metadata +72 -42
  207. data/src/core/ext/filters/client_channel/lb_policy/xds/lrs.cc +0 -537
  208. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_routing.cc +0 -1141
  209. data/src/core/ext/upb-generated/gogoproto/gogo.upb.c +0 -17
  210. data/src/core/ext/upb-generated/gogoproto/gogo.upb.h +0 -29
  211. data/src/core/ext/xds/xds_channel.h +0 -46
  212. data/src/core/ext/xds/xds_channel_secure.cc +0 -103
  213. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_pku.c +0 -110
  214. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_sxnet.c +0 -274
@@ -0,0 +1,321 @@
1
+ //
2
+ // Copyright 2020 gRPC authors.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ //
16
+
17
+ #include <grpc/support/port_platform.h>
18
+
19
+ #include "src/core/lib/security/credentials/tls/grpc_tls_certificate_distributor.h"
20
+
21
+ #include <grpc/support/alloc.h>
22
+ #include <grpc/support/log.h>
23
+ #include <grpc/support/string_util.h>
24
+ #include <stdlib.h>
25
+ #include <string.h>
26
+
27
+ void grpc_tls_certificate_distributor::SetKeyMaterials(
28
+ const std::string& cert_name, absl::optional<std::string> pem_root_certs,
29
+ absl::optional<PemKeyCertPairList> pem_key_cert_pairs) {
30
+ GPR_ASSERT(pem_root_certs.has_value() || pem_key_cert_pairs.has_value());
31
+ grpc_core::MutexLock lock(&mu_);
32
+ auto& cert_info = certificate_info_map_[cert_name];
33
+ if (pem_root_certs.has_value()) {
34
+ // Successful credential updates will clear any pre-existing error.
35
+ cert_info.SetRootError(GRPC_ERROR_NONE);
36
+ for (auto* watcher_ptr : cert_info.root_cert_watchers) {
37
+ GPR_ASSERT(watcher_ptr != nullptr);
38
+ const auto watcher_it = watchers_.find(watcher_ptr);
39
+ GPR_ASSERT(watcher_it != watchers_.end());
40
+ GPR_ASSERT(watcher_it->second.root_cert_name.has_value());
41
+ absl::optional<PemKeyCertPairList> pem_key_cert_pairs_to_report;
42
+ if (pem_key_cert_pairs.has_value() &&
43
+ watcher_it->second.identity_cert_name == cert_name) {
44
+ pem_key_cert_pairs_to_report = pem_key_cert_pairs;
45
+ } else if (watcher_it->second.identity_cert_name.has_value()) {
46
+ auto& identity_cert_info =
47
+ certificate_info_map_[*watcher_it->second.identity_cert_name];
48
+ pem_key_cert_pairs_to_report = identity_cert_info.pem_key_cert_pairs;
49
+ }
50
+ watcher_ptr->OnCertificatesChanged(
51
+ pem_root_certs, std::move(pem_key_cert_pairs_to_report));
52
+ }
53
+ cert_info.pem_root_certs = std::move(*pem_root_certs);
54
+ }
55
+ if (pem_key_cert_pairs.has_value()) {
56
+ // Successful credential updates will clear any pre-existing error.
57
+ cert_info.SetIdentityError(GRPC_ERROR_NONE);
58
+ for (const auto watcher_ptr : cert_info.identity_cert_watchers) {
59
+ GPR_ASSERT(watcher_ptr != nullptr);
60
+ const auto watcher_it = watchers_.find(watcher_ptr);
61
+ GPR_ASSERT(watcher_it != watchers_.end());
62
+ GPR_ASSERT(watcher_it->second.identity_cert_name.has_value());
63
+ absl::optional<absl::string_view> pem_root_certs_to_report;
64
+ if (pem_root_certs.has_value() &&
65
+ watcher_it->second.root_cert_name == cert_name) {
66
+ // In this case, We've already sent the credential updates at the time
67
+ // when checking pem_root_certs, so we will skip here.
68
+ continue;
69
+ } else if (watcher_it->second.root_cert_name.has_value()) {
70
+ auto& root_cert_info =
71
+ certificate_info_map_[*watcher_it->second.root_cert_name];
72
+ pem_root_certs_to_report = root_cert_info.pem_root_certs;
73
+ }
74
+ watcher_ptr->OnCertificatesChanged(pem_root_certs_to_report,
75
+ pem_key_cert_pairs);
76
+ }
77
+ cert_info.pem_key_cert_pairs = std::move(*pem_key_cert_pairs);
78
+ }
79
+ }
80
+
81
+ bool grpc_tls_certificate_distributor::HasRootCerts(
82
+ const std::string& root_cert_name) {
83
+ grpc_core::MutexLock lock(&mu_);
84
+ const auto it = certificate_info_map_.find(root_cert_name);
85
+ return it != certificate_info_map_.end() &&
86
+ !it->second.pem_root_certs.empty();
87
+ };
88
+
89
+ bool grpc_tls_certificate_distributor::HasKeyCertPairs(
90
+ const std::string& identity_cert_name) {
91
+ grpc_core::MutexLock lock(&mu_);
92
+ const auto it = certificate_info_map_.find(identity_cert_name);
93
+ return it != certificate_info_map_.end() &&
94
+ !it->second.pem_key_cert_pairs.empty();
95
+ };
96
+
97
+ void grpc_tls_certificate_distributor::SetErrorForCert(
98
+ const std::string& cert_name, absl::optional<grpc_error*> root_cert_error,
99
+ absl::optional<grpc_error*> identity_cert_error) {
100
+ GPR_ASSERT(root_cert_error.has_value() || identity_cert_error.has_value());
101
+ grpc_core::MutexLock lock(&mu_);
102
+ CertificateInfo& cert_info = certificate_info_map_[cert_name];
103
+ if (root_cert_error.has_value()) {
104
+ for (auto* watcher_ptr : cert_info.root_cert_watchers) {
105
+ GPR_ASSERT(watcher_ptr != nullptr);
106
+ const auto watcher_it = watchers_.find(watcher_ptr);
107
+ GPR_ASSERT(watcher_it != watchers_.end());
108
+ // identity_cert_error_to_report is the error of the identity cert this
109
+ // watcher is watching, if there is any.
110
+ grpc_error* identity_cert_error_to_report = GRPC_ERROR_NONE;
111
+ if (identity_cert_error.has_value() &&
112
+ watcher_it->second.identity_cert_name == cert_name) {
113
+ identity_cert_error_to_report = *identity_cert_error;
114
+ } else if (watcher_it->second.identity_cert_name.has_value()) {
115
+ auto& identity_cert_info =
116
+ certificate_info_map_[*watcher_it->second.identity_cert_name];
117
+ identity_cert_error_to_report = identity_cert_info.identity_cert_error;
118
+ }
119
+ watcher_ptr->OnError(GRPC_ERROR_REF(*root_cert_error),
120
+ GRPC_ERROR_REF(identity_cert_error_to_report));
121
+ }
122
+ cert_info.SetRootError(*root_cert_error);
123
+ }
124
+ if (identity_cert_error.has_value()) {
125
+ for (auto* watcher_ptr : cert_info.identity_cert_watchers) {
126
+ GPR_ASSERT(watcher_ptr != nullptr);
127
+ const auto watcher_it = watchers_.find(watcher_ptr);
128
+ GPR_ASSERT(watcher_it != watchers_.end());
129
+ // root_cert_error_to_report is the error of the root cert this watcher is
130
+ // watching, if there is any.
131
+ grpc_error* root_cert_error_to_report = GRPC_ERROR_NONE;
132
+ if (root_cert_error.has_value() &&
133
+ watcher_it->second.root_cert_name == cert_name) {
134
+ // In this case, We've already sent the error updates at the time when
135
+ // checking root_cert_error, so we will skip here.
136
+ continue;
137
+ } else if (watcher_it->second.root_cert_name.has_value()) {
138
+ auto& root_cert_info =
139
+ certificate_info_map_[*watcher_it->second.root_cert_name];
140
+ root_cert_error_to_report = root_cert_info.root_cert_error;
141
+ }
142
+ watcher_ptr->OnError(GRPC_ERROR_REF(root_cert_error_to_report),
143
+ GRPC_ERROR_REF(*identity_cert_error));
144
+ }
145
+ cert_info.SetIdentityError(*identity_cert_error);
146
+ }
147
+ };
148
+
149
+ void grpc_tls_certificate_distributor::SetError(grpc_error* error) {
150
+ GPR_ASSERT(error != GRPC_ERROR_NONE);
151
+ grpc_core::MutexLock lock(&mu_);
152
+ for (const auto& watcher : watchers_) {
153
+ const auto watcher_ptr = watcher.first;
154
+ GPR_ASSERT(watcher_ptr != nullptr);
155
+ const auto& watcher_info = watcher.second;
156
+ watcher_ptr->OnError(
157
+ watcher_info.root_cert_name.has_value() ? GRPC_ERROR_REF(error)
158
+ : GRPC_ERROR_NONE,
159
+ watcher_info.identity_cert_name.has_value() ? GRPC_ERROR_REF(error)
160
+ : GRPC_ERROR_NONE);
161
+ }
162
+ for (auto& cert_info_entry : certificate_info_map_) {
163
+ auto& cert_info = cert_info_entry.second;
164
+ cert_info.SetRootError(GRPC_ERROR_REF(error));
165
+ cert_info.SetIdentityError(GRPC_ERROR_REF(error));
166
+ }
167
+ GRPC_ERROR_UNREF(error);
168
+ };
169
+
170
+ void grpc_tls_certificate_distributor::WatchTlsCertificates(
171
+ std::unique_ptr<TlsCertificatesWatcherInterface> watcher,
172
+ absl::optional<std::string> root_cert_name,
173
+ absl::optional<std::string> identity_cert_name) {
174
+ bool start_watching_root_cert = false;
175
+ bool already_watching_identity_for_root_cert = false;
176
+ bool start_watching_identity_cert = false;
177
+ bool already_watching_root_for_identity_cert = false;
178
+ GPR_ASSERT(root_cert_name.has_value() || identity_cert_name.has_value());
179
+ TlsCertificatesWatcherInterface* watcher_ptr = watcher.get();
180
+ GPR_ASSERT(watcher_ptr != nullptr);
181
+ // Update watchers_ and certificate_info_map_.
182
+ {
183
+ grpc_core::MutexLock lock(&mu_);
184
+ const auto watcher_it = watchers_.find(watcher_ptr);
185
+ // The caller needs to cancel the watcher first if it wants to re-register
186
+ // the watcher.
187
+ GPR_ASSERT(watcher_it == watchers_.end());
188
+ watchers_[watcher_ptr] = {std::move(watcher), root_cert_name,
189
+ identity_cert_name};
190
+ absl::optional<absl::string_view> updated_root_certs;
191
+ absl::optional<PemKeyCertPairList> updated_identity_pairs;
192
+ grpc_error* root_error = GRPC_ERROR_NONE;
193
+ grpc_error* identity_error = GRPC_ERROR_NONE;
194
+ if (root_cert_name.has_value()) {
195
+ CertificateInfo& cert_info = certificate_info_map_[*root_cert_name];
196
+ start_watching_root_cert = cert_info.root_cert_watchers.empty();
197
+ already_watching_identity_for_root_cert =
198
+ !cert_info.identity_cert_watchers.empty();
199
+ cert_info.root_cert_watchers.insert(watcher_ptr);
200
+ root_error = GRPC_ERROR_REF(cert_info.root_cert_error);
201
+ // Empty credentials will be treated as no updates.
202
+ if (!cert_info.pem_root_certs.empty()) {
203
+ updated_root_certs = cert_info.pem_root_certs;
204
+ }
205
+ }
206
+ if (identity_cert_name.has_value()) {
207
+ CertificateInfo& cert_info = certificate_info_map_[*identity_cert_name];
208
+ start_watching_identity_cert = cert_info.identity_cert_watchers.empty();
209
+ already_watching_root_for_identity_cert =
210
+ !cert_info.root_cert_watchers.empty();
211
+ cert_info.identity_cert_watchers.insert(watcher_ptr);
212
+ identity_error = GRPC_ERROR_REF(cert_info.identity_cert_error);
213
+ // Empty credentials will be treated as no updates.
214
+ if (!cert_info.pem_key_cert_pairs.empty()) {
215
+ updated_identity_pairs = cert_info.pem_key_cert_pairs;
216
+ }
217
+ }
218
+ // Notify this watcher if the certs it is watching already had some
219
+ // contents. Note that an *_cert_error in cert_info only indicates error
220
+ // occurred while trying to fetch the latest cert, but the updated_*_certs
221
+ // should always be valid. So we will send the updates regardless of
222
+ // *_cert_error.
223
+ if (updated_root_certs.has_value() || updated_identity_pairs.has_value()) {
224
+ watcher_ptr->OnCertificatesChanged(updated_root_certs,
225
+ std::move(updated_identity_pairs));
226
+ }
227
+ // Notify this watcher if the certs it is watching already had some errors.
228
+ if (root_error != GRPC_ERROR_NONE || identity_error != GRPC_ERROR_NONE) {
229
+ watcher_ptr->OnError(GRPC_ERROR_REF(root_error),
230
+ GRPC_ERROR_REF(identity_error));
231
+ }
232
+ GRPC_ERROR_UNREF(root_error);
233
+ GRPC_ERROR_UNREF(identity_error);
234
+ }
235
+ // Invoke watch status callback if needed.
236
+ {
237
+ grpc_core::MutexLock lock(&callback_mu_);
238
+ if (watch_status_callback_ != nullptr) {
239
+ if (root_cert_name == identity_cert_name &&
240
+ (start_watching_root_cert || start_watching_identity_cert)) {
241
+ watch_status_callback_(*root_cert_name, start_watching_root_cert,
242
+ start_watching_identity_cert);
243
+ } else {
244
+ if (start_watching_root_cert) {
245
+ watch_status_callback_(*root_cert_name, true,
246
+ already_watching_identity_for_root_cert);
247
+ }
248
+ if (start_watching_identity_cert) {
249
+ watch_status_callback_(*identity_cert_name,
250
+ already_watching_root_for_identity_cert, true);
251
+ }
252
+ }
253
+ }
254
+ }
255
+ };
256
+
257
+ void grpc_tls_certificate_distributor::CancelTlsCertificatesWatch(
258
+ TlsCertificatesWatcherInterface* watcher) {
259
+ absl::optional<std::string> root_cert_name;
260
+ absl::optional<std::string> identity_cert_name;
261
+ bool stop_watching_root_cert = false;
262
+ bool already_watching_identity_for_root_cert = false;
263
+ bool stop_watching_identity_cert = false;
264
+ bool already_watching_root_for_identity_cert = false;
265
+ // Update watchers_ and certificate_info_map_.
266
+ {
267
+ grpc_core::MutexLock lock(&mu_);
268
+ auto it = watchers_.find(watcher);
269
+ if (it == watchers_.end()) return;
270
+ WatcherInfo& watcher_info = it->second;
271
+ root_cert_name = std::move(watcher_info.root_cert_name);
272
+ identity_cert_name = std::move(watcher_info.identity_cert_name);
273
+ watchers_.erase(it);
274
+ if (root_cert_name.has_value()) {
275
+ auto it = certificate_info_map_.find(*root_cert_name);
276
+ GPR_ASSERT(it != certificate_info_map_.end());
277
+ CertificateInfo& cert_info = it->second;
278
+ cert_info.root_cert_watchers.erase(watcher);
279
+ stop_watching_root_cert = cert_info.root_cert_watchers.empty();
280
+ already_watching_identity_for_root_cert =
281
+ !cert_info.identity_cert_watchers.empty();
282
+ if (stop_watching_root_cert && !already_watching_identity_for_root_cert) {
283
+ certificate_info_map_.erase(it);
284
+ }
285
+ }
286
+ if (identity_cert_name.has_value()) {
287
+ auto it = certificate_info_map_.find(*identity_cert_name);
288
+ GPR_ASSERT(it != certificate_info_map_.end());
289
+ CertificateInfo& cert_info = it->second;
290
+ cert_info.identity_cert_watchers.erase(watcher);
291
+ stop_watching_identity_cert = cert_info.identity_cert_watchers.empty();
292
+ already_watching_root_for_identity_cert =
293
+ !cert_info.root_cert_watchers.empty();
294
+ if (stop_watching_identity_cert &&
295
+ !already_watching_root_for_identity_cert) {
296
+ certificate_info_map_.erase(it);
297
+ }
298
+ }
299
+ }
300
+ // Invoke watch status callback if needed.
301
+ {
302
+ grpc_core::MutexLock lock(&callback_mu_);
303
+ if (watch_status_callback_ != nullptr) {
304
+ if (root_cert_name == identity_cert_name &&
305
+ (stop_watching_root_cert || stop_watching_identity_cert)) {
306
+ watch_status_callback_(*root_cert_name, !stop_watching_root_cert,
307
+ !stop_watching_identity_cert);
308
+ } else {
309
+ if (stop_watching_root_cert) {
310
+ watch_status_callback_(*root_cert_name, false,
311
+ already_watching_identity_for_root_cert);
312
+ }
313
+ if (stop_watching_identity_cert) {
314
+ watch_status_callback_(*identity_cert_name,
315
+ already_watching_root_for_identity_cert,
316
+ false);
317
+ }
318
+ }
319
+ }
320
+ }
321
+ };
@@ -0,0 +1,214 @@
1
+ //
2
+ // Copyright 2020 gRPC authors.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ //
16
+
17
+ #ifndef GRPC_CORE_LIB_SECURITY_CREDENTIALS_TLS_GRPC_TLS_CERTIFICATE_DISTRIBUTOR_H
18
+ #define GRPC_CORE_LIB_SECURITY_CREDENTIALS_TLS_GRPC_TLS_CERTIFICATE_DISTRIBUTOR_H
19
+
20
+ #include <grpc/support/port_platform.h>
21
+
22
+ #include <grpc/grpc_security.h>
23
+
24
+ #include "absl/container/inlined_vector.h"
25
+ #include "absl/types/optional.h"
26
+ #include "src/core/lib/gprpp/ref_counted.h"
27
+ #include "src/core/lib/security/security_connector/ssl_utils.h"
28
+
29
+ // TLS certificate distributor.
30
+ struct grpc_tls_certificate_distributor
31
+ : public grpc_core::RefCounted<grpc_tls_certificate_distributor> {
32
+ public:
33
+ typedef absl::InlinedVector<grpc_core::PemKeyCertPair, 1> PemKeyCertPairList;
34
+
35
+ // Interface for watching TLS certificates update.
36
+ class TlsCertificatesWatcherInterface {
37
+ public:
38
+ virtual ~TlsCertificatesWatcherInterface() = default;
39
+
40
+ // Handles the delivery of the updated root and identity certificates.
41
+ // An absl::nullopt value indicates no corresponding contents for
42
+ // root_certs or key_cert_pairs. Note that we will send updates of the
43
+ // latest contents for both root and identity certificates, even when only
44
+ // one side of it got updated.
45
+ //
46
+ // @param root_certs the contents of the reloaded root certs.
47
+ // @param key_cert_pairs the contents of the reloaded identity key-cert
48
+ // pairs.
49
+ virtual void OnCertificatesChanged(
50
+ absl::optional<absl::string_view> root_certs,
51
+ absl::optional<PemKeyCertPairList> key_cert_pairs) = 0;
52
+
53
+ // Handles an error that occurs while attempting to fetch certificate data.
54
+ // Note that if a watcher sees an error, it simply means the Provider is
55
+ // having problems renewing new data. If the watcher has previously received
56
+ // several OnCertificatesChanged, all the data received from that function
57
+ // is valid.
58
+ // In that case, watcher might simply log the error. If the watcher hasn't
59
+ // received any OnCertificatesChanged before the error occurs, no valid
60
+ // data is available yet, and the watcher should either fail or "waiting"
61
+ // for the valid data in a non-blocking way.
62
+ //
63
+ // @param root_cert_error the error occurred while reloading root
64
+ // certificates.
65
+ // @param identity_cert_error the error occurred while reloading identity
66
+ // certificates.
67
+ virtual void OnError(grpc_error* root_cert_error,
68
+ grpc_error* identity_cert_error) = 0;
69
+ };
70
+
71
+ // Sets the key materials based on their certificate name. Note that we are
72
+ // not doing any copies for pem_root_certs and pem_key_cert_pairs. For
73
+ // pem_root_certs, the original string contents need to outlive the
74
+ // distributor; for pem_key_cert_pairs, internally it is taking two
75
+ // unique_ptr(s) to the credential string, so the ownership is actually
76
+ // transferred.
77
+ //
78
+ // @param cert_name The name of the certificates being updated.
79
+ // @param pem_root_certs The content of root certificates.
80
+ // @param pem_key_cert_pairs The content of identity key-cert pairs.
81
+ void SetKeyMaterials(const std::string& cert_name,
82
+ absl::optional<std::string> pem_root_certs,
83
+ absl::optional<PemKeyCertPairList> pem_key_cert_pairs);
84
+
85
+ bool HasRootCerts(const std::string& root_cert_name);
86
+
87
+ bool HasKeyCertPairs(const std::string& identity_cert_name);
88
+
89
+ // Propagates the error that the caller (e.g. Producer) encounters to all the
90
+ // watchers watching a particular certificate name.
91
+ //
92
+ // @param cert_name The watching cert name of the watchers that the caller
93
+ // wants to notify when encountering error.
94
+ // @param root_cert_error The error that the caller encounters when reloading
95
+ // root certs.
96
+ // @param identity_cert_error The error that the caller encounters when
97
+ // reloading identity certs.
98
+ void SetErrorForCert(const std::string& cert_name,
99
+ absl::optional<grpc_error*> root_cert_error,
100
+ absl::optional<grpc_error*> identity_cert_error);
101
+
102
+ // Propagates the error that the caller (e.g. Producer) encounters to all
103
+ // watchers.
104
+ //
105
+ // @param error The error that the caller encounters.
106
+ void SetError(grpc_error* error);
107
+
108
+ // Sets the TLS certificate watch status callback function. The
109
+ // grpc_tls_certificate_distributor will invoke this callback when a new
110
+ // certificate name is watched by a newly registered watcher, or when a
111
+ // certificate name is no longer watched by any watchers.
112
+ // Note that when the callback shows a cert is no longer being watched, the
113
+ // distributor will delete the corresponding certificate data from its cache,
114
+ // and clear the corresponding error, if there is any. This means that if the
115
+ // callback subsequently says the same cert is now being watched again, the
116
+ // provider must re-provide the credentials or re-invoke the errors to the
117
+ // distributor, to indicate a successful or failed reloading.
118
+ // @param callback The callback function being set by the caller, e.g the
119
+ // Producer. Note that this callback will be invoked for each certificate
120
+ // name.
121
+ //
122
+ // For the parameters in the callback function:
123
+ // string_value The name of the certificates being watched.
124
+ // bool_value_1 If the root certificates with the specific name are being
125
+ // watched. bool_value_2 If the identity certificates with the specific name
126
+ // are being watched.
127
+ void SetWatchStatusCallback(
128
+ std::function<void(std::string, bool, bool)> callback) {
129
+ grpc_core::MutexLock lock(&mu_);
130
+ watch_status_callback_ = callback;
131
+ };
132
+
133
+ // Registers a watcher. The caller may keep a raw pointer to the watcher,
134
+ // which may be used only for cancellation. (Because the caller does not own
135
+ // the watcher, the pointer must not be used for any other purpose.) At least
136
+ // one of root_cert_name and identity_cert_name must be specified.
137
+ //
138
+ // @param watcher The watcher being registered.
139
+ // @param root_cert_name The name of the root certificates that will be
140
+ // watched. If set to absl::nullopt, the root certificates won't be watched.
141
+ // @param identity_cert_name The name of the identity certificates that will
142
+ // be watched. If set to absl::nullopt, the identity certificates won't be
143
+ // watched.
144
+ void WatchTlsCertificates(
145
+ std::unique_ptr<TlsCertificatesWatcherInterface> watcher,
146
+ absl::optional<std::string> root_cert_name,
147
+ absl::optional<std::string> identity_cert_name);
148
+
149
+ // Cancels a watcher.
150
+ //
151
+ // @param watcher The watcher being cancelled.
152
+ void CancelTlsCertificatesWatch(TlsCertificatesWatcherInterface* watcher);
153
+
154
+ private:
155
+ // Contains the information about each watcher.
156
+ struct WatcherInfo {
157
+ std::unique_ptr<TlsCertificatesWatcherInterface> watcher;
158
+ absl::optional<std::string> root_cert_name;
159
+ absl::optional<std::string> identity_cert_name;
160
+ };
161
+ // CertificateInfo contains the credential contents and some additional
162
+ // watcher information.
163
+ // Note that having errors doesn't indicate the corresponding credentials are
164
+ // invalid. For example, if root_cert_error != nullptr but pem_root_certs has
165
+ // value, it simply means an error occurs while trying to fetch the latest
166
+ // root certs, while pem_root_certs still contains the valid old data.
167
+ struct CertificateInfo {
168
+ // The contents of the root certificates.
169
+ std::string pem_root_certs;
170
+ // The contents of the identity key-certificate pairs.
171
+ PemKeyCertPairList pem_key_cert_pairs;
172
+ // The root cert reloading error propagated by the caller.
173
+ grpc_error* root_cert_error = GRPC_ERROR_NONE;
174
+ // The identity cert reloading error propagated by the caller.
175
+ grpc_error* identity_cert_error = GRPC_ERROR_NONE;
176
+ // The set of watchers watching root certificates.
177
+ // This is mainly used for quickly looking up the affected watchers while
178
+ // performing a credential reloading.
179
+ std::set<TlsCertificatesWatcherInterface*> root_cert_watchers;
180
+ // The set of watchers watching identity certificates. This is mainly used
181
+ // for quickly looking up the affected watchers while performing a
182
+ // credential reloading.
183
+ std::set<TlsCertificatesWatcherInterface*> identity_cert_watchers;
184
+
185
+ ~CertificateInfo() {
186
+ GRPC_ERROR_UNREF(root_cert_error);
187
+ GRPC_ERROR_UNREF(identity_cert_error);
188
+ }
189
+ void SetRootError(grpc_error* error) {
190
+ GRPC_ERROR_UNREF(root_cert_error);
191
+ root_cert_error = error;
192
+ }
193
+ void SetIdentityError(grpc_error* error) {
194
+ GRPC_ERROR_UNREF(identity_cert_error);
195
+ identity_cert_error = error;
196
+ }
197
+ };
198
+
199
+ grpc_core::Mutex mu_;
200
+ // We need a dedicated mutex for watch_status_callback_ for allowing
201
+ // callers(e.g. Producer) to directly set key materials in the callback
202
+ // functions.
203
+ grpc_core::Mutex callback_mu_;
204
+ // Stores information about each watcher.
205
+ std::map<TlsCertificatesWatcherInterface*, WatcherInfo> watchers_;
206
+ // The callback to notify the caller, e.g. the Producer, that the watch status
207
+ // is changed.
208
+ std::function<void(std::string, bool, bool)> watch_status_callback_;
209
+ // Stores the names of each certificate, and their corresponding credential
210
+ // contents as well as some additional watcher information.
211
+ std::map<std::string, CertificateInfo> certificate_info_map_;
212
+ };
213
+
214
+ #endif // GRPC_CORE_LIB_SECURITY_CREDENTIALS_TLS_GRPC_TLS_CERTIFICATE_DISTRIBUTOR_H