Debian Codegraph @

src/tls/test-cockpit-certificate-ensure.c

raw
1 /*
2 * Copyright (C) 2021 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include "testlib/cockpittest.h"
9
10 #include <fcntl.h>
11 #include <glib.h>
12 #include <sys/stat.h>
13
14 #define CERTIFICATE_HELPER BUILDDIR "/cockpit-certificate-ensure"
15
16 typedef struct {
17 GSubprocessLauncher *launcher;
18
19 gchar *config_dir;
20 int config_dir_fd;
21
22 char *runtime_dir;
23 int runtime_dir_fd;
24 } Fixture;
25
26 typedef struct {
27 const gchar **files;
28
29 const gchar *check_stdout;
30 const gchar *check_stderr;
31 int check_exit;
32
33 const gchar *copy_stdout;
34 const gchar *copy_stderr;
35 int copy_exit;
36 const gchar *key_source;
37 const gchar *cert_source;
38 } TestCase;
39
40 static void
41 delete_all_files (int fd,
42 const char *path)
43 {
44 int dirfd = openat (fd, path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
45 g_assert_no_errno (dirfd);
46 DIR *dirp = fdopendir (dirfd); /* owns dirfd */
47
48 const struct dirent *entry;
49 while ((entry = readdir (dirp)))
50 if (entry->d_name[0] != '.')
51 {
52 int r = unlinkat (dirfd, entry->d_name, 0);
53 g_assert_no_errno (r);
54 }
55
56 closedir (dirp);
57 }
58
59 static void
60 fixture_teardown (Fixture *self,
61 gconstpointer data)
62 {
63 g_clear_object (&self->launcher);
64
65 delete_all_files (self->config_dir_fd, "cockpit/ws-certs.d");
66
67 int r = unlinkat (self->config_dir_fd, "cockpit/ws-certs.d", AT_REMOVEDIR);
68 g_assert_no_errno (r);
69
70 r = unlinkat (self->config_dir_fd, "cockpit", AT_REMOVEDIR);
71 g_assert_no_errno (r);
72
73 r = rmdir (self->config_dir);
74 g_assert_no_errno (r);
75
76 close (self->config_dir_fd);
77 g_free (self->config_dir);
78
79 /* "server/" is only created for successful copy */
80 if (faccessat (self->runtime_dir_fd, "server", F_OK, 0) == 0)
81 {
82 delete_all_files (self->runtime_dir_fd, "server");
83
84 r = unlinkat (self->runtime_dir_fd, "server", AT_REMOVEDIR);
85 g_assert_no_errno (r);
86 }
87
88 r = rmdir (self->runtime_dir);
89 g_assert_no_errno (r);
90
91 close (self->runtime_dir_fd);
92 free (self->runtime_dir);
93 }
94
95 static void
96 fixture_setup (Fixture *self,
97 gconstpointer data)
98 {
99 const TestCase *tc = data;
100 g_autoptr(GError) error = NULL;
101
102 /* runtime dir */
103 self->runtime_dir = g_dir_make_tmp ("cockpit-test-runtime.XXXXXX", &error);
104 g_assert_no_error (error);
105
106 self->runtime_dir_fd = open (self->runtime_dir, O_PATH | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
107 g_assert_no_errno (self->runtime_dir_fd);
108
109 /* config dir */
110 self->config_dir = g_dir_make_tmp ("cockpit-test-config.XXXXXX", &error);
111 g_assert_no_error (error);
112
113 self->config_dir_fd = open (self->config_dir, O_PATH | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
114 g_assert_no_errno (self->config_dir_fd);
115
116 int r = mkdirat (self->config_dir_fd, "cockpit", 0700);
117 g_assert_no_errno (r);
118
119 r = mkdirat (self->config_dir_fd, "cockpit/ws-certs.d", 0700);
120 g_assert_no_errno (r);
121
122 /* populate ws-certs.d */
123 for (int i = 0; tc->files[i]; i++)
124 {
125 g_autofree gchar *linkname = g_strconcat ("cockpit/ws-certs.d", strrchr (tc->files[i], '/'), NULL);
126 r = symlinkat (tc->files[i], self->config_dir_fd, linkname);
127 g_assert_no_errno (r);
128 }
129
130 /* launcher */
131 self->launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_PIPE);
132 g_subprocess_launcher_setenv (self->launcher, "XDG_CONFIG_DIRS", self->config_dir, TRUE);
133 g_subprocess_launcher_setenv (self->launcher, "RUNTIME_DIRECTORY", self->runtime_dir, TRUE);
134 }
135
136 static void
137 test_check (Fixture *fixture,
138 gconstpointer data)
139 {
140 const TestCase *tc = data;
141 g_autoptr(GError) error = NULL;
142
143 g_autoptr(GSubprocess) helper = g_subprocess_launcher_spawn (fixture->launcher, &error,
144 CERTIFICATE_HELPER, "--check", NULL);
145 g_assert_no_error (error);
146
147 g_autofree gchar *stdout_str = NULL;
148 g_autofree gchar *stderr_str = NULL;
149 gboolean result = g_subprocess_communicate_utf8 (helper, NULL, NULL, &stdout_str, &stderr_str, &error);
150 g_assert_no_error (error);
151 g_assert (result);
152
153 g_assert (g_subprocess_get_if_exited (helper));
154
155 cockpit_assert_strmatch (stdout_str, tc->check_stdout);
156 cockpit_assert_strmatch (stderr_str, tc->check_stderr);
157 g_assert_cmpint (g_subprocess_get_exit_status (helper), ==, tc->check_exit);
158 }
159
160 static gchar *
161 areadlinkat (int dirfd,
162 const char *filename)
163 {
164 char buffer[PATH_MAX];
165 ssize_t s = readlinkat (dirfd, filename, buffer, sizeof buffer);
166 g_assert_cmpint (s, <, sizeof buffer);
167 if (s == -1 && errno == ENOENT)
168 s = 0; /* results in returning empty string */
169 else
170 g_assert_no_errno (s);
171 buffer[s] = '\0';
172 return g_strdup (buffer);
173 }
174
175 static void
176 append_file_to_string (GString *string,
177 const gchar *filename)
178 {
179 g_autoptr(GError) error = NULL;
180 g_autofree gchar *content = NULL;
181 gsize size;
182
183 g_file_get_contents (filename, &content, &size, &error);
184 g_assert_no_error (error);
185
186 g_string_append_len (string, content, size);
187 }
188
189 static void
190 test_copy (Fixture *fixture,
191 gconstpointer data)
192 {
193 const TestCase *tc = data;
194 g_autoptr(GError) error = NULL;
195
196 g_autoptr(GSubprocess) helper = g_subprocess_launcher_spawn (fixture->launcher, &error,
197 CERTIFICATE_HELPER, "--for-cockpit-tls", NULL);
198 g_assert_no_error (error);
199
200 g_autofree gchar *stdout_str = NULL;
201 g_autofree gchar *stderr_str = NULL;
202 gboolean result = g_subprocess_communicate_utf8 (helper, NULL, NULL, &stdout_str, &stderr_str, &error);
203 g_assert_no_error (error);
204 g_assert (result);
205
206 g_assert (g_subprocess_get_if_exited (helper));
207
208 cockpit_assert_strmatch (stdout_str, tc->copy_stdout);
209 cockpit_assert_strmatch (stderr_str, tc->copy_stderr);
210 g_assert_cmpint (g_subprocess_get_exit_status (helper), ==, tc->copy_exit);
211
212 g_autofree gchar *cert_source = areadlinkat (fixture->runtime_dir_fd, "server/0.crt.source");
213 cockpit_assert_strmatch (cert_source, tc->cert_source);
214
215 g_autofree gchar *key_source = areadlinkat (fixture->runtime_dir_fd, "server/0.key.source");
216 cockpit_assert_strmatch (key_source, tc->key_source);
217
218 if (tc->copy_exit == EXIT_SUCCESS)
219 {
220 /* Check to make sure the input is the same as the output */
221 g_autoptr(GString) input_data = g_string_new (NULL);
222 append_file_to_string (input_data, cert_source);
223 if (!g_str_equal (key_source, cert_source))
224 append_file_to_string (input_data, key_source);
225 g_autoptr(GTlsCertificate) input = g_tls_certificate_new_from_pem (input_data->str, input_data->len, &error);
226 g_assert_no_error (error);
227
228 g_autofree gchar *certfile = g_build_filename (fixture->runtime_dir, "server", "0.crt", NULL);
229 g_autofree gchar *keyfile = g_build_filename (fixture->runtime_dir, "server", "0.key", NULL);
230 g_autoptr(GTlsCertificate) output = g_tls_certificate_new_from_files (certfile, keyfile, &error);
231 #if !GLIB_CHECK_VERSION(2,58,0)
232 /* Older GLib (RHEL 8) doesn't know how to find EC private keys */
233 if (error &&
234 strstr (error->message, "No PEM-encoded private key found") &&
235 strstr (input_data->str, "BEGIN EC PRIVATE KEY"))
236 {
237 g_test_skip ("EC private keys unsupported");
238 return;
239 }
240 #endif
241 g_assert_no_error (error);
242
243 /* NB: doesn't check key, and there's no way to read it back :( */
244 g_assert (g_tls_certificate_is_same (input, output));
245 }
246 }
247
248 const gchar *no_files[] = { NULL };
249 const gchar *good_rsa_files[] = { SRCDIR "/test/data/mock-server.crt",
250 SRCDIR "/test/data/mock-server.key", NULL };
251 const gchar *good_ecc_files[] = { SRCDIR "/test/data/mock-ecc.crt",
252 SRCDIR "/test/data/mock-ecc.key", NULL };
253 const gchar *bad_files[] = { SRCDIR "/bad", NULL };
254 const gchar *bad_files2[] = { SRCDIR "/test/data/mock-server.crt", SRCDIR "/bad2", NULL };
255 const gchar *invalid_files1[] = { SRCDIR "/src/common/mock-config/cockpit/cockpit.conf",
256 SRCDIR "/src/common/mock-config/cockpit/cockpit-alt.conf", NULL };
257 const gchar *invalid_files2[] = { SRCDIR "/test/data/mock-server.crt",
258 SRCDIR "/test/data/mock-client.crt", NULL };
259 const gchar *invalid_files3[] = { SRCDIR "/test/data/mock-client.key", NULL };
260
261 static const TestCase case_good_rsa_file = {
262 .files = good_rsa_files,
263
264 .check_stdout = "Would use */mock-server.crt*",
265 .check_stderr = "",
266 .check_exit = EXIT_SUCCESS,
267
268 .copy_stdout = "",
269 .copy_stderr = "",
270 .copy_exit = EXIT_SUCCESS,
271 .cert_source = "*/cockpit/ws-certs.d/mock-server.crt",
272 .key_source = "*/cockpit/ws-certs.d/mock-server.key",
273 };
274
275 static const TestCase case_good_ecc_file = {
276 .files = good_ecc_files,
277
278 .check_stdout = "Would use */mock-ecc.crt*",
279 .check_stderr = "",
280 .check_exit = EXIT_SUCCESS,
281
282 .copy_stdout = "",
283 .copy_stderr = "",
284 .copy_exit = EXIT_SUCCESS,
285 .cert_source = "*/cockpit/ws-certs.d/mock-ecc.crt",
286 .key_source = "*/cockpit/ws-certs.d/mock-ecc.key",
287 };
288
289 static const TestCase case_bad_file = {
290 .files = bad_files,
291
292 .check_stdout = "Unable to find*Would create*",
293 .check_stderr = "",
294 .check_exit = EXIT_FAILURE
295 };
296
297 static const TestCase case_bad_file2 = {
298 .files = bad_files2,
299
300 .check_stdout = "",
301 .check_stderr = "*open*mock-server.key*No such file*",
302 .check_exit = EXIT_FAILURE,
303
304 .copy_stdout = "",
305 .copy_stderr = "*open*mock-server.key*No such file*",
306 .copy_exit = EXIT_FAILURE,
307 };
308
309 static const TestCase case_invalid1 = {
310 .files = invalid_files1,
311
312 .check_stdout = "Unable to find*Would create*",
313 .check_stderr = "",
314 .check_exit = EXIT_FAILURE
315 };
316
317 static const TestCase case_invalid2 = {
318 .files = invalid_files2,
319
320 .check_stdout = "",
321 .check_stderr = "*open*mock-server.key*No such file*",
322 .check_exit = EXIT_FAILURE,
323
324 .copy_stdout = "",
325 .copy_stderr = "*open*mock-server.key*No such file*",
326 .copy_exit = EXIT_FAILURE,
327 };
328
329 static const TestCase case_invalid3 = {
330 .files = invalid_files3,
331
332 .check_stdout = "Unable to find*Would create*",
333 .check_stderr = "",
334 .check_exit = EXIT_FAILURE
335 };
336
337 static const TestCase case_create = {
338 .files = no_files,
339
340 .check_stdout = "Unable to find*Would create*",
341 .check_stderr = "",
342 .check_exit = EXIT_FAILURE
343 };
344
345 static const TestCase case_invalid_validity = {
346 .files = (const gchar *[]) { SRCDIR "/test/data/100years/0-self-signed.cert",
347 SRCDIR "/test/data/100years/0-self-signed.key",
348 NULL },
349 .check_stdout = "Found*self-signed*but it needs to be reissued*",
350 .check_stderr = "",
351 .check_exit = EXIT_FAILURE
352 };
353
354 static const TestCase case_expired = {
355 .files = (const gchar *[]) { SRCDIR "/test/data/expired/0-self-signed.cert",
356 SRCDIR "/test/data/expired/0-self-signed.key",
357 NULL },
358 .check_stdout = "Found*self-signed*but it needs to be reissued*",
359 .check_stderr = "",
360 .check_exit = EXIT_FAILURE
361 };
362
363 static const TestCase case_mismatched = {
364 .files = (const gchar *[]) { SRCDIR "/test/data/expired/0-self-signed.cert",
365 SRCDIR "/test/data/100years/0-self-signed.key",
366 NULL },
367 .check_stdout = "",
368 .check_stderr = "*certificate and the given key do not match*",
369 .check_exit = EXIT_FAILURE,
370
371 .copy_stdout = "",
372 .copy_stderr = "*certificate and the given key do not match*",
373 .copy_exit = EXIT_FAILURE,
374 .cert_source = "",
375 .key_source = ""
376 };
377
378 static const TestCase expired_not_selfsign = {
379 .files = (const gchar *[]) { SRCDIR "/test/data/expired/1.cert",
380 SRCDIR "/test/data/expired/1.key",
381 NULL },
382 .check_stdout = "Would use*1.cert*",
383 .check_stderr = "",
384 .check_exit = EXIT_SUCCESS,
385
386 .copy_stdout = "",
387 .copy_stderr = "",
388 .copy_exit = EXIT_SUCCESS,
389 .cert_source = "*/cockpit/ws-certs.d/1.cert",
390 .key_source = "*/cockpit/ws-certs.d/1.key",
391 };
392
393 static const TestCase expired_combined = {
394 .files = (const gchar *[]) { SRCDIR "/test/data/expired/combined.cert",
395 NULL },
396 .check_stdout = "",
397 .check_stderr = "*merged certificate and key files are unsupported*",
398 .check_exit = EXIT_FAILURE,
399
400 .copy_stdout = "",
401 .copy_stderr = "*merged certificate and key files are deprecated*",
402 .copy_exit = EXIT_SUCCESS,
403 .key_source = "*/cockpit/ws-certs.d/combined.cert",
404 .cert_source = "*/cockpit/ws-certs.d/combined.cert"
405 };
406
407 static const TestCase many_files = {
408 .files = (const gchar *[]) { SRCDIR "/test/data/expired/0-self-signed.cert",
409 SRCDIR "/test/data/expired/0-self-signed.key",
410 SRCDIR "/test/data/expired/1.cert",
411 SRCDIR "/test/data/expired/1.key",
412 SRCDIR "/test/data/expired/combined.cert",
413 NULL },
414 .check_stdout = "",
415 .check_stderr = "*merged certificate and key files are unsupported*",
416 .check_exit = EXIT_FAILURE,
417
418 .copy_stdout = "",
419 .copy_stderr = "*merged certificate and key files are deprecated*",
420 .copy_exit = EXIT_SUCCESS,
421 .key_source = "*/cockpit/ws-certs.d/combined.cert",
422 .cert_source = "*/cockpit/ws-certs.d/combined.cert"
423 };
424
425 int
426 main (int argc,
427 char *argv[])
428 {
429 cockpit_test_init (&argc, &argv);
430
431 g_test_add ("/cockpit-certificate-ensure/check/good-rsa",
432 Fixture, &case_good_rsa_file, fixture_setup, test_check, fixture_teardown);
433 g_test_add ("/cockpit-certificate-ensure/copy/good-rsa",
434 Fixture, &case_good_rsa_file, fixture_setup, test_copy, fixture_teardown);
435 g_test_add ("/cockpit-certificate-ensure/check/good-ecc",
436 Fixture, &case_good_ecc_file, fixture_setup, test_check, fixture_teardown);
437 g_test_add ("/cockpit-certificate-ensure/copy/good-ecc",
438 Fixture, &case_good_ecc_file, fixture_setup, test_copy, fixture_teardown);
439 g_test_add ("/cockpit-certificate-ensure/bad-file",
440 Fixture, &case_bad_file, fixture_setup, test_check, fixture_teardown);
441 g_test_add ("/cockpit-certificate-ensure/bad-file2",
442 Fixture, &case_bad_file2, fixture_setup, test_check, fixture_teardown);
443 g_test_add ("/cockpit-certificate-ensure/not-valid",
444 Fixture, &case_invalid1, fixture_setup, test_check, fixture_teardown);
445 g_test_add ("/cockpit-certificate-ensure/no-key",
446 Fixture, &case_invalid2, fixture_setup, test_check, fixture_teardown);
447 g_test_add ("/cockpit-certificate-ensure/no-cert",
448 Fixture, &case_invalid3, fixture_setup, test_check, fixture_teardown);
449 g_test_add ("/cockpit-certificate-ensure/create",
450 Fixture, &case_create, fixture_setup, test_check, fixture_teardown);
451
452 g_test_add ("/cockpit-certificate-ensure/invalid-validity",
453 Fixture, &case_invalid_validity, fixture_setup, test_check, fixture_teardown);
454 g_test_add ("/cockpit-certificate-ensure/expired",
455 Fixture, &case_expired, fixture_setup, test_check, fixture_teardown);
456 g_test_add ("/cockpit-certificate-ensure/check/mismatched",
457 Fixture, &case_mismatched, fixture_setup, test_check, fixture_teardown);
458 g_test_add ("/cockpit-certificate-ensure/copy/mismatched",
459 Fixture, &case_mismatched, fixture_setup, test_copy, fixture_teardown);
460
461 g_test_add ("/cockpit-certificate-ensure/check/expired-not-self-signed",
462 Fixture, &expired_not_selfsign, fixture_setup, test_check, fixture_teardown);
463 g_test_add ("/cockpit-certificate-ensure/copy/expired-not-self-signed",
464 Fixture, &expired_not_selfsign, fixture_setup, test_copy, fixture_teardown);
465
466 g_test_add ("/cockpit-certificate-ensure/check/expired-combined",
467 Fixture, &expired_combined, fixture_setup, test_check, fixture_teardown);
468 g_test_add ("/cockpit-certificate-ensure/copy/expired-combined",
469 Fixture, &expired_combined, fixture_setup, test_copy, fixture_teardown);
470
471 g_test_add ("/cockpit-certificate-ensure/check/many-files",
472 Fixture, &many_files, fixture_setup, test_check, fixture_teardown);
473 g_test_add ("/cockpit-certificate-ensure/copy/many-files",
474 Fixture, &many_files, fixture_setup, test_copy, fixture_teardown);
475
476 return g_test_run ();
477 }