Documentation
¶
Overview ¶
Package hostjoin is the hub's half of `muster host join <url>` (docs/design/three-components.md, PR 4): a machine that wants to join asks the hub for a short code, its person types the code on Account → Hosts in a browser signed in as an owner, and the hub hands that machine a mirror token over the connection it is waiting on.
The shape is the one "Sign in with the muster app" built for phones (docs/design/qr-sign-in.md), run for a host:
- Create: the machine starts an ATTEMPT. Two secrets come back: the CODE (short, typed by a person: internal/attemptcode's XXXX-XXXX-XXXX, 60 bits) and the BINDING (32 random bytes, kept by the waiting process and never shown). Only their sha256s are kept.
- Lookup: the approving browser shows the attempt's details by its code.
- Approve / Deny: the approving browser decides it. Approve runs the mint (the caller's, under the approver's own principal) and keeps the token on the attempt until the machine collects it.
- Wait: the machine waits by its BINDING. An approved attempt hands its token to a wait holding that binding: the same token to each such wait until the attempt's end (TTL plus CollectGrace), when it is forgotten.
- Cancel: the machine gave up (Ctrl-C), so the code stops working at once.
🔴 THE CODE ALONE COLLECTS NOTHING. A code read off a screen, a log or a photo lets its reader ask for nothing: only the waiting process's binding collects the token (the QR flow's F1, an unbound session).
🔴 SINGLE USE, SHORT LIFE. An attempt is decided once (pending -> approved or denied, under the store's lock), an approval mints one token, and every step checks TTL against the store's clock: a code past it is unknown.
🔴 A HAND-OVER IS NOT A DELIVERY. The hub answers a wait and cannot know that the machine read the answer: the machine's -wait can run out as the owner approves (it closed its connection, but the server has not noticed yet, so the wait's context is still live), or a proxy can keep the hub's side of a connection open after its client left. Were the token forgotten at its first hand-over, the machine's next wait would read "expired", and the person would start again over a token minted for nobody. So the binding's holder collects the SAME token again until the attempt's end: no second mint, and nothing for anyone without the binding, the one secret that could collect it the first time.
🔴 COMPARED IN CONSTANT TIME. A code or binding is hashed, and the hash is compared with EVERY live attempt's in constant time (no early exit), so the time a look-up takes says nothing about which bytes matched.
🔴 IN MEMORY, ON PURPOSE. An attempt lives minutes, is useless after a restart (its machine waits on a connection to THIS process), and the server runs one replica (the chart pins it). Nothing here needs a table: what must outlast the process — the token itself and the audit row — is written by the caller, to the database, when the approval mints it.
Index ¶
- Constants
- Variables
- func BindingShapeOK(s string) bool
- func Clip(s string, max int) string
- func Fingerprint(code string) string
- type Attempt
- type Mint
- type Minted
- type Result
- type State
- type Store
- func (s *Store) Approve(code string, by int64, host string, mint Mint) (Attempt, error)
- func (s *Store) Cancel(binding string) (a Attempt, revoked bool, err error)
- func (s *Store) Create(ip, agent, host string) (code, binding string, a Attempt, err error)
- func (s *Store) Deny(code string) (Attempt, error)
- func (s *Store) Known(binding string) bool
- func (s *Store) Live() int
- func (s *Store) Lookup(code string) (Attempt, error)
- func (s *Store) SetOnAbandon(f func(Attempt))
- func (s *Store) Sweep()
- func (s *Store) Wait(ctx context.Context, binding string, hold time.Duration) (Result, Attempt, error)
Constants ¶
const ( // TTL is how long an attempt lives, from its creation: a person has this // long to open the hub, sign in again if asked, and approve. Longer than // the phone flow's two minutes because the person moves to ANOTHER // machine's browser, and may have to confirm a passkey there first. TTL = 10 * time.Minute // BindingPrefix starts every binding, so a log line or a support paste // that holds one is recognisable as one. BindingPrefix = "mhj_" // MaxLive bounds the attempts held at once (pending or approved). Past // it Create refuses (ErrFull): an unauthenticated route cannot grow the // hub's memory without bound. MaxLive = 100 // MaxLivePerAddr bounds the attempts held at once for one requesting // address (Create refuses past it: ErrAddrFull), so one address cannot // hold the whole store and keep every other machine from joining. MaxLivePerAddr = 3 // MaxAgentLen, MaxIPLen and MaxHostLen bound the details kept for the // approver's card. MaxAgentLen = 120 MaxIPLen = 64 MaxHostLen = 64 )
const CollectGrace = 2 * time.Minute
CollectGrace is how long past TTL an APPROVED attempt's token may still be collected by its waiting machine: an approval landing in the code's last seconds is not lost to a wait already on its way. Past it the token is revoked (Minted.Revoke), never left live and unused.
Variables ¶
var ( // ErrNotFound: no live, undecided attempt for that code (unknown, // expired, decided, cancelled, collected) or no live attempt for that // binding. One error for all of them. ErrNotFound = errors.New("hostjoin: no such live join attempt") // ErrFull: MaxLive attempts are live; try again when some expire. ErrFull = errors.New("hostjoin: too many join attempts are waiting") // ErrAddrFull: MaxLivePerAddr attempts from this address are live. ErrAddrFull = errors.New("hostjoin: too many join attempts are waiting from this address") // ErrNoApprover: Approve with no approver. ErrNoApprover = errors.New("hostjoin: an approval needs the approver") )
Functions ¶
func BindingShapeOK ¶
BindingShapeOK reports whether s is shaped like a minted binding.
func Clip ¶
Clip bounds a detail to max runes, dropping invalid UTF-8 and control characters: every detail is the machine's own claim, shown on a page.
func Fingerprint ¶
Fingerprint is the fingerprint a typed code would carry ("" for a malformed one): what a refused look-up is logged and audited by.
Types ¶
type Attempt ¶
type Attempt struct {
ID int64
// Host is the name the machine proposed (its hostname, or -host): the
// approver confirms or changes it.
Host string
// IP and Agent are the requesting connection's address and User-Agent:
// the machine's own claims about itself, shown so the approver can tell
// their own machine from a stranger's.
IP, Agent string
Created time.Time
Expires time.Time
State State
// Fingerprint is attemptcode.Fingerprint of the code's hash: what a log
// line or an audit row names the attempt by, never the code.
Fingerprint string
// ApprovedHost, ApprovedBy and TokenID are set by an approval.
ApprovedHost string
ApprovedBy int64
TokenID int64
}
Attempt is one attempt's details: what the approver's card shows and the audit row records. It never carries the code, the binding or the token.
type Mint ¶
Mint mints the token an approval hands over, for host: the caller's, run under the approver's own principal so the token is the approver's.
type Minted ¶
Minted is what an approval's mint produced: the token, its id, and how to revoke it should the machine never collect it.
type Result ¶
type Result struct {
State State
// Host and Token: only with StateApproved. Undelivered is run by the
// caller when it could not hand the token on: the token stays for the
// machine's next wait, and is revoked at the attempt's end unless a wait
// was handed it meanwhile.
Host string
Token string
Undelivered func()
}
Result is what Wait hands the waiting machine.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store holds the live attempts. The zero value is not ready: use New.
func (*Store) Approve ¶
Approve decides the attempt code names: by approves it, under the name host, and mint mints the token the machine will collect. The mint runs OUTSIDE the store's lock (it is a database write); meanwhile the attempt is claimed, so a second approval, a denial or a cancel meets ErrNotFound. A failed mint puts the attempt back to pending.
func (*Store) Cancel ¶
Cancel forgets the attempt binding names, whatever its state: its machine gave up. A token an approval minted that no wait was handed is revoked with it (revoked; the returned attempt's TokenID names the token). One a wait was handed is kept, as at the attempt's end: two runs of the command on one machine share the binding (its kept attempt), so a cancel from one says nothing about a token the other already saved.
func (*Store) Create ¶
Create starts an attempt from a machine at ip whose User-Agent is agent and which proposes the name host. It returns the code (for the person), the binding (for the waiting process) and the attempt.
func (*Store) Deny ¶
Deny decides the attempt code names as denied: its machine is told, and the attempt is forgotten once told (or when it expires).
func (*Store) Known ¶
Known reports whether binding names a live attempt, changing nothing: what a throttle keys a wait by.
func (*Store) SetOnAbandon ¶
SetOnAbandon sets what is told of an approved attempt whose token was revoked because no hand-over of it stands (none was made, or each was undelivered): past CollectGrace, or cancelled. Call it before the store is used.
func (*Store) Sweep ¶
func (s *Store) Sweep()
Sweep forgets every attempt past its end (TTL; for an approved one, TTL plus CollectGrace) and revokes each approved token no wait was handed (telling SetOnAbandon's hook).
func (*Store) Wait ¶
func (s *Store) Wait(ctx context.Context, binding string, hold time.Duration) (Result, Attempt, error)
Wait is the waiting machine's read, by its binding: an approved attempt hands its token over (the same token to every wait on that binding until the attempt's end: a hand-over is not a delivery), a denied one says so (and is forgotten), and an undecided one is held until it changes, ctx ends, hold passes or the attempt expires, and then reads pending. A binding naming no live attempt is ErrNotFound (the caller answers StateExpired).