distribution

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 51 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 distribution serves bundles to the engines (0004 §4.7): every operator replica runs the BundleService, pushes each new bundle to its connected engines, and records what they applied. The leader merges the replicas' records into the Enforced condition of each policy.

Index

Constants

View Source
const (
	// DefaultHelloTimeout bounds how long an engine may take to say Hello.
	DefaultHelloTimeout = 10 * time.Second
	// DefaultIdleTimeout closes a stream that sent nothing (no heartbeat)
	// for this long; engines send heartbeats every few seconds.
	DefaultIdleTimeout = 30 * time.Second
	// DefaultMaxSends bounds concurrent bundle sends, so a new bundle for
	// hundreds of engines doesn't spike the operator.
	DefaultMaxSends = 50
)

Defaults of the Server.

View Source
const (
	ReasonEnforced      = "Enforced"
	ReasonEnginesBehind = "EnginesBehind"
	// ReasonEnginesUnreported: a replica's engine report was over its
	// size limit, so not every engine is known.
	ReasonEnginesUnreported = "EnginesUnreported"
	ReasonEngineNack        = "EngineNack"
	ReasonNoEngines         = "NoEngines"
	ReasonNotInBundle       = "NotInBundle"
	// ReasonEnginesOutdated: an engine runs the bundle but is too old to
	// enforce a field the policy sets, which it ignores.
	ReasonEnginesOutdated = "EnginesOutdated"
	// ReasonSuspended: the bundle suspends inspection (the InkwallConfig's
	// spec.suspend), so engines allow every request.
	ReasonSuspended = "Suspended"
)

Reasons of the Enforced condition.

View Source
const (
	DroppedNotInBundle    = "not_in_bundle"
	DroppedLatencyBuckets = "latency_buckets"
	DroppedEvicted        = "evicted"
)

Reasons of RequestStatsDropped.

View Source
const Audience = "inkwall-operator"

Audience is the token audience engines request.

View Source
const (
	EnginesLabel = "waf.inkwall.dev/engines"
)

Report labels and keys of the replicas' engine ConfigMaps.

Variables

View Source
var (
	ErrUnauthenticated = errors.New("not authenticated")
	ErrForbidden       = errors.New("not allowed to read the bundle")
)

Errors of Authenticate.

View Source
var (

	// RequestStatsDropped counts, per policy and report, the request counts
	// this replica drops: here, and in the controlplane package's totals.
	RequestStatsDropped = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "inkwall_operator_request_stats_dropped_total",
		Help: "Policies' request counts this replica dropped, by reason: not_in_bundle (an engine reported a policy the bundle served here doesn't have), latency_buckets (its latency was counted in other buckets: the counts are kept, the latency dropped), evicted (the totals of a policy no longer in the bundle, forgotten so they stay bounded).",
	}, []string{"reason"})
)

Functions

func Enforced

func Enforced(version int64, engines []Engine, leftOut int) (metav1.ConditionStatus, string, string)

Enforced computes a policy's Enforced condition: whether every connected engine runs a bundle that contains the policy's current generation: the first such bundle is its status.bundleVersion, and later ones have it too. Every engine gets the same bundle, so every engine counts until engines are matched to the routes they serve.

leftOut engines are connected but missing from their replica's report.

The messages don't count engines, so engines connecting or going away don't rewrite every policy's status; the inkwall_operator_engines metric has the counts. EngineNack names the first engine (by ID) that rejected the bundle, with its error.

func Key added in v0.4.3

func Key(id Identity, engineID string) string

Key is the table key of an engine authenticated as id that says it is engineID: the pod its token is bound to, otherwise its ServiceAccount and engineID. An engine can't take the record of another pod, or of another ServiceAccount's engine, by naming its engine ID.

func Ready

func Ready(conds []metav1.Condition, generation int64) metav1.Condition

Ready combines a policy's conditions (0004 §3.2): True when Accepted, Programmed and Enforced all are; otherwise the first that isn't, by its reason, so kubectl shows what is missing. Suspended inspection (Enforced's reason Suspended) comes first: whatever else holds, every engine allows every request (#291).

Types

type Authenticator

type Authenticator interface {
	Authenticate(ctx context.Context, token string) (Identity, error)
}

Authenticator checks an engine's bearer token.

type BundleSource

type BundleSource struct {
	Client    client.Client
	Namespace string
	Store     *Store
}

BundleSource loads the bundle Secret into the Store on every replica.

func (*BundleSource) Reconcile

func (b *BundleSource) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error)

