| 1 |
# Copyright New York University and the in-toto contributors |
| 2 |
# SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
""" |
| 5 |
<Module Name> |
| 6 |
in_toto/rulelib.py |
| 7 |
|
| 8 |
<Author> |
| 9 |
Santiago Torres-Arias <[email protected]> |
| 10 |
Lukas Puehringer <[email protected]> |
| 11 |
|
| 12 |
<Started> |
| 13 |
Nov 18, 2016 |
| 14 |
|
| 15 |
<Copyright> |
| 16 |
See LICENSE for licensing information. |
| 17 |
|
| 18 |
<Purpose> |
| 19 |
This module provides functions parse artifact rules and validate their |
| 20 |
syntax. |
| 21 |
|
| 22 |
""" |
| 23 |
import securesystemslib.exceptions |
| 24 |
import securesystemslib.formats |
| 25 |
|
| 26 |
GENERIC_RULES = { |
| 27 |
"create", |
| 28 |
"modify", |
| 29 |
"delete", |
| 30 |
"allow", |
| 31 |
"disallow", |
| 32 |
"require", |
| 33 |
} |
| 34 |
COMPLEX_RULES = { |
| 35 |
"match", |
| 36 |
} |
| 37 |
ALL_RULES = GENERIC_RULES | COMPLEX_RULES |
| 38 |
|
| 39 |
|
| 40 |
def unpack_rule(rule): |
| 41 |
""" |
| 42 |
Parses the rule and extracts and returns the necessary data to apply the |
| 43 |
rule. Can also be used to verify if a rule complies with any of the formats |
| 44 |
|
| 45 |
<Arguments> |
| 46 |
rule: |
| 47 |
The list of rule elements, in one of the following formats: |
| 48 |
MATCH <pattern> [IN <source-path-prefix>] WITH (MATERIALS|PRODUCTS) |
| 49 |
[IN <destination-path-prefix>] FROM <step>, |
| 50 |
CREATE <pattern>, |
| 51 |
DELETE <pattern>, |
| 52 |
MODIFY <pattern>, |
| 53 |
ALLOW <pattern>, |
| 54 |
DISALLOW <pattern>, |
| 55 |
REQUIRE <file> |
| 56 |
|
| 57 |
Note that REQUIRE is somewhat of a weird animal that does not use patterns |
| 58 |
but rather single filenames (for now). |
| 59 |
|
| 60 |
<Exceptions> |
| 61 |
raises FormatError, if the rule does not comply with any of the formats. |
| 62 |
|
| 63 |
<Side Effects> |
| 64 |
None. |
| 65 |
|
| 66 |
<Returns> |
| 67 |
A dictionary of the artifact rule data, |
| 68 |
if it is a generic rule the dictionary is: |
| 69 |
{ |
| 70 |
"rule_type": rule[0] ("CREATE"|"MODIFY"|"DELETE"|"ALLOW"|"DISALLOW") |
| 71 |
"pattern" : rule[1], a path pattern |
| 72 |
} |
| 73 |
|
| 74 |
if it is a match rule, the dictionary is: |
| 75 |
{ |
| 76 |
"rule_type": rule[0], ("MATCH"), |
| 77 |
"pattern": rule[1], a path pattern |
| 78 |
"source_prefix": path or empty string |
| 79 |
"dest_prefix": path or empty string |
| 80 |
"dest_type" : destination artifact type, ("MATERIAL"|"PRODUCT") |
| 81 |
"dest_name": destination step/inspection name |
| 82 |
} |
| 83 |
|
| 84 |
""" |
| 85 |
securesystemslib.formats.LIST_OF_ANY_STRING_SCHEMA.check_match(rule) |
| 86 |
|
| 87 |
# Create all lower rule copy to case insensitively parse out tokens whose |
| 88 |
# position we don't know yet |
| 89 |
# We keep the original rule to retain the non-token elements' case |
| 90 |
rule_lower = [] |
| 91 |
for rule_elem in rule: |
| 92 |
rule_lower.append(rule_elem.lower()) |
| 93 |
|
| 94 |
rule_len = len(rule) |
| 95 |
|
| 96 |
if rule_len < 2 or rule_lower[0] not in ALL_RULES: |
| 97 |
raise securesystemslib.exceptions.FormatError( |
| 98 |
"Wrong rule format," |
| 99 |
" rules must start with one of '{0}' and specify a 'pattern' as" |
| 100 |
" second element.\n" |
| 101 |
"Got: \n\t'{1}'".format(", ".join(ALL_RULES), rule) |
| 102 |
) |
| 103 |
|
| 104 |
rule_type = rule_lower[0] |
| 105 |
pattern = rule[1] |
| 106 |
|
| 107 |
# Type is one of "CREATE", "MODIFY", "DELETE", "ALLOW", "DISALLOW" |
| 108 |
if rule_type in GENERIC_RULES: |
| 109 |
# pylint: disable=no-else-raise |
| 110 |
if rule_len != 2: |
| 111 |
raise securesystemslib.exceptions.FormatError( |
| 112 |
"Wrong rule format," |
| 113 |
" generic rules must have one of the formats:\n\t" |
| 114 |
"CREATE <pattern>\n\t" |
| 115 |
"MODIFY <pattern>\n\t" |
| 116 |
"DELETE <pattern>\n\t" |
| 117 |
"ALLOW <pattern>\n\t" |
| 118 |
"DISALLOW <pattern>\n" |
| 119 |
"REQUIRE <file>\n" |
| 120 |
"Got:\n\t{}".format(rule) |
| 121 |
) |
| 122 |
else: |
| 123 |
return { |
| 124 |
"rule_type": rule_type, |
| 125 |
"pattern": pattern, |
| 126 |
} |
| 127 |
|
| 128 |
# Type is "MATCH" |
| 129 |
# NOTE: Can't reach `else` branch, if the rule is neither in GENERIC_RULES |
| 130 |
# nor in COMPLEX_RULES an exception is raised earlier. |
| 131 |
elif rule_type in COMPLEX_RULES: # pragma: no branch |
| 132 |
# ... IN <source-path-prefix> WITH (MATERIALS|PRODUCTS) |
| 133 |
# IN <destination-path-prefix> FROM <step> |
| 134 |
if ( |
| 135 |
rule_len == 10 |
| 136 |
and rule_lower[2] == "in" |
| 137 |
and rule_lower[4] == "with" |
| 138 |
and rule_lower[6] == "in" |
| 139 |
and rule_lower[8] == "from" |
| 140 |
): |
| 141 |
source_prefix = rule[3] |
| 142 |
dest_type = rule_lower[5] |
| 143 |
dest_prefix = rule[7] |
| 144 |
dest_name = rule[9] |
| 145 |
|
| 146 |
# ... IN <source-path-prefix> WITH (MATERIALS|PRODUCTS) FROM <step> |
| 147 |
elif ( |
| 148 |
rule_len == 8 |
| 149 |
and rule_lower[2] == "in" |
| 150 |
and rule_lower[4] == "with" |
| 151 |
and rule_lower[6] == "from" |
| 152 |
): |
| 153 |
source_prefix = rule[3] |
| 154 |
dest_type = rule_lower[5] |
| 155 |
dest_prefix = "" |
| 156 |
dest_name = rule[7] |
| 157 |
|
| 158 |
# ... WITH (MATERIALS|PRODUCTS) IN <destination-path-prefix> FROM <step> |
| 159 |
elif ( |
| 160 |
rule_len == 8 |
| 161 |
and rule_lower[2] == "with" |
| 162 |
and rule_lower[4] == "in" |
| 163 |
and rule_lower[6] == "from" |
| 164 |
): |
| 165 |
source_prefix = "" |
| 166 |
dest_type = rule_lower[3] |
| 167 |
dest_prefix = rule[5] |
| 168 |
dest_name = rule[7] |
| 169 |
|
| 170 |
# ... WITH (MATERIALS|PRODUCTS) FROM <step> |
| 171 |
elif ( |
| 172 |
rule_len == 6 |
| 173 |
and rule_lower[2] == "with" |
| 174 |
and rule_lower[4] == "from" |
| 175 |
): |
| 176 |
source_prefix = "" |
| 177 |
dest_type = rule_lower[3] |
| 178 |
dest_prefix = "" |
| 179 |
dest_name = rule[5] |
| 180 |
|
| 181 |
else: |
| 182 |
raise securesystemslib.exceptions.FormatError( |
| 183 |
"Wrong rule format," |
| 184 |
" match rules must have the format:\n\t" |
| 185 |
" MATCH <pattern> [IN <source-path-prefix>] WITH" |
| 186 |
" (MATERIALS|PRODUCTS) [IN <destination-path-prefix>] FROM <step>.\n" |
| 187 |
"Got: \n\t{}".format(rule) |
| 188 |
) |
| 189 |
|
| 190 |
if dest_type not in {"materials", "products"}: |
| 191 |
raise securesystemslib.exceptions.FormatError( |
| 192 |
"Wrong rule format," |
| 193 |
" match rules must have either MATERIALS or PRODUCTS (case" |
| 194 |
" insensitive) as destination.\n" |
| 195 |
"Got: \n\t{}".format(rule) |
| 196 |
) |
| 197 |
|
| 198 |
return { |
| 199 |
"rule_type": rule_type, |
| 200 |
"pattern": pattern, |
| 201 |
"source_prefix": source_prefix, |
| 202 |
"dest_prefix": dest_prefix, |
| 203 |
"dest_type": dest_type, |
| 204 |
"dest_name": dest_name, |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
def pack_rule( |
| 209 |
rule_type, |
| 210 |
pattern, |
| 211 |
source_prefix=None, |
| 212 |
dest_type=None, |
| 213 |
dest_prefix=None, |
| 214 |
dest_name=None, |
| 215 |
): |
| 216 |
""" |
| 217 |
<Purpose> |
| 218 |
Create an artifact rule in the passed arguments and return it as list |
| 219 |
as it is stored in a step's or inspection's expected_material or |
| 220 |
expected_product field. |
| 221 |
|
| 222 |
<Arguments> |
| 223 |
rule_type: |
| 224 |
One of "MATCH", "CREATE", "DELETE", MODIFY, ALLOW, DISALLOW, |
| 225 |
REQUIRE (case insensitive). |
| 226 |
|
| 227 |
pattern: |
| 228 |
A glob pattern to match artifact paths. |
| 229 |
|
| 230 |
source_prefix: (only for "MATCH" rules) |
| 231 |
A prefix for 'pattern' to match artifacts reported by the link |
| 232 |
corresponding to the step that contains the rule. |
| 233 |
|
| 234 |
dest_type: (only for "MATCH" rules) |
| 235 |
One of "MATERIALS" or "PRODUCTS" (case insensitive) to specify |
| 236 |
if materials or products of the link corresponding to the step |
| 237 |
identified by 'dest_name' should be matched. |
| 238 |
|
| 239 |
dest_prefix: (only for "MATCH" rules) |
| 240 |
A prefix for 'pattern' to match artifacts reported by the link |
| 241 |
corresponding to the step identified by 'dest_name'. |
| 242 |
|
| 243 |
dest_name: (only for "MATCH" rules) |
| 244 |
The name of the step whose corresponding link is used to match |
| 245 |
artifacts. |
| 246 |
|
| 247 |
|
| 248 |
<Exceptions> |
| 249 |
securesystemslib.exceptions.FormatError |
| 250 |
if any of the arguments is malformed. |
| 251 |
|
| 252 |
|
| 253 |
<Returns> |
| 254 |
One of the following rule formats in as a list |
| 255 |
MATCH <pattern> [IN <source-path-prefix>] WITH (MATERIALS|PRODUCTS) |
| 256 |
[IN <destination-path-prefix>] FROM <step>, |
| 257 |
CREATE <pattern>, |
| 258 |
DELETE <pattern>, |
| 259 |
MODIFY <pattern>, |
| 260 |
ALLOW <pattern>, |
| 261 |
DISALLOW <pattern>, |
| 262 |
REQUIRE <file> |
| 263 |
|
| 264 |
Note that REQUIRE is somewhat of a weird animal that does not use patterns |
| 265 |
but rather single filenames (for now). |
| 266 |
""" |
| 267 |
securesystemslib.formats.ANY_STRING_SCHEMA.check_match(rule_type) |
| 268 |
securesystemslib.formats.ANY_STRING_SCHEMA.check_match(pattern) |
| 269 |
|
| 270 |
if rule_type.lower() not in ALL_RULES: |
| 271 |
raise securesystemslib.exceptions.FormatError( |
| 272 |
"'{0}' is not a valid " |
| 273 |
"'type'. Rule type must be one of: {1} (case insensitive).".format( |
| 274 |
rule_type, ", ".join(ALL_RULES) |
| 275 |
) |
| 276 |
) |
| 277 |
|
| 278 |
if rule_type.upper() == "MATCH": |
| 279 |
if not securesystemslib.formats.ANY_STRING_SCHEMA.matches( |
| 280 |
dest_type |
| 281 |
) or not ( |
| 282 |
dest_type.lower() == "materials" or dest_type.lower() == "products" |
| 283 |
): |
| 284 |
raise securesystemslib.exceptions.FormatError( |
| 285 |
"'{}' is not a valid" |
| 286 |
" 'dest_type'. Rules of type 'MATCH' require a destination type of" |
| 287 |
" either 'MATERIALS' or 'PRODUCTS' (case insensitive).".format( |
| 288 |
dest_type |
| 289 |
) |
| 290 |
) |
| 291 |
|
| 292 |
if not ( |
| 293 |
securesystemslib.formats.ANY_STRING_SCHEMA.matches(dest_name) |
| 294 |
and dest_name |
| 295 |
): |
| 296 |
raise securesystemslib.exceptions.FormatError( |
| 297 |
"'{}' is not a valid" |
| 298 |
" 'dest_name'. Rules of type 'MATCH' require a step name as a" |
| 299 |
" destination name.".format(dest_name) |
| 300 |
) |
| 301 |
|
| 302 |
# Construct rule |
| 303 |
rule = ["MATCH", pattern] |
| 304 |
|
| 305 |
if source_prefix: |
| 306 |
securesystemslib.formats.ANY_STRING_SCHEMA.check_match( |
| 307 |
source_prefix |
| 308 |
) |
| 309 |
rule += ["IN", source_prefix] |
| 310 |
|
| 311 |
rule += ["WITH", dest_type.upper()] |
| 312 |
|
| 313 |
if dest_prefix: |
| 314 |
securesystemslib.formats.ANY_STRING_SCHEMA.check_match(dest_prefix) |
| 315 |
rule += ["IN", dest_prefix] |
| 316 |
|
| 317 |
rule += ["FROM", dest_name] |
| 318 |
|
| 319 |
else: |
| 320 |
rule = [rule_type.upper(), pattern] |
| 321 |
|
| 322 |
return rule |
| 323 |
|
| 324 |
|
| 325 |
def pack_rule_data(rule_data): |
| 326 |
"""Shortcut for 'pack_rule' to pack a rule based on a rule_data dictionary |
| 327 |
as returned by 'unpack_rule'.""" |
| 328 |
return pack_rule(**rule_data) |
| 329 |
|
| 330 |
|
| 331 |
def pack_create_rule(pattern): |
| 332 |
"""Shortcut for 'pack_rule' to pack a CREATE rule.""" |
| 333 |
return pack_rule("CREATE", pattern) |
| 334 |
|
| 335 |
|
| 336 |
def pack_delete_rule(pattern): |
| 337 |
"""Shortcut for 'pack_rule' to pack a DELETE rule.""" |
| 338 |
return pack_rule("DELETE", pattern) |
| 339 |
|
| 340 |
|
| 341 |
def pack_modify_rule(pattern): |
| 342 |
"""Shortcut for 'pack_rule' to pack a MODIFY rule.""" |
| 343 |
return pack_rule("MODIFY", pattern) |
| 344 |
|
| 345 |
|
| 346 |
def pack_allow_rule(pattern): |
| 347 |
"""Shortcut for 'pack_rule' to pack an ALLOW rule.""" |
| 348 |
return pack_rule("ALLOW", pattern) |
| 349 |
|
| 350 |
|
| 351 |
def pack_disallow_rule(pattern): |
| 352 |
"""Shortcut for 'pack_rule' to pack a DISALLOW rule.""" |
| 353 |
return pack_rule("DISALLOW", pattern) |
| 354 |
|
| 355 |
|
| 356 |
def pack_require_rule(filename): |
| 357 |
""" |
| 358 |
Shortcut for 'pack_rule' to pack a REQUIRE rule: |
| 359 |
note that REQUIRE is somewhat of a weird animal that does not use patterns |
| 360 |
but rather single filenames (for now). |
| 361 |
""" |
| 362 |
return pack_rule("REQUIRE", filename) |