Files

236 lines
8.2 KiB
Go

package access
import (
"context"
"errors"
"time"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
)
// The Cedar vocabulary every policy and every assembled entity shares. A
// policy naming an unknown type, action or attribute is refused when it is
// saved and denied at every start (the engine's validation).
const (
// Namespace prefixes every Cedar entity type the engine assembles.
Namespace = "Archie"
// EntityTypeIdentity is the principal: an Archie identity.
EntityTypeIdentity = Namespace + "::Identity"
// EntityTypeOrg is the org a resource belongs to.
EntityTypeOrg = Namespace + "::Org"
// EntityTypeWorkspace is the workspace a resource belongs to.
EntityTypeWorkspace = Namespace + "::Workspace"
// EntityTypeObject is the resource acted on. Every record -- source,
// binding, workflow, secret, task, the org itself -- is one Object
// entity carrying its kind, so a policy can scope on
// `resource.kind == Archie::Kind::"binding"` without a type per record.
EntityTypeObject = Namespace + "::Object"
// EntityTypeAction is the action entity of a request, one per Action.
EntityTypeAction = Namespace + "::Action"
// AttributeOrg is the org ID an identity or object carries.
AttributeOrg = "org"
// AttributeRole is the principal's effective role for the resource
// (workspace membership role when one exists, else its org-wide role).
AttributeRole = "role"
// AttributeKind is an object's resource kind.
AttributeKind = "kind"
// AttributeOwner is the ID of the identity that owns an object.
AttributeOwner = "owner"
// AttributeState is an object's state (binding's draft/armed, task's
// status, and so on), as the caller that assembles the request sees it.
AttributeState = "state"
)
// Principal is who acts: an identity, with the memberships that give it
// roles. The org is the one the identity serves (org.Repository's
// OrgForIdentity); Memberships carries its org-wide and workspace roles.
type Principal struct {
IdentityID identity.IdentityID
Kind identity.Kind
Org org.OrgID
Memberships []org.Membership
// Workspaces are the workspaces of Org. A principal acts only in its own
// org, so these describe the workspace of any resource it may reach.
Workspaces []org.Workspace
}
// Environment returns the environment of ws in the principal's org, empty
// when it names none.
func (p Principal) Environment(ws org.WorkspaceID) string {
for _, w := range p.Workspaces {
if w.ID == ws {
return w.Environment
}
}
return ""
}
// Role returns the principal's effective role for a resource: the role of
// its membership in the resource's workspace when it holds one, else the
// role of its org-wide membership, else the empty string (no role, which
// the shipped role policies never permit).
func (p Principal) Role(ws org.WorkspaceID) org.Role {
var orgRole org.Role
for _, m := range p.Memberships {
if m.OrgID != p.Org {
continue
}
if m.WorkspaceID == "" {
orgRole = m.Role
}
if m.WorkspaceID == ws {
return m.Role
}
}
return orgRole
}
// InstanceAdmin reports whether the principal owns the default org (org-sys):
// its owners administer the whole instance, whichever org they act in.
func (p Principal) InstanceAdmin() bool {
for _, m := range p.Memberships {
if m.OrgID == org.DefaultOrgID && m.WorkspaceID == "" && m.Role == org.RoleOwner {
return true
}
}
return false
}
// ErrNotMember refuses selecting an org the principal holds no membership in.
var ErrNotMember = errors.New("not a member of that org")
// WorkspaceLister reads an org's workspaces.
type WorkspaceLister interface {
ListWorkspaces(context.Context, org.OrgID) ([]org.Workspace, error)
}
// Select returns the principal acting in org id, which it must hold a
// membership in. Selecting the org it already serves needs no lookup. Every
// authority that honours a caller's chosen org goes through here, so the
// membership check is the single gate.
func (p Principal) Select(ctx context.Context, id org.OrgID, workspaces WorkspaceLister) (Principal, error) {
if id == "" || id == p.Org {
return p, nil
}
if !p.Member(id) {
return Principal{}, ErrNotMember
}
if workspaces == nil {
return Principal{}, errors.New("org selection unavailable")
}
list, err := workspaces.ListWorkspaces(ctx, id)
if err != nil {
return Principal{}, err
}
p.Org, p.Workspaces = id, list
return p, nil
}
// Member reports whether the principal holds any membership in org id.
func (p Principal) Member(id org.OrgID) bool {
for _, m := range p.Memberships {
if m.OrgID == id {
return true
}
}
return false
}
// Resource is the record acted on: its type, its workspace and org as
// parents, its owner and its state.
type Resource struct {
Kind ResourceKind
// ID is the record's caller-facing key: a source's path, a binding's
// ID, an org's ID, a workflow's name.
ID string
// Org is the org the record belongs to. Empty means the principal's own
// org, which is how a create request names a resource that does not
// exist yet.
Org org.OrgID
// Workspace is the workspace the record belongs to, empty for
// org-owned records.
Workspace org.WorkspaceID
Owner identity.IdentityID
State string
}
// Context is what was going on when the decision was asked for.
type Context struct {
// Signature is the event's signature result ("valid", "unsigned", ...).
Signature string
// Addr is the address the event came from.
Addr string
// Time is when the request was made; zero means now.
Time time.Time
// Run and Step identify the run and step when the principal is an agent.
Run string
Step string
}
// Decision is the outcome of one Authorize call. When the request is denied,
// Level names the level that decided and Policies the IDs of the deciding
// policies at that level, so a denial record can name them.
type Decision struct {
Allowed bool
Level Level
Policies []string
// Err carries the engine failure behind a deny-all level: a stored
// policy that no longer parses. A denied request with Err set was never
// evaluated -- the level refuses everything rather than decide.
Err error
}
// Allowed is the shared permit decision: no level denied.
func Allowed() Decision { return Decision{Allowed: true} }
// Unavailable is the refusal of a chain that has not loaded: nothing was
// evaluated, so it is an availability failure rather than a denial.
func Unavailable() Decision {
return Decision{Level: LevelInstance, Err: ErrChainUnavailable}
}
// DeniedAt records a denial decided at a level by the given policies.
func DeniedAt(level Level, policies []string) Decision {
return Decision{Level: level, Policies: policies}
}
// PrincipalSource assembles the principal for one identity: the org it
// serves and its memberships. The State Store implements it (the memberships
// live in the State Store); the dashboard and dispatch call it over the wire.
type PrincipalSource interface {
// PrincipalFor returns the assembled principal. An identity in no org
// resolves to the default org, a principal with no role, which no
// shipped policy permits.
PrincipalFor(ctx context.Context, id identity.IdentityID) (Principal, error)
}
// Denial is one recorded refusal: who was refused, doing what, to what, by
// which level and which policies.
type Denial struct {
Principal identity.IdentityID `json:"principal"`
Org org.OrgID `json:"org"`
Action Action `json:"action"`
Kind ResourceKind `json:"kind"`
ResourceID string `json:"resource"`
Level Level `json:"level"`
Policies []string `json:"policies"`
At time.Time `json:"at"`
// Count is how many identical denials this record stands for. The store
// coalesces identical denials within a minute into one row with a count.
Count int64 `json:"count"`
}
// SharedTokenOwner is the principal the single-operator shared token acts as:
// the system identity, owner of the default org, with no membership row.
func SharedTokenOwner() Principal {
return Principal{
IdentityID: identity.SystemID,
Kind: identity.KindSystem,
Org: org.DefaultOrgID,
Memberships: []org.Membership{{IdentityID: identity.SystemID, OrgID: org.DefaultOrgID, Role: org.RoleOwner}},
}
}