policy

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package policy allows, checks, and confirms. A destructive call waits for a stored approval.

Index

Constants

View Source
const (
	// StatusPending is a recorded call that has not been approved.
	StatusPending = "pending"
	// StatusApproved is a yes that ConsumeFor accepts once.
	StatusApproved = "approved"
	// StatusConsumed is a yes that already ran.
	StatusConsumed = "consumed"
)

Variables

View Source
var ErrPendingApproval = errors.New("pending id does not run the call")

ErrPendingApproval is a pending id submitted as if it were already approved.

View Source
var ErrUnknownApproval = errors.New("unknown approval")

ErrUnknownApproval is an id approve cannot find, or one that is already consumed or expired.

Functions

func ConfirmAsk added in v0.1.5

func ConfirmAsk(caller, operationID string, params map[string]string) string

ConfirmAsk is the confirmation form. The call does not run until accept.

func ConfirmSentence

func ConfirmSentence(operationID string, params map[string]string) string

ConfirmSentence is the text shown for a held call.

func DefaultApprovalDir

func DefaultApprovalDir() (string, error)

DefaultApprovalDir is the user-config path serve and approve share.

func WithInput

func WithInput(ctx context.Context, in Input) context.Context

WithInput stores the call under policy.

Types

type Around

type Around func(ctx context.Context, op *catalog.Operation) (Decision, bool, error)

Around may stop before the next hook. Stop plus deny wins.

type Builtin

type Builtin struct {
	Caller string
	Allow  map[string]bool
}

Allow nil permits every declared permission.

func (Builtin) Check

func (b Builtin) Check(ctx context.Context, op *catalog.Operation) (Decision, error)

type CommandNotifier

type CommandNotifier struct {
	// contains filtered or unexported fields
}

func (*CommandNotifier) Pending

func (c *CommandNotifier) Pending(ctx context.Context, notice Notice) error

type Decision

type Decision string

Decision is allow, deny, or confirmation required.

const (
	// DecisionAllow permits the call.
	DecisionAllow Decision = "allow"
	// DecisionDeny rejects the call.
	DecisionDeny Decision = "deny"
	// DecisionConfirmationNeeded holds the call until an approval is stored.
	DecisionConfirmationNeeded Decision = "confirmation_required"
)

func Check

func Check(ctx context.Context, hook Hook, op *catalog.Operation) (Decision, error)

Check runs hook. Nil hook is Builtin.

func Combine added in v0.1.1

func Combine(a, b Decision, op *catalog.Operation) Decision

Combine returns the stricter of a and b. A required confirmation upgrades an allow.

type Files added in v0.1.1

type Files struct {
	Memory
	Dir string
}

Files keeps records as JSON under Dir/confirmations.

func (*Files) Claim added in v0.1.1

func (f *Files) Claim(ctx context.Context, id string) (bool, error)

func (*Files) FindApproved added in v0.1.1

func (f *Files) FindApproved(ctx context.Context, id string) (Record, bool, error)

func (*Files) Get added in v0.1.1

func (f *Files) Get(ctx context.Context, id string) (Record, bool, error)

func (*Files) Put added in v0.1.1

func (f *Files) Put(ctx context.Context, rec Record) error

func (*Files) Remove added in v0.1.1

func (f *Files) Remove(ctx context.Context, rec Record)

type HTTPNotifier

type HTTPNotifier struct {
	// contains filtered or unexported fields
}

func (*HTTPNotifier) Pending

func (h *HTTPNotifier) Pending(ctx context.Context, notice Notice) error

type Hook

type Hook interface {
	Check(ctx context.Context, op *catalog.Operation) (Decision, error)
}

Hook decides one operation.

type Input

type Input struct {
	Params    map[string]string
	Arguments map[string]any
	Caller    string
}

Input is the call under policy. Caller is the caller or tenant.

func InputFrom

func InputFrom(ctx context.Context) Input

InputFrom reads the call stored by WithInput.

type Memory added in v0.1.1

type Memory struct {
	// contains filtered or unexported fields
}

Memory keeps records in this process.

func (*Memory) Claim added in v0.1.1

func (m *Memory) Claim(ctx context.Context, id string) (bool, error)

func (*Memory) FindApproved added in v0.1.1

func (m *Memory) FindApproved(ctx context.Context, id string) (Record, bool, error)

