Documentation
¶
Overview ¶
Package authposture owns the durable proxy-authentication posture ratchet (owner decision "Durable Authentication Posture", 2026-11-23; ADR-0063).
It answers exactly one question: "is proxy authentication required on this node even when no usable proxy identity provider is left?" Once the answer is yes it stays yes. It is not an authentication engine: it verifies no credential, knows no provider and decides no request. Package main asks it that one question on the request path and tells it when a provider was first activated.
A Ratchet is a monotonic boolean backed by one small JSON marker file:
- Established is an atomic load, so the proxy hot path pays no lock, no allocation and no I/O for it.
- Establish is write-ahead: the marker is durably written (through the injected writer, which in production is the repository's atomic temp+fsync+rename+dir-fsync helper) BEFORE the in-memory flag flips, and an error is returned, with nothing changed, when it cannot be. A caller that must not report an activation the ratchet has not recorded calls Establish before it publishes the activation.
- EstablishFailClosed is for activations that cannot be refused (a provider loaded at startup from configuration that is itself on disk): the flag flips even when the write fails, and the status records that the marker is not durable. A later Establish retries the write.
- Nothing clears it. There is no method that lowers the flag; Open never lowers a flag already raised in memory.
Reading the marker is fail-closed. A marker that is present but corrupt (not valid JSON, an unknown version, an established record without a timestamp, trailing data, unknown fields) or unreadable (a permission error, a directory in its place) is treated as ESTABLISHED, and the status names the problem so the owner can surface it. Only a missing file is not established, and Open reports it as such so the owner can run its migration (derive the posture from other durable evidence); RecordFresh then writes a "fresh" record so the migration runs once, not on every start.
MergeForRestore is the monotonic merge a backup restore applies: whatever side establishes (or is unreadable) wins, the current node first, and a "fresh" record is never carried over, so the restored node re-derives it from the data it now holds.
The package has no package-level state, no init, starts no goroutine and logs nothing. It reads the marker with os.ReadFile unless a reader is injected, and writes only through the injected writer. Every Ratchet is independent; the zero value is usable and in-memory only (not established, no file) until Open names a file.
Index ¶
- Constants
- func Encode(rec Record) ([]byte, error)
- func MergeForRestore(current, incoming []byte) (out []byte, keep bool)
- type Outcome
- type Problem
- type Ratchet
- func (r *Ratchet) Establish(by string, now time.Time) (changed bool, err error)
- func (r *Ratchet) EstablishFailClosed(by string, now time.Time) (changed bool, err error)
- func (r *Ratchet) Established() bool
- func (r *Ratchet) MigrationPending() bool
- func (r *Ratchet) Open(path string, write WriteFunc, read ReadFunc) (Outcome, error)
- func (r *Ratchet) RecordFresh(now time.Time) error
- func (r *Ratchet) Status() Status
- type ReadFunc
- type Record
- type Status
- type WriteFunc
Constants ¶
const FormatVersion = 1
FormatVersion is the only marker version this build reads or writes. A marker with any other version is corrupt, and so treated as established.
Variables ¶
This section is empty.
Functions ¶
func MergeForRestore ¶
MergeForRestore is the monotonic marker merge of a backup restore. current is the marker on the node being restored and incoming the one in the archive; each is nil when absent. A side that establishes, or is corrupt (and so treated as established), wins, the current node first; keep is false when neither does, in which case the restore must leave NO marker, so the next start re-derives the posture from the restored data. A fresh record is never carried over. A restore can therefore set the posture but never clear it.
Types ¶
type Outcome ¶
type Outcome int
Outcome is what Open found.
const ( // OutcomeMissing means no marker file. Not established; a migration is pending. OutcomeMissing Outcome = iota // OutcomeFresh means a valid fresh record. Not established; no migration. OutcomeFresh // OutcomeEstablished means a valid established record. OutcomeEstablished // OutcomeCorrupt means present but invalid. Treated as established. OutcomeCorrupt // OutcomeUnreadable means it could not be read. Treated as established. OutcomeUnreadable )
type Problem ¶
type Problem string
Problem names why an established status is not backed by a readable, durable marker. The empty Problem means none.
const ( // ProblemCorrupt means the marker exists but is not a valid record. ProblemCorrupt Problem = "corrupt" // ProblemUnreadable means the marker exists (or cannot be shown not to) but // could not be read. ProblemUnreadable Problem = "unreadable" // ProblemNotDurable means established in memory by EstablishFailClosed, but the // marker could not be written. ProblemNotDurable Problem = "not_durable" )
type Ratchet ¶
type Ratchet struct {
// contains filtered or unexported fields
}
Ratchet is the monotonic "proxy authentication established" flag and its marker file. The zero value is usable: in memory only and not established.
func (*Ratchet) Establish ¶
Establish records that proxy authentication is established, WRITE-AHEAD: the marker is written first and the in-memory flag is raised only once the write succeeded. On a write error nothing changes and the error is returned, so the caller can refuse the activation it was about to report. changed is true only for the call that raised the flag (or completed a pending write).
func (*Ratchet) EstablishFailClosed ¶
EstablishFailClosed is Establish for an activation that cannot be refused: the flag is raised even when the marker cannot be written, the status then reports ProblemNotDurable, and the write error is still returned for the caller to log. A later Establish retries the write.
func (*Ratchet) Established ¶
Established reports whether proxy authentication is required even with no usable provider. Lock-free and allocation-free: an atomic load.
func (*Ratchet) MigrationPending ¶
MigrationPending reports whether Open found no marker and no later Establish or RecordFresh has settled the posture yet.
func (*Ratchet) Open ¶
Open names the marker file, the writer that persists it and (optionally) the reader, and loads the marker. It never lowers a flag already raised: a ratchet established in memory stays established whatever the file says.
The error is non-nil only for OutcomeCorrupt and OutcomeUnreadable and carries the cause; the ratchet is then established (fail closed).
func (*Ratchet) RecordFresh ¶
RecordFresh settles a pending migration that found no evidence: it writes a fresh record so later starts do not re-run the migration. A no-op unless Open found the marker missing and nothing has established it since. On a write error the migration stays pending (the next start evaluates again).
type Record ¶
type Record struct {
Version int `json:"version"`
Established bool `json:"established"`
Since string `json:"since,omitempty"`
By string `json:"by,omitempty"`
EvaluatedAt string `json:"evaluated_at,omitempty"`
}
Record is the marker file's content.
An established record carries Since (RFC 3339, UTC) and By (what established it). A fresh record carries EvaluatedAt: the moment a node whose marker was missing was evaluated and found no evidence that proxy authentication had ever been established.
type Status ¶
type Status struct {
Established bool
Since string // RFC 3339; empty when unknown (corrupt/unreadable marker)
By string // what established it; empty when unknown
Problem Problem
Detail string // the read/parse/write error behind Problem, for the owner's log
}
Status is a point-in-time copy of the ratchet's state.