validation

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 12 Imported by: 0

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

This section is empty.

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 Instance

func Instance() *validator.Validate

Instance returns the shared validator for registering custom validators.

func PendingTarget

func PendingTarget(
	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(
	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 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(
	v any,
) (string, bool)

Struct validates a struct and returns the error message and false if invalid.

func TargetHostname

func TargetHostname(
	hostname string,
) (string, bool)

TargetHostname validates a `{hostname}` path parameter that addresses managed machines. It accepts a literal hostname, the reserved routing values `_any` and `_all`, or a `key:value` label selector — whatever `valid_target` admits. Returns the error message and false when the value is not a usable target.

This exists because oapi-codegen generates no validate tags for path parameters in strict-server mode, an upstream limitation, so a path parameter is validated by hand in the handler.

It lives here rather than in a handler package because two packages need the same check: node-targeted domains under internal/controller/api/node and the power domain below it. It is deliberately NOT what the agent domain uses — an agent endpoint addresses one enrolled agent by its literal hostname, where a routing value would be meaningless, so that domain validates differently and says so.

func Var

func Var(
	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     string
	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.

Jump to

Keyboard shortcuts

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