Documentation
¶
Overview ¶
Package store holds mash's JetStream layout: per-endpoint inbox streams, their consumer, and the directory and lease KV buckets (spec section 5).
Index ¶
- Constants
- Variables
- func ConsumerConfig() jetstream.ConsumerConfig
- func CreateIssuedBucket(ctx context.Context, js jetstream.JetStream) (jetstream.KeyValue, error)
- func CreateLeaseBucket(ctx context.Context, js jetstream.JetStream, cfg jetstream.KeyValueConfig) (jetstream.KeyValue, error)
- func DirectoryConfig() jetstream.KeyValueConfig
- func DirectoryConfigFor(l Limits) jetstream.KeyValueConfig
- func HardenLeaseBucket(ctx context.Context, js jetstream.JetStream) error
- func InboxConfig(a sap.Address) jetstream.StreamConfig
- func InboxConfigFor(a sap.Address, l Limits) jetstream.StreamConfig
- func InitBucketCaps(l Limits) map[string]int64
- func InitBucketsMaxBytes(l Limits) int64
- func IssuedConfig() jetstream.KeyValueConfig
- func IssuedMarkerRaised(n int, seq uint64) string
- func LeaseConfig() jetstream.KeyValueConfig
- func LeaseConfigFor(l Limits) jetstream.KeyValueConfig
- func LeaseRollupAllowed(cfg jetstream.StreamConfig) bool
- func LeaseStreamConfig(ctx context.Context, js jetstream.JetStream) (jetstream.StreamConfig, error)
- func LeaseTombstone() []byte
- func MailSubjectFor(a sap.Address) string
- func PurposeClassChanged(from, to string) bool
- func RetiredConfig() jetstream.KeyValueConfig
- func SuspendedSubject(flat string) string
- func ValidCap(c string) bool
- func ValidCaps(caps []string) error
- func ValidDescription(v string) error
- func ValidLabelKey(k string) bool
- func ValidLabels(l map[string]string) error
- func ValidMeta(field, v string) error
- func ValidPurpose(p string) bool
- func VerifyIssuedBucket(ctx context.Context, js jetstream.JetStream) error
- func VerifyLeaseBucket(ctx context.Context, js jetstream.JetStream) error
- func VerifyRetiredStatus(st jetstream.KeyValueStatus) error
- type InboxEviction
- type InboxState
- type Issued
- type IssuedMarker
- type Limits
- type Record
- type Retired
Constants ¶
const ( IssuedMarkerKey = "mash_issued_history" IssuedMarkerFull = "full" )
IssuedMarkerKey is the key in the issue record stream's own metadata (the stream config's Metadata map, which no data write can change) that says whether the retained history of a key can be trusted to be complete. A bucket made at the full history IssuedHistory carries IssuedMarkerFull. When `mashctl init` raises an older bucket to IssuedHistory it writes IssuedMarkerRaised(n, seq): n is the per-key history it had, seq the stream's last sequence at that moment. A bucket with neither is a legacy or unknown one.
const ( // IssuedEndpoint is a policy endpoint's user, issued by `mashctl endpoint create` or `mashctl endpoint reissue`. IssuedEndpoint = "endpoint" // IssuedAdmin is the admin user mash or the operator uses to administer the account. IssuedAdmin = "admin" )
The kinds of an issued user.
const ( // PurposeIssue is an ordinary issue: the endpoint command, a reissue that keeps the key, the admin user at init. PurposeIssue = "issue" // PurposeRotation is the new key of a reissue that rotates the user key. PurposeRotation = "rotation" // PurposeProbe is a probe credential of the policy gate. No mash path writes it yet. PurposeProbe = "probe" )
The purposes of an issued user: why mash issued the credential. The set is closed; an unknown purpose is refused on write.
const ( DirectoryBucket = "mash-directory" LeaseBucket = "mash-leases" RetiredBucket = "mash-retired" IssuedBucket = "mash-issued" Consumer = "deliver" )
Bucket and consumer names.
const ( InboxMaxAge = 30 * 24 * time.Hour InboxMaxBytes = 16 << 20 MaxMsgSize = 1 << 20 LeaseTTL = 90 * time.Second )
Inbox limits (spec 5.1).
const ( DirectoryMaxBytes = 16 << 20 LeaseMaxBytes = 4 << 20 // RetiredMaxBytes caps the retired-name bucket. The bucket keeps one small marker per retired new-form identity // for good and mash never deletes one. A full bucket makes the stream discard new markers, so // VerifyRetiredStatus and provisioning then refuse. 64 MiB holds many markers. RetiredMaxBytes = 64 << 20 )
Bucket size caps. Hosted NATS accounts (Synadia Cloud) reject a stream config with no MaxBytes, so every bucket sets one. Directory records and leases are tiny; these are generous.
const ( MetaSuspendedSubjects = "mash_suspended_subjects" MetaSuspendedAt = "mash_suspended_at" MetaSuspendedID = "mash_suspended_id" // random 128-bit hex token naming one suspension // MetaSuspendedUser is the user public key of the directory record at suspend time; empty or absent when there was // no record. The drained deprovision requires the record to still carry it. MetaSuspendedUser = "mash_suspended_user" )
const ( RetiredPending = "pending" RetiredFinal = "final" )
The states of a retired marker. Either one blocks provisioning of the name.
const InboxDuplicateWindow = 2 * time.Minute
InboxDuplicateWindow is how long the inbox stream remembers a Nats-Msg-Id: a publish with the same id inside it is dropped, after it the message is stored again. It bounds what "send --id" can promise.
const InboxNearFullPercent = 90
InboxNearFullPercent is the share of an inbox's byte cap at which mashctl doctor and mash status call it near full.
const InitBuckets = 4
InitBuckets is how many KV buckets mashctl init makes (directory, leases, retired, issued).
const IssuedHistory = 64
IssuedHistory is the most revisions the issue record keeps of one key: 64, the most a JetStream KV bucket allows (nats.go's KeyValueMaxHistory). A key is re-recorded only by a reissue that keeps it, so 64 is far more than a key sees; the cap is still a cap: a 65th revision of one key evicts the oldest, and the record holds the last 64.
const IssuedMaxBytes = 8 << 20
IssuedMaxBytes caps the issue record's bucket. A record is a few hundred bytes. The stream discards new messages when full, never an old record, so a full bucket refuses new issues (fail closed) and nothing is lost.
const IssuedMinHistory = 8
IssuedMinHistory is the smallest per-key history verification accepts. The first version of the record kept 8 revisions of a key; such a bucket is accepted and `mashctl init` raises it to IssuedHistory (a raise loses nothing).
const IssuedStreamName = "KV_" + IssuedBucket
IssuedStreamName is the stream that backs the issue record's bucket.
const LeaseStreamName = "KV_" + LeaseBucket
LeaseStreamName is the stream that backs the lease bucket.
const MaxCaps = 8
MaxCaps is the most capability words one record may carry.
const MaxDescriptionLen = 256
MaxDescriptionLen is the longest Description.
const MaxLabels = 64
MaxLabels is the most labels one record may carry. It is enforced where labels are written (ValidLabels), not where a record is read, so a record with more is still parsed (ADR 0072).
const MaxMetaLen = 64
MaxMetaLen is the longest Role, Host or label value.
Variables ¶
var ( ErrKeyRevoked = errors.New("mash: issue record: the public key has been revoked") ErrKeyBoundElsewhere = errors.New("mash: issue record: the public key is bound to another address") // ErrKeyPurposeChanged is what a checked write refuses with when it would move an active key between the sender // class (issue, rotation) and the probe class. ErrKeyPurposeChanged = errors.New("mash: issue record: the revision changes the purpose class of the key") )
ErrKeyRevoked and ErrKeyBoundElsewhere are what a checked write of the issue record refuses with: the key's record has revoked_at set, or is bound to an address other than the one being written.
var ErrIssuedBucket = errors.New("issue record bucket is not hardened")
ErrIssuedBucket is returned when the issue record's stream is not configured as mash makes it.
var ErrLeaseBucketRollup = errors.New("lease bucket allows rollup; run `mashctl init`")
ErrLeaseBucketRollup is returned when the lease bucket's stream allows rollup and that could not be changed.
Functions ¶
func ConsumerConfig ¶
func ConsumerConfig() jetstream.ConsumerConfig
ConsumerConfig is the durable consumer the endpoint manager reads its inbox through.
func CreateIssuedBucket ¶ added in v0.5.0
CreateIssuedBucket creates the issue record's bucket with its stream refusing rollup, delete and purge from the first moment, the way CreateLeaseBucket does for the lease bucket (the KV default allows rollup, and KeyValueConfig cannot say otherwise). If the stream already exists the existing bucket is verified, never altered. It is verified after creation and an error is returned unless it is as mash makes it (fail closed).
func CreateLeaseBucket ¶ added in v0.4.0
func CreateLeaseBucket(ctx context.Context, js jetstream.JetStream, cfg jetstream.KeyValueConfig) (jetstream.KeyValue, error)
CreateLeaseBucket creates the lease bucket with its stream refusing rollup from the first moment. It builds the stream configuration the way nats.go's CreateKeyValue does (name, subjects, history, size cap, TTL, duplicate window, discard policy and the rest) with AllowRollup off, so a bucket made here never allows rollup, not even briefly. cfg.Bucket must be LeaseBucket; a mirror, sources or a limit marker TTL are not supported. If the stream already exists (another caller created it first) the existing bucket is hardened. The result is verified, and an error is returned unless the stream refuses rollup (fail closed).
func DirectoryConfig ¶
func DirectoryConfig() jetstream.KeyValueConfig
DirectoryConfig is the directory bucket: name -> Record, written by the admin.
func DirectoryConfigFor ¶ added in v0.3.1
func DirectoryConfigFor(l Limits) jetstream.KeyValueConfig
DirectoryConfigFor is DirectoryConfig under limits l.
func HardenLeaseBucket ¶ added in v0.4.0
HardenLeaseBucket makes the lease bucket's stream refuse rollup. The lease bucket does not allow rollup. The KV default stream allows rollup, and KeyValueConfig cannot say otherwise, so the setting is made on the stream. It reads the stream, updates it only if AllowRollup is set, then reads it again and returns an error unless the effective setting is off (fail closed). It is idempotent. DenyPurge is left as it is (the server refuses DenyPurge together with AllowRollup).
func InboxConfig ¶
func InboxConfig(a sap.Address) jetstream.StreamConfig
InboxConfig is the inbox stream of the endpoint at a. No subject transform: a stored Agent-Sender header must verify against the stored subject (SAP §13.8).
func InboxConfigFor ¶ added in v0.3.1
func InboxConfigFor(a sap.Address, l Limits) jetstream.StreamConfig
InboxConfigFor is InboxConfig under limits l: inbox-<flat key>, filtered on a's own mail subject only (a.MailFilter()).
func InitBucketCaps ¶ added in v0.5.10
InitBucketCaps is the configured size cap of each bucket init makes, by bucket name; the values sum to InitBucketsMaxBytes(l).
func InitBucketsMaxBytes ¶ added in v0.5.9
InitBucketsMaxBytes is the sum of the size caps of the buckets init makes, with the caps of l for the directory and lease buckets. Keep it in step with InitBuckets and with the buckets admin.Init makes.
func IssuedConfig ¶ added in v0.5.0
func IssuedConfig() jetstream.KeyValueConfig
IssuedConfig is the issue record's bucket: user public key -> Issued, written only by the minting authority (the admin connection of mashctl). It has no TTL and a history of IssuedHistory.
func IssuedMarkerRaised ¶ added in v0.5.0
IssuedMarkerRaised is the marker value for a bucket raised from a per-key history of n at stream sequence seq.
func LeaseConfig ¶
func LeaseConfig() jetstream.KeyValueConfig
LeaseConfig is the lease bucket: name -> holder, expiring unless refreshed.
func LeaseConfigFor ¶ added in v0.3.1
func LeaseConfigFor(l Limits) jetstream.KeyValueConfig
LeaseConfigFor is LeaseConfig under limits l.
func LeaseRollupAllowed ¶ added in v0.5.0
func LeaseRollupAllowed(cfg jetstream.StreamConfig) bool
LeaseRollupAllowed reports whether a lease bucket stream configuration allows rollup writes. It reads nothing and changes nothing: a warning-only check (an endpoint at open) uses it where VerifyLeaseBucket's error would be too much.
func LeaseStreamConfig ¶ added in v0.4.0
LeaseStreamConfig is the current configuration of the lease bucket's stream.
func LeaseTombstone ¶
func LeaseTombstone() []byte
LeaseTombstone is what Deprovision writes to an endpoint's lease key: it fences the name, so a live or starting session cannot take the lease back after its endpoint is gone. It expires with the bucket's TTL (LeaseTTL) like any lease, and Provision clears it.
func MailSubjectFor ¶ added in v0.3.3
MailSubjectFor is the mail subject of the endpoint at a.
func PurposeClassChanged ¶ added in v0.5.0
PurposeClassChanged reports whether a revision from purpose from to purpose to moves a key between the sender class (issue, rotation) and the probe class. An empty purpose reads as PurposeIssue.
func RetiredConfig ¶ added in v0.4.0
func RetiredConfig() jetstream.KeyValueConfig
RetiredConfig is the retired-name bucket: flat identity -> Retired, written when a new-form identity is deprovisioned. It has no TTL (unlike the lease bucket, whose tombstone lapses after LeaseTTL) and nothing in mash deletes from it: INV-002, a retired name is never reassigned, needs the marker to outlive the lease. The KV stream jetstream makes for it discards new messages when full (never an old marker); VerifyRetiredStatus refuses a bucket whose stream does otherwise.
func SuspendedSubject ¶ added in v0.3.3
SuspendedSubject is the one subject a suspended endpoint's inbox stream takes (ADR 0057). It sits outside agents.>, so no endpoint's publish allow-list reaches it. MetaSuspendedSubjects (a JSON list) and MetaSuspendedAt (RFC 3339, UTC) are the stream metadata keys that hold the subjects it had and when they were parked. The argument is the endpoint's flat key (Address.Flat).
func ValidCap ¶ added in v0.3.2
ValidCap reports whether c is a legal capability word: [a-z][a-z0-9_.-]{0,31}.
func ValidCaps ¶ added in v0.3.2
ValidCaps checks a capability list: at most MaxCaps words, each passing ValidCap, none repeated.
func ValidDescription ¶ added in v0.3.2
ValidDescription checks a description: at most MaxDescriptionLen characters, printable, no tab or newline.
func ValidLabelKey ¶
ValidLabelKey reports whether k is a legal label key.
func ValidLabels ¶
ValidLabels checks a label set: at most MaxLabels keys, each key [a-z][a-z0-9_.-]{0,31}, each value passing ValidMeta.
func ValidMeta ¶
ValidMeta checks one metadata value: at most MaxMetaLen characters, printable, no tab or newline (they end up in tab-separated output). Empty is valid (unset).
func ValidPurpose ¶ added in v0.5.0
ValidPurpose reports whether p is one of the closed set of purposes.
func VerifyIssuedBucket ¶ added in v0.5.0
VerifyIssuedBucket reads the issue record's stream and returns an error wrapping ErrIssuedBucket unless it refuses rollup, delete and purge, keeps no TTL, discards new messages when full and keeps at least IssuedMinHistory revisions of a key. It never changes the bucket.
func VerifyLeaseBucket ¶ added in v0.4.0
VerifyLeaseBucket reads the lease bucket's stream and returns ErrLeaseBucketRollup unless it refuses rollup.
func VerifyRetiredStatus ¶ added in v0.4.0
func VerifyRetiredStatus(st jetstream.KeyValueStatus) error
VerifyRetiredStatus is the fail-closed check of the retired-name bucket's configuration: a TTL would expire markers, a history other than RetiredConfig's is not a bucket mash made, and memory storage would lose every marker on a server restart. It never changes the bucket; the error tells the admin to fix or recreate it.
Types ¶
type InboxEviction ¶ added in v0.4.0
type InboxEviction struct {
// Dropped is the number of messages whose sequence is below the stream's first: every message the stream took and
// no longer holds. Deleting and purging are denied on an inbox, so what is gone was dropped by a limit: the byte cap
// (the oldest mail goes first) or the age limit. A stream that is deprovisioned and provisioned again starts at 0.
Dropped uint64
// Unread is a lower bound on how many of those were lost unread: at least this many were never delivered to the
// endpoint. It comes from the consumer's sequences, which count deliveries and not what the user was shown, so it
// can read low: mail that was fetched but never shown to the user counts as delivered, and a redelivery counts as
// a delivery again. A zero therefore does not prove that nothing was lost unread.
Unread uint64
// Fill is the stored bytes as a percentage of the byte cap; 0 when there is no cap.
Fill float64
}
InboxEviction is how much mail an inbox has dropped and how full it is.
func Eviction ¶ added in v0.4.0
func Eviction(s InboxState) InboxEviction
Eviction derives an inbox's drops from stream state alone. The server keeps no counter, but it does keep the first sequence, and sequences are never reused. A consumer that delivers from the start numbers its deliveries from 1, and the ack floor stalls below a gap only until the next delivery jumps over it, so the ack floor cannot say what was lost; the delivered stream sequence against the delivered consumer sequence can: every stream sequence at or below the first delivered one that the consumer skipped was dropped before it got there, and every message below FirstSeq that is past the last delivery was dropped waiting. The result is a lower bound on what was lost unread: consumer sequences count deliveries, not what the user was shown.
func (InboxEviction) NearFull ¶ added in v0.4.0
func (e InboxEviction) NearFull() bool
NearFull reports whether the inbox is at or over InboxNearFullPercent of its byte cap.
type InboxState ¶ added in v0.4.0
type InboxState struct {
FirstSeq, LastSeq uint64
Bytes uint64
MaxBytes int64 // the stream's byte cap; 0 or less when it has none
HaveConsumer bool // the stream has the durable consumer, and it delivers from the start (policy all)
DeliveredStream uint64 // the highest stream sequence the consumer delivered
DeliveredConsumer uint64 // the consumer sequence of its last delivery
}
InboxState is what the server shows of one inbox stream and its consumer, the inputs of Eviction.
type Issued ¶ added in v0.5.0
type Issued struct {
PublicKey string `json:"public_key"`
// Address is the endpoint's flat key (Address.Flat; for an admin user, the name in its JWT). Owner is the
// endpoint's owner token ("" when the user has none).
Address string `json:"address"`
Owner string `json:"owner,omitempty"`
Kind string `json:"kind"`
Tags []string `json:"tags"`
// Generation is the generation of the policy the JWT was issued under. mash has no policy generation yet, so it
// is 0.
Generation int `json:"generation"`
// IssuedAt is RFC 3339, UTC.
IssuedAt string `json:"issued_at"`
// Purpose is why the credential was issued (PurposeIssue, PurposeRotation, PurposeProbe). A record from before the
// field has none, which is read as PurposeIssue (see EffectivePurpose).
Purpose string `json:"purpose,omitempty"`
// RevokedAt is RFC 3339, UTC: when mash revoked the key. Omitted while unset; once set it is never cleared.
RevokedAt string `json:"revoked_at,omitempty"`
}
Issued is one entry of the issue record: a user JWT mash issued. It holds public information only, never a seed or a JWT. The bucket key is the user's public key.
func (Issued) EffectivePurpose ¶ added in v0.5.0
EffectivePurpose is Purpose, or PurposeIssue for a record written before the field existed. An unknown value is returned as it is.
type IssuedMarker ¶ added in v0.5.0
IssuedMarker is a parsed marker. Kind is "" (no marker), "full" or "raised"; for "raised", N and Seq are set.
func ParseIssuedMarker ¶ added in v0.5.0
func ParseIssuedMarker(md map[string]string) (IssuedMarker, error)
ParseIssuedMarker reads the marker out of a stream's metadata. A value that is present but not one of the two forms is an error: it is not read as absent.
type Limits ¶ added in v0.3.1
type Limits struct {
InboxMaxBytes int64
InboxMaxAge time.Duration
DirectoryMaxBytes int64
LeaseMaxBytes int64
InboxMaxConsumers int // -1 is unlimited
}
Limits are the configurable caps: the inbox's size, age and consumer count, and the two buckets' sizes. DefaultLimits is what mash has always used.
func DefaultLimits ¶ added in v0.3.1
func DefaultLimits() Limits
DefaultLimits returns the built-in limits.
type Record ¶
type Record struct {
Owner string `json:"owner"`
Group string `json:"group"`
Instance string `json:"instance"`
Agent string `json:"agent"`
Account string `json:"account"`
User string `json:"user"`
MailSubject string `json:"mail_subject"`
Created string `json:"created"`
Note string `json:"note,omitempty"`
// Role, Host and Labels are optional directory metadata: what the endpoint is for, the machine it is meant to run
// on, and free-form key=value labels (for example a team, project or fleet). They are the identity on record; the
// lease is the live view.
Role string `json:"role,omitempty"`
Host string `json:"host,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
// Description is free text on what the endpoint does, and Caps are up to MaxCaps capability words (sorted), both
// set by the admin.
Description string `json:"description,omitempty"`
Caps []string `json:"caps,omitempty"`
}
Record is one directory entry (spec 6.1). Owner, Group and Instance are the endpoint's address (Address); Agent is the harness that runs it.
func ParseRecord ¶
ParseRecord decodes a directory value and checks it is an endpoint of this release stored under key: a valid address (owner, group, instance), key equal to the address's flat key, the mail subject of that address, and a user. Any other value is refused.
type Retired ¶ added in v0.4.0
type Retired struct {
Owner string `json:"owner"`
Group string `json:"group"`
Instance string `json:"instance"`
User string `json:"user,omitempty"`
RetiredAt string `json:"retired_at"` // RFC 3339, UTC
// State is RetiredPending while a retirement has started and not finished, and RetiredFinal once the record is gone.
// A marker with no state (written before the two phases existed) counts as final.
State string `json:"state,omitempty"`
}
Retired is the value of one retired-name marker. It names the identity's three tokens and the user public key that was retired; it never holds a seed or any other secret. User is empty when the record could not be read.