delegation

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package delegation provides delegation chain management for agent principals. Delegation allows principals to authorize other principals (particularly agents) to act on their behalf with constrained capabilities.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned for an unknown principal.
	ErrNotFound = errors.New("delegation: principal not found")
	// ErrNotAgent is returned when the delegate is not an agent.
	ErrNotAgent = errors.New("delegation: delegate must be an agent principal")
	// ErrCannotDelegate is returned when the delegator lacks the
	// can_delegate capability or is not active.
	ErrCannotDelegate = errors.New("delegation: delegator cannot delegate")
	// ErrSelfDelegation is returned when a principal delegates to itself.
	ErrSelfDelegation = errors.New("delegation: a principal cannot delegate to itself")
	// ErrCircularDelegation is returned when a delegation would make the
	// delegate its own ancestor.
	ErrCircularDelegation = errors.New("delegation: circular delegation")
	// ErrDelegatorMismatch is returned when revoking a delegation the
	// principal did not grant.
	ErrDelegatorMismatch = errors.New("delegation: delegator mismatch")
)

Functions

This section is empty.

Types

type ActionValidation

type ActionValidation struct {
	Allowed      bool   `json:"allowed"`
	Reason       string `json:"reason,omitempty"`
	Confirmation bool   `json:"confirmation_required"`
}

ActionValidation represents the result of validating an action.

type Chain

type Chain struct {
	// Links ordered from root (human/service) to current (typically agent)
	Links []Link `json:"links"`
	// Effective constraints computed by intersecting all links
	EffectiveConstraints Constraints `json:"effective_constraints"`
}

Chain represents a full delegation chain from root to current principal.

func (*Chain) Current

func (c *Chain) Current() *Link

Current returns the current (last) principal in the chain.

func (*Chain) Depth

func (c *Chain) Depth() int

Depth returns the delegation depth.

func (*Chain) Root

func (c *Chain) Root() *Link

Root returns the root principal in the chain.

type Constraints

type Constraints struct {
	// AllowedCapabilities lists capabilities the delegate can use.
	// Empty means all capabilities of the parent are available.
	AllowedCapabilities []string `json:"allowed_capabilities,omitempty"`

	// AllowedScopes lists scopes the delegate can request.
	// Empty means all scopes of the parent are available.
	AllowedScopes []string `json:"allowed_scopes,omitempty"`

	// AllowedResources lists resource patterns the delegate can access.
	// Supports glob patterns like "project/*" or "org/123/*"
	AllowedResources []string `json:"allowed_resources,omitempty"`

	// AllowedActions lists actions the delegate can perform.
	// Example: ["read", "write"], ["*"]
	AllowedActions []string `json:"allowed_actions,omitempty"`

	// MaxTokenLifetime limits token duration for the delegate.
	MaxTokenLifetime time.Duration `json:"max_token_lifetime,omitempty"`

	// RequiresConfirmation indicates actions need human approval.
	RequiresConfirmation bool `json:"requires_confirmation"`

	// ExpiresAt is when the delegation expires.
	ExpiresAt *time.Time `json:"expires_at,omitempty"`
}

Constraints defines what a delegated principal can do. In a delegation request, an empty list leaves that dimension unrestricted by the delegation. In a chain's effective constraints, a nil list is unrestricted and an empty, non-nil list allows nothing, which is what constraints that do not overlap leave.

type CreateInput

type CreateInput struct {
	// DelegatorID is the principal granting the delegation.
	DelegatorID uuid.UUID

	// DelegateID is the principal receiving the delegation.
	DelegateID uuid.UUID

	// Constraints on what the delegate can do.
	Constraints Constraints

	// Reason for the delegation (audit).
	Reason string
}

CreateInput contains fields for creating a delegation.

type DefaultService

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

DefaultService implements the Service interface.

func (*DefaultService) ComputeEffectiveConstraints

func (s *DefaultService) ComputeEffectiveConstraints(chain *Chain) Constraints

ComputeEffectiveConstraints computes the effective constraints by intersecting constraints through the delegation chain.

func (*DefaultService) CreateDelegation

func (s *DefaultService) CreateDelegation(ctx context.Context, input CreateInput) (*Link, error)

CreateDelegation makes an agent act for a delegator under constraints, replacing any delegation the agent had. Every constraint is stored and enforced by ValidateAction.

func (*DefaultService) GetChain

func (s *DefaultService) GetChain(ctx context.Context, principalID uuid.UUID) (*Chain, error)

GetChain retrieves the delegation chain ending at a principal, from its root to the principal.

func (*DefaultService) ListDelegations

func (s *DefaultService) ListDelegations(ctx context.Context, delegatorID uuid.UUID) ([]*Link, error)

ListDelegations lists all delegations granted by a principal.

func (*DefaultService) RevokeDelegation

func (s *DefaultService) RevokeDelegation(ctx context.Context, delegatorID, delegateID uuid.UUID, reason string) error

RevokeDelegation ends an agent's delegation. The agent then acts for no one.

func (*DefaultService) ValidateAction

func (s *DefaultService) ValidateAction(ctx context.Context, principalID uuid.UUID, action, resource string) (*ActionValidation, error)

ValidateAction checks whether a principal may perform an action on a resource. A principal that is not an agent acts for itself. An agent acts only through a delegation from an active root, with every principal in its chain active, and within the chain's effective constraints.

type Link struct {
	PrincipalID   uuid.UUID      `json:"principal_id"`
	PrincipalType principal.Type `json:"principal_type"`
	DisplayName   string         `json:"display_name"`
	Constraints   Constraints    `json:"constraints"`
	GrantedAt     time.Time      `json:"granted_at"`
	// Active reports whether the principal is active; a chain with an
	// inactive principal authorizes nothing.
	Active bool `json:"active"`
}

Link represents a single link in a delegation chain.

type Service

type Service interface {
	// CreateDelegation creates a delegation from one principal to another.
	CreateDelegation(ctx context.Context, input CreateInput) (*Link, error)

	// GetChain retrieves the full delegation chain for a principal.
	GetChain(ctx context.Context, principalID uuid.UUID) (*Chain, error)

	// ValidateAction checks if a principal can perform an action on a resource.
	ValidateAction(ctx context.Context, principalID uuid.UUID, action, resource string) (*ActionValidation, error)

	// RevokeDelegation revokes a delegation.
	RevokeDelegation(ctx context.Context, delegatorID, delegateID uuid.UUID, reason string) error

	// ListDelegations lists all delegations granted by a principal.
	ListDelegations(ctx context.Context, delegatorID uuid.UUID) ([]*Link, error)

	// ComputeEffectiveConstraints computes the effective constraints
	// by intersecting constraints through the delegation chain.
	ComputeEffectiveConstraints(chain *Chain) Constraints
}

Service defines the business logic interface for delegation.

func NewService

func NewService(client *ent.Client) Service

NewService creates a new DelegationService.

Jump to

Keyboard shortcuts

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