Documentation
¶
Overview ¶
Package validation provides a shared validator instance.
Index ¶
- Constants
- func AtLeastOneField(v any) (string, bool)
- func Instance() *validator.Validate
- func PendingTarget(target string) (string, bool)
- func RegisterTargetValidator(lister AgentLister)
- func ResolveTarget(target string) string
- func SetEnforceVerifiedRegistrations(enforce bool)
- func Struct(v any) (string, bool)
- func TargetHostname(hostname string) (string, bool)
- func Var(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 ¶
This section is empty.
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 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 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 TargetHostname ¶
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.
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.