Skip to content

Commit

Permalink
[admission] Handle DELETEs better (#818)
Browse files Browse the repository at this point in the history
* Handle DELETEs better

* Update version.txt

* Update CHANGELOG.md

* Update validate.go

* fix object on request

* add logs

* add info log

* add log

* version

* more logs

* revert opa

* revert log
  • Loading branch information
rbren authored Oct 11, 2023
1 parent 39660cc commit 95e377e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 3 additions & 0 deletions plugins/admission/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.13.1
* Fix for DELETE requests

## 1.13.0
* Migrate from go-funk to lo

Expand Down
16 changes: 13 additions & 3 deletions plugins/admission/pkg/admission/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ func (v *Validator) handleInternal(ctx context.Context, req admission.Request) (
msg := fmt.Sprintf("Insights admission controller is ignoring service account %s.", username)
return true, []string{msg}, nil, nil
}
rawBytes := req.Object.Raw
if req.Operation == "DELETE" {
rawBytes = req.OldObject.Raw // Object.Raw is empty for DELETEs
}
var decoded map[string]any
err := json.Unmarshal(req.Object.Raw, &decoded)
err := json.Unmarshal(rawBytes, &decoded)
if err != nil {
logrus.Errorf("Error unmarshaling JSON")
return false, nil, nil, err
Expand Down Expand Up @@ -183,6 +187,9 @@ type MetadataReport struct {
}

func getRequestReport(req admission.Request, namespaceMetadata map[string]any) (models.ReportInfo, error) {
if req.Operation == "DELETE" {
req.Object = req.OldObject // DELETE requests don't have an object
}
metadataReport := MetadataReport{AdmissionRequest: req.AdmissionRequest, NamespaceMetadata: namespaceMetadata}
contents, err := json.Marshal(&metadataReport)
return models.ReportInfo{
Expand All @@ -200,12 +207,13 @@ func processInputYAML(ctx context.Context, iConfig models.InsightsConfig, config
return false, nil, nil, err
}
reports := []models.ReportInfo{metadataReport}
if config.Reports.Polaris {
if config.Reports.Polaris && len(req.Object.Raw) > 0 {
logrus.Info("Running Polaris")
// Scan manifests with Polaris
polarisConfig := *config.Polaris
polarisReport, err := polaris.GetPolarisReport(ctx, polarisConfig, req.Object.Raw)
if err != nil {
logrus.Errorf("Error while running Polaris: %v", err)
return false, nil, nil, err
}
reports = append(reports, polarisReport)
Expand All @@ -215,12 +223,13 @@ func processInputYAML(ctx context.Context, iConfig models.InsightsConfig, config
logrus.Info("Running OPA")
opaReport, err := opa.ProcessOPA(ctx, decoded, req, config, iConfig)
if err != nil {
logrus.Errorf("Error while running OPA: %v", err)
return false, nil, nil, err
}
reports = append(reports, opaReport)
}

if config.Reports.Pluto {
if config.Reports.Pluto && len(req.Object.Raw) > 0 {
logrus.Info("Running Pluto")
userTargetVersionsStr := os.Getenv("PLUTO_TARGET_VERSIONS")
userTargetVersions, err := pluto.ParsePlutoTargetVersions(userTargetVersionsStr)
Expand All @@ -230,6 +239,7 @@ func processInputYAML(ctx context.Context, iConfig models.InsightsConfig, config
}
plutoReport, err := pluto.ProcessPluto(req.Object.Raw, userTargetVersions)
if err != nil {
logrus.Errorf("Error while running Pluto: %v", err)
return false, nil, nil, err
}
reports = append(reports, plutoReport)
Expand Down
2 changes: 1 addition & 1 deletion plugins/admission/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.13.0
1.13.1

0 comments on commit 95e377e

Please sign in to comment.