Debian Codegraph @

launcher/spec/launch_policy_test.go

raw
1 package spec
2
3 import (
4 "testing"
5
6 "github.com/google/go-cmp/cmp"
7 "github.com/google/go-tpm-tools/launcher/internal/launchermount"
8 "github.com/google/go-tpm-tools/launcher/internal/logging"
9 )
10
11 func TestLaunchPolicy(t *testing.T) {
12 testCases := []struct {
13 testName string
14 imageLabels map[string]string
15 expectedPolicy LaunchPolicy
16 }{
17 {
18 "single ENV override, CMD override",
19 map[string]string{
20 envOverride: "foo",
21 cmdOverride: "true",
22 },
23 LaunchPolicy{
24 AllowedEnvOverride: []string{"foo"},
25 AllowedCmdOverride: true,
26 },
27 },
28 {
29 "multiple ENV override, no CMD override",
30 map[string]string{
31 envOverride: "foo,bar",
32 },
33 LaunchPolicy{
34 AllowedEnvOverride: []string{"foo", "bar"},
35 AllowedCmdOverride: false,
36 },
37 },
38 {
39 "no ENV override, no CMD override",
40 nil,
41 LaunchPolicy{
42 AllowedEnvOverride: nil,
43 AllowedCmdOverride: false,
44 },
45 },
46 {
47 "empty string in ENV override",
48 map[string]string{
49 envOverride: ",,,foo",
50 cmdOverride: "false",
51 },
52 LaunchPolicy{
53 AllowedEnvOverride: []string{"foo"},
54 AllowedCmdOverride: false,
55 },
56 },
57 }
58
59 for _, testcase := range testCases {
60 t.Run(testcase.testName, func(t *testing.T) {
61 // Add default values for policy fields. Not relevant to tested behavior.
62 testcase.expectedPolicy.HardenedImageMonitoring = None
63 testcase.expectedPolicy.DebugImageMonitoring = MemoryOnly
64
65 got, err := GetLaunchPolicy(testcase.imageLabels, logging.SimpleLogger())
66 if err != nil {
67 t.Fatal(err)
68 }
69
70 if !cmp.Equal(got, testcase.expectedPolicy) {
71 t.Errorf("Launchspec got %+v, want %+v", got, testcase.expectedPolicy)
72 }
73 })
74 }
75 }
76
77 func TestVerify(t *testing.T) {
78 testCases := []struct {
79 testName string
80 policy LaunchPolicy
81 spec LaunchSpec
82 expectErr bool
83 }{
84 {
85 "allows everything",
86 LaunchPolicy{
87 AllowedEnvOverride: []string{"foo"},
88 AllowedCmdOverride: true,
89 AllowedLogRedirect: always,
90 HardenedImageMonitoring: MemoryOnly,
91 DebugImageMonitoring: MemoryOnly,
92 },
93 LaunchSpec{
94 Envs: []EnvVar{{Name: "foo", Value: "foo"}},
95 Cmd: []string{"foo"},
96 LogRedirect: Everywhere,
97 MonitoringEnabled: MemoryOnly,
98 },
99 false,
100 },
101 {
102 "default case",
103 LaunchPolicy{},
104 LaunchSpec{},
105 false,
106 },
107 {
108 "env override violation",
109 LaunchPolicy{
110 AllowedEnvOverride: []string{"foo"},
111 },
112 LaunchSpec{
113 Envs: []EnvVar{{Name: "bar", Value: ""}},
114 },
115 true,
116 },
117 {
118 "cmd violation",
119 LaunchPolicy{
120 AllowedCmdOverride: false,
121 },
122 LaunchSpec{
123 Cmd: []string{"foo"},
124 },
125 true,
126 },
127 {
128 "log redirect (never, everywhere, hardened): err",
129 LaunchPolicy{
130 AllowedLogRedirect: never,
131 },
132 LaunchSpec{
133 LogRedirect: Everywhere,
134 Hardened: true,
135 },
136 true,
137 },
138 {
139 "log redirect (never, cloudlogging, hardened): err",
140 LaunchPolicy{
141 AllowedLogRedirect: never,
142 },
143 LaunchSpec{
144 LogRedirect: CloudLogging,
145 Hardened: true,
146 },
147 true,
148 },
149 {
150 "log redirect (never, serial, hardened): err",
151 LaunchPolicy{
152 AllowedLogRedirect: never,
153 },
154 LaunchSpec{
155 LogRedirect: Serial,
156 Hardened: true,
157 },
158 true,
159 },
160 {
161 "log redirect (never, nowhere, hardened): noerr",
162 LaunchPolicy{
163 AllowedLogRedirect: never,
164 },
165 LaunchSpec{
166 LogRedirect: Nowhere,
167 Hardened: true,
168 },
169 false,
170 },
171 {
172 "log redirect (never, everywhere, debug): err",
173 LaunchPolicy{
174 AllowedLogRedirect: never,
175 },
176 LaunchSpec{
177 LogRedirect: Everywhere,
178 Hardened: false,
179 },
180 true,
181 },
182 {
183 "log redirect (never, cloudlogging, debug): err",
184 LaunchPolicy{
185 AllowedLogRedirect: never,
186 },
187 LaunchSpec{
188 LogRedirect: CloudLogging,
189 Hardened: false,
190 },
191 true,
192 },
193 {
194 "log redirect (never, serial, debug): err",
195 LaunchPolicy{
196 AllowedLogRedirect: never,
197 },
198 LaunchSpec{
199 LogRedirect: Serial,
200 Hardened: false,
201 },
202 true,
203 },
204 {
205 "log redirect (never, nowhere, debug): noerr",
206 LaunchPolicy{
207 AllowedLogRedirect: never,
208 },
209 LaunchSpec{
210 LogRedirect: Nowhere,
211 Hardened: false,
212 },
213 false,
214 },
215 {
216 "log redirect (debugOnly, everywhere, hardened): err",
217 LaunchPolicy{
218 AllowedLogRedirect: debugOnly,
219 },
220 LaunchSpec{
221 LogRedirect: Everywhere,
222 Hardened: true,
223 },
224 true,
225 },
226 {
227 "log redirect (debugOnly, cloudlogging, hardened): err",
228 LaunchPolicy{
229 AllowedLogRedirect: debugOnly,
230 },
231 LaunchSpec{
232 LogRedirect: CloudLogging,
233 Hardened: true,
234 },
235 true,
236 },
237 {
238 "log redirect (debugOnly, serial, hardened): err",
239 LaunchPolicy{
240 AllowedLogRedirect: debugOnly,
241 },
242 LaunchSpec{
243 LogRedirect: Serial,
244 Hardened: true,
245 },
246 true,
247 },
248 {
249 "log redirect (debugOnly, nowhere, hardened): noerr",
250 LaunchPolicy{
251 AllowedLogRedirect: debugOnly,
252 },
253 LaunchSpec{
254 LogRedirect: Nowhere,
255 Hardened: true,
256 },
257 false,
258 },
259 {
260 "log redirect (debugOnly, everywhere, debug): noerr",
261 LaunchPolicy{
262 AllowedLogRedirect: debugOnly,
263 },
264 LaunchSpec{
265 LogRedirect: Everywhere,
266 Hardened: false,
267 },
268 false,
269 },
270 {
271 "log redirect (debugOnly, cloudlogging, debug): noerr",
272 LaunchPolicy{
273 AllowedLogRedirect: debugOnly,
274 },
275 LaunchSpec{
276 LogRedirect: CloudLogging,
277 Hardened: false,
278 },
279 false,
280 },
281 {
282 "log redirect (debugOnly, serial, debug): noerr",
283 LaunchPolicy{
284 AllowedLogRedirect: debugOnly,
285 },
286 LaunchSpec{
287 LogRedirect: Serial,
288 Hardened: false,
289 },
290 false,
291 },
292 {
293 "log redirect (debugOnly, nowhere, debug): noerr",
294 LaunchPolicy{
295 AllowedLogRedirect: debugOnly,
296 },
297 LaunchSpec{
298 LogRedirect: Nowhere,
299 Hardened: false,
300 },
301 false,
302 },
303 {
304 "log redirect (always, everywhere, hardened): noerr",
305 LaunchPolicy{
306 AllowedLogRedirect: always,
307 },
308 LaunchSpec{
309 LogRedirect: Everywhere,
310 Hardened: true,
311 },
312 false,
313 },
314 {
315 "log redirect (always, cloudlogging, hardened): noerr",
316 LaunchPolicy{
317 AllowedLogRedirect: always,
318 },
319 LaunchSpec{
320 LogRedirect: CloudLogging,
321 Hardened: true,
322 },
323 false,
324 },
325 {
326 "log redirect (always, serial, hardened): noerr",
327 LaunchPolicy{
328 AllowedLogRedirect: always,
329 },
330 LaunchSpec{
331 LogRedirect: Serial,
332 Hardened: true,
333 },
334 false,
335 },
336 {
337 "log redirect (always, nowhere, hardened): noerr",
338 LaunchPolicy{
339 AllowedLogRedirect: always,
340 },
341 LaunchSpec{
342 LogRedirect: Nowhere,
343 Hardened: true,
344 },
345 false,
346 },
347 {
348 "log redirect (always, everywhere, debug): noerr",
349 LaunchPolicy{
350 AllowedLogRedirect: always,
351 },
352 LaunchSpec{
353 LogRedirect: Everywhere,
354 Hardened: false,
355 },
356 false,
357 },
358 {
359 "log redirect (always, cloudlogging, debug): noerr",
360 LaunchPolicy{
361 AllowedLogRedirect: always,
362 },
363 LaunchSpec{
364 LogRedirect: CloudLogging,
365 Hardened: false,
366 },
367 false,
368 },
369 {
370 "log redirect (always, serial, debug): noerr",
371 LaunchPolicy{
372 AllowedLogRedirect: always,
373 },
374 LaunchSpec{
375 LogRedirect: Serial,
376 Hardened: false,
377 },
378 false,
379 },
380 {
381 "log redirect (always, nowhere, debug): noerr",
382 LaunchPolicy{
383 AllowedLogRedirect: always,
384 },
385 LaunchSpec{
386 LogRedirect: Nowhere,
387 Hardened: false,
388 },
389 false,
390 },
391 {
392 "allowed mount dest",
393 LaunchPolicy{
394 AllowedMountDestinations: []string{"/a"},
395 },
396 LaunchSpec{
397 Mounts: []launchermount.Mount{
398 launchermount.TmpfsMount{Destination: "/a/b"},
399 },
400 },
401 false,
402 },
403 {
404 "allowed mount dest same dir",
405 LaunchPolicy{
406 AllowedMountDestinations: []string{"/a"},
407 },
408 LaunchSpec{
409 Mounts: []launchermount.Mount{
410 launchermount.TmpfsMount{Destination: "/a"},
411 },
412 },
413 false,
414 },
415 {
416 "allowed mount dest multiple",
417 LaunchPolicy{
418 AllowedMountDestinations: []string{"/a", "/b", "/c/d"},
419 },
420 LaunchSpec{
421 Mounts: []launchermount.Mount{
422 launchermount.TmpfsMount{Destination: "/a"},
423 launchermount.TmpfsMount{Destination: "/b"},
424 launchermount.TmpfsMount{Destination: "/c/d"},
425 launchermount.TmpfsMount{Destination: "/a/b"},
426 launchermount.TmpfsMount{Destination: "/a/b/c"},
427 launchermount.TmpfsMount{Destination: "/c/d/e"},
428 launchermount.TmpfsMount{Destination: "/c/d/f"},
429 launchermount.TmpfsMount{Destination: "/c/d/e/f/g/../b"},
430 launchermount.TmpfsMount{Destination: "/c/d/e/f/./../b"},
431 launchermount.TmpfsMount{Destination: "/c/d/e/f/./../../b"},
432 },
433 },
434 false,
435 },
436 {
437 "mount dest relative",
438 LaunchPolicy{
439 AllowedMountDestinations: []string{"/b"},
440 },
441 LaunchSpec{
442 Mounts: []launchermount.Mount{
443 launchermount.TmpfsMount{Destination: "/a/../b"},
444 },
445 },
446 false,
447 },
448 {
449 "mount dest not abs",
450 LaunchPolicy{
451 AllowedMountDestinations: []string{"/as"},
452 },
453 LaunchSpec{
454 Mounts: []launchermount.Mount{
455 launchermount.TmpfsMount{Destination: "asd"},
456 },
457 },
458 true,
459 },
460 {
461 "allowed mount dest not abs",
462 LaunchPolicy{
463 AllowedMountDestinations: []string{"as"},
464 },
465 LaunchSpec{
466 Mounts: []launchermount.Mount{
467 launchermount.TmpfsMount{Destination: "/asd"},
468 },
469 },
470 true,
471 },
472 {
473 "mount dest prefix but not subdir",
474 LaunchPolicy{
475 AllowedMountDestinations: []string{"/a"},
476 },
477 LaunchSpec{
478 Mounts: []launchermount.Mount{
479 launchermount.TmpfsMount{Destination: "/abcd"},
480 },
481 },
482 true,
483 },
484 {
485 "mount dest parent of allowed",
486 LaunchPolicy{
487 AllowedMountDestinations: []string{"/a/b"},
488 },
489 LaunchSpec{
490 Mounts: []launchermount.Mount{
491 launchermount.TmpfsMount{Destination: "/a"},
492 },
493 },
494 true,
495 },
496 {
497 "caps allowed unused",
498 LaunchPolicy{
499 PrivilegedCaps: true,
500 },
501 LaunchSpec{},
502 false,
503 },
504 {
505 "caps allowed and used",
506 LaunchPolicy{
507 PrivilegedCaps: true,
508 },
509 LaunchSpec{
510 AddedCapabilities: []string{"new", "no", "cap"},
511 },
512 false,
513 },
514 {
515 "caps not allowed but used",
516 LaunchPolicy{
517 PrivilegedCaps: false,
518 },
519 LaunchSpec{
520 AddedCapabilities: []string{"new", "no", "cap"},
521 },
522 true,
523 },
524 {
525 "caps allowed unset but used",
526 LaunchPolicy{},
527 LaunchSpec{
528 AddedCapabilities: []string{"new", "no", "cap"},
529 },
530 true,
531 },
532 {
533 "caps allowed unused",
534 LaunchPolicy{
535 AllowCgroups: true,
536 },
537 LaunchSpec{},
538 false,
539 },
540 {
541 "cgroups allowed and used",
542 LaunchPolicy{
543 AllowCgroups: true,
544 },
545 LaunchSpec{
546 CgroupNamespace: true,
547 },
548 false,
549 }, {
550 "cgroups not allowed but used",
551 LaunchPolicy{
552 AllowCgroups: false,
553 },
554 LaunchSpec{
555 CgroupNamespace: true,
556 },
557 true,
558 },
559 {
560 "caps allowed unset but used",
561 LaunchPolicy{},
562 LaunchSpec{
563 CgroupNamespace: true,
564 },
565 true,
566 },
567 }
568 for _, testCase := range testCases {
569 t.Run(testCase.testName, func(t *testing.T) {
570 err := testCase.policy.Verify(testCase.spec)
571 if testCase.expectErr {
572 if err == nil {
573 t.Errorf("expected error, but got nil")
574 }
575 } else {
576 if err != nil {
577 t.Errorf("expected no error, but got %v", err)
578 }
579 }
580 })
581 }
582 }
583
584 func TestVerifyMonitoringSettings(t *testing.T) {
585 testCases := []struct {
586 testName string
587 monitoring MonitoringType
588 spec LaunchSpec
589 }{
590 {
591 "none policy, disabled by spec",
592 None,
593 LaunchSpec{
594 MonitoringEnabled: None,
595 LogRedirect: Nowhere,
596 },
597 },
598 {
599 "memory-only policy, all disabled by spec",
600 MemoryOnly,
601 LaunchSpec{
602 MonitoringEnabled: None,
603 LogRedirect: Nowhere,
604 },
605 },
606 {
607 "memory-only policy, memory enabled by spec",
608 MemoryOnly,
609 LaunchSpec{
610 MonitoringEnabled: MemoryOnly,
611 LogRedirect: Nowhere,
612 },
613 },
614 {
615 "all enabled by policy, all enabled by spec",
616 All,
617 LaunchSpec{
618 MonitoringEnabled: All,
619 LogRedirect: Nowhere,
620 },
621 },
622 {
623 "all enabled by policy, disabled by spec",
624 All,
625 LaunchSpec{
626 MonitoringEnabled: None,
627 LogRedirect: Nowhere,
628 },
629 },
630 {
631 "all enabled by policy, memory enabled by spec",
632 All,
633 LaunchSpec{
634 MonitoringEnabled: MemoryOnly,
635 LogRedirect: Nowhere,
636 },
637 },
638 }
639
640 for _, testCase := range testCases {
641 // Debug.
642 t.Run("[Debug] "+testCase.testName, func(t *testing.T) {
643 policy := LaunchPolicy{
644 DebugImageMonitoring: testCase.monitoring,
645 }
646 if err := policy.Verify(testCase.spec); err != nil {
647 t.Errorf("expected no error, but got %v", err)
648 }
649 })
650
651 // Hardened.
652 t.Run("[Hardened] "+testCase.testName, func(t *testing.T) {
653 policy := LaunchPolicy{
654 HardenedImageMonitoring: testCase.monitoring,
655 }
656
657 // Copy the spec and set Hardened=true.
658 spec := testCase.spec
659 spec.Hardened = true
660 if err := policy.Verify(spec); err != nil {
661 t.Errorf("expected no error, but got %v", err)
662 }
663 })
664 }
665 }
666
667 func TestVerifyMonitoringSettingsErrors(t *testing.T) {
668 testCases := []struct {
669 testName string
670 monitoring MonitoringType
671 spec LaunchSpec
672 }{
673 {
674 "[Hardened] disabled policy, all enabled by spec",
675 None,
676 LaunchSpec{
677 MonitoringEnabled: All,
678 Hardened: true,
679 LogRedirect: Nowhere,
680 },
681 },
682 {
683 "[Hardened] disabled policy, memory enabled by spec",
684 None,
685 LaunchSpec{
686 MonitoringEnabled: MemoryOnly,
687 Hardened: true,
688 LogRedirect: Nowhere,
689 },
690 },
691 {
692 "[Hardened] memory-only policy, all enabled by spec",
693 MemoryOnly,
694 LaunchSpec{
695 MonitoringEnabled: All,
696 Hardened: true,
697 LogRedirect: Nowhere,
698 },
699 },
700 }
701
702 for _, testCase := range testCases {
703 t.Run(testCase.testName, func(t *testing.T) {
704 // Debug.
705 t.Run("[Debug] "+testCase.testName, func(t *testing.T) {
706 policy := LaunchPolicy{
707 DebugImageMonitoring: testCase.monitoring,
708 }
709 if err := policy.Verify(testCase.spec); err == nil {
710 t.Errorf("expected error, but got nil")
711 }
712 })
713
714 // Hardened.
715 t.Run("[Hardened] "+testCase.testName, func(t *testing.T) {
716 policy := LaunchPolicy{
717 HardenedImageMonitoring: testCase.monitoring,
718 }
719
720 // Copy the spec and set Hardened=true.
721 spec := testCase.spec
722 spec.Hardened = true
723 if err := policy.Verify(spec); err == nil {
724 t.Errorf("expected error, but got nil")
725 }
726 })
727 })
728 }
729 }
730
731 func TestIsHardened(t *testing.T) {
732 testCases := []struct {
733 testName string
734 kernelCmd string
735 expectHardened bool
736 }{
737 {
738 "empty kernel cmd",
739 "",
740 false,
741 },
742 {
743 "no confidential-space.hardened arg",
744 "BOOT_IMAGE=/syslinux/vmlinuz.B init=/usr/lib/systemd/systemd boot=local rootwait ro noresume loglevel=7 console=tty1 console=ttyS0 security=apparmor virtio_net.napi_tx=1 nmi_watchdog=0 csm.disabled=1 loadpin.exclude=kernel-module modules-load=loadpin_trigger module.sig_enforce=1 dm_verity.error_behavior=3 dm_verity.max_bios=-1 dm_verity.dev_wait=1 i915.modeset=1 cros_efi cos.protected_stateful_partition=e systemd.mask=update-engine.service ds=nocloud;s=/usr/share/oem/ cros_debug root=/dev/dm-0 \"dm=2 vroot none ro 1,0 4077568 verity payload=PARTUUID=DC7DB0DC-DDCC-AA45-BAE3-A41CA1698E83 hashtree=PARTUUID=DC7DB0DC-DDCC-AA45-BAE3-A41CA1698E83 hashstart=4077568 alg=sha256 root_hexdigest=6d5887660805db1b366319bd1c2161600d11b9e53f059b0e44b760a7277e1b0a salt=f4a41993832655a00d48f5769351370bebafd7de906df068bc1b1929b175ee43,oemroot none ro 1, 0 1024000 verity payload=PARTUUID=fd5af56a-7b25-c448-a616-19eb240b3260 hashtree=PARTUUID=fd5af56a-7b25-c448-a616-19eb240b3260 hashstart=1024000 alg=sha256 root_hexdigest=50c406c129054649a432fa144eeff56aa8b707d4c86f3ab44edde589356e8b23 salt=2a3461269a26ad6247f4b64cacd84f64e5a3311cd4b2f742bab6442291bf4977\"",
745 false,
746 },
747 {
748 "has kernel arg confidential-space.hardened=true",
749 "BOOT_IMAGE=/syslinux/vmlinuz.B init=/usr/lib/systemd/systemd boot=local rootwait ro noresume loglevel=7 console=tty1 console=ttyS0 security=apparmor virtio_net.napi_tx=1 nmi_watchdog=0 csm.disabled=1 loadpin.exclude=kernel-module modules-load=loadpin_trigger module.sig_enforce=1 dm_verity.error_behavior=3 dm_verity.max_bios=-1 dm_verity.dev_wait=1 i915.modeset=1 cros_efi confidential-space.hardened=true cos.protected_stateful_partition=e systemd.mask=update-engine.service ds=nocloud;s=/usr/share/oem/ cros_debug root=/dev/dm-0 \"dm=2 vroot none ro 1,0 4077568 verity payload=PARTUUID=DC7DB0DC-DDCC-AA45-BAE3-A41CA1698E83 hashtree=PARTUUID=DC7DB0DC-DDCC-AA45-BAE3-A41CA1698E83 hashstart=4077568 alg=sha256 root_hexdigest=6d5887660805db1b366319bd1c2161600d11b9e53f059b0e44b760a7277e1b0a salt=f4a41993832655a00d48f5769351370bebafd7de906df068bc1b1929b175ee43,oemroot none ro 1, 0 1024000 verity payload=PARTUUID=fd5af56a-7b25-c448-a616-19eb240b3260 hashtree=PARTUUID=fd5af56a-7b25-c448-a616-19eb240b3260 hashstart=1024000 alg=sha256 root_hexdigest=50c406c129054649a432fa144eeff56aa8b707d4c86f3ab44edde589356e8b23 salt=2a3461269a26ad6247f4b64cacd84f64e5a3311cd4b2f742bab6442291bf4977\"",
750 true,
751 },
752 {
753 "has kernel arg confidential-space.hardened=false",
754 "BOOT_IMAGE=/syslinux/vmlinuz.B init=/usr/lib/systemd/systemd boot=local rootwait ro noresume loglevel=7 console=tty1 console=ttyS0 security=apparmor virtio_net.napi_tx=1 nmi_watchdog=0 csm.disabled=1 loadpin.exclude=kernel-module modules-load=loadpin_trigger module.sig_enforce=1 dm_verity.error_behavior=3 dm_verity.max_bios=-1 dm_verity.dev_wait=1 i915.modeset=1 cros_efi confidential-space.hardened=false cos.protected_stateful_partition=e systemd.mask=update-engine.service ds=nocloud;s=/usr/share/oem/ cros_debug root=/dev/dm-0 \"dm=2 vroot none ro 1,0 4077568 verity payload=PARTUUID=DC7DB0DC-DDCC-AA45-BAE3-A41CA1698E83 hashtree=PARTUUID=DC7DB0DC-DDCC-AA45-BAE3-A41CA1698E83 hashstart=4077568 alg=sha256 root_hexdigest=6d5887660805db1b366319bd1c2161600d11b9e53f059b0e44b760a7277e1b0a salt=f4a41993832655a00d48f5769351370bebafd7de906df068bc1b1929b175ee43,oemroot none ro 1, 0 1024000 verity payload=PARTUUID=fd5af56a-7b25-c448-a616-19eb240b3260 hashtree=PARTUUID=fd5af56a-7b25-c448-a616-19eb240b3260 hashstart=1024000 alg=sha256 root_hexdigest=50c406c129054649a432fa144eeff56aa8b707d4c86f3ab44edde589356e8b23 salt=2a3461269a26ad6247f4b64cacd84f64e5a3311cd4b2f742bab6442291bf4977\"",
755 false,
756 },
757 }
758
759 for _, testCase := range testCases {
760 t.Run(testCase.testName, func(t *testing.T) {
761 hardened := isHardened(testCase.kernelCmd)
762 if testCase.expectHardened != hardened {
763 t.Errorf("expected %t, but got %t", testCase.expectHardened, hardened)
764 }
765 })
766 }
767 }
768
769 func TestGetMonitoringPolicy(t *testing.T) {
770 testcases := []struct {
771 name string
772 labels map[string]string
773 expectedPolicy *LaunchPolicy
774 }{
775 {
776 name: "memory_monitoring_allow=always",
777 labels: map[string]string{
778 memoryMonitoring: "always",
779 },
780 expectedPolicy: &LaunchPolicy{
781 HardenedImageMonitoring: MemoryOnly,
782 DebugImageMonitoring: MemoryOnly,
783 },
784 },
785 {
786 name: "memory_monitoring_allow=never",
787 labels: map[string]string{
788 memoryMonitoring: "never",
789 },
790 expectedPolicy: &LaunchPolicy{
791 HardenedImageMonitoring: None,
792 DebugImageMonitoring: None,
793 },
794 },
795 {
796 name: "memory_monitoring_allow=debugonly",
797 labels: map[string]string{
798 memoryMonitoring: "debugonly",
799 },
800 expectedPolicy: &LaunchPolicy{
801 HardenedImageMonitoring: None,
802 DebugImageMonitoring: MemoryOnly,
803 },
804 },
805 {
806 name: "HardenedImageMonitoring=none",
807 labels: map[string]string{
808 hardenedMonitoring: "none",
809 },
810 expectedPolicy: &LaunchPolicy{
811 HardenedImageMonitoring: None,
812 DebugImageMonitoring: MemoryOnly,
813 },
814 },
815 {
816 name: "HardenedImageMonitoring=memoryonly",
817 labels: map[string]string{
818 hardenedMonitoring: "memoryonly",
819 },
820 expectedPolicy: &LaunchPolicy{
821 HardenedImageMonitoring: MemoryOnly,
822 DebugImageMonitoring: MemoryOnly,
823 },
824 },
825 {
826 name: "HardenedImageMonitoring=all",
827 labels: map[string]string{
828 hardenedMonitoring: "all",
829 },
830 expectedPolicy: &LaunchPolicy{
831 HardenedImageMonitoring: All,
832 DebugImageMonitoring: MemoryOnly,
833 },
834 },
835 {
836 name: "DebugImageMonitoring=none",
837 labels: map[string]string{
838 debugMonitoring: "none",
839 },
840 expectedPolicy: &LaunchPolicy{
841 HardenedImageMonitoring: None,
842 DebugImageMonitoring: None,
843 },
844 },
845 {
846 name: "DebugImageMonitoring=memoryonly",
847 labels: map[string]string{
848 debugMonitoring: "memoryonly",
849 },
850 expectedPolicy: &LaunchPolicy{
851 HardenedImageMonitoring: None,
852 DebugImageMonitoring: MemoryOnly,
853 },
854 },
855 {
856 name: "DebugImageMonitoring=all",
857 labels: map[string]string{
858 debugMonitoring: "all",
859 },
860 expectedPolicy: &LaunchPolicy{
861 HardenedImageMonitoring: None,
862 DebugImageMonitoring: All,
863 },
864 },
865 // Set both fields to non-default values.
866 {
867 name: "HardenedImageMonitoring=all, DebugImageMonitoring=none",
868 labels: map[string]string{
869 hardenedMonitoring: "all",
870 debugMonitoring: "none",
871 },
872 expectedPolicy: &LaunchPolicy{
873 HardenedImageMonitoring: All,
874 DebugImageMonitoring: None,
875 },
876 },
877 }
878
879 for _, tc := range testcases {
880 t.Run(tc.name, func(t *testing.T) {
881 policy := &LaunchPolicy{}
882 if err := configureMonitoringPolicy(tc.labels, policy, logging.SimpleLogger()); err != nil {
883 t.Errorf("getMonitoringPolicy returned error: %v", err)
884 return
885 }
886
887 if !cmp.Equal(policy, tc.expectedPolicy) {
888 t.Errorf("getMonitoringPolicy did not return expected policy: got %v, want %v", policy, tc.expectedPolicy)
889 }
890 })
891 }
892 }
893
894 func TestGetMonitoringPolicyErrors(t *testing.T) {
895 testcases := []struct {
896 name string
897 labels map[string]string
898 }{
899 {
900 name: "memory_monitoring_allow and hardened_monitoring specified",
901 labels: map[string]string{
902 memoryMonitoring: "always",
903 hardenedMonitoring: "all",
904 },
905 },
906 {
907 name: "memory_monitoring_allow and debug_monitoring specified",
908 labels: map[string]string{
909 memoryMonitoring: "always",
910 debugMonitoring: "all",
911 },
912 },
913 {
914 name: "memory_monitoring_allow, hardened_monitoring, and debug_monitoring specified",
915 labels: map[string]string{
916 memoryMonitoring: "always",
917 hardenedMonitoring: "all",
918 debugMonitoring: "memoryOnly",
919 },
920 },
921 {
922 name: "invalid value for memory_monitoring_allow",
923 labels: map[string]string{
924 memoryMonitoring: "this is not valid",
925 },
926 },
927 {
928 name: "invalid value for hardened_monitoring",
929 labels: map[string]string{
930 hardenedMonitoring: "this is not valid",
931 },
932 },
933 {
934 name: "invalid value for debug_monitoring",
935 labels: map[string]string{
936 debugMonitoring: "this is not valid",
937 },
938 },
939 }
940
941 for _, tc := range testcases {
942 t.Run(tc.name, func(t *testing.T) {
943 policy := &LaunchPolicy{}
944 if err := configureMonitoringPolicy(tc.labels, policy, logging.SimpleLogger()); err == nil {
945 t.Errorf("Expected getMonitoringPolicy to return error, returned successfully with policy %v", policy)
946 }
947 })
948 }
949 }