policytest

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: GPL-3.0 Imports: 15 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 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

func DefaultPath(configPath string) string

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

func FromRecordings(recs []Recording, now time.Time) (string, error)

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.

func ParseWhen

func ParseWhen(s string, now time.Time) (time.Time, error)

ParseWhen understands a few spellings of a point in time: RFC3339, a date with a time, a date, a time alone (on the day of now), or a weekday with a time (the next one from now, today included).

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.

func (*Expect) UnmarshalYAML

func (e *Expect) UnmarshalYAML(n *yaml.Node) error

UnmarshalYAML takes `expect: deny` as well as the 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.

func Load

func Load(path string) (*File, error)

Load reads and checks a test file.

func Parse

func Parse(raw []byte) (*File, error)

Parse reads and checks a test file's contents.

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.

func Run

func Run(f *File, opts Options) []Outcome

Run runs the tests of f against the policy in opts.Config.

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 Result

type Result struct {
	Text    string `yaml:"text"`
	IsError bool   `yaml:"is_error"`
}

Result is what a call returned.

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.

Jump to

Keyboard shortcuts

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