grpc 1.17.1 → 1.18.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 (166) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +1228 -988
  3. data/etc/roots.pem +242 -30
  4. data/include/grpc/grpc.h +2 -1
  5. data/include/grpc/grpc_security_constants.h +3 -3
  6. data/include/grpc/impl/codegen/atm_gcc_sync.h +2 -0
  7. data/include/grpc/impl/codegen/atm_windows.h +2 -0
  8. data/include/grpc/impl/codegen/compression_types.h +2 -1
  9. data/include/grpc/impl/codegen/grpc_types.h +1 -1
  10. data/include/grpc/impl/codegen/port_platform.h +9 -0
  11. data/src/core/ext/filters/client_channel/client_channel.cc +163 -882
  12. data/src/core/ext/filters/client_channel/health/health_check_client.cc +2 -4
  13. data/src/core/ext/filters/client_channel/health/health_check_client.h +2 -3
  14. data/src/core/ext/filters/client_channel/lb_policy.cc +1 -1
  15. data/src/core/ext/filters/client_channel/lb_policy.h +8 -17
  16. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +176 -216
  17. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel.h +1 -1
  18. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc +20 -23
  19. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +1 -1
  20. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +49 -52
  21. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +13 -35
  22. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +31 -30
  23. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +69 -225
  24. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_channel.h +1 -1
  25. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_channel_secure.cc +20 -23
  26. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h +1 -1
  27. data/src/core/ext/filters/client_channel/lb_policy_factory.h +2 -84
  28. data/src/core/ext/filters/client_channel/request_routing.cc +936 -0
  29. data/src/core/ext/filters/client_channel/request_routing.h +177 -0
  30. data/src/core/ext/filters/client_channel/resolver.cc +1 -1
  31. data/src/core/ext/filters/client_channel/resolver.h +1 -1
  32. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +37 -26
  33. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.cc +30 -18
  34. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +119 -100
  35. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +8 -5
  36. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +5 -4
  37. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_posix.cc +2 -1
  38. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_windows.cc +12 -14
  39. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +5 -9
  40. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc +2 -1
  41. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h +1 -2
  42. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.cc +17 -17
  43. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +45 -52
  44. data/src/core/ext/filters/client_channel/resolver_result_parsing.h +13 -17
  45. data/src/core/ext/filters/client_channel/server_address.cc +103 -0
  46. data/src/core/ext/filters/client_channel/server_address.h +108 -0
  47. data/src/core/ext/filters/client_channel/subchannel.cc +10 -8
  48. data/src/core/ext/filters/client_channel/subchannel.h +9 -6
  49. data/src/core/ext/filters/client_channel/subchannel_index.cc +20 -27
  50. data/src/core/ext/transport/chttp2/client/chttp2_connector.cc +3 -2
  51. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.cc +8 -9
  52. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +1 -1
  53. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +1 -1
  54. data/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.cc +8 -11
  55. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +24 -54
  56. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +3 -1
  57. data/src/core/ext/transport/chttp2/transport/context_list.cc +67 -0
  58. data/src/core/ext/transport/chttp2/transport/context_list.h +53 -0
  59. data/src/core/ext/transport/chttp2/transport/internal.h +38 -11
  60. data/src/core/ext/transport/chttp2/transport/writing.cc +5 -0
  61. data/src/core/ext/transport/inproc/inproc_transport.cc +1 -1
  62. data/src/core/lib/channel/channelz.cc +19 -18
  63. data/src/core/lib/channel/channelz.h +7 -1
  64. data/src/core/lib/channel/channelz_registry.cc +3 -2
  65. data/src/core/lib/debug/trace.cc +3 -0
  66. data/src/core/lib/debug/trace.h +5 -3
  67. data/src/core/lib/gpr/sync_posix.cc +96 -4
  68. data/src/core/lib/gprpp/inlined_vector.h +25 -19
  69. data/src/core/lib/gprpp/memory.h +2 -11
  70. data/src/core/lib/gprpp/orphanable.h +18 -82
  71. data/src/core/lib/gprpp/ref_counted.h +75 -84
  72. data/src/core/lib/gprpp/ref_counted_ptr.h +22 -17
  73. data/src/core/lib/http/httpcli_security_connector.cc +101 -94
  74. data/src/core/lib/http/parser.h +5 -5
  75. data/src/core/lib/iomgr/buffer_list.cc +16 -5
  76. data/src/core/lib/iomgr/buffer_list.h +10 -3
  77. data/src/core/lib/iomgr/call_combiner.cc +50 -2
  78. data/src/core/lib/iomgr/call_combiner.h +29 -2
  79. data/src/core/lib/iomgr/dynamic_annotations.h +67 -0
  80. data/src/core/lib/iomgr/endpoint.cc +4 -0
  81. data/src/core/lib/iomgr/endpoint.h +3 -0
  82. data/src/core/lib/iomgr/endpoint_pair_posix.cc +2 -2
  83. data/src/core/lib/iomgr/ev_epoll1_linux.cc +4 -0
  84. data/src/core/lib/iomgr/ev_epollex_linux.cc +4 -0
  85. data/src/core/lib/iomgr/ev_poll_posix.cc +4 -0
  86. data/src/core/lib/iomgr/ev_posix.cc +15 -7
  87. data/src/core/lib/iomgr/ev_posix.h +10 -0
  88. data/src/core/lib/iomgr/exec_ctx.cc +13 -0
  89. data/src/core/lib/iomgr/fork_posix.cc +1 -1
  90. data/src/core/lib/iomgr/internal_errqueue.cc +36 -3
  91. data/src/core/lib/iomgr/internal_errqueue.h +7 -1
  92. data/src/core/lib/iomgr/iomgr.cc +7 -0
  93. data/src/core/lib/iomgr/iomgr.h +4 -0
  94. data/src/core/lib/iomgr/iomgr_custom.cc +3 -1
  95. data/src/core/lib/iomgr/iomgr_internal.cc +4 -0
  96. data/src/core/lib/iomgr/iomgr_internal.h +4 -0
  97. data/src/core/lib/iomgr/iomgr_posix.cc +6 -1
  98. data/src/core/lib/iomgr/iomgr_windows.cc +4 -1
  99. data/src/core/lib/iomgr/port.h +1 -2
  100. data/src/core/lib/iomgr/resource_quota.cc +1 -0
  101. data/src/core/lib/iomgr/sockaddr_utils.cc +1 -0
  102. data/src/core/lib/iomgr/tcp_custom.cc +4 -1
  103. data/src/core/lib/iomgr/tcp_posix.cc +95 -35
  104. data/src/core/lib/iomgr/tcp_windows.cc +4 -1
  105. data/src/core/lib/iomgr/timer_manager.cc +6 -0
  106. data/src/core/lib/security/context/security_context.cc +75 -108
  107. data/src/core/lib/security/context/security_context.h +59 -35
  108. data/src/core/lib/security/credentials/alts/alts_credentials.cc +36 -48
  109. data/src/core/lib/security/credentials/alts/alts_credentials.h +37 -10
  110. data/src/core/lib/security/credentials/composite/composite_credentials.cc +97 -157
  111. data/src/core/lib/security/credentials/composite/composite_credentials.h +60 -24
  112. data/src/core/lib/security/credentials/credentials.cc +18 -142
  113. data/src/core/lib/security/credentials/credentials.h +119 -95
  114. data/src/core/lib/security/credentials/fake/fake_credentials.cc +46 -71
  115. data/src/core/lib/security/credentials/fake/fake_credentials.h +23 -5
  116. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +144 -51
  117. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +28 -5
  118. data/src/core/lib/security/credentials/iam/iam_credentials.cc +27 -35
  119. data/src/core/lib/security/credentials/iam/iam_credentials.h +18 -4
  120. data/src/core/lib/security/credentials/jwt/jwt_credentials.cc +60 -69
  121. data/src/core/lib/security/credentials/jwt/jwt_credentials.h +29 -10
  122. data/src/core/lib/security/credentials/jwt/jwt_verifier.cc +2 -0
  123. data/src/core/lib/security/credentials/local/local_credentials.cc +19 -32
  124. data/src/core/lib/security/credentials/local/local_credentials.h +32 -11
  125. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +130 -149
  126. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +74 -29
  127. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +59 -77
  128. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +40 -17
  129. data/src/core/lib/security/credentials/ssl/ssl_credentials.cc +66 -83
  130. data/src/core/lib/security/credentials/ssl/ssl_credentials.h +58 -15
  131. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +152 -177
  132. data/src/core/lib/security/security_connector/alts/alts_security_connector.h +12 -10
  133. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +210 -215
  134. data/src/core/lib/security/security_connector/fake/fake_security_connector.h +9 -6
  135. data/src/core/lib/security/security_connector/local/local_security_connector.cc +176 -169
  136. data/src/core/lib/security/security_connector/local/local_security_connector.h +10 -9
  137. data/src/core/lib/security/security_connector/security_connector.cc +41 -124
  138. data/src/core/lib/security/security_connector/security_connector.h +102 -105
  139. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +348 -370
  140. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.h +14 -12
  141. data/src/core/lib/security/security_connector/ssl_utils.cc +13 -9
  142. data/src/core/lib/security/security_connector/ssl_utils.h +3 -1
  143. data/src/core/lib/security/transport/client_auth_filter.cc +50 -50
  144. data/src/core/lib/security/transport/secure_endpoint.cc +7 -1
  145. data/src/core/lib/security/transport/security_handshaker.cc +82 -66
  146. data/src/core/lib/security/transport/server_auth_filter.cc +15 -13
  147. data/src/core/lib/surface/init.cc +1 -0
  148. data/src/core/lib/surface/server.cc +13 -11
  149. data/src/core/lib/surface/server.h +6 -6
  150. data/src/core/lib/surface/version.cc +2 -2
  151. data/src/core/lib/transport/metadata.cc +1 -0
  152. data/src/core/lib/transport/static_metadata.cc +228 -221
  153. data/src/core/lib/transport/static_metadata.h +75 -71
  154. data/src/core/lib/transport/transport.cc +2 -1
  155. data/src/core/lib/transport/transport.h +5 -1
  156. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +9 -2
  157. data/src/core/tsi/ssl_transport_security.cc +35 -24
  158. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +1 -1
  159. data/src/ruby/lib/grpc/generic/rpc_server.rb +61 -0
  160. data/src/ruby/lib/grpc/generic/service.rb +1 -1
  161. data/src/ruby/lib/grpc/version.rb +1 -1
  162. data/src/ruby/pb/grpc/health/checker.rb +2 -3
  163. data/src/ruby/spec/generic/rpc_server_spec.rb +22 -0
  164. data/src/ruby/spec/support/services.rb +1 -0
  165. metadata +37 -32
  166. data/src/core/ext/filters/client_channel/lb_policy_factory.cc +0 -163
