Documentation
¶
Overview ¶
Package policy is the control-plane PolicyProvider: static grants, optional argument predicates on MCP tools/call, and an optional sync webhook gate for connector session mint and MCP tools/call. Absent config preserves V1 open behavior; when any grant, predicate, or webhook is configured, unmatched requests default to deny.
Index ¶
- Constants
- func BindArgs(raw json.RawMessage) (parsed any, digest string, meta map[string]any)
- func BindArgsStrict(raw json.RawMessage) (parsed any, digest string, meta map[string]any, err error)
- type AuditStore
- type Auditor
- type Config
- type Engine
- func (e *Engine) CacheLen() int
- func (e *Engine) Decide(ctx context.Context, in PolicyInput) PolicyDecision
- func (e *Engine) Enabled() bool
- func (e *Engine) MatchMandatoryHITL(in PolicyInput) *MandatoryHITLRule
- func (e *Engine) ReplaceMandatoryHITL(overlays []MandatoryHITLRule)
- func (e *Engine) ReplaceOverlays(overlays []Grant)
- func (e *Engine) SetExporter(exp Exporter)
- type Exporter
- type Grant
- type MandatoryHITLRule
- type PolicyDecision
- type PolicyInput
- type Predicate
- type Provider
- type Static
- type ToolFilter
- type Webhook
- type WebhookConfig
Constants ¶
const ( StageConnectorSession = "connector.session" StageToolCall = "tool.call" StageRunCreate = "run.create" )
Stages evaluated by Decide. Connector stages use static grants; run.create skips connector matching (authz + kill switches run in the admission Gate; optional webhook still applies here).
const ( EffectAllow = "allow" EffectDeny = "deny" EffectPending = "pending" // connector HITL: Admin approve → one-shot retry )
Effects returned by Decide.
const ( ReasonPolicyDeny = "policy_deny" ReasonPolicyNoGrant = "policy_no_grant" ReasonPolicyToolDenied = "policy_tool_denied" ReasonPolicyWebhookDeny = "policy_webhook_deny" ReasonPolicyWebhookFailed = "policy_webhook_failed" ReasonPolicyDefaultDeny = "policy_default_deny" ReasonPolicyMissingBinding = "policy_missing_binding" ReasonPolicyPending = "policy_pending" ReasonPolicyAdmissionDeny = "policy_admission_deny" ReasonBreakGlass = "break_glass" ReasonKillActive = "kill_active" ReasonKillActivate = "kill_activate" ReasonKillClear = "kill_clear" ReasonAuthzDeny = "authz_deny" ReasonBudgetExceeded = "budget_exceeded" ReasonBudgetSoft = "budget_soft" ReasonBudgetAlert = "budget_alert" // soft_pct approach (under hard cap) ReasonBudgetKill = "budget_kill" // cancel_inflight on hard breach ReasonUsageUnpriced = "usage_unpriced" // tokens > 0, no cost_usd, non-empty pricebook missing this model ReasonUsageUnmetered = "usage_unmetered" // runner saw an AI reply but extracted zero usage in any recognized shape )
Stable reason codes for denials / failures.
const ReasonMandatoryHITL = "mandatory_hitl"
ReasonMandatoryHITL is returned when a config mandatory_hitl rule forces tool.call to pending despite an underlying allow.
const ReasonPolicyPredicate = "policy_predicate"
ReasonPolicyPredicate is the default reason_code when a predicate denies and the config row omitted reason_code. Pending with an empty code uses ReasonPolicyPending so Admin HITL stays on the existing path.
Variables ¶
This section is empty.
Functions ¶
func BindArgs ¶
BindArgs turns MCP tools/call params.arguments into the three PolicyInput fields: full value for predicate eval, SHA-256 digest for the decision cache, and a size-capped redacted map for audit/webhook display.
Missing or empty arguments digest as the JSON object {}. The digest is a versioned sha256 of sorted-key JSON with exact normalized numbers. This is not RFC 8785. Secret-looking keys are stripped from the display map only; they still feed the digest.
func BindArgsStrict ¶
func BindArgsStrict(raw json.RawMessage) (parsed any, digest string, meta map[string]any, err error)
BindArgsStrict rejects ambiguous arguments instead of authorizing a different interpretation from the downstream service. HTTP callers must handle its error.
Types ¶
type AuditStore ¶
type AuditStore interface {
WriteAuditEvent(ctx context.Context, ev *models.AuditEvent) error
}
AuditStore is the narrow write path for policy decisions. Implemented by SQL state stores (Postgres/MySQL/SQLite); Mongo omits it.
type Auditor ¶
type Auditor interface {
WritePolicyDecision(ctx context.Context, in PolicyInput, dec PolicyDecision) error
}
Auditor persists a decision. Nil/no-op is fine (Compatible backends).
func NewStoreAuditor ¶
func NewStoreAuditor(store AuditStore) Auditor
NewStoreAuditor wraps a durable AuditStore.
type Config ¶
type Config struct {
DefaultEffect string // "allow" | "deny"; default "deny" when enabled
FailClosed *bool // webhook errors; default true
CacheTTL time.Duration
Grants []Grant // deployment defaults (baseline)
Overlays []Grant // DB grants loaded at startup
// MandatoryHITL forces matching tool.call allows to pending (config-only).
MandatoryHITL []MandatoryHITLRule
// Predicates deny or pending a tools/call from argument values.
// They never allow; invalid rows are skipped at New.
Predicates []Predicate
Webhook *WebhookConfig
Auditor Auditor
Exporter Exporter
// ForceEnable builds an engine even with no grants/webhook (empty
// "policy": {} for Admin-API-only grant management).
ForceEnable bool
}
Config builds an Engine from langgraph.json's policy section.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine chains static grants then optional webhook. First deny wins. When Enabled() is false, callers must skip Decide (V1 open).
func (*Engine) CacheLen ¶
CacheLen is the current decision-cache occupancy (tests / ops). 0 when caching is off.
func (*Engine) Decide ¶
func (e *Engine) Decide(ctx context.Context, in PolicyInput) PolicyDecision
Decide evaluates static grants, then argument predicates, then webhook. Call only when Enabled(). Every call (including cache hits) emits an OTel span event, writes audit when configured, and fans out to Exporter (SIEM) when set.
func (*Engine) Enabled ¶
Enabled reports whether the policy engine is attached. An empty ForceEnable engine (no grants/webhook/predicates yet) is still enabled — Decide applies default_effect until Admin overlays or config grants appear.
func (*Engine) MatchMandatoryHITL ¶
func (e *Engine) MatchMandatoryHITL(in PolicyInput) *MandatoryHITLRule
MatchMandatoryHITL reports whether a rule would force pending for this input (observability / break-glass audit). Nil when none match.
func (*Engine) ReplaceMandatoryHITL ¶
func (e *Engine) ReplaceMandatoryHITL(overlays []MandatoryHITLRule)
ReplaceMandatoryHITL swaps durable SQL mandatory-HITL overlays and rebuilds the active rule list. Overlay rows win over baseline on (tenant_id, agent_id, connector).
func (*Engine) ReplaceOverlays ¶
ReplaceOverlays swaps durable DB grants and rebuilds the static layer. Overlay rows win over baseline on (tenant_id, agent_id, connector).
func (*Engine) SetExporter ¶
SetExporter attaches (or clears) the async SIEM/export sink after New.
type Exporter ¶
type Exporter interface {
ExportPolicyDecision(ctx context.Context, in PolicyInput, dec PolicyDecision)
}
Exporter fans a decision out asynchronously (SIEM webhook, etc.). Must not block; nil is a no-op.
type Grant ¶
type Grant struct {
ID string `json:"id,omitempty"`
TenantID string `json:"tenant_id"`
AgentID string `json:"agent_id"`
Connector string `json:"connector"` // connector name
Tools *ToolFilter `json:"tools,omitempty"`
}
Grant is one connector grant from langgraph.json policy.grants.
type MandatoryHITLRule ¶
type MandatoryHITLRule struct {
ID string `json:"id,omitempty"`
TenantID string `json:"tenant_id"`
AgentID string `json:"agent_id,omitempty"` // empty = whole tenant
Connector string `json:"connector"`
Tools []string `json:"tools,omitempty"` // empty = all tools
}
MandatoryHITLRule forces connector tool.call decisions to pending when they would otherwise allow. Empty AgentID matches every agent in the tenant; empty Tools matches every tool on the connector. Hard deny from Decide is never elevated to pending.
type PolicyDecision ¶
type PolicyDecision struct {
Effect string
Reason string
ReasonCode string
RuleID string
LatencyMs int
}
PolicyDecision is the allow/deny answer for one Decide call.
type PolicyInput ¶
type PolicyInput struct {
Stage string
TenantID string
Principal string
AgentID string
RunID string
Generation int64
Connector string
Tool string
// Args is the full parsed tools/call arguments (predicate eval).
// Secret-looking keys stay here; ArgsMeta is the redacted display copy.
Args any
ArgsMeta map[string]any
ArgsDigest string
Claims map[string]any
Labels map[string]string
}
PolicyInput is the decision context for one gate check.
type Predicate ¶
type Predicate struct {
ID string
TenantID string
AgentID string
Connector string
Tool string
Path string
Op string
Value any
Effect string
Reason string
ReasonCode string
}
Predicate is one config-declared argument check. Effect is deny or pending only — predicates never grant access. Empty AgentID matches the whole tenant; empty Tool matches every tool on the connector. Missing When.Path does not match (not fail-closed).
func ValidatePredicate ¶
ValidatePredicate returns a normalized copy or an error if the row cannot be enforced. Callers skip invalid rows rather than fail-open.
type Provider ¶
type Provider interface {
Decide(ctx context.Context, in PolicyInput) (PolicyDecision, error)
}
Provider evaluates one policy layer.
type Static ¶
type Static struct {
// contains filtered or unexported fields
}
Static evaluates in-process grants. Unmatched → Deny (caller applies default_effect only when no static layer exists).
func (*Static) Decide ¶
func (s *Static) Decide(_ context.Context, in PolicyInput) (PolicyDecision, error)
Decide implements Provider for static grants.
type ToolFilter ¶
type ToolFilter struct {
Allow []string `json:"allow,omitempty"`
Deny []string `json:"deny,omitempty"`
}
ToolFilter mirrors connector.ToolFilter semantics for per-grant tools.
type Webhook ¶
type Webhook struct {
// contains filtered or unexported fields
}
Webhook POSTs a decision request and parses allow/deny.
func NewWebhook ¶
func NewWebhook(cfg WebhookConfig) *Webhook
NewWebhook builds a webhook provider. Timeout defaults to hooks.DefaultPreflightTimeout.
func (*Webhook) Decide ¶
func (w *Webhook) Decide(ctx context.Context, in PolicyInput, failClosed bool) PolicyDecision
Decide calls the webhook. failClosed=true → errors become deny.