| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export GIT_CONFIG_NOSYSTEM=1 |
| 6 | git config --global user.name valley-check |
| 7 | git config --global user.email valley-check@localhost |
| 8 | git config --global init.defaultBranch main |
| 9 | |
| 10 | # The real floor is in the owning valley's repository, which a build cannot |
| 11 | # fetch. The example floor stands in for it: it has the same prose class |
| 12 | # and unclassified rule. The docs template is composed beside it, as an |
| 13 | # instance's templates are, and this layer does not embed it. |
| 14 | policy="$TMPDIR/policy" |
| 15 | mkdir -p "$policy/instance" "$policy/project" |
| 16 | cp "$floor" "$policy/instance/floor.cue" |
| 17 | cp "$template" "$policy/instance/template.cue" |
| 18 | cp "$project"/*.cue "$policy/project/" |
| 19 | |
| 20 | # The layer composes. |
| 21 | cue vet -c "$schema" "$policy"/instance/*.cue "$policy"/project/*.cue |
| 22 | |
| 23 | # Every check the composition names is a flake check this flake defines. |
| 24 | # A policy naming a check nobody can build would be found only when a |
| 25 | # change that requires it is approved. |
| 26 | cat > "$TMPDIR/attributes.cue" <<'EOF' |
| 27 | package verification |
| 28 | |
| 29 | import "strings" |
| 30 | |
| 31 | _attributes: strings.Join([ |
| 32 | for n, c in policy.checks if c.runner == "nix" {c.attribute}, |
| 33 | ], "\n") |
| 34 | EOF |
| 35 | cue eval --out text -e _attributes "$schema" "$policy"/instance/*.cue \ |
| 36 | "$policy"/project/*.cue "$TMPDIR/attributes.cue" > attributes.txt |
| 37 | [ -s attributes.txt ] || { echo "own-policy: no nix check named" >&2; exit 1; } |
| 38 | missing=0 |
| 39 | while IFS= read -r a; do |
| 40 | case " $checkNames " in |
| 41 | *" $a "*) ;; |
| 42 | *) |
| 43 | echo "own-policy: the policy names $a, which is not a flake check" >&2 |
| 44 | missing=1 |
| 45 | ;; |
| 46 | esac |
| 47 | done < attributes.txt |
| 48 | [ "$missing" -eq 0 ] || exit 1 |
| 49 | |
| 50 | # A change to the docs requires the floor's prose-format alone, and a |
| 51 | # change to the hook requires the hook's tests. Classes that matched too much or nothing |
| 52 | # at all would break one or the other. |
| 53 | repo="$TMPDIR/tree" |
| 54 | git init --quiet "$repo" |
| 55 | cd "$repo" || exit 1 |
| 56 | mkdir -p docs valleyhook nix/checks |
| 57 | echo seed > docs/architecture.md |
| 58 | echo seed > valleyhook/main.go |
| 59 | echo seed > nix/packages.nix |
| 60 | # One path in each scope the carry modes below are asserted over. |
| 61 | for f in integrator/main.go sigverify/keys.go identity/registry.go \ |
| 62 | schema/identity.cue schema/verification.cue policy/project/policy.cue \ |
| 63 | policy/valley examples/policy/project/policy.cue \ |
| 64 | examples/identity/registry/registry.cue research/notes.txt; do |
| 65 | mkdir -p "$(dirname "$f")" |
| 66 | echo seed > "$f" |
| 67 | done |
| 68 | for f in $checkFiles; do |
| 69 | echo seed > "nix/checks/$f" |
| 70 | done |
| 71 | git add -A |
| 72 | git commit --quiet -m base |
| 73 | base="$(git rev-parse HEAD)" |
| 74 | |
| 75 | required() { |
| 76 | valley checks --schema "$schema" \ |
| 77 | --instance "$policy/instance" --project "$policy/project" \ |
| 78 | --names "$base" HEAD |
| 79 | } |
| 80 | |
| 81 | git checkout --quiet -B docs "$base" |
| 82 | echo edit > docs/architecture.md |
| 83 | git commit --quiet -am docs |
| 84 | got="$(required)" |
| 85 | [ "$got" = prose-format ] || { |
| 86 | echo "own-policy: a docs-only change must require prose-format alone, got:" >&2 |
| 87 | echo "$got" >&2 |
| 88 | exit 1 |
| 89 | } |
| 90 | |
| 91 | git checkout --quiet -B hook "$base" |
| 92 | echo edit > valleyhook/main.go |
| 93 | git commit --quiet -am hook |
| 94 | got="$(required)" |
| 95 | grep -qx valleyhook-unit <<<"$got" || { |
| 96 | echo "own-policy: a hook-only change must require valleyhook-unit, got:" >&2 |
| 97 | echo "$got" >&2 |
| 98 | exit 1 |
| 99 | } |
| 100 | if grep -qx prose-format <<<"$got"; then |
| 101 | echo "own-policy: a hook-only change fell to the unclassified rule" >&2 |
| 102 | exit 1 |
| 103 | fi |
| 104 | |
| 105 | # A change adding only an entry named vendor, such as a symlink, requires |
| 106 | # go-dependencies: Go vendors a module's packages from it with no change to |
| 107 | # a go.mod or a .go file. |
| 108 | git checkout --quiet -B vendor "$base" |
| 109 | ln -s ../docs valleyhook/vendor |
| 110 | git add valleyhook/vendor |
| 111 | git commit --quiet -m vendor |
| 112 | got="$(required)" |
| 113 | grep -qx go-dependencies <<<"$got" || { |
| 114 | echo "own-policy: a change adding a vendor symlink must require go-dependencies, got:" >&2 |
| 115 | echo "$got" >&2 |
| 116 | exit 1 |
| 117 | } |
| 118 | |
| 119 | # A check's own files require that check. Every file under nix/checks/ |
| 120 | # named after a flake check requires that check, and every other script |
| 121 | # there requires some check the nix class does not. A script that matched |
| 122 | # the nix class alone could be changed without its check being required. Other files that |
| 123 | # are not named after a check are definitions or data shared by several |
| 124 | # checks, which the policy may leave to the nix class. |
| 125 | git checkout --quiet -B shared "$base" |
| 126 | echo edit > nix/packages.nix |
| 127 | git commit --quiet -am shared |
| 128 | shared="$(required | LC_ALL=C sort)" |
| 129 | failed=0 |
| 130 | for f in $checkFiles; do |
| 131 | name="${f%.*}" |
| 132 | case " $checkNames " in |
| 133 | *" $name "*) named=1 ;; |
| 134 | *) named=0 ;; |
| 135 | esac |
| 136 | [ "$named" -eq 1 ] || [ "${f##*.}" = sh ] || continue |
| 137 | git checkout --quiet -B file "$base" |
| 138 | echo edit > "nix/checks/$f" |
| 139 | git commit --quiet -am "$f" |
| 140 | got="$(required | LC_ALL=C sort)" |
| 141 | if [ "$named" -eq 1 ]; then |
| 142 | grep -qx -- "$name" <<<"$got" && continue |
| 143 | echo "own-policy: a change to nix/checks/$f must require $name, got:" >&2 |
| 144 | else |
| 145 | [ -n "$(LC_ALL=C comm -23 <(echo "$got") <(echo "$shared"))" ] && continue |
| 146 | echo "own-policy: a change to nix/checks/$f requires no check of its own, got:" >&2 |
| 147 | fi |
| 148 | echo "$got" >&2 |
| 149 | failed=1 |
| 150 | done |
| 151 | [ "$failed" -eq 0 ] || exit 1 |
| 152 | |
| 153 | # Each scope's carry mode, as the deriver reports it for a change to one |
| 154 | # path in it. The hook, the integrator, the signature verifier and the |
| 155 | # identity compiler are same-class, the policy is exact, and everything |
| 156 | # else, unclassified paths included, is same-paths. |
| 157 | while read -r path mode; do |
| 158 | git checkout --quiet -B carry "$base" |
| 159 | echo edit > "$path" |
| 160 | git commit --quiet -am "$path" |
| 161 | got="$(valley checks --schema "$schema" \ |
| 162 | --instance "$policy/instance" --project "$policy/project" "$base" HEAD \ |
| 163 | | sed -n 's/^carry mode: //p')" |
| 164 | [ "$got" = "$mode" ] && continue |
| 165 | echo "own-policy: a change to $path must be $mode, got '$got'" >&2 |
| 166 | failed=1 |
| 167 | done <<'EOF' |
| 168 | valleyhook/main.go same-class |
| 169 | integrator/main.go same-class |
| 170 | sigverify/keys.go same-class |
| 171 | identity/registry.go same-class |
| 172 | schema/identity.cue same-class |
| 173 | policy/project/policy.cue exact |
| 174 | policy/valley exact |
| 175 | schema/verification.cue exact |
| 176 | examples/policy/project/policy.cue same-paths |
| 177 | examples/identity/registry/registry.cue same-paths |
| 178 | docs/architecture.md same-paths |
| 179 | research/notes.txt same-paths |
| 180 | EOF |
| 181 | [ "$failed" -eq 0 ] || exit 1 |
| 182 | |
| 183 | # The scopes the carry classes newly classify keep every check they |
| 184 | # required before those classes existed. Each line is a path and the checks |
| 185 | # a change to it required then, composed with this same floor. |
| 186 | # schema/verification.cue requires more now: the integrator's checks read |
| 187 | # it. |
| 188 | while read -r path before; do |
| 189 | git checkout --quiet -B classified "$base" |
| 190 | echo edit > "$path" |
| 191 | git commit --quiet -am "$path" |
| 192 | got="$(required)" |
| 193 | for check in ${before//,/ }; do |
| 194 | grep -qx -- "$check" <<<"$got" && continue |
| 195 | echo "own-policy: a change to $path no longer requires $check, got:" >&2 |
| 196 | echo "$got" >&2 |
| 197 | failed=1 |
| 198 | done |
| 199 | done <<'EOF' |
| 200 | identity/registry.go identity-e2e,identity-unit |
| 201 | schema/identity.cue attest-schema,cue-vet,identity-schema |
| 202 | policy/project/policy.cue cue-vet,own-policy,policy-deriver |
| 203 | policy/valley cue-vet,own-policy,policy-deriver |
| 204 | schema/verification.cue attest-schema,cue-vet,identity-schema,own-policy,policy-deriver |
| 205 | EOF |
| 206 | [ "$failed" -eq 0 ] || exit 1 |
| 207 | |
| 208 | # The restacker's cap is the default. |
| 209 | cap="$(cue export -e policy.restack.maxAutomaticVersions "$schema" \ |
| 210 | "$policy"/instance/*.cue "$policy"/project/*.cue)" |
| 211 | [ "$cap" = 64 ] || { |
| 212 | echo "own-policy: the restacker's cap is $cap, not 64" >&2 |
| 213 | exit 1 |
| 214 | } |
| 215 | |
| 216 | touch "$out" |