package connectors import ( "context" "net/http" "slices" "strings" "github.com/danielgtaylor/huma/v2" "github.com/google/uuid" "github.com/samcharles93/archie-core/modules/connectors/internal/store" "github.com/samcharles93/archie-core/modules/governance" ) // Scope is who a request acts for: a person in an organisation. A background // job acts for an organisation alone, so it never reaches a person's // installations or credentials. type Scope struct { OrgID string PersonID string } type scopeKey struct{} // WithScope makes every installation and credential lookup made with ctx act // for s. func WithScope(ctx context.Context, s Scope) context.Context { return context.WithValue(ctx, scopeKey{}, s) } // ScopeFromContext returns the scope ctx acts for: the one WithScope set, or // the organisation and the user the request is authenticated as. func ScopeFromContext(ctx context.Context) Scope { if s, ok := ctx.Value(scopeKey{}).(Scope); ok { return s } s := Scope{OrgID: string(governance.OrgFromContext(ctx))} if p, ok := governance.PrincipalFromContext(ctx); ok && p.Kind == governance.KindUser { s.PersonID = string(p.IdentityID) } return s } // Scopes an installation can be owned by. const ( ScopeInstance = store.ScopeInstance ScopeOrg = store.ScopeOrg ScopePerson = store.ScopePerson ) // Installation identifies the installation a tool, a guard or a credential // came from, so a decision can record which configuration acted. type Installation struct { ID uuid.UUID `json:"id"` Scope string `json:"scope" enum:"instance,org,person"` } // owners lists the owners whose installations and credentials s may use, most // specific first. func (s Scope) owners() []store.Owner { var out []store.Owner if s.PersonID != "" { out = append(out, store.Owner{Scope: store.ScopePerson, PersonID: s.PersonID}) } if s.OrgID != "" { out = append(out, store.Owner{Scope: store.ScopeOrg, OrgID: s.OrgID}) } return append(out, store.Owner{Scope: store.ScopeInstance}) } // scopeOf is the scope an installation or a credential owner acts for when it // looks up the credentials and engines it is configured with. A person's // installation reaches the person's credentials, then the instance's. func scopeOf(o store.Owner) Scope { return Scope{OrgID: o.OrgID, PersonID: o.PersonID} } // member is what resolution needs to know of one installation. type member struct { id string name string owner store.Owner contract string enabled bool } type override struct { id string owner store.Owner } // index resolves names to installations. It is read-only: the module builds a // new one whenever an installation or an override changes. type index struct { members map[string]member overrides map[override]struct{} } func newIndex(installed []store.Installed, overrides []store.Override) index { ix := index{members: make(map[string]member, len(installed)), overrides: make(map[override]struct{}, len(overrides))} for _, a := range installed { ix.members[a.ID.String()] = member{ id: a.ID.String(), name: a.Name, owner: a.Owner(), contract: a.Contract, enabled: a.Enabled, } } for _, o := range overrides { ix.overrides[override{id: o.InstallationID.String(), owner: store.Owner{Scope: o.Scope, OrgID: o.OrgID, PersonID: o.PersonID}}] = struct{}{} } return ix } // usable reports whether s may use m: m is owned by s's person or // organisation or by the instance, and no override of s's switches it off. func (ix index) usable(s Scope, m member) bool { if !m.enabled { return false } switch m.owner.Scope { case store.ScopePerson: return s.PersonID != "" && m.owner.PersonID == s.PersonID case store.ScopeOrg: return s.OrgID != "" && m.owner.OrgID == s.OrgID && !ix.hidden(s, m) } return !ix.hidden(s, m) } // hidden reports whether an organisation or a person switched m off for s. func (ix index) hidden(s Scope, m member) bool { return len(ix.overriddenBy(s, m)) > 0 } // overriddenBy lists the scopes of s that switched m off, person first. func (ix index) overriddenBy(s Scope, m member) []string { var out []string if s.PersonID != "" { if _, off := ix.overrides[override{id: m.id, owner: store.Owner{Scope: store.ScopePerson, PersonID: s.PersonID}}]; off { out = append(out, store.ScopePerson) } } if s.OrgID != "" { if _, off := ix.overrides[override{id: m.id, owner: store.Owner{Scope: store.ScopeOrg, OrgID: s.OrgID}}]; off { out = append(out, store.ScopeOrg) } } return out } // resolve finds the installation ref names for s. An installation ID wins when // s may use it. A name resolves to the person's installation, then the // organisation's, then the instance's. func (ix index) resolve(s Scope, ref string) (member, bool) { if _, err := uuid.Parse(ref); err == nil { m, ok := ix.members[ref] return m, ok && ix.usable(s, m) } for _, o := range s.owners() { for _, m := range ix.members { if m.name == ref && m.owner == o && ix.usable(s, m) { return m, true } } } return member{}, false } // visible lists what each name resolves to for s, in name order. func (ix index) visible(s Scope) []member { seen := map[string]bool{} var out []member for _, o := range s.owners() { for _, m := range ix.members { if m.owner == o && !seen[m.name] && ix.usable(s, m) { seen[m.name] = true out = append(out, m) } } } slices.SortFunc(out, func(a, b member) int { return strings.Compare(a.name, b.name) }) return out } // authority is what a caller may manage and read. type authority struct{ p governance.Principal } func authorityOf(ctx context.Context) (authority, error) { p, ok := governance.PrincipalFromContext(ctx) if !ok { return authority{}, huma.NewError(http.StatusUnauthorized, "sign in to manage installations") } return authority{p}, nil } // manages reports whether the caller may change what o owns: an instance // administrator for the instance, an organisation owner for that organisation, // and a person for their own. func (a authority) manages(o store.Owner) bool { switch o.Scope { case store.ScopeInstance: return a.p.InstanceAdmin() case store.ScopeOrg: return a.p.Kind == governance.KindUser && o.OrgID == string(a.p.Org) && a.p.RoleIn(a.p.Org) == governance.RoleOwner case store.ScopePerson: return a.p.Kind == governance.KindUser && a.p.IdentityID != "" && o.PersonID == string(a.p.IdentityID) } return false } // sees reports whether the caller may list what o owns: everything the // instance owns, the caller's own organisation's, and the caller's own. func (a authority) sees(o store.Owner) bool { switch o.Scope { case store.ScopeInstance: return true case store.ScopeOrg: return o.OrgID == string(a.p.Org) && a.p.Member(a.p.Org) case store.ScopePerson: return a.manages(o) } return false } // ownerAt returns the owner the caller means by naming scope: the caller's own // organisation or person, or the instance. It fails when the caller may not // manage what that owner holds. func (a authority) ownerAt(scope string) (store.Owner, error) { var o store.Owner switch scope { case store.ScopeInstance: o = store.Owner{Scope: scope} case store.ScopeOrg: o = store.Owner{Scope: scope, OrgID: string(a.p.Org)} case store.ScopePerson: o = store.Owner{Scope: scope, PersonID: string(a.p.IdentityID)} default: return store.Owner{}, huma.NewError(http.StatusBadRequest, "scope is instance, org or person") } if !a.manages(o) { return store.Owner{}, huma.NewError(http.StatusForbidden, "you may not change "+scope+" installations") } return o, nil } // overrideScopes lists the scopes the caller may switch installation o off at. // An organisation may switch off an instance installation and a person may // switch off one owned by the instance or their organisation, which are the // scopes they inherit. The installation's own scope and narrower ones are not // inherited, so they cannot override it. func overrideScopes(a authority, o store.Owner) []string { var out []string if a.p.Kind == governance.KindUser && a.p.RoleIn(a.p.Org) == governance.RoleOwner && o.Scope == store.ScopeInstance { out = append(out, store.ScopeOrg) } if a.p.Kind == governance.KindUser && a.p.IdentityID != "" && o.Scope != store.ScopePerson { out = append(out, store.ScopePerson) } return out } // pickInstalled finds the installation name resolves to for s among installed, // running or not: the person's, then the organisation's, then the instance's. func pickInstalled(installed []store.Installed, s Scope, name string) (store.Installed, bool) { for _, o := range s.owners() { for _, a := range installed { if a.Name == name && a.Owner() == o { return a, true } } } return store.Installed{}, false }