h2_oauth2.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <grpc/support/alloc.h>
  21. #include <grpc/support/log.h>
  22. #include "src/core/lib/channel/channel_args.h"
  23. #include "src/core/lib/gprpp/host_port.h"
  24. #include "src/core/lib/iomgr/iomgr.h"
  25. #include "src/core/lib/iomgr/load_file.h"
  26. #include "src/core/lib/security/credentials/credentials.h"
  27. #include "src/core/lib/security/credentials/ssl/ssl_credentials.h"
  28. #include "test/core/end2end/end2end_tests.h"
  29. #include "test/core/util/port.h"
  30. #include "test/core/util/test_config.h"
  31. #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
  32. #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
  33. #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
  34. static const char oauth2_md[] = "Bearer aaslkfjs424535asdf";
  35. static const char* client_identity_property_name = "smurf_name";
  36. static const char* client_identity = "Brainy Smurf";
  37. struct fullstack_secure_fixture_data {
  38. std::string localaddr;
  39. grpc_tls_version tls_version;
  40. };
  41. static const grpc_metadata* find_metadata(const grpc_metadata* md,
  42. size_t md_count, const char* key,
  43. const char* value) {
  44. size_t i;
  45. for (i = 0; i < md_count; i++) {
  46. if (grpc_slice_str_cmp(md[i].key, key) == 0 &&
  47. grpc_slice_str_cmp(md[i].value, value) == 0) {
  48. return &md[i];
  49. }
  50. }
  51. return nullptr;
  52. }
  53. typedef struct {
  54. size_t pseudo_refcount;
  55. } test_processor_state;
  56. static void process_oauth2_success(void* state, grpc_auth_context* ctx,
  57. const grpc_metadata* md, size_t md_count,
  58. grpc_process_auth_metadata_done_cb cb,
  59. void* user_data) {
  60. const grpc_metadata* oauth2 =
  61. find_metadata(md, md_count, "authorization", oauth2_md);
  62. test_processor_state* s;
  63. GPR_ASSERT(state != nullptr);
  64. s = static_cast<test_processor_state*>(state);
  65. GPR_ASSERT(s->pseudo_refcount == 1);
  66. GPR_ASSERT(oauth2 != nullptr);
  67. grpc_auth_context_add_cstring_property(ctx, client_identity_property_name,
  68. client_identity);
  69. GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(
  70. ctx, client_identity_property_name) == 1);
  71. cb(user_data, oauth2, 1, nullptr, 0, GRPC_STATUS_OK, nullptr);
  72. }
  73. static void process_oauth2_failure(void* state, grpc_auth_context* /*ctx*/,
  74. const grpc_metadata* md, size_t md_count,
  75. grpc_process_auth_metadata_done_cb cb,
  76. void* user_data) {
  77. const grpc_metadata* oauth2 =
  78. find_metadata(md, md_count, "authorization", oauth2_md);
  79. test_processor_state* s;
  80. GPR_ASSERT(state != nullptr);
  81. s = static_cast<test_processor_state*>(state);
  82. GPR_ASSERT(s->pseudo_refcount == 1);
  83. GPR_ASSERT(oauth2 != nullptr);
  84. cb(user_data, oauth2, 1, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
  85. }
  86. static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
  87. const grpc_channel_args* /*client_args*/,
  88. const grpc_channel_args* /*server_args*/, grpc_tls_version tls_version) {
  89. grpc_end2end_test_fixture f;
  90. int port = grpc_pick_unused_port_or_die();
  91. fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data();
  92. memset(&f, 0, sizeof(f));
  93. ffd->localaddr = grpc_core::JoinHostPort("localhost", port);
  94. ffd->tls_version = tls_version;
  95. f.fixture_data = ffd;
  96. f.cq = grpc_completion_queue_create_for_next(nullptr);
  97. f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
  98. return f;
  99. }
  100. static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack_tls1_2(
  101. const grpc_channel_args* client_args,
  102. const grpc_channel_args* server_args) {
  103. return chttp2_create_fixture_secure_fullstack(client_args, server_args,
  104. grpc_tls_version::TLS1_2);
  105. }
  106. static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack_tls1_3(
  107. const grpc_channel_args* client_args,
  108. const grpc_channel_args* server_args) {
  109. return chttp2_create_fixture_secure_fullstack(client_args, server_args,
  110. grpc_tls_version::TLS1_3);
  111. }
  112. static void chttp2_init_client_secure_fullstack(
  113. grpc_end2end_test_fixture* f, const grpc_channel_args* client_args,
  114. grpc_channel_credentials* creds) {
  115. fullstack_secure_fixture_data* ffd =
  116. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  117. f->client = grpc_channel_create(ffd->localaddr.c_str(), creds, client_args);
  118. GPR_ASSERT(f->client != nullptr);
  119. grpc_channel_credentials_release(creds);
  120. }
  121. static void chttp2_init_server_secure_fullstack(
  122. grpc_end2end_test_fixture* f, const grpc_channel_args* server_args,
  123. grpc_server_credentials* server_creds) {
  124. fullstack_secure_fixture_data* ffd =
  125. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  126. if (f->server) {
  127. grpc_server_destroy(f->server);
  128. }
  129. f->server = grpc_server_create(server_args, nullptr);
  130. grpc_server_register_completion_queue(f->server, f->cq, nullptr);
  131. GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(),
  132. server_creds));
  133. grpc_server_credentials_release(server_creds);
  134. grpc_server_start(f->server);
  135. }
  136. void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture* f) {
  137. fullstack_secure_fixture_data* ffd =
  138. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  139. delete ffd;
  140. }
  141. static void chttp2_init_client_simple_ssl_with_oauth2_secure_fullstack(
  142. grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) {
  143. grpc_core::ExecCtx exec_ctx;
  144. grpc_slice ca_slice;
  145. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  146. grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
  147. const char* test_root_cert =
  148. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
  149. grpc_channel_credentials* ssl_creds =
  150. grpc_ssl_credentials_create(test_root_cert, nullptr, nullptr, nullptr);
  151. if (f != nullptr && ssl_creds != nullptr) {
  152. // Set the min and max TLS version.
  153. grpc_ssl_credentials* creds =
  154. reinterpret_cast<grpc_ssl_credentials*>(ssl_creds);
  155. fullstack_secure_fixture_data* ffd =
  156. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  157. creds->set_min_tls_version(ffd->tls_version);
  158. creds->set_max_tls_version(ffd->tls_version);
  159. }
  160. grpc_call_credentials* oauth2_creds =
  161. grpc_md_only_test_credentials_create("authorization", oauth2_md);
  162. grpc_channel_credentials* ssl_oauth2_creds =
  163. grpc_composite_channel_credentials_create(ssl_creds, oauth2_creds,
  164. nullptr);
  165. grpc_arg ssl_name_override = {
  166. GRPC_ARG_STRING,
  167. const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
  168. {const_cast<char*>("foo.test.google.fr")}};
  169. const grpc_channel_args* new_client_args =
  170. grpc_channel_args_copy_and_add(client_args, &ssl_name_override, 1);
  171. chttp2_init_client_secure_fullstack(f, new_client_args, ssl_oauth2_creds);
  172. grpc_channel_args_destroy(new_client_args);
  173. grpc_channel_credentials_release(ssl_creds);
  174. grpc_call_credentials_release(oauth2_creds);
  175. grpc_slice_unref(ca_slice);
  176. }
  177. static int fail_server_auth_check(const grpc_channel_args* server_args) {
  178. size_t i;
  179. if (server_args == nullptr) return 0;
  180. for (i = 0; i < server_args->num_args; i++) {
  181. if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
  182. 0) {
  183. return 1;
  184. }
  185. }
  186. return 0;
  187. }
  188. static void processor_destroy(void* state) {
  189. test_processor_state* s = static_cast<test_processor_state*>(state);
  190. GPR_ASSERT((s->pseudo_refcount--) == 1);
  191. gpr_free(s);
  192. }
  193. static grpc_auth_metadata_processor test_processor_create(int failing) {
  194. test_processor_state* s =
  195. static_cast<test_processor_state*>(gpr_malloc(sizeof(*s)));
  196. grpc_auth_metadata_processor result;
  197. s->pseudo_refcount = 1;
  198. result.state = s;
  199. result.destroy = processor_destroy;
  200. if (failing) {
  201. result.process = process_oauth2_failure;
  202. } else {
  203. result.process = process_oauth2_success;
  204. }
  205. return result;
  206. }
  207. static void chttp2_init_server_simple_ssl_secure_fullstack(
  208. grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) {
  209. grpc_slice cert_slice, key_slice;
  210. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  211. "load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
  212. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  213. grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
  214. const char* server_cert =
  215. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
  216. const char* server_key =
  217. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
  218. grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key, server_cert};
  219. grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
  220. nullptr, &pem_key_cert_pair, 1, 0, nullptr);
  221. if (f != nullptr && ssl_creds != nullptr) {
  222. // Set the min and max TLS version.
  223. grpc_ssl_server_credentials* creds =
  224. reinterpret_cast<grpc_ssl_server_credentials*>(ssl_creds);
  225. fullstack_secure_fixture_data* ffd =
  226. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  227. creds->set_min_tls_version(ffd->tls_version);
  228. creds->set_max_tls_version(ffd->tls_version);
  229. }
  230. grpc_server_credentials_set_auth_metadata_processor(
  231. ssl_creds, test_processor_create(fail_server_auth_check(server_args)));
  232. chttp2_init_server_secure_fullstack(f, server_args, ssl_creds);
  233. grpc_slice_unref(cert_slice);
  234. grpc_slice_unref(key_slice);
  235. }
  236. /* All test configurations */
  237. static grpc_end2end_test_config configs[] = {
  238. {"chttp2/simple_ssl_with_oauth2_fullstack_tls1_2",
  239. FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
  240. FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS |
  241. FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
  242. FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER,
  243. "foo.test.google.fr", chttp2_create_fixture_secure_fullstack_tls1_2,
  244. chttp2_init_client_simple_ssl_with_oauth2_secure_fullstack,
  245. chttp2_init_server_simple_ssl_secure_fullstack,
  246. chttp2_tear_down_secure_fullstack},
  247. {"chttp2/simple_ssl_with_oauth2_fullstack_tls1_3",
  248. FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
  249. FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS |
  250. FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
  251. FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER |
  252. FEATURE_MASK_DOES_NOT_SUPPORT_CLIENT_HANDSHAKE_COMPLETE_FIRST,
  253. "foo.test.google.fr", chttp2_create_fixture_secure_fullstack_tls1_3,
  254. chttp2_init_client_simple_ssl_with_oauth2_secure_fullstack,
  255. chttp2_init_server_simple_ssl_secure_fullstack,
  256. chttp2_tear_down_secure_fullstack},
  257. };
  258. int main(int argc, char** argv) {
  259. size_t i;
  260. grpc::testing::TestEnvironment env(argc, argv);
  261. grpc_end2end_tests_pre_init();
  262. grpc_init();
  263. for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
  264. grpc_end2end_tests(argc, argv, configs[i]);
  265. }
  266. grpc_shutdown();
  267. return 0;
  268. }