Skip to content

safekeys/policy-engine [built] ​

The policy engine decides which sidecar, principal, and process may resolve which object, for which scope, into which environment. It runs at token issuance and again as a local check in the sidecar, so a token that exists is still not sufficient if local policy forbids the request.

Status: built

Why this exists ​

A valid token proves authority was once granted; it does not prove the request in front of you is allowed now, from this host, under this injection method. Policy is the layer that expresses customer rules — this sidecar may inject env in staging but never inject-file in production — and it is where enterprise control eventually lives.

Design decisions ​

policy-at-both-points [accepted] ​

Decision: Evaluate policy at issuance and again locally at resolve.

Rationale: Issuance-time checks alone cannot express host-local constraints like which process may inject a file. Resolve-time checks alone cannot prevent unnecessary tokens existing at all. Both layers together give least privilege at each moment.

default-deny [accepted] ​

Decision: Default to deny when no rule matches.

Rationale: Allow-by-default silently fails open as configuration drifts. Deny-by-default makes an incomplete configuration safe rather than dangerous.

enterprise-features-later [accepted] ​

Decision: Keep SSO, compliance reporting, and rich policy authoring out of the MVP.

Rationale: The MVP needs a working allow/deny engine with audit, not a policy authoring UX. The richer control surface is the commercial layer.

policy-via-database-rules [accepted] ​

Decision: Evaluate policy from operator-authored rules in Postgres, rebuilt per evaluation so a change takes effect without a restart.

Rationale: Issuance-time checks alone cannot express host-local constraints like which process may inject a file; resolve-time checks alone cannot prevent unnecessary tokens existing. The engine itself is complete and default-denies, but the sidecar's local-policy checker in this MVP build allows what the issuer authorised rather than loading its own rule set — that refinement is tracked here rather than overstated as done.

Consequences: The control-plane engine is built and tested (default-deny, first-match-wins, scope narrowing, environment binding, injection-method restriction). The sidecar's resolve-time rule loading is a follow-up; until it lands, resolve-time policy is an allow for any token the issuer minted.

Date: 2026-09-22

resolve-time-rules-now-loaded [accepted] ​

Decision: The sidecar now loads real resolve-time rules from the control plane (cached, default-deny) instead of the previous hardcoded allow.

Rationale: This closes the gap this feature was marked in-progress for. The control-plane engine was always built and tested, but the sidecar's checker returned true unconditionally, so a token that existed was sufficient regardless of what local policy said — the 'a valid token is still not sufficient' half of the contract was unenforced. Rules are now fetched from the same source evaluated at issuance, so both points agree and an operator's change takes effect without a restart.

Consequences: SAFEKEYS_POLICY_FILE provides a static ruleset for air-gapped or bootstrap use; a malformed file is fatal rather than being treated as an empty (deny-all) set by accident. A control plane that cannot be reached at startup is fatal, so a misconfiguration surfaces as a boot failure rather than a silent deny-everything.

Date: 2026-09-26

policy-fail-closed-warm-cache [accepted] ​

Decision: Policy evaluation denies when rules cannot be loaded and no cache exists; a warm cache serves last-known-good.

Rationale: A checker that cannot see policy must not assume it is satisfied. But the same reasoning as the key cache applies: a brief control-plane blip should not take resolution dark, and a stale rule set is safer than a resolver that stops working.

Consequences: Tested directly: removal of a rule takes effect, and an outage with a warm cache does not deny live requests.

Date: 2026-09-26

How it works ​

Evaluate policy at issuance (evaluate-issuance-policy) ​

The control plane decides whether to mint the requested token at all.

  1. Receive — Receive a mint request with requester, object, scope, audience, and TTL.
  2. Validate — Load the applicable policy for the requester and object.
  3. Observe — Allow, or deny with a generic error.
  4. Record — The decision is recorded in the audit log.

Evaluate local policy at resolve (evaluate-local-policy) ​

The sidecar applies host-local policy before injecting.

  1. Receive — Sidecar receives a resolve request with a valid token and an injection method.
  2. Validate — Check local policy — which processes may request which injection method for which object.
  3. Observe — Allow, or deny by default if unspecified.
  4. Record — The decision, including denials, is reported to the audit log.

Guarantees ​

least-privilege-scope ​

Policy grants the narrowest scope that satisfies the request.

Acceptance criteria:

  • [ ] Scope is one of read, unwrap, inject-env, inject-file, sign
  • [ ] A broad request is denied rather than silently reduced unless a rule permits the reduction
  • [ ] No wildcard scope is granted implicitly

local-policy-default-deny ​

Absent an explicit local allow, the sidecar denies.

Acceptance criteria:

  • [ ] Undefined rules do not default to permit
  • [ ] The default is documented and tested
  • [ ] Local policy is operator-editable without redeploying the sidecar

environment-binding ​

Policy can bind resolution to a specific environment or audience.

Acceptance criteria:

  • [ ] Rules can require a matching audience or environment
  • [ ] A staging policy cannot be satisfied by a production token
  • [ ] Binding is enforced at resolve, not only at issuance

policy-decisions-audited ​

Both allow and deny decisions are recorded.

Acceptance criteria:

  • [ ] Denials are recorded with the rule that denied
  • [ ] Allows are recorded with the matched rule
  • [ ] The audit trail explains a decision without exposing the secret

Test specifications ​

undefined-rule-denies ​

Given:

  • local_policy: "empty"

When: evaluate-local-policy

Then:

  • assertion: the resolve is denied
  • assertion: the denial is recorded

Contracts verified: local-policy-default-deny

injection-method-restricted ​

Given:

  • policy_allows: "inject-env"
  • request: "inject-file"

When: evaluate-local-policy

Then:

  • assertion: the request is denied
  • assertion: the reason names the denied rule

Contracts verified: least-privilege-scope

cross-environment-policy-not-satisfied ​

Given:

  • policy_requires: "env-production"
  • token_aud: "env-staging"

When: evaluate-local-policy

Then:

  • assertion: the resolve is denied
  • assertion: policy is enforced at resolve as well as issuance

Contracts verified: environment-binding

decisions-are-audited ​

Given:

  • outcome: "deny"

When: evaluate-issuance-policy

Then:

  • assertion: an audit record exists for the policy decision
  • assertion: it identifies the denying rule

Contracts verified: policy-decisions-audited

Implementation ​

  • Primary: pkg/localpolicy/localpolicy.go
  • Test code: pkg/localpolicy/localpolicy_test.go
  • Test code status: manual

See Also ​

  • safekeys/control-plane-api
  • safekeys/sidecar-resolution
  • safekeys/audit-logging