| 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_layout.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Santiago Torres-Arias <[email protected]> |
| 12 |
|
| 13 |
<Started> |
| 14 |
Nov 18, 2016 |
| 15 |
|
| 16 |
<Copyright> |
| 17 |
See LICENSE for licensing information. |
| 18 |
|
| 19 |
<Purpose> |
| 20 |
Test layout class functions. |
| 21 |
|
| 22 |
""" |
| 23 |
|
| 24 |
import os |
| 25 |
import shutil |
| 26 |
import unittest |
| 27 |
|
| 28 |
import securesystemslib.exceptions |
| 29 |
|
| 30 |
import in_toto.exceptions |
| 31 |
import in_toto.models.link |
| 32 |
import in_toto.verifylib |
| 33 |
from in_toto.models.layout import Inspection, Layout, Step |
| 34 |
from in_toto.models.metadata import Metablock |
| 35 |
from tests.common import GPGKeysMixin, TmpDirMixin |
| 36 |
|
| 37 |
|
| 38 |
class TestLayoutMethods(unittest.TestCase, TmpDirMixin, GPGKeysMixin): |
| 39 |
"""Test Layout methods.""" |
| 40 |
|
| 41 |
@classmethod |
| 42 |
def setUpClass(self): |
| 43 |
"""Create temporary test directory and copy gpg keychain, and rsa keys from |
| 44 |
demo files.""" |
| 45 |
demo_files = os.path.join( |
| 46 |
os.path.dirname(os.path.realpath(__file__)), "..", "demo_files" |
| 47 |
) |
| 48 |
|
| 49 |
self.set_up_test_dir() |
| 50 |
self.set_up_gpg_keys() |
| 51 |
|
| 52 |
# Copy keys to temp test dir |
| 53 |
key_names = ["bob", "bob.pub", "carl.pub"] |
| 54 |
for name in key_names: |
| 55 |
shutil.copy(os.path.join(demo_files, name), name) |
| 56 |
|
| 57 |
self.key_path = os.path.join(self.test_dir, "bob") |
| 58 |
self.pubkey_path1 = os.path.join(self.test_dir, "bob.pub") |
| 59 |
self.pubkey_path2 = os.path.join(self.test_dir, "carl.pub") |
| 60 |
|
| 61 |
@classmethod |
| 62 |
def tearDownClass(self): |
| 63 |
self.tear_down_test_dir() |
| 64 |
|
| 65 |
def test_set_relative_expiration(self): |
| 66 |
"""Test adding expiration date relative from today.""" |
| 67 |
layout = Layout() |
| 68 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 69 |
layout.set_relative_expiration(days=None, months=0, years=0) |
| 70 |
|
| 71 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 72 |
layout.set_relative_expiration(days=0, months="", years=0) |
| 73 |
|
| 74 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 75 |
layout.set_relative_expiration(days=0, months=0, years=[]) |
| 76 |
|
| 77 |
layout.set_relative_expiration(days=1) |
| 78 |
layout._validate_expires() |
| 79 |
layout.set_relative_expiration(months=2) |
| 80 |
layout._validate_expires() |
| 81 |
layout.set_relative_expiration(years=3) |
| 82 |
layout._validate_expires() |
| 83 |
layout.set_relative_expiration(days=3, months=2, years=1) |
| 84 |
layout._validate_expires() |
| 85 |
|
| 86 |
# It's possible to add an expiration date in the past |
| 87 |
layout.set_relative_expiration(days=-3, months=-2, years=-1) |
| 88 |
layout._validate_expires() |
| 89 |
|
| 90 |
def test_get_step_name_list(self): |
| 91 |
"""Test getting list of step names.""" |
| 92 |
names = ["a", "b", "c"] |
| 93 |
layout = Layout(steps=[Step(name=name) for name in names]) |
| 94 |
self.assertListEqual(layout.get_step_name_list(), names) |
| 95 |
|
| 96 |
def test_get_step_by_name(self): |
| 97 |
"""Test getting step by name.""" |
| 98 |
names = ["a", "b", "c"] |
| 99 |
layout = Layout(steps=[Step(name=name) for name in names]) |
| 100 |
self.assertEqual(layout.get_step_by_name("b").name, "b") |
| 101 |
|
| 102 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 103 |
layout.get_step_by_name(None) |
| 104 |
|
| 105 |
def test_remove_step_by_name(self): |
| 106 |
"""Test removing step by name.""" |
| 107 |
names = ["a", "b", "c"] |
| 108 |
layout = Layout(steps=[Step(name=name) for name in names]) |
| 109 |
|
| 110 |
self.assertEqual(len(layout.steps), 3) |
| 111 |
self.assertTrue("b" in layout.get_step_name_list()) |
| 112 |
# Items are only removed if they exist |
| 113 |
for _ in range(2): |
| 114 |
layout.remove_step_by_name("b") |
| 115 |
self.assertEqual(len(layout.steps), 2) |
| 116 |
self.assertTrue("b" not in layout.get_step_name_list()) |
| 117 |
|
| 118 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 119 |
layout.get_step_by_name([]) |
| 120 |
|
| 121 |
def test_get_inspection_name_list(self): |
| 122 |
"""Test getting list of inspection names.""" |
| 123 |
names = ["a", "b", "c"] |
| 124 |
layout = Layout(inspect=[Inspection(name=name) for name in names]) |
| 125 |
self.assertListEqual(layout.get_inspection_name_list(), names) |
| 126 |
|
| 127 |
def test_get_inspection_by_name(self): |
| 128 |
"""Test getting inspection by name.""" |
| 129 |
names = ["a", "b", "c"] |
| 130 |
layout = Layout(inspect=[Inspection(name=name) for name in names]) |
| 131 |
self.assertEqual(layout.get_inspection_by_name("b").name, "b") |
| 132 |
|
| 133 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 134 |
layout.get_inspection_by_name(1) |
| 135 |
|
| 136 |
def test_remove_inspection_by_name(self): |
| 137 |
"""Test removing inspection by name.""" |
| 138 |
names = ["a", "b", "c"] |
| 139 |
layout = Layout(inspect=[Inspection(name=name) for name in names]) |
| 140 |
|
| 141 |
self.assertEqual(len(layout.inspect), 3) |
| 142 |
self.assertTrue("b" in layout.get_inspection_name_list()) |
| 143 |
# Items are only removed if they exist |
| 144 |
for _ in range(2): |
| 145 |
layout.remove_inspection_by_name("b") |
| 146 |
self.assertEqual(len(layout.inspect), 2) |
| 147 |
self.assertTrue("b" not in layout.get_inspection_name_list()) |
| 148 |
|
| 149 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 150 |
layout.remove_inspection_by_name(False) |
| 151 |
|
| 152 |
def test_functionary_keys(self): |
| 153 |
"""Test adding and listing functionary keys (securesystemslib and gpg).""" |
| 154 |
layout = Layout() |
| 155 |
self.assertEqual(len(layout.get_functionary_key_id_list()), 0) |
| 156 |
|
| 157 |
layout.add_functionary_keys_from_paths( |
| 158 |
[self.pubkey_path1, self.pubkey_path2] |
| 159 |
) |
| 160 |
|
| 161 |
layout.add_functionary_keys_from_gpg_keyids( |
| 162 |
[self.gpg_key_768C43, self.gpg_key_85DA58], gpg_home=self.gnupg_home |
| 163 |
) |
| 164 |
|
| 165 |
layout._validate_keys() |
| 166 |
|
| 167 |
self.assertEqual(len(layout.get_functionary_key_id_list()), 4) |
| 168 |
|
| 169 |
# Must be a valid key object |
| 170 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 171 |
layout.add_functionary_key("abcd") |
| 172 |
|
| 173 |
# Must be pubkey and not private key |
| 174 |
with self.assertRaises(securesystemslib.exceptions.Error): |
| 175 |
layout.add_functionary_key_from_path(self.key_path) |
| 176 |
|
| 177 |
# Must be a valid path |
| 178 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 179 |
layout.add_functionary_key_from_path(123) |
| 180 |
|
| 181 |
# Must be a valid keyid |
| 182 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 183 |
layout.add_functionary_key_from_gpg_keyid("abcdefg") |
| 184 |
|
| 185 |
# Must be a list of paths |
| 186 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 187 |
layout.add_functionary_keys_from_paths("abcd") |
| 188 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 189 |
layout.add_functionary_keys_from_paths([1]) |
| 190 |
|
| 191 |
# Must be a list of keyids |
| 192 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 193 |
layout.add_functionary_keys_from_gpg_keyids(None) |
| 194 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 195 |
layout.add_functionary_keys_from_gpg_keyids(["abcdefg"]) |
| 196 |
|
| 197 |
|
| 198 |
class TestLayoutValidator(unittest.TestCase): |
| 199 |
"""Test in_toto.models.layout.Layout validators.""" |
| 200 |
|
| 201 |
def setUp(self): |
| 202 |
"""Populate a base layout that we can use.""" |
| 203 |
self.layout = Layout() |
| 204 |
self.layout.expires = "2016-11-18T16:44:55Z" |
| 205 |
|
| 206 |
def test_wrong_type(self): |
| 207 |
"""Test that the type field is validated properly.""" |
| 208 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 209 |
self.layout._type = "wrong" |
| 210 |
self.layout._validate_type() |
| 211 |
self.layout.validate() |
| 212 |
|
| 213 |
self.layout._type = "layout" |
| 214 |
self.layout._validate_type() |
| 215 |
|
| 216 |
def test_validate_readme_field(self): |
| 217 |
"""Tests the readme field data type validator.""" |
| 218 |
self.layout.readme = 1 |
| 219 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 220 |
self.layout._validate_readme() |
| 221 |
|
| 222 |
self.layout.readme = "This is a test supply chain" |
| 223 |
self.layout._validate_readme() |
| 224 |
|
| 225 |
def test_wrong_expires(self): |
| 226 |
"""Test the expires field is properly populated.""" |
| 227 |
|
| 228 |
self.layout.expires = "" |
| 229 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 230 |
self.layout._validate_expires() |
| 231 |
|
| 232 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 233 |
self.layout.validate() |
| 234 |
|
| 235 |
self.layout.expires = "-1" |
| 236 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 237 |
self.layout._validate_expires() |
| 238 |
|
| 239 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 240 |
self.layout.validate() |
| 241 |
|
| 242 |
# notice the wrong month |
| 243 |
self.layout.expires = "2016-13-18T16:44:55Z" |
| 244 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 245 |
self.layout._validate_expires() |
| 246 |
|
| 247 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 248 |
self.layout.validate() |
| 249 |
|
| 250 |
self.layout.expires = "2016-11-18T16:44:55Z" |
| 251 |
self.layout._validate_expires() |
| 252 |
self.layout.validate() |
| 253 |
|
| 254 |
def test_wrong_key_dictionary(self): |
| 255 |
"""Test that the keys dictionary is properly populated.""" |
| 256 |
rsa_key_one = securesystemslib.keys.generate_rsa_key() |
| 257 |
rsa_key_two = securesystemslib.keys.generate_rsa_key() |
| 258 |
|
| 259 |
# FIXME: attr.ib reutilizes the default dictionary, so future constructor |
| 260 |
# are not empty... |
| 261 |
self.layout.keys = {"kek": rsa_key_one} |
| 262 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 263 |
self.layout._validate_keys() |
| 264 |
|
| 265 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 266 |
self.layout.validate() |
| 267 |
|
| 268 |
self.layout.keys = {} |
| 269 |
self.layout.keys[rsa_key_two["keyid"]] = "kek" |
| 270 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 271 |
self.layout._validate_keys() |
| 272 |
|
| 273 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 274 |
self.layout.validate() |
| 275 |
|
| 276 |
self.layout.keys = {} |
| 277 |
del rsa_key_one["keyval"]["private"] |
| 278 |
del rsa_key_two["keyval"]["private"] |
| 279 |
self.layout.keys[rsa_key_one["keyid"]] = rsa_key_one |
| 280 |
self.layout.keys[rsa_key_two["keyid"]] = rsa_key_two |
| 281 |
|
| 282 |
self.layout._validate_keys() |
| 283 |
self.layout.validate() |
| 284 |
|
| 285 |
def test_wrong_steps_list(self): |
| 286 |
"""Check that the validate method checks the steps' correctness.""" |
| 287 |
self.layout.steps = "not-a-step" |
| 288 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 289 |
self.layout.validate() |
| 290 |
|
| 291 |
self.layout.steps = ["not-a-step"] |
| 292 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 293 |
self.layout.validate() |
| 294 |
|
| 295 |
test_step = Step(name="this-is-a-step") |
| 296 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 297 |
test_step.expected_materials = ["this is a malformed step"] |
| 298 |
self.layout.steps = [test_step] |
| 299 |
self.layout.validate() |
| 300 |
|
| 301 |
test_step = Step(name="this-is-a-step") |
| 302 |
test_step.expected_materials = [["CREATE", "foo"]] |
| 303 |
test_step.threshold = 1 |
| 304 |
self.layout.steps = [test_step] |
| 305 |
self.layout.validate() |
| 306 |
|
| 307 |
def test_wrong_inspect_list(self): |
| 308 |
"""Check that the validate method checks the inspections' correctness.""" |
| 309 |
|
| 310 |
self.layout.inspect = "not-an-inspection" |
| 311 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 312 |
self.layout.validate() |
| 313 |
|
| 314 |
self.layout.inspect = ["not-an-inspection"] |
| 315 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 316 |
self.layout.validate() |
| 317 |
|
| 318 |
test_inspection = Inspection(name="this-is-a-step") |
| 319 |
test_inspection.expected_materials = [ |
| 320 |
"this is a malformed artifact rule" |
| 321 |
] |
| 322 |
self.layout.inspect = [test_inspection] |
| 323 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 324 |
self.layout.validate() |
| 325 |
|
| 326 |
test_inspection = Inspection(name="this-is-a-step") |
| 327 |
test_inspection.expected_materials = [["CREATE", "foo"]] |
| 328 |
self.layout.inspect = [test_inspection] |
| 329 |
self.layout.validate() |
| 330 |
|
| 331 |
def test_repeated_step_names(self): |
| 332 |
"""Check that only unique names exist in the steps and inspect lists""" |
| 333 |
|
| 334 |
self.layout.steps = [Step(name="name"), Step(name="name")] |
| 335 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 336 |
self.layout.validate() |
| 337 |
|
| 338 |
self.layout.steps = [Step(name="name")] |
| 339 |
self.layout.inspect = [Inspection(name="name")] |
| 340 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 341 |
self.layout.validate() |
| 342 |
|
| 343 |
self.layout.step = [Step(name="name"), Step(name="othername")] |
| 344 |
self.layout.inspect = [Inspection(name="thirdname")] |
| 345 |
self.layout.validate() |
| 346 |
|
| 347 |
def test_import_step_metadata_wrong_type(self): |
| 348 |
functionary_key = securesystemslib.keys.generate_rsa_key() |
| 349 |
name = "name" |
| 350 |
|
| 351 |
# Create and dump a link file with a wrong type |
| 352 |
link_name = in_toto.models.link.FILENAME_FORMAT.format( |
| 353 |
step_name=name, keyid=functionary_key["keyid"] |
| 354 |
) |
| 355 |
|
| 356 |
link_path = os.path.abspath(link_name) |
| 357 |
link = in_toto.models.link.Link(name=name) |
| 358 |
metadata = Metablock(signed=link) |
| 359 |
metadata.signed._type = "wrong-type" |
| 360 |
metadata.dump(link_path) |
| 361 |
|
| 362 |
# Add the single step to the test layout and try to read the failing link |
| 363 |
self.layout.steps.append( |
| 364 |
Step(name=name, pubkeys=[functionary_key["keyid"]]) |
| 365 |
) |
| 366 |
|
| 367 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 368 |
in_toto.verifylib.load_links_for_layout(self.layout, ".") |
| 369 |
|
| 370 |
# Clean up |
| 371 |
os.remove(link_path) |
| 372 |
|
| 373 |
def test_wrong_pubkeys(self): |
| 374 |
"""Check validate pubkeys fails with wrong keys.""" |
| 375 |
# Pubkeys must be lists ... |
| 376 |
tmp_step = Step() |
| 377 |
tmp_step.pubkeys = "abcd" |
| 378 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 379 |
tmp_step.validate() |
| 380 |
|
| 381 |
# ... of keyids (hex schema) |
| 382 |
tmp_step.pubkeys = ["abcdefg"] |
| 383 |
with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 384 |
tmp_step.validate() |
| 385 |
|
| 386 |
|
| 387 |
if __name__ == "__main__": |
| 388 |
unittest.main() |