104 lines
3.2 KiB
Go
104 lines
3.2 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 who answers for the target.
|
|
Owner string
|
|
}
|
|
|
|
// TargetLookup supplies the facts the target's own declaration lacks.
|
|
type TargetLookup interface {
|
|
Facts(ctx context.Context, t connectors.Target) (TargetFacts, error)
|
|
}
|
|
|
|
// NoTargetFacts knows nothing about any target, so Impact holds every changing
|
|
// action until a connector lookup supplies the data.
|
|
type NoTargetFacts struct{}
|
|
|
|
// Facts returns no facts.
|
|
func (NoTargetFacts) Facts(context.Context, connectors.Target) (TargetFacts, error) {
|
|
return TargetFacts{}, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
if missing := missingImpactData(a.Target, facts); len(missing) > 0 {
|
|
v := Verdict{Ceiling: ProposeAndWait, BlindSpots: missing}
|
|
for _, m := range missing {
|
|
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
|
|
}
|
|
|
|
// 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
|
|
}
|