3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "test/core/end2end/end2end_tests.h"
24 #include <grpc/grpc_security.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
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"
43 typedef grpc_core::InlinedVector<grpc_core::Thread, 1> ThreadList;
45 typedef struct fullstack_secure_fixture_data {
48 } fullstack_secure_fixture_data;
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);
59 f.cq = grpc_completion_queue_create_for_next(nullptr);
60 f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
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,
68 GPR_ASSERT(state == nullptr);
69 cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
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);
78 grpc_secure_channel_create(creds, ffd->localaddr, client_args, nullptr);
79 GPR_ASSERT(f->client != nullptr);
80 grpc_channel_credentials_release(creds);
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);
89 grpc_server_destroy(f->server);
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,
95 grpc_server_credentials_release(server_creds);
96 grpc_server_start(f->server);
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();
105 gpr_free(ffd->localaddr);
106 grpc_core::Delete(ffd);
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);
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();
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);
152 // new credential has been reloaded.
153 arg->status = GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_NEW;
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() !=
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);
178 // new credential has been reloaded.
179 arg->status = GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_NEW;
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,
191 grpc_tls_credentials_options_set_credential_reload_config(options,
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);
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,
211 grpc_tls_credentials_options_set_credential_reload_config(options,
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);
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 = {
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);
235 static int fail_server_auth_check(grpc_channel_args* server_args) {
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) ==
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,
253 grpc_server_credentials_set_auth_metadata_processor(ssl_creds, processor);
255 chttp2_init_server_secure_fullstack(f, server_args, ssl_creds);
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},
269 int main(int argc, char** argv) {
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);
281 GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, roots_filename);
283 for (size_t ind = 0; ind < sizeof(configs) / sizeof(*configs); ind++) {
284 grpc_end2end_tests(argc, argv, configs[ind]);
288 remove(roots_filename);
289 gpr_free(roots_filename);