audit

package
v0.7.20 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: Apache-2.0 Imports: 8 Imported by: ? 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 audit defines the security events a cedar server reports: the handshake, each authentication attempt, sessions as they are created, resumed, imported and ended, and the authorization decision for each command.

cedar reports events through a Hook and records nothing itself. The embedding program decides what to keep, where, and for how long. A Hook is called synchronously on the connection's goroutine (or, for sessions ending, on whichever goroutine touched the cache), so it must be quick, must not block, and must be safe for concurrent use. A program that writes events to disk queues them in the Hook and writes them elsewhere.

An Event carries no secret: no session key, no token signature, no whole token. It does carry the session id, which a peer presents in the clear to resume a session; record SessionRef(key, id) rather than the id itself.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Emit

func Emit(ctx context.Context, h Hook, e Event)

Emit calls h with e, stamping e.Time if it is unset. A nil h does nothing.

func SessionRef

func SessionRef(key []byte, id string) string

SessionRef returns a stable reference to a session id that does not reveal it: the first 16 bytes of HMAC-SHA256(key, id), in hex. Events with the same id and key share a reference. Keep key secret and stable across restarts so references join over time.

Types

type Count

type Count struct {
	Type    Type
	Outcome Outcome
	Reason  Reason
	N       uint64
}

Count is one row of a Counter's snapshot.

type Counter

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

Counter is a Hook that only counts events, by type, outcome and reason. It records nothing else, so it is cheap enough to run on a busy daemon to learn how many events of each kind a full recorder would have to keep.

func (*Counter) Record

func (c *Counter) Record(_ context.Context, e Event)

Record counts e. Its signature is a Hook's.

func (*Counter) Snapshot

func (c *Counter) Snapshot() []Count

Snapshot returns the counts so far, sorted by type, outcome and reason.

type Credential

type Credential struct {
	// Tokens (IDTOKENS, TOKEN, SCITOKENS).
	Issuer  string
	Subject string
	KeyID   string // the signing key's id ("kid")
	ID      string // the token's own id ("jti")
	Scopes  []string
	Expires time.Time

	// X.509 (SSL).
	CertSubject string
	CertIssuer  string
	CertSerial  string
	CertSHA256  string // hex SHA-256 of the DER certificate
}

Credential is what an identity rests on, as the authentication method verified it. Only the fields the method provides are set.

type Direction

type Direction string

Direction is which side of the connection this process was on.

const (
	Inbound  Direction = "inbound"
	Outbound Direction = "outbound"
)

Directions. Every event cedar's server reports is Inbound. A session ending is reported by the cache, which holds sessions of both kinds and does not know which; its Direction is empty.

type Event

type Event struct {
	Time      time.Time
	Type      Type
	Outcome   Outcome
	Reason    Reason
	Direction Direction

	// Err is the error, for people. It can quote what the peer sent.
	Err error

	// Peer is the peer's "host:port".
	Peer string

	// Stage is how far a failed handshake got.
	Stage Stage

	// SessionID is the session's id. Record SessionRef(key, SessionID), not
	// the id: a peer that knows a session's id can try to resume it.
	SessionID string

	// SessionPeer is the address the session was created with, for a resumed
	// or ended session.
	SessionPeer string

	// Command is the command a command event is about. For DC_SEC_QUERY and
	// DC_AUTHENTICATE it is the command the peer asked about.
	Command int
	// Levels are the authorization levels Command is registered at.
	Levels []string
	// Limits are the authorization levels the session's credential is
	// limited to; nil when it has no such limit.
	Limits []string

	// AuthMethod is the authentication method: the one attempted, for
	// AuthAttempt; the one the session authenticated with, otherwise.
	AuthMethod string
	// CryptoMethod is the session's cipher.
	CryptoMethod string
	// Authenticated and Encrypted describe the session.
	Authenticated bool
	Encrypted     bool

	// AuthenticatedAs is the identity the authentication method produced,
	// before any mapping. User is the identity the session runs as.
	AuthenticatedAs string
	User            string

	// Credential is what the identity rests on, when the method verified
	// one.
	Credential *Credential

	// Expires and Lease are a session's absolute expiry and idle lease.
	Expires time.Time
	Lease   time.Duration
	// ValidCommands is how many commands a new session may run.
	ValidCommands int
}

Event is one audit event. Fields that do not apply to its Type are zero.

type Hook

type Hook func(ctx context.Context, e Event)

Hook receives events. See the package documentation for its contract.

type Outcome

type Outcome string

Outcome is whether it worked.

const (
	Success Outcome = "success"
	Failure Outcome = "failure"
	Denied  Outcome = "denied"
)

Outcomes.

type Reason

type Reason string

Reason is a stable code for why. Match on it, never on Event.Err's text.

