diff --git a/api/internal/target/kusttarget.go b/api/internal/target/kusttarget.go index 06af972bc..d7cb1f4fd 100644 --- a/api/internal/target/kusttarget.go +++ b/api/internal/target/kusttarget.go @@ -5,6 +5,7 @@ package target import ( "bytes" + "crypto/sha1" "encoding/json" "fmt" "strings" @@ -288,14 +289,36 @@ func (kt *KustTarget) runValidators(ra *accumulator.ResAccumulator) error { return err } for _, v := range validators { - err := v.Validate(ra.ResMap()) + // Validators shouldn't modify the resource map + orignalHash, err := getSha1Hash(ra.ResMap()) if err != nil { return err } + err = v.Validate(ra.ResMap()) + if err != nil { + return err + } + newHash, err := getSha1Hash(ra.ResMap()) + if err != nil { + return err + } + if !bytes.Equal(orignalHash, newHash) { + return fmt.Errorf("validator %#v shouldn't modify the resource map", v) + } } return nil } +func getSha1Hash(rm resmap.ResMap) ([]byte, error) { + sha1Hash := sha1.New() + yamlBytes, err := rm.AsYaml() + if err != nil { + return nil, err + } + sha1Hash.Write(yamlBytes) + return sha1Hash.Sum(nil), nil +} + func (kt *KustTarget) configureExternalValidators() ([]resmap.Validator, error) { ra := accumulator.MakeEmptyAccumulator() ra, err := kt.accumulateResources(ra, kt.kustomization.Validators)