secure_channel_create.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <grpc/grpc.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <grpc/support/alloc.h>
  37. #include "src/core/census/grpc_filter.h"
  38. #include "src/core/channel/channel_args.h"
  39. #include "src/core/channel/client_channel.h"
  40. #include "src/core/channel/compress_filter.h"
  41. #include "src/core/channel/http_client_filter.h"
  42. #include "src/core/client_config/resolver_registry.h"
  43. #include "src/core/iomgr/tcp_client.h"
  44. #include "src/core/security/auth_filters.h"
  45. #include "src/core/security/credentials.h"
  46. #include "src/core/security/secure_transport_setup.h"
  47. #include "src/core/surface/channel.h"
  48. #include "src/core/transport/chttp2_transport.h"
  49. #include "src/core/tsi/transport_security_interface.h"
  50. typedef struct {
  51. grpc_connector base;
  52. gpr_refcount refs;
  53. grpc_channel_security_connector *security_connector;
  54. grpc_closure *notify;
  55. grpc_connect_in_args args;
  56. grpc_connect_out_args *result;
  57. gpr_mu mu;
  58. grpc_endpoint *connecting_endpoint;
  59. grpc_endpoint *newly_connecting_endpoint;
  60. grpc_closure connected_closure;
  61. grpc_mdctx *mdctx;
  62. grpc_workqueue *workqueue;
  63. } connector;
  64. static void connector_ref(grpc_connector *con) {
  65. connector *c = (connector *)con;
  66. gpr_ref(&c->refs);
  67. }
  68. static void connector_unref(grpc_connector *con, grpc_call_list *call_list) {
  69. connector *c = (connector *)con;
  70. if (gpr_unref(&c->refs)) {
  71. grpc_mdctx_unref(c->mdctx);
  72. GRPC_WORKQUEUE_UNREF(c->workqueue, "connector", call_list);
  73. gpr_free(c);
  74. }
  75. }
  76. static void on_secure_transport_setup_done(void *arg,
  77. grpc_security_status status,
  78. grpc_endpoint *wrapped_endpoint,
  79. grpc_endpoint *secure_endpoint,
  80. grpc_call_list *call_list) {
  81. connector *c = arg;
  82. grpc_closure *notify;
  83. gpr_mu_lock(&c->mu);
  84. if (c->connecting_endpoint == NULL) {
  85. memset(c->result, 0, sizeof(*c->result));
  86. gpr_mu_unlock(&c->mu);
  87. } else if (status != GRPC_SECURITY_OK) {
  88. GPR_ASSERT(c->connecting_endpoint == wrapped_endpoint);
  89. gpr_log(GPR_ERROR, "Secure transport setup failed with error %d.", status);
  90. memset(c->result, 0, sizeof(*c->result));
  91. c->connecting_endpoint = NULL;
  92. gpr_mu_unlock(&c->mu);
  93. } else {
  94. GPR_ASSERT(c->connecting_endpoint == wrapped_endpoint);
  95. c->connecting_endpoint = NULL;
  96. gpr_mu_unlock(&c->mu);
  97. c->result->transport = grpc_create_chttp2_transport(
  98. c->args.channel_args, secure_endpoint, c->mdctx, 1, call_list);
  99. grpc_chttp2_transport_start_reading(c->result->transport, NULL, 0,
  100. call_list);
  101. c->result->filters = gpr_malloc(sizeof(grpc_channel_filter *) * 2);
  102. c->result->filters[0] = &grpc_http_client_filter;
  103. c->result->filters[1] = &grpc_client_auth_filter;
  104. c->result->num_filters = 2;
  105. }
  106. notify = c->notify;
  107. c->notify = NULL;
  108. notify->cb(notify->cb_arg, 1, call_list);
  109. }
  110. static void connected(void *arg, int success, grpc_call_list *call_list) {
  111. connector *c = arg;
  112. grpc_closure *notify;
  113. grpc_endpoint *tcp = c->newly_connecting_endpoint;
  114. if (tcp != NULL) {
  115. gpr_mu_lock(&c->mu);
  116. GPR_ASSERT(c->connecting_endpoint == NULL);
  117. c->connecting_endpoint = tcp;
  118. gpr_mu_unlock(&c->mu);
  119. grpc_setup_secure_transport(&c->security_connector->base, tcp,
  120. on_secure_transport_setup_done, c, call_list);
  121. } else {
  122. memset(c->result, 0, sizeof(*c->result));
  123. notify = c->notify;
  124. c->notify = NULL;
  125. notify->cb(notify->cb_arg, 1, call_list);
  126. }
  127. }
  128. static void connector_shutdown(grpc_connector *con, grpc_call_list *call_list) {
  129. connector *c = (connector *)con;
  130. grpc_endpoint *ep;
  131. gpr_mu_lock(&c->mu);
  132. ep = c->connecting_endpoint;
  133. c->connecting_endpoint = NULL;
  134. gpr_mu_unlock(&c->mu);
  135. if (ep) {
  136. grpc_endpoint_shutdown(ep, call_list);
  137. }
  138. }
  139. static void connector_connect(grpc_connector *con,
  140. const grpc_connect_in_args *args,
  141. grpc_connect_out_args *result,
  142. grpc_closure *notify, grpc_call_list *call_list) {
  143. connector *c = (connector *)con;
  144. GPR_ASSERT(c->notify == NULL);
  145. GPR_ASSERT(notify->cb);
  146. c->notify = notify;
  147. c->args = *args;
  148. c->result = result;
  149. gpr_mu_lock(&c->mu);
  150. GPR_ASSERT(c->connecting_endpoint == NULL);
  151. gpr_mu_unlock(&c->mu);
  152. grpc_closure_init(&c->connected_closure, connected, c);
  153. grpc_tcp_client_connect(&c->connected_closure, &c->newly_connecting_endpoint,
  154. args->interested_parties, args->addr, args->addr_len,
  155. args->deadline, call_list);
  156. }
  157. static const grpc_connector_vtable connector_vtable = {
  158. connector_ref, connector_unref, connector_shutdown, connector_connect};
  159. typedef struct {
  160. grpc_subchannel_factory base;
  161. gpr_refcount refs;
  162. grpc_mdctx *mdctx;
  163. grpc_channel_args *merge_args;
  164. grpc_channel_security_connector *security_connector;
  165. grpc_channel *master;
  166. } subchannel_factory;
  167. static void subchannel_factory_ref(grpc_subchannel_factory *scf) {
  168. subchannel_factory *f = (subchannel_factory *)scf;
  169. gpr_ref(&f->refs);
  170. }
  171. static void subchannel_factory_unref(grpc_subchannel_factory *scf,
  172. grpc_call_list *call_list) {
  173. subchannel_factory *f = (subchannel_factory *)scf;
  174. if (gpr_unref(&f->refs)) {
  175. GRPC_SECURITY_CONNECTOR_UNREF(&f->security_connector->base,
  176. "subchannel_factory");
  177. GRPC_CHANNEL_INTERNAL_UNREF(f->master, "subchannel_factory", call_list);
  178. grpc_channel_args_destroy(f->merge_args);
  179. grpc_mdctx_unref(f->mdctx);
  180. gpr_free(f);
  181. }
  182. }
  183. static grpc_subchannel *subchannel_factory_create_subchannel(
  184. grpc_subchannel_factory *scf, grpc_subchannel_args *args,
  185. grpc_call_list *call_list) {
  186. subchannel_factory *f = (subchannel_factory *)scf;
  187. connector *c = gpr_malloc(sizeof(*c));
  188. grpc_channel_args *final_args =
  189. grpc_channel_args_merge(args->args, f->merge_args);
  190. grpc_subchannel *s;
  191. memset(c, 0, sizeof(*c));
  192. c->base.vtable = &connector_vtable;
  193. c->security_connector = f->security_connector;
  194. c->mdctx = f->mdctx;
  195. grpc_mdctx_ref(c->mdctx);
  196. c->workqueue = grpc_channel_get_workqueue(f->master);
  197. GRPC_WORKQUEUE_REF(c->workqueue, "connector");
  198. gpr_ref_init(&c->refs, 1);
  199. args->args = final_args;
  200. args->master = f->master;
  201. s = grpc_subchannel_create(&c->base, args);
  202. grpc_connector_unref(&c->base, call_list);
  203. grpc_channel_args_destroy(final_args);
  204. return s;
  205. }
  206. static const grpc_subchannel_factory_vtable subchannel_factory_vtable = {
  207. subchannel_factory_ref, subchannel_factory_unref,
  208. subchannel_factory_create_subchannel};
  209. /* Create a secure client channel:
  210. Asynchronously: - resolve target
  211. - connect to it (trying alternatives as presented)
  212. - perform handshakes */
  213. grpc_channel *grpc_secure_channel_create(grpc_credentials *creds,
  214. const char *target,
  215. const grpc_channel_args *args,
  216. void *reserved) {
  217. grpc_channel *channel;
  218. grpc_arg connector_arg;
  219. grpc_channel_args *args_copy;
  220. grpc_channel_args *new_args_from_connector;
  221. grpc_channel_security_connector *connector;
  222. grpc_mdctx *mdctx;
  223. grpc_workqueue *workqueue;
  224. grpc_resolver *resolver;
  225. subchannel_factory *f;
  226. #define MAX_FILTERS 3
  227. const grpc_channel_filter *filters[MAX_FILTERS];
  228. grpc_call_list call_list = GRPC_CALL_LIST_INIT;
  229. size_t n = 0;
  230. GPR_ASSERT(reserved == NULL);
  231. if (grpc_find_security_connector_in_args(args) != NULL) {
  232. gpr_log(GPR_ERROR, "Cannot set security context in channel args.");
  233. return grpc_lame_client_channel_create(
  234. target, GRPC_STATUS_INVALID_ARGUMENT,
  235. "Security connector exists in channel args.");
  236. }
  237. if (grpc_credentials_create_security_connector(
  238. creds, target, args, NULL, &connector, &new_args_from_connector) !=
  239. GRPC_SECURITY_OK) {
  240. return grpc_lame_client_channel_create(
  241. target, GRPC_STATUS_INVALID_ARGUMENT,
  242. "Failed to create security connector.");
  243. }
  244. mdctx = grpc_mdctx_create();
  245. workqueue = grpc_workqueue_create(&call_list);
  246. connector_arg = grpc_security_connector_to_arg(&connector->base);
  247. args_copy = grpc_channel_args_copy_and_add(
  248. new_args_from_connector != NULL ? new_args_from_connector : args,
  249. &connector_arg, 1);
  250. if (grpc_channel_args_is_census_enabled(args)) {
  251. filters[n++] = &grpc_client_census_filter;
  252. }
  253. filters[n++] = &grpc_compress_filter;
  254. filters[n++] = &grpc_client_channel_filter;
  255. GPR_ASSERT(n <= MAX_FILTERS);
  256. channel = grpc_channel_create_from_filters(target, filters, n, args_copy,
  257. mdctx, workqueue, 1, &call_list);
  258. f = gpr_malloc(sizeof(*f));
  259. f->base.vtable = &subchannel_factory_vtable;
  260. gpr_ref_init(&f->refs, 1);
  261. grpc_mdctx_ref(mdctx);
  262. f->mdctx = mdctx;
  263. GRPC_SECURITY_CONNECTOR_REF(&connector->base, "subchannel_factory");
  264. f->security_connector = connector;
  265. f->merge_args = grpc_channel_args_copy(args_copy);
  266. f->master = channel;
  267. GRPC_CHANNEL_INTERNAL_REF(channel, "subchannel_factory");
  268. resolver = grpc_resolver_create(target, &f->base);
  269. if (!resolver) {
  270. return NULL;
  271. }
  272. grpc_client_channel_set_resolver(grpc_channel_get_channel_stack(channel),
  273. resolver, &call_list);
  274. GRPC_RESOLVER_UNREF(resolver, "create", &call_list);
  275. grpc_subchannel_factory_unref(&f->base, &call_list);
  276. GRPC_SECURITY_CONNECTOR_UNREF(&connector->base, "channel_create");
  277. grpc_channel_args_destroy(args_copy);
  278. if (new_args_from_connector != NULL) {
  279. grpc_channel_args_destroy(new_args_from_connector);
  280. }
  281. grpc_call_list_run(&call_list);
  282. return channel;
  283. }