Files
archie/modules/governance/entities.go
T
sam 0218e4ec89 feat(governance): move identity, org and Cedar access engine into governance (#2017)
Copied from internal/domain/identity, internal/domain/org, internal/domain/access, internal/infrastructure/access and internal/infrastructure/postgres.
2026-11-18 02:09:29 +11:00

91 lines
2.4 KiB
Go

package governance
import (
"time"
cedar "github.com/cedar-policy/cedar-go"
"github.com/cedar-policy/cedar-go/types"
)
func buildRequest(p Principal, a Action, r Resource, c AccessContext) types.Request {
record := types.RecordMap{}
if c.Signature != "" {
record["signature"] = types.String(c.Signature)
}
if c.Addr != "" {
if addr, err := types.ParseIPAddr(c.Addr); err == nil {
record["addr"] = addr
}
}
if c.Run != "" {
record["run"] = types.String(c.Run)
}
if c.Step != "" {
record["step"] = types.String(c.Step)
}
at := c.Time
if at.IsZero() {
at = time.Now().UTC()
}
record["time"] = types.NewDatetime(at)
return types.Request{
Principal: types.NewEntityUID(EntityTypeIdentity, types.String(p.IdentityID)),
Action: types.NewEntityUID(EntityTypeAction, types.String(string(a))),
Resource: types.NewEntityUID(EntityTypeObject, types.String(r.ID)),
Context: types.NewRecord(record),
}
}
func buildEntities(p Principal, r Resource) cedar.EntityMap {
resOrg := r.Org
if resOrg == "" {
resOrg = p.Org
}
entities := cedar.EntityMap{}
orgUID := types.NewEntityUID(EntityTypeOrg, types.String(resOrg))
entities[orgUID] = cedar.Entity{UID: orgUID}
principalUID := types.NewEntityUID(EntityTypeIdentity, types.String(p.IdentityID))
entities[principalUID] = cedar.Entity{
UID: principalUID,
Parents: cedar.NewEntityUIDSet(orgUID),
Attributes: types.NewRecord(types.RecordMap{
"org": types.String(resOrg),
"role": types.String(string(p.Role(r.Workspace))),
}),
}
parents := []types.EntityUID{orgUID}
resUID := types.NewEntityUID(EntityTypeObject, types.String(r.ID))
attrs := types.RecordMap{
"org": types.String(resOrg),
"kind": types.String(string(r.Kind)),
}
if r.Workspace != "" {
wsUID := types.NewEntityUID(EntityTypeWorkspace, types.String(r.Workspace))
entities[wsUID] = cedar.Entity{
UID: wsUID,
Parents: cedar.NewEntityUIDSet(orgUID),
Attributes: types.NewRecord(types.RecordMap{
"org": types.String(resOrg),
"environment": types.String(p.Environment(r.Workspace)),
}),
}
parents = append(parents, wsUID)
attrs["workspace"] = wsUID
}
if r.Owner != "" {
attrs["owner"] = types.String(string(r.Owner))
}
if r.State != "" {
attrs["state"] = types.String(r.State)
}
entities[resUID] = cedar.Entity{
UID: resUID,
Parents: cedar.NewEntityUIDSet(parents...),
Attributes: types.NewRecord(attrs),
}
return entities
}