Reconcile loads the current bundle.

func (*BundleSource) SetupWithManager

func (b *BundleSource) SetupWithManager(mgr ctrl.Manager) error

SetupWithManager registers the source with mgr.

type Engine

type Engine struct {
	// ID is the engine's own, from its Hello.
	ID string `json:"id"`
	// ServiceAccount authenticated the stream, as "namespace/name".
	ServiceAccount string `json:"serviceAccount"`
	// Pod and node: from the token when it is bound to a pod, otherwise as
	// the engine reported them.
	PodNamespace  string `json:"podNamespace,omitempty"`
	PodName       string `json:"podName,omitempty"`
	NodeName      string `json:"nodeName,omitempty"`
	EngineVersion string `json:"engineVersion,omitempty"`
	// Applied is the newest bundle version the engine runs.
	Applied uint64 `json:"applied"`
	// Rejected is the newest version the engine refused, and why.
	Rejected    uint64 `json:"rejected,omitempty"`
	RejectError string `json:"rejectError,omitempty"`
	// TrustedKeys are the IDs of the bundle signing keys the engine
	// accepts; empty from an engine older than key rotation.
	TrustedKeys []string `json:"trustedKeys,omitempty"`
	// Replica is the operator replica the engine is connected to.
	Replica     string    `json:"replica"`
	ConnectedAt time.Time `json:"connectedAt"`
	// contains filtered or unexported fields
}

Engine is what a replica knows about one connected engine.

func ReadEngines

func ReadEngines(ctx context.Context, c client.Reader, namespace string, stale time.Duration, now time.Time) (engines []Engine, leftOut int, err error)

ReadEngines merges the replicas' reports, skipping those not refreshed within stale (their replica is gone). An engine listed twice, while it moves between replicas, counts once, with its newest connection (reportKey). They are sorted by ID. leftOut counts the engines left out of full reports: while it isn't 0, the list is incomplete.

type GRPCServer

type GRPCServer struct {
	Port    int
	CertDir string
	Ready   <-chan struct{}
	Service agentv1.BundleServiceServer
	// contains filtered or unexported fields
}

GRPCServer serves the BundleService over TLS, with the certificate in CertDir (the webhook's), once Ready is closed.

func (*GRPCServer) Check

func (g *GRPCServer) Check(*http.Request) error

Check is a readiness check: the server is listening.

func (*GRPCServer) NeedLeaderElection

func (*GRPCServer) NeedLeaderElection() bool

NeedLeaderElection implements LeaderElectionRunnable: every replica serves.

func (*GRPCServer) Start

func (g *GRPCServer) Start(ctx context.Context) error

Start implements manager.Runnable.

type Identity

type Identity struct {
	// ServiceAccount as "namespace/name".
	ServiceAccount string
	// The pod the token is bound to, if it is.
	PodNamespace string
	PodName      string
}

Identity is an authenticated engine.

type KubeAuthenticator

type KubeAuthenticator struct {
	Client client.Client
	// Namespace is the operator's, where the bundle Secret is.
	Namespace string
	// TTL caches a successful check, so reconnecting engines don't each
	// cost two API calls. Revoking access takes effect after at most TTL.
	TTL time.Duration
	Now func() time.Time
	// contains filtered or unexported fields
}

KubeAuthenticator checks tokens with TokenReview, and authorizes them with a SubjectAccessReview for reading the bundle Secret.

func (*KubeAuthenticator) Authenticate

func (a *KubeAuthenticator) Authenticate(ctx context.Context, token string) (Identity, error)

Authenticate implements Authenticator.

type Publisher

type Publisher struct {
	Client client.Client
	// Reader reads uncached: the manager only caches ConfigMaps of rules.
	Reader    client.Reader
	Namespace string
	Replica   string
	// Owner, if set, is the replica's Pod: its ConfigMap goes with it.
	Owner    *metav1.OwnerReference
	Engines  *Table
	Interval time.Duration
	Refresh  time.Duration
}

Publisher writes a replica's engines to its ConfigMap when they change, and at least every Refresh, so the leader can tell a live replica from a dead one.

func (*Publisher) NeedLeaderElection

func (*Publisher) NeedLeaderElection() bool

NeedLeaderElection implements LeaderElectionRunnable.

func (*Publisher) Start

func (p *Publisher) Start(ctx context.Context) error

Start implements manager.Runnable.

type Reporter

type Reporter struct {
	Client client.Client
	// Reader reads the reports uncached (the manager doesn't cache them).
	Reader    client.Reader
	Namespace string
	Interval  time.Duration
	// Stale is how old a replica's report may be before it is ignored.
	Stale time.Duration
	// Store has the bundle being served, for the engine metrics.
	Store *Store
}

Reporter writes the Enforced condition of every policy. It runs on the leader, from the replicas' engine reports.

func (*Reporter) NeedLeaderElection

func (*Reporter) NeedLeaderElection() bool

NeedLeaderElection implements LeaderElectionRunnable.

func (*Reporter) Start

func (r *Reporter) Start(ctx context.Context) error

Start implements manager.Runnable.

type Server

type Server struct {
	agentv1.UnimplementedBundleServiceServer

	Bundles *Store
	Engines *Table
	Auth    Authenticator
	// Replica names this operator replica in the engine records.
	Replica string
	Log     logr.Logger

	HelloTimeout time.Duration
	IdleTimeout  time.Duration

	// Events receives the engines' security events, stamped with the
	// stream's engine and pod. Nil: they are counted and discarded.
	Events func(*eventsv1.EventBatch)
	// Stats receives what the engines counted per policy since their
	// previous report. Nil: discarded.
	Stats func([]*eventsv1.PolicyStats)
	// contains filtered or unexported fields
}

Server implements BundleService.

func NewServer

func NewServer(bundles *Store, engines *Table, auth Authenticator, replica string, log logr.Logger) *Server

NewServer returns a Server with the defaults.

func (*Server) Watch

func (s *Server) Watch(stream agentv1.BundleService_WatchServer) error

Watch serves one engine: Hello, then the current bundle and every new one, while recording its acks, nacks and heartbeats.

type Store

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

Store holds the current bundle and tells streams when it changes.

func NewStore

func NewStore() *Store

NewStore returns an empty store.

func (*Store) Current

func (s *Store) Current() (*bundlev1.SignedBundle, uint64, <-chan struct{})

Current returns the current bundle (nil before the first), its version, and a channel closed when it is replaced.

func (*Store) HasPolicy added in v0.3.0

func (s *Store) HasPolicy(id string) bool

HasPolicy reports whether the current bundle has the policy id.

func (*Store) PolicyVersion added in v0.3.1

func (s *Store) PolicyVersion(id string) uint64

PolicyVersion returns the first bundle that enforces the policy id as the current bundle does; 0 if it doesn't have it.

func (*Store) Set

func (s *Store) Set(signed *bundlev1.SignedBundle, version uint64)

Set makes signed, of version, the current bundle.

func (*Store) SetPolicies added in v0.3.0

func (s *Store) SetPolicies(versions map[string]uint64, suspended bool)

SetPolicies records the current bundle's policies, the only ones whose request counts are taken from engines, with the first bundle that enforces each as it is now; and whether the bundle suspends inspection.

func (*Store) Suspended added in v0.3.1

func (s *Store) Suspended() bool

Suspended reports whether the current bundle suspends inspection.

type Table

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

Table is a replica's connected engines.

func NewTable

func NewTable() *Table

NewTable returns an empty table.

func (*Table) Connect

func (t *Table) Connect(key string, e Engine) uint64

Connect records a new stream for the engine e under key (Key), replacing an older one, and returns the connection to pass to Update and Disconnect.

func (*Table) Disconnect

func (t *Table) Disconnect(key string, conn uint64)

Disconnect forgets the record of key if conn still owns it.

func (*Table) Snapshot

func (t *Table) Snapshot() ([]Engine, uint64)

Snapshot returns the engines sorted by ID, and a generation that changes whenever the table does.

func (*Table) Update

func (t *Table) Update(key string, conn uint64, f func(*Engine) bool)

Update changes the record of key if conn still owns it; f reports whether it changed anything.

Jump to

Keyboard shortcuts

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