* fix(postgres): scope OpenPRs to the caller's org A principal-carrying caller read every org's pr_open tasks. The query now honours the request scope; the daemon's unscoped review scan still sees all orgs. Issue: archie-core-kgxw.23 * fix(statestore): serve task logs only to the task's org ReadTaskLog and StreamTaskLogContent took a task ID and read the file with no org check. The State Store's task-log reader now confirms the task is visible under the caller's scope first; unscoped services read as before. Issue: archie-core-kgxw.24 * fix(postgres): scope ListPolicies to the caller's org Every org's policy text reached any principal-carrying caller. A scoped read now sees its own org's policies, plus instance policies when scoped to the system org. The engines and the lockout guard decide for every org, so they read the full chain unscoped: the dashboard engine loads without the request's principal and the guard lifts the caller's scope. Issue: archie-core-kgxw.25 * fix(postgres): scope identity reads to the caller's org ListIdentities and GetIdentity (and its alias fallback) returned every identity to any caller. A caller scoped to an org now sees only that org's members and assigned agents; the system org and unscoped services still see the whole instance. Issue: archie-core-kgxw.26 * fix(postgres): hide instance status from other orgs ListPresence, ListChannelStatus, ListApplyStatus and GetConfigSnapshot answered any org principal with instance-wide state. They now return nothing unless the request is unscoped or scoped to the system org. The dashboard reads the published config as itself, since its handlers project it for callers of every org. Issue: archie-core-kgxw.27 * feat(postgres): assign new agents an org and role at creation A bot or service account created through the API served no org until the next State Store boot granted it one. Create now assigns it to the org the request acts in with the developer role in the same transaction, so it is authorized immediately. Issue: archie-core-kgxw.14 * feat(edastore): create event records in a chosen workspace Sources are created in a workspace of the caller's org, the default one when none is named; event types and bindings take their source's workspace and mappings their event type's, the same rule the org backfill applied. The binding, mapping and event type contracts carry the workspace, the four list endpoints accept ?workspace=, and the Events page has a workspace selector that filters every step and picks where a new source goes. Issue: archie-core-kgxw.11 * feat(webui): authorize a record in its workspace The API built every access request with no workspace, so workspace policies and per-workspace roles never applied. A request naming a source, binding, mapping, event type or task now reads the record's workspace under the caller's scope and authorizes in it; tasks carry their workspace on the State Store contract for this. Issue: archie-core-kgxw.9 * feat(access): expose workspace environment to policies Policies can test resource.workspace.environment. The workspace attribute is the workspace entity, which carries its environment; the principal brings its org's workspaces from the State Store, so the dashboard and the daemon's run checks describe a resource's workspace without another lookup, and the daemon now authorizes runs in the task's or binding's workspace. Issue: archie-core-kgxw.10 * fix(webui): ask read_secret when a source secret is read Rotating a source's secret returns its value, but the route asked update, which every developer holds, so no policy could narrow who reads a secret. It now asks read_secret on the secret, which the shipped roles give admins and an object policy can narrow further. Issue: archie-core-kgxw.17 * refactor(org): delete the unused upgrade-incomplete sentinel Nothing returns org.ErrUpgradeIncomplete: the State Store finishes the org upgrade before it serves. Issue: archie-core-kgxw.18
195 lines
7.0 KiB
Go
195 lines
7.0 KiB
Go
package access
|
|
|
|
import (
|
|
"context"
|
|
"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
|
|
}
|
|
|
|
// 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}},
|
|
}
|
|
}
|