carlos / platform Public

console: moving an app's record refuses over policy state (#328, #329)

The controller's ruling on the final fix wave's MoveApp concern: the
move door refuses when the app's name has policy state (a policy object
or a revision row) in the source or the destination account, checked
before the confirm card and again under both names' policy locks
(Store.MoveAppChecked). A moved app bypasses create's clear, so it
would otherwise take up an earlier app's policy at the destination, or
leave its own behind. New strings IP1-49, IP1-58, IP1-59 in the copy
deck.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 4317d894f1f5af0d28ba4f4ae57dc239487de206 parent 8c6904e
4 files changed, +203 −7
  • docs/superpowers/notes/copy-instance-policy-plan1.md +5 −2
  • internal/console/apppolicy_names_test.go +121 −0
  • internal/console/appsettings.go +65 −5
  • internal/console/store.go +12 −0

CI failed — run details

diff --git a/docs/superpowers/notes/copy-instance-policy-plan1.md b/docs/superpowers/notes/copy-instance-policy-plan1.md
index 8f256014f..44c98749e 100644
--- a/docs/superpowers/notes/copy-instance-policy-plan1.md
+++ b/docs/superpowers/notes/copy-instance-policy-plan1.md
@@ -129,9 +129,9 @@ size -> large (cli)`.
The lease refusals (IP1-15 to IP1-19) also answer a create:
`apicli.go:487`, `apitemporary.go:223`, and the flash at `server.go:4562`.
-## 4a. App rename (final review finding 3, `internal/console/appsettings.go`)
+## 4a. App rename and move (final review finding 3 and the controller's move ruling, `internal/console/appsettings.go`)
-A rename is refused when either name has had a policy: an object at its
+A rename, or a move of an app's record to another account, is refused when either name has had a policy: an object at its
key, or a revision row in the console's table. The row outlives a cleared
policy, so these refusals do not tell the person to clear anything.
@@ -140,6 +140,9 @@ policy, so these refusals do not tell the person to clear anything.
| IP1-46 | internal/console/appsettings.go:919 `renamePolicyFromMsg` | This app has had an instance policy set by an operator, so it cannot be renamed. Create a new app instead. | the rename form's flash (app settings, General) | 0 |
| IP1-47 | internal/console/appsettings.go:920 `renamePolicyToMsg` | The name %s has had an instance policy set by an operator, so no app can be renamed to it. Choose another name. | the same flash; `%s` is the new name | 0 |
| IP1-48 | internal/console/appsettings.go:921 `renamePolicyCheckMsg` | Could not check the instance policies, so nothing was renamed. Try again. | the same flash, when the bucket or the table cannot be read | 0 |
+| IP1-49 | internal/console/appsettings.go:927 `movePolicyFromMsg` | This app has had an instance policy set by an operator, so its record cannot be moved to another account. Ask an operator to move it. | the "Move to <account>" repair's flash (app settings, General) | 0 |
+| IP1-58 | internal/console/appsettings.go:928 `movePolicyToMsg` | An earlier app named %s in %s had an instance policy set by an operator, so this app cannot be moved there. Ask an operator to move it. | the same flash; the app's name, then the destination account's name | 0 |
+| IP1-59 | internal/console/appsettings.go:929 `movePolicyCheckMsg` | Could not check the instance policies, so nothing was moved. Try again. | the same flash, when the bucket or the table cannot be read | 0 |
## 5. The CLI (Task 12)
diff --git a/internal/console/apppolicy_names_test.go b/internal/console/apppolicy_names_test.go
index d22e45638..96ab9ed5c 100644
--- a/internal/console/apppolicy_names_test.go
+++ b/internal/console/apppolicy_names_test.go
@@ -448,3 +448,124 @@ func (fx *policyFx) hasApp(t *testing.T, app string) bool {
}
return has
}
+
+// ---- the controller's ruling: MoveApp refuses over policy state -----------
+
+const moveDestID = 4242
+
+// moveWorld is the policy fixture with a second account, Second (cdef), that
+// kass's routes already deploy from: the state the move door repairs.
+func moveWorld(t *testing.T, code string) (*policyFx, *http.Client) {
+ t.Helper()
+ fx := newPolicyFx(t, code)
+ if err := fx.st.CreateAccount(moveDestID, "cdef", "Second", "paul@keymail.dev", time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ stampKassRoutes(t, fx.consoleFixture, "cdef")
+ return fx, signInPaul(t, fx.consoleFixture)
+}
+
+// moveKass posts a confirmed move of kass through the real door, walking the
+// passkey when it is asked for, and returns where it landed and that page.
+func moveKass(t *testing.T, fx *policyFx, c *http.Client) (string, string) {
+ t.Helper()
+ st, loc, body := postGated(t, fx.consoleFixture, c, fx.u("/apps/kass/move"),
+ url.Values{"csrf": {sessionToken(t, fx.consoleFixture, c)}, "confirmed": {"1"}})
+ if st != http.StatusSeeOther {
+ t.Fatalf("move = %d:\n%s", st, body)
+ }
+ _, _, page := get(t, c, fx.ts.URL+loc)
+ return loc, page
+}
+
+func (fx *policyFx) kassIn(t *testing.T, accountID int64) bool {
+ t.Helper()
+ has, err := fx.st.HasAppIn(accountID, "kass")
+ if err != nil {
+ t.Fatal(err)
+ }
+ return has
+}
+
+// Through the real move door: a move is refused when the app's name has
+// policy state in either account, and nothing moves. Fails without
+// movePolicyRefusal: the moved app takes up the earlier app's policy at the
+// destination, or leaves its own behind.
+func TestMoveRefusedWhileEitherNameHasAnAppPolicy(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+ toMsg := strings.Replace(strings.Replace(movePolicyToMsg, "%s", "kass", 1), "%s", "Second", 1)
+ for _, tc := range []struct {
+ name string
+ seed func(t *testing.T, fx *policyFx)
+ want string // the flash; "" for a move that goes ahead
+ }{
+ {"the destination name has an earlier app's policy object", func(t *testing.T, fx *policyFx) {
+ putAtKey(t, fx.rel, "cdef", "kass", signPolicy(t, fx.consoleFixture, apppolicy.Policy{V: 1, Account: "cdef",
+ App: "kass", Revision: 1, SignedAt: time.Now().UTC(), DefaultSize: "xlarge", EgressAllow: []string{"10.0.5.20"}}))
+ }, toMsg},
+ {"the destination name has a revision row only", func(t *testing.T, fx *policyFx) {
+ wire := signPolicy(t, fx.consoleFixture, apppolicy.Policy{V: 1, Account: "cdef", App: "kass", Revision: 2,
+ SignedAt: time.Now().UTC(), DefaultSize: "large"})
+ if err := fx.st.RecordAppPolicyContext(ctx, moveDestID, "kass", 2, wire); err != nil {
+ t.Fatal(err)
+ }
+ }, toMsg},
+ {"the app has a policy", func(t *testing.T, fx *policyFx) {
+ fx.mustPut(t, "kass", firstGrant, policyCreate)
+ }, movePolicyFromMsg},
+ {"control: neither name has one", func(*testing.T, *policyFx) {}, ""},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+ fx, c := moveWorld(t, "code-move-policy-"+claimSafeName(tc.name))
+ tc.seed(t, fx)
+ srcObj := policyObject(t, fx.consoleFixture, "kass")
+ loc, page := moveKass(t, fx, c)
+ if tc.want == "" {
+ if loc != "/a/cdef/apps/kass" || !fx.kassIn(t, moveDestID) || fx.kassIn(t, OpsAccountID) {
+ t.Fatalf("move -> %q; want it done:\n%s", loc, page)
+ }
+ return
+ }
+ if loc != fx.path("/apps/kass/settings") || !strings.Contains(html.UnescapeString(page), tc.want) {
+ t.Fatalf("move -> %q, want the flash %q:\n%s", loc, tc.want, page)
+ }
+ if !fx.kassIn(t, OpsAccountID) || fx.kassIn(t, moveDestID) {
+ t.Fatal("the refused move moved the record")
+ }
+ if got := policyObject(t, fx.consoleFixture, "kass"); !bytes.Equal(got, srcObj) {
+ t.Fatalf("kass's policy object changed: %s", got)
+ }
+ })
+ }
+}
+
+// A grant that lands after the move door's first check, while the move waits
+// for the names' policy locks, still refuses the move: the check runs again
+// under the locks. Fails without the check inside MoveAppChecked.
+func TestMoveRechecksThePolicyUnderTheLocks(t *testing.T) {
+ t.Parallel()
+ fx, c := moveWorld(t, "code-move-policy-recheck")
+ var once sync.Once
+ fx.st.setAppNameWaiting(func(_ int64, app string) {
+ if app == "kass" {
+ once.Do(func() {
+ st, _, out := policyReq(t, fx.consoleFixture, http.MethodPut, "kass", fx.owner, firstGrant, policyCreate)
+ if st != http.StatusOK {
+ t.Errorf("grant during the move = %d %s", st, out)
+ }
+ })
+ }
+ })
+ loc, page := moveKass(t, fx, c)
+ if loc != fx.path("/apps/kass/settings") || !strings.Contains(html.UnescapeString(page), movePolicyFromMsg) {
+ t.Fatalf("move -> %q, want the flash %q:\n%s", loc, movePolicyFromMsg, page)
+ }
+ if !fx.kassIn(t, OpsAccountID) || fx.kassIn(t, moveDestID) {
+ t.Fatal("the move went ahead over the grant")
+ }
+ if _, _, etag := fx.stored(t, "kass"); etag == "" {
+ t.Fatal("the grant is gone from kass's key")
+ }
+}
diff --git a/internal/console/appsettings.go b/internal/console/appsettings.go
index aa3ac57c1..d292d4ad9 100644
--- a/internal/console/appsettings.go
+++ b/internal/console/appsettings.go
@@ -921,6 +921,54 @@ const (
renamePolicyCheckMsg = "Could not check the instance policies, so nothing was renamed. Try again."
)
+// Move refusals over an app policy (#328, #329; the controller's ruling on
+// the final fix wave's MoveApp concern).
+const (
+ movePolicyFromMsg = "This app has had an instance policy set by an operator, so its record cannot be moved to another account. Ask an operator to move it."
+ movePolicyToMsg = "An earlier app named %s in %s had an instance policy set by an operator, so this app cannot be moved there. Ask an operator to move it."
+ movePolicyCheckMsg = "Could not check the instance policies, so nothing was moved. Try again."
+)
+
+// movePolicyRefused carries a move refusal out of MoveAppChecked's check.
+type movePolicyRefused string
+
+func (e movePolicyRefused) Error() string { return string(e) }
+
+// appPolicyState reports whether the name has policy state: a policy object
+// at its key, or a revision row in this console's table.
+func (s *Server) appPolicyState(ctx context.Context, acct *Account, name string) (bool, error) {
+ var present bool
+ var err error
+ if s.cfg.Releases != nil { // no bucket holds no policy object
+ present, err = s.appPolicyPresent(ctx, acct, name)
+ }
+ if err == nil && !present {
+ present, err = s.store.HasAppPolicyRevisionContext(ctx, acct.ID, name)
+ }
+ return present, err
+}
+
+// movePolicyRefusal is the move's refusal when the app's name has policy
+// state in either account, for the reason renamePolicyRefusal gives: a
+// policy stays at its account's key, so the move would leave the app's
+// behind, and the moved app, which no create clears for, would take up an
+// earlier app's at the destination. "" means the move may go ahead.
+func (s *Server) movePolicyRefusal(ctx context.Context, from, to *Account, app string) string {
+ for _, acct := range []*Account{from, to} {
+ present, err := s.appPolicyState(ctx, acct, app)
+ switch {
+ case err != nil:
+ log.Printf("console: move %s/%s -> %s: policy state in %s: %v", from.Sqid, app, to.Sqid, acct.Sqid, err)
+ return movePolicyCheckMsg
+ case present && acct == from:
+ return movePolicyFromMsg
+ case present:
+ return fmt.Sprintf(movePolicyToMsg, app, to.Name)
+ }
+ }
+ return ""
+}
+
// renamePolicyRefusal is the rename's refusal when either name has policy
// state: a policy object at its key, or a revision row in this console's
// table. A policy lives at its name's key, signed for that name, and its
@@ -931,10 +979,7 @@ const (
// handles it. A failed check refuses ("" means the rename may go ahead).
func (s *Server) renamePolicyRefusal(ctx context.Context, acct *Account, from, to string) string {
for _, name := range []string{from, to} {
- present, err := s.appPolicyPresent(ctx, acct, name)
- if err == nil && !present {
- present, err = s.store.HasAppPolicyRevisionContext(ctx, acct.ID, name)
- }
+ present, err := s.appPolicyState(ctx, acct, name)
switch {
case err != nil:
log.Printf("console: rename %s/%s -> %s: policy state of %s: %v", acct.Sqid, from, to, name, err)
@@ -1438,6 +1483,12 @@ func (s *Server) handleAppMoveAccount(w http.ResponseWriter, r *http.Request) {
back("err", "Cannot move "+app+" to "+dest.Name+": "+apexHeldSentence(s.apexHost(dest.Sqid, app), hold)+".")
return
}
+ // Neither name may carry an app policy (#328, #329), checked here as
+ // advice and again under both names' policy locks in the move itself.
+ if msg := s.movePolicyRefusal(r.Context(), acct, dest, app); msg != "" {
+ back("err", msg)
+ return
+ }
if drole, err := s.store.Role(dest.ID, addr); err != nil || drole == "" {
back("err", "You are not a member of "+dest.Name+" ("+target+"), so moving the record there would strand the app for you. Ask an owner of that account, or an operator, to finish the move.")
return
@@ -1473,7 +1524,16 @@ func (s *Server) handleAppMoveAccount(w http.ResponseWriter, r *http.Request) {
if !s.requireFreshAuth(w, r, addr, raw) {
return
}
- if err := s.store.MoveApp(app, acct.ID, dest.ID); err != nil {
+ var policyRefused movePolicyRefused
+ if err := s.store.MoveAppChecked(app, acct.ID, dest.ID, func() error {
+ if msg := s.movePolicyRefusal(r.Context(), acct, dest, app); msg != "" {
+ return movePolicyRefused(msg)
+ }
+ return nil
+ }); errors.As(err, &policyRefused) {
+ back("err", string(policyRefused))
+ return
+ } else if err != nil {
log.Printf("console: move %s %s -> %s: %v", app, acct.Sqid, target, err)
back("err", "Could not move the record — nothing changed. "+moveAdvice(err, dest.Name))
return
diff --git a/internal/console/store.go b/internal/console/store.go
index 658cf0810..6cbb6cef1 100644
--- a/internal/console/store.go
+++ b/internal/console/store.go
@@ -1971,7 +1971,19 @@ func (s *Store) AppAccounts(app string) ([]int64, error) {
// (apppolicy_names.go) and counts both as freed: the source's name no longer
// names the app, and the destination's names a different one (#328, #329).
func (s *Store) MoveApp(app string, from, to int64) error {
+ return s.MoveAppChecked(app, from, to, nil)
+}
+
+// MoveAppChecked is MoveApp with check run first, under both names' policy
+// locks: a check error refuses the move with nothing written. The move door
+// refuses there when either name has policy state (#328, #329).
+func (s *Store) MoveAppChecked(app string, from, to int64, check func() error) error {
defer s.lockAppNames(appNameKey{from, app}, appNameKey{to, app})()
+ if check != nil {
+ if err := check(); err != nil {
+ return err
+ }
+ }
if err := s.moveApp(app, from, to); err != nil {
return err
}