Debian Codegraph @

tests/test_in_toto_verify.py

raw
1 #!/usr/bin/env python
2
3 # Copyright New York University and the in-toto contributors
4 # SPDX-License-Identifier: Apache-2.0
5
6 """
7 <Program Name>
8 test_in_toto_verify.py
9
10 <Author>
11 Lukas Puehringer <[email protected]>
12
13 <Started>
14 Jan 9, 2017
15
16 <Copyright>
17 See LICENSE for licensing information.
18
19 <Purpose>
20 Test in_toto_verify command line tool.
21
22 """
23
24 import os
25 import shutil
26 import unittest
27
28 from securesystemslib.gpg.constants import have_gpg
29 from securesystemslib.interface import (
30 import_ed25519_privatekey_from_file,
31 import_rsa_privatekey_from_file,
32 )
33 from securesystemslib.signer import SSlibSigner
34
35 from in_toto.in_toto_verify import main as in_toto_verify_main
36 from in_toto.models.metadata import Metadata
37 from tests.common import CliTestCase, GPGKeysMixin, TmpDirMixin
38
39
40 class TestInTotoVerifyTool(CliTestCase, TmpDirMixin):
41 """
42 Tests
43 - in_toto_verify's main() - requires sys.argv patching;
44 - in_toto_verify - calls verifylib.in_toto_verify and error logs/exits
45 in case of a raised Exception.
46
47 Uses in-toto demo supply chain link metadata files and basic layout for
48 verification:
49
50 Copies the basic layout for different test scenarios:
51 - signed layout
52 - multiple signed layout (using two project owner keys)
53 """
54
55 cli_main_func = staticmethod(in_toto_verify_main)
56
57 @classmethod
58 def setUpClass(self):
59 """Creates and changes into temporary directory.
60 Copies demo files to temp dir...
61 - owner/functionary key pairs
62 - *.link metadata files
63 - layout template (not signed, no expiration date)
64 - final product
65
66 ...and dumps various layouts for different test scenarios
67 """
68
69 # Find demo files
70 demo_files = os.path.join(
71 os.path.dirname(os.path.realpath(__file__)), "demo_files"
72 )
73 # find where the scripts directory is located.
74 scripts_directory = os.path.join(
75 os.path.dirname(os.path.realpath(__file__)), "scripts"
76 )
77
78 self.set_up_test_dir()
79
80 # Copy demo files to temp dir
81 for fn in os.listdir(demo_files):
82 shutil.copy(os.path.join(demo_files, fn), self.test_dir)
83
84 shutil.copytree(scripts_directory, "scripts")
85
86 # Load layout template
87 layout_template = Metadata.load("demo.layout.template")
88
89 # Store layout paths to be used in tests
90 self.layout_single_signed_path = "single-signed.layout"
91 self.layout_double_signed_path = "double-signed.layout"
92
93 # Import layout signing keys
94 alice = import_rsa_privatekey_from_file("alice")
95 bob = import_rsa_privatekey_from_file("bob")
96 self.alice_path = "alice.pub"
97 self.bob_path = "bob.pub"
98
99 # dump a single signed layout
100 layout_template.sign(alice)
101 layout_template.dump(self.layout_single_signed_path)
102 # dump a double signed layout
103 layout_template.sign(bob)
104 layout_template.dump(self.layout_double_signed_path)
105
106 @classmethod
107 def tearDownClass(self):
108 self.tear_down_test_dir()
109
110 def test_main_required_args(self):
111 """Test in-toto-verify CLI tool with required arguments."""
112 args = [
113 "--layout",
114 self.layout_single_signed_path,
115 "--layout-keys",
116 self.alice_path,
117 ]
118
119 self.assert_cli_sys_exit(args, 0)
120
121 def test_main_wrong_args(self):
122 """Test in-toto-verify CLI tool with wrong arguments."""
123 wrong_args_list = [
124 [],
125 ["--layout", self.layout_single_signed_path],
126 ["--key", self.alice_path],
127 ]
128
129 for wrong_args in wrong_args_list:
130 self.assert_cli_sys_exit(wrong_args, 2)
131
132 def test_main_multiple_keys(self):
133 """Test in-toto-verify CLI tool with multiple keys."""
134 args = [
135 "--layout",
136 self.layout_double_signed_path,
137 "--layout-keys",
138 self.alice_path,
139 self.bob_path,
140 ]
141 self.assert_cli_sys_exit(args, 0)
142
143 def test_main_failing_bad_layout_path(self):
144 """Test in-toto-verify CLI tool with bad layout path."""
145 args = ["-l", "not-a-path-to-a-layout", "-k", self.alice_path]
146 self.assert_cli_sys_exit(args, 1)
147
148 def test_main_link_dir(self):
149 """Test in-toto-verify CLI tool with explicit link dir."""
150
151 # Use current working directory explicitly to load links
152 args = [
153 "--layout",
154 self.layout_single_signed_path,
155 "--layout-keys",
156 self.alice_path,
157 "--link-dir",
158 ".",
159 ]
160 self.assert_cli_sys_exit(args, 0)
161
162 # Fail with an explicit link directory, where no links are found
163 args = [
164 "--layout",
165 self.layout_single_signed_path,
166 "--layout-keys",
167 self.alice_path,
168 "--link-dir",
169 "bad-link-dir",
170 ]
171 self.assert_cli_sys_exit(args, 1)
172
173
174 class TestInTotoVerifyToolWithDSSE(CliTestCase, TmpDirMixin):
175 """
176 Tests
177 - in_toto_verify's main() - requires sys.argv patching;
178 - in_toto_verify - calls verifylib.in_toto_verify and error logs/exits
179 in case of a raised Exception.
180
181 Uses in-toto demo supply chain link metadata files and basic layout for
182 verification:
183
184 Copies the basic layout for different test scenarios:
185 - signed layout
186 - multiple signed layout (using two project owner keys)
187 """
188
189 cli_main_func = staticmethod(in_toto_verify_main)
190
191 @classmethod
192 def setUpClass(self):
193 """Creates and changes into temporary directory.
194 Copies demo files to temp dir...
195 - owner/functionary key pairs
196 - *.link metadata files
197 - layout template (not signed, no expiration date)
198 - final product
199
200 ...and dumps various layouts for different test scenarios
201 """
202
203 # Find demo files
204 demo_files = os.path.join(
205 os.path.dirname(os.path.realpath(__file__)), "demo_files"
206 )
207
208 # Demo DSSE Metadata Files
209 demo_dsse_files = os.path.join(
210 os.path.dirname(os.path.realpath(__file__)), "demo_dsse_files"
211 )
212
213 # find where the scripts directory is located.
214 scripts_directory = os.path.join(
215 os.path.dirname(os.path.realpath(__file__)), "scripts"
216 )
217
218 self.set_up_test_dir()
219
220 # Copy demo files to temp dir
221 for fn in os.listdir(demo_files):
222 shutil.copy(os.path.join(demo_files, fn), self.test_dir)
223
224 for fn in os.listdir(demo_dsse_files):
225 shutil.copy(os.path.join(demo_dsse_files, fn), self.test_dir)
226
227 shutil.copytree(scripts_directory, "scripts")
228
229 # Load layout template
230 layout_template = Metadata.load("demo.layout.template")
231
232 # Store layout paths to be used in tests
233 self.layout_single_signed_path = "single-signed.layout"
234 self.layout_double_signed_path = "double-signed.layout"
235
236 # Import layout signing keys
237 alice = import_rsa_privatekey_from_file("alice")
238 bob = import_rsa_privatekey_from_file("bob")
239 self.alice_path = "alice.pub"
240 self.bob_path = "bob.pub"
241
242 # dump a single signed layout
243 layout_template.create_signature(SSlibSigner(alice))
244 layout_template.dump(self.layout_single_signed_path)
245 # dump a double signed layout
246 layout_template.create_signature(SSlibSigner(bob))
247 layout_template.dump(self.layout_double_signed_path)
248
249 @classmethod
250 def tearDownClass(self):
251 self.tear_down_test_dir()
252
253 def test_main_required_args(self):
254 """Test in-toto-verify CLI tool with required arguments."""
255 args = [
256 "--layout",
257 self.layout_single_signed_path,
258 "--layout-keys",
259 self.alice_path,
260 ]
261
262 self.assert_cli_sys_exit(args, 0)
263
264 def test_main_multiple_keys(self):
265 """Test in-toto-verify CLI tool with multiple keys."""
266 args = [
267 "--layout",
268 self.layout_double_signed_path,
269 "--layout-keys",
270 self.alice_path,
271 self.bob_path,
272 ]
273 self.assert_cli_sys_exit(args, 0)
274
275 def test_main_link_dir(self):
276 """Test in-toto-verify CLI tool with explicit link dir."""
277
278 # Use current working directory explicitly to load links
279 args = [
280 "--layout",
281 self.layout_single_signed_path,
282 "--layout-keys",
283 self.alice_path,
284 "--link-dir",
285 ".",
286 ]
287 self.assert_cli_sys_exit(args, 0)
288
289 # Fail with an explicit link directory, where no links are found
290 args = [
291 "--layout",
292 self.layout_single_signed_path,
293 "--layout-keys",
294 self.alice_path,
295 "--link-dir",
296 "bad-link-dir",
297 ]
298 self.assert_cli_sys_exit(args, 1)
299
300
301 class TestInTotoVerifyToolMixedKeys(CliTestCase, TmpDirMixin):
302 """Tests in-toto-verify like TestInTotoVerifyTool but with
303 both rsa and ed25519 project owner and functionary keys."""
304
305 cli_main_func = staticmethod(in_toto_verify_main)
306
307 @classmethod
308 def setUpClass(self):
309 """Creates and changes into temporary directory.
310 Copies demo files to temp dir...
311 - owner/functionary key pairs
312 - *.link metadata files
313 - layout template (not signed, no expiration date)
314 - final product
315
316 ...and dumps layout for test scenario
317 """
318 # Find demo files
319 demo_files = os.path.join(
320 os.path.dirname(os.path.realpath(__file__)), "demo_files"
321 )
322
323 scripts_directory = os.path.join(
324 os.path.dirname(os.path.realpath(__file__)), "scripts"
325 )
326
327 self.set_up_test_dir()
328
329 # Copy demo files to temp dir
330 for fn in os.listdir(demo_files):
331 shutil.copy(os.path.join(demo_files, fn), self.test_dir)
332
333 shutil.copytree(scripts_directory, "scripts")
334
335 # Load layout template
336 layout_template = Metadata.load("demo.layout.template")
337
338 # Store layout paths to be used in tests
339 self.layout_double_signed_path = "double-signed.layout"
340
341 # Import layout signing keys
342 alice = import_rsa_privatekey_from_file("alice")
343 danny = import_ed25519_privatekey_from_file("danny")
344 self.alice_path = "alice.pub"
345 self.danny_path = "danny.pub"
346
347 # dump a double signed layout
348 layout_template.sign(alice)
349 layout_template.sign(danny)
350 layout_template.dump(self.layout_double_signed_path)
351
352 @classmethod
353 def tearDownClass(self):
354 self.tear_down_test_dir()
355
356 def test_main_multiple_keys(self):
357 """Test in-toto-verify CLI tool with multiple keys."""
358 args = [
359 "--layout",
360 self.layout_double_signed_path,
361 "--layout-keys",
362 self.alice_path,
363 self.danny_path,
364 "--key-types",
365 "rsa",
366 "ed25519",
367 ]
368 self.assert_cli_sys_exit(args, 0)
369
370
371 class TestInTotoVerifyToolMixedKeysWithDSSE(CliTestCase, TmpDirMixin):
372 """Tests in-toto-verify like TestInTotoVerifyTool but with
373 both rsa and ed25519 project owner and functionary keys."""
374
375 cli_main_func = staticmethod(in_toto_verify_main)
376
377 @classmethod
378 def setUpClass(self):
379 """Creates and changes into temporary directory.
380 Copies demo files to temp dir...
381 - owner/functionary key pairs
382 - *.link metadata files
383 - layout template (not signed, no expiration date)
384 - final product
385
386 ...and dumps layout for test scenario
387 """
388 # Find demo files
389 demo_files = os.path.join(
390 os.path.dirname(os.path.realpath(__file__)), "demo_files"
391 )
392
393 # Demo DSSE Metadata Files
394 demo_dsse_files = os.path.join(
395 os.path.dirname(os.path.realpath(__file__)), "demo_dsse_files"
396 )
397
398 # find where the scripts directory is located.
399 scripts_directory = os.path.join(
400 os.path.dirname(os.path.realpath(__file__)), "scripts"
401 )
402
403 self.set_up_test_dir()
404
405 # Copy demo files to temp dir
406 for fn in os.listdir(demo_files):
407 shutil.copy(os.path.join(demo_files, fn), self.test_dir)
408
409 for fn in os.listdir(demo_dsse_files):
410 shutil.copy(os.path.join(demo_dsse_files, fn), self.test_dir)
411
412 shutil.copytree(scripts_directory, "scripts")
413
414 # Load layout template
415 layout_template = Metadata.load("demo.layout.template")
416
417 # Store layout paths to be used in tests
418 self.layout_double_signed_path = "double-signed.layout"
419
420 # Import layout signing keys
421 alice = import_rsa_privatekey_from_file("alice")
422 danny = import_ed25519_privatekey_from_file("danny")
423 self.alice_path = "alice.pub"
424 self.danny_path = "danny.pub"
425
426 # dump a double signed layout
427 layout_template.create_signature(SSlibSigner(alice))
428 layout_template.create_signature(SSlibSigner(danny))
429 layout_template.dump(self.layout_double_signed_path)
430
431 @classmethod
432 def tearDownClass(self):
433 self.tear_down_test_dir()
434
435 def test_main_multiple_keys(self):
436 """Test in-toto-verify CLI tool with multiple keys."""
437 args = [
438 "--layout",
439 self.layout_double_signed_path,
440 "--layout-keys",
441 self.alice_path,
442 self.danny_path,
443 "--key-types",
444 "rsa",
445 "ed25519",
446 ]
447 self.assert_cli_sys_exit(args, 0)
448
449
450 @unittest.skipIf(not have_gpg(), "gpg not found")
451 class TestInTotoVerifyToolGPG(CliTestCase, TmpDirMixin, GPGKeysMixin):
452 """Tests in-toto-verify like TestInTotoVerifyTool but with
453 gpg project owner and functionary keys."""
454
455 cli_main_func = staticmethod(in_toto_verify_main)
456
457 @classmethod
458 def setUpClass(self):
459 """Copy test gpg rsa keyring, gpg demo metadata files and demo final
460 product to tmp test dir."""
461 # Copy gpg demo metadata files
462 demo_files = os.path.join(
463 os.path.dirname(os.path.realpath(__file__)), "demo_files_gpg"
464 )
465
466 # find where the scripts directory is located.
467 scripts_directory = os.path.join(
468 os.path.dirname(os.path.realpath(__file__)), "scripts"
469 )
470
471 self.set_up_test_dir()
472 self.set_up_gpg_keys()
473
474 for fn in os.listdir(demo_files):
475 shutil.copy(os.path.join(demo_files, fn), self.test_dir)
476
477 # Change into test dir
478 shutil.copytree(scripts_directory, "scripts")
479
480 # Sign layout template with gpg key
481 layout_template = Metadata.load("demo.layout.template")
482
483 self.layout_path = "gpg_signed.layout"
484 layout_template.sign_gpg(self.gpg_key_0C8A17, self.gnupg_home)
485 layout_template.dump(self.layout_path)
486
487 @classmethod
488 def tearDownClass(self):
489 self.tear_down_test_dir()
490
491 def test_gpg_signed_layout_with_gpg_functionary_keys(self):
492 """Successfully test demo supply chain where the layout lists gpg keys
493 as functionary keys and is signed with a gpg key."""
494 args = [
495 "--layout",
496 self.layout_path,
497 "--gpg",
498 self.gpg_key_0C8A17,
499 "--gpg-home",
500 self.gnupg_home,
501 ]
502
503 self.assert_cli_sys_exit(args, 0)
504
505
506 if __name__ == "__main__":
507 unittest.main()