Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(audit-logs): Redact secret data #508

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkg/admission/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package admission

import (
"context"
"encoding/json"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -110,5 +112,29 @@ func logAdmissionRequest(ctx context.Context) {
if err != nil {
return
}

// Redact secret data from the log
if admissionRequest.Kind.Kind == "Secret" {
admissionRequest.Object.Raw = redactRawObject(admissionRequest.Object.Raw)
admissionRequest.OldObject.Raw = redactRawObject(admissionRequest.OldObject.Raw)
}
ctrl.Log.Info("AdmissionRequest", "Request", admissionRequest)
}

// redactRawObject redacts secret data and annotations from the raw object
func redactRawObject(rawObject []byte) []byte {
// Redact secret data from the log
secret := corev1.Secret{}
err := json.Unmarshal(rawObject, &secret)
if err == nil {
// delete annotations as they might have the last applied configuration
secret.Annotations = nil
// redact all secret data entries
for key := range secret.Data {
secret.Data[key] = []byte("REDACTED")
}
secretJSON, _ := json.Marshal(secret)
return secretJSON
}
return rawObject
}
Loading