hostjoin

package
v0.322.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 over exactly once, to a caller holding that binding, and 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), its token is handed over once, and every step checks TTL against the store's clock: a code past it is unknown.

🔴 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

View Source
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
)
View Source
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

View Source
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

func BindingShapeOK(s string) bool

BindingShapeOK reports whether s is shaped like a minted binding.

func Clip

func Clip(s string, max int) string

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

func Fingerprint(code string) string

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

type Mint func(host string) (Minted, error)

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

type Minted struct {
	Token  string
	ID     int64
	Revoke func()
}

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, exactly once. Revoke revokes
	// that token: the caller runs it when it could not hand the token on.
	Host   string
	Token  string
	Revoke func()
}

Result is what Wait hands the waiting machine.

type State

type State string

State is an attempt's state as Wait reports it.

const (
	StatePending  State = "pending"
	StateApproved State = "approved"
	StateDenied   State = "denied"
	// StateExpired is what Wait reports for a binding that names no live
	// attempt: expired, already collected, cancelled, or never minted.
	StateExpired State = "expired"
)

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store holds the live attempts. The zero value is not ready: use New.

func New

func New(now func() time.Time) *Store

New returns an empty store on clock now (time.Now when nil).

func (*Store) Approve

func (s *Store) Approve(code string, by int64, host string, mint Mint) (Attempt, error)

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

func (s *Store) Cancel(binding string) (Attempt, error)

Cancel forgets the attempt binding names, whatever its state: its machine gave up. A token an approval minted and nobody collected is revoked with it (the returned attempt's TokenID names it).

func (*Store) Create

func (s *Store) Create(ip, agent, host string) (code, binding string, a Attempt, err error)

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

func (s *Store) Deny(code string) (Attempt, error)

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

func (s *Store) Known(binding string) bool

Known reports whether binding names a live attempt, changing nothing: what a throttle keys a wait by.

func (*Store) Live

func (s *Store) Live() int

Live reports how many attempts are held.

func (*Store) Lookup

func (s *Store) Lookup(code string) (Attempt, error)

Lookup is the live, undecided attempt a typed code names.

func (*Store) SetOnAbandon

func (s *Store) SetOnAbandon(f func(Attempt))

SetOnAbandon sets what is told of an approved attempt whose token was revoked because its machine never collected it (past CollectGrace, or cancelled). Call it before the store is used.

func (*Store) Sweep

func (s *Store) Sweep() []Attempt

Sweep forgets every attempt past its end (TTL; for an approved one, TTL plus CollectGrace), revokes each approved token nobody collected, and returns those attempts.

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 (once: the attempt is then forgotten), 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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL