| 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 |
link.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Lukas Puehringer <[email protected]> |
| 12 |
Santiago Torres <[email protected]> |
| 13 |
|
| 14 |
<Started> |
| 15 |
Sep 23, 2016 |
| 16 |
|
| 17 |
<Copyright> |
| 18 |
See LICENSE for licensing information. |
| 19 |
|
| 20 |
<Purpose> |
| 21 |
Provides a class for link metadata which is information gathered when a |
| 22 |
step of the supply chain is performed. |
| 23 |
""" |
| 24 |
|
| 25 |
import attr |
| 26 |
import securesystemslib.formats |
| 27 |
|
| 28 |
from in_toto.models.common import Signable |
| 29 |
|
| 30 |
FILENAME_FORMAT = "{step_name}.{keyid:.8}.link" |
| 31 |
FILENAME_FORMAT_SHORT = "{step_name}.link" |
| 32 |
UNFINISHED_FILENAME_FORMAT = ".{step_name}.{keyid:.8}.link-unfinished" |
| 33 |
UNFINISHED_FILENAME_FORMAT_GLOB = ".{step_name}.{pattern}.link-unfinished" |
| 34 |
|
| 35 |
|
| 36 |
@attr.s(repr=False, init=False) |
| 37 |
class Link(Signable): |
| 38 |
"""Evidence for a performed step or inspection of the supply chain. |
| 39 |
|
| 40 |
A Link object is usually contained in a generic Metablock object for signing, |
| 41 |
serialization and I/O capabilities. |
| 42 |
|
| 43 |
Attributes: |
| 44 |
name: A unique name used to identify the related step or inspection in an |
| 45 |
in-toto layout. |
| 46 |
|
| 47 |
command: A list of command and command arguments that report how the |
| 48 |
corresponding step is performed. |
| 49 |
|
| 50 |
materials: A dictionary of the artifacts *used* by the step, i.e:: |
| 51 |
|
| 52 |
{ |
| 53 |
"<material path>": { |
| 54 |
"<hash algorithm name>": "<hash digest of material>", |
| 55 |
... |
| 56 |
}, |
| 57 |
... |
| 58 |
} |
| 59 |
|
| 60 |
products: A dictionary of the artifacts *produced* by the step, i.e:: |
| 61 |
|
| 62 |
{ |
| 63 |
"<product path>": { |
| 64 |
"<hash algorithm name>": "<hash digest of product>", |
| 65 |
... |
| 66 |
}, |
| 67 |
... |
| 68 |
} |
| 69 |
|
| 70 |
byproducts: An opaque dictionary that lists byproducts of the link command |
| 71 |
execution. It should have at least the following entries |
| 72 |
"stdout" (str), "stderr" (str) and "return-value" (int). |
| 73 |
|
| 74 |
environment: An opaque dictionary that lists information about the |
| 75 |
execution environment of the link command. eg.:: |
| 76 |
|
| 77 |
{ |
| 78 |
"variables": "<list of env var KEY=value pairs>", |
| 79 |
"filesystem": "<filesystem info>", |
| 80 |
"workdir": "<CWD when executing link command>" |
| 81 |
} |
| 82 |
|
| 83 |
""" |
| 84 |
|
| 85 |
_type = attr.ib() |
| 86 |
name = attr.ib() |
| 87 |
materials = attr.ib() |
| 88 |
products = attr.ib() |
| 89 |
byproducts = attr.ib() |
| 90 |
command = attr.ib() |
| 91 |
environment = attr.ib() |
| 92 |
|
| 93 |
def __init__(self, **kwargs): |
| 94 |
super(Link, self).__init__() |
| 95 |
|
| 96 |
self._type = "link" |
| 97 |
self.name = kwargs.get("name") |
| 98 |
self.materials = kwargs.get("materials", {}) |
| 99 |
self.products = kwargs.get("products", {}) |
| 100 |
self.byproducts = kwargs.get("byproducts", {}) |
| 101 |
self.command = kwargs.get("command", []) |
| 102 |
self.environment = kwargs.get("environment", {}) |
| 103 |
|
| 104 |
self.validate() |
| 105 |
|
| 106 |
@property |
| 107 |
def type_(self): |
| 108 |
"""The string "link" to indentify the in-toto metadata type.""" |
| 109 |
# NOTE: We expose the type_ property in the API documentation instead of |
| 110 |
# _type to protect it against modification. |
| 111 |
# NOTE: Trailing underscore is used by convention (pep8) to avoid conflict |
| 112 |
# with Python's type keyword. |
| 113 |
return self._type |
| 114 |
|
| 115 |
@staticmethod |
| 116 |
def read(data): |
| 117 |
"""Creates a Link object from its dictionary representation. |
| 118 |
|
| 119 |
Arguments: |
| 120 |
data: A dictionary with link metadata fields. |
| 121 |
|
| 122 |
Raises: |
| 123 |
securesystemslib.exceptions.FormatError: Passed data is invalid. |
| 124 |
|
| 125 |
Returns: |
| 126 |
The created Link object. |
| 127 |
|
| 128 |
""" |
| 129 |
return Link(**data) |
| 130 |
|
| 131 |
def _validate_type(self): |
| 132 |
"""Private method to check that `_type` is set to "link".""" |
| 133 |
if self._type != "link": |
| 134 |
raise securesystemslib.exceptions.FormatError( |
| 135 |
"Invalid Link: field `_type` must be set to 'link', got: {}".format( |
| 136 |
self._type |
| 137 |
) |
| 138 |
) |
| 139 |
|
| 140 |
def _validate_materials(self): |
| 141 |
"""Private method to check that `materials` is a `dict` of `HASHDICTs`.""" |
| 142 |
if not isinstance(self.materials, dict): |
| 143 |
raise securesystemslib.exceptions.FormatError( |
| 144 |
"Invalid Link: field `materials` must be of type dict, got: {}".format( |
| 145 |
type(self.materials) |
| 146 |
) |
| 147 |
) |
| 148 |
|
| 149 |
for material in list(self.materials.values()): |
| 150 |
securesystemslib.formats.HASHDICT_SCHEMA.check_match(material) |
| 151 |
|
| 152 |
def _validate_products(self): |
| 153 |
"""Private method to check that `products` is a `dict` of `HASHDICTs`.""" |
| 154 |
if not isinstance(self.products, dict): |
| 155 |
raise securesystemslib.exceptions.FormatError( |
| 156 |
"Invalid Link: field `products` must be of type dict, got: {}".format( |
| 157 |
type(self.products) |
| 158 |
) |
| 159 |
) |
| 160 |
|
| 161 |
for product in list(self.products.values()): |
| 162 |
securesystemslib.formats.HASHDICT_SCHEMA.check_match(product) |
| 163 |
|
| 164 |
def _validate_byproducts(self): |
| 165 |
"""Private method to check that `byproducts` is a `dict`.""" |
| 166 |
if not isinstance(self.byproducts, dict): |
| 167 |
raise securesystemslib.exceptions.FormatError( |
| 168 |
"Invalid Link: field `byproducts` must be of type dict, got: {}".format( |
| 169 |
type(self.byproducts) |
| 170 |
) |
| 171 |
) |
| 172 |
|
| 173 |
def _validate_command(self): |
| 174 |
"""Private method to check that `command` is a `list`.""" |
| 175 |
if not isinstance(self.command, list): |
| 176 |
raise securesystemslib.exceptions.FormatError( |
| 177 |
"Invalid Link: field `command` must be of type list, got: {}".format( |
| 178 |
type(self.command) |
| 179 |
) |
| 180 |
) |
| 181 |
|
| 182 |
def _validate_environment(self): |
| 183 |
"""Private method to check that `environment` is a `dict`.""" |
| 184 |
if not isinstance(self.environment, dict): |
| 185 |
raise securesystemslib.exceptions.FormatError( |
| 186 |
"Invalid Link: field `environment` must be of type dict, got: {}".format( |
| 187 |
type(self.environment) |
| 188 |
) |
| 189 |
) |