| 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_verifylib.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Lukas Puehringer <[email protected]> |
| 12 |
|
| 13 |
<Started> |
| 14 |
Nov 07, 2016 |
| 15 |
|
| 16 |
<Copyright> |
| 17 |
See LICENSE for licensing information. |
| 18 |
|
| 19 |
<Purpose> |
| 20 |
Test verifylib functions. |
| 21 |
|
| 22 |
""" |
| 23 |
|
| 24 |
import copy |
| 25 |
import glob |
| 26 |
import os |
| 27 |
import shlex |
| 28 |
import shutil |
| 29 |
import tempfile |
| 30 |
import unittest |
| 31 |
from datetime import datetime |
| 32 |
from unittest.mock import patch |
| 33 |
|
| 34 |
import securesystemslib.exceptions |
| 35 |
import securesystemslib.gpg.functions |
| 36 |
from dateutil.relativedelta import relativedelta |
| 37 |
from securesystemslib.gpg.constants import have_gpg |
| 38 |
from securesystemslib.interface import ( |
| 39 |
import_publickeys_from_file, |
| 40 |
import_rsa_privatekey_from_file, |
| 41 |
import_rsa_publickey_from_file, |
| 42 |
) |
| 43 |
|
| 44 |
import in_toto.exceptions |
| 45 |
import in_toto.settings |
| 46 |
from in_toto.exceptions import ( |
| 47 |
BadReturnValueError, |
| 48 |
LayoutExpiredError, |
| 49 |
RuleVerificationError, |
| 50 |
SignatureVerificationError, |
| 51 |
ThresholdVerificationError, |
| 52 |
) |
| 53 |
from in_toto.models.layout import ( |
| 54 |
SUBLAYOUT_LINK_DIR_FORMAT, |
| 55 |
Inspection, |
| 56 |
Layout, |
| 57 |
Step, |
| 58 |
) |
| 59 |
from in_toto.models.link import FILENAME_FORMAT, Link |
| 60 |
from in_toto.models.metadata import Metablock |
| 61 |
from in_toto.rulelib import unpack_rule |
| 62 |
from in_toto.verifylib import ( |
| 63 |
_raise_on_bad_retval, |
| 64 |
get_summary_link, |
| 65 |
in_toto_verify, |
| 66 |
load_links_for_layout, |
| 67 |
run_all_inspections, |
| 68 |
verify_all_item_rules, |
| 69 |
verify_allow_rule, |
| 70 |
verify_command_alignment, |
| 71 |
verify_create_rule, |
| 72 |
verify_delete_rule, |
| 73 |
verify_disallow_rule, |
| 74 |
verify_item_rules, |
| 75 |
verify_link_signature_thresholds, |
| 76 |
verify_match_rule, |
| 77 |
verify_modify_rule, |
| 78 |
verify_require_rule, |
| 79 |
verify_sublayouts, |
| 80 |
verify_threshold_constraints, |
| 81 |
) |
| 82 |
from tests.common import GPGKeysMixin, TmpDirMixin |
| 83 |
|
| 84 |
|
| 85 |
class Test_RaiseOnBadRetval(unittest.TestCase): |
| 86 |
"""Tests internal function that raises an exception if the passed |
| 87 |
"return_value" is not and integer and not zero.""" |
| 88 |
|
| 89 |
def test_zero_return_value(self): |
| 90 |
"""Don't raise exception on zero return value.""" |
| 91 |
_raise_on_bad_retval(0) |
| 92 |
_raise_on_bad_retval(0, "command") |
| 93 |
|
| 94 |
def test_non_int_return_value(self): |
| 95 |
"""Raise exception on non-int return value.""" |
| 96 |
with self.assertRaises(BadReturnValueError): |
| 97 |
_raise_on_bad_retval("bad retval") |
| 98 |
with self.assertRaises(BadReturnValueError): |
| 99 |
_raise_on_bad_retval("bad retval", "bad command") |
| 100 |
|
| 101 |
def test_non_zero_return_value(self): |
| 102 |
"""Raise exception on non-zero return value.""" |
| 103 |
with self.assertRaises(BadReturnValueError): |
| 104 |
_raise_on_bad_retval(1) |
| 105 |
with self.assertRaises(BadReturnValueError): |
| 106 |
_raise_on_bad_retval(-1, "bad command") |
| 107 |
|
| 108 |
|
| 109 |
class TestRunAllInspections(unittest.TestCase, TmpDirMixin): |
| 110 |
"""Test verifylib.run_all_inspections(layout, persist_inspection_links)""" |
| 111 |
|
| 112 |
@classmethod |
| 113 |
def setUpClass(self): |
| 114 |
""" |
| 115 |
Create layout with dummy inpsection. |
| 116 |
Create and change into temp test directory with dummy artifact.""" |
| 117 |
|
| 118 |
# find where the scripts directory is located. |
| 119 |
scripts_directory = os.path.join( |
| 120 |
os.path.dirname(os.path.realpath(__file__)), "scripts" |
| 121 |
) |
| 122 |
|
| 123 |
# Create layout with one inspection |
| 124 |
self.layout = Layout.read( |
| 125 |
{ |
| 126 |
"_type": "layout", |
| 127 |
"steps": [], |
| 128 |
"inspect": [ |
| 129 |
{ |
| 130 |
"name": "touch-bar", |
| 131 |
"run": [ |
| •132 |
"python3", |
| 133 |
os.path.join(scripts_directory, "touch"), |
| 134 |
"bar", |
| 135 |
], |
| 136 |
} |
| 137 |
], |
| 138 |
} |
| 139 |
) |
| 140 |
|
| 141 |
# Create directory where the verification will take place |
| 142 |
self.set_up_test_dir() |
| 143 |
with open("foo", "w", encoding="utf8") as f: |
| 144 |
f.write("foo") |
| 145 |
|
| 146 |
@classmethod |
| 147 |
def tearDownClass(self): |
| 148 |
self.tear_down_test_dir() |
| 149 |
|
| 150 |
def test_inpsection_artifacts_with_base_path_ignored(self): |
| 151 |
"""Create new dummy test dir and set as base path, must ignore.""" |
| 152 |
ignore_dir = os.path.realpath(tempfile.mkdtemp()) |
| 153 |
ignore_foo = os.path.join(ignore_dir, "ignore_foo") |
| 154 |
with open(ignore_foo, "w", encoding="utf8") as f: |
| 155 |
f.write("ignore foo") |
| 156 |
in_toto.settings.ARTIFACT_BASE_PATH = ignore_dir |
| 157 |
|
| 158 |
run_all_inspections(self.layout, True) |
| 159 |
link = Metablock.load("touch-bar.link") |
| 160 |
self.assertListEqual(list(link.signed.materials.keys()), ["foo"]) |
| 161 |
self.assertListEqual( |
| 162 |
sorted(list(link.signed.products.keys())), sorted(["foo", "bar"]) |
| 163 |
) |
| 164 |
|
| 165 |
in_toto.settings.ARTIFACT_BASE_PATH = None |
| 166 |
shutil.rmtree(ignore_dir) |
| 167 |
|
| 168 |
def test_inspection_fail_with_non_zero_retval(self): |
| 169 |
"""Test fail run inspections with non-zero return value.""" |
| 170 |
layout = Layout.read( |
| 171 |
{ |
| 172 |
"_type": "layout", |
| 173 |
"steps": [], |
| 174 |
"inspect": [ |
| 175 |
{ |
| 176 |
"name": "non-zero-inspection", |
| •177 |
"run": ["python3", "./scripts/expr", "1", "/", "0"], |
| 178 |
} |
| 179 |
], |
| 180 |
} |
| 181 |
) |
| 182 |
with self.assertRaises(BadReturnValueError): |
| 183 |
run_all_inspections(layout, True) |
| 184 |
|
| 185 |
def test_inspection_persistence_false(self): |
| 186 |
"""Test metadata link file non-persistence""" |
| 187 |
if os.path.exists("touch-bar.link"): |
| 188 |
os.remove("touch-bar.link") |
| 189 |
run_all_inspections(self.layout, False) |
| 190 |
self.assertFalse(os.path.exists("touch-bar.link")) |
| 191 |
|
| 192 |
def test_inspeciton_persistence_true(self): |
| 193 |
run_all_inspections(self.layout, True) |
| 194 |
self.assertTrue(os.path.exists("touch-bar.link")) |
| 195 |
|
| 196 |
|
| 197 |
class TestVerifyCommandAlignment(unittest.TestCase): |
| 198 |
"""Test verifylib.verify_command_alignment(command, expected_command)""" |
| 199 |
|
| 200 |
def setUp(self): |
| 201 |
self.command = ["vi", "file1", "file2"] |
| 202 |
|
| 203 |
def test_commands_align(self): |
| 204 |
"""Cmd and expected cmd are equal, passes.""" |
| 205 |
expected_command = ["vi", "file1", "file2"] |
| 206 |
verify_command_alignment(self.command, expected_command) |
| 207 |
|
| 208 |
def test_commands_do_not_fully_align_log_warning(self): |
| 209 |
"""Cmd and expected cmd differ slightly.""" |
| 210 |
expected_command = ["/usr/bin/vi", "file1", "file2"] |
| 211 |
|
| 212 |
with patch("in_toto.verifylib.LOG") as mock_logging: |
| 213 |
verify_command_alignment(self.command, expected_command) |
| 214 |
mock_logging.warning.assert_called_with( |
| 215 |
"Run command '{0}'" |
| 216 |
" differs from expected command '{1}'".format( |
| 217 |
self.command, expected_command |
| 218 |
) |
| 219 |
) |
| 220 |
|
| 221 |
def test_commands_do_not_align_at_all_log_warning(self): |
| 222 |
"""Cmd and expected cmd differ completely.""" |
| 223 |
expected_command = ["make install"] |
| 224 |
|
| 225 |
with patch("in_toto.verifylib.LOG") as mock_logging: |
| 226 |
verify_command_alignment(self.command, expected_command) |
| 227 |
mock_logging.warning.assert_called_with( |
| 228 |
"Run command '{0}'" |
| 229 |
" differs from expected command '{1}'".format( |
| 230 |
self.command, expected_command |
| 231 |
) |
| 232 |
) |
| 233 |
|
| 234 |
|
| 235 |
class TestVerifyRule(unittest.TestCase): |
| 236 |
"""Table driven tests for individual rule verification functions.""" |
| 237 |
|
| 238 |
def test_verify_delete_rule(self): |
| 239 |
"""Test verifylib.verify_delete_rule.""" |
| 240 |
test_data_keys = [ |
| 241 |
"rule pattern", |
| 242 |
"artifact queue", |
| 243 |
"materials", |
| 244 |
"products", |
| 245 |
"expected", |
| 246 |
] |
| 247 |
test_cases = [ |
| 248 |
# Consume deleted artifact |
| 249 |
["foo", {"foo"}, {"foo"}, set(), {"foo"}], |
| 250 |
# Consume multiple deleted artifacts with wildcard |
| 251 |
["*", {"foo", "bar"}, {"foo", "bar"}, set(), {"foo", "bar"}], |
| 252 |
# Don't consume created artifact (in products only) |
| 253 |
["foo", {"foo"}, set(), {"foo"}, set()], |
| 254 |
# Don't consume artifact that's not in materials or products |
| 255 |
# NOTE: In real life this shouldn't be in the queue either |
| 256 |
["foo", {"foo"}, set(), set(), set()], |
| 257 |
# Don't consume deleted but not queued artifact |
| 258 |
["foo", set(), {"foo"}, set(), set()], |
| 259 |
# Don't consume deleted but not matched artifact |
| 260 |
["bar", {"foo"}, {"foo"}, set(), set()], |
| 261 |
] |
| 262 |
|
| 263 |
for i, test_data in enumerate(test_cases): |
| 264 |
pattern, queue, materials, products, expected = test_data |
| 265 |
result = verify_delete_rule(pattern, queue, materials, products) |
| 266 |
self.assertSetEqual( |
| 267 |
result, |
| 268 |
expected, |
| 269 |
"test {}: {}".format(i, dict(zip(test_data_keys, test_data))), |
| 270 |
) |
| 271 |
|
| 272 |
def test_verify_create_rule(self): |
| 273 |
"""Test verifylib.verify_create_rule.""" |
| 274 |
test_data_keys = [ |
| 275 |
"rule pattern", |
| 276 |
"artifact queue", |
| 277 |
"materials", |
| 278 |
"products", |
| 279 |
"expected", |
| 280 |
] |
| 281 |
test_cases = [ |
| 282 |
# Consume created artifact |
| 283 |
["foo", {"foo"}, set(), {"foo"}, {"foo"}], |
| 284 |
# Consume multiple created artifacts with wildcard |
| 285 |
["*", {"foo", "bar"}, set(), {"foo", "bar"}, {"foo", "bar"}], |
| 286 |
# Don't consume deleted artifact (in materials only) |
| 287 |
["foo", {"foo"}, {"foo"}, set(), set()], |
| 288 |
# Don't consume artifact that's not in materials or products |
| 289 |
# NOTE: In real life this shouldn't be in the queue either |
| 290 |
["foo", {"foo"}, set(), set(), set()], |
| 291 |
# Don't consume created but not queued artifact |
| 292 |
["foo", set(), set(), {"foo"}, set()], |
| 293 |
# Don't consume created but not matched artifact |
| 294 |
["bar", {"foo"}, set(), {"foo"}, set()], |
| 295 |
] |
| 296 |
|
| 297 |
for i, test_data in enumerate(test_cases): |
| 298 |
pattern, queue, materials, products, expected = test_data |
| 299 |
result = verify_create_rule(pattern, queue, materials, products) |
| 300 |
self.assertSetEqual( |
| 301 |
result, |
| 302 |
expected, |
| 303 |
"test {}: {}".format(i, dict(zip(test_data_keys, test_data))), |
| 304 |
) |
| 305 |
|
| 306 |
def test_verify_modify_rule(self): |
| 307 |
"""Test verifylib.verify_modify_rule.""" |
| 308 |
sha_a = ( |
| 309 |
"d65165279105ca6773180500688df4bdc69a2c7b771752f0a46ef120b7fd8ec3" |
| 310 |
) |
| 311 |
sha_b = ( |
| 312 |
"155c693a6b7481f48626ebfc545f05236df679f0099225d6d0bc472e6dd21155" |
| 313 |
) |
| 314 |
|
| 315 |
test_data_keys = [ |
| 316 |
"rule pattern", |
| 317 |
"artifact queue", |
| 318 |
"materials", |
| 319 |
"products", |
| 320 |
"expected", |
| 321 |
] |
| 322 |
test_cases = [ |
| 323 |
# Consume modified artifact |
| 324 |
[ |
| 325 |
"foo", |
| 326 |
{"foo"}, |
| 327 |
{"foo": {"sha256": sha_a}}, |
| 328 |
{"foo": {"sha256": sha_b}}, |
| 329 |
{"foo"}, |
| 330 |
], |
| 331 |
# Consume multiple modified artifacts with wildcard |
| 332 |
[ |
| 333 |
"*", |
| 334 |
{"foo", "bar"}, |
| 335 |
{"foo": {"sha256": sha_a}, "bar": {"sha256": sha_a}}, |
| 336 |
{"foo": {"sha256": sha_b}, "bar": {"sha256": sha_b}}, |
| 337 |
{"foo", "bar"}, |
| 338 |
], |
| 339 |
# Don't consume unmodified artifact |
| 340 |
[ |
| 341 |
"foo", |
| 342 |
{"foo"}, |
| 343 |
{"foo": {"sha256": sha_a}}, |
| 344 |
{"foo": {"sha256": sha_a}}, |
| 345 |
set(), |
| 346 |
], |
| 347 |
# Don't consume artifact that's not in materials or products |
| 348 |
# NOTE: In real life this shouldn't be in the queue either |
| 349 |
["foo", {"foo"}, {}, {}, set()], |
| 350 |
# Don't consume modified but not queued artifact |
| 351 |
[ |
| 352 |
"foo", |
| 353 |
set(), |
| 354 |
{"foo": {"sha256": sha_a}}, |
| 355 |
{"foo": {"sha256": sha_b}}, |
| 356 |
set(), |
| 357 |
], |
| 358 |
# Don't consume modified but not matched artifact |
| 359 |
[ |
| 360 |
"bar", |
| 361 |
{"foo"}, |
| 362 |
{"foo": {"sha256": sha_a}}, |
| 363 |
{"foo": {"sha256": sha_b}}, |
| 364 |
set(), |
| 365 |
], |
| 366 |
] |
| 367 |
|
| 368 |
for i, test_data in enumerate(test_cases): |
| 369 |
pattern, queue, materials, products, expected = test_data |
| 370 |
result = verify_modify_rule(pattern, queue, materials, products) |
| 371 |
self.assertSetEqual( |
| 372 |
result, |
| 373 |
expected, |
| 374 |
"test {}: {}".format(i, dict(zip(test_data_keys, test_data))), |
| 375 |
) |
| 376 |
|
| 377 |
def test_verify_allow_rule(self): |
| 378 |
"""Test verifylib.verify_allow_rule.""" |
| 379 |
test_data_keys = ["rule pattern", "artifact queue", "expected"] |
| 380 |
test_cases = [ |
| 381 |
# Consume allowed artifact |
| 382 |
["foo", {"foo"}, {"foo"}], |
| 383 |
# Consume multiple allowed artifacts with wildcard |
| 384 |
["*", {"foo", "bar"}, {"foo", "bar"}], |
| 385 |
# Consume multiple allowed artifacts with wildcard 2 |
| 386 |
["foo*", {"foo", "foobar", "bar"}, {"foo", "foobar"}], |
| 387 |
# Don't consume unmatched artifacts |
| 388 |
["bar", {"foo"}, set()], |
| 389 |
# Don't consume artifacts if nothing is in the queue |
| 390 |
["foo", set(), set()], |
| 391 |
] |
| 392 |
for i, test_data in enumerate(test_cases): |
| 393 |
pattern, queue, expected = test_data |
| 394 |
result = verify_allow_rule(pattern, queue) |
| 395 |
self.assertSetEqual( |
| 396 |
result, |
| 397 |
expected, |
| 398 |
"test {}: {}".format(i, dict(zip(test_data_keys, test_data))), |
| 399 |
) |
| 400 |
|
| 401 |
def test_verify_disallow_rule(self): |
| 402 |
"""Test verifylib.verify_disallow_rule.""" |
| 403 |
test_data_keys = ["rule pattern", "artifact queue"] |
| 404 |
test_cases = [ |
| 405 |
# Foo disallowed, raise |
| 406 |
["foo", {"foo"}, True], |
| 407 |
# All disallowed, raise |
| 408 |
["*", {"foo", "bar"}, True], |
| 409 |
# Foo disallowed, but only bar there, don't raise |
| 410 |
["foo", {"bar"}, False], |
| 411 |
# All disallowed, but no artifacts, don't raise |
| 412 |
["*", {}, False], |
| 413 |
] |
| 414 |
|
| 415 |
for i, test_data in enumerate(test_cases): |
| 416 |
pattern, queue, should_raise = test_data |
| 417 |
|
| 418 |
msg = "test {}: {}".format(i, dict(zip(test_data_keys, test_data))) |
| 419 |
exception = None |
| 420 |
|
| 421 |
try: |
| 422 |
verify_disallow_rule(pattern, queue) |
| 423 |
except RuleVerificationError as e: |
| 424 |
exception = e |
| 425 |
|
| 426 |
if should_raise and not exception: |
| 427 |
self.fail("Expected 'RuleVerificationError'\n{}".format(msg)) |
| 428 |
|
| 429 |
if exception and not should_raise: |
| 430 |
self.fail("Unexpected {}\n{}".format(exception, msg)) |
| 431 |
|
| 432 |
def test_verify_require_rule(self): |
| 433 |
"""Test verifylib.verify_require_rule.""" |
| 434 |
test_data_keys = ["rule pattern", "artifact queue"] |
| 435 |
test_cases = [ |
| 436 |
# Foo required, pass |
| 437 |
["foo", {"foo"}, False], |
| 438 |
# Foo is required, but only bar there, blow up |
| 439 |
["foo", {"bar"}, True], |
| 440 |
# A pattern is passed, which should be interpreted *literally* |
| 441 |
["*", {"*"}, False], |
| 442 |
["*", {"foo"}, True] |
| 443 |
# |
| 444 |
] |
| 445 |
|
| 446 |
for i, test_data in enumerate(test_cases): |
| 447 |
pattern, queue, should_raise = test_data |
| 448 |
|
| 449 |
msg = "test {}: {}".format(i, dict(zip(test_data_keys, test_data))) |
| 450 |
exception = None |
| 451 |
|
| 452 |
try: |
| 453 |
verify_require_rule(pattern, queue) |
| 454 |
except RuleVerificationError as e: |
| 455 |
exception = e |
| 456 |
|
| 457 |
if should_raise and not exception: |
| 458 |
self.fail("Expected 'RuleVerificationError'\n{}".format(msg)) |
| 459 |
|
| 460 |
if exception and not should_raise: |
| 461 |
self.fail("Unexpected {}\n{}".format(exception, msg)) |
| 462 |
|
| 463 |
|
| 464 |
class TestVerifyMatchRule(unittest.TestCase): |
| 465 |
"""Test verifylib.verify_match_rule(rule, artifact_queue, artifacts, links)""" |
| 466 |
|
| 467 |
def setUp(self): |
| 468 |
"""Setup artifact queues, artifacts dictionary and Link dictionary.""" |
| 469 |
|
| 470 |
# Dummy artifact hashes |
| 471 |
self.sha256_foo = ( |
| 472 |
"d65165279105ca6773180500688df4bdc69a2c7b771752f0a46ef120b7fd8ec3" |
| 473 |
) |
| 474 |
self.sha256_foobar = ( |
| 475 |
"155c693a6b7481f48626ebfc545f05236df679f0099225d6d0bc472e6dd21155" |
| 476 |
) |
| 477 |
self.sha256_bar = ( |
| 478 |
"cfdaaf1ab2e4661952a9dec5e8fa3c360c1b06b1a073e8493a7c46d2af8c504b" |
| 479 |
) |
| 480 |
self.sha256_barfoo = ( |
| 481 |
"2036784917e49b7685c7c17e03ddcae4a063979aa296ee5090b5bb8f8aeafc5d" |
| 482 |
) |
| 483 |
|
| 484 |
# Link dictionary containing dummy artifacts related to Steps the rule is |
| 485 |
# matched with (match destination). |
| 486 |
self.materials = { |
| 487 |
"foo": {"sha256": self.sha256_foo}, |
| 488 |
"foobar": {"sha256": self.sha256_foobar}, |
| 489 |
"sub/foo": {"sha256": self.sha256_foo}, |
| 490 |
"sub/foobar": {"sha256": self.sha256_foobar}, |
| 491 |
"lib/foo": {"sha256": self.sha256_foo}, |
| 492 |
"lib/foobar": {"sha256": self.sha256_foobar}, |
| 493 |
"lib/build/foo": {"sha256": self.sha256_foo}, |
| 494 |
"lib/build/foobar": {"sha256": self.sha256_foobar}, |
| 495 |
} |
| 496 |
self.products = { |
| 497 |
"bar": {"sha256": self.sha256_bar}, |
| 498 |
"barfoo": {"sha256": self.sha256_barfoo}, |
| 499 |
"sub/bar": {"sha256": self.sha256_bar}, |
| 500 |
"sub/barfoo": {"sha256": self.sha256_barfoo}, |
| 501 |
"sub/lib/bar": {"sha256": self.sha256_bar}, |
| 502 |
"sub/lib/barfoo": {"sha256": self.sha256_barfoo}, |
| 503 |
"build/bar": {"sha256": self.sha256_bar}, |
| 504 |
"build/barfoo": {"sha256": self.sha256_barfoo}, |
| 505 |
} |
| 506 |
|
| 507 |
self.links = { |
| 508 |
"dest-item": Link( |
| 509 |
name="dest-item", |
| 510 |
materials=self.materials, |
| 511 |
products=self.products, |
| 512 |
), |
| 513 |
} |
| 514 |
|
| 515 |
def test_verify_match_rule(self): |
| 516 |
test_data_keys = [ |
| 517 |
"rule string", |
| 518 |
"artifacts queue", |
| 519 |
"source artifacts", |
| 520 |
"expected", |
| 521 |
] |
| 522 |
test_cases = [ |
| 523 |
[ |
| 524 |
# Consume foo matching with dest material foo |
| 525 |
"MATCH foo WITH MATERIALS FROM dest-item", |
| 526 |
set(self.materials.keys()), |
| 527 |
self.materials, |
| 528 |
{"foo"}, |
| 529 |
], |
| 530 |
[ |
| 531 |
# Consume foo matching with dest product foo |
| 532 |
"MATCH bar WITH PRODUCTS FROM dest-item", |
| 533 |
set(self.products.keys()), |
| 534 |
self.products, |
| 535 |
{"bar"}, |
| 536 |
], |
| 537 |
[ |
| 538 |
# Consume sub/foo matching with dest material foo |
| 539 |
"MATCH foo IN sub WITH MATERIALS FROM dest-item", |
| 540 |
set(self.materials.keys()), |
| 541 |
self.materials, |
| 542 |
{"sub/foo"}, |
| 543 |
], |
| 544 |
[ |
| 545 |
# Consume sub/foo matching with dest material foo (ignore trailing /) |
| 546 |
"MATCH foo IN sub/ WITH MATERIALS FROM dest-item", |
| 547 |
set(self.materials.keys()), |
| 548 |
self.materials, |
| 549 |
{"sub/foo"}, |
| 550 |
], |
| 551 |
[ |
| 552 |
# Consume sub/bar matching with dest product bar |
| 553 |
"MATCH bar IN sub WITH PRODUCTS FROM dest-item", |
| 554 |
set(self.products.keys()), |
| 555 |
self.products, |
| 556 |
{"sub/bar"}, |
| 557 |
], |
| 558 |
[ |
| 559 |
# Consume foo matching with dest material sub/foo |
| 560 |
"MATCH foo WITH MATERIALS IN sub FROM dest-item", |
| 561 |
set(self.materials.keys()), |
| 562 |
self.materials, |
| 563 |
{"foo"}, |
| 564 |
], |
| 565 |
[ |
| 566 |
# Consume bar matching with dest product sub/bar |
| 567 |
"MATCH bar WITH PRODUCTS IN sub FROM dest-item", |
| 568 |
set(self.products.keys()), |
| 569 |
self.products, |
| 570 |
{"bar"}, |
| 571 |
], |
| 572 |
[ |
| 573 |
# Consume bar matching with dest product sub/bar (ignore trailing /) |
| 574 |
"MATCH bar WITH PRODUCTS IN sub/ FROM dest-item", |
| 575 |
set(self.products.keys()), |
| 576 |
self.products, |
| 577 |
{"bar"}, |
| 578 |
], |
| 579 |
[ |
| 580 |
# Consume foo* matching with dest material foo* |
| 581 |
"MATCH foo* WITH MATERIALS FROM dest-item", |
| 582 |
set(self.materials.keys()), |
| 583 |
self.materials, |
| 584 |
{"foo", "foobar"}, |
| 585 |
], |
| 586 |
[ |
| 587 |
# Consume sub/foo* matching with dest material foo* |
| 588 |
"MATCH foo* IN sub WITH MATERIALS FROM dest-item", |
| 589 |
set(self.materials.keys()), |
| 590 |
self.materials, |
| 591 |
{"sub/foo", "sub/foobar"}, |
| 592 |
], |
| 593 |
[ |
| 594 |
# Consume bar* matching with dest product bar* |
| 595 |
"MATCH bar* WITH PRODUCTS FROM dest-item", |
| 596 |
set(self.products.keys()), |
| 597 |
self.products, |
| 598 |
{"bar", "barfoo"}, |
| 599 |
], |
| 600 |
[ |
| 601 |
# Consume bar* matching with dest product sub/bar* |
| 602 |
"MATCH bar* WITH PRODUCTS IN sub FROM dest-item", |
| 603 |
set(self.products.keys()), |
| 604 |
self.products, |
| 605 |
{"bar", "barfoo"}, |
| 606 |
], |
| 607 |
[ |
| 608 |
# Don't consume (empty queue) |
| 609 |
"MATCH foo WITH MATERIALS FROM dest-item", |
| 610 |
set(), |
| 611 |
self.materials, |
| 612 |
set(), |
| 613 |
], |
| 614 |
[ |
| 615 |
# Don't consume (no destination artifact) |
| 616 |
"MATCH foo WITH PRODUCTS FROM dest-item", |
| 617 |
set(self.materials.keys()), |
| 618 |
self.materials, |
| 619 |
set(), |
| 620 |
], |
| 621 |
[ |
| 622 |
# Don't consume (non-matching hashes) |
| 623 |
"MATCH foo WITH MATERIALS FROM dest-item", |
| 624 |
{"foo"}, |
| 625 |
{"foo": {"sha256": "deadbeef"}}, |
| 626 |
set(), |
| 627 |
], |
| 628 |
[ |
| 629 |
# Don't consume (missing link) |
| 630 |
"MATCH foo WITH MATERIALS FROM dest-item-missing-link", |
| 631 |
set(self.materials.keys()), |
| 632 |
self.materials, |
| 633 |
set(), |
| 634 |
], |
| 635 |
[ |
| 636 |
# Ensures the sub/bar file matches the product file in the sub folder |
| 637 |
# from the dest-item step. |
| 638 |
"MATCH bar IN sub WITH PRODUCTS IN sub FROM dest-item", |
| 639 |
set(self.products.keys()), |
| 640 |
self.products, |
| 641 |
{"sub/bar"}, |
| 642 |
], |
| 643 |
[ |
| 644 |
# Ensures the sub/bar and sub/barfoo files matches the product |
| 645 |
# files in the sub folder from the dest-item step. |
| 646 |
"MATCH bar* IN sub WITH PRODUCTS IN sub/ FROM dest-item", |
| 647 |
set(self.products.keys()), |
| 648 |
self.products, |
| 649 |
{"sub/bar", "sub/barfoo"}, |
| 650 |
], |
| 651 |
[ |
| 652 |
# Ensure the sub/bar file matches the product file in the |
| 653 |
# sub/lib folder from the dest-item step. |
| 654 |
"MATCH bar IN sub WITH PRODUCTS IN sub/lib FROM dest-item", |
| 655 |
set(self.products.keys()), |
| 656 |
self.products, |
| 657 |
{"sub/bar"}, |
| 658 |
], |
| 659 |
[ |
| 660 |
# Do not consume. Missing link |
| 661 |
"MATCH bar IN sub WITH PRODUCTS IN sub FROM dest-item-missing-link", |
| 662 |
set(self.products.keys()), |
| 663 |
self.products, |
| 664 |
set(), |
| 665 |
], |
| 666 |
[ |
| 667 |
# Ensures the sub/foo file matches the material file in the |
| 668 |
# lib folder from the dest-item step. |
| 669 |
"MATCH foo IN sub WITH MATERIALS IN lib FROM dest-item", |
| 670 |
set(self.materials.keys()), |
| 671 |
self.materials, |
| 672 |
{"sub/foo"}, |
| 673 |
], |
| 674 |
[ |
| 675 |
# Ensures any sub/foo and sub/foobar files matches material |
| 676 |
# files in the lib folder from the dest-item step. |
| 677 |
"MATCH foo* IN sub WITH MATERIALS IN lib/ FROM dest-item", |
| 678 |
set(self.materials.keys()), |
| 679 |
self.materials, |
| 680 |
{"sub/foo", "sub/foobar"}, |
| 681 |
], |
| 682 |
[ |
| 683 |
# Ensures the sub/foo file matches the material files in the lib/build |
| 684 |
# the lib/build folder from the dest-item step. |
| 685 |
"MATCH foo IN sub WITH MATERIALS IN lib/build FROM dest-item", |
| 686 |
set(self.materials.keys()), |
| 687 |
self.materials, |
| 688 |
{"sub/foo"}, |
| 689 |
], |
| 690 |
[ |
| 691 |
# Ensures the sub/foo and sub/foobar files matches the material files |
| 692 |
# in the lib/build folder from the dest-item step. |
| 693 |
"MATCH foo* IN sub WITH MATERIALS IN lib/build FROM dest-item", |
| 694 |
set(self.materials.keys()), |
| 695 |
self.materials, |
| 696 |
{"sub/foo", "sub/foobar"}, |
| 697 |
], |
| 698 |
] |
| 699 |
|
| 700 |
for i, test_data in enumerate(test_cases): |
| 701 |
rule_string, queue, source_artifacts, expected = test_data |
| 702 |
|
| 703 |
# Generate rule data from rule string |
| 704 |
rule_data = unpack_rule(shlex.split(rule_string)) |
| 705 |
|
| 706 |
result = verify_match_rule( |
| 707 |
rule_data, queue, source_artifacts, self.links |
| 708 |
) |
| 709 |
|
| 710 |
self.assertSetEqual( |
| 711 |
result, |
| 712 |
expected, |
| 713 |
"'result': {}\n test {}: {}, 'links':{}".format( |
| 714 |
result, i, dict(zip(test_data_keys, test_data)), self.links |
| 715 |
), |
| 716 |
) |
| 717 |
|
| 718 |
|
| 719 |
class TestVerifyItemRules(unittest.TestCase): |
| 720 |
"""Test verifylib.verify_item_rules(source_name, source_type, rules, links)""" |
| 721 |
|
| 722 |
def setUp(self): |
| 723 |
self.item_name = "item" |
| 724 |
self.sha256_1 = ( |
| 725 |
"d65165279105ca6773180500688df4bdc69a2c7b771752f0a46ef120b7fd8ec3" |
| 726 |
) |
| 727 |
self.sha256_2 = ( |
| 728 |
"cfdaaf1ab2e4661952a9dec5e8fa3c360c1b06b1a073e8493a7c46d2af8c504b" |
| 729 |
) |
| 730 |
|
| 731 |
self.links = { |
| 732 |
"item": Link( |
| 733 |
name="item", |
| 734 |
materials={ |
| 735 |
"foo": {"sha256": self.sha256_1}, |
| 736 |
"foobar": {"sha256": self.sha256_1}, |
| 737 |
"bar": {"sha256": self.sha256_1}, |
| 738 |
"foobarbaz": {"sha256": self.sha256_1}, |
| 739 |
}, |
| 740 |
products={ |
| 741 |
"baz": {"sha256": self.sha256_1}, |
| 742 |
"foo": {"sha256": self.sha256_1}, |
| 743 |
"bar": {"sha256": self.sha256_2}, |
| 744 |
"foobarbaz": {"sha256": self.sha256_1}, |
| 745 |
}, |
| 746 |
) |
| 747 |
} |
| 748 |
|
| 749 |
def test_pass_rules_with_each_rule_type(self): |
| 750 |
"""Pass with list of rules of each rule type.""" |
| 751 |
rules = [ |
| 752 |
["DELETE", "foobar"], |
| 753 |
["REQUIRE", "foobarbaz"], |
| 754 |
["CREATE", "baz"], |
| 755 |
["MODIFY", "bar"], |
| 756 |
[ |
| 757 |
"MATCH", |
| 758 |
"foo", |
| 759 |
"WITH", |
| 760 |
"MATERIALS", |
| 761 |
"FROM", |
| 762 |
"item", |
| 763 |
], # match with self |
| 764 |
["ALLOW", "foobarbaz"], |
| 765 |
["DISALLOW", "*"], |
| 766 |
] |
| 767 |
for source_type in ["materials", "products"]: |
| 768 |
verify_item_rules(self.item_name, source_type, rules, self.links) |
| 769 |
|
| 770 |
def test_fail_disallow_not_consumed_artifacts(self): |
| 771 |
"""Fail with not consumed artifacts and terminal DISALLOW.""" |
| 772 |
rules = [ |
| 773 |
["DISALLOW", "*"], |
| 774 |
] |
| 775 |
with self.assertRaises(RuleVerificationError): |
| 776 |
verify_item_rules(self.item_name, "materials", rules, self.links) |
| 777 |
|
| 778 |
def test_fail_wrong_source_type(self): |
| 779 |
"""Fail with wrong source_type.""" |
| 780 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 781 |
verify_item_rules(self.item_name, "artifacts", [], self.links) |
| 782 |
|
| 783 |
def test_pass_not_consumed_artifacts(self): |
| 784 |
"""Pass with not consumed artifacts and implicit terminal ALLOW *""" |
| 785 |
verify_item_rules(self.item_name, "materials", [], self.links) |
| 786 |
|
| 787 |
|
| 788 |
class TestVerifyAllItemRules(unittest.TestCase): |
| 789 |
"""Test verifylib.verify_all_item_rules(items, links).""" |
| 790 |
|
| 791 |
def setUp(self): |
| 792 |
"""Create a dummy supply chain with two steps one inspection and the |
| 793 |
according link metadata: |
| 794 |
|
| 795 |
write-code (Step) -> package (step) -> untar (Inspection) |
| 796 |
|
| 797 |
'write-code' creates an artifact foo |
| 798 |
'package' creates foo.tar.gz and deletes foo |
| 799 |
'untar' untars foo.tar.gz which results in foo.tar.gz and foo |
| 800 |
|
| 801 |
""" |
| 802 |
|
| 803 |
self.sha256_foo = ( |
| 804 |
"d65165279105ca6773180500688df4bdc69a2c7b771752f0a46ef120b7fd8ec3" |
| 805 |
) |
| 806 |
self.sha256_foo_tar = ( |
| 807 |
"93c3c35a039a6a3d53e81c5dbee4ebb684de57b7c8be11b8739fd35804a0e918" |
| 808 |
) |
| 809 |
|
| 810 |
self.steps = [ |
| 811 |
Step( |
| 812 |
name="write-code", |
| 813 |
expected_products=[["CREATE", "foo"]], |
| 814 |
), |
| 815 |
Step( |
| 816 |
name="package", |
| 817 |
expected_materials=[ |
| 818 |
["MATCH", "foo", "WITH", "PRODUCTS", "FROM", "write-code"] |
| 819 |
], |
| 820 |
expected_products=[["CREATE", "foo.tar.gz"], ["DELETE", "foo"]], |
| 821 |
), |
| 822 |
] |
| 823 |
|
| 824 |
self.inspections = [ |
| 825 |
Inspection( |
| 826 |
name="untar", |
| 827 |
expected_materials=[ |
| 828 |
["REQUIRE", "foo.tar.gz"], |
| 829 |
[ |
| 830 |
"MATCH", |
| 831 |
"foo.tar.gz", |
| 832 |
"WITH", |
| 833 |
"PRODUCTS", |
| 834 |
"FROM", |
| 835 |
"package", |
| 836 |
], |
| 837 |
], |
| 838 |
expected_products=[ |
| 839 |
[ |
| 840 |
"MATCH", |
| 841 |
"foo", |
| 842 |
"IN", |
| 843 |
"dir", |
| 844 |
"WITH", |
| 845 |
"PRODUCTS", |
| 846 |
"FROM", |
| 847 |
"write-code", |
| 848 |
] |
| 849 |
], |
| 850 |
) |
| 851 |
] |
| 852 |
|
| 853 |
self.links = { |
| 854 |
"write-code": Link( |
| 855 |
name="write-code", products={"foo": {"sha256": self.sha256_foo}} |
| 856 |
), |
| 857 |
"package": Link( |
| 858 |
name="package", |
| 859 |
materials={"foo": {"sha256": self.sha256_foo}}, |
| 860 |
products={"foo.tar.gz": {"sha256": self.sha256_foo_tar}}, |
| 861 |
), |
| 862 |
"untar": Link( |
| 863 |
name="untar", |
| 864 |
materials={"foo.tar.gz": {"sha256": self.sha256_foo_tar}}, |
| 865 |
products={ |
| 866 |
"dir/foo": {"sha256": self.sha256_foo}, |
| 867 |
}, |
| 868 |
), |
| 869 |
} |
| 870 |
|
| 871 |
def test_pass_verify_all_step_rules(self): |
| 872 |
"""Pass rule verification for dummy supply chain Steps.""" |
| 873 |
verify_all_item_rules(self.steps, self.links) |
| 874 |
|
| 875 |
def test_pass_verify_all_inspection_rules(self): |
| 876 |
"""Pass rule verification for dummy supply chain Inspections.""" |
| 877 |
verify_all_item_rules(self.inspections, self.links) |
| 878 |
|
| 879 |
|
| 880 |
class TestInTotoVerify(unittest.TestCase, TmpDirMixin): |
| 881 |
""" |
| 882 |
Tests verifylib.in_toto_verify(layout_path, layout_key_paths). |
| 883 |
|
| 884 |
Uses in-toto demo supply chain link metadata files and basic layout for |
| 885 |
verification. |
| 886 |
|
| 887 |
Copies the basic layout for different test scenarios: |
| 888 |
- single-signed layout |
| 889 |
- double-signed layout |
| 890 |
- expired layout |
| 891 |
- layout with failing link rule |
| 892 |
- layout with failing step rule |
| 893 |
|
| 894 |
""" |
| 895 |
|
| 896 |
@classmethod |
| 897 |
def setUpClass(self): |
| 898 |
"""Creates and changes into temporary directory. |
| 899 |
Copies demo files to temp dir... |
| 900 |
- owner/functionary key pairs |
| 901 |
- *.link metadata files |
| 902 |
- layout template (not signed, no expiration date) |
| 903 |
- final product |
| 904 |
|
| 905 |
...and dumps various layouts for different test scenarios |
| 906 |
""" |
| 907 |
|
| 908 |
# Find demo files |
| 909 |
demo_files = os.path.join( |
| 910 |
os.path.dirname(os.path.realpath(__file__)), "demo_files" |
| 911 |
) |
| 912 |
|
| 913 |
# find where the scripts directory is located. |
| 914 |
scripts_directory = os.path.join( |
| 915 |
os.path.dirname(os.path.realpath(__file__)), "scripts" |
| 916 |
) |
| 917 |
|
| 918 |
# Create and change into temporary directory |
| 919 |
self.set_up_test_dir() |
| 920 |
|
| 921 |
# Copy demo files to temp dir |
| 922 |
for fn in os.listdir(demo_files): |
| 923 |
shutil.copy(os.path.join(demo_files, fn), self.test_dir) |
| 924 |
|
| 925 |
# copy scripts over |
| 926 |
shutil.copytree(scripts_directory, "scripts") |
| 927 |
|
| 928 |
# Load layout template |
| 929 |
layout_template = Metablock.load("demo.layout.template") |
| 930 |
|
| 931 |
# Store various layout paths to be used in tests |
| 932 |
self.layout_single_signed_path = "single-signed.layout" |
| 933 |
self.layout_double_signed_path = "double-signed.layout" |
| 934 |
self.layout_bad_sig = "bad-sig.layout" |
| 935 |
self.layout_expired_path = "expired.layout" |
| 936 |
self.layout_failing_step_rule_path = "failing-step-rule.layout" |
| 937 |
self.layout_failing_inspection_rule_path = ( |
| 938 |
"failing-inspection-rule.layout" |
| 939 |
) |
| 940 |
self.layout_failing_inspection_retval = ( |
| 941 |
"failing-inspection-retval.layout" |
| 942 |
) |
| 943 |
self.layout_no_steps_no_inspections = "no_steps_no_inspections.layout" |
| 944 |
|
| 945 |
# Import layout signing keys |
| 946 |
alice = import_rsa_privatekey_from_file("alice") |
| 947 |
bob = import_rsa_privatekey_from_file("bob") |
| 948 |
self.alice_path = "alice.pub" |
| 949 |
self.bob_path = "bob.pub" |
| 950 |
|
| 951 |
# dump single signed layout |
| 952 |
layout = copy.deepcopy(layout_template) |
| 953 |
layout.sign(alice) |
| 954 |
layout.dump(self.layout_single_signed_path) |
| 955 |
|
| 956 |
# dump double signed layout |
| 957 |
layout = copy.deepcopy(layout_template) |
| 958 |
layout.sign(alice) |
| 959 |
layout.sign(bob) |
| 960 |
layout.dump(self.layout_double_signed_path) |
| 961 |
|
| 962 |
# dump layout with bad signature |
| 963 |
layout = copy.deepcopy(layout_template) |
| 964 |
layout.sign(alice) |
| 965 |
layout.signed.readme = "this breaks the signature" |
| 966 |
layout.dump(self.layout_bad_sig) |
| 967 |
|
| 968 |
# dump expired layout |
| 969 |
layout = copy.deepcopy(layout_template) |
| 970 |
layout.signed.expires = ( |
| 971 |
datetime.today() + relativedelta(months=-1) |
| 972 |
).strftime("%Y-%m-%dT%H:%M:%SZ") |
| 973 |
layout.sign(alice) |
| 974 |
layout.dump(self.layout_expired_path) |
| 975 |
|
| 976 |
# dump layout with failing step rule |
| 977 |
layout = copy.deepcopy(layout_template) |
| 978 |
layout.signed.steps[0].expected_products.insert(0, ["DISALLOW", "*"]) |
| 979 |
layout.signed.steps[0].expected_products.insert(0, ["MODIFY", "*"]) |
| 980 |
layout.sign(alice) |
| 981 |
layout.dump(self.layout_failing_step_rule_path) |
| 982 |
|
| 983 |
# dump layout with failing inspection rule |
| 984 |
layout = copy.deepcopy(layout_template) |
| 985 |
layout.signed.inspect[0].expected_materials.insert(0, ["MODIFY", "*"]) |
| 986 |
layout.signed.inspect[0].expected_materials.append(["DISALLOW", "*"]) |
| 987 |
layout.sign(alice) |
| 988 |
layout.dump(self.layout_failing_inspection_rule_path) |
| 989 |
|
| 990 |
# dump layout with failing inspection retval |
| 991 |
layout = copy.deepcopy(layout_template) |
| 992 |
layout.signed.inspect[0].run = [ |
| •993 |
"python3", |
| 994 |
"./scripts/expr", |
| 995 |
"1", |
| 996 |
"/", |
| 997 |
"0", |
| 998 |
] |
| 999 |
layout.sign(alice) |
| 1000 |
layout.dump(self.layout_failing_inspection_retval) |
| 1001 |
|
| 1002 |
# dump empty layout |
| 1003 |
layout = Metablock(signed=Layout()) |
| 1004 |
layout.sign(alice) |
| 1005 |
layout.dump(self.layout_no_steps_no_inspections) |
| 1006 |
self.alice = alice |
| 1007 |
|
| 1008 |
@classmethod |
| 1009 |
def tearDownClass(self): |
| 1010 |
self.tear_down_test_dir() |
| 1011 |
|
| 1012 |
def test_verify_passing(self): |
| 1013 |
"""Test pass verification of single-signed layout.""" |
| 1014 |
layout = Metablock.load(self.layout_single_signed_path) |
| 1015 |
layout_key_dict = import_publickeys_from_file([self.alice_path]) |
| 1016 |
in_toto_verify(layout, layout_key_dict) |
| 1017 |
|
| 1018 |
def test_verify_passing_double_signed_layout(self): |
| 1019 |
"""Test pass verification of double-signed layout.""" |
| 1020 |
layout = Metablock.load(self.layout_double_signed_path) |
| 1021 |
layout_key_dict = import_publickeys_from_file( |
| 1022 |
[self.alice_path, self.bob_path] |
| 1023 |
) |
| 1024 |
in_toto_verify(layout, layout_key_dict) |
| 1025 |
|
| 1026 |
def test_verify_passing_empty_layout(self): |
| 1027 |
"""Test pass verification of layout without steps or inspections.""" |
| 1028 |
layout = Metablock.load(self.layout_no_steps_no_inspections) |
| 1029 |
layout_key_dict = import_publickeys_from_file([self.alice_path]) |
| 1030 |
in_toto_verify(layout, layout_key_dict) |
| 1031 |
|
| 1032 |
def test_verify_failing_wrong_key(self): |
| 1033 |
"""Test fail verification with wrong layout key.""" |
| 1034 |
layout = Metablock.load(self.layout_single_signed_path) |
| 1035 |
layout_key_dict = import_publickeys_from_file([self.bob_path]) |
| 1036 |
with self.assertRaises(SignatureVerificationError): |
| 1037 |
in_toto_verify(layout, layout_key_dict) |
| 1038 |
|
| 1039 |
def test_verify_failing_bad_signature(self): |
| 1040 |
"""Test fail verification with bad layout signature.""" |
| 1041 |
layout = Metablock.load(self.layout_bad_sig) |
| 1042 |
layout_key_dict = import_publickeys_from_file([self.alice_path]) |
| 1043 |
with self.assertRaises(SignatureVerificationError): |
| 1044 |
in_toto_verify(layout, layout_key_dict) |
| 1045 |
|
| 1046 |
def test_verify_failing_layout_expired(self): |
| 1047 |
"""Test fail verification with expired layout.""" |
| 1048 |
layout = Metablock.load(self.layout_expired_path) |
| 1049 |
layout_key_dict = import_publickeys_from_file([self.alice_path]) |
| 1050 |
with self.assertRaises(LayoutExpiredError): |
| 1051 |
in_toto_verify(layout, layout_key_dict) |
| 1052 |
|
| 1053 |
def test_verify_failing_link_metadata_files(self): |
| 1054 |
"""Test fail verification with link metadata files not found.""" |
| 1055 |
os.rename("package.2f89b927.link", "package.link.bak") |
| 1056 |
layout = Metablock.load(self.layout_single_signed_path) |
| 1057 |
layout_key_dict = import_publickeys_from_file([self.alice_path]) |
| 1058 |
with self.assertRaises(in_toto.exceptions.LinkNotFoundError): |
| 1059 |
in_toto_verify(layout, layout_key_dict) |
| 1060 |
os.rename("package.link.bak", "package.2f89b927.link") |
| 1061 |
|
| 1062 |
def test_verify_failing_inspection_exits_non_zero(self): |
| 1063 |
"""Test fail verification with inspection returning non-zero.""" |
| 1064 |
layout = Metablock.load(self.layout_failing_inspection_retval) |
| 1065 |
layout_key_dict = import_publickeys_from_file([self.alice_path]) |
| 1066 |
with self.assertRaises(BadReturnValueError): |
| 1067 |
in_toto_verify(layout, layout_key_dict) |
| 1068 |
|
| 1069 |
def test_verify_failing_step_rules(self): |
| 1070 |
"""Test fail verification with failing step artifact rule.""" |
| 1071 |
layout = Metablock.load(self.layout_failing_step_rule_path) |
| 1072 |
layout_key_dict = import_publickeys_from_file([self.alice_path]) |
| 1073 |
with self.assertRaises(RuleVerificationError): |
| 1074 |
in_toto_verify(layout, layout_key_dict) |
| 1075 |
|
| 1076 |
def test_verify_failing_inspection_rules(self): |
| 1077 |
"""Test fail verification with failing inspection artifact rule.""" |
| 1078 |
layout = Metablock.load(self.layout_failing_inspection_rule_path) |
| 1079 |
layout_key_dict = import_publickeys_from_file([self.alice_path]) |
| 1080 |
with self.assertRaises(RuleVerificationError): |
| 1081 |
in_toto_verify(layout, layout_key_dict) |
| 1082 |
|
| 1083 |
def test_verify_layout_signatures_fail_with_no_keys(self): |
| 1084 |
"""Layout signature verification fails when no keys are passed.""" |
| 1085 |
layout_metablock = Metablock(signed=Layout()) |
| 1086 |
with self.assertRaises(SignatureVerificationError): |
| 1087 |
in_toto_verify(layout_metablock, {}) |
| 1088 |
|
| 1089 |
def test_verify_layout_signatures_fail_with_malformed_signature(self): |
| 1090 |
"""Layout signature verification fails with malformed signatures.""" |
| 1091 |
layout_metablock = Metablock(signed=Layout()) |
| 1092 |
signature = layout_metablock.sign(self.alice) |
| 1093 |
pubkey = copy.deepcopy(self.alice) |
| 1094 |
pubkey["keyval"]["private"] = "" |
| 1095 |
|
| 1096 |
del signature["sig"] |
| 1097 |
layout_metablock.signed.signatures = [signature] |
| 1098 |
with self.assertRaises(SignatureVerificationError): |
| 1099 |
in_toto_verify(layout_metablock, {self.alice["keyid"]: pubkey}) |
| 1100 |
|
| 1101 |
|
| 1102 |
class TestInTotoVerifyThresholds(unittest.TestCase): |
| 1103 |
"""Test verifylib functions related to signature thresholds. |
| 1104 |
|
| 1105 |
- verifylib.verify_link_signature_thresholds |
| 1106 |
- verifylib.verify_threshold_constraints""" |
| 1107 |
|
| 1108 |
@classmethod |
| 1109 |
def setUpClass(self): |
| 1110 |
"""Load test keys from demo files.""" |
| 1111 |
demo_files = os.path.join( |
| 1112 |
os.path.dirname(os.path.realpath(__file__)), "demo_files" |
| 1113 |
) |
| 1114 |
|
| 1115 |
self.alice = import_rsa_privatekey_from_file( |
| 1116 |
os.path.join(demo_files, "alice") |
| 1117 |
) |
| 1118 |
self.alice_pubkey = import_rsa_publickey_from_file( |
| 1119 |
os.path.join(demo_files, "alice.pub") |
| 1120 |
) |
| 1121 |
self.alice_keyid = self.alice["keyid"] |
| 1122 |
|
| 1123 |
self.bob = import_rsa_privatekey_from_file( |
| 1124 |
os.path.join(demo_files, "bob") |
| 1125 |
) |
| 1126 |
self.bob_pubkey = import_rsa_publickey_from_file( |
| 1127 |
os.path.join(demo_files, "bob.pub") |
| 1128 |
) |
| 1129 |
self.bob_keyid = self.bob["keyid"] |
| 1130 |
|
| 1131 |
self.name = "test" |
| 1132 |
self.foo_hash = ( |
| 1133 |
"d65165279105ca6773180500688df4bdc69a2c7b771752f0a46ef120b7fd8ec3" |
| 1134 |
) |
| 1135 |
|
| 1136 |
def test_thresholds_skip_unauthorized_links(self): |
| 1137 |
"""Ignore links with unauthorized signatures.""" |
| 1138 |
# Layout with one step, one authorized functionary and threshold 1 |
| 1139 |
layout = Layout( |
| 1140 |
keys={self.bob_keyid: self.bob_pubkey}, |
| 1141 |
steps=[Step(name=self.name, pubkeys=[self.bob_keyid])], |
| 1142 |
) |
| 1143 |
|
| 1144 |
# Signed links (one authorized the other one not) |
| 1145 |
link_bob = Metablock(signed=Link(name=self.name)) |
| 1146 |
link_bob.sign(self.bob) |
| 1147 |
link_alice = Metablock(signed=Link(name=self.name)) |
| 1148 |
link_alice.sign(self.alice) |
| 1149 |
|
| 1150 |
# The dictionary of links per step passed to the verify function |
| 1151 |
chain_link_dict = { |
| 1152 |
self.name: {self.bob_keyid: link_bob, self.alice_keyid: link_alice} |
| 1153 |
} |
| 1154 |
|
| 1155 |
# The dictionary of links expected to be returned, not containing the |
| 1156 |
# unauthorized link, but enough (threshold) authorized links |
| 1157 |
expected_chain_link_dict = {self.name: {self.bob_keyid: link_bob}} |
| 1158 |
# Verify signatures/thresholds |
| 1159 |
returned_chain_link_dict = verify_link_signature_thresholds( |
| 1160 |
layout, chain_link_dict |
| 1161 |
) |
| 1162 |
# Test that the returned dict is as expected |
| 1163 |
self.assertDictEqual(returned_chain_link_dict, expected_chain_link_dict) |
| 1164 |
|
| 1165 |
def test_thresholds_skip_links_with_failing_signature(self): |
| 1166 |
"""Ignore links with failing signatures.""" |
| 1167 |
|
| 1168 |
# Layout with one step, two authorized functionaries and threshold 1 |
| 1169 |
layout = Layout( |
| 1170 |
keys={ |
| 1171 |
self.bob_keyid: self.bob_pubkey, |
| 1172 |
self.alice_keyid: self.alice_pubkey, |
| 1173 |
}, |
| 1174 |
steps=[ |
| 1175 |
Step( |
| 1176 |
name=self.name, |
| 1177 |
pubkeys=[self.bob_keyid, self.alice_keyid], |
| 1178 |
threshold=1, |
| 1179 |
) |
| 1180 |
], |
| 1181 |
) |
| 1182 |
|
| 1183 |
# Authorized links (one signed one not) |
| 1184 |
link_bob = Metablock(signed=Link(name=self.name)) |
| 1185 |
link_bob.sign(self.bob) |
| 1186 |
link_alice = Metablock(signed=Link(name=self.name)) |
| 1187 |
|
| 1188 |
# The dictionary of links per step passed to the verify function |
| 1189 |
chain_link_dict = { |
| 1190 |
self.name: {self.bob_keyid: link_bob, self.alice_keyid: link_alice} |
| 1191 |
} |
| 1192 |
|
| 1193 |
# The dictionary of links expected to be returned, not containing the |
| 1194 |
# unauthorized link, but enough (threshold) authorized links |
| 1195 |
expected_chain_link_dict = {self.name: {self.bob_keyid: link_bob}} |
| 1196 |
|
| 1197 |
# Verify signatures/thresholds |
| 1198 |
returned_chain_link_dict = verify_link_signature_thresholds( |
| 1199 |
layout, chain_link_dict |
| 1200 |
) |
| 1201 |
# Test that the returned dict is as expected |
| 1202 |
self.assertDictEqual(returned_chain_link_dict, expected_chain_link_dict) |
| 1203 |
|
| 1204 |
def test_thresholds_fail_with_not_enough_valid_links(self): |
| 1205 |
"""Fail with not enough authorized links.""" |
| 1206 |
|
| 1207 |
# Layout with one step, two authorized functionaries and threshold 2 |
| 1208 |
layout = Layout( |
| 1209 |
keys={ |
| 1210 |
self.bob_keyid: self.bob_pubkey, |
| 1211 |
self.alice_keyid: self.alice_pubkey, |
| 1212 |
}, |
| 1213 |
steps=[ |
| 1214 |
Step( |
| 1215 |
name=self.name, |
| 1216 |
pubkeys=[self.bob_keyid, self.alice_keyid], |
| 1217 |
threshold=2, |
| 1218 |
) |
| 1219 |
], |
| 1220 |
) |
| 1221 |
|
| 1222 |
# Only one authorized and validly signed link |
| 1223 |
link_bob = Metablock(signed=Link(name=self.name)) |
| 1224 |
link_bob.sign(self.bob) |
| 1225 |
|
| 1226 |
# The dictionary of links per step passed to the verify function |
| 1227 |
chain_link_dict = {self.name: {self.bob_keyid: link_bob}} |
| 1228 |
|
| 1229 |
# Fail signature threshold verification with not enough links |
| 1230 |
with self.assertRaises(ThresholdVerificationError): |
| 1231 |
verify_link_signature_thresholds(layout, chain_link_dict) |
| 1232 |
|
| 1233 |
def test_threshold_constraints_fail_with_not_enough_links(self): |
| 1234 |
"""Fail with not enough links.""" |
| 1235 |
# Layout with one step and threshold 2 |
| 1236 |
layout = Layout(steps=[Step(name=self.name, threshold=2)]) |
| 1237 |
# Authorized (unsigned) link |
| 1238 |
# This function does not care for signatures it just verifies if the |
| 1239 |
# different links have recorded the same artifacts. Signature verification |
| 1240 |
# happens earlier in the final product verification (see tests above) |
| 1241 |
link_bob = Metablock(signed=Link(name=self.name)) |
| 1242 |
|
| 1243 |
chain_link_dict = { |
| 1244 |
self.name: { |
| 1245 |
self.bob_keyid: link_bob, |
| 1246 |
} |
| 1247 |
} |
| 1248 |
|
| 1249 |
with self.assertRaises(ThresholdVerificationError): |
| 1250 |
verify_threshold_constraints(layout, chain_link_dict) |
| 1251 |
|
| 1252 |
def test_threshold_constraints_fail_with_unequal_links(self): |
| 1253 |
"""Test that the links for a step recorded the same artifacts.""" |
| 1254 |
# Layout with one step and threshold 2 |
| 1255 |
layout = Layout(steps=[Step(name=self.name, threshold=2)]) |
| 1256 |
link_bob = Link( |
| 1257 |
name=self.name, materials={"foo": {"sha256": self.foo_hash}} |
| 1258 |
) |
| 1259 |
# Cf. signing comment in test_thresholds_constraints_with_not_enough_links |
| 1260 |
link_alice = Link(name=self.name) |
| 1261 |
|
| 1262 |
chain_link_dict = { |
| 1263 |
self.name: { |
| 1264 |
self.bob_keyid: link_bob, |
| 1265 |
self.alice_keyid: link_alice, |
| 1266 |
} |
| 1267 |
} |
| 1268 |
|
| 1269 |
with self.assertRaises(ThresholdVerificationError): |
| 1270 |
verify_threshold_constraints(layout, chain_link_dict) |
| 1271 |
|
| 1272 |
def test_threshold_constraints_pas_with_equal_links(self): |
| 1273 |
"""Pass threshold constraint verification with equal links.""" |
| 1274 |
# Layout with one step and threshold 2 |
| 1275 |
layout = Layout(steps=[Step(name=self.name, threshold=2)]) |
| 1276 |
# Two authorized links with equal artifact recordings (materials) |
| 1277 |
# Cf. signing comment in test_thresholds_constraints_with_not_enough_links |
| 1278 |
link_bob = Link( |
| 1279 |
name=self.name, materials={"foo": {"sha256": self.foo_hash}} |
| 1280 |
) |
| 1281 |
link_alice = Link( |
| 1282 |
name=self.name, materials={"foo": {"sha256": self.foo_hash}} |
| 1283 |
) |
| 1284 |
|
| 1285 |
chain_link_dict = { |
| 1286 |
self.name: { |
| 1287 |
self.bob_keyid: link_bob, |
| 1288 |
self.alice_keyid: link_alice, |
| 1289 |
} |
| 1290 |
} |
| 1291 |
|
| 1292 |
verify_threshold_constraints(layout, chain_link_dict) |
| 1293 |
|
| 1294 |
|
| 1295 |
@unittest.skipIf(not have_gpg(), "gpg not found") |
| 1296 |
class TestInTotoVerifyThresholdsGpgSubkeys( |
| 1297 |
unittest.TestCase, TmpDirMixin, GPGKeysMixin |
| 1298 |
): |
| 1299 |
""" |
| 1300 |
Test the following 8 scenarios for combinations of link authorization, |
| 1301 |
where a link is either signed by a master or subkey (SIG), and the |
| 1302 |
corresponding step authorizes either the master or subkey (AUTH), and the |
| 1303 |
corresponding top level key in the layout key store is either a master key |
| 1304 |
(bundle, i.e. with subkeys) or a subkey (KEY). |
| 1305 |
|
| 1306 |
M ... Masterkey |
| 1307 |
S ... Subkey |
| 1308 |
|
| 1309 |
SIG AUTH KEY(bundle)| OK | Comment |
| 1310 |
--------------------------------------------------------------- |
| 1311 |
M M M | Yes | Normal scenario (*) |
| 1312 |
M M S | No | Cannot find key in key store + cannot sign (*) |
| 1313 |
M S M | No | Unallowed trust delegation + cannot sign (*) |
| 1314 |
M S S | No | Unallowed trust delegation + cannot sign (*) |
| 1315 |
S M M | Yes | Allowed trust delegation |
| 1316 |
S M S | No | Cannot associate keys |
| 1317 |
S S M | Yes | Can find key in key store |
| 1318 |
S S S | Yes | Generalizes to normal scenario |
| 1319 |
|
| 1320 |
(*) NOTE: Master keys with a subkey with signing capability always use that |
| 1321 |
subkey, even if the master keyid is specified and has signing capability. |
| 1322 |
|
| 1323 |
|
| 1324 |
Plus additional gpg subkey related threshold tests. |
| 1325 |
|
| 1326 |
""" |
| 1327 |
|
| 1328 |
@classmethod |
| 1329 |
def setUpClass(self): |
| 1330 |
self.set_up_test_dir() |
| 1331 |
self.set_up_gpg_keys() |
| 1332 |
|
| 1333 |
master_key = securesystemslib.gpg.functions.export_pubkey( |
| 1334 |
self.gpg_key_0C8A17, self.gnupg_home |
| 1335 |
) |
| 1336 |
sub_key = master_key["subkeys"][self.gpg_key_D924E9] |
| 1337 |
|
| 1338 |
# We need a gpg key without subkeys to test the normal scenario (M M M), |
| 1339 |
# because keys with signing subkeys always use that subkey for signing. |
| 1340 |
master_key2 = securesystemslib.gpg.functions.export_pubkey( |
| 1341 |
self.gpg_key_768C43, self.gnupg_home |
| 1342 |
) |
| 1343 |
|
| 1344 |
self.pub_key_dict = { |
| 1345 |
self.gpg_key_0C8A17: master_key, |
| 1346 |
self.gpg_key_D924E9: sub_key, |
| 1347 |
self.gpg_key_768C43: master_key2, |
| 1348 |
} |
| 1349 |
|
| 1350 |
self.step_name = "name" |
| 1351 |
|
| 1352 |
@classmethod |
| 1353 |
def tearDownClass(self): |
| 1354 |
self.tear_down_test_dir() |
| 1355 |
|
| 1356 |
def _verify_link_signature_tresholds(self, sig_id, auth_id, key_id): |
| 1357 |
metablock = Metablock(signed=Link(name=self.step_name)) |
| 1358 |
metablock.sign_gpg(sig_id, self.gnupg_home) # SIG |
| 1359 |
|
| 1360 |
chain_link_dict = {self.step_name: {sig_id: metablock}} # SIG |
| 1361 |
|
| 1362 |
layout = Layout( |
| 1363 |
steps=[Step(name=self.step_name, pubkeys=[auth_id])], # AUTH |
| 1364 |
keys={key_id: self.pub_key_dict[key_id]}, # KEY |
| 1365 |
) |
| 1366 |
return layout, chain_link_dict |
| 1367 |
|
| 1368 |
def test_verify_link_signature_thresholds__M_M_M(self): |
| 1369 |
"""Normal scenario.""" |
| 1370 |
layout, chain_link_dict = self._verify_link_signature_tresholds( |
| 1371 |
self.gpg_key_768C43, self.gpg_key_768C43, self.gpg_key_768C43 |
| 1372 |
) |
| 1373 |
|
| 1374 |
# print("path: {}".format(os.environ['PATH'])) |
| 1375 |
verify_link_signature_thresholds(layout, chain_link_dict) |
| 1376 |
|
| 1377 |
def test_verify_link_signature_thresholds__M_M_S__M_S_M__M_S_S(self): |
| 1378 |
"""Cannot sign with master key if subkey is present.""" |
| 1379 |
# The scenarios MMS, MSM, MSS are impossible because we cannot sign |
| 1380 |
# with a master key, if there is a subkey with signing capability |
| 1381 |
# GPG will always use that subkey. |
| 1382 |
# Even if gpg would use the masterkey, these scenarios are not allowed, |
| 1383 |
# see table in docstring of testcase |
| 1384 |
signature = securesystemslib.gpg.functions.create_signature( |
| 1385 |
b"data", self.gpg_key_0C8A17, self.gnupg_home |
| 1386 |
) |
| 1387 |
|
| 1388 |
self.assertTrue(signature["keyid"] == self.gpg_key_D924E9) |
| 1389 |
|
| 1390 |
def test_verify_link_signature_thresholds__S_M_M(self): |
| 1391 |
"""Allowed trust delegation.""" |
| 1392 |
layout, chain_link_dict = self._verify_link_signature_tresholds( |
| 1393 |
self.gpg_key_D924E9, self.gpg_key_0C8A17, self.gpg_key_0C8A17 |
| 1394 |
) |
| 1395 |
verify_link_signature_thresholds(layout, chain_link_dict) |
| 1396 |
|
| 1397 |
def test_verify_link_signature_thresholds__S_M_S(self): |
| 1398 |
"""Cannot associate keys.""" |
| 1399 |
layout, chain_link_dict = self._verify_link_signature_tresholds( |
| 1400 |
self.gpg_key_D924E9, self.gpg_key_0C8A17, self.gpg_key_D924E9 |
| 1401 |
) |
| 1402 |
with self.assertRaises(ThresholdVerificationError): |
| 1403 |
verify_link_signature_thresholds(layout, chain_link_dict) |
| 1404 |
|
| 1405 |
def test_verify_link_signature_thresholds__S_S_M(self): |
| 1406 |
"""No trust delegation and can find key in key store.""" |
| 1407 |
layout, chain_link_dict = self._verify_link_signature_tresholds( |
| 1408 |
self.gpg_key_D924E9, self.gpg_key_D924E9, self.gpg_key_0C8A17 |
| 1409 |
) |
| 1410 |
verify_link_signature_thresholds(layout, chain_link_dict) |
| 1411 |
|
| 1412 |
def test_verify_link_signature_thresholds__S_S_S(self): |
| 1413 |
"""Generalizes to normal scenario.""" |
| 1414 |
layout, chain_link_dict = self._verify_link_signature_tresholds( |
| 1415 |
self.gpg_key_D924E9, self.gpg_key_D924E9, self.gpg_key_D924E9 |
| 1416 |
) |
| 1417 |
verify_link_signature_thresholds(layout, chain_link_dict) |
| 1418 |
|
| 1419 |
def test_verify_subkey_thresholds(self): |
| 1420 |
"""Subkeys of same main key count only once towards threshold.""" |
| 1421 |
|
| 1422 |
masterkey = "40e692c3ae03f6b88dff95d0d2c9fe930766998d" |
| 1423 |
subkey1 = "35830aa342b9fea0178876b02b25647ff0ef59fe" |
| 1424 |
subkey2 = "732d722578f71a9ec967a64bfead922c91eb7351" |
| 1425 |
|
| 1426 |
link1 = Metablock(signed=Link(name=self.step_name)) |
| 1427 |
link1.sign_gpg(subkey1, self.gnupg_home) |
| 1428 |
link2 = Metablock(signed=Link(name=self.step_name)) |
| 1429 |
link2.sign_gpg(subkey2, self.gnupg_home) |
| 1430 |
|
| 1431 |
chain_link_dict = {self.step_name: {subkey1: link1, subkey2: link2}} |
| 1432 |
|
| 1433 |
layout = Layout( |
| 1434 |
steps=[Step(name=self.step_name, pubkeys=[masterkey], threshold=2)], |
| 1435 |
keys={ |
| 1436 |
masterkey: securesystemslib.gpg.functions.export_pubkey( |
| 1437 |
masterkey, self.gnupg_home |
| 1438 |
) |
| 1439 |
}, |
| 1440 |
) |
| 1441 |
with self.assertRaises(ThresholdVerificationError): |
| 1442 |
verify_link_signature_thresholds(layout, chain_link_dict) |
| 1443 |
|
| 1444 |
def test_verify_thresholds_skip_expired_key(self): |
| 1445 |
"""Verify that a link signed with an expired key is skipped. |
| 1446 |
|
| 1447 |
NOTE: This test would be a better fit for `TestInTotoVerifyThresholds`, |
| 1448 |
but we make use of `TestInTotoVerifyThresholdsGpgSubkeys`'s gpg setup here. |
| 1449 |
|
| 1450 |
""" |
| 1451 |
expired_key_id = "e8ac80c924116dabb51d4b987cb07d6d2c199c7c" |
| 1452 |
expired_key = securesystemslib.gpg.functions.export_pubkey( |
| 1453 |
expired_key_id, self.gnupg_home |
| 1454 |
) |
| 1455 |
|
| 1456 |
# Chain link dict containing a single link for a single step |
| 1457 |
# The link's signature is (supposedly) signed by an expired key and |
| 1458 |
# hence does not count towards the link threshold as defined in the layout. |
| 1459 |
chain_link_dict = { |
| 1460 |
self.step_name: { |
| 1461 |
expired_key_id: Metablock( |
| 1462 |
signed=Link(name=self.step_name), |
| 1463 |
signatures=[ |
| 1464 |
{ |
| 1465 |
"keyid": expired_key_id, |
| 1466 |
"other_headers": "deadbeef", |
| 1467 |
"signature": "deadbeef", |
| 1468 |
} |
| 1469 |
], |
| 1470 |
) |
| 1471 |
} |
| 1472 |
} |
| 1473 |
layout = Layout( |
| 1474 |
steps=[ |
| 1475 |
Step(name=self.step_name, pubkeys=[expired_key_id], threshold=1) |
| 1476 |
], |
| 1477 |
keys={expired_key_id: expired_key}, |
| 1478 |
) |
| 1479 |
|
| 1480 |
with self.assertRaises(ThresholdVerificationError), patch( |
| 1481 |
"in_toto.verifylib.LOG" |
| 1482 |
) as mock_log: |
| 1483 |
verify_link_signature_thresholds(layout, chain_link_dict) |
| 1484 |
|
| 1485 |
msg = mock_log.info.call_args[0][0] |
| 1486 |
self.assertTrue( |
| 1487 |
"Skipping link" in msg and "expired" in msg, |
| 1488 |
"Unexpected log message: {}".format(msg), |
| 1489 |
) |
| 1490 |
|
| 1491 |
|
| 1492 |
class TestVerifySublayouts(unittest.TestCase, TmpDirMixin): |
| 1493 |
"""Tests verifylib.verify_sublayouts(layout, reduced_chain_link_dict). |
| 1494 |
Call with one-step super layout that has a sublayout (demo layout).""" |
| 1495 |
|
| 1496 |
@classmethod |
| 1497 |
def setUpClass(self): |
| 1498 |
"""Creates and changes into temporary directory and prepares two layouts. |
| 1499 |
The superlayout, which has one step and its sublayout, which is the usual |
| 1500 |
demo layout (write code, package, inspect tar).""" |
| 1501 |
# Find demo files |
| 1502 |
demo_files = os.path.join( |
| 1503 |
os.path.dirname(os.path.realpath(__file__)), "demo_files" |
| 1504 |
) |
| 1505 |
|
| 1506 |
# find where the scripts directory is located. |
| 1507 |
scripts_directory = os.path.join( |
| 1508 |
os.path.dirname(os.path.realpath(__file__)), "scripts" |
| 1509 |
) |
| 1510 |
|
| 1511 |
# Create and change into temporary directory |
| 1512 |
self.set_up_test_dir() |
| 1513 |
|
| 1514 |
# Copy demo files to temp dir |
| 1515 |
for fn in os.listdir(demo_files): |
| 1516 |
shutil.copy(os.path.join(demo_files, fn), self.test_dir) |
| 1517 |
|
| 1518 |
# copy portable scripts over |
| 1519 |
shutil.copytree(scripts_directory, "scripts") |
| 1520 |
|
| 1521 |
# Import sub layout signing (private) and verifying (public) keys |
| 1522 |
alice = import_rsa_privatekey_from_file("alice") |
| 1523 |
alice_pub = import_rsa_publickey_from_file("alice.pub") |
| 1524 |
|
| 1525 |
# From the perspective of the superlayout, the sublayout is treated as |
| 1526 |
# a link corresponding to a step, hence needs a name. |
| 1527 |
sub_layout_name = "sub_layout" |
| 1528 |
|
| 1529 |
# Sublayout links are expected in a directory relative to the superlayout's |
| 1530 |
# link directory |
| 1531 |
sub_layout_link_dir = SUBLAYOUT_LINK_DIR_FORMAT.format( |
| 1532 |
name=sub_layout_name, keyid=alice["keyid"] |
| 1533 |
) |
| 1534 |
|
| 1535 |
for sublayout_link_name in glob.glob("*.link"): |
| 1536 |
dest_path = os.path.join(sub_layout_link_dir, sublayout_link_name) |
| 1537 |
os.renames(sublayout_link_name, dest_path) |
| 1538 |
|
| 1539 |
# Copy, sign and dump sub layout as link from template |
| 1540 |
layout_template = Metablock.load("demo.layout.template") |
| 1541 |
sub_layout = copy.deepcopy(layout_template) |
| 1542 |
sub_layout_path = FILENAME_FORMAT.format( |
| 1543 |
step_name=sub_layout_name, keyid=alice_pub["keyid"] |
| 1544 |
) |
| 1545 |
sub_layout.sign(alice) |
| 1546 |
sub_layout.dump(sub_layout_path) |
| 1547 |
|
| 1548 |
# Create super layout that has only one step, the sublayout |
| 1549 |
self.super_layout = Layout() |
| 1550 |
self.super_layout.keys[alice_pub["keyid"]] = alice_pub |
| 1551 |
sub_layout_step = Step( |
| 1552 |
name=sub_layout_name, pubkeys=[alice_pub["keyid"]] |
| 1553 |
) |
| 1554 |
self.super_layout.steps.append(sub_layout_step) |
| 1555 |
|
| 1556 |
# Load the super layout links (i.e. the sublayout) |
| 1557 |
self.super_layout_links = load_links_for_layout(self.super_layout, ".") |
| 1558 |
|
| 1559 |
@classmethod |
| 1560 |
def tearDownClass(self): |
| 1561 |
self.tear_down_test_dir() |
| 1562 |
|
| 1563 |
def test_verify_demo_as_sublayout(self): |
| 1564 |
"""Test super layout's passing sublayout verification.""" |
| 1565 |
verify_sublayouts(self.super_layout, self.super_layout_links, ".") |
| 1566 |
|
| 1567 |
|
| 1568 |
class TestInTotoVerifyMultiLevelSublayouts(unittest.TestCase, TmpDirMixin): |
| 1569 |
"""Test verifylib.in_toto_verify with multiple levels of sublayouts.""" |
| 1570 |
|
| 1571 |
def test_verify_multi_level_sublayout(self): |
| 1572 |
# Find demo files |
| 1573 |
demo_files = os.path.join( |
| 1574 |
os.path.dirname(os.path.realpath(__file__)), "demo_files" |
| 1575 |
) |
| 1576 |
|
| 1577 |
# Create and change into temporary directory |
| 1578 |
self.set_up_test_dir() |
| 1579 |
|
| 1580 |
# We don't need to copy the demo files, we just load the keys |
| 1581 |
keys = {} |
| 1582 |
for key_name in ["alice", "bob", "carl"]: |
| 1583 |
keys[key_name + "_priv"] = import_rsa_privatekey_from_file( |
| 1584 |
os.path.join(demo_files, key_name) |
| 1585 |
) |
| 1586 |
keys[key_name + "_pub"] = import_rsa_publickey_from_file( |
| 1587 |
os.path.join(demo_files, key_name + ".pub") |
| 1588 |
) |
| 1589 |
|
| 1590 |
# Create layout hierarchy |
| 1591 |
|
| 1592 |
# Root layout |
| 1593 |
# The root layout is the layout that will be passed to `in_toto_verify` |
| 1594 |
# It only has one step which is a sublayout, into which verification |
| 1595 |
# recurses. Only the root layout and root layout verification key will be |
| 1596 |
# passed to verification. |
| 1597 |
root_layout_pub_key_dict = { |
| 1598 |
keys["alice_pub"]["keyid"]: keys["alice_pub"] |
| 1599 |
} |
| 1600 |
|
| 1601 |
root_layout_step_name = "delegated-to-bob" |
| 1602 |
|
| 1603 |
root_layout = Metablock( |
| 1604 |
signed=Layout( |
| 1605 |
keys={keys["bob_pub"]["keyid"]: keys["bob_pub"]}, |
| 1606 |
steps=[ |
| 1607 |
Step( |
| 1608 |
name=root_layout_step_name, |
| 1609 |
pubkeys=[keys["bob_pub"]["keyid"]], |
| 1610 |
) |
| 1611 |
], |
| 1612 |
) |
| 1613 |
) |
| 1614 |
root_layout.sign(keys["alice_priv"]) |
| 1615 |
|
| 1616 |
# Sublayout (first level) |
| 1617 |
# The first level sublayout wil be treated as a link from the |
| 1618 |
# superlayout's perspective and loaded from the current working directory. |
| 1619 |
# The link for the only step of this sublayout will be placed in a |
| 1620 |
# namespaced subdir, that link itself is a sublayout (subsublayout). |
| 1621 |
bobs_layout_name = FILENAME_FORMAT.format( |
| 1622 |
step_name=root_layout_step_name, keyid=keys["bob_pub"]["keyid"] |
| 1623 |
) |
| 1624 |
|
| 1625 |
bobs_layout_link_dir = SUBLAYOUT_LINK_DIR_FORMAT.format( |
| 1626 |
name=root_layout_step_name, keyid=keys["bob_pub"]["keyid"] |
| 1627 |
) |
| 1628 |
os.mkdir(bobs_layout_link_dir) |
| 1629 |
|
| 1630 |
bobs_layout_step_name = "delegated-to-carl" |
| 1631 |
|
| 1632 |
bobs_layout = Metablock( |
| 1633 |
signed=Layout( |
| 1634 |
keys={keys["carl_pub"]["keyid"]: keys["carl_pub"]}, |
| 1635 |
steps=[ |
| 1636 |
Step( |
| 1637 |
name=bobs_layout_step_name, |
| 1638 |
pubkeys=[keys["carl_pub"]["keyid"]], |
| 1639 |
) |
| 1640 |
], |
| 1641 |
) |
| 1642 |
) |
| 1643 |
bobs_layout.sign(keys["bob_priv"]) |
| 1644 |
bobs_layout.dump(bobs_layout_name) |
| 1645 |
|
| 1646 |
# Subsublayout (second level) |
| 1647 |
# The subsublayout will be placed in the namespaced link dir |
| 1648 |
# of its superlayout (sublayout from the root layout's perspective), for |
| 1649 |
# for which it serves as link. |
| 1650 |
carls_layout_name = FILENAME_FORMAT.format( |
| 1651 |
step_name=bobs_layout_step_name, keyid=keys["carl_pub"]["keyid"] |
| 1652 |
) |
| 1653 |
|
| 1654 |
carls_layout_path = os.path.join( |
| 1655 |
bobs_layout_link_dir, carls_layout_name |
| 1656 |
) |
| 1657 |
carls_layout = Metablock(signed=Layout()) |
| 1658 |
carls_layout.sign(keys["carl_priv"]) |
| 1659 |
carls_layout.dump(carls_layout_path) |
| 1660 |
|
| 1661 |
in_toto_verify(root_layout, root_layout_pub_key_dict) |
| 1662 |
|
| 1663 |
self.tear_down_test_dir() |
| 1664 |
|
| 1665 |
|
| 1666 |
class TestSublayoutVerificationMatchRule(unittest.TestCase, TmpDirMixin): |
| 1667 |
"""Tests a sublayout and checks if a MATCH rule is successful after sublayout |
| 1668 |
is resolved into a summary link.""" |
| 1669 |
|
| 1670 |
def test_verify_sublayout_match_rule(self): |
| 1671 |
# Find demo files |
| 1672 |
demo_files = os.path.join( |
| 1673 |
os.path.dirname(os.path.realpath(__file__)), "demo_files" |
| 1674 |
) |
| 1675 |
|
| 1676 |
script_files = os.path.join( |
| 1677 |
os.path.dirname(os.path.realpath(__file__)), "scripts" |
| 1678 |
) |
| 1679 |
|
| 1680 |
# Create and change into temporary directory |
| 1681 |
self.set_up_test_dir() |
| 1682 |
|
| 1683 |
# We don't need to copy the demo files, we just load the keys |
| 1684 |
keys = {} |
| 1685 |
for key_name in ["alice", "bob"]: |
| 1686 |
keys[key_name + "_priv"] = import_rsa_privatekey_from_file( |
| 1687 |
os.path.join(demo_files, key_name) |
| 1688 |
) |
| 1689 |
keys[key_name + "_pub"] = import_rsa_publickey_from_file( |
| 1690 |
os.path.join(demo_files, key_name + ".pub") |
| 1691 |
) |
| 1692 |
|
| 1693 |
# Create layout hierarchy |
| 1694 |
|
| 1695 |
# Root layout |
| 1696 |
# The root layout is the layout that will be passed to `in_toto_verify` |
| 1697 |
# It only has one step which is a sublayout, into which verification |
| 1698 |
# recurses. Only the root layout and root layout verification key will be |
| 1699 |
# passed to verification. |
| 1700 |
root_layout_pub_key_dict = { |
| 1701 |
keys["alice_pub"]["keyid"]: keys["alice_pub"] |
| 1702 |
} |
| 1703 |
|
| 1704 |
root_layout_step_name = "delegated-to-bob" |
| 1705 |
|
| 1706 |
root_layout = Metablock( |
| 1707 |
signed=Layout( |
| 1708 |
keys={keys["bob_pub"]["keyid"]: keys["bob_pub"]}, |
| 1709 |
steps=[ |
| 1710 |
Step( |
| 1711 |
name=root_layout_step_name, |
| 1712 |
pubkeys=[keys["bob_pub"]["keyid"]], |
| 1713 |
expected_products=[ |
| 1714 |
[ |
| 1715 |
"MATCH", |
| 1716 |
"foo.tar.gz", |
| 1717 |
"WITH", |
| 1718 |
"PRODUCTS", |
| 1719 |
"FROM", |
| 1720 |
root_layout_step_name, |
| 1721 |
], |
| 1722 |
["DISALLOW", "*"], |
| 1723 |
], |
| 1724 |
) |
| 1725 |
], |
| 1726 |
) |
| 1727 |
) |
| 1728 |
root_layout.sign(keys["alice_priv"]) |
| 1729 |
|
| 1730 |
# Sublayout (first level) |
| 1731 |
# The sublayout will be treated as a link from the superlayout's |
| 1732 |
# perspective and loaded from the current working directory. The links for |
| 1733 |
# the steps of this sublayout will be placed in a namespaced subdir. |
| 1734 |
bobs_layout_name = FILENAME_FORMAT.format( |
| 1735 |
step_name=root_layout_step_name, keyid=keys["bob_pub"]["keyid"] |
| 1736 |
) |
| 1737 |
|
| 1738 |
bobs_layout_link_dir = SUBLAYOUT_LINK_DIR_FORMAT.format( |
| 1739 |
name=root_layout_step_name, keyid=keys["bob_pub"]["keyid"] |
| 1740 |
) |
| 1741 |
os.mkdir(bobs_layout_link_dir) |
| 1742 |
|
| 1743 |
bobs_layout = Metablock.load( |
| 1744 |
os.path.join(demo_files, "demo.layout.template") |
| 1745 |
) |
| 1746 |
bobs_layout.sign(keys["bob_priv"]) |
| 1747 |
bobs_layout.dump(bobs_layout_name) |
| 1748 |
shutil.copy2( |
| 1749 |
os.path.join(demo_files, "write-code.776a00e2.link"), |
| 1750 |
bobs_layout_link_dir, |
| 1751 |
) |
| 1752 |
shutil.copy2( |
| 1753 |
os.path.join(demo_files, "package.2f89b927.link"), |
| 1754 |
bobs_layout_link_dir, |
| 1755 |
) |
| 1756 |
shutil.copy2(os.path.join(demo_files, "foo.tar.gz"), ".") |
| 1757 |
shutil.copytree(script_files, os.path.join(".", "scripts")) |
| 1758 |
|
| 1759 |
in_toto_verify(root_layout, root_layout_pub_key_dict) |
| 1760 |
|
| 1761 |
self.tear_down_test_dir() |
| 1762 |
|
| 1763 |
|
| 1764 |
class TestGetSummaryLink(unittest.TestCase, TmpDirMixin): |
| 1765 |
"""Tests verifylib.get_summary_link(layout, reduced_chain_link_dict). |
| 1766 |
Pass two step demo layout and according link files and verify the |
| 1767 |
returned summary link. |
| 1768 |
""" |
| 1769 |
|
| 1770 |
@classmethod |
| 1771 |
def setUpClass(self): |
| 1772 |
"""Creates and changes into temporary directory and prepares two layouts. |
| 1773 |
The superlayout, which has one step and its sublayout, which is the usual |
| 1774 |
demo layout (write code, package, inspect tar).""" |
| 1775 |
|
| 1776 |
# Find demo files |
| 1777 |
demo_files = os.path.join( |
| 1778 |
os.path.dirname(os.path.realpath(__file__)), "demo_files" |
| 1779 |
) |
| 1780 |
|
| 1781 |
# Create and change into temporary directory |
| 1782 |
self.set_up_test_dir() |
| 1783 |
|
| 1784 |
# Copy demo files to temp dir |
| 1785 |
for fn in os.listdir(demo_files): |
| 1786 |
shutil.copy(os.path.join(demo_files, fn), self.test_dir) |
| 1787 |
|
| 1788 |
self.demo_layout = Metablock.load("demo.layout.template") |
| 1789 |
self.code_link = Metablock.load("package.2f89b927.link") |
| 1790 |
self.package_link = Metablock.load("write-code.776a00e2.link") |
| 1791 |
self.demo_links = { |
| 1792 |
"write-code": self.code_link.signed, |
| 1793 |
"package": self.package_link.signed, |
| 1794 |
} |
| 1795 |
|
| 1796 |
@classmethod |
| 1797 |
def tearDownClass(self): |
| 1798 |
self.tear_down_test_dir() |
| 1799 |
|
| 1800 |
def test_get_summary_link_from_demo_layout(self): |
| 1801 |
"""Create summary link from demo link files and compare properties.""" |
| 1802 |
sum_link = get_summary_link( |
| 1803 |
self.demo_layout.signed, self.demo_links, "" |
| 1804 |
) |
| 1805 |
|
| 1806 |
self.assertEqual(sum_link._type, self.code_link.signed._type) |
| 1807 |
self.assertEqual(sum_link.name, "") |
| 1808 |
self.assertEqual(sum_link.materials, self.code_link.signed.materials) |
| 1809 |
|
| 1810 |
self.assertEqual(sum_link.products, self.package_link.signed.products) |
| 1811 |
self.assertEqual(sum_link.command, self.package_link.signed.command) |
| 1812 |
self.assertEqual( |
| 1813 |
sum_link.byproducts, self.package_link.signed.byproducts |
| 1814 |
) |
| 1815 |
self.assertEqual( |
| 1816 |
sum_link.byproducts.get("return-value"), |
| 1817 |
self.package_link.signed.byproducts.get("return-value"), |
| 1818 |
) |
| 1819 |
|
| 1820 |
|
| 1821 |
if __name__ == "__main__": |
| 1822 |
unittest.main() |