@@ -33,49 +33,45 @@
33
33
 
34
34
  /* -- Fake transport security credentials. -- */
35
35
 
36
- static grpc_security_status fake_transport_security_create_security_connector(
37
- grpc_channel_credentials* c, grpc_call_credentials* call_creds,
38
- const char* target, const grpc_channel_args* args,
39
- grpc_channel_security_connector** sc, grpc_channel_args** new_args) {
40
- *sc =
41
- grpc_fake_channel_security_connector_create(c, call_creds, target, args);
42
- return GRPC_SECURITY_OK;
43
- }
44
-
45
- static grpc_security_status
46
- fake_transport_security_server_create_security_connector(
47
- grpc_server_credentials* c, grpc_server_security_connector** sc) {
48
- *sc = grpc_fake_server_security_connector_create(c);
49
- return GRPC_SECURITY_OK;
50
- }
36
+ namespace {
37
+ class grpc_fake_channel_credentials final : public grpc_channel_credentials {
38
+ public:
39
+ grpc_fake_channel_credentials()
40
+ : grpc_channel_credentials(
41
+ GRPC_CHANNEL_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY) {}
42
+ ~grpc_fake_channel_credentials() override = default;
43
+
44
+ grpc_core::RefCountedPtr<grpc_channel_security_connector>
45
+ create_security_connector(
46
+ grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
47
+ const char* target, const grpc_channel_args* args,
48
+ grpc_channel_args** new_args) override {
49
+ return grpc_fake_channel_security_connector_create(
50
+ this->Ref(), std::move(call_creds), target, args);
51
+ }
52
+ };
53
+
54
+ class grpc_fake_server_credentials final : public grpc_server_credentials {
55
+ public:
56
+ grpc_fake_server_credentials()
57
+ : grpc_server_credentials(
58
+ GRPC_CHANNEL_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY) {}
59
+ ~grpc_fake_server_credentials() override = default;
60
+
61
+ grpc_core::RefCountedPtr<grpc_server_security_connector>
62
+ create_security_connector() override {
63
+ return grpc_fake_server_security_connector_create(this->Ref());
64
+ }
65
+ };
66
+ } // namespace
51
67
 
52
- static grpc_channel_credentials_vtable
53
- fake_transport_security_credentials_vtable = {
54
- nullptr, fake_transport_security_create_security_connector, nullptr};
55
-
56
- static grpc_server_credentials_vtable
57
- fake_transport_security_server_credentials_vtable = {
58
- nullptr, fake_transport_security_server_create_security_connector};
59
-
60
- grpc_channel_credentials* grpc_fake_transport_security_credentials_create(
61
- void) {
62
- grpc_channel_credentials* c = static_cast<grpc_channel_credentials*>(
63
- gpr_zalloc(sizeof(grpc_channel_credentials)));
64
- c->type = GRPC_CHANNEL_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY;
65
- c->vtable = &fake_transport_security_credentials_vtable;
66
- gpr_ref_init(&c->refcount, 1);
67
- return c;
68
+ grpc_channel_credentials* grpc_fake_transport_security_credentials_create() {
69
+ return grpc_core::New<grpc_fake_channel_credentials>();
68
70
  }
69
71
 
70
- grpc_server_credentials* grpc_fake_transport_security_server_credentials_create(
71
- void) {
72
- grpc_server_credentials* c = static_cast<grpc_server_credentials*>(
73
- gpr_malloc(sizeof(grpc_server_credentials)));
74
- memset(c, 0, sizeof(grpc_server_credentials));
75
- c->type = GRPC_CHANNEL_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY;
76
- gpr_ref_init(&c->refcount, 1);
77
- c->vtable = &fake_transport_security_server_credentials_vtable;
78
- return c;
72
+ grpc_server_credentials*
73
+ grpc_fake_transport_security_server_credentials_create() {
74
+ return grpc_core::New<grpc_fake_server_credentials>();
79
75
  }
80
76
 
81
77
  grpc_arg grpc_fake_transport_expected_targets_arg(char* expected_targets) {
@@ -92,46 +88,25 @@ const char* grpc_fake_transport_get_expected_targets(
92
88
 
93
89
  /* -- Metadata-only test credentials. -- */
94
90
 
95
- static void md_only_test_destruct(grpc_call_credentials* creds) {
96
- grpc_md_only_test_credentials* c =
97
- reinterpret_cast<grpc_md_only_test_credentials*>(creds);
98
- GRPC_MDELEM_UNREF(c->md);
99
- }
100
-
101
- static bool md_only_test_get_request_metadata(
102
- grpc_call_credentials* creds, grpc_polling_entity* pollent,
103
- grpc_auth_metadata_context context, grpc_credentials_mdelem_array* md_array,
104
- grpc_closure* on_request_metadata, grpc_error** error) {
105
- grpc_md_only_test_credentials* c =
106
- reinterpret_cast<grpc_md_only_test_credentials*>(creds);
107
- grpc_credentials_mdelem_array_add(md_array, c->md);
108
- if (c->is_async) {
91
+ bool grpc_md_only_test_credentials::get_request_metadata(
92
+ grpc_polling_entity* pollent, grpc_auth_metadata_context context,
93
+ grpc_credentials_mdelem_array* md_array, grpc_closure* on_request_metadata,
94
+ grpc_error** error) {
95
+ grpc_credentials_mdelem_array_add(md_array, md_);
96
+ if (is_async_) {
109
97
  GRPC_CLOSURE_SCHED(on_request_metadata, GRPC_ERROR_NONE);
110
98
  return false;
111
99
  }
112
100
  return true;
113
101
  }
114
102
 
115
- static void md_only_test_cancel_get_request_metadata(
116
- grpc_call_credentials* c, grpc_credentials_mdelem_array* md_array,
117
- grpc_error* error) {
103
+ void grpc_md_only_test_credentials::cancel_get_request_metadata(
104
+ grpc_credentials_mdelem_array* md_array, grpc_error* error) {
118
105
  GRPC_ERROR_UNREF(error);
119
106
  }
120
107
 
121
- static grpc_call_credentials_vtable md_only_test_vtable = {
122
- md_only_test_destruct, md_only_test_get_request_metadata,
123
- md_only_test_cancel_get_request_metadata};
124
-
125
108
  grpc_call_credentials* grpc_md_only_test_credentials_create(
126
109
  const char* md_key, const char* md_value, bool is_async) {
127
- grpc_md_only_test_credentials* c =
128
- static_cast<grpc_md_only_test_credentials*>(
129
- gpr_zalloc(sizeof(grpc_md_only_test_credentials)));
130
- c->base.type = GRPC_CALL_CREDENTIALS_TYPE_OAUTH2;
131
- c->base.vtable = &md_only_test_vtable;
132
- gpr_ref_init(&c->base.refcount, 1);
133
- c->md = grpc_mdelem_from_slices(grpc_slice_from_copied_string(md_key),
134
- grpc_slice_from_copied_string(md_value));
135
- c->is_async = is_async;
136
- return &c->base;
110
+ return grpc_core::New<grpc_md_only_test_credentials>(md_key, md_value,
111
+ is_async);
137
112
  }
@@ -55,10 +55,28 @@ const char* grpc_fake_transport_get_expected_targets(
55
55
 
56
56
  /* -- Metadata-only Test credentials. -- */
57
57
 
58
- typedef struct {
59
- grpc_call_credentials base;
60
- grpc_mdelem md;
61
- bool is_async;
62
- } grpc_md_only_test_credentials;
58
+ class grpc_md_only_test_credentials : public grpc_call_credentials {
59
+ public:
60
+ grpc_md_only_test_credentials(const char* md_key, const char* md_value,
61
+ bool is_async)
62
+ : grpc_call_credentials(GRPC_CALL_CREDENTIALS_TYPE_OAUTH2),
63
+ md_(grpc_mdelem_from_slices(grpc_slice_from_copied_string(md_key),
64
+ grpc_slice_from_copied_string(md_value))),
65
+ is_async_(is_async) {}
66
+ ~grpc_md_only_test_credentials() override { GRPC_MDELEM_UNREF(md_); }
67
+
68
+ bool get_request_metadata(grpc_polling_entity* pollent,
69
+ grpc_auth_metadata_context context,
70
+ grpc_credentials_mdelem_array* md_array,
71
+ grpc_closure* on_request_metadata,
72
+ grpc_error** error) override;
73
+
74
+ void cancel_get_request_metadata(grpc_credentials_mdelem_array* md_array,
75
+ grpc_error* error) override;
76
+
77
+ private:
78
+ grpc_mdelem md_;
79
+ bool is_async_;
80
+ };
63
81
 
64
82
  #endif /* GRPC_CORE_LIB_SECURITY_CREDENTIALS_FAKE_FAKE_CREDENTIALS_H */
@@ -30,6 +30,7 @@
30
30
  #include "src/core/lib/channel/channel_args.h"
31
31
  #include "src/core/lib/gpr/env.h"
32
32
  #include "src/core/lib/gpr/string.h"
33
+ #include "src/core/lib/gprpp/ref_counted_ptr.h"
33
34
  #include "src/core/lib/http/httpcli.h"
34
35
  #include "src/core/lib/http/parser.h"
35
36
  #include "src/core/lib/iomgr/load_file.h"
@@ -49,9 +50,16 @@
49
50
 
50
51
  /* -- Default credentials. -- */
51
52
 
52
- static int g_compute_engine_detection_done = 0;
53
- static int g_need_compute_engine_creds = 0;
53
+ /* A sticky bit that will be set only if the result of metadata server detection
54
+ * is positive. We do not set the bit if the result is negative. Because it
55
+ * means the detection is done via network test that is unreliable and the
56
+ * unreliable result should not be referred by successive calls. */
57
+ static int g_metadata_server_available = 0;
58
+ static int g_is_on_gce = 0;
54
59
  static gpr_mu g_state_mu;
60
+ /* Protect a metadata_server_detector instance that can be modified by more than
61
+ * one gRPC threads */
62
+ static gpr_mu* g_polling_mu;
55
63
  static gpr_once g_once = GPR_ONCE_INIT;
56
64
  static grpc_core::internal::grpc_gce_tenancy_checker g_gce_tenancy_checker =
57
65
  grpc_alts_is_running_on_gcp;
@@ -63,22 +71,13 @@ typedef struct {
63
71
  int is_done;
64
72
  int success;
65
73
  grpc_http_response response;
66
- } compute_engine_detector;
67
-
68
- static void google_default_credentials_destruct(
69
- grpc_channel_credentials* creds) {
70
- grpc_google_default_channel_credentials* c =
71
- reinterpret_cast<grpc_google_default_channel_credentials*>(creds);
72
- grpc_channel_credentials_unref(c->alts_creds);
73
- grpc_channel_credentials_unref(c->ssl_creds);
74
- }
74
+ } metadata_server_detector;
75
75
 
76
- static grpc_security_status google_default_create_security_connector(
77
- grpc_channel_credentials* creds, grpc_call_credentials* call_creds,
76
+ grpc_core::RefCountedPtr<grpc_channel_security_connector>
77
+ grpc_google_default_channel_credentials::create_security_connector(
78
+ grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
78
79
  const char* target, const grpc_channel_args* args,
79
- grpc_channel_security_connector** sc, grpc_channel_args** new_args) {
80
- grpc_google_default_channel_credentials* c =
81
- reinterpret_cast<grpc_google_default_channel_credentials*>(creds);
80
+ grpc_channel_args** new_args) {
82
81
  bool is_grpclb_load_balancer = grpc_channel_arg_get_bool(
83
82
  grpc_channel_args_find(args, GRPC_ARG_ADDRESS_IS_GRPCLB_LOAD_BALANCER),
84
83
  false);
@@ -88,11 +87,17 @@ static grpc_security_status google_default_create_security_connector(
88
87
  false);
89
88
  bool use_alts =
90
89
  is_grpclb_load_balancer || is_backend_from_grpclb_load_balancer;
91
- grpc_security_status status = GRPC_SECURITY_ERROR;
92
- status = use_alts ? c->alts_creds->vtable->create_security_connector(
93
- c->alts_creds, call_creds, target, args, sc, new_args)
94
- : c->ssl_creds->vtable->create_security_connector(
95
- c->ssl_creds, call_creds, target, args, sc, new_args);
90
+ /* Return failure if ALTS is selected but not running on GCE. */
91
+ if (use_alts && !g_is_on_gce) {
92
+ gpr_log(GPR_ERROR, "ALTS is selected, but not running on GCE.");
93
+ return nullptr;
94
+ }
95
+
96
+ grpc_core::RefCountedPtr<grpc_channel_security_connector> sc =
97
+ use_alts ? alts_creds_->create_security_connector(call_creds, target,
98
+ args, new_args)
99
+ : ssl_creds_->create_security_connector(call_creds, target, args,
100
+ new_args);
96
101
  /* grpclb-specific channel args are removed from the channel args set
97
102
  * to ensure backends and fallback adresses will have the same set of channel
98
103
  * args. By doing that, it guarantees the connections to backends will not be
@@ -106,20 +111,103 @@ static grpc_security_status google_default_create_security_connector(
106
111
  *new_args = grpc_channel_args_copy_and_add_and_remove(
107
112
  args, args_to_remove, GPR_ARRAY_SIZE(args_to_remove), nullptr, 0);
108
113
  }
109
- return status;
114
+ return sc;
115
+ }
116
+
117
+ static void on_metadata_server_detection_http_response(void* user_data,
118
+ grpc_error* error) {
119
+ metadata_server_detector* detector =
120
+ static_cast<metadata_server_detector*>(user_data);
121
+ if (error == GRPC_ERROR_NONE && detector->response.status == 200 &&
122
+ detector->response.hdr_count > 0) {
123
+ /* Internet providers can return a generic response to all requests, so
124
+ it is necessary to check that metadata header is present also. */
125
+ size_t i;
126
+ for (i = 0; i < detector->response.hdr_count; i++) {
127
+ grpc_http_header* header = &detector->response.hdrs[i];
128
+ if (strcmp(header->key, "Metadata-Flavor") == 0 &&
129
+ strcmp(header->value, "Google") == 0) {
130
+ detector->success = 1;
131
+ break;
132
+ }
133
+ }
134
+ }
135
+ gpr_mu_lock(g_polling_mu);
136
+ detector->is_done = 1;
137
+ GRPC_LOG_IF_ERROR(
138
+ "Pollset kick",
139
+ grpc_pollset_kick(grpc_polling_entity_pollset(&detector->pollent),
140
+ nullptr));
141
+ gpr_mu_unlock(g_polling_mu);
110
142
  }
111
143
 
112
- static grpc_channel_credentials_vtable google_default_credentials_vtable = {
113
- google_default_credentials_destruct,
114
- google_default_create_security_connector, nullptr};
144
+ static void destroy_pollset(void* p, grpc_error* e) {
145
+ grpc_pollset_destroy(static_cast<grpc_pollset*>(p));
146
+ }
147
+
148
+ static int is_metadata_server_reachable() {
149
+ metadata_server_detector detector;
150
+ grpc_httpcli_request request;
151
+ grpc_httpcli_context context;
152
+ grpc_closure destroy_closure;
153
+ /* The http call is local. If it takes more than one sec, it is for sure not
154
+ on compute engine. */
155
+ grpc_millis max_detection_delay = GPR_MS_PER_SEC;
156
+ grpc_pollset* pollset =
157
+ static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
158
+ grpc_pollset_init(pollset, &g_polling_mu);
159
+ detector.pollent = grpc_polling_entity_create_from_pollset(pollset);
160
+ detector.is_done = 0;
161
+ detector.success = 0;
162
+ memset(&detector.response, 0, sizeof(detector.response));
163
+ memset(&request, 0, sizeof(grpc_httpcli_request));
164
+ request.host = (char*)GRPC_COMPUTE_ENGINE_DETECTION_HOST;
165
+ request.http.path = (char*)"/";
166
+ grpc_httpcli_context_init(&context);
167
+ grpc_resource_quota* resource_quota =
168
+ grpc_resource_quota_create("google_default_credentials");
169
+ grpc_httpcli_get(
170
+ &context, &detector.pollent, resource_quota, &request,
171
+ grpc_core::ExecCtx::Get()->Now() + max_detection_delay,
172
+ GRPC_CLOSURE_CREATE(on_metadata_server_detection_http_response, &detector,
173
+ grpc_schedule_on_exec_ctx),
174
+ &detector.response);
175
+ grpc_resource_quota_unref_internal(resource_quota);
176
+ grpc_core::ExecCtx::Get()->Flush();
177
+ /* Block until we get the response. This is not ideal but this should only be
178
+ called once for the lifetime of the process by the default credentials. */
179
+ gpr_mu_lock(g_polling_mu);
180
+ while (!detector.is_done) {
181
+ grpc_pollset_worker* worker = nullptr;
182
+ if (!GRPC_LOG_IF_ERROR(
183
+ "pollset_work",
184
+ grpc_pollset_work(grpc_polling_entity_pollset(&detector.pollent),
185
+ &worker, GRPC_MILLIS_INF_FUTURE))) {
186
+ detector.is_done = 1;
187
+ detector.success = 0;
188
+ }
189
+ }
190
+ gpr_mu_unlock(g_polling_mu);
191
+ grpc_httpcli_context_destroy(&context);
192
+ GRPC_CLOSURE_INIT(&destroy_closure, destroy_pollset,
193
+ grpc_polling_entity_pollset(&detector.pollent),
194
+ grpc_schedule_on_exec_ctx);
195
+ grpc_pollset_shutdown(grpc_polling_entity_pollset(&detector.pollent),
196
+ &destroy_closure);
197
+ g_polling_mu = nullptr;
198
+ grpc_core::ExecCtx::Get()->Flush();
199
+ gpr_free(grpc_polling_entity_pollset(&detector.pollent));
200
+ grpc_http_response_destroy(&detector.response);
201
+ return detector.success;
202
+ }
115
203
 
116
204
  /* Takes ownership of creds_path if not NULL. */
117
205
  static grpc_error* create_default_creds_from_path(
118
- char* creds_path, grpc_call_credentials** creds) {
206
+ char* creds_path, grpc_core::RefCountedPtr<grpc_call_credentials>* creds) {
119
207
  grpc_json* json = nullptr;
120
208
  grpc_auth_json_key key;
121
209
  grpc_auth_refresh_token token;
122
- grpc_call_credentials* result = nullptr;
210
+ grpc_core::RefCountedPtr<grpc_call_credentials> result;
123
211
  grpc_slice creds_data = grpc_empty_slice();
124
212
  grpc_error* error = GRPC_ERROR_NONE;
125
213
  if (creds_path == nullptr) {
@@ -176,13 +264,12 @@ end:
176
264
  return error;
177
265
  }
178
266
 
179
- grpc_channel_credentials* grpc_google_default_credentials_create(void) {
267
+ grpc_channel_credentials* grpc_google_default_credentials_create() {
180
268
  grpc_channel_credentials* result = nullptr;
181
- grpc_call_credentials* call_creds = nullptr;
269
+ grpc_core::RefCountedPtr<grpc_call_credentials> call_creds;
182
270
  grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
183
271
  "Failed to create Google credentials");
184
272
  grpc_error* err;
185
- int need_compute_engine_creds = 0;
186
273
  grpc_core::ExecCtx exec_ctx;
187
274
 
188
275
  GRPC_API_TRACE("grpc_google_default_credentials_create(void)", 0, ());
@@ -202,17 +289,23 @@ grpc_channel_credentials* grpc_google_default_credentials_create(void) {
202
289
  error = grpc_error_add_child(error, err);
203
290
 
204
291
  gpr_mu_lock(&g_state_mu);
205
- /* At last try to see if we're on compute engine (do the detection only once
206
- since it requires a network test). */
207
- if (!g_compute_engine_detection_done) {
208
- g_need_compute_engine_creds = g_gce_tenancy_checker();
209
- g_compute_engine_detection_done = 1;
292
+
293
+ /* Try a platform-provided hint for GCE. */
294
+ if (!g_metadata_server_available) {
295
+ g_is_on_gce = g_gce_tenancy_checker();
296
+ g_metadata_server_available = g_is_on_gce;
297
+ }
298
+ /* TODO: Add a platform-provided hint for GAE. */
299
+
300
+ /* Do a network test for metadata server. */
301
+ if (!g_metadata_server_available) {
302
+ g_metadata_server_available = is_metadata_server_reachable();
210
303
  }
211
- need_compute_engine_creds = g_need_compute_engine_creds;
212
304
  gpr_mu_unlock(&g_state_mu);
213
305
 
214
- if (need_compute_engine_creds) {
215
- call_creds = grpc_google_compute_engine_credentials_create(nullptr);
306
+ if (g_metadata_server_available) {
307
+ call_creds = grpc_core::RefCountedPtr<grpc_call_credentials>(
308
+ grpc_google_compute_engine_credentials_create(nullptr));
216
309
  if (call_creds == nullptr) {
217
310
  error = grpc_error_add_child(
218
311
  error, GRPC_ERROR_CREATE_FROM_STATIC_STRING(
@@ -223,23 +316,23 @@ grpc_channel_credentials* grpc_google_default_credentials_create(void) {
223
316
  end:
224
317
  if (call_creds != nullptr) {
225
318
  /* Create google default credentials. */
226
- auto creds = static_cast<grpc_google_default_channel_credentials*>(
227
- gpr_zalloc(sizeof(grpc_google_default_channel_credentials)));
228
- creds->base.vtable = &google_default_credentials_vtable;
229
- creds->base.type = GRPC_CHANNEL_CREDENTIALS_TYPE_GOOGLE_DEFAULT;
230
- gpr_ref_init(&creds->base.refcount, 1);
231
- creds->ssl_creds =
319
+ grpc_channel_credentials* ssl_creds =
232
320
  grpc_ssl_credentials_create(nullptr, nullptr, nullptr, nullptr);
233
- GPR_ASSERT(creds->ssl_creds != nullptr);
321
+ GPR_ASSERT(ssl_creds != nullptr);
234
322
  grpc_alts_credentials_options* options =
235
323
  grpc_alts_credentials_client_options_create();
236
- creds->alts_creds = grpc_alts_credentials_create(options);
324
+ grpc_channel_credentials* alts_creds =
325
+ grpc_alts_credentials_create(options);
237
326
  grpc_alts_credentials_options_destroy(options);
238
- result = grpc_composite_channel_credentials_create(&creds->base, call_creds,
239
- nullptr);
327
+ auto creds =
328
+ grpc_core::MakeRefCounted<grpc_google_default_channel_credentials>(
329
+ alts_creds != nullptr ? alts_creds->Ref() : nullptr,
330
+ ssl_creds != nullptr ? ssl_creds->Ref() : nullptr);
331
+ if (ssl_creds) ssl_creds->Unref();
332
+ if (alts_creds) alts_creds->Unref();
333
+ result = grpc_composite_channel_credentials_create(
334
+ creds.get(), call_creds.get(), nullptr);
240
335
  GPR_ASSERT(result != nullptr);
241
- grpc_channel_credentials_unref(&creds->base);
242
- grpc_call_credentials_unref(call_creds);
243
336
  } else {
244
337
  gpr_log(GPR_ERROR, "Could not create google default credentials: %s",
245
338
  grpc_error_string(error));
@@ -259,7 +352,7 @@ void grpc_flush_cached_google_default_credentials(void) {
259
352
  grpc_core::ExecCtx exec_ctx;
260
353
  gpr_once_init(&g_once, init_default_credentials);
261
354
  gpr_mu_lock(&g_state_mu);
262
- g_compute_engine_detection_done = 0;
355
+ g_metadata_server_available = 0;
263
356
  gpr_mu_unlock(&g_state_mu);
264
357
  }
265
358