Files

142 lines
4.5 KiB
Go

package judgement
import (
"context"
"fmt"
"slices"
"github.com/samcharles93/archie-core/modules/connectors"
)
// TargetFacts is what a lookup knows about an action's target. A zero field
// is a fact nobody has supplied.
type TargetFacts struct {
// Count is how many things the action reaches.
Count int
// DataClass names what the target holds, such as "temp" or "customer".
DataClass string
// Owner is the id of the person who answers for the target, empty when the
// target has no recorded owner or the owner isn't a person Archie knows.
Owner string
// OwnerIdentity is the owner as the source recorded them, kept so a finding
// can say who couldn't be matched to a person.
OwnerIdentity string
}
// DefaultOwners says whether the organisation has a default owner, who is
// asked when a target's owner is unknown.
type DefaultOwners interface {
HasDefaultOwner(ctx context.Context) (bool, error)
}
// TargetLookup supplies the facts the target's own declaration lacks.
type TargetLookup interface {
Facts(ctx context.Context, t connectors.Target) (TargetFacts, error)
}
const (
environmentProduction = "production"
dataTemp = "temp"
dataCache = "cache"
dataNone = "none"
)
// lowRiskData are the data classes whose loss costs nothing that can't be
// regenerated.
var lowRiskData = []string{dataTemp, dataCache, dataNone}
// Impact sets a ceiling from what breaks if the action goes wrong: the
// environment, how many targets it reaches, what data they hold and who owns
// them. A datum nobody supplied holds the action at propose and wait.
type Impact struct {
Lookup TargetLookup
// Defaults, when set, lets an unknown owner's finding say that no default
// owner is set to take the request.
Defaults DefaultOwners
}
// Name is the guard's name in verdicts.
func (Impact) Name() string { return "impact" }
// Advisory is false.
func (Impact) Advisory() bool { return false }
// Judge returns the impact ceiling. Reading breaks nothing.
func (i Impact) Judge(ctx context.Context, a Action) (Verdict, error) {
if a.Effect.ReadOnly {
return Verdict{Ceiling: ActQuietly}, nil
}
facts, err := i.Lookup.Facts(ctx, a.Target)
if err != nil {
return Verdict{}, fmt.Errorf("look up target facts: %w", err)
}
v, err := i.verdict(ctx, a, facts)
v.Owner = facts.Owner
return v, err
}
// verdict folds the facts into a ceiling.
func (i Impact) verdict(ctx context.Context, a Action, facts TargetFacts) (Verdict, error) {
if missing := missingImpactData(a.Target, facts); len(missing) > 0 {
v := Verdict{Ceiling: ProposeAndWait, BlindSpots: missing}
for _, m := range missing {
if m == "owner" {
text, err := i.unknownOwner(ctx, facts)
if err != nil {
return Verdict{}, err
}
v.Findings = append(v.Findings, Finding{Text: text})
continue
}
v.Findings = append(v.Findings, Finding{Text: "no " + m + " is known for the target"})
}
return v, nil
}
switch {
case a.Target.Environment == environmentProduction:
return Verdict{Ceiling: ProposeAndWait, Findings: []Finding{{Text: "the target is in production"}}}, nil
case facts.Count > 1:
return Verdict{Ceiling: ProposeAndWait, Findings: []Finding{{Text: fmt.Sprintf("the action reaches %d targets", facts.Count)}}}, nil
case !slices.Contains(lowRiskData, facts.DataClass):
return Verdict{Ceiling: ProposeAndWait, Findings: []Finding{{Text: "the target holds " + facts.DataClass + " data"}}}, nil
}
return Verdict{Ceiling: ActQuietly}, nil
}
// unknownOwner words the finding for a target nobody is known to own, and says
// where the request goes instead.
func (i Impact) unknownOwner(ctx context.Context, f TargetFacts) (string, error) {
text := "unknown owner: no owner is recorded for the target"
if f.OwnerIdentity != "" {
text = fmt.Sprintf("unknown owner: %s isn't a person Archie knows", f.OwnerIdentity)
}
if i.Defaults != nil {
set, err := i.Defaults.HasDefaultOwner(ctx)
if err != nil {
return "", fmt.Errorf("read default owner: %w", err)
}
if !set {
return text + ", and no default owner is set", nil
}
}
return text + ", so the request goes to the default owner", nil
}
// missingImpactData names what Impact needs and doesn't have.
func missingImpactData(t connectors.Target, f TargetFacts) []string {
var missing []string
if t.Environment == "" {
missing = append(missing, "environment")
}
if f.Count < 1 {
missing = append(missing, "target count")
}
if f.DataClass == "" {
missing = append(missing, "data class")
}
if f.Owner == "" {
missing = append(missing, "owner")
}
return missing
}