Make BASE_OWNER add or promote an owner at boot
Changing BASE_OWNER used to do nothing once Base had members. Now the address becomes an owner (added, or promoted if already a member); nobody is demoted or removed. Paul's Oficina identity is paul@tito.io, so he becomes Base's owner by setting BASE_OWNER.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+56
−8
internal/store/bootstrap.go+12 −7internal/store/store_test.go+44 −1
diff --git a/internal/store/bootstrap.go b/internal/store/bootstrap.go| index cf0e6d8..c7c67d5 100644 |
| --- a/internal/store/bootstrap.go |
| +++ b/internal/store/bootstrap.go |
| @@ -9,18 +9,23 @@ import ( |
| "amadan.net/rastrillo/rastrillo/assertion" |
| ) |
| -// Bootstrap seeds the owner when there are no members, and does nothing |
| -// otherwise. The insert and the emptiness check are one statement, so two |
| -// Bases booting together cannot both seed. |
| +// Bootstrap makes ownerEmail (BASE_OWNER) an owner: it adds the address as |
| +// an owner when no member has it, and promotes the member who has it. It |
| +// never demotes or removes anyone, so changing BASE_OWNER adds an owner and |
| +// leaves the previous one in place. Both statements are idempotent, so two |
| +// Bases booting together end in the same state. |
| func Bootstrap(ctx context.Context, db *sql.DB, ownerEmail string) error { |
| + email := strings.ToLower(strings.TrimSpace(ownerEmail)) |
| id, err := assertion.RandomID() |
| if err != nil { |
| return err |
| } |
| - now := time.Now().UTC() |
| - _, err = db.ExecContext(ctx, |
| + if _, err := db.ExecContext(ctx, |
| `INSERT INTO members (id, email, role, invited_at) |
| - SELECT ?, ?, 'owner', ? WHERE NOT EXISTS (SELECT 1 FROM members)`, |
| - id, strings.ToLower(strings.TrimSpace(ownerEmail)), now) |
| + SELECT ?, ?, 'owner', ? WHERE NOT EXISTS (SELECT 1 FROM members WHERE email = ?)`, |
| + id, email, time.Now().UTC(), email); err != nil { |
| + return err |
| + } |
| + _, err = db.ExecContext(ctx, `UPDATE members SET role = 'owner' WHERE email = ? AND role <> 'owner'`, email) |
| return err |
| } |
diff --git a/internal/store/store_test.go b/internal/store/store_test.go| index cf5b569..983c714 100644 |
| --- a/internal/store/store_test.go |
| +++ b/internal/store/store_test.go |
| @@ -27,7 +27,7 @@ func TestBootstrapSeedsOwnerOnce(t *testing.T) { |
| if err := Bootstrap(ctx, db, "owner@example.com"); err != nil { |
| t.Fatal(err) |
| } |
| - if err := Bootstrap(ctx, db, "other@example.com"); err != nil { |
| + if err := Bootstrap(ctx, db, "Owner@Example.com "); err != nil { |
| t.Fatal(err) |
| } |
| ms, err := Members(ctx, db) |
| @@ -42,6 +42,49 @@ func TestBootstrapSeedsOwnerOnce(t *testing.T) { |
| } |
| } |
| +// BASE_OWNER is the operator's word on who owns Base: a new address is |
| +// added as an owner, an existing member is promoted, and nobody is demoted |
| +// or removed. |
| +func TestBootstrapMakesANewOwnerAddressAnOwner(t *testing.T) { |
| + ctx, db := context.Background(), open(t) |
| + if err := Bootstrap(ctx, db, "first@example.com"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, _, err := Invite(ctx, db, "member@example.com"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := Bootstrap(ctx, db, "member@example.com"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := Bootstrap(ctx, db, "new@example.com"); err != nil { |
| + t.Fatal(err) |
| + } |
| + ms, err := Members(ctx, db) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + roles := map[string]string{} |
| + for _, m := range ms { |
| + roles[m.Email] = m.Role |
| + } |
| + want := map[string]string{"first@example.com": "owner", "member@example.com": "owner", "new@example.com": "owner"} |
| + if len(roles) != len(want) { |
| + t.Fatalf("members = %+v", ms) |
| + } |
| + for email, role := range want { |
| + if roles[email] != role { |
| + t.Errorf("%s role = %q, want %q", email, roles[email], role) |
| + } |
| + } |
| + for _, m := range ms { |
| + if m.Email == "new@example.com" { |
| + if n, err := MemberNumber(ctx, db, m.ID); err != nil || n == 0 { |
| + t.Errorf("new owner has no member number: n=%d err=%v", n, err) |
| + } |
| + } |
| + } |
| +} |
| + |
| func TestClaimInstallOnlyOnce(t *testing.T) { |
| ctx, db := context.Background(), open(t) |
| first, err := ClaimInstall(ctx, db, "scratchpad", "acme-scratchpad.apps.example.com") |