Files

93 lines
2.6 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)),
// Always present: a policy that compares the state of a resource that
// has none would otherwise error and never match, so a shipped rule
// such as "not an owner's membership" would refuse the route guard's
// request, which names no state.
"state": types.String(r.State),
}
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))
}
entities[resUID] = cedar.Entity{
UID: resUID,
Parents: cedar.NewEntityUIDSet(parents...),
Attributes: types.NewRecord(attrs),
}
return entities
}