func (*Memory) Get added in v0.1.1

func (m *Memory) Get(ctx context.Context, id string) (Record, bool, error)

func (*Memory) Put added in v0.1.1

func (m *Memory) Put(ctx context.Context, rec Record) error

func (*Memory) Remove added in v0.1.1

func (m *Memory) Remove(ctx context.Context, rec Record)

type Notice

type Notice struct {
	ID        string
	Operation string
	Caller    string
}

Notice is a pending call. It carries no parameters and no credentials.

type Notifier

type Notifier interface {
	Pending(ctx context.Context, notice Notice) error
}

Notifier is told when a call is pending. It does not approve the call.

func NewWebhook

func NewWebhook(rawURL string, command []string) (Notifier, error)

type PendingConfirmation

type PendingConfirmation struct {
	ID          string            `json:"ID"`
	OperationID string            `json:"OperationID"`
	Params      map[string]string `json:"Params"`
	Caller      string            `json:"Caller,omitempty"`
}

PendingConfirmation is a held call. The id does not authorize HTTP.

type Record added in v0.1.1

type Record struct {
	ID          string            `json:"id"`
	OperationID string            `json:"operation_id"`
	Params      map[string]string `json:"params,omitempty"`
	Status      string            `json:"status"`
	Expiry      int64             `json:"expiry,omitempty"`
	ApprovedID  string            `json:"approved_id,omitempty"`
	Caller      string            `json:"caller,omitempty"`
}

Record is one confirmation.

type State

type State struct {
	// contains filtered or unexported fields
}

State is the confirmation use case. Memory is the store. SetStore, SetNonceDir, and SetSigner attach adapters.

func NewState

func NewState() *State

NewState uses Memory.

func (*State) Approve

func (s *State) Approve(ctx context.Context, id string) (string, error)

Approve records a separate decision. The returned id is what a later invoke accepts once.

func (*State) ConsumeFor

func (s *State) ConsumeFor(ctx context.Context, caller, approvalID, opID string, params map[string]string) (bool, error)

ConsumeFor accepts an approved id once, and only for the caller that received it.

func (*State) Open added in v0.1.5

func (s *State) Open(ctx context.Context, opt StoreOptions) error

Open points state at the store, files dir, and signer. Empty URL keeps Files or Memory.

func (*State) Pending

func (s *State) Pending(ctx context.Context, id string) *PendingConfirmation

func (*State) RequestFor

func (s *State) RequestFor(ctx context.Context, caller, opID string, params map[string]string) (string, error)

RequestFor records a pending call for one caller. The id it returns does not authorize HTTP. A store error means the file was not written, so there is no pending id to approve.

func (*State) SetNonceDir

func (s *State) SetNonceDir(dir string)

SetNonceDir uses Files in dir.

func (*State) SetSigner

func (s *State) SetSigner(secret []byte, ttl time.Duration) error

SetSigner attaches HMAC. Empty secret is an error. Unset ttl is 15 minutes.

func (*State) SetStore added in v0.1.1

func (s *State) SetStore(store Store)

SetStore replaces the confirmation store. Nil uses Memory.

type Store added in v0.1.1

type Store interface {
	Put(ctx context.Context, rec Record) error
	Get(ctx context.Context, id string) (Record, bool, error)
	FindApproved(ctx context.Context, approvedID string) (Record, bool, error)
	Claim(ctx context.Context, id string) (bool, error)
	Remove(ctx context.Context, rec Record)
}

Store persists confirmation records. Memory is the default. Files is one machine. Claim is consume-once. valkey is the replica adapter: SET NX on the pending id.

type StoreOptions added in v0.1.5

type StoreOptions struct {
	URL    string
	Dir    string
	Secret []byte
	TTL    time.Duration
	Dial   func(context.Context, string) (Store, error)
}

StoreOptions attaches a store, a files dir, and a signer. Dial opens a URL.

type Wrapped

type Wrapped struct {
	// contains filtered or unexported fields
}

Wrapped is next with Around in front.

func Wrap

func Wrap(next Hook, around Around) Wrapped

Wrap puts around in front of next. Nil next is Builtin.

func (Wrapped) Check

func (w Wrapped) Check(ctx context.Context, op *catalog.Operation) (Decision, error)

Jump to

Keyboard shortcuts

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