Documentation
¶
Overview ¶
Package policytest runs policy tests: calls, the session they are made in, and the decisions they have to get.
A test file sits next to the config and runs in CI with `agentgate test`, so that a change to a rule, a pack or a parameter cannot quietly let through what it was there to stop. Each test runs in a session of its own, through the same evaluator, session tracker and label rules the proxy uses.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultPath ¶
DefaultPath is where the tests for a config live when none are named: next to it, with .test before the extension — agentgate.test.yaml for agentgate.yaml.
func FromRecordings ¶
FromRecordings writes a test file with one test per recorded session: every call it made, in order, at the time it was made, expecting the decision the policy reached then. Run against a changed policy, it fails on exactly the calls the change would decide differently.
Types ¶
type Annotations ¶
type Annotations struct {
Title string `yaml:"title"`
ReadOnly *bool `yaml:"read_only"`
Destructive *bool `yaml:"destructive"`
Idempotent *bool `yaml:"idempotent"`
OpenWorld *bool `yaml:"open_world"`
}
Annotations is what the server says about a tool.
type Case ¶
type Case struct {
Name string `yaml:"name"`
// Host is the host that opened the session, as name or name/version.
Host string `yaml:"host"`
// At is when the calls are made; empty means now.
At string `yaml:"at"`
// Labels are the labels the session carries before its first call.
Labels []string `yaml:"labels"`
// Before are the calls the session made first, in order.
Before []Step `yaml:"before"`
// Call is the call under test.
Call *Step `yaml:"call"`
// Expect is what the call has to get.
Expect *Expect `yaml:"expect"`
// Steps is the session, call by call.
Steps []Step `yaml:"steps"`
// Line is where the test starts in its file.
Line int `yaml:"-"`
}
Case is one test. It is written in one of two forms: a call, the calls before it and what the call has to get; or steps, a whole session in which any call may carry what it has to get.
type Expect ¶
type Expect struct {
Action policy.Action `yaml:"action" json:"action"`
Rule string `yaml:"rule" json:"rule,omitempty"`
Reason string `yaml:"reason" json:"reason,omitempty"`
Labels []string `yaml:"labels" json:"labels,omitempty"`
}
Expect is what a call has to get: an action, and optionally the rule that decides, words the reason contains and labels the session carries after the call. In a file it is either just the action, or a mapping.
type File ¶
type File struct {
Tests []Case `yaml:"tests"`
// Path is where the file was read from.
Path string `yaml:"-"`
}
File is a test file.
type Options ¶
type Options struct {
Config *config.Config
// ResultLabels returns the labels agentgate itself attaches to a
// result (canary-read, injection-suspected), as the proxy would. Nil
// attaches none.
ResultLabels func(*policy.Result) []string
// Now is when a test without at runs.
Now time.Time
// Match selects the tests to run by name; nil runs every test.
Match *regexp.Regexp
}
Options control a run.
type Outcome ¶
type Outcome struct {
Name string `json:"name"`
Line int `json:"line,omitempty"`
Passed bool `json:"passed"`
// Got is the decision the last checked call got.
Got policy.Decision `json:"got"`
// Labels are the labels that call earned.
Labels []string `json:"labels,omitempty"`
// Expected is what the test asked of it.
Expected Expect `json:"expected"`
// Checked counts the calls that had something to get: one, or in a
// test written as steps, every step with an expect.
Checked int `json:"checked"`
// Problems say what did not match.
Problems []string `json:"problems,omitempty"`
}
Outcome is how one test went.
type Recording ¶
type Recording struct {
Session *audit.Session
Calls []*audit.Call
// Annotations returns what the servers said about their tools in the
// catalog a call was made under, keyed by exposed name; nil means
// nothing is known.
Annotations func(catalogHash string) map[string]policy.Annotations
}
Recording is a recorded session to turn into a test.
type Step ¶
type Step struct {
// Tool is the tool as the host sees it (fs__write_file), or as
// upstream.tool (fs.write_file).
Tool string `yaml:"tool"`
Args map[string]any `yaml:"args"`
Annotations *Annotations `yaml:"annotations"`
// Result is what the call returned, for label rules that look at it.
Result *Result `yaml:"result"`
// At is when this call is made, if not when the test's calls are.
At string `yaml:"at"`
// Repeat makes the call this many times.
Repeat int `yaml:"repeat"`
// Expect is what the call has to get; in steps only.
Expect *Expect `yaml:"expect"`
// Line is where the step starts in its file.
Line int `yaml:"-"`
}
Step is one call.