Scope group install umask and pin bootstrap execution modes
Run the released host-env installer with umask 022 in a subshell so non-root services can traverse its installed paths. Preserve the parent umask and explicit host.env mode; pin exec forms and refresh the census proposal.
Co-Authored-By: GPT-6 Astra <noreply@openai.com>
6 files changed,
+188
−9
docs/superpowers/notes/census-proposal-region-ha-p3b1-f4.md+2 −2infra/modules/region-host/group-init.sh.tftpl+2 −1internal/host/group_boot_staging_test.go+60 −2internal/provision/testdata/plan14-root-reads-chain.tsv+1 −1internal/provision/testdata/plan14-root-reads-region-ha-p3b1-f4.tsv+3 −3scripts/region_group_boot_umask_test.py+120 −0
diff --git a/docs/superpowers/notes/census-proposal-region-ha-p3b1-f4.md b/docs/superpowers/notes/census-proposal-region-ha-p3b1-f4.md| index 4ed86f0fd..888af6ca0 100644 |
| --- a/docs/superpowers/notes/census-proposal-region-ha-p3b1-f4.md |
| +++ b/docs/superpowers/notes/census-proposal-region-ha-p3b1-f4.md |
| @@ -4,7 +4,7 @@ Ruling `census-ruling-region-ha-p3b1-f4`. The independent reviewer approves or r |
| | unit | change | read sites | what changed | |
| |---|---|---|---| |
| -| `infra/modules/region-host/group-init.sh.tftpl` `external` | body | unchanged (1) | Move bootstrap and chrony staging to root:root 0700 /var/lib/carlos-group-boot through the existing O_NOFOLLOW walker; refuse unsafe preexisting directory. Bootstrap 0700; retain initial hash verification and rehash against a root-only captured SSM digest immediately before all three execs; named noexec refusal. Root reads only its own helper/hash and verified binary; no tenant/app/agent writable input or authority added. Final removal preserved. Round 5 adds /etc/sysctl.d to the same root-owned, non-writable, O_NOFOLLOW directory walker before its existing exclusive sysctl file write; missing directory is created, unsafe ancestors refuse. All other template bytes unchanged in round 5. | |
| +| `infra/modules/region-host/group-init.sh.tftpl` `external` | body | unchanged (1) | Move bootstrap and chrony staging to root:root 0700 /var/lib/carlos-group-boot through the existing O_NOFOLLOW walker; refuse unsafe preexisting directory. Bootstrap 0700; retain initial hash verification and rehash against a root-only captured SSM digest immediately before all three execs; named noexec refusal. Root reads only its own helper/hash and verified binary; no tenant/app/agent writable input or authority added. Final removal preserved. Round 5 adds /etc/sysctl.d to the same root-owned, non-writable, O_NOFOLLOW directory walker before its existing exclusive sysctl file write; missing directory is created, unsafe ancestors refuse. Round 6 scopes umask 022 to the verified host-env install exec in a subshell; its bootstrap/install-channel children create traversable install directories and readable unit files, matching fixed boxes. Parent umask remains 077; verify-identity is read-only and hairpin install creates only 0700/0600 private state. Explicitly chmod the freshly created instance-control drop-in directory 0755. host.env remains explicitly chmodded 0600 in unchanged released Go. No new input/read or write authority; all verification and directory trust checks remain. | |
| Envelopes re-pinned: `infra/modules/region-host`. |
| @@ -14,6 +14,6 @@ Recorded dispositions, which each change must still satisfy: |
| - `infra/modules/region-host/group-init.sh.tftpl` `external`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=census-ruling-region-ha-p3b;basis=New root user-data boot: operator Terraform/SSM supply scoped bootstrap object version/hash, queue, reader inventory and ingress; hash-verified bootstrap verifies signed IMDS identity. Root exclusively writes /etc/carlos/group.json, group-identity.json, region-config.json and group-ingress.json with current kernel boot ID; public address comes from protected IMDSv2, not signed document or headers. Install IMDS denial, chrony, dormant hairpin fence and protected drop-ins before bootstrap/install/start. No agent receipt is read by root; restart authority is separate maintenance permit. F1: operator-seeded SSM boot/bootstrap-object requires floor=absent-expected or required beside key/version/known_readers/deployment_aws_region; root copies it into group.json before config_digest. Missing or invalid floor refuses before bootstrap fetch; Terraform only names this SSM key and does not write the object. Agent and instance-control gain AF_NETLINK in their address-family allowlists, with no CAP_NET_ADMIN grant. |
| -Seal: `86aa1e39a21072d17678411a4047983a756494b84a25f7719c0cdf4a61cd1b85` → `6747141e803bcfe7a7482c9d14ed2c8880deee8623f55691bbdf91d5d5411dbf`. |
| +Seal: `86aa1e39a21072d17678411a4047983a756494b84a25f7719c0cdf4a61cd1b85` → `e56c679339aa2d040e9a61ae7612ede56dd59e7ee8611d9e0b60916429ce9c0f`. |
| Overlay: `internal/provision/testdata/plan14-root-reads-region-ha-p3b1-f4.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/infra/modules/region-host/group-init.sh.tftpl b/infra/modules/region-host/group-init.sh.tftpl| index 90826ea0a..f858c5b53 100644 |
| --- a/infra/modules/region-host/group-init.sh.tftpl |
| +++ b/infra/modules/region-host/group-init.sh.tftpl |
| @@ -289,6 +289,7 @@ files = { |
| '/etc/systemd/system/carlos-instance-control.service.d/60-hairpin.conf': '[Service]\nRestrictAddressFamilies=AF_UNIX AF_NETLINK\nReadWritePaths=-/run/carlos-hairpin\n', |
| } |
| os.mkdir('/etc/systemd/system/carlos-instance-control.service.d', 0o755) |
| +os.chmod('/etc/systemd/system/carlos-instance-control.service.d', 0o755) |
| for path, body in files.items(): |
| fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) |
| os.fchmod(fd, 0o644) |
| @@ -319,7 +320,7 @@ test "$(cat /etc/systemd/system/carlos-edge.service.d/50-group.conf)" = "$(print |
| # bootstrap and install channel |
| python3 -I /var/lib/carlos-group-boot/carlos_group_boot_verify.py |
| -/var/lib/carlos-group-boot/bootstrap system host-env write --group --deployment "$CARLOS_DEPLOYMENT" --region "$CARLOS_REGION" --aws-region "$AWS_REGION" --install |
| +( umask 022; exec /var/lib/carlos-group-boot/bootstrap system host-env write --group --deployment "$CARLOS_DEPLOYMENT" --region "$CARLOS_REGION" --aws-region "$AWS_REGION" --install ) |
| # host.env is parsed as data by the verified binary; it is never sourced. |
| # start guarded agent |
diff --git a/internal/host/group_boot_staging_test.go b/internal/host/group_boot_staging_test.go| index a3ddc7e17..422c893ea 100644 |
| --- a/internal/host/group_boot_staging_test.go |
| +++ b/internal/host/group_boot_staging_test.go |
| @@ -15,6 +15,8 @@ import ( |
| const ( |
| groupBootStaging = "/var/lib/carlos-group-boot" |
| groupBootVerify = "python3 -I " + groupBootStaging + "/carlos_group_boot_verify.py" |
| + groupBootHostenv = `( umask 022; exec ` + groupBootStaging + `/bootstrap system host-env write --group --deployment "$CARLOS_DEPLOYMENT" --region "$CARLOS_REGION" --aws-region "$AWS_REGION" --install )` |
| + groupBootDropin = "/etc/systemd/system/carlos-instance-control.service.d" |
| ) |
| func groupInitTemplate(t *testing.T) string { |
| @@ -86,6 +88,29 @@ func checkGroupBootStaging(s string) error { |
| } |
| } |
| lines := strings.Split(s, "\n") |
| + // The script runs under umask 077. Only the host-env install child widens |
| + // it, inside its own subshell; no other line changes the umask. |
| + umasks := 0 |
| + for i, line := range lines { |
| + if !strings.Contains(line, "umask") { |
| + continue |
| + } |
| + umasks++ |
| + if line != "umask 077" && line != groupBootHostenv { |
| + return fmt.Errorf("line %d changes the umask outside the audited forms: %s", i+1, line) |
| + } |
| + } |
| + if umasks != 2 || len(lines) < 3 || lines[2] != "umask 077" { |
| + return fmt.Errorf("umask forms changed") |
| + } |
| + for i, line := range lines { |
| + if line == "os.mkdir('"+groupBootDropin+"', 0o755)" && (i+1 >= len(lines) || lines[i+1] != "os.chmod('"+groupBootDropin+"', 0o755)") { |
| + return fmt.Errorf("drop-in directory is not chmodded 0755 after mkdir") |
| + } |
| + } |
| + if strings.Count(s, "os.mkdir('"+groupBootDropin+"', 0o755)\nos.chmod('"+groupBootDropin+"', 0o755)\n") != 1 { |
| + return fmt.Errorf("drop-in directory mkdir/chmod pair changed") |
| + } |
| execs := 0 |
| for i, line := range lines { |
| if !strings.Contains(line, "bootstrap") { |
| @@ -96,7 +121,7 @@ func checkGroupBootStaging(s string) error { |
| prev = lines[i-1] |
| } |
| switch { |
| - case line == groupBootStaging+"/bootstrap instance-control --group-hairpin-install" || line == groupBootStaging+`/bootstrap system host-env write --group --deployment "$CARLOS_DEPLOYMENT" --region "$CARLOS_REGION" --aws-region "$AWS_REGION" --install`: |
| + case line == groupBootStaging+"/bootstrap instance-control --group-hairpin-install" || line == groupBootHostenv: |
| execs++ |
| if prev != groupBootVerify { |
| return fmt.Errorf("line %d executes the bootstrap without the shared verification", i+1) |
| @@ -127,7 +152,8 @@ func TestGroupBootStagingGuard(t *testing.T) { |
| t.Fatal(err) |
| } |
| install := groupBootStaging + "/bootstrap instance-control --group-hairpin-install" |
| - hostenv := groupBootStaging + "/bootstrap system host-env" |
| + hostenv := groupBootHostenv |
| + bareHostenv := strings.TrimSuffix(strings.TrimPrefix(groupBootHostenv, "( umask 022; exec "), " )") |
| mutants := map[string]func(string) string{ |
| "run staging": func(s string) string { return strings.ReplaceAll(s, groupBootStaging, "/run/carlos-group-boot") }, |
| "tmp staging": func(s string) string { return strings.ReplaceAll(s, groupBootStaging, "/tmp/carlos-group-boot") }, |
| @@ -188,6 +214,38 @@ func TestGroupBootStagingGuard(t *testing.T) { |
| }, |
| "var-run in rw paths": func(s string) string { return s + "ReadWritePaths=/var/run/x\n" }, |
| "unlisted tmp argument": func(s string) string { return s + "cp x /tmp\n" }, |
| + "parent umask changed": func(s string) string { return strings.Replace(s, "umask 077\n", "umask 027\n", 1) }, |
| + "parent umask removed": func(s string) string { return strings.Replace(s, "umask 077\n", "", 1) }, |
| + "subshell umask removed": func(s string) string { |
| + return strings.Replace(s, hostenv, "( exec "+bareHostenv+" )", 1) |
| + }, |
| + "subshell umask 077": func(s string) string { return strings.Replace(s, "umask 022; exec", "umask 077; exec", 1) }, |
| + "subshell umask 000": func(s string) string { return strings.Replace(s, "umask 022; exec", "umask 000; exec", 1) }, |
| + "leaking parent umask 022": func(s string) string { |
| + return strings.Replace(s, hostenv, "umask 022\n"+bareHostenv, 1) |
| + }, |
| + "bare hostenv exec": func(s string) string { return strings.Replace(s, hostenv, bareHostenv, 1) }, |
| + "subshell without exec": func(s string) string { |
| + return strings.Replace(s, "umask 022; exec", "umask 022;", 1) |
| + }, |
| + "umask near hairpin install": func(s string) string { |
| + return strings.Replace(s, groupBootVerify+"\n"+install, "umask 022\n"+groupBootVerify+"\n"+install, 1) |
| + }, |
| + "umask subshell on hairpin install": func(s string) string { |
| + return strings.Replace(s, install, "( umask 022; exec "+install+" )", 1) |
| + }, |
| + "umask near python verify": func(s string) string { |
| + return strings.Replace(s, "boot_verify.check()\nsubprocess", "boot_verify.check()\nos.umask(0o022)\nsubprocess", 1) |
| + }, |
| + "umask in python verify": func(s string) string { |
| + return strings.Replace(s, "boot_verify.check()\nsubprocess", "boot_verify.check()\nos.umask(0o022); subprocess", 1) |
| + }, |
| + "drop-in chmod removed": func(s string) string { |
| + return strings.Replace(s, "os.chmod('"+groupBootDropin+"', 0o755)\n", "", 1) |
| + }, |
| + "drop-in chmod 0700": func(s string) string { |
| + return strings.Replace(s, "os.chmod('"+groupBootDropin+"', 0o755)", "os.chmod('"+groupBootDropin+"', 0o700)", 1) |
| + }, |
| } |
| for name, mutate := range mutants { |
| t.Run(name, func(t *testing.T) { |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index 481343791..30c755988 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -88,4 +88,4 @@ plan14-root-reads-region-ha-p3b.tsv face84b4c688cd3c179b0a34ced4d3521e71de8e63cb |
| plan14-root-reads-stateless-coordinator-oneboxfleet.tsv face84b4c688cd3c179b0a34ced4d3521e71de8e63cbd45f21c27972c5b1a921 |
| plan14-root-reads-region-ha-group-rtb.tsv 526d434888d043a8b09d8fe196f6324782d6ca20d759b080f99daa528adf6a32 |
| plan14-root-reads-region-ha-p3b1-fixes.tsv 86aa1e39a21072d17678411a4047983a756494b84a25f7719c0cdf4a61cd1b85 |
| -plan14-root-reads-region-ha-p3b1-f4.tsv 6747141e803bcfe7a7482c9d14ed2c8880deee8623f55691bbdf91d5d5411dbf |
| +plan14-root-reads-region-ha-p3b1-f4.tsv e56c679339aa2d040e9a61ae7612ede56dd59e7ee8611d9e0b60916429ce9c0f |
diff --git a/internal/provision/testdata/plan14-root-reads-region-ha-p3b1-f4.tsv b/internal/provision/testdata/plan14-root-reads-region-ha-p3b1-f4.tsv| index 736b3ecd5..f7271a1ed 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-region-ha-p3b1-f4.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-region-ha-p3b1-f4.tsv |
| @@ -1,5 +1,5 @@ |
| # Census overlay region-ha-p3b1-f4, written by census-propose (#369). census-ruling-region-ha-p3b1-f4, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-region-ha-p3b1-f4.md. |
| # 1 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| -@legacy 3424be63316e3d85cf37de4550c8e3ce7b8e236970a3bd5d1a99c3e332d5045a infra/modules/region-host/group-init.sh.tftpl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3b;basis=New root user-data boot: operator Terraform/SSM supply scoped bootstrap object version/hash, queue, reader inventory and ingress; hash-verified bootstrap verifies signed IMDS identity. Root exclusively writes /etc/carlos/group.json, group-identity.json, region-config.json and group-ingress.json with current kernel boot ID; public address comes from protected IMDSv2, not signed document or headers. Install IMDS denial, chrony, dormant hairpin fence and protected drop-ins before bootstrap/install/start. No agent receipt is read by root; restart authority is separate maintenance permit. F1: operator-seeded SSM boot/bootstrap-object requires floor=absent-expected or required beside key/version/known_readers/deployment_aws_region; root copies it into group.json before config_digest. Missing or invalid floor refuses before bootstrap fetch; Terraform only names this SSM key and does not write the object. Agent and instance-control gain AF_NETLINK in their address-family allowlists, with no CAP_NET_ADMIN grant.;rev3=census-ruling-region-ha-p3b1-f4;basis=Move bootstrap and chrony staging to root:root 0700 /var/lib/carlos-group-boot through the existing O_NOFOLLOW walker; refuse unsafe preexisting directory. Bootstrap 0700; retain initial hash verification and rehash against a root-only captured SSM digest immediately before all three execs; named noexec refusal. Root reads only its own helper/hash and verified binary; no tenant/app/agent writable input or authority added. Final removal preserved. Round 5 adds /etc/sysctl.d to the same root-owned, non-writable, O_NOFOLLOW directory walker before its existing exclusive sysctl file write; missing directory is created, unsafe ancestors refuse. All other template bytes unchanged in round 5. file:external@0 0ae418d250d74909847959a71097a8a523ad157c479401d2d71d2212cbaa14f4 |
| -infra/modules/region-host/group-init.sh.tftpl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3b;basis=New root user-data boot: operator Terraform/SSM supply scoped bootstrap object version/hash, queue, reader inventory and ingress; hash-verified bootstrap verifies signed IMDS identity. Root exclusively writes /etc/carlos/group.json, group-identity.json, region-config.json and group-ingress.json with current kernel boot ID; public address comes from protected IMDSv2, not signed document or headers. Install IMDS denial, chrony, dormant hairpin fence and protected drop-ins before bootstrap/install/start. No agent receipt is read by root; restart authority is separate maintenance permit. F1: operator-seeded SSM boot/bootstrap-object requires floor=absent-expected or required beside key/version/known_readers/deployment_aws_region; root copies it into group.json before config_digest. Missing or invalid floor refuses before bootstrap fetch; Terraform only names this SSM key and does not write the object. Agent and instance-control gain AF_NETLINK in their address-family allowlists, with no CAP_NET_ADMIN grant.;rev3=census-ruling-region-ha-p3b1-f4;basis=Move bootstrap and chrony staging to root:root 0700 /var/lib/carlos-group-boot through the existing O_NOFOLLOW walker; refuse unsafe preexisting directory. Bootstrap 0700; retain initial hash verification and rehash against a root-only captured SSM digest immediately before all three execs; named noexec refusal. Root reads only its own helper/hash and verified binary; no tenant/app/agent writable input or authority added. Final removal preserved. Round 5 adds /etc/sysctl.d to the same root-owned, non-writable, O_NOFOLLOW directory walker before its existing exclusive sysctl file write; missing directory is created, unsafe ancestors refuse. All other template bytes unchanged in round 5. file:external@0 0ae418d250d74909847959a71097a8a523ad157c479401d2d71d2212cbaa14f4 |
| -@coverage infra/modules/region-host 1644713517d7b1044eefc2d4c4ce2c33394a83e088ee8bcb86b08c019f5b37ff |
| +@legacy b5f3cc30f0a0bbe84955685fe50876caa2bc4f1cea8f905be308f341eaa3311b infra/modules/region-host/group-init.sh.tftpl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3b;basis=New root user-data boot: operator Terraform/SSM supply scoped bootstrap object version/hash, queue, reader inventory and ingress; hash-verified bootstrap verifies signed IMDS identity. Root exclusively writes /etc/carlos/group.json, group-identity.json, region-config.json and group-ingress.json with current kernel boot ID; public address comes from protected IMDSv2, not signed document or headers. Install IMDS denial, chrony, dormant hairpin fence and protected drop-ins before bootstrap/install/start. No agent receipt is read by root; restart authority is separate maintenance permit. F1: operator-seeded SSM boot/bootstrap-object requires floor=absent-expected or required beside key/version/known_readers/deployment_aws_region; root copies it into group.json before config_digest. Missing or invalid floor refuses before bootstrap fetch; Terraform only names this SSM key and does not write the object. Agent and instance-control gain AF_NETLINK in their address-family allowlists, with no CAP_NET_ADMIN grant.;rev3=census-ruling-region-ha-p3b1-f4;basis=Move bootstrap and chrony staging to root:root 0700 /var/lib/carlos-group-boot through the existing O_NOFOLLOW walker; refuse unsafe preexisting directory. Bootstrap 0700; retain initial hash verification and rehash against a root-only captured SSM digest immediately before all three execs; named noexec refusal. Root reads only its own helper/hash and verified binary; no tenant/app/agent writable input or authority added. Final removal preserved. Round 5 adds /etc/sysctl.d to the same root-owned, non-writable, O_NOFOLLOW directory walker before its existing exclusive sysctl file write; missing directory is created, unsafe ancestors refuse. Round 6 scopes umask 022 to the verified host-env install exec in a subshell; its bootstrap/install-channel children create traversable install directories and readable unit files, matching fixed boxes. Parent umask remains 077; verify-identity is read-only and hairpin install creates only 0700/0600 private state. Explicitly chmod the freshly created instance-control drop-in directory 0755. host.env remains explicitly chmodded 0600 in unchanged released Go. No new input/read or write authority; all verification and directory trust checks remain. file:external@0 0c17d53b79760e8d5c45419b428dd1d3812ef6e457f0948891c89ef2bdfd7fb1 |
| +infra/modules/region-host/group-init.sh.tftpl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3b;basis=New root user-data boot: operator Terraform/SSM supply scoped bootstrap object version/hash, queue, reader inventory and ingress; hash-verified bootstrap verifies signed IMDS identity. Root exclusively writes /etc/carlos/group.json, group-identity.json, region-config.json and group-ingress.json with current kernel boot ID; public address comes from protected IMDSv2, not signed document or headers. Install IMDS denial, chrony, dormant hairpin fence and protected drop-ins before bootstrap/install/start. No agent receipt is read by root; restart authority is separate maintenance permit. F1: operator-seeded SSM boot/bootstrap-object requires floor=absent-expected or required beside key/version/known_readers/deployment_aws_region; root copies it into group.json before config_digest. Missing or invalid floor refuses before bootstrap fetch; Terraform only names this SSM key and does not write the object. Agent and instance-control gain AF_NETLINK in their address-family allowlists, with no CAP_NET_ADMIN grant.;rev3=census-ruling-region-ha-p3b1-f4;basis=Move bootstrap and chrony staging to root:root 0700 /var/lib/carlos-group-boot through the existing O_NOFOLLOW walker; refuse unsafe preexisting directory. Bootstrap 0700; retain initial hash verification and rehash against a root-only captured SSM digest immediately before all three execs; named noexec refusal. Root reads only its own helper/hash and verified binary; no tenant/app/agent writable input or authority added. Final removal preserved. Round 5 adds /etc/sysctl.d to the same root-owned, non-writable, O_NOFOLLOW directory walker before its existing exclusive sysctl file write; missing directory is created, unsafe ancestors refuse. Round 6 scopes umask 022 to the verified host-env install exec in a subshell; its bootstrap/install-channel children create traversable install directories and readable unit files, matching fixed boxes. Parent umask remains 077; verify-identity is read-only and hairpin install creates only 0700/0600 private state. Explicitly chmod the freshly created instance-control drop-in directory 0755. host.env remains explicitly chmodded 0600 in unchanged released Go. No new input/read or write authority; all verification and directory trust checks remain. file:external@0 0c17d53b79760e8d5c45419b428dd1d3812ef6e457f0948891c89ef2bdfd7fb1 |
| +@coverage infra/modules/region-host 367679e9ee5bb141a5f70cd17c7963b335b28cd1828da351a4b20a80033f392d |
diff --git a/scripts/region_group_boot_umask_test.py b/scripts/region_group_boot_umask_test.py| new file mode 100644 |
| index 000000000..beec53984 |
| --- /dev/null |
| +++ b/scripts/region_group_boot_umask_test.py |
| @@ -0,0 +1,120 @@ |
| +import os |
| +import stat |
| +import subprocess |
| +import sys |
| +import tempfile |
| +import textwrap |
| +import unittest |
| +from pathlib import Path |
| + |
| +TEMPLATE = Path(__file__).resolve().parent.parent / 'infra/modules/region-host/group-init.sh.tftpl' |
| +STAGING = '/var/lib/carlos-group-boot' |
| +HOSTENV = '( umask 022; exec ' + STAGING + '/bootstrap system host-env write --group --deployment "$CARLOS_DEPLOYMENT" --region "$CARLOS_REGION" --aws-region "$AWS_REGION" --install )' |
| +HAIRPIN = STAGING + '/bootstrap instance-control --group-hairpin-install' |
| +IDENTITY = "subprocess.check_call([work + 'bootstrap', 'instance-control', '--group-hairpin-verify-identity'], timeout=10)" |
| +DROPIN = '/etc/systemd/system/carlos-instance-control.service.d' |
| + |
| +STUB = textwrap.dedent('''\ |
| + #!/bin/sh |
| + echo "$(umask) $*" >> "$STUB_LOG" |
| + mkdir "$STUB_DIR/implicit" |
| + : > "$STUB_DIR/implicit/file" |
| + exit "${STUB_EXIT:-0}" |
| +''') |
| + |
| + |
| +class UmaskTests(unittest.TestCase): |
| + @classmethod |
| + def setUpClass(cls): |
| + cls.lines = TEMPLATE.read_text().split('\n') |
| + |
| + def setUp(self): |
| + self.tmp = tempfile.TemporaryDirectory(dir=os.environ.get('TMPDIR')) |
| + self.addCleanup(self.tmp.cleanup) |
| + self.dir = Path(self.tmp.name) |
| + self.stub = self.dir / 'stub' |
| + self.stub.write_text(STUB) |
| + self.stub.chmod(0o700) |
| + self.log = self.dir / 'log' |
| + self.env = dict(os.environ, STUB_LOG=str(self.log), CARLOS_DEPLOYMENT='d', CARLOS_REGION='r', AWS_REGION='a') |
| + |
| + def run_sh(self, line, exit_code): |
| + line = line.replace(STAGING + '/bootstrap', str(self.stub)) |
| + self.assertIn(str(self.stub), line) |
| + work = self.dir / ('w%d' % exit_code) |
| + work.mkdir() |
| + env = dict(self.env, STUB_DIR=str(work), STUB_EXIT=str(exit_code)) |
| + out = subprocess.run(['sh', '-c', 'umask 077\n' + line + '\nstatus=$?\necho "parent $(umask) $status"'], env=env, capture_output=True, text=True, check=True) |
| + return work, out.stdout.split() |
| + |
| + def child_umask(self): |
| + return self.log.read_text().split()[0] |
| + |
| + def test_template_has_exact_lines(self): |
| + for line in (HOSTENV, HAIRPIN, IDENTITY): |
| + self.assertEqual(self.lines.count(line), 1, line) |
| + self.assertEqual(self.lines[2], 'umask 077') |
| + self.assertEqual(sum('umask' in line for line in self.lines), 2) |
| + verify = 'python3 -I ' + STAGING + '/carlos_group_boot_verify.py' |
| + self.assertEqual(self.lines[self.lines.index(HOSTENV) - 1], verify) |
| + |
| + def test_hostenv_child_gets_022_and_parent_stays_077(self): |
| + for code in (0, 3): |
| + with self.subTest(exit=code): |
| + self.log.unlink(missing_ok=True) |
| + work, out = self.run_sh(HOSTENV, code) |
| + self.assertEqual(self.child_umask(), '0022') |
| + self.assertEqual(out, ['parent', '0077', str(code)]) |
| + self.assertEqual(stat.S_IMODE((work / 'implicit').stat().st_mode), 0o755) |
| + self.assertEqual(stat.S_IMODE((work / 'implicit/file').stat().st_mode), 0o644) |
| + |
| + def test_hairpin_install_child_keeps_077(self): |
| + for code in (0, 3): |
| + with self.subTest(exit=code): |
| + self.log.unlink(missing_ok=True) |
| + work, out = self.run_sh(HAIRPIN, code) |
| + self.assertEqual(self.child_umask(), '0077') |
| + self.assertEqual(out, ['parent', '0077', str(code)]) |
| + self.assertEqual(stat.S_IMODE((work / 'implicit').stat().st_mode), 0o700) |
| + self.assertEqual(stat.S_IMODE((work / 'implicit/file').stat().st_mode), 0o600) |
| + |
| + def test_python_identity_child_keeps_077(self): |
| + for code in (0, 3): |
| + with self.subTest(exit=code): |
| + self.log.unlink(missing_ok=True) |
| + work = self.dir / ('p%d' % code) |
| + work.mkdir() |
| + env = dict(self.env, STUB_DIR=str(work), STUB_EXIT=str(code)) |
| + src = 'import os, subprocess\nos.umask(0o077)\nwork = %r\ntry:\n %s\nexcept subprocess.CalledProcessError:\n pass\nprint(oct(os.umask(0o077)))\n' % (str(self.dir) + '/', IDENTITY) |
| + src = src.replace("work + 'bootstrap'", repr(str(self.stub))) |
| + out = subprocess.run([sys.executable, '-I', '-c', src], env=env, capture_output=True, text=True, check=True) |
| + self.assertEqual(self.child_umask(), '0077') |
| + self.assertEqual(out.stdout.strip(), '0o77') |
| + self.assertEqual(stat.S_IMODE((work / 'implicit').stat().st_mode), 0o700) |
| + |
| + def test_template_writes_stay_explicit_modes(self): |
| + start = self.lines.index('def write(path, body, mode=0o644):') |
| + helper = '\n'.join(self.lines[start:start + 7]) |
| + self.assertIn('os.fchmod(fd, mode)', helper) |
| + for mode in (0o600, 0o644): |
| + path = self.dir / ('f%o' % mode) |
| + code = 'import os\nos.umask(0o077)\n' + helper + '\nwrite(%r, b"x", %d)\n' % (str(path), mode) |
| + subprocess.run([sys.executable, '-I', '-c', code], check=True) |
| + self.assertEqual(stat.S_IMODE(path.stat().st_mode), mode) |
| + self.assertIn("write(work + 'bootstrap.sha256', sha.encode(), 0o600)", self.lines) |
| + |
| + def test_dropin_directory_chmodded_after_mkdir(self): |
| + i = self.lines.index("os.mkdir('%s', 0o755)" % DROPIN) |
| + self.assertEqual(self.lines[i + 1], "os.chmod('%s', 0o755)" % DROPIN) |
| + root = self.dir / 'd' |
| + code = 'import os\nos.umask(0o077)\nos.mkdir(%r, 0o755)\n' % str(root) |
| + subprocess.run([sys.executable, '-I', '-c', code], check=True) |
| + self.assertEqual(stat.S_IMODE(root.stat().st_mode), 0o700) |
| + code += 'os.chmod(%r, 0o755)\n' % str(root) |
| + root.rmdir() |
| + subprocess.run([sys.executable, '-I', '-c', code], check=True) |
| + self.assertEqual(stat.S_IMODE(root.stat().st_mode), 0o755) |
| + |
| + |
| +if __name__ == '__main__': |
| + unittest.main() |