hosted slice 2: review fixes, image caller role and tfvars host
Task 32 finding 4. equipo-hosted-image takes a required -expect-role, the caller ARN the run bound at its first ops guard, and its STS check (with the tool's own frozen keys) now compares the account and exactly that ARN before any image-service call: another role or session in the same account is refused. ops guard -print-role prints the bound ARN; the account plan's img wrapper passes it. Task 32 finding 6. ops tfvars --instance-host uses the egress manifest's own check (ctlegress.ExactHost): exactly amadan.net, the forge, is refused and a name under it is a different host (Task 2's ruling); a label with a leading hyphen is refused as the manifest does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
12 files changed,
+358
−47
cmd/equipo-hosted-image/main.go+5 −2cmd/equipo-hosted-image/main_test.go+63 −5docs/superpowers/plans/2026-11-30-hosted-prod-account-plan.md+4 −2internal/hostedimage/caller_test.go+87 −0internal/hostedimage/publish.go+27 −10internal/hostedimage/publish_test.go+16 −8internal/hostedops/fake_test.go+4 −0internal/hostedops/ops.go+30 −7internal/hostedops/t32_test.go+77 −0internal/hostedops/tfvars.go+10 −6internal/isolatedjob/awsjob/imageaws/imageaws.go+3 −3internal/isolatedjob/awsjob/imageaws/imageaws_test.go+32 −4
diff --git a/cmd/equipo-hosted-image/main.go b/cmd/equipo-hosted-image/main.go| index a9674a47..be4be128 100644 |
| --- a/cmd/equipo-hosted-image/main.go |
| +++ b/cmd/equipo-hosted-image/main.go |
| @@ -1,7 +1,7 @@ |
| // Command equipo-hosted-image builds the hosted runner's image input and |
| // publishes it as a Lambda MicroVM image (spec §4; plan Task 10): |
| // |
| -// equipo-hosted-image [-profile P] -account A -region R -prefix X COMMAND [flags] |
| +// equipo-hosted-image [-profile P] -account A -region R -prefix X [-expect-role ARN] COMMAND [flags] |
| // |
| // build -manifest FILE -inputs DIR [-gitleaks-config FILE] -out FILE |
| // scans every pinned input (archives and RPMs file by file) with the |
| @@ -36,7 +36,9 @@ |
| // prefix as flags: static credentials only, resolved once before any |
| // call (-profile, else AWS_PROFILE, else default; awsctl.ResolveStatic); |
| // clients pinned to the region's standard endpoints, one attempt per |
| -// call, 60 s per call; STS must name -account before any other call; |
| +// call, 60 s per call; STS, asked with those keys, must name -account and |
| +// exactly -expect-role (required: the caller ARN the run bound at its |
| +// first ops guard, from ops guard -print-role) before any other call; |
| // waits bounded (at most 60 minutes); every name the tool creates, |
| // deletes or waits on carries -prefix. The SDK half is |
| // internal/isolatedjob/awsjob/imageaws; the guards are hostedimage's. |
| @@ -150,6 +152,7 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer, connect c |
| top.StringVar(&t.Account, "account", "", "the one account the tool may act in") |
| top.StringVar(&t.Region, "region", "", "the one region the tool may act in") |
| top.StringVar(&t.Prefix, "prefix", "", "every name the tool touches starts with this") |
| + top.StringVar(&t.Caller, "expect-role", "", "the caller ARN STS must name: the role and session the run bound at its first ops guard (ops guard -print-role)") |
| if err := top.Parse(args); err != nil { |
| return report(stderr, usage("%v", err)) |
| } |
diff --git a/cmd/equipo-hosted-image/main_test.go b/cmd/equipo-hosted-image/main_test.go| index f87018f2..8dbf3f0a 100644 |
| --- a/cmd/equipo-hosted-image/main_test.go |
| +++ b/cmd/equipo-hosted-image/main_test.go |
| @@ -25,7 +25,11 @@ const ( |
| prefix = "equipo-hosted-prod-1201-abcdef" |
| ) |
| -var global = []string{"-account", acct, "-region", region, "-prefix", prefix} |
| +// approvedRole is the role and session the run bound at its first ops |
| +// guard; every AWS command is given its STS ARN (-expect-role). |
| +const approvedRole = "OrganizationAccountAccessRole/equipo-hosted-bringup" |
| + |
| +var global = []string{"-account", acct, "-region", region, "-prefix", prefix, "-expect-role", "arn:aws:sts::" + acct + ":assumed-role/" + approvedRole} |
| // noConnect fails the test if a command reaches for credentials. |
| func noConnect(t *testing.T) connectFunc { |
| @@ -196,9 +200,9 @@ func TestCredentialsRefusedBeforeAnyCall(t *testing.T) { |
| type fake struct{ calls []string } |
| func (f *fake) Region() string { return region } |
| -func (f *fake) CallerAccount(context.Context) (string, error) { |
| - f.calls = append(f.calls, "CallerAccount") |
| - return "444455556666", nil |
| +func (f *fake) CallerIdentity(context.Context) (hostedimage.Caller, error) { |
| + f.calls = append(f.calls, "CallerIdentity") |
| + return hostedimage.Caller{Account: "444455556666", ARN: "arn:aws:sts::444455556666:assumed-role/" + approvedRole}, nil |
| } |
| func (f *fake) BaseVersions(context.Context, string) ([]hostedimage.BaseVersion, error) { |
| f.calls = append(f.calls, "BaseVersions") |
| @@ -248,7 +252,7 @@ func TestPublishFlags(t *testing.T) { |
| if code != hostedimage.ExitGuard || stdout != "" || strings.Count(stderr, "\n") != 1 || !strings.Contains(stderr, "caller account") { |
| t.Fatalf("publish = %d, stdout %q, stderr %q", code, stdout, stderr) |
| } |
| - if !slices.Equal(f.calls, []string{"CallerAccount"}) { |
| + if !slices.Equal(f.calls, []string{"CallerIdentity"}) { |
| t.Fatalf("calls %v, want only the account check", f.calls) |
| } |
| if code, _, _ := runCmd(t, conn, append(slices.Clone(args[:len(args)-2]), "-size", "huge")...); code != hostedimage.ExitUsage { |
| @@ -347,3 +351,57 @@ func TestAssembleThenBuildFromBundle(t *testing.T) { |
| t.Fatalf("assemble with the checked-in pins = %d %s; want the pins refused as incomplete", code, stderr) |
| } |
| } |
| + |
| +// Task 32 (whole-branch review finding 4). |
| + |
| +// roleFake is a provider in the right account whose caller is arn. |
| +type roleFake struct { |
| + fake |
| + arn string |
| +} |
| + |
| +func (f *roleFake) CallerIdentity(context.Context) (hostedimage.Caller, error) { |
| + f.calls = append(f.calls, "CallerIdentity") |
| + return hostedimage.Caller{Account: acct, ARN: f.arn}, nil |
| +} |
| + |
| +// TestWrongRoleRefusedBeforeImageCall: a profile that resolves to another |
| +// role in the same account is refused by every AWS command, with the |
| +// guard's exit, before any image-service call. |
| +func TestWrongRoleRefusedBeforeImageCall(t *testing.T) { |
| + cmds := map[string][]string{ |
| + "list": {"list"}, |
| + "delete": {"delete", "-arn", "arn:aws:lambda:" + region + ":" + acct + ":microvm-image:" + prefix}, |
| + "connector-wait": {"connector-wait", "-arn", "arn:aws:lambda:" + region + ":" + acct + ":network-connector:nc-1"}, |
| + "publish": {"publish", "-ledger", t.TempDir(), "-name", prefix, "-bucket", "equipo-hosted-image-inputs", "-key", "images/x.zip", |
| + "-base-version", "4", "-build-role-arn", "arn:aws:iam::" + acct + ":role/build", "-log-group", "/equipo-hosted/image-build", |
| + "-size", "large", "-capabilities", "ALL", "-out", filepath.Join(t.TempDir(), "image.json")}, |
| + } |
| + for name, args := range cmds { |
| + f := &roleFake{arn: "arn:aws:sts::" + acct + ":assumed-role/AnotherRole/equipo-hosted-bringup"} |
| + conn := func(context.Context, string, string) (hostedimage.Provider, error) { return f, nil } |
| + code, stdout, stderr := runCmd(t, conn, append(slices.Clone(global), args...)...) |
| + if code != hostedimage.ExitGuard || stdout != "" || !strings.Contains(stderr, "AnotherRole") { |
| + t.Errorf("%s as another role = %d, stdout %q, stderr %q; want a guard refusal", name, code, stdout, stderr) |
| + } |
| + if !slices.Equal(f.calls, []string{"CallerIdentity"}) { |
| + t.Errorf("%s as another role: calls %v, want only the caller check", name, f.calls) |
| + } |
| + } |
| +} |
| + |
| +// TestExpectRoleRequired: an AWS command without -expect-role, or with |
| +// one in another account, is refused before any credential is read. |
| +func TestExpectRoleRequired(t *testing.T) { |
| + base := []string{"-account", acct, "-region", region, "-prefix", prefix} |
| + for name, extra := range map[string][]string{ |
| + "missing": nil, |
| + "another account": {"-expect-role", "arn:aws:sts::444455556666:assumed-role/" + approvedRole}, |
| + "a role, no ARN": {"-expect-role", approvedRole}, |
| + } { |
| + code, _, stderr := runCmd(t, noConnect(t), append(append(slices.Clone(base), extra...), "list")...) |
| + if code != hostedimage.ExitUsage || !strings.Contains(stderr, "expect-role") { |
| + t.Errorf("list, -expect-role %s = %d %s, want a usage refusal", name, code, stderr) |
| + } |
| + } |
| +} |
diff --git a/docs/superpowers/plans/2026-11-30-hosted-prod-account-plan.md b/docs/superpowers/plans/2026-11-30-hosted-prod-account-plan.md| index fd92141d..34f1496c 100644 |
| --- a/docs/superpowers/plans/2026-11-30-hosted-prod-account-plan.md |
| +++ b/docs/superpowers/plans/2026-11-30-hosted-prod-account-plan.md |
| @@ -31,7 +31,9 @@ profile, as the other member accounts are. |
| **Envelope.** Every `ops`, `img` and `cert` invocation that calls AWS resolves its credentials once, |
| verifies through STS that they are `149757027610` under |
| `OrganizationAccountAccessRole/equipo-hosted-bringup` in `eu-west-1`, and uses those same frozen |
| -credentials for every later call in that process (slice 2 Task 33). `ops tf` hands the same frozen keys |
| +credentials for every later call in that process (slice 2 Task 33). `img` verifies against the exact |
| +caller ARN the run bound at its first `ops guard` (`-expect-role`, from `ops guard -print-role`), so |
| +another role or session in the same account is refused before any image call (Task 32). `ops tf` hands the same frozen keys |
| to `tofu`, with every profile, config-file and endpoint-override variable unset, and refuses a root |
| whose backend block sets anything but `bucket`, `key`, `region` and `use_lockfile`. Every OpenTofu provider block carries `allowed_account_ids = ["149757027610"]` and |
| `region = "eu-west-1"`, so the provider itself refuses any other account. The named exceptions to |
| @@ -279,7 +281,7 @@ RUN=$RUN |
| PIN=$PIN |
| REPO=$P/repo |
| ops() { "$P/bin/equipo-hosted-ops" -dir "$P" -run "$RUN" -profile equipo-hosted -account 149757027610 -region eu-west-1 "\$@"; } |
| -img() ( eval "\$(aws configure export-credentials --profile equipo-hosted --format env)"; unset AWS_PROFILE; exec "$P/bin/equipo-hosted-image" -account 149757027610 -region eu-west-1 -prefix "equipo-hosted-prod-$RUN" "\$@" ) # static keys in this subshell only (Task 17 R42) |
| +img() ( r=\$(ops guard -print-role) || exit 99; eval "\$(aws configure export-credentials --profile equipo-hosted --format env)"; unset AWS_PROFILE; exec "$P/bin/equipo-hosted-image" -account 149757027610 -region eu-west-1 -prefix "equipo-hosted-prod-$RUN" -expect-role "\$r" "\$@" ) # static keys in this subshell only (Task 17 R42); the caller the run bound (Task 32) |
| cert() { "$P/bin/equipo-hosted-certify" "\$@"; } |
| EOF |
| "$P/bin/equipo-hosted-ops" -dir "$P" -run "$RUN" ledger init |
diff --git a/internal/hostedimage/caller_test.go b/internal/hostedimage/caller_test.go| new file mode 100644 |
| index 00000000..4808c793 |
| --- /dev/null |
| +++ b/internal/hostedimage/caller_test.go |
| @@ -0,0 +1,87 @@ |
| +package hostedimage |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "slices" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// Task 32 (whole-branch review finding 4): every AWS operation verifies |
| +// the whole caller with the tool's own frozen credentials before any |
| +// image-service call: the account, and the role and session the run |
| +// bound at its first ops guard (Target.Caller, -expect-role). A profile |
| +// that resolves to another role in the same account is refused. |
| + |
| +// ops are the four AWS operations, each run against p. |
| +var ops = map[string]func(tt *testing.T, p Provider, t Target) error{ |
| + "publish": func(tt *testing.T, p Provider, t Target) error { |
| + _, err := Publish(context.Background(), p, t, request(tt)) |
| + return err |
| + }, |
| + "delete": func(_ *testing.T, p Provider, t Target) error { |
| + _, err := Delete(context.Background(), p, t, "arn:aws:lambda:"+region+":"+acct+":microvm-image:"+prefix, wait()) |
| + return err |
| + }, |
| + "list": func(_ *testing.T, p Provider, t Target) error { _, err := List(context.Background(), p, t); return err }, |
| + "connector-wait": func(_ *testing.T, p Provider, t Target) error { |
| + _, err := ConnectorWait(context.Background(), p, t, "arn:aws:lambda:"+region+":"+acct+":network-connector:nc-1", wait()) |
| + return err |
| + }, |
| +} |
| + |
| +func wait() Wait { |
| + return Wait{Timeout: time.Minute, Interval: time.Second, Clock: &fakeClock{now: time.Unix(1_800_000_000, 0)}} |
| +} |
| + |
| +func TestSameAccountWrongRoleRefused(t *testing.T) { |
| + for _, caller := range []string{ |
| + "arn:aws:sts::" + acct + ":assumed-role/AnotherRole/equipo-hosted-bringup", |
| + "arn:aws:sts::" + acct + ":assumed-role/OrganizationAccountAccessRole/someone-else", |
| + "arn:aws:iam::" + acct + ":user/someone", |
| + approvedCaller + "x", |
| + } { |
| + for name, op := range ops { |
| + t.Run(name+" as "+caller, func(t *testing.T) { |
| + p := newFake(t) |
| + p.arn = caller |
| + err := op(t, p, target) |
| + var f *Failure |
| + if !errors.As(err, &f) || f.Exit != ExitGuard { |
| + t.Fatalf("%s as %s = %v, want a guard refusal", name, caller, err) |
| + } |
| + if !slices.Equal(p.calls, []string{"CallerIdentity"}) { |
| + t.Fatalf("%s as %s: calls %v, want only the caller check", name, caller, p.calls) |
| + } |
| + }) |
| + } |
| + } |
| +} |
| + |
| +// TestExpectedCallerRequired: a target with no expected caller, or one |
| +// that is not a caller ARN in the target account, is refused before any |
| +// call. |
| +func TestExpectedCallerRequired(t *testing.T) { |
| + for _, caller := range []string{ |
| + "", |
| + approvedRole, |
| + "arn:aws:sts::444455556666:assumed-role/" + approvedRole, |
| + "arn:aws:iam::" + acct + ":role/OrganizationAccountAccessRole", |
| + "arn:aws:sts::" + acct + ":assumed-role/OrganizationAccountAccessRole", |
| + "arn:aws:sts::" + acct + ":assumed-role/OrganizationAccountAccessRole/a/b", |
| + } { |
| + for name, op := range ops { |
| + t.Run(name+" expecting "+caller, func(t *testing.T) { |
| + p := newFake(t) |
| + tg := target |
| + tg.Caller = caller |
| + err := op(t, p, tg) |
| + var f *Failure |
| + if !errors.As(err, &f) || f.Exit != ExitUsage || len(p.calls) != 0 { |
| + t.Fatalf("%s expecting %q = %v with calls %v, want a usage refusal and no call", name, caller, err, p.calls) |
| + } |
| + }) |
| + } |
| + } |
| +} |
diff --git a/internal/hostedimage/publish.go b/internal/hostedimage/publish.go| index bc4426ba..5e53079a 100644 |
| --- a/internal/hostedimage/publish.go |
| +++ b/internal/hostedimage/publish.go |
| @@ -100,7 +100,7 @@ type Connector struct{ ARN, Name, State, Reason string } |
| // it; each method is one SDK call (or one paged read). |
| type Provider interface { |
| Region() string // the region every call goes to |
| - CallerAccount(ctx context.Context) (string, error) |
| + CallerIdentity(ctx context.Context) (Caller, error) |
| BaseVersions(ctx context.Context, baseARN string) ([]BaseVersion, error) |
| CreateImage(ctx context.Context, c CreateImage) (Created, error) |
| GetImage(ctx context.Context, id string) (Image, error) |
| @@ -110,9 +110,13 @@ type Provider interface { |
| GetConnector(ctx context.Context, id string) (Connector, error) |
| } |
| +// Caller is who STS says the credentials are: the account and the |
| +// caller's ARN. |
| +type Caller struct{ Account, ARN string } |
| + |
| // Target is where the tool may act: the account, the region and the name |
| -// prefix, all given as flags. |
| -type Target struct{ Account, Region, Prefix string } |
| +// prefix, and the caller it must act as, all given as flags. |
| +type Target struct{ Account, Region, Prefix, Caller string } |
| // Clock is time for the waits; tests use a fake one. |
| type Clock interface { |
| @@ -166,6 +170,10 @@ var ( |
| accountPattern = regexp.MustCompile(`^[0-9]{12}$`) |
| regionPattern = regexp.MustCompile(`^[a-z]{2}(-[a-z]+)+-[0-9]$`) |
| prefixPattern = regexp.MustCompile(`^[a-z][a-z0-9-]{1,62}[a-z0-9]$`) |
| + // callerPattern is a caller ARN as STS names it: a role's session, |
| + // or an IAM user. The account is its first group (a session) or its |
| + // second (a user). |
| + callerPattern = regexp.MustCompile(`^arn:aws:(?:sts::([0-9]{12}):assumed-role/[A-Za-z0-9+=,.@_-]{1,64}/[A-Za-z0-9+=,.@_-]{2,64}|iam::([0-9]{12}):user/[A-Za-z0-9+=,.@_-]{1,64})$`) |
| namePattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,127}$`) |
| bucketPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$`) |
| keyPattern = regexp.MustCompile(`^[A-Za-z0-9!_.*'()/:+=@-]+$`) |
| @@ -202,9 +210,11 @@ func apiFailure(op string, err error) *Failure { |
| return f |
| } |
| -// Check refuses a target that is not one account, one region and a |
| -// prefix every name the tool touches must carry. |
| +// Check refuses a target that is not one account, one region, a prefix |
| +// every name the tool touches must carry, and the one caller in that |
| +// account the tool must act as. |
| func (t Target) Check() error { |
| + m := callerPattern.FindStringSubmatch(t.Caller) |
| switch { |
| case !accountPattern.MatchString(t.Account): |
| return usage("-account %q is not a 12-digit account ID", t.Account) |
| @@ -212,6 +222,8 @@ func (t Target) Check() error { |
| return usage("-region %q is not a region such as eu-west-1", t.Region) |
| case !prefixPattern.MatchString(t.Prefix): |
| return usage("-prefix %q must be 3 to 64 lower-case letters, digits and '-'", t.Prefix) |
| + case m == nil || m[1]+m[2] != t.Account: |
| + return usage("-expect-role %q is not a caller ARN in account %s (arn:aws:sts::%s:assumed-role/ROLE/SESSION, from ops guard -print-role)", t.Caller, t.Account, t.Account) |
| } |
| return nil |
| } |
| @@ -231,18 +243,23 @@ func (t Target) arnIn(arn, kind string) (string, bool) { |
| } |
| // checkCaller is the guard every AWS operation passes first: the provider |
| -// calls the target's region, and STS names the target's account. Nothing |
| -// else is called before it passes. |
| +// calls the target's region, and STS, asked with the tool's own frozen |
| +// credentials, names the target's account and exactly the caller the run |
| +// bound at its first ops guard (t.Caller): another role or session in |
| +// the same account is refused. Nothing else is called before it passes. |
| func checkCaller(ctx context.Context, p Provider, t Target) error { |
| if r := p.Region(); r != t.Region { |
| return guard("the client calls region %q, want %s", r, t.Region) |
| } |
| - a, err := p.CallerAccount(ctx) |
| + id, err := p.CallerIdentity(ctx) |
| if err != nil { |
| return apiFailure("GetCallerIdentity", err) |
| } |
| - if a != t.Account { |
| - return guard("caller account is %q, want %s", a, t.Account) |
| + if id.Account != t.Account { |
| + return guard("caller account is %q, want %s", id.Account, t.Account) |
| + } |
| + if id.ARN != t.Caller { |
| + return guard("the caller is %q, but this run is bound to %s", id.ARN, t.Caller) |
| } |
| return nil |
| } |
diff --git a/internal/hostedimage/publish_test.go b/internal/hostedimage/publish_test.go| index 90f36324..a4c93ee9 100644 |
| --- a/internal/hostedimage/publish_test.go |
| +++ b/internal/hostedimage/publish_test.go |
| @@ -20,7 +20,14 @@ const ( |
| prefix = "equipo-hosted-prod-1201-abcdef" |
| ) |
| -var target = Target{Account: acct, Region: region, Prefix: prefix} |
| +// approvedRole is the role and session the run bound at its first guard; |
| +// approvedCaller its STS ARN. |
| +const ( |
| + approvedRole = "OrganizationAccountAccessRole/equipo-hosted-bringup" |
| + approvedCaller = "arn:aws:sts::" + acct + ":assumed-role/" + approvedRole |
| +) |
| + |
| +var target = Target{Account: acct, Region: region, Prefix: prefix, Caller: approvedCaller} |
| // fakeProvider answers from its fields and records every call. It is the |
| // provider-neutral half's whole view of AWS; the SDK half is tested in |
| @@ -29,6 +36,7 @@ type fakeProvider struct { |
| t *testing.T |
| region string |
| account string |
| + arn string // the caller ARN STS names |
| calls []string |
| base []BaseVersion |
| created []CreateImage |
| @@ -41,15 +49,15 @@ type fakeProvider struct { |
| } |
| func newFake(t *testing.T) *fakeProvider { |
| - return &fakeProvider{t: t, region: region, account: acct, |
| + return &fakeProvider{t: t, region: region, account: acct, arn: approvedCaller, |
| base: []BaseVersion{{Version: "3", Status: "AVAILABLE"}, {Version: "4", Status: "AVAILABLE"}, {Version: "5", Status: "AVAILABLE"}}, |
| images: map[string]Image{}} |
| } |
| func (f *fakeProvider) Region() string { return f.region } |
| -func (f *fakeProvider) CallerAccount(context.Context) (string, error) { |
| - f.calls = append(f.calls, "CallerAccount") |
| - return f.account, nil |
| +func (f *fakeProvider) CallerIdentity(context.Context) (Caller, error) { |
| + f.calls = append(f.calls, "CallerIdentity") |
| + return Caller{Account: f.account, ARN: f.arn}, nil |
| } |
| func (f *fakeProvider) BaseVersions(_ context.Context, base string) ([]BaseVersion, error) { |
| f.calls = append(f.calls, "BaseVersions") |
| @@ -169,7 +177,7 @@ func TestPublishRefusesOtherAccount(t *testing.T) { |
| if f := failureOf(t, err); f.Exit != ExitGuard { |
| t.Fatalf("Publish = %+v, want a guard refusal", f) |
| } |
| - if !slices.Equal(p.calls, []string{"CallerAccount"}) { |
| + if !slices.Equal(p.calls, []string{"CallerIdentity"}) { |
| t.Fatalf("calls %v, want only the account check", p.calls) |
| } |
| if j := readJournal(t, req.Ledger); len(j) != 0 { |
| @@ -218,7 +226,7 @@ func TestPublishRefusesForeignName(t *testing.T) { |
| } |
| }) |
| } |
| - if _, err := Publish(context.Background(), newFake(t), Target{Account: acct, Region: region, Prefix: ""}, request(t)); err == nil { |
| + if _, err := Publish(context.Background(), newFake(t), Target{Account: acct, Region: region, Prefix: "", Caller: approvedCaller}, request(t)); err == nil { |
| t.Fatal("Publish with no prefix = nil, want a refusal") |
| } |
| } |
| @@ -426,7 +434,7 @@ func TestListOnlyPrefix(t *testing.T) { |
| if err != nil || len(got) != 2 || got[0].Name != prefix || got[1].Name != prefix+"-2" { |
| t.Fatalf("List = %+v, %v", got, err) |
| } |
| - if p.calls[0] != "CallerAccount" { |
| + if p.calls[0] != "CallerIdentity" { |
| t.Fatalf("calls %v, want the account check first", p.calls) |
| } |
| } |
diff --git a/internal/hostedops/fake_test.go b/internal/hostedops/fake_test.go| index d44b04ac..9ce08e37 100644 |
| --- a/internal/hostedops/fake_test.go |
| +++ b/internal/hostedops/fake_test.go |
| @@ -49,6 +49,7 @@ type fakeCloud struct { |
| calls []string |
| clock *fakeClock |
| account string |
| + arn string // the caller's ARN, when not the approved role's |
| listVMs func(n int, image string) ([]VM, error) |
| nList int |
| @@ -109,6 +110,9 @@ func (f *fakeCloud) count(prefix string) int { |
| } |
| func (f *fakeCloud) Identity() Identity { |
| + if f.arn != "" { |
| + return Identity{Account: f.account, Region: testRegion, ARN: f.arn} |
| + } |
| return Identity{Account: f.account, Region: testRegion, ARN: "arn:aws:sts::" + f.account + ":assumed-role/OrganizationAccountAccessRole/equipo-hosted-bringup"} |
| } |
| func (f *fakeCloud) Keys() Keys { return f.keys } |
diff --git a/internal/hostedops/ops.go b/internal/hostedops/ops.go| index dba8b74f..6488f16b 100644 |
| --- a/internal/hostedops/ops.go |
| +++ b/internal/hostedops/ops.go |
| @@ -208,15 +208,12 @@ func (o *Ops) open() (Cloud, error) { |
| if err != nil { |
| return nil, err |
| } |
| - bound := "" // the caller the run's first guard verified (F6) |
| for _, e := range l.Entries { |
| if e.Verb == "GUARD" && (e.Attr("account") != o.Account || e.Attr("region") != o.Region) { |
| return nil, stopf("this run was opened for account %s in %s; refusing %s in %s", e.Attr("account"), e.Attr("region"), o.Account, o.Region) |
| } |
| - if e.Verb == "GUARD" && bound == "" { |
| - bound = e.Attr("arn") |
| - } |
| } |
| + bound := boundCaller(l) |
| c, err := o.Opener.Open(o.ctx, Target{Profile: o.Profile, Account: o.Account, Region: o.Region, Role: o.Role}) |
| if err != nil { |
| if IsStop(err) { |
| @@ -251,7 +248,7 @@ var commands map[string]command |
| func init() { |
| commands = map[string]command{ |
| - "guard": {true, cmdGuard, "verify the credentials (account, role, region)"}, |
| + "guard": {true, cmdGuard, "[-print-role]: verify the credentials (account, role, region); -print-role prints only the bound caller ARN"}, |
| "assert": {false, cmdAssert, "assert WHAT GOT WANT | assert NAMED-CHECK ...: 99 on a mismatch"}, |
| "fact": {false, cmdFact, "fact WHAT GOT WANT | fact TEXT | fact NAMED-CHECK: a FINDING on a mismatch"}, |
| "ledger init": {false, cmdLedgerInit, ""}, |
| @@ -457,11 +454,37 @@ func parseFlags(fs *flag.FlagSet, args []string) ([]string, error) { |
| } |
| } |
| +// boundCaller is the caller ARN the run's first guard verified (F6); |
| +// every later guard, and equipo-hosted-image's -expect-role, must name |
| +// it. "" before the first guard. |
| +func boundCaller(l *Ledger) string { |
| + for _, e := range l.Entries { |
| + if e.Verb == "GUARD" { |
| + return e.Attr("arn") |
| + } |
| + } |
| + return "" |
| +} |
| + |
| +// cmdGuard reports the verified caller. With -print-role it prints only |
| +// the caller ARN the run is bound to (boundCaller), for the account |
| +// plan's img wrapper to pass as equipo-hosted-image -expect-role; open |
| +// has already refused any other caller, and journalled this one. |
| func cmdGuard(o *Ops, args []string) error { |
| - if len(args) != 0 { |
| - return usagef("guard takes no arguments") |
| + fs := flag.NewFlagSet("guard", flag.ContinueOnError) |
| + printRole := fs.Bool("print-role", false, "print only the caller ARN the run is bound to") |
| + if pos, err := parseFlags(fs, args); err != nil || len(pos) != 0 { |
| + return usagef("guard [-print-role]") |
| } |
| id := o.cloud.Identity() |
| + if *printRole { |
| + l, err := o.ledger() |
| + if err != nil { |
| + return err |
| + } |
| + o.out("%s", boundCaller(l)) |
| + return nil |
| + } |
| o.out("guard: account %s, %s, region %s", id.Account, id.ARN, id.Region) |
| return nil |
| } |
diff --git a/internal/hostedops/t32_test.go b/internal/hostedops/t32_test.go| new file mode 100644 |
| index 00000000..ed61c919 |
| --- /dev/null |
| +++ b/internal/hostedops/t32_test.go |
| @@ -0,0 +1,77 @@ |
| +package hostedops |
| + |
| +import ( |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + |
| + "amadan.net/oficina/equipo/internal/hostedctl/ctlegress" |
| +) |
| + |
| +// Task 32 (the whole-branch review's fix wave): the run kit's fixes. |
| + |
| +// Finding 6: tfvars takes the instance host the controller's egress |
| +// manifest takes (ctlegress.ExactHost): a name under amadan.net is a |
| +// different host and is accepted; exactly amadan.net (the forge) is |
| +// refused, and so is anything else the manifest refuses. |
| +func TestTfvarsInstanceHostAsManifest(t *testing.T) { |
| + for _, tc := range []struct { |
| + host string |
| + ok bool |
| + }{ |
| + {"equipo.example.com", true}, |
| + {"equipo.amadan.net", true}, |
| + {"notamadan.net", true}, |
| + {"amadan.net.example.com", true}, |
| + {"amadan.net", false}, |
| + {"Equipo.example.com", false}, |
| + {"*.example.com", false}, |
| + {"example", false}, |
| + {"example.com.", false}, |
| + {"10.0.0.1", false}, |
| + {"example.123", false}, |
| + {"-bad.example.com", false}, |
| + } { |
| + t.Run(tc.host, func(t *testing.T) { |
| + if manifest := ctlegress.ExactHost(tc.host) == nil; manifest != tc.ok { |
| + t.Fatalf("the manifest takes %q: %t; this case says %t", tc.host, manifest, tc.ok) |
| + } |
| + r := newRig(t) |
| + code := r.ops("tfvars", "--instance-host", tc.host) |
| + b, _ := os.ReadFile(filepath.Join(r.dir, "prod.tfvars")) |
| + wrote := strings.Contains(string(b), `"host":"`+tc.host+`"`) |
| + if tc.ok && (code != 0 || !wrote) { |
| + t.Fatalf("tfvars --instance-host %s = %d (written %t), want accepted:\n%s", tc.host, code, wrote, r.out.String()) |
| + } |
| + if !tc.ok && (code != ExitFail || wrote) { |
| + t.Fatalf("tfvars --instance-host %s = %d (written %t), want refused:\n%s", tc.host, code, wrote, r.out.String()) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// Finding 4: ops guard -print-role prints only the caller ARN the run |
| +// bound at its first guard, the one the plan's img wrapper passes to |
| +// equipo-hosted-image as -expect-role; a caller that differs from it is |
| +// refused, as every ops command refuses it. |
| +func TestGuardPrintsBoundRole(t *testing.T) { |
| + r := newRig(t) |
| + var stdout, stderr strings.Builder |
| + r.deps.Stdout, r.deps.Stderr = &stdout, &stderr |
| + want := r.cloud.Identity().ARN |
| + for range 2 { |
| + stdout.Reset() |
| + if code := r.ops("guard", "-print-role"); code != 0 { |
| + t.Fatalf("guard -print-role = %d: %s", code, stderr.String()) |
| + } |
| + if stdout.String() != want+"\n" { |
| + t.Fatalf("guard -print-role printed %q, want only %q", stdout.String(), want+"\n") |
| + } |
| + } |
| + r.cloud.arn = "arn:aws:sts::" + ProdAccount + ":assumed-role/AnotherRole/equipo-hosted-bringup" |
| + stdout.Reset() |
| + if code := r.ops("guard", "-print-role"); code != ExitStop || stdout.Len() != 0 { |
| + t.Fatalf("guard -print-role as another role = %d, printed %q; want 99 and nothing", code, stdout.String()) |
| + } |
| +} |
diff --git a/internal/hostedops/tfvars.go b/internal/hostedops/tfvars.go| index a8449428..bc677fd2 100644 |
| --- a/internal/hostedops/tfvars.go |
| +++ b/internal/hostedops/tfvars.go |
| @@ -14,6 +14,8 @@ import ( |
| "regexp" |
| "sort" |
| "strings" |
| + |
| + "amadan.net/oficina/equipo/internal/hostedctl/ctlegress" |
| ) |
| // tfvars writes the one variables file every root reads (1.2, 5.1): only |
| @@ -32,10 +34,9 @@ import ( |
| // roots' fixed addresses keep their defaults. |
| var ( |
| - hostPattern = regexp.MustCompile(`^([a-z0-9-]+\.)+[a-z]{2,}$`) |
| - amiPattern = regexp.MustCompile(`^ami-[0-9a-f]{8,17}$`) |
| - digestRe = regexp.MustCompile(`sha256:[0-9a-f]{64}`) |
| - tagPattern = regexp.MustCompile(`^[0-9A-Za-z][0-9A-Za-z._-]{0,63}$`) |
| + amiPattern = regexp.MustCompile(`^ami-[0-9a-f]{8,17}$`) |
| + digestRe = regexp.MustCompile(`sha256:[0-9a-f]{64}`) |
| + tagPattern = regexp.MustCompile(`^[0-9A-Za-z][0-9A-Za-z._-]{0,63}$`) |
| ) |
| // networkCheckOutputs are the network root's outputs the 3.3 checks read. |
| @@ -77,8 +78,11 @@ func cmdTfvars(o *Ops, args []string) error { |
| } |
| vars["run"] = o.Run |
| if *host != "" { |
| - if !hostPattern.MatchString(*host) || strings.HasSuffix(*host, "amadan.net") { |
| - return failf("--instance-host %q is not an exact host name outside amadan.net", *host) |
| + // The controller's egress manifest takes exactly these names |
| + // (ctlegress.ExactHost): only amadan.net itself, the forge, is |
| + // refused; a name under it is a different host. |
| + if err := ctlegress.ExactHost(*host); err != nil { |
| + return failf("--instance-host: %v", err) |
| } |
| vars["egress_services"] = map[string]any{"instance": map[string]any{"host": *host, "port": 443, "class": "instance"}} |
| } |
diff --git a/internal/isolatedjob/awsjob/imageaws/imageaws.go b/internal/isolatedjob/awsjob/imageaws/imageaws.go| index ad8a9596..1af072c8 100644 |
| --- a/internal/isolatedjob/awsjob/imageaws/imageaws.go |
| +++ b/internal/isolatedjob/awsjob/imageaws/imageaws.go |
| @@ -71,12 +71,12 @@ func callErr(op string, err error) error { |
| // errPages is a paged read that would not end. |
| var errPages = errors.New("imageaws: more than 100 pages, or a page token repeated; stopped reading") |
| -func (p *Provider) CallerAccount(ctx context.Context) (string, error) { |
| +func (p *Provider) CallerIdentity(ctx context.Context) (hostedimage.Caller, error) { |
| out, err := p.sts.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{}) |
| if err != nil { |
| - return "", callErr("GetCallerIdentity", err) |
| + return hostedimage.Caller{}, callErr("GetCallerIdentity", err) |
| } |
| - return aws.ToString(out.Account), nil |
| + return hostedimage.Caller{Account: aws.ToString(out.Account), ARN: aws.ToString(out.Arn)}, nil |
| } |
| func (p *Provider) BaseVersions(ctx context.Context, baseARN string) ([]hostedimage.BaseVersion, error) { |
diff --git a/internal/isolatedjob/awsjob/imageaws/imageaws_test.go b/internal/isolatedjob/awsjob/imageaws/imageaws_test.go| index d725ce02..685d5780 100644 |
| --- a/internal/isolatedjob/awsjob/imageaws/imageaws_test.go |
| +++ b/internal/isolatedjob/awsjob/imageaws/imageaws_test.go |
| @@ -97,7 +97,7 @@ func healthy(account string) func(op string, in any) (any, error) { |
| return func(op string, in any) (any, error) { |
| switch op { |
| case "GetCallerIdentity": |
| - return &sts.GetCallerIdentityOutput{Account: aws.String(account)}, nil |
| + return &sts.GetCallerIdentityOutput{Account: aws.String(account), Arn: aws.String(callerIn(account, approvedRole))}, nil |
| case "ListManagedMicrovmImageVersions": |
| return &vms.ListManagedMicrovmImageVersionsOutput{Items: []types.ManagedMicrovmImageVersion{ |
| {ImageVersion: aws.String("3"), Status: types.ManagedMicrovmImageVersionStatusAvailable}, |
| @@ -186,7 +186,15 @@ func request(t *testing.T) hostedimage.PublishRequest { |
| } |
| } |
| -var target = hostedimage.Target{Account: acct, Region: region, Prefix: prefix} |
| +// approvedRole is the role and session the run bound; callerIn is its |
| +// STS ARN in account. |
| +const approvedRole = "OrganizationAccountAccessRole/equipo-hosted-bringup" |
| + |
| +func callerIn(account, role string) string { |
| + return "arn:aws:sts::" + account + ":assumed-role/" + role |
| +} |
| + |
| +var target = hostedimage.Target{Account: acct, Region: region, Prefix: prefix, Caller: callerIn(acct, approvedRole)} |
| // TestPublishRefusesOtherAccountThroughSDK: through the real clients, |
| // when STS names another account the only operation is |
| @@ -321,7 +329,27 @@ func TestReadsMapped(t *testing.T) { |
| if c, err := p.GetConnector(context.Background(), "arn:c"); err != nil || c.Name != prefix || c.State != "ACTIVE" { |
| t.Fatalf("GetConnector = %+v, %v", c, err) |
| } |
| - if a, err := p.CallerAccount(context.Background()); err != nil || a != acct { |
| - t.Fatalf("CallerAccount = %s, %v", a, err) |
| + if a, err := p.CallerIdentity(context.Background()); err != nil || a.Account != acct { |
| + t.Fatalf("CallerIdentity = %+v, %v", a, err) |
| + } |
| +} |
| + |
| +// TestPublishRefusesOtherRoleThroughSDK (Task 32 finding 4): through the |
| +// real clients, when STS names the right account but another role, the |
| +// only operation is GetCallerIdentity. |
| +func TestPublishRefusesOtherRoleThroughSDK(t *testing.T) { |
| + s := &stub{answer: func(op string, in any) (any, error) { |
| + if op == "GetCallerIdentity" { |
| + return &sts.GetCallerIdentityOutput{Account: aws.String(acct), Arn: aws.String(callerIn(acct, "AnotherRole/equipo-hosted-bringup"))}, nil |
| + } |
| + return healthy(acct)(op, in) |
| + }} |
| + _, err := hostedimage.Publish(context.Background(), provider(s), target, request(t)) |
| + var f *hostedimage.Failure |
| + if !errors.As(err, &f) || f.Exit != hostedimage.ExitGuard { |
| + t.Fatalf("Publish = %v, want a guard refusal", err) |
| + } |
| + if got := s.seen(); !slices.Equal(got, []string{"GetCallerIdentity"}) { |
| + t.Fatalf("operations %v, want only GetCallerIdentity", got) |
| } |
| } |