| 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_run.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Lukas Puehringer <[email protected]> |
| 12 |
|
| 13 |
<Started> |
| 14 |
Nov 28, 2016 |
| 15 |
|
| 16 |
<Copyright> |
| 17 |
See LICENSE for licensing information. |
| 18 |
|
| 19 |
<Purpose> |
| 20 |
Test in_toto_run command line tool. |
| 21 |
|
| 22 |
""" |
| 23 |
|
| 24 |
import glob |
| 25 |
import os |
| 26 |
import tempfile |
| 27 |
import unittest |
| 28 |
from pathlib import Path |
| 29 |
from unittest import mock |
| 30 |
|
| 31 |
import securesystemslib.interface # pylint: disable=unused-import |
| 32 |
|
| 33 |
from in_toto.in_toto_run import main as in_toto_run_main |
| 34 |
from in_toto.models.link import FILENAME_FORMAT |
| 35 |
from in_toto.models.metadata import Metablock, Metadata |
| 36 |
from tests.common import CliTestCase, GenKeysMixin, GPGKeysMixin, TmpDirMixin |
| 37 |
|
| 38 |
|
| 39 |
class TestInTotoRunTool(CliTestCase, TmpDirMixin, GPGKeysMixin, GenKeysMixin): |
| 40 |
"""Test in_toto_run's main() - requires sys.argv patching; and |
| 41 |
in_toto_run- calls runlib and error logs/exits on Exception.""" |
| 42 |
|
| 43 |
cli_main_func = staticmethod(in_toto_run_main) |
| 44 |
|
| 45 |
@classmethod |
| 46 |
def setUpClass(self): |
| 47 |
"""Create and change into temporary directory, |
| 48 |
generate key pair, dummy artifact and base arguments.""" |
| 49 |
self.set_up_test_dir() |
| 50 |
self.set_up_gpg_keys() |
| 51 |
self.set_up_keys() |
| 52 |
|
| 53 |
self.test_step = "test_step" |
| 54 |
self.test_link_rsa = FILENAME_FORMAT.format( |
| 55 |
step_name=self.test_step, keyid=self.rsa_key_id |
| 56 |
) |
| 57 |
self.test_link_ed25519 = FILENAME_FORMAT.format( |
| 58 |
step_name=self.test_step, keyid=self.ed25519_key_id |
| 59 |
) |
| 60 |
self.test_link_ecdsa = FILENAME_FORMAT.format( |
| 61 |
step_name=self.test_step, keyid=self.ecdsa_key_id |
| 62 |
) |
| 63 |
self.test_link_rsa_enc = FILENAME_FORMAT.format( |
| 64 |
step_name=self.test_step, keyid=self.rsa_key_enc_id |
| 65 |
) |
| 66 |
self.test_link_ed25519_enc = FILENAME_FORMAT.format( |
| 67 |
step_name=self.test_step, keyid=self.ed25519_key_enc_id |
| 68 |
) |
| 69 |
self.test_link_ecdsa_enc = FILENAME_FORMAT.format( |
| 70 |
step_name=self.test_step, keyid=self.ecdsa_key_enc_id |
| 71 |
) |
| 72 |
|
| 73 |
self.test_artifact = "test_artifact" |
| 74 |
Path(self.test_artifact).touch() |
| 75 |
|
| 76 |
@classmethod |
| 77 |
def tearDownClass(self): |
| 78 |
self.tear_down_test_dir() |
| 79 |
|
| 80 |
def tearDown(self): |
| 81 |
for link in glob.glob("*.link"): |
| 82 |
os.remove(link) |
| 83 |
|
| 84 |
def test_main_required_args(self): |
| 85 |
"""Test CLI command with required arguments.""" |
| 86 |
|
| 87 |
args = [ |
| 88 |
"--step-name", |
| 89 |
self.test_step, |
| 90 |
"--key", |
| 91 |
self.rsa_key_path, |
| 92 |
"--", |
| •93 |
"python3", |
| 94 |
"--version", |
| 95 |
] |
| 96 |
|
| 97 |
self.assert_cli_sys_exit(args, 0) |
| 98 |
self.assertTrue(os.path.exists(self.test_link_rsa)) |
| 99 |
|
| 100 |
def test_main_optional_args(self): |
| 101 |
"""Test CLI command with optional arguments.""" |
| 102 |
|
| 103 |
named_args = [ |
| 104 |
"--step-name", |
| 105 |
self.test_step, |
| 106 |
"--key", |
| 107 |
self.rsa_key_path, |
| 108 |
"--materials", |
| 109 |
self.test_artifact, |
| 110 |
"--products", |
| 111 |
self.test_artifact, |
| 112 |
"--record-streams", |
| 113 |
] |
| •114 |
positional_args = ["--", "python3", "--version"] |
| 115 |
|
| 116 |
# Test and assert recorded artifacts |
| 117 |
args1 = named_args + positional_args |
| 118 |
self.assert_cli_sys_exit(args1, 0) |
| 119 |
link_metadata = Metablock.load(self.test_link_rsa) |
| 120 |
self.assertTrue( |
| 121 |
self.test_artifact in list(link_metadata.signed.materials.keys()) |
| 122 |
) |
| 123 |
self.assertTrue( |
| 124 |
self.test_artifact in list(link_metadata.signed.products.keys()) |
| 125 |
) |
| 126 |
|
| 127 |
# Test and assert exlcuded artifacts |
| 128 |
args2 = named_args + ["--exclude", "*test*"] + positional_args |
| 129 |
self.assert_cli_sys_exit(args2, 0) |
| 130 |
link_metadata = Metablock.load(self.test_link_rsa) |
| 131 |
self.assertFalse(link_metadata.signed.materials) |
| 132 |
self.assertFalse(link_metadata.signed.products) |
| 133 |
|
| 134 |
# Test with base path |
| 135 |
args3 = named_args + ["--base-path", self.test_dir] + positional_args |
| 136 |
self.assert_cli_sys_exit(args3, 0) |
| 137 |
link_metadata = Metablock.load(self.test_link_rsa) |
| 138 |
self.assertListEqual( |
| 139 |
list(link_metadata.signed.materials.keys()), [self.test_artifact] |
| 140 |
) |
| 141 |
self.assertListEqual( |
| 142 |
list(link_metadata.signed.products.keys()), [self.test_artifact] |
| 143 |
) |
| 144 |
|
| 145 |
# Test with bogus base path |
| 146 |
args4 = named_args + ["--base-path", "bogus/path"] + positional_args |
| 147 |
self.assert_cli_sys_exit(args4, 1) |
| 148 |
|
| 149 |
# Test with lstrip path |
| 150 |
strip_prefix = self.test_artifact[:-1] |
| 151 |
args5 = named_args + ["--lstrip-paths", strip_prefix] + positional_args |
| 152 |
self.assert_cli_sys_exit(args5, 0) |
| 153 |
link_metadata = Metablock.load(self.test_link_rsa) |
| 154 |
self.assertListEqual( |
| 155 |
list(link_metadata.signed.materials.keys()), |
| 156 |
[self.test_artifact[len(strip_prefix) :]], |
| 157 |
) |
| 158 |
self.assertListEqual( |
| 159 |
list(link_metadata.signed.products.keys()), |
| 160 |
[self.test_artifact[len(strip_prefix) :]], |
| 161 |
) |
| 162 |
|
| 163 |
def test_main_with_metadata_directory(self): |
| 164 |
"""Test CLI command with metadata directory.""" |
| 165 |
tmp_dir = os.path.realpath(tempfile.mkdtemp(dir=os.getcwd())) |
| 166 |
args = [ |
| 167 |
"--step-name", |
| 168 |
self.test_step, |
| 169 |
"--key", |
| 170 |
self.rsa_key_path, |
| 171 |
"--metadata-directory", |
| 172 |
tmp_dir, |
| 173 |
"--", |
| •174 |
"python3", |
| 175 |
"--version", |
| 176 |
] |
| 177 |
|
| 178 |
self.assert_cli_sys_exit(args, 0) |
| 179 |
|
| 180 |
linkpath = os.path.join(tmp_dir, self.test_link_rsa) |
| 181 |
|
| 182 |
self.assertTrue(os.path.exists(linkpath)) |
| 183 |
|
| 184 |
def test_main_with_unencrypted_ed25519_key(self): |
| 185 |
"""Test CLI command with ed25519 key.""" |
| 186 |
args = [ |
| 187 |
"-n", |
| 188 |
self.test_step, |
| 189 |
"--key", |
| 190 |
self.ed25519_key_path, |
| 191 |
"--key-type", |
| 192 |
"ed25519", |
| 193 |
"--", |
| 194 |
"ls", |
| 195 |
] |
| 196 |
|
| 197 |
self.assert_cli_sys_exit(args, 0) |
| 198 |
self.assertTrue(os.path.exists(self.test_link_ed25519)) |
| 199 |
|
| 200 |
def test_main_with_unencrypted_ecdsa_key(self): |
| 201 |
"""Test CLI command with ecdsa key.""" |
| 202 |
args = [ |
| 203 |
"-n", |
| 204 |
self.test_step, |
| 205 |
"--key", |
| 206 |
self.ecdsa_key_path, |
| 207 |
"--key-type", |
| 208 |
"ecdsa", |
| 209 |
"--", |
| 210 |
"ls", |
| 211 |
] |
| 212 |
|
| 213 |
self.assert_cli_sys_exit(args, 0) |
| 214 |
self.assertTrue(os.path.exists(self.test_link_ecdsa)) |
| 215 |
|
| 216 |
def test_main_with_encrypted_keys(self): |
| 217 |
"""Test CLI command with encrypted ed25519 key.""" |
| 218 |
|
| 219 |
for key_type, key_path, link_path in [ |
| 220 |
("rsa", self.rsa_key_enc_path, self.test_link_rsa_enc), |
| 221 |
("ed25519", self.ed25519_key_enc_path, self.test_link_ed25519_enc), |
| 222 |
("ecdsa", self.ecdsa_key_enc_path, self.test_link_ecdsa_enc), |
| 223 |
]: |
| 224 |
# Define common arguments passed to in in-toto-run below |
| 225 |
args = [ |
| 226 |
"-n", |
| 227 |
self.test_step, |
| 228 |
"--key", |
| 229 |
key_path, |
| 230 |
"--key-type", |
| 231 |
key_type, |
| 232 |
] |
| •233 |
cmd = ["--", "python3", "--version"] |
| 234 |
|
| 235 |
# Make sure the link file to be generated doesn't already exist |
| 236 |
self.assertFalse(os.path.exists(link_path)) |
| 237 |
|
| 238 |
# Test 1: Call in-toto-run entering signing key password on prompt |
| 239 |
with mock.patch( |
| 240 |
"securesystemslib.interface.get_password", |
| 241 |
return_value=self.key_pw, |
| 242 |
): |
| 243 |
self.assert_cli_sys_exit(args + ["--password"] + cmd, 0) |
| 244 |
|
| 245 |
self.assertTrue(os.path.exists(link_path)) |
| 246 |
os.remove(link_path) |
| 247 |
|
| 248 |
# Test 2: Call in-toto-run passing signing key password |
| 249 |
self.assert_cli_sys_exit( |
| 250 |
args + ["--password", self.key_pw] + cmd, 0 |
| 251 |
) |
| 252 |
self.assertTrue(os.path.exists(link_path)) |
| 253 |
os.remove(link_path) |
| 254 |
|
| 255 |
def test_main_with_specified_gpg_key(self): |
| 256 |
"""Test CLI command with specified gpg key.""" |
| 257 |
args = [ |
| 258 |
"-n", |
| 259 |
self.test_step, |
| 260 |
"--gpg", |
| 261 |
self.gpg_key_85DA58, |
| 262 |
"--gpg-home", |
| 263 |
self.gnupg_home, |
| 264 |
"--", |
| •265 |
"python3", |
| 266 |
"--version", |
| 267 |
] |
| 268 |
|
| 269 |
self.assert_cli_sys_exit(args, 0) |
| 270 |
link_filename = FILENAME_FORMAT.format( |
| 271 |
step_name=self.test_step, keyid=self.gpg_key_85DA58 |
| 272 |
) |
| 273 |
|
| 274 |
self.assertTrue(os.path.exists(link_filename)) |
| 275 |
|
| 276 |
def test_main_with_default_gpg_key(self): |
| 277 |
"""Test CLI command with default gpg key.""" |
| 278 |
args = [ |
| 279 |
"-n", |
| 280 |
self.test_step, |
| 281 |
"--gpg", |
| 282 |
"--gpg-home", |
| 283 |
self.gnupg_home, |
| 284 |
"--", |
| •285 |
"python3", |
| 286 |
"--version", |
| 287 |
] |
| 288 |
|
| 289 |
self.assert_cli_sys_exit(args, 0) |
| 290 |
|
| 291 |
link_filename = FILENAME_FORMAT.format( |
| 292 |
step_name=self.test_step, keyid=self.gpg_key_D924E9 |
| 293 |
) |
| 294 |
|
| 295 |
self.assertTrue(os.path.exists(link_filename)) |
| 296 |
|
| 297 |
def test_main_no_command_arg(self): |
| 298 |
"""Test CLI command with --no-command argument.""" |
| 299 |
|
| 300 |
args = [ |
| 301 |
"in_toto_run.py", |
| 302 |
"--step-name", |
| 303 |
self.test_step, |
| 304 |
"--key", |
| 305 |
self.rsa_key_path, |
| 306 |
"--no-command", |
| 307 |
] |
| 308 |
|
| 309 |
self.assert_cli_sys_exit(args, 0) |
| 310 |
|
| 311 |
self.assertTrue(os.path.exists(self.test_link_rsa)) |
| 312 |
|
| 313 |
def test_main_wrong_args(self): |
| 314 |
"""Test CLI command with missing arguments.""" |
| 315 |
|
| 316 |
wrong_args_list = [ |
| 317 |
[], |
| 318 |
["--step-name", "some"], |
| 319 |
["--key", self.rsa_key_path], |
| 320 |
["--", "echo", "blub"], |
| 321 |
["--step-name", "test-step", "--key", self.rsa_key_path], |
| 322 |
["--step-name", "--", "echo", "blub"], |
| 323 |
["--key", self.rsa_key_path, "--", "echo", "blub"], |
| 324 |
["--step-name", "test-step", "--key", self.rsa_key_path, "--"], |
| 325 |
[ |
| 326 |
"--step-name", |
| 327 |
"test-step", |
| 328 |
"--key", |
| 329 |
self.rsa_key_path, |
| 330 |
"--gpg", |
| 331 |
"--", |
| 332 |
"echo", |
| 333 |
"blub", |
| 334 |
], |
| 335 |
] |
| 336 |
|
| 337 |
for wrong_args in wrong_args_list: |
| 338 |
self.assert_cli_sys_exit(wrong_args, 2) |
| 339 |
self.assertFalse(os.path.exists(self.test_link_rsa)) |
| 340 |
|
| 341 |
def test_main_wrong_key_exits(self): |
| 342 |
"""Test CLI command with wrong key argument, exits and logs error""" |
| 343 |
|
| 344 |
args = [ |
| 345 |
"--step-name", |
| 346 |
self.test_step, |
| 347 |
"--key", |
| 348 |
"non-existing-key", |
| 349 |
"--", |
| 350 |
"echo", |
| 351 |
"test", |
| 352 |
] |
| 353 |
|
| 354 |
self.assert_cli_sys_exit(args, 1) |
| 355 |
self.assertFalse(os.path.exists(self.test_link_rsa)) |
| 356 |
|
| 357 |
def test_main_encrypted_key_but_no_pw(self): |
| 358 |
"""Test CLI command exits 1 with encrypted key but no pw.""" |
| 359 |
args = ["-n", self.test_step, "--key", self.rsa_key_enc_path, "-x"] |
| 360 |
self.assert_cli_sys_exit(args, 1) |
| 361 |
self.assertFalse(os.path.exists(self.test_link_rsa_enc)) |
| 362 |
|
| 363 |
args = ["-n", self.test_step, "--key", self.ed25519_key_enc_path, "-x"] |
| 364 |
self.assert_cli_sys_exit(args, 1) |
| 365 |
self.assertFalse(os.path.exists(self.test_link_ed25519_enc)) |
| 366 |
|
| 367 |
|
| 368 |
class TestInTotoRunToolWithDSSE( |
| 369 |
CliTestCase, TmpDirMixin, GPGKeysMixin, GenKeysMixin |
| 370 |
): |
| 371 |
"""Test in_toto_run's main() with --use-dsse argument - requires sys.argv |
| 372 |
patching; and in_toto_run- calls runlib and error logs/exits on Exception. |
| 373 |
""" |
| 374 |
|
| 375 |
cli_main_func = staticmethod(in_toto_run_main) |
| 376 |
|
| 377 |
@classmethod |
| 378 |
def setUpClass(self): |
| 379 |
"""Create and change into temporary directory, |
| 380 |
generate key pair, dummy artifact and base arguments.""" |
| 381 |
self.set_up_test_dir() |
| 382 |
self.set_up_gpg_keys() |
| 383 |
self.set_up_keys() |
| 384 |
|
| 385 |
self.test_step = "test_step" |
| 386 |
self.test_link_rsa = FILENAME_FORMAT.format( |
| 387 |
step_name=self.test_step, keyid=self.rsa_key_id |
| 388 |
) |
| 389 |
self.test_link_ed25519 = FILENAME_FORMAT.format( |
| 390 |
step_name=self.test_step, keyid=self.ed25519_key_id |
| 391 |
) |
| 392 |
self.test_link_rsa_enc = FILENAME_FORMAT.format( |
| 393 |
step_name=self.test_step, keyid=self.rsa_key_enc_id |
| 394 |
) |
| 395 |
self.test_link_ed25519_enc = FILENAME_FORMAT.format( |
| 396 |
step_name=self.test_step, keyid=self.ed25519_key_enc_id |
| 397 |
) |
| 398 |
|
| 399 |
self.test_artifact = "test_artifact" |
| 400 |
Path(self.test_artifact).touch() |
| 401 |
|
| 402 |
@classmethod |
| 403 |
def tearDownClass(self): |
| 404 |
self.tear_down_test_dir() |
| 405 |
|
| 406 |
def tearDown(self): |
| 407 |
for link in glob.glob("*.link"): |
| 408 |
os.remove(link) |
| 409 |
|
| 410 |
def test_main_required_args(self): |
| 411 |
"""Test CLI command with required arguments.""" |
| 412 |
|
| 413 |
args = [ |
| 414 |
"--step-name", |
| 415 |
self.test_step, |
| 416 |
"--key", |
| 417 |
self.rsa_key_path, |
| 418 |
"--use-dsse", |
| 419 |
"--", |
| •420 |
"python3", |
| 421 |
"--version", |
| 422 |
] |
| 423 |
|
| 424 |
self.assert_cli_sys_exit(args, 0) |
| 425 |
self.assertTrue(os.path.exists(self.test_link_rsa)) |
| 426 |
|
| 427 |
def test_main_optional_args(self): |
| 428 |
"""Test CLI command with optional arguments.""" |
| 429 |
|
| 430 |
named_args = [ |
| 431 |
"--step-name", |
| 432 |
self.test_step, |
| 433 |
"--key", |
| 434 |
self.rsa_key_path, |
| 435 |
"--materials", |
| 436 |
self.test_artifact, |
| 437 |
"--products", |
| 438 |
self.test_artifact, |
| 439 |
"--record-streams", |
| 440 |
"--use-dsse", |
| 441 |
] |
| •442 |
positional_args = ["--", "python3", "--version"] |
| 443 |
|
| 444 |
# Test and assert recorded artifacts |
| 445 |
args1 = named_args + positional_args |
| 446 |
self.assert_cli_sys_exit(args1, 0) |
| 447 |
metadata = Metadata.load(self.test_link_rsa) |
| 448 |
link = metadata.get_payload() |
| 449 |
self.assertTrue(self.test_artifact in list(link.materials.keys())) |
| 450 |
self.assertTrue(self.test_artifact in list(link.products.keys())) |
| 451 |
|
| 452 |
# Test and assert exlcuded artifacts |
| 453 |
args2 = named_args + ["--exclude", "*test*"] + positional_args |
| 454 |
self.assert_cli_sys_exit(args2, 0) |
| 455 |
link = Metadata.load(self.test_link_rsa).get_payload() |
| 456 |
self.assertFalse(link.materials) |
| 457 |
self.assertFalse(link.products) |
| 458 |
|
| 459 |
# Test with base path |
| 460 |
args3 = named_args + ["--base-path", self.test_dir] + positional_args |
| 461 |
self.assert_cli_sys_exit(args3, 0) |
| 462 |
link = Metadata.load(self.test_link_rsa).get_payload() |
| 463 |
self.assertListEqual(list(link.materials.keys()), [self.test_artifact]) |
| 464 |
self.assertListEqual(list(link.products.keys()), [self.test_artifact]) |
| 465 |
|
| 466 |
# Test with bogus base path |
| 467 |
args4 = named_args + ["--base-path", "bogus/path"] + positional_args |
| 468 |
self.assert_cli_sys_exit(args4, 1) |
| 469 |
|
| 470 |
# Test with lstrip path |
| 471 |
strip_prefix = self.test_artifact[:-1] |
| 472 |
args5 = named_args + ["--lstrip-paths", strip_prefix] + positional_args |
| 473 |
self.assert_cli_sys_exit(args5, 0) |
| 474 |
link = Metadata.load(self.test_link_rsa).get_payload() |
| 475 |
self.assertListEqual( |
| 476 |
list(link.materials.keys()), |
| 477 |
[self.test_artifact[len(strip_prefix) :]], |
| 478 |
) |
| 479 |
self.assertListEqual( |
| 480 |
list(link.products.keys()), |
| 481 |
[self.test_artifact[len(strip_prefix) :]], |
| 482 |
) |
| 483 |
|
| 484 |
def test_main_with_default_gpg_key(self): |
| 485 |
"""Test CLI command with default gpg key.""" |
| 486 |
args = [ |
| 487 |
"-n", |
| 488 |
self.test_step, |
| 489 |
"--gpg", |
| 490 |
"--gpg-home", |
| 491 |
self.gnupg_home, |
| 492 |
"--use-dsse", |
| 493 |
"--", |
| •494 |
"python3", |
| 495 |
"--version", |
| 496 |
] |
| 497 |
|
| 498 |
self.assert_cli_sys_exit(args, 1) |
| 499 |
|
| 500 |
def test_main_no_command_arg(self): |
| 501 |
"""Test CLI command with --no-command argument.""" |
| 502 |
|
| 503 |
args = [ |
| 504 |
"in_toto_run.py", |
| 505 |
"--step-name", |
| 506 |
self.test_step, |
| 507 |
"--key", |
| 508 |
self.rsa_key_path, |
| 509 |
"--no-command", |
| 510 |
"--use-dsse", |
| 511 |
] |
| 512 |
|
| 513 |
self.assert_cli_sys_exit(args, 0) |
| 514 |
|
| 515 |
self.assertTrue(os.path.exists(self.test_link_rsa)) |
| 516 |
|
| 517 |
|
| 518 |
if __name__ == "__main__": |
| 519 |
unittest.main() |