const (
	// Any failure.
	ReasonIO       Reason = "io"       // the connection failed or closed
	ReasonCanceled Reason = "canceled" // the connection's context ended
	ReasonProtocol Reason = "protocol" // the peer sent something malformed or unexpected

	// HandshakeFailed at StageNegotiate.
	ReasonAuthPolicyMismatch       Reason = "auth_policy_mismatch"       // one side requires authentication, the other forbids it
	ReasonEncryptionPolicyMismatch Reason = "encryption_policy_mismatch" // likewise for encryption or integrity
	ReasonNoCommonAuthMethod       Reason = "no_common_auth_method"
	ReasonNoCommonCryptoMethod     Reason = "no_common_crypto_method"

	// HandshakeFailed at StageAuthenticate, and AuthAttempt.
	ReasonAuthFailed        Reason = "auth_failed"        // the method ran and did not authenticate the peer
	ReasonNoCredential      Reason = "no_credential"      // the method completed but the peer presented no credential (SSL without a client certificate)
	ReasonNoMethodSelected  Reason = "no_method_selected" // the client offered no method the server accepts
	ReasonClientGaveUp      Reason = "client_gave_up"     // the client ran out of methods while authentication was required
	ReasonMethodUnsupported Reason = "method_unsupported" // the method is not implemented

	// HandshakeFailed at StageKeyExchange.
	ReasonKeyExchange Reason = "key_exchange"

	// SessionResumeFailed.
	ReasonSessionNotFound   Reason = "session_not_found"   // unknown or expired
	ReasonUnsupportedCrypto Reason = "unsupported_crypto"  // keyed with a cipher cedar cannot read
	ReasonSessionKeyMissing Reason = "session_key_missing" // an authenticated session with no key to prove possession

	// AuthzDenied.
	ReasonCommandNotFound Reason = "command_not_found" // no handler for the command
	ReasonSecurityLevel   Reason = "security_level"    // the session lacks authentication or encryption the command requires
	ReasonOutsideLimits   Reason = "outside_limits"    // the credential's authorization limits exclude the command's levels
	ReasonLevelNotGranted Reason = "level_not_granted" // the identity is not authorized at any of the command's levels

	// CommandFailed.
	ReasonHandler Reason = "handler"

	// SessionImported.
	ReasonClaim        Reason = "claim"         // from a claim id
	ReasonFileTransfer Reason = "file_transfer" // from a claim id, for file transfer
	ReasonInherited    Reason = "inherited"     // handed down by a parent process
	ReasonMinted       Reason = "minted"        // minted for a child process
	ReasonClaimMinted  Reason = "claim_minted"  // minted with a new claim id (a startd)

	// SessionEnded.
	ReasonExpired     Reason = "expired"     // its duration or lease ran out
	ReasonInvalidated Reason = "invalidated" // something invalidated it by id
	ReasonCleared     Reason = "cleared"     // the whole cache was cleared
	ReasonReplaced    Reason = "replaced"    // another session was stored under its id
)

Reasons. New ones may be added; existing ones keep their meaning.

type Stage

type Stage string

Stage is how far a handshake got.

const (
	// StageCommand: reading the command integer that opens the connection.
	StageCommand Stage = "command"
	// StageRequest: reading the client's security ad.
	StageRequest Stage = "request"
	// StageResume: resuming a cached session.
	StageResume Stage = "resume"
	// StageNegotiate: reconciling the two sides' security policies.
	StageNegotiate Stage = "negotiate"
	// StageAuthenticate: running authentication methods.
	StageAuthenticate Stage = "authenticate"
	// StageKeyExchange: deriving and enabling the session key.
	StageKeyExchange Stage = "key_exchange"
	// StagePostAuth: sending the post-authentication reply.
	StagePostAuth Stage = "post_auth"
)

Handshake stages, in order.

type Type

type Type string

Type is what happened.

const (
	// HandshakeFailed: the security handshake failed before a session was
	// established, for a reason other than the command being refused (that
	// is AuthzDenied). Stage says how far it got.
	HandshakeFailed Type = "handshake.failed"

	// AuthAttempt: one authentication method finished on the server, either
	// way. A client that tries several methods produces one per method.
	AuthAttempt Type = "auth.attempt"

	// SessionCreated: a full handshake established a session and cached it.
	SessionCreated Type = "session.created"

	// SessionResumed: a peer resumed a cached session on a new connection.
	// SessionPeer is the address the session was created with.
	SessionResumed Type = "session.resumed"

	// SessionResumeFailed: a peer asked to resume a session the server would
	// not resume.
	SessionResumeFailed Type = "session.resume_failed"

	// SessionImported: a session entered the cache without a handshake, from
	// a claim id, a parent process, or minted for a child. Reason says which.
	SessionImported Type = "session.imported"

	// SessionEnded: a session left the cache. Reason says why.
	SessionEnded Type = "session.ended"

	// AuthzDenied: a command was refused, whether by the handshake or at
	// dispatch.
	AuthzDenied Type = "authz.denied"

	// AuthzAllowed: a command was authorized and is being dispatched.
	AuthzAllowed Type = "authz.allowed"

	// CommandFailed: a dispatched command's handler returned an error.
	CommandFailed Type = "command.failed"
)

Event types.

Jump to

Keyboard shortcuts

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