validation

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 17 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 validation provides a shared validator instance.

Index

Constants

View Source
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

View Source
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.

View Source
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.

View Source
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

func AtLeastOneField(
	v any,
) (string, bool)

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

func Breaks(
	in any,
) (out []string)

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

func InScope(
	ctx context.Context,
	target string,
	scope []string,
) bool

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 Instance

func Instance() *validator.Validate

Instance returns the shared validator for registering custom validators.

func PendingTarget

func PendingTarget(
	ctx context.Context,
	target string,
) (string, bool)

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

func ResolveTarget(
	ctx context.Context,
	target string,
) string

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

func ScopeEntry(
	s string,
) bool

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

func Struct(
	ctx context.Context,
	v any,
) (string, bool)

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

func UnverifiedTarget(
	ctx context.Context,
	target string,
) (string, bool)

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.

func Var

func Var(
	ctx context.Context,
	field any,
	tag string,
) (string, bool)

Var validates a single variable against a tag string and returns the error message and false if invalid. This is useful for path parameters that oapi-codegen does not generate validate tags for in strict-server mode.

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.

Directories

Path Synopsis
Package rule is what a validate tag means in a spec.
Package rule is what a validate tag means in a spec.

Jump to

Keyboard shortcuts

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