Documentation
¶
Overview ¶
Package policy asks the embedded OPA whether a tool call may go on, and at which price: at once, after one operator, or after two.
Input: the principal and a ToolCall that the server filled from its own data (the principal record, the partner record, the stored document, the payload of the effect). The client chooses none of it. Output: a Decision with the names of the rules that forbade the action (Deny) or turned it into an approval request (Fired). An error of the engine means no decision: the caller must not do the effect.
The Rego text and its data are embedded (policies/) and prepared once at start. Amounts reach the policy as whole cents, so no rounding can move a limit.
Index ¶
Constants ¶
const ( ApprovalNone = "none" ApprovalOne = "one" ApprovalTwo = "two" )
Approval levels of a decision.
Variables ¶
This section is empty.
Functions ¶
func Cents ¶
Cents reads a decimal string with at most two places ("12", "12.5", "12.50") into whole cents. ok is false for anything else. An amount above the exact range of the engine is cut to maxCents, which is above any limit.
func InvoiceLines ¶
InvoiceLines gives the lines of an invoice or of an order to a supplier in the normal form the policy compares: [sku, quantity in thousandths, unit price in cents] per line, merged by (sku, price) and sorted. Both sides are built by this one function, so the policy only tests equality.
Types ¶
type AllowAll ¶
type AllowAll struct{}
AllowAll is the policy of the mode no-policy (C3-A2): every call is allowed at once, with no tier, no approval request and no daily counter. The data checks of the tools (a SKU that is not in the price list, a missing business reference) are not policy and stay.
type Decision ¶
type Decision struct {
Allow bool `json:"allow"`
Tier int `json:"tier"`
Approval string `json:"approval"`
Deny []string `json:"deny"`
Fired []string `json:"fired"`
}
Decision is the answer of the policy.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is the prepared policy.
type Option ¶
Option changes the data the policy reads (data.json is the default).
func WithUpstreamAllow ¶
WithUpstreamAllow sets data.upstream_allow: the up.* tools each tenant may call. Without it every upstream tool is forbidden (TL-07).
type Partner ¶
type Partner struct {
Role string `json:"role"`
Active bool `json:"active"`
AutoMaxCents *int64 `json:"auto_max_cents"`
DailyMaxCents *int64 `json:"daily_max_cents"`
ForecastCents *int64 `json:"forecast_cents"`
ShipToCodes []string `json:"ship_to_codes"`
Contacts []string `json:"contacts"`
}
Partner is the partner record the policy checks the call against. A limit that is nil is not set.
type Principal ¶
type Principal struct {
ID string `json:"id"`
Kind string `json:"kind"`
TenantID string `json:"tenant"`
}
Principal is the caller as the server knows it from the principal record.
type SupplierOrder ¶
type SupplierOrder struct {
TotalCents int64 `json:"total_cents"`
Currency string `json:"currency"`
LinesNorm [][]string `json:"lines_norm"`
}
SupplierOrder is the order to a supplier an invoice is checked against. Lines are in the normal form of InvoiceLines.
type ToolCall ¶
type ToolCall struct {
Tool string
// Effect is the payload of the effect (the same value the request hash is taken
// over); it reaches the policy as input.effect, with amount_cents added.
Effect any
AmountCents int64
Partner *Partner // nil: the partner is not registered
DailyAutoCents int64
Document Document
SupplierOrder *SupplierOrder // nil: no such order
}
ToolCall is everything the policy sees about a call.