|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +import re |
| 6 | + |
| 7 | +from taskgraph.transforms.base import TransformSequence |
| 8 | +from taskgraph.util.schema import ( |
| 9 | + Schema, |
| 10 | + optionally_keyed_by, |
| 11 | + resolve_keyed_by, |
| 12 | +) |
| 13 | +from voluptuous import ALLOW_EXTRA, Any, Optional, Required |
| 14 | + |
| 15 | +SIGNING_FORMATS = ["autograph_gpg"] |
| 16 | +SIGNING_TYPES = ["dep", "release"] |
| 17 | +DETACHED_SIGNATURE_EXTENSION = ".asc" |
| 18 | + |
| 19 | +signing_schema = Schema( |
| 20 | + { |
| 21 | + Required("attributes"): { |
| 22 | + Optional("artifacts"): dict, |
| 23 | + Required("build-type"): str, |
| 24 | + }, |
| 25 | + Required("signing"): optionally_keyed_by( |
| 26 | + "build-type", |
| 27 | + "level", |
| 28 | + { |
| 29 | + Required("format"): optionally_keyed_by( |
| 30 | + "build-type", "level", Any(*SIGNING_FORMATS) |
| 31 | + ), |
| 32 | + Optional("type"): optionally_keyed_by( |
| 33 | + "build-type", "level", Any(*SIGNING_TYPES) |
| 34 | + ), |
| 35 | + Optional("ignore-artifacts"): list, |
| 36 | + }, |
| 37 | + ), |
| 38 | + Required("worker"): { |
| 39 | + Required("upstream-artifacts"): [ |
| 40 | + { |
| 41 | + # Paths to the artifacts to sign |
| 42 | + Required("paths"): [str], |
| 43 | + } |
| 44 | + ], |
| 45 | + }, |
| 46 | + }, |
| 47 | + extra=ALLOW_EXTRA, |
| 48 | +) |
| 49 | + |
| 50 | +transforms = TransformSequence() |
| 51 | +transforms.add_validate(signing_schema) |
| 52 | + |
| 53 | + |
| 54 | +@transforms.add |
| 55 | +def resolve_signing_keys(config, tasks): |
| 56 | + for task in tasks: |
| 57 | + for key in ( |
| 58 | + "signing", |
| 59 | + "signing.format", |
| 60 | + "signing.type", |
| 61 | + ): |
| 62 | + resolve_keyed_by( |
| 63 | + task, |
| 64 | + key, |
| 65 | + item_name=task["name"], |
| 66 | + **{ |
| 67 | + "build-type": task["attributes"]["build-type"], |
| 68 | + "level": config.params["level"], |
| 69 | + }, |
| 70 | + ) |
| 71 | + yield task |
| 72 | + |
| 73 | + |
| 74 | +@transforms.add |
| 75 | +def set_signing_attributes(_, tasks): |
| 76 | + for task in tasks: |
| 77 | + task["attributes"]["signed"] = True |
| 78 | + yield task |
| 79 | + |
| 80 | + |
| 81 | +@transforms.add |
| 82 | +def set_signing_format(_, tasks): |
| 83 | + for task in tasks: |
| 84 | + for upstream_artifact in task["worker"]["upstream-artifacts"]: |
| 85 | + upstream_artifact["formats"] = [task["signing"]["format"]] |
| 86 | + yield task |
| 87 | + |
| 88 | + |
| 89 | +@transforms.add |
| 90 | +def set_signing_and_worker_type(config, tasks): |
| 91 | + for task in tasks: |
| 92 | + signing_type = task["signing"].get("type") |
| 93 | + if not signing_type: |
| 94 | + signing_type = "release" if config.params["level"] == "3" else "dep" |
| 95 | + |
| 96 | + task.setdefault("worker", {})["signing-type"] = f"{signing_type}-signing" |
| 97 | + |
| 98 | + if "worker-type" not in task: |
| 99 | + worker_type = "signing" |
| 100 | + build_type = task["attributes"]["build-type"] |
| 101 | + |
| 102 | + if signing_type == "dep": |
| 103 | + worker_type = f"dep-{worker_type}" |
| 104 | + if build_type == "macos": |
| 105 | + worker_type = f"{build_type}-{worker_type}" |
| 106 | + task["worker-type"] = worker_type |
| 107 | + |
| 108 | + yield task |
| 109 | + |
| 110 | + |
| 111 | +@transforms.add |
| 112 | +def filter_out_ignored_artifacts(_, tasks): |
| 113 | + for task in tasks: |
| 114 | + ignore = task["signing"].get("ignore-artifacts") |
| 115 | + if not ignore: |
| 116 | + yield task |
| 117 | + continue |
| 118 | + |
| 119 | + def is_ignored(artifact): |
| 120 | + return not any(re.search(i, artifact) for i in ignore) |
| 121 | + |
| 122 | + if task["attributes"].get("artifacts"): |
| 123 | + task["attributes"]["artifacts"] = { |
| 124 | + extension: path |
| 125 | + for extension, path in task["attributes"]["artifacts"].items() |
| 126 | + if is_ignored(path) |
| 127 | + } |
| 128 | + |
| 129 | + for upstream_artifact in task["worker"]["upstream-artifacts"]: |
| 130 | + upstream_artifact["paths"] = [ |
| 131 | + path for path in upstream_artifact["paths"] if is_ignored(path) |
| 132 | + ] |
| 133 | + |
| 134 | + yield task |
| 135 | + |
| 136 | + |
| 137 | +@transforms.add |
| 138 | +def set_gpg_detached_signature_artifacts(_, tasks): |
| 139 | + for task in tasks: |
| 140 | + if task["signing"]["format"] != "autograph_gpg": |
| 141 | + yield task |
| 142 | + continue |
| 143 | + |
| 144 | + task["attributes"]["artifacts"] = { |
| 145 | + extension |
| 146 | + + DETACHED_SIGNATURE_EXTENSION: path |
| 147 | + + DETACHED_SIGNATURE_EXTENSION |
| 148 | + for extension, path in task["attributes"]["artifacts"].items() |
| 149 | + } |
| 150 | + |
| 151 | + yield task |
| 152 | + |
| 153 | + |
| 154 | +@transforms.add |
| 155 | +def remove_signing_config(_, tasks): |
| 156 | + for task in tasks: |
| 157 | + del task["signing"] |
| 158 | + yield task |
0 commit comments