🤖 roster
Use roster to keep an instance's member list in step with the root app. The root sends a signed snapshot of the whole roster, and the instance applies it to its own tables. A snapshot says who belongs and whether the instance is active. It carries no credentials, only the IDs of the agent grants each member holds, so a member still signs in through handoff.
amadan.net/rastrillo/rastrillo/roster
Mount it
// The platform host, even when the instance serves on a custom domain.
platformHost := "https://" + carlos.Host()
verifier, err := roster.NewVerifier(setup.Root, platformHost, setup.Keys)
// ...
receiver, err := roster.New(roster.Config{
Verifier: verifier,
Store: store,
DB: db,
Roles: []string{"owner", "member"},
// stop cancels the context whose Done() is rastrillo.Options.Stop.
OnOrigin: func(previous, next string) { stop() },
})
// ...
mux.Handle(roster.Path, receiver)
Exempt the path from CSRF with csrf.Exempt(handoff.EnterPath, roster.Path). The root's POST is cross-site, and its signature protects it. Merge roster.Schema and handoff.Schema into BootSchema.
Receiver
const Path = "/_carlos/roster"
func New(config Config) (*Receiver, error)
func (r *Receiver) ServeHTTP(w http.ResponseWriter, req *http.Request)
func (r *Receiver) Suspended(ctx context.Context) (bool, error)
func (r *Receiver) Origin(ctx context.Context) (string, error)
func StoredOrigin(ctx context.Context, db *sql.DB) (string, error)
func (r *Receiver) Plan(ctx context.Context) (Plan, error)
func StoredPlan(ctx context.Context, db *sql.DB) (Plan, error)
The receiver takes POST only, with the token as the body. It answers 204 when the snapshot is applied. It answers only on the verifier's audience origin, which is the instance's own host even for a hub tenant. On any other host it answers 404 without reading the body.
| Status | Meaning |
|---|---|
| 204 | Applied. |
| 403 | The token did not verify. |
| 404 | The request came in on a host other than the audience origin's. |
| 409 | An older snapshot than one already applied. |
| 413 | The token is over 1 MiB. |
| 422 | A member has a role not in Config.Roles. |
| 503 | Another apply is running, or kept taking the lease from this one. Retry-After says when to try again. |
| 500 | The store or the database failed. |
Each snapshot contains the whole roster, so the next snapshot recovers any one the instance missed. The receiver uses Put to add or change members. It uses Remove for members missing from the snapshot. It records the snapshot's sequence last, so a failed apply is applied again on the root's retry.
A time-limited database lock allows only one apply at a time, even when several processes share the database. A busy lease answers 503 with Retry-After. Config.Roles lists the app's roles, highest first.
Suspended reports whether the last applied snapshot suspended the instance. Before any snapshot it is false. Pass it as handoff.Config.Suspended.
Keep emails out of the receiver's row
The receiver keeps a copy of the last applied snapshot in its own row, roster_state, so it can repair the roster after a lost lease (see Store). By default the copy includes each member's email and name.
To keep emails and names out of the receiver's row, set Config.RecordRolesOnly:
receiver, err := roster.New(roster.Config{
// ...
RecordRolesOnly: true,
})
The row then holds only each member's subject, role, and grant IDs. Your Store still gets every field. To avoid storing the email, drop it in your Store too. Set the option in every process that shares the database: a receiver without it writes emails into the row again.
Repairing from such a row restores the member list, each member's role, and each member's grant IDs, so revoked grants stay revoked. It keeps the email and name your Store already has. A member added back by the repair has no email or name until the next snapshot. Any email or name changed by the stale apply also remains until that snapshot. The repaired row contains no emails, even if this receiver does not set RecordRolesOnly.
If the row already holds emails, it is rewritten without them the next time a snapshot is applied. To rewrite the row immediately, have the root send the current snapshot again. This removes emails from the row. The old bytes can remain in the database file, its write-ahead log and any existing backups.
Custom origin
A snapshot can carry the account's custom origin, such as https://blog.example.org. The receiver stores it with the sequence. Origin returns the stored origin, or "" when the instance serves on its own host. StoredOrigin reads the same value from the database before any receiver exists, which is what boot needs: see instance.ServingOrigin.
The app fixes its origin at boot, so a change needs a restart. Set Config.OnOrigin, which receives the previous and next origin. The receiver calls it after it has applied a snapshot that changes the origin and sent the 204. A snapshot that repeats the origin does not call it, and neither does the root's retry. OnOrigin runs inside the roster request. From it, cancel the context whose Done() you passed as rastrillo.Options.Stop, and Serve shuts down gracefully. Do not wait there for the server to stop: the shutdown waits for this request. instance shows the wiring.
Make the verifier's audience the instance's platform host, https:// and carlos.Host(), not the custom origin. The root always delivers to the platform host, so a custom domain that stops working cannot cut the root off.
When several processes share the database, only the one that applied the snapshot calls OnOrigin.
Plan
type Plan struct {
Name string // "" when the root named none
LapsedAt time.Time // zero when it never lapsed or holds a paid plan
Until time.Time // zero when the plan has no deadline
}
func (p Plan) Effective(now time.Time) Plan
A snapshot can include the account's plan and the time it last stopped holding a paid plan. The receiver stores them with the sequence. A newer snapshot with no plan clears the stored plan. Plan returns the current stored plan and lapse time. StoredPlan reads the same row without a receiver, for a command or at boot. Before any snapshot, both return the zero Plan. Treat that as your free plan.
Until is when the plan stops granting, unless a newer snapshot says otherwise. It can be the end of a comp, a paid period or a grace deadline. Enforce Plan.Effective(now), not the stored Plan. At or after Until, Effective returns no plan, with LapsedAt set to Until or to an earlier LapsedAt. Before Until, or when Until is zero, it returns the plan unchanged. A site still stops granting on time if its next roster never arrives.
To act on a plan change, set Config.OnPlan to a func(). Read Receiver.Plan for the current plan. The receiver calls OnPlan after sending the 204, in the process that applied the snapshot, when the snapshot changes the stored plan name, lapse time or deadline. A retry, an older snapshot, or a snapshot with the same plan does not call it. OnPlan runs before OnOrigin. Keep it short.
OnPlan passes no plan because two calls can run out of order: a second snapshot can be applied while the first one's call is still running. Receiver.Plan always returns the plan stored last, so read it in each call.
A snapshot with the same sequence as the stored one still updates the members, but keeps the stored plan and origin and calls neither hook.
Name is the name the root gives the plan. Your app decides what each name allows and how to use LapsedAt.
Store
type Store interface {
Members(ctx context.Context) ([]Member, error)
Put(ctx context.Context, member Member) error
Remove(ctx context.Context, subject string) error
}
The app's Store keeps the members in its own tables. Every method should honour ctx. The receiver gives an apply 25 seconds through ctx. A method that ignores ctx can keep writing after the lease expires.
So writes from a stale apply can land on top of a newer roster. The receiver repairs them before it answers. After its Store writes, it checks that it still holds the lease. If another process took the lease meanwhile, the receiver takes a new lease and applies the newest recorded roster again, diffing it against Members. Then it answers 409 for its own, older snapshot. If the lease stays busy, or is lost three times in a row, it answers 503, and the root's retry finishes the repair.
When a Store method fails, the receiver answers 500 and logs only which method failed: store put failed, store remove failed or store members failed. It leaves out the error's text, which could hold a member's email or name.
Snapshots
type State string
const (
Active State = "active"
Suspended State = "suspended"
)
type Member struct {
Subject, Email, Name, Role string
Grants []string `json:"grants,omitempty"`
}
type Snapshot struct {
Version, Sequence, IssuedAt, ExpiresAt int64
Issuer, Audience, KeyID string
State State
Members []Member
Origin string
Plan string `json:"plan,omitempty"`
LapsedAt int64 `json:"lapsed_at,omitzero"`
PlanUntil int64 `json:"plan_until,omitzero"`
}
func Sign(key *crypto.Keypair, snapshot Snapshot) (string, error)
func NewVerifier(issuer, audience string, keys map[string][]byte) (*Verifier, error)
func (v *Verifier) Verify(token string, now time.Time) (Snapshot, error)
Sign is for the root, and for tests. The snapshot is signed under the context rastrillo/roster/v1, so an assertion signed by the same key cannot be used as a roster, nor the reverse. Version is 1. A snapshot lives at most 300 seconds, and a token is at most 1 MiB. Sequence starts at 1 and rises with every snapshot for one instance. An empty Members list is valid.
Origin is empty, or https:// and a lower-case host with no port, path, query or user info. It cannot be the issuer's origin. Sign and Verify refuse anything else with ErrClaims. An empty Origin is left out of the token, so a snapshot without one has the same bytes as before. Verify refuses fields it does not know, so a receiver older than this field refuses a snapshot that sets it. Upgrade every instance before the root sends an origin.
Plan is empty, or a name of 1 to 64 lower-case letters, digits and hyphens, with no hyphen first or last, such as pro or team-2. LapsedAt is Unix seconds, never negative, and at most 300 seconds after IssuedAt. 0 means the account never lapsed, or holds a paid plan again. PlanUntil is Unix seconds. It is never negative and can be any time in the future. 0 means no deadline. A snapshot with an empty Plan must have PlanUntil 0. Sign and Verify refuse anything else with ErrClaims. Zero values for Plan, LapsedAt and PlanUntil are left out of the signed token, so old snapshots keep their bytes. A receiver older than this release refuses a snapshot that sets any of these fields. Upgrade every instance before the root sends a plan.
Verify checks the signature, issuer, audience and lifetime. It does not compare sequences; the receiver does.
Grants
func (r *Receiver) GrantActive(ctx context.Context, subject, grant string) (bool, error)
Member.Grants lists the IDs of the agent grants a member holds. A grant token is valid only while its ID is in the latest snapshot, so ending a grant at the root cuts the agent off when the site applies the next snapshot. See granttoken. Each ID is at most 64 characters, each member can hold at most 64 grants, and no ID can appear twice in one snapshot, even under two members. GrantActive reports whether the latest applied snapshot lists the grant under that subject on an active instance. It is false before any snapshot and while the instance is suspended.
The Store keeps no grants. The receiver keeps them in its own row, and passes them to Put only when it has to write the member for another reason. A snapshot that changes only a member's grants does not call Store.Put.
Member holds a slice, so it is no longer comparable with ==. Compare the fields you need.
A snapshot with no grants signs the same bytes as before. A receiver older than v0.47.0 refuses a snapshot that holds grants, and the root does not retry a refused snapshot. Upgrade in this order:
- Release Rastrillo v0.47.0.
- Deploy every hosted site on it.
- Only then let the root put grant IDs in snapshots.
Errors
Errors carry no submitted values, so they can be logged as they are.
ErrMalformed: the token is not shaped like a snapshot, or is too large.ErrVersion: the version is not1.ErrUnknownKey: the key id is not one the verifier trusts.ErrSignature: the signature does not match.ErrIssuer,ErrAudience: the snapshot is from or for someone else.ErrLifetime,ErrExpired: the lifetime is over 300 seconds, or the snapshot is from the future or has expired.ErrClaims: a field is missing, duplicated or out of range.ErrConfig:NewVerifierorNewwas given a configuration it cannot use.ErrSigning:Signwas given a key it cannot sign with.ErrUnknownRole: a member has a role the app does not define. The receiver answers 422.
For testing, see handofftest.
Read this page as markdown — exact, unstyled, and cheap for an agent to fetch.