Documentation
¶
Overview ¶
Package validation provides a shared validator instance.
Index ¶
- Constants
- Variables
- func AtLeastOneField(v any) (string, bool)
- func Breaks(in any) (out []string)
- func Hint(ctx context.Context, fe validator.FieldError) string
- func InScope(ctx context.Context, target string, scope []string) bool
- func Instance() *validator.Validate
- func PendingTarget(ctx context.Context, target string) (string, bool)
- func RegisterTargetValidator(lister AgentLister)
- func ResolveTarget(ctx context.Context, target string) string
- func ScopeEntry(s string) bool
- func SetEnforceVerifiedRegistrations(enforce bool)
- func Struct(ctx context.Context, v any) (string, bool)
- func UnverifiedTarget(ctx context.Context, target string) (string, bool)
- func Var(ctx context.Context, field any, tag string) (string, bool)
- type AgentLister
- type AgentTarget
Constants ¶
const MinSigningKeyLen = 32
MinSigningKeyLen is the minimum character length required for a JWT signing key, matching the output of `openssl rand -hex 32`. A short key makes brute-forcing the HS256 secret and forging tokens practical.
Variables ¶
var PackageNamePattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9.+_:~=-]*$`)
PackageNamePattern matches a package name as apt and dpkg take it, with an architecture (:amd64) or a version (=1.2-1). The first character is a letter or digit, so the package manager cannot read the name as an option.
var PackageVersionPattern = regexp.MustCompile(`^[0-9][A-Za-z0-9.+~:-]*$`)
PackageVersionPattern is a Debian package version, such as 8.5.0-2ubuntu10 or 1:2.3: a digit first, then letters, digits and . + ~ : -. It becomes part of an apt-get argument, so the provider checks it again.
var TimezonePattern = regexp.MustCompile(`^[A-Za-z0-9_+][A-Za-z0-9_+-]*(/[A-Za-z0-9_+-]+)*$`)
TimezonePattern is an IANA timezone name, such as UTC or America/Argentina/Buenos_Aires: segments of letters, digits, _, + and - joined by /, never starting with - and with no dot. It becomes an argument of timedatectl, so the provider checks it again.
Functions ¶
func AtLeastOneField ¶
AtLeastOneField checks that at least one exported pointer, slice, or map field in the struct is non-nil, or that at least one non-pointer field is non-zero. Returns an error message and false if all fields are nil/zero (i.e., the update body is empty).
func Breaks ¶ added in v0.3.0
Breaks is each rule that in breaks, as "<field>:<tag>", with the field as its Go path below in, such as "Name:max" or "Body.Target:valid_target". A method's declared bad inputs are checked against it, so each rule of an input has an input that breaks it.
func Hint ¶ added in v0.3.0
func Hint( ctx context.Context, fe validator.FieldError, ) string
Hint is what a failure of a custom validate tag means, such as what a signing key must look like, or "" for a tag without one. ctx is the request's: the valid_target hint looks up whether the agent is pending.
func InScope ¶ added in v0.3.0
InScope reports whether a token whose target scope is scope may send work to target. An empty scope covers each target. A scoped token never covers _all or _any, because those reach hosts outside the scope.
A label target, such as group:web.dev, is in the scope when a label of the scope is the same label or a prefix of it, as group:web is. A host target is in the scope when the scope names the host, or when the agent that the target resolves to has a label that a label of the scope covers.
func PendingTarget ¶
PendingTarget reports whether target names an agent, or a whole fleet, that is pending PKI enrollment, so an error message can say to accept it rather than only that the target was invalid.
It derives the answer from the target it is given. The previous version recorded it in a package-level variable during validation and read it back when formatting the message, which two concurrent requests could overwrite — telling one operator to accept the other's agent.
func RegisterTargetValidator ¶
func RegisterTargetValidator( lister AgentLister, )
RegisterTargetValidator registers the valid_target custom validator and sets the AgentLister it uses. Call this at API server startup after the job client is created, or in test SetupSuite to inject a mock.
func ResolveTarget ¶
ResolveTarget resolves a target string to a machine ID for NATS subject routing. If the target matches a hostname, the corresponding machine ID is returned. If it matches a machine ID directly, it is returned as-is. Broadcast and label targets are returned unchanged.
func ScopeEntry ¶ added in v0.3.0
ScopeEntry reports whether s can be an entry of a token's target scope: a hostname, such as web-01, or a label, such as group:web. _all, _any and _local cannot: a scope names hosts, and those name each host.
func SetEnforceVerifiedRegistrations ¶
func SetEnforceVerifiedRegistrations( enforce bool, )
SetEnforceVerifiedRegistrations turns registration verification on for target resolution. Called once at startup, when the controller has a key store to verify against.
func Struct ¶
Struct validates a struct and returns the error message and false if invalid. ctx is the request's, so a rule that reads NATS, such as valid_target, does it inside the request's trace and stops with it.
func UnverifiedTarget ¶ added in v0.3.0
UnverifiedTarget reports whether target names an agent that is registered, and that the controller does not route to because its registration is not signed by its admitted key, so an error message can say why the target is refused rather than that no such agent exists. It returns the agent's hostname.
Types ¶
type AgentLister ¶
type AgentLister func(ctx context.Context) ([]AgentTarget, error)
AgentLister returns active agents with their hostnames and labels.
type AgentTarget ¶
type AgentTarget struct {
MachineID string
Hostname string
State types.AgentState
Labels map[string]string
// Verified reports whether the registration behind this target was signed
// by the key stored for its machine ID. Only a verified target is
// resolvable: an unverified registration is not an error, it simply does
// not decide where work goes. True when the controller is not enforcing.
Verified bool
}
AgentTarget holds the routing-relevant fields of an active agent.