package governance import ( "context" "slices" "time" ) // PathStep is one stop on the route an unanswered approval request takes: who // it is passed to, and how long it waits there. The first step is the target's // owner when there is one, otherwise the default owner, who may be nobody, and // then each member on the escalation path who may answer it. type PathStep struct { // Account is the id of the member asked at this step, or empty. Account string `json:"account"` Name string `json:"name"` DeadlineMS int64 `json:"deadline_ms"` } // Deadline is how long the request waits at this step. func (p PathStep) Deadline() time.Duration { return time.Duration(p.DeadlineMS) * time.Millisecond } // HasDefaultOwner reports whether the organisation has a default owner. func (m *Module) HasDefaultOwner(ctx context.Context) (bool, error) { set, err := m.Settings(ctx) if err != nil { return false, err } return set.DefaultOwner != "", nil } // EscalationPath returns the route an approval request takes. owner is the // account that answers for the action's target, or empty when none is known. // A known owner is asked first and the default owner is the next step; an // unknown owner leaves the default owner first. Every later step is a member // on the escalation path. A member the access policies don't let approve is // left out, so the request never moves to someone who could not answer it. // When the action needs authority over policy as well, so must they be allowed // to manage policies. func (m *Module) EscalationPath(ctx context.Context, owner string, needAuthority bool) ([]PathStep, error) { set, err := m.Settings(ctx) if err != nil { return nil, err } names, err := m.memberNames(ctx) if err != nil { return nil, err } actions := []Action{ActionApprove} if needAuthority { actions = append(actions, ActionManagePolicies) } candidates := []string{owner, set.DefaultOwner} for _, st := range set.Escalation { candidates = append(candidates, st.Account) } eligible := map[string]string{} for _, id := range candidates { name, member := names[id] if id == "" || !member { continue } may, err := m.Holds(ctx, IdentityID(id), Resource{Kind: KindApproval, ID: "path", Org: DefaultOrgID}, actions...) if err != nil { return nil, err } if may { eligible[id] = name } } return set.path(eligible, owner), nil } // path lays the route out over eligible, the members who may answer by id. func (s Settings) path(eligible map[string]string, owner string) []PathStep { defaultOwner := PathStep{DeadlineMS: s.ApprovalDeadline.Milliseconds()} if name, ok := eligible[s.DefaultOwner]; ok { defaultOwner.Account, defaultOwner.Name = s.DefaultOwner, name } var out []PathStep if name, ok := eligible[owner]; ok && owner != "" { out = append(out, PathStep{Account: owner, Name: name, DeadlineMS: s.ApprovalDeadline.Milliseconds()}) } // With no owner to ask, the default owner leads even when nobody is set, // so the request still goes to anyone who may answer it. if len(out) == 0 || (defaultOwner.Account != "" && defaultOwner.Account != owner) { out = append(out, defaultOwner) } for _, st := range s.Escalation { name, ok := eligible[st.Account] if !ok || slices.ContainsFunc(out, func(o PathStep) bool { return o.Account == st.Account }) { continue } out = append(out, PathStep{Account: st.Account, Name: name, DeadlineMS: st.Deadline.Milliseconds()}) } return out } // Holds reports whether the account is allowed every one of actions on r by // the live access policies. It is false while no policy chain is attached. func (m *Module) Holds(ctx context.Context, account IdentityID, r Resource, actions ...Action) (bool, error) { authz := m.authz() if authz == nil { return false, nil } p, err := m.PrincipalFor(ctx, account) if err != nil { return false, err } for _, a := range actions { d := authz.Authorize(ctx, p, a, r, AccessContext{}) if d.Err != nil { return false, d.Err } if !d.Allowed { return false, nil } } return true, nil }