gunk-dev/the-valley

own-policy.sh

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