| 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_record.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_record command line tool. |
| 21 |
|
| 22 |
""" |
| 23 |
|
| 24 |
import os |
| 25 |
import tempfile |
| 26 |
import unittest |
| 27 |
from pathlib import Path |
| 28 |
from unittest import mock |
| 29 |
|
| 30 |
import securesystemslib.interface # pylint: disable=unused-import |
| 31 |
|
| 32 |
from in_toto.in_toto_record import main as in_toto_record_main |
| 33 |
from in_toto.models.link import UNFINISHED_FILENAME_FORMAT |
| 34 |
from tests.common import CliTestCase, GenKeysMixin, GPGKeysMixin, TmpDirMixin |
| 35 |
|
| 36 |
|
| 37 |
class TestInTotoRecordTool( |
| 38 |
CliTestCase, TmpDirMixin, GPGKeysMixin, GenKeysMixin |
| 39 |
): |
| 40 |
"""Test in_toto_record's main() - requires sys.argv patching; and |
| 41 |
in_toto_record_start/in_toto_record_stop - calls runlib and error logs/exits |
| 42 |
on Exception.""" |
| 43 |
|
| 44 |
cli_main_func = staticmethod(in_toto_record_main) |
| 45 |
|
| 46 |
@classmethod |
| 47 |
def setUpClass(self): |
| 48 |
"""Create and change into temporary directory, |
| 49 |
generate key pair, dummy artifact and base arguments.""" |
| 50 |
self.set_up_test_dir() |
| 51 |
self.set_up_gpg_keys() |
| 52 |
self.set_up_keys() |
| 53 |
|
| 54 |
self.test_artifact1 = "test_artifact1" |
| 55 |
self.test_artifact2 = "test_artifact2" |
| 56 |
Path(self.test_artifact1).touch() |
| 57 |
Path(self.test_artifact2).touch() |
| 58 |
|
| 59 |
@classmethod |
| 60 |
def tearDownClass(self): |
| 61 |
self.tear_down_test_dir() |
| 62 |
|
| 63 |
def test_start_stop(self): |
| 64 |
"""Test CLI command record start/stop with various arguments.""" |
| 65 |
|
| 66 |
# Start/stop recording using rsa key |
| 67 |
args = ["--step-name", "test1", "--key", self.rsa_key_path] |
| 68 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 69 |
self.assert_cli_sys_exit(["stop"] + args, 0) |
| 70 |
|
| 71 |
# Start/stop recording using encrypted rsa key with password on prompt |
| 72 |
args = [ |
| 73 |
"--step-name", |
| 74 |
"test1.1", |
| 75 |
"--key", |
| 76 |
self.rsa_key_enc_path, |
| 77 |
"--password", |
| 78 |
] |
| 79 |
with mock.patch( |
| 80 |
"securesystemslib.interface.get_password", return_value=self.key_pw |
| 81 |
): |
| 82 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 83 |
self.assert_cli_sys_exit(["stop"] + args, 0) |
| 84 |
|
| 85 |
# Start/stop recording using encrypted rsa key passing the pw |
| 86 |
args = [ |
| 87 |
"--step-name", |
| 88 |
"test1.2", |
| 89 |
"--key", |
| 90 |
self.rsa_key_enc_path, |
| 91 |
"--password", |
| 92 |
self.key_pw, |
| 93 |
] |
| 94 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 95 |
self.assert_cli_sys_exit(["stop"] + args, 0) |
| 96 |
|
| 97 |
# Start/stop with recording one artifact using rsa key |
| 98 |
args = ["--step-name", "test2", "--key", self.rsa_key_path] |
| 99 |
self.assert_cli_sys_exit( |
| 100 |
["start"] + args + ["--materials", self.test_artifact1], 0 |
| 101 |
) |
| 102 |
self.assert_cli_sys_exit( |
| 103 |
["stop"] + args + ["--products", self.test_artifact1], 0 |
| 104 |
) |
| 105 |
|
| 106 |
# Start/stop with excluding one artifact using rsa key |
| 107 |
args = ["--step-name", "test2.5", "--key", self.rsa_key_path] |
| 108 |
self.assert_cli_sys_exit( |
| 109 |
["start"] |
| 110 |
+ args |
| 111 |
+ ["--materials", self.test_artifact1, "--exclude", "test*"], |
| 112 |
0, |
| 113 |
) |
| 114 |
self.assert_cli_sys_exit( |
| 115 |
["stop"] |
| 116 |
+ args |
| 117 |
+ ["--products", self.test_artifact1, "--exclude", "test*"], |
| 118 |
0, |
| 119 |
) |
| 120 |
|
| 121 |
# Start/stop with base-path using rsa key |
| 122 |
args = [ |
| 123 |
"--step-name", |
| 124 |
"test2.6", |
| 125 |
"--key", |
| 126 |
self.rsa_key_path, |
| 127 |
"--base-path", |
| 128 |
self.test_dir, |
| 129 |
] |
| 130 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 131 |
self.assert_cli_sys_exit(["stop"] + args, 0) |
| 132 |
|
| 133 |
# Start/stop with recording multiple artifacts using rsa key |
| 134 |
args = ["--step-name", "test3", "--key", self.rsa_key_path] |
| 135 |
self.assert_cli_sys_exit( |
| 136 |
["start"] |
| 137 |
+ args |
| 138 |
+ ["--materials", self.test_artifact1, self.test_artifact2], |
| 139 |
0, |
| 140 |
) |
| 141 |
self.assert_cli_sys_exit( |
| 142 |
["stop"] |
| 143 |
+ args |
| 144 |
+ ["--products", self.test_artifact2, self.test_artifact2], |
| 145 |
0, |
| 146 |
) |
| 147 |
|
| 148 |
# Start/stop recording using ed25519 key |
| 149 |
args = [ |
| 150 |
"--step-name", |
| 151 |
"test4", |
| 152 |
"--key", |
| 153 |
self.ed25519_key_path, |
| 154 |
"--key-type", |
| 155 |
"ed25519", |
| 156 |
] |
| 157 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 158 |
self.assert_cli_sys_exit(["stop"] + args, 0) |
| 159 |
|
| 160 |
# Start/stop with encrypted ed25519 key entering password on the prompt |
| 161 |
args = [ |
| 162 |
"--step-name", |
| 163 |
"test4.1", |
| 164 |
"--key", |
| 165 |
self.ed25519_key_enc_path, |
| 166 |
"--key-type", |
| 167 |
"ed25519", |
| 168 |
"--password", |
| 169 |
] |
| 170 |
with mock.patch( |
| 171 |
"securesystemslib.interface.get_password", return_value=self.key_pw |
| 172 |
): |
| 173 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 174 |
self.assert_cli_sys_exit(["stop"] + args, 0) |
| 175 |
|
| 176 |
# Start/stop with encrypted ed25519 key passing the password |
| 177 |
args = [ |
| 178 |
"--step-name", |
| 179 |
"test4.2", |
| 180 |
"--key", |
| 181 |
self.ed25519_key_enc_path, |
| 182 |
"--key-type", |
| 183 |
"ed25519", |
| 184 |
"--password", |
| 185 |
self.key_pw, |
| 186 |
] |
| 187 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 188 |
self.assert_cli_sys_exit(["stop"] + args, 0) |
| 189 |
|
| 190 |
# Start/stop with recording one artifact using ed25519 key |
| 191 |
args = [ |
| 192 |
"--step-name", |
| 193 |
"test5", |
| 194 |
"--key", |
| 195 |
self.ed25519_key_path, |
| 196 |
"--key-type", |
| 197 |
"ed25519", |
| 198 |
] |
| 199 |
self.assert_cli_sys_exit( |
| 200 |
["start"] + args + ["--materials", self.test_artifact1], 0 |
| 201 |
) |
| 202 |
self.assert_cli_sys_exit( |
| 203 |
["stop"] + args + ["--products", self.test_artifact1], 0 |
| 204 |
) |
| 205 |
|
| 206 |
# Start/stop with excluding one artifact using ed25519 key |
| 207 |
args = [ |
| 208 |
"--step-name", |
| 209 |
"test5.5", |
| 210 |
"--key", |
| 211 |
self.ed25519_key_path, |
| 212 |
"--key-type", |
| 213 |
"ed25519", |
| 214 |
] |
| 215 |
self.assert_cli_sys_exit( |
| 216 |
["start"] |
| 217 |
+ args |
| 218 |
+ ["--materials", self.test_artifact1, "--exclude", "test*"], |
| 219 |
0, |
| 220 |
) |
| 221 |
self.assert_cli_sys_exit( |
| 222 |
["stop"] |
| 223 |
+ args |
| 224 |
+ ["--products", self.test_artifact1, "--exclude", "test*"], |
| 225 |
0, |
| 226 |
) |
| 227 |
|
| 228 |
# Start/stop with base-path using ed25519 key |
| 229 |
args = [ |
| 230 |
"--step-name", |
| 231 |
"test5.6", |
| 232 |
"--key", |
| 233 |
self.ed25519_key_path, |
| 234 |
"--key-type", |
| 235 |
"ed25519", |
| 236 |
"--base-path", |
| 237 |
self.test_dir, |
| 238 |
] |
| 239 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 240 |
self.assert_cli_sys_exit(["stop"] + args, 0) |
| 241 |
|
| 242 |
# Start/stop with recording multiple artifacts using ed25519 key |
| 243 |
args = [ |
| 244 |
"--step-name", |
| 245 |
"test6", |
| 246 |
"--key", |
| 247 |
self.ed25519_key_path, |
| 248 |
"--key-type", |
| 249 |
"ed25519", |
| 250 |
] |
| 251 |
self.assert_cli_sys_exit( |
| 252 |
["start"] |
| 253 |
+ args |
| 254 |
+ ["--materials", self.test_artifact1, self.test_artifact2], |
| 255 |
0, |
| 256 |
) |
| 257 |
self.assert_cli_sys_exit( |
| 258 |
["stop"] |
| 259 |
+ args |
| 260 |
+ ["--products", self.test_artifact2, self.test_artifact2], |
| 261 |
0, |
| 262 |
) |
| 263 |
|
| 264 |
# Start/stop sign with specified gpg keyid |
| 265 |
args = [ |
| 266 |
"--step-name", |
| 267 |
"test7", |
| 268 |
"--gpg", |
| 269 |
self.gpg_key_768C43, |
| 270 |
"--gpg-home", |
| 271 |
self.gnupg_home, |
| 272 |
] |
| 273 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 274 |
self.assert_cli_sys_exit(["stop"] + args, 0) |
| 275 |
|
| 276 |
# Start/stop sign with default gpg keyid |
| 277 |
args = ["--step-name", "test8", "--gpg", "--gpg-home", self.gnupg_home] |
| 278 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 279 |
self.assert_cli_sys_exit(["stop"] + args, 0) |
| 280 |
|
| 281 |
# Start/stop sign with metadata directory |
| 282 |
args = ["--step-name", "test9", "--key", self.rsa_key_path] |
| 283 |
tmp_dir = os.path.realpath(tempfile.mkdtemp(dir=os.getcwd())) |
| 284 |
metadata_directory_arg = ["--metadata-directory", tmp_dir] |
| 285 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 286 |
self.assert_cli_sys_exit(["stop"] + metadata_directory_arg + args, 0) |
| 287 |
|
| 288 |
def test_glob_no_unfinished_files(self): |
| 289 |
"""Test record stop with missing unfinished files when globbing (gpg).""" |
| 290 |
args = [ |
| 291 |
"--step-name", |
| 292 |
"test-no-glob", |
| 293 |
"--gpg", |
| 294 |
self.gpg_key_768C43, |
| 295 |
"--gpg-home", |
| 296 |
self.gnupg_home, |
| 297 |
] |
| 298 |
self.assert_cli_sys_exit(["stop"] + args, 1) |
| 299 |
|
| 300 |
def test_glob_to_many_unfinished_files(self): |
| 301 |
"""Test record stop with to many unfinished files when globbing (gpg).""" |
| 302 |
name = "test-to-many-glob" |
| 303 |
fn1 = UNFINISHED_FILENAME_FORMAT.format( |
| 304 |
step_name=name, keyid="a12345678" |
| 305 |
) |
| 306 |
fn2 = UNFINISHED_FILENAME_FORMAT.format( |
| 307 |
step_name=name, keyid="b12345678" |
| 308 |
) |
| 309 |
Path(fn1).touch() |
| 310 |
Path(fn2).touch() |
| 311 |
args = [ |
| 312 |
"--step-name", |
| 313 |
name, |
| 314 |
"--gpg", |
| 315 |
self.gpg_key_768C43, |
| 316 |
"--gpg-home", |
| 317 |
self.gnupg_home, |
| 318 |
] |
| 319 |
self.assert_cli_sys_exit(["stop"] + args, 1) |
| 320 |
|
| 321 |
def test_encrypted_key_but_no_pw(self): |
| 322 |
args = ["--step-name", "enc-key", "--key", self.rsa_key_enc_path] |
| 323 |
self.assert_cli_sys_exit(["start"] + args, 1) |
| 324 |
self.assert_cli_sys_exit(["stop"] + args, 1) |
| 325 |
|
| 326 |
args = [ |
| 327 |
"--step-name", |
| 328 |
"enc-key", |
| 329 |
"--key", |
| 330 |
self.ed25519_key_enc_path, |
| 331 |
"--key-type", |
| 332 |
"ed25519", |
| 333 |
] |
| 334 |
self.assert_cli_sys_exit(["start"] + args, 1) |
| 335 |
self.assert_cli_sys_exit(["stop"] + args, 1) |
| 336 |
|
| 337 |
def test_wrong_key(self): |
| 338 |
"""Test CLI command record with wrong key exits 1""" |
| 339 |
args = ["--step-name", "wrong-key", "--key", "non-existing-key"] |
| 340 |
self.assert_cli_sys_exit(["start"] + args, 1) |
| 341 |
self.assert_cli_sys_exit(["stop"] + args, 1) |
| 342 |
|
| 343 |
def test_no_key(self): |
| 344 |
"""Test if no key is specified, argparse error exists with 2""" |
| 345 |
args = ["--step-name", "no-key"] |
| 346 |
self.assert_cli_sys_exit(["start"] + args, 2) |
| 347 |
self.assert_cli_sys_exit(["stop"] + args, 2) |
| 348 |
|
| 349 |
def test_missing_unfinished_link(self): |
| 350 |
"""Error exit with missing unfinished link file.""" |
| 351 |
args = ["--step-name", "no-link", "--key", self.rsa_key_path] |
| 352 |
self.assert_cli_sys_exit(["stop"] + args, 1) |
| 353 |
|
| 354 |
args = [ |
| 355 |
"--step-name", |
| 356 |
"no-link", |
| 357 |
"--key", |
| 358 |
self.ed25519_key_path, |
| 359 |
"--key-type", |
| 360 |
"ed25519", |
| 361 |
] |
| 362 |
self.assert_cli_sys_exit(["stop"] + args, 1) |
| 363 |
|
| 364 |
|
| 365 |
class TestInTotoRecordToolWithDSSE( |
| 366 |
CliTestCase, TmpDirMixin, GPGKeysMixin, GenKeysMixin |
| 367 |
): |
| 368 |
"""Test in_toto_record's main() with --use-dsse argument - requires sys.argv |
| 369 |
patching; and in_toto_record_start/in_toto_record_stop - calls runlib and |
| 370 |
error logs/exits on Exception.""" |
| 371 |
|
| 372 |
cli_main_func = staticmethod(in_toto_record_main) |
| 373 |
|
| 374 |
@classmethod |
| 375 |
def setUpClass(self): |
| 376 |
"""Create and change into temporary directory, |
| 377 |
generate key pair, dummy artifact and base arguments.""" |
| 378 |
self.set_up_test_dir() |
| 379 |
self.set_up_keys() |
| 380 |
|
| 381 |
self.test_artifact1 = "test_artifact1" |
| 382 |
self.test_artifact2 = "test_artifact2" |
| 383 |
Path(self.test_artifact1).touch() |
| 384 |
Path(self.test_artifact2).touch() |
| 385 |
|
| 386 |
@classmethod |
| 387 |
def tearDownClass(self): |
| 388 |
self.tear_down_test_dir() |
| 389 |
|
| 390 |
def test_start_stop(self): |
| 391 |
"""Test CLI command record start/stop with various arguments.""" |
| 392 |
|
| 393 |
# Start/stop recording using rsa key |
| 394 |
args = [ |
| 395 |
"--step-name", |
| 396 |
"test1", |
| 397 |
"--key", |
| 398 |
self.rsa_key_path, |
| 399 |
"--use-dsse", |
| 400 |
] |
| 401 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 402 |
self.assert_cli_sys_exit(["stop"] + args, 0) |
| 403 |
|
| 404 |
# Start/stop with recording one artifact using rsa key |
| 405 |
args = [ |
| 406 |
"--step-name", |
| 407 |
"test2", |
| 408 |
"--key", |
| 409 |
self.rsa_key_path, |
| 410 |
"--use-dsse", |
| 411 |
] |
| 412 |
self.assert_cli_sys_exit( |
| 413 |
["start"] + args + ["--materials", self.test_artifact1], 0 |
| 414 |
) |
| 415 |
self.assert_cli_sys_exit( |
| 416 |
["stop"] + args + ["--products", self.test_artifact1], 0 |
| 417 |
) |
| 418 |
|
| 419 |
# Start/stop with excluding one artifact using rsa key |
| 420 |
args = [ |
| 421 |
"--step-name", |
| 422 |
"test2.5", |
| 423 |
"--key", |
| 424 |
self.rsa_key_path, |
| 425 |
"--use-dsse", |
| 426 |
] |
| 427 |
self.assert_cli_sys_exit( |
| 428 |
["start"] |
| 429 |
+ args |
| 430 |
+ ["--materials", self.test_artifact1, "--exclude", "test*"], |
| 431 |
0, |
| 432 |
) |
| 433 |
self.assert_cli_sys_exit( |
| 434 |
["stop"] |
| 435 |
+ args |
| 436 |
+ ["--products", self.test_artifact1, "--exclude", "test*"], |
| 437 |
0, |
| 438 |
) |
| 439 |
|
| 440 |
# Start/stop with base-path using rsa key |
| 441 |
args = [ |
| 442 |
"--step-name", |
| 443 |
"test2.6", |
| 444 |
"--key", |
| 445 |
self.rsa_key_path, |
| 446 |
"--base-path", |
| 447 |
self.test_dir, |
| 448 |
"--use-dsse", |
| 449 |
] |
| 450 |
self.assert_cli_sys_exit(["start"] + args, 0) |
| 451 |
self.assert_cli_sys_exit(["stop"] + args, 0) |
| 452 |
|
| 453 |
# Start/stop with recording multiple artifacts using rsa key |
| 454 |
args = [ |
| 455 |
"--step-name", |
| 456 |
"test3", |
| 457 |
"--key", |
| 458 |
self.rsa_key_path, |
| 459 |
"--use-dsse", |
| 460 |
] |
| 461 |
self.assert_cli_sys_exit( |
| 462 |
["start"] |
| 463 |
+ args |
| 464 |
+ ["--materials", self.test_artifact1, self.test_artifact2], |
| 465 |
0, |
| 466 |
) |
| 467 |
self.assert_cli_sys_exit( |
| 468 |
["stop"] |
| 469 |
+ args |
| 470 |
+ ["--products", self.test_artifact2, self.test_artifact2], |
| 471 |
0, |
| 472 |
) |
| 473 |
|
| 474 |
|
| 475 |
if __name__ == "__main__": |
| 476 |
unittest.main() |