Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / test / core / end2end / fixtures / h2_spiffe.cc
1 /*
2  *
3  * Copyright 2018 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
19 #include "test/core/end2end/end2end_tests.h"
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <grpc/grpc_security.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27
28 #include <grpc/support/string_util.h>
29 #include "src/core/lib/channel/channel_args.h"
30 #include "src/core/lib/gpr/env.h"
31 #include "src/core/lib/gpr/host_port.h"
32 #include "src/core/lib/gpr/string.h"
33 #include "src/core/lib/gpr/tmpfile.h"
34 #include "src/core/lib/gprpp/inlined_vector.h"
35 #include "src/core/lib/gprpp/thd.h"
36 #include "src/core/lib/security/credentials/credentials.h"
37 #include "src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h"
38 #include "src/core/lib/security/security_connector/ssl_utils.h"
39 #include "test/core/end2end/data/ssl_test_data.h"
40 #include "test/core/util/port.h"
41 #include "test/core/util/test_config.h"
42
43 typedef grpc_core::InlinedVector<grpc_core::Thread, 1> ThreadList;
44
45 typedef struct fullstack_secure_fixture_data {
46   char* localaddr;
47   ThreadList thd_list;
48 } fullstack_secure_fixture_data;
49
50 static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
51     grpc_channel_args* client_args, grpc_channel_args* server_args) {
52   grpc_end2end_test_fixture f;
53   int port = grpc_pick_unused_port_or_die();
54   fullstack_secure_fixture_data* ffd =
55       grpc_core::New<fullstack_secure_fixture_data>();
56   memset(&f, 0, sizeof(f));
57   gpr_join_host_port(&ffd->localaddr, "localhost", port);
58   f.fixture_data = ffd;
59   f.cq = grpc_completion_queue_create_for_next(nullptr);
60   f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
61   return f;
62 }
63
64 static void process_auth_failure(void* state, grpc_auth_context* ctx,
65                                  const grpc_metadata* md, size_t md_count,
66                                  grpc_process_auth_metadata_done_cb cb,
67                                  void* user_data) {
68   GPR_ASSERT(state == nullptr);
69   cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
70 }
71
72 static void chttp2_init_client_secure_fullstack(
73     grpc_end2end_test_fixture* f, grpc_channel_args* client_args,
74     grpc_channel_credentials* creds) {
75   fullstack_secure_fixture_data* ffd =
76       static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
77   f->client =
78       grpc_secure_channel_create(creds, ffd->localaddr, client_args, nullptr);
79   GPR_ASSERT(f->client != nullptr);
80   grpc_channel_credentials_release(creds);
81 }
82
83 static void chttp2_init_server_secure_fullstack(
84     grpc_end2end_test_fixture* f, grpc_channel_args* server_args,
85     grpc_server_credentials* server_creds) {
86   fullstack_secure_fixture_data* ffd =
87       static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
88   if (f->server) {
89     grpc_server_destroy(f->server);
90   }
91   f->server = grpc_server_create(server_args, nullptr);
92   grpc_server_register_completion_queue(f->server, f->cq, nullptr);
93   GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr,
94                                                server_creds));
95   grpc_server_credentials_release(server_creds);
96   grpc_server_start(f->server);
97 }
98
99 void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture* f) {
100   fullstack_secure_fixture_data* ffd =
101       static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
102   for (size_t ind = 0; ind < ffd->thd_list.size(); ind++) {
103     ffd->thd_list[ind].Join();
104   }
105   gpr_free(ffd->localaddr);
106   grpc_core::Delete(ffd);
107 }
108
109 // Application-provided callback for server authorization check.
110 static void server_authz_check_cb(void* user_data) {
111   grpc_tls_server_authorization_check_arg* check_arg =
112       static_cast<grpc_tls_server_authorization_check_arg*>(user_data);
113   GPR_ASSERT(check_arg != nullptr);
114   // result = 1 indicates the server authorization check passes.
115   // Normally, the application code should resort to mapping information
116   // between server identity and target name to derive the result.
117   // For this test, we directly return 1 for simplicity.
118   check_arg->success = 1;
119   check_arg->status = GRPC_STATUS_OK;
120   check_arg->cb(check_arg);
121 }
122
123 // Asynchronous implementation of schedule field in
124 // grpc_server_authorization_check_config.
125 static int server_authz_check_async(
126     void* config_user_data, grpc_tls_server_authorization_check_arg* arg) {
127   fullstack_secure_fixture_data* ffd =
128       static_cast<fullstack_secure_fixture_data*>(config_user_data);
129   ffd->thd_list.push_back(
130       grpc_core::Thread("h2_spiffe_test", &server_authz_check_cb, arg));
131   ffd->thd_list[ffd->thd_list.size() - 1].Start();
132   return 1;
133 }
134
135 // Synchronous implementation of schedule field in
136 // grpc_tls_credential_reload_config instance that is a part of client-side
137 // grpc_tls_credentials_options instance.
138 static int client_cred_reload_sync(void* config_user_data,
139                                    grpc_tls_credential_reload_arg* arg) {
140   grpc_ssl_pem_key_cert_pair** key_cert_pair =
141       static_cast<grpc_ssl_pem_key_cert_pair**>(
142           gpr_zalloc(sizeof(grpc_ssl_pem_key_cert_pair*)));
143   key_cert_pair[0] = static_cast<grpc_ssl_pem_key_cert_pair*>(
144       gpr_zalloc(sizeof(grpc_ssl_pem_key_cert_pair)));
145   key_cert_pair[0]->private_key = gpr_strdup(test_server1_key);
146   key_cert_pair[0]->cert_chain = gpr_strdup(test_server1_cert);
147   if (arg->key_materials_config->pem_key_cert_pair_list().empty()) {
148     grpc_tls_key_materials_config_set_key_materials(
149         arg->key_materials_config, gpr_strdup(test_root_cert),
150         (const grpc_ssl_pem_key_cert_pair**)key_cert_pair, 1);
151   }
152   // new credential has been reloaded.
153   arg->status = GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_NEW;
154   return 0;
155 }
156
157 // Synchronous implementation of schedule field in
158 // grpc_tls_credential_reload_config instance that is a part of server-side
159 // grpc_tls_credentials_options instance.
160 static int server_cred_reload_sync(void* config_user_data,
161                                    grpc_tls_credential_reload_arg* arg) {
162   grpc_ssl_pem_key_cert_pair** key_cert_pair =
163       static_cast<grpc_ssl_pem_key_cert_pair**>(
164           gpr_zalloc(sizeof(grpc_ssl_pem_key_cert_pair*)));
165   key_cert_pair[0] = static_cast<grpc_ssl_pem_key_cert_pair*>(
166       gpr_zalloc(sizeof(grpc_ssl_pem_key_cert_pair)));
167   key_cert_pair[0]->private_key = gpr_strdup(test_server1_key);
168   key_cert_pair[0]->cert_chain = gpr_strdup(test_server1_cert);
169   GPR_ASSERT(arg != nullptr);
170   GPR_ASSERT(arg->key_materials_config != nullptr);
171   GPR_ASSERT(arg->key_materials_config->pem_key_cert_pair_list().data() !=
172              nullptr);
173   if (arg->key_materials_config->pem_key_cert_pair_list().empty()) {
174     grpc_tls_key_materials_config_set_key_materials(
175         arg->key_materials_config, gpr_strdup(test_root_cert),
176         (const grpc_ssl_pem_key_cert_pair**)key_cert_pair, 1);
177   }
178   // new credential has been reloaded.
179   arg->status = GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_NEW;
180   return 0;
181 }
182
183 // Create a SPIFFE channel credential.
184 static grpc_channel_credentials* create_spiffe_channel_credentials(
185     fullstack_secure_fixture_data* ffd) {
186   grpc_tls_credentials_options* options = grpc_tls_credentials_options_create();
187   /* Set credential reload config. */
188   grpc_tls_credential_reload_config* reload_config =
189       grpc_tls_credential_reload_config_create(nullptr, client_cred_reload_sync,
190                                                nullptr, nullptr);
191   grpc_tls_credentials_options_set_credential_reload_config(options,
192                                                             reload_config);
193   /* Set server authorization check config. */
194   grpc_tls_server_authorization_check_config* check_config =
195       grpc_tls_server_authorization_check_config_create(
196           ffd, server_authz_check_async, nullptr, nullptr);
197   grpc_tls_credentials_options_set_server_authorization_check_config(
198       options, check_config);
199   /* Create SPIFFE channel credentials. */
200   grpc_channel_credentials* creds = grpc_tls_spiffe_credentials_create(options);
201   return creds;
202 }
203
204 // Create a SPIFFE server credential.
205 static grpc_server_credentials* create_spiffe_server_credentials() {
206   grpc_tls_credentials_options* options = grpc_tls_credentials_options_create();
207   /* Set credential reload config. */
208   grpc_tls_credential_reload_config* reload_config =
209       grpc_tls_credential_reload_config_create(nullptr, server_cred_reload_sync,
210                                                nullptr, nullptr);
211   grpc_tls_credentials_options_set_credential_reload_config(options,
212                                                             reload_config);
213   /* Set client certificate request type. */
214   grpc_tls_credentials_options_set_cert_request_type(
215       options, GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY);
216   grpc_server_credentials* creds =
217       grpc_tls_spiffe_server_credentials_create(options);
218   return creds;
219 }
220
221 static void chttp2_init_client(grpc_end2end_test_fixture* f,
222                                grpc_channel_args* client_args) {
223   grpc_channel_credentials* ssl_creds = create_spiffe_channel_credentials(
224       static_cast<fullstack_secure_fixture_data*>(f->fixture_data));
225   grpc_arg ssl_name_override = {
226       GRPC_ARG_STRING,
227       const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
228       {const_cast<char*>("foo.test.google.fr")}};
229   grpc_channel_args* new_client_args =
230       grpc_channel_args_copy_and_add(client_args, &ssl_name_override, 1);
231   chttp2_init_client_secure_fullstack(f, new_client_args, ssl_creds);
232   grpc_channel_args_destroy(new_client_args);
233 }
234
235 static int fail_server_auth_check(grpc_channel_args* server_args) {
236   size_t i;
237   if (server_args == nullptr) return 0;
238   for (i = 0; i < server_args->num_args; i++) {
239     if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
240         0) {
241       return 1;
242     }
243   }
244   return 0;
245 }
246
247 static void chttp2_init_server(grpc_end2end_test_fixture* f,
248                                grpc_channel_args* server_args) {
249   grpc_server_credentials* ssl_creds = create_spiffe_server_credentials();
250   if (fail_server_auth_check(server_args)) {
251     grpc_auth_metadata_processor processor = {process_auth_failure, nullptr,
252                                               nullptr};
253     grpc_server_credentials_set_auth_metadata_processor(ssl_creds, processor);
254   }
255   chttp2_init_server_secure_fullstack(f, server_args, ssl_creds);
256 }
257
258 static grpc_end2end_test_config configs[] = {
259     /* client sync reload async authz + server sync reload. */
260     {"chttp2/simple_ssl_fullstack",
261      FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
262          FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS |
263          FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
264          FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER,
265      "foo.test.google.fr", chttp2_create_fixture_secure_fullstack,
266      chttp2_init_client, chttp2_init_server, chttp2_tear_down_secure_fullstack},
267 };
268
269 int main(int argc, char** argv) {
270   FILE* roots_file;
271   size_t roots_size = strlen(test_root_cert);
272   char* roots_filename;
273   grpc_test_init(argc, argv);
274   grpc_end2end_tests_pre_init();
275   /* Set the SSL roots env var. */
276   roots_file = gpr_tmpfile("chttp2_simple_ssl_fullstack_test", &roots_filename);
277   GPR_ASSERT(roots_filename != nullptr);
278   GPR_ASSERT(roots_file != nullptr);
279   GPR_ASSERT(fwrite(test_root_cert, 1, roots_size, roots_file) == roots_size);
280   fclose(roots_file);
281   GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, roots_filename);
282   grpc_init();
283   for (size_t ind = 0; ind < sizeof(configs) / sizeof(*configs); ind++) {
284     grpc_end2end_tests(argc, argv, configs[ind]);
285   }
286   grpc_shutdown();
287   /* Cleanup. */
288   remove(roots_filename);
289   gpr_free(roots_filename);
290   return 0;
291 }