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
@@ -25,16 +25,37 @@
25
25
 
26
26
  #include "src/core/lib/security/credentials/credentials.h"
27
27
 
28
- /* Main struct for grpc local channel credential. */
29
- typedef struct grpc_local_credentials {
30
- grpc_channel_credentials base;
31
- grpc_local_connect_type connect_type;
32
- } grpc_local_credentials;
33
-
34
- /* Main struct for grpc local server credential. */
35
- typedef struct grpc_local_server_credentials {
36
- grpc_server_credentials base;
37
- grpc_local_connect_type connect_type;
38
- } grpc_local_server_credentials;
28
+ /* Main class for grpc local channel credential. */
29
+ class grpc_local_credentials final : public grpc_channel_credentials {
30
+ public:
31
+ explicit grpc_local_credentials(grpc_local_connect_type connect_type);
32
+ ~grpc_local_credentials() override = default;
33
+
34
+ grpc_core::RefCountedPtr<grpc_channel_security_connector>
35
+ create_security_connector(
36
+ grpc_core::RefCountedPtr<grpc_call_credentials> request_metadata_creds,
37
+ const char* target_name, const grpc_channel_args* args,
38
+ grpc_channel_args** new_args) override;
39
+
40
+ grpc_local_connect_type connect_type() const { return connect_type_; }
41
+
42
+ private:
43
+ grpc_local_connect_type connect_type_;
44
+ };
45
+
46
+ /* Main class for grpc local server credential. */
47
+ class grpc_local_server_credentials final : public grpc_server_credentials {
48
+ public:
49
+ explicit grpc_local_server_credentials(grpc_local_connect_type connect_type);
50
+ ~grpc_local_server_credentials() override = default;
51
+
52
+ grpc_core::RefCountedPtr<grpc_server_security_connector>
53
+ create_security_connector() override;
54
+
55
+ grpc_local_connect_type connect_type() const { return connect_type_; }
56
+
57
+ private:
58
+ grpc_local_connect_type connect_type_;
59
+ };
39
60
 
40
61
  #endif /* GRPC_CORE_LIB_SECURITY_CREDENTIALS_LOCAL_LOCAL_CREDENTIALS_H */
@@ -22,6 +22,7 @@
22
22
 
23
23
  #include <string.h>
24
24
 
25
+ #include "src/core/lib/gprpp/ref_counted_ptr.h"
25
26
  #include "src/core/lib/security/util/json_util.h"
26
27
  #include "src/core/lib/surface/api_trace.h"
27
28
 
@@ -105,13 +106,12 @@ void grpc_auth_refresh_token_destruct(grpc_auth_refresh_token* refresh_token) {
105
106
  // Oauth2 Token Fetcher credentials.
106
107
  //
107
108
 
108
- static void oauth2_token_fetcher_destruct(grpc_call_credentials* creds) {
109
- grpc_oauth2_token_fetcher_credentials* c =
110
- reinterpret_cast<grpc_oauth2_token_fetcher_credentials*>(creds);
111
- GRPC_MDELEM_UNREF(c->access_token_md);
112
- gpr_mu_destroy(&c->mu);
113
- grpc_pollset_set_destroy(grpc_polling_entity_pollset_set(&c->pollent));
114
- grpc_httpcli_context_destroy(&c->httpcli_context);
109
+ grpc_oauth2_token_fetcher_credentials::
110
+ ~grpc_oauth2_token_fetcher_credentials() {
111
+ GRPC_MDELEM_UNREF(access_token_md_);
112
+ gpr_mu_destroy(&mu_);
113
+ grpc_pollset_set_destroy(grpc_polling_entity_pollset_set(&pollent_));
114
+ grpc_httpcli_context_destroy(&httpcli_context_);
115
115
  }
116
116
 
117
117
  grpc_credentials_status
@@ -209,25 +209,29 @@ static void on_oauth2_token_fetcher_http_response(void* user_data,
209
209
  grpc_credentials_metadata_request* r =
210
210
  static_cast<grpc_credentials_metadata_request*>(user_data);
211
211
  grpc_oauth2_token_fetcher_credentials* c =
212
- reinterpret_cast<grpc_oauth2_token_fetcher_credentials*>(r->creds);
212
+ reinterpret_cast<grpc_oauth2_token_fetcher_credentials*>(r->creds.get());
213
+ c->on_http_response(r, error);
214
+ }
215
+
216
+ void grpc_oauth2_token_fetcher_credentials::on_http_response(
217
+ grpc_credentials_metadata_request* r, grpc_error* error) {
213
218
  grpc_mdelem access_token_md = GRPC_MDNULL;
214
219
  grpc_millis token_lifetime;
215
220
  grpc_credentials_status status =
216
221
  grpc_oauth2_token_fetcher_credentials_parse_server_response(
217
222
  &r->response, &access_token_md, &token_lifetime);
218
223
  // Update cache and grab list of pending requests.
219
- gpr_mu_lock(&c->mu);
220
- c->token_fetch_pending = false;
221
- c->access_token_md = GRPC_MDELEM_REF(access_token_md);
222
- c->token_expiration =
224
+ gpr_mu_lock(&mu_);
225
+ token_fetch_pending_ = false;
226
+ access_token_md_ = GRPC_MDELEM_REF(access_token_md);
227
+ token_expiration_ =
223
228
  status == GRPC_CREDENTIALS_OK
224
229
  ? gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
225
230
  gpr_time_from_millis(token_lifetime, GPR_TIMESPAN))
226
231
  : gpr_inf_past(GPR_CLOCK_MONOTONIC);
227
- grpc_oauth2_pending_get_request_metadata* pending_request =
228
- c->pending_requests;
229
- c->pending_requests = nullptr;
230
- gpr_mu_unlock(&c->mu);
232
+ grpc_oauth2_pending_get_request_metadata* pending_request = pending_requests_;
233
+ pending_requests_ = nullptr;
234
+ gpr_mu_unlock(&mu_);
231
235
  // Invoke callbacks for all pending requests.
232
236
  while (pending_request != nullptr) {
233
237
  if (status == GRPC_CREDENTIALS_OK) {
@@ -239,42 +243,40 @@ static void on_oauth2_token_fetcher_http_response(void* user_data,
239
243
  }
240
244
  GRPC_CLOSURE_SCHED(pending_request->on_request_metadata, error);
241
245
  grpc_polling_entity_del_from_pollset_set(
242
- pending_request->pollent, grpc_polling_entity_pollset_set(&c->pollent));
246
+ pending_request->pollent, grpc_polling_entity_pollset_set(&pollent_));
243
247
  grpc_oauth2_pending_get_request_metadata* prev = pending_request;
244
248
  pending_request = pending_request->next;
245
249
  gpr_free(prev);
246
250
  }
247
251
  GRPC_MDELEM_UNREF(access_token_md);
248
- grpc_call_credentials_unref(r->creds);
252
+ Unref();
249
253
  grpc_credentials_metadata_request_destroy(r);
250
254
  }
251
255
 
252
- static bool oauth2_token_fetcher_get_request_metadata(
253
- grpc_call_credentials* creds, grpc_polling_entity* pollent,
254
- grpc_auth_metadata_context context, grpc_credentials_mdelem_array* md_array,
255
- grpc_closure* on_request_metadata, grpc_error** error) {
256
- grpc_oauth2_token_fetcher_credentials* c =
257
- reinterpret_cast<grpc_oauth2_token_fetcher_credentials*>(creds);
256
+ bool grpc_oauth2_token_fetcher_credentials::get_request_metadata(
257
+ grpc_polling_entity* pollent, grpc_auth_metadata_context context,
258
+ grpc_credentials_mdelem_array* md_array, grpc_closure* on_request_metadata,
259
+ grpc_error** error) {
258
260
  // Check if we can use the cached token.
259
261
  grpc_millis refresh_threshold =
260
262
  GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS * GPR_MS_PER_SEC;
261
263
  grpc_mdelem cached_access_token_md = GRPC_MDNULL;
262
- gpr_mu_lock(&c->mu);
263
- if (!GRPC_MDISNULL(c->access_token_md) &&
264
+ gpr_mu_lock(&mu_);
265
+ if (!GRPC_MDISNULL(access_token_md_) &&
264
266
  gpr_time_cmp(
265
- gpr_time_sub(c->token_expiration, gpr_now(GPR_CLOCK_MONOTONIC)),
267
+ gpr_time_sub(token_expiration_, gpr_now(GPR_CLOCK_MONOTONIC)),
266
268
  gpr_time_from_seconds(GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS,
267
269
  GPR_TIMESPAN)) > 0) {
268
- cached_access_token_md = GRPC_MDELEM_REF(c->access_token_md);
270
+ cached_access_token_md = GRPC_MDELEM_REF(access_token_md_);
269
271
  }
270
272
  if (!GRPC_MDISNULL(cached_access_token_md)) {
271
- gpr_mu_unlock(&c->mu);
273
+ gpr_mu_unlock(&mu_);
272
274
  grpc_credentials_mdelem_array_add(md_array, cached_access_token_md);
273
275
  GRPC_MDELEM_UNREF(cached_access_token_md);
274
276
  return true;
275
277
  }
276
278
  // Couldn't get the token from the cache.
277
- // Add request to c->pending_requests and start a new fetch if needed.
279
+ // Add request to pending_requests_ and start a new fetch if needed.
278
280
  grpc_oauth2_pending_get_request_metadata* pending_request =
279
281
  static_cast<grpc_oauth2_pending_get_request_metadata*>(
280
282
  gpr_malloc(sizeof(*pending_request)));
@@ -282,41 +284,37 @@ static bool oauth2_token_fetcher_get_request_metadata(
282
284
  pending_request->on_request_metadata = on_request_metadata;
283
285
  pending_request->pollent = pollent;
284
286
  grpc_polling_entity_add_to_pollset_set(
285
- pollent, grpc_polling_entity_pollset_set(&c->pollent));
286
- pending_request->next = c->pending_requests;
287
- c->pending_requests = pending_request;
287
+ pollent, grpc_polling_entity_pollset_set(&pollent_));
288
+ pending_request->next = pending_requests_;
289
+ pending_requests_ = pending_request;
288
290
  bool start_fetch = false;
289
- if (!c->token_fetch_pending) {
290
- c->token_fetch_pending = true;
291
+ if (!token_fetch_pending_) {
292
+ token_fetch_pending_ = true;
291
293
  start_fetch = true;
292
294
  }
293
- gpr_mu_unlock(&c->mu);
295
+ gpr_mu_unlock(&mu_);
294
296
  if (start_fetch) {
295
- grpc_call_credentials_ref(creds);
296
- c->fetch_func(grpc_credentials_metadata_request_create(creds),
297
- &c->httpcli_context, &c->pollent,
298
- on_oauth2_token_fetcher_http_response,
299
- grpc_core::ExecCtx::Get()->Now() + refresh_threshold);
297
+ Ref().release();
298
+ fetch_oauth2(grpc_credentials_metadata_request_create(this->Ref()),
299
+ &httpcli_context_, &pollent_,
300
+ on_oauth2_token_fetcher_http_response,
301
+ grpc_core::ExecCtx::Get()->Now() + refresh_threshold);
300
302
  }
301
303
  return false;
302
304
  }
303
305
 
304
- static void oauth2_token_fetcher_cancel_get_request_metadata(
305
- grpc_call_credentials* creds, grpc_credentials_mdelem_array* md_array,
306
- grpc_error* error) {
307
- grpc_oauth2_token_fetcher_credentials* c =
308
- reinterpret_cast<grpc_oauth2_token_fetcher_credentials*>(creds);
309
- gpr_mu_lock(&c->mu);
306
+ void grpc_oauth2_token_fetcher_credentials::cancel_get_request_metadata(
307
+ grpc_credentials_mdelem_array* md_array, grpc_error* error) {
308
+ gpr_mu_lock(&mu_);
310
309
  grpc_oauth2_pending_get_request_metadata* prev = nullptr;
311
- grpc_oauth2_pending_get_request_metadata* pending_request =
312
- c->pending_requests;
310
+ grpc_oauth2_pending_get_request_metadata* pending_request = pending_requests_;
313
311
  while (pending_request != nullptr) {
314
312
  if (pending_request->md_array == md_array) {
315
313
  // Remove matching pending request from the list.
316
314
  if (prev != nullptr) {
317
315
  prev->next = pending_request->next;
318
316
  } else {
319
- c->pending_requests = pending_request->next;
317
+ pending_requests_ = pending_request->next;
320
318
  }
321
319
  // Invoke the callback immediately with an error.
322
320
  GRPC_CLOSURE_SCHED(pending_request->on_request_metadata,
@@ -327,96 +325,89 @@ static void oauth2_token_fetcher_cancel_get_request_metadata(
327
325
  prev = pending_request;
328
326
  pending_request = pending_request->next;
329
327
  }
330
- gpr_mu_unlock(&c->mu);
328
+ gpr_mu_unlock(&mu_);
331
329
  GRPC_ERROR_UNREF(error);
332
330
  }
333
331
 
334
- static void init_oauth2_token_fetcher(grpc_oauth2_token_fetcher_credentials* c,
335
- grpc_fetch_oauth2_func fetch_func) {
336
- memset(c, 0, sizeof(grpc_oauth2_token_fetcher_credentials));
337
- c->base.type = GRPC_CALL_CREDENTIALS_TYPE_OAUTH2;
338
- gpr_ref_init(&c->base.refcount, 1);
339
- gpr_mu_init(&c->mu);
340
- c->token_expiration = gpr_inf_past(GPR_CLOCK_MONOTONIC);
341
- c->fetch_func = fetch_func;
342
- c->pollent =
343
- grpc_polling_entity_create_from_pollset_set(grpc_pollset_set_create());
344
- grpc_httpcli_context_init(&c->httpcli_context);
332
+ grpc_oauth2_token_fetcher_credentials::grpc_oauth2_token_fetcher_credentials()
333
+ : grpc_call_credentials(GRPC_CALL_CREDENTIALS_TYPE_OAUTH2),
334
+ token_expiration_(gpr_inf_past(GPR_CLOCK_MONOTONIC)),
335
+ pollent_(grpc_polling_entity_create_from_pollset_set(
336
+ grpc_pollset_set_create())) {
337
+ gpr_mu_init(&mu_);
338
+ grpc_httpcli_context_init(&httpcli_context_);
345
339
  }
346
340
 
347
341
  //
348
342
  // Google Compute Engine credentials.
349
343
  //
350
344
 
351
- static grpc_call_credentials_vtable compute_engine_vtable = {
352
- oauth2_token_fetcher_destruct, oauth2_token_fetcher_get_request_metadata,
353
- oauth2_token_fetcher_cancel_get_request_metadata};
345
+ namespace {
346
+
347
+ class grpc_compute_engine_token_fetcher_credentials
348
+ : public grpc_oauth2_token_fetcher_credentials {
349
+ public:
350
+ grpc_compute_engine_token_fetcher_credentials() = default;
351
+ ~grpc_compute_engine_token_fetcher_credentials() override = default;
352
+
353
+ protected:
354
+ void fetch_oauth2(grpc_credentials_metadata_request* metadata_req,
355
+ grpc_httpcli_context* http_context,
356
+ grpc_polling_entity* pollent,
357
+ grpc_iomgr_cb_func response_cb,
358
+ grpc_millis deadline) override {
359
+ grpc_http_header header = {(char*)"Metadata-Flavor", (char*)"Google"};
360
+ grpc_httpcli_request request;
361
+ memset(&request, 0, sizeof(grpc_httpcli_request));
362
+ request.host = (char*)GRPC_COMPUTE_ENGINE_METADATA_HOST;
363
+ request.http.path = (char*)GRPC_COMPUTE_ENGINE_METADATA_TOKEN_PATH;
364
+ request.http.hdr_count = 1;
365
+ request.http.hdrs = &header;
366
+ /* TODO(ctiller): Carry the resource_quota in ctx and share it with the host
367
+ channel. This would allow us to cancel an authentication query when under
368
+ extreme memory pressure. */
369
+ grpc_resource_quota* resource_quota =
370
+ grpc_resource_quota_create("oauth2_credentials");
371
+ grpc_httpcli_get(http_context, pollent, resource_quota, &request, deadline,
372
+ GRPC_CLOSURE_CREATE(response_cb, metadata_req,
373
+ grpc_schedule_on_exec_ctx),
374
+ &metadata_req->response);
375
+ grpc_resource_quota_unref_internal(resource_quota);
376
+ }
377
+ };
354
378
 
355
- static void compute_engine_fetch_oauth2(
356
- grpc_credentials_metadata_request* metadata_req,
357
- grpc_httpcli_context* httpcli_context, grpc_polling_entity* pollent,
358
- grpc_iomgr_cb_func response_cb, grpc_millis deadline) {
359
- grpc_http_header header = {(char*)"Metadata-Flavor", (char*)"Google"};
360
- grpc_httpcli_request request;
361
- memset(&request, 0, sizeof(grpc_httpcli_request));
362
- request.host = (char*)GRPC_COMPUTE_ENGINE_METADATA_HOST;
363
- request.http.path = (char*)GRPC_COMPUTE_ENGINE_METADATA_TOKEN_PATH;
364
- request.http.hdr_count = 1;
365
- request.http.hdrs = &header;
366
- /* TODO(ctiller): Carry the resource_quota in ctx and share it with the host
367
- channel. This would allow us to cancel an authentication query when under
368
- extreme memory pressure. */
369
- grpc_resource_quota* resource_quota =
370
- grpc_resource_quota_create("oauth2_credentials");
371
- grpc_httpcli_get(
372
- httpcli_context, pollent, resource_quota, &request, deadline,
373
- GRPC_CLOSURE_CREATE(response_cb, metadata_req, grpc_schedule_on_exec_ctx),
374
- &metadata_req->response);
375
- grpc_resource_quota_unref_internal(resource_quota);
376
- }
379
+ } // namespace
377
380
 
378
381
  grpc_call_credentials* grpc_google_compute_engine_credentials_create(
379
382
  void* reserved) {
380
- grpc_oauth2_token_fetcher_credentials* c =
381
- static_cast<grpc_oauth2_token_fetcher_credentials*>(
382
- gpr_malloc(sizeof(grpc_oauth2_token_fetcher_credentials)));
383
383
  GRPC_API_TRACE("grpc_compute_engine_credentials_create(reserved=%p)", 1,
384
384
  (reserved));
385
385
  GPR_ASSERT(reserved == nullptr);
386
- init_oauth2_token_fetcher(c, compute_engine_fetch_oauth2);
387
- c->base.vtable = &compute_engine_vtable;
388
- return &c->base;
386
+ return grpc_core::MakeRefCounted<
387
+ grpc_compute_engine_token_fetcher_credentials>()
388
+ .release();
389
389
  }
390
390
 
391
391
  //
392
392
  // Google Refresh Token credentials.
393
393
  //
394
394
 
395
- static void refresh_token_destruct(grpc_call_credentials* creds) {
396
- grpc_google_refresh_token_credentials* c =
397
- reinterpret_cast<grpc_google_refresh_token_credentials*>(creds);
398
- grpc_auth_refresh_token_destruct(&c->refresh_token);
399
- oauth2_token_fetcher_destruct(&c->base.base);
395
+ grpc_google_refresh_token_credentials::
396
+ ~grpc_google_refresh_token_credentials() {
397
+ grpc_auth_refresh_token_destruct(&refresh_token_);
400
398
  }
401
399
 
402
- static grpc_call_credentials_vtable refresh_token_vtable = {
403
- refresh_token_destruct, oauth2_token_fetcher_get_request_metadata,
404
- oauth2_token_fetcher_cancel_get_request_metadata};
405
-
406
- static void refresh_token_fetch_oauth2(
400
+ void grpc_google_refresh_token_credentials::fetch_oauth2(
407
401
  grpc_credentials_metadata_request* metadata_req,
408
402
  grpc_httpcli_context* httpcli_context, grpc_polling_entity* pollent,
409
403
  grpc_iomgr_cb_func response_cb, grpc_millis deadline) {
410
- grpc_google_refresh_token_credentials* c =
411
- reinterpret_cast<grpc_google_refresh_token_credentials*>(
412
- metadata_req->creds);
413
404
  grpc_http_header header = {(char*)"Content-Type",
414
405
  (char*)"application/x-www-form-urlencoded"};
415
406
  grpc_httpcli_request request;
416
407
  char* body = nullptr;
417
408
  gpr_asprintf(&body, GRPC_REFRESH_TOKEN_POST_BODY_FORMAT_STRING,
418
- c->refresh_token.client_id, c->refresh_token.client_secret,
419
- c->refresh_token.refresh_token);
409
+ refresh_token_.client_id, refresh_token_.client_secret,
410
+ refresh_token_.refresh_token);
420
411
  memset(&request, 0, sizeof(grpc_httpcli_request));
421
412
  request.host = (char*)GRPC_GOOGLE_OAUTH2_SERVICE_HOST;
422
413
  request.http.path = (char*)GRPC_GOOGLE_OAUTH2_SERVICE_TOKEN_PATH;
@@ -437,20 +428,19 @@ static void refresh_token_fetch_oauth2(
437
428
  gpr_free(body);
438
429
  }
439
430
 
440
- grpc_call_credentials*
431
+ grpc_google_refresh_token_credentials::grpc_google_refresh_token_credentials(
432
+ grpc_auth_refresh_token refresh_token)
433
+ : refresh_token_(refresh_token) {}
434
+
435
+ grpc_core::RefCountedPtr<grpc_call_credentials>
441
436
  grpc_refresh_token_credentials_create_from_auth_refresh_token(
442
437
  grpc_auth_refresh_token refresh_token) {
443
- grpc_google_refresh_token_credentials* c;
444
438
  if (!grpc_auth_refresh_token_is_valid(&refresh_token)) {
445
439
  gpr_log(GPR_ERROR, "Invalid input for refresh token credentials creation");
446
440
  return nullptr;
447
441
  }
448
- c = static_cast<grpc_google_refresh_token_credentials*>(
449
- gpr_zalloc(sizeof(grpc_google_refresh_token_credentials)));
450
- init_oauth2_token_fetcher(&c->base, refresh_token_fetch_oauth2);
451
- c->base.base.vtable = &refresh_token_vtable;
452
- c->refresh_token = refresh_token;
453
- return &c->base.base;
442
+ return grpc_core::MakeRefCounted<grpc_google_refresh_token_credentials>(
443
+ refresh_token);
454
444
  }
455
445
 
456
446
  static char* create_loggable_refresh_token(grpc_auth_refresh_token* token) {
@@ -478,59 +468,50 @@ grpc_call_credentials* grpc_google_refresh_token_credentials_create(
478
468
  gpr_free(loggable_token);
479
469
  }
480
470
  GPR_ASSERT(reserved == nullptr);
481
- return grpc_refresh_token_credentials_create_from_auth_refresh_token(token);
471
+ return grpc_refresh_token_credentials_create_from_auth_refresh_token(token)
472
+ .release();
482
473
  }
483
474
 
484
475
  //
485
476
  // Oauth2 Access Token credentials.
486
477
  //
487
478
 
488
- static void access_token_destruct(grpc_call_credentials* creds) {
489
- grpc_access_token_credentials* c =
490
- reinterpret_cast<grpc_access_token_credentials*>(creds);
491
- GRPC_MDELEM_UNREF(c->access_token_md);
479
+ grpc_access_token_credentials::~grpc_access_token_credentials() {
480
+ GRPC_MDELEM_UNREF(access_token_md_);
492
481
  }
493
482
 
494
- static bool access_token_get_request_metadata(
495
- grpc_call_credentials* creds, grpc_polling_entity* pollent,
496
- grpc_auth_metadata_context context, grpc_credentials_mdelem_array* md_array,
497
- grpc_closure* on_request_metadata, grpc_error** error) {
498
- grpc_access_token_credentials* c =
499
- reinterpret_cast<grpc_access_token_credentials*>(creds);
500
- grpc_credentials_mdelem_array_add(md_array, c->access_token_md);
483
+ bool grpc_access_token_credentials::get_request_metadata(
484
+ grpc_polling_entity* pollent, grpc_auth_metadata_context context,
485
+ grpc_credentials_mdelem_array* md_array, grpc_closure* on_request_metadata,
486
+ grpc_error** error) {
487
+ grpc_credentials_mdelem_array_add(md_array, access_token_md_);
501
488
  return true;
502
489
  }
503
490
 
504
- static void access_token_cancel_get_request_metadata(
505
- grpc_call_credentials* c, grpc_credentials_mdelem_array* md_array,
506
- grpc_error* error) {
491
+ void grpc_access_token_credentials::cancel_get_request_metadata(
492
+ grpc_credentials_mdelem_array* md_array, grpc_error* error) {
507
493
  GRPC_ERROR_UNREF(error);
508
494
  }
509
495
 
510
- static grpc_call_credentials_vtable access_token_vtable = {
511
- access_token_destruct, access_token_get_request_metadata,
512
- access_token_cancel_get_request_metadata};
496
+ grpc_access_token_credentials::grpc_access_token_credentials(
497
+ const char* access_token)
498
+ : grpc_call_credentials(GRPC_CALL_CREDENTIALS_TYPE_OAUTH2) {
499
+ char* token_md_value;
500
+ gpr_asprintf(&token_md_value, "Bearer %s", access_token);
501
+ grpc_core::ExecCtx exec_ctx;
502
+ access_token_md_ = grpc_mdelem_from_slices(
503
+ grpc_slice_from_static_string(GRPC_AUTHORIZATION_METADATA_KEY),
504
+ grpc_slice_from_copied_string(token_md_value));
505
+ gpr_free(token_md_value);
506
+ }
513
507
 
514
508
  grpc_call_credentials* grpc_access_token_credentials_create(
515
509
  const char* access_token, void* reserved) {
516
- grpc_access_token_credentials* c =
517
- static_cast<grpc_access_token_credentials*>(
518
- gpr_zalloc(sizeof(grpc_access_token_credentials)));
519
510
  GRPC_API_TRACE(
520
511
  "grpc_access_token_credentials_create(access_token=<redacted>, "
521
512
  "reserved=%p)",
522
513
  1, (reserved));
523
514
  GPR_ASSERT(reserved == nullptr);
524
- c->base.type = GRPC_CALL_CREDENTIALS_TYPE_OAUTH2;
525
- c->base.vtable = &access_token_vtable;
526
- gpr_ref_init(&c->base.refcount, 1);
527
- char* token_md_value;
528
- gpr_asprintf(&token_md_value, "Bearer %s", access_token);
529
- grpc_core::ExecCtx exec_ctx;
530
- c->access_token_md = grpc_mdelem_from_slices(
531
- grpc_slice_from_static_string(GRPC_AUTHORIZATION_METADATA_KEY),
532
- grpc_slice_from_copied_string(token_md_value));
533
-
534
- gpr_free(token_md_value);
535
- return &c->base;
515
+ return grpc_core::MakeRefCounted<grpc_access_token_credentials>(access_token)
516
+ .release();
536
517
  }