diff --git a/pkg/operator/ceph/controller/predicate.go b/pkg/operator/ceph/controller/predicate.go index 45b88a755..3b874a3a9 100644 --- a/pkg/operator/ceph/controller/predicate.go +++ b/pkg/operator/ceph/controller/predicate.go @@ -396,12 +396,21 @@ func objectChanged(oldObj, newObj runtime.Object, objectName string) (bool, erro doReconcile = true return doReconcile, errors.Wrap(err, "failed to calculate object diff but let's reconcile just in case") } else if diff.IsEmpty() { - logger.Debug("diff is empty nothing to reconcile") + logger.Debugf("object %q diff is empty, nothing to reconcile", objectName) return doReconcile, nil } - logger.Debugf("object diff is %s", diff.String()) - return isValidEvent(diff.Patch, objectName), nil + // Do not leak details of diff if the object contains sensitive data (e.g., it is a Secret) + isSensitive := false + if _, ok := new.(*corev1.Secret); ok { + logger.Debugf("object %q diff is [redacted for Secrets]", objectName) + isSensitive = true + } else { + logger.Debugf("object %q diff is %s", objectName, diff.String()) + isSensitive = false + } + + return isValidEvent(diff.Patch, objectName, isSensitive), nil } // WatchPredicateForNonCRDObject is a special filter for create events @@ -514,17 +523,19 @@ func WatchPredicateForNonCRDObject(owner runtime.Object, scheme *runtime.Scheme) } } -// isValidEvent analyses the diff between two objects events and determines -// if we should reconcile that event or not -// The goal is to avoid double-reconcile as much as possible -func isValidEvent(patch []byte, objectName string) bool { +// isValidEvent analyses the diff between two objects events and determines if we should reconcile +// that event or not. The goal is to avoid double-reconcile as much as possible. +// If the patch could contain sensitive data, isValidEvent will not leak the data to logs. +func isValidEvent(patch []byte, objectName string, patchContainsSensitiveData bool) bool { var p map[string]interface{} err := json.Unmarshal(patch, &p) if err != nil { - logger.Errorf("failed to unmarshal patch. %v", err) + logErrorUnlessSensitive("failed to unmarshal patch", err, patchContainsSensitiveData) return false } - logger.Debugf("patch before trimming is %s", string(patch)) + if !patchContainsSensitiveData { + logger.Debugf("patch before trimming is %s", string(patch)) + } // don't reconcile on status update on an object (e.g. status "creating") logger.Debugf("trimming 'status' field from patch") @@ -543,16 +554,28 @@ func isValidEvent(patch []byte, objectName string) bool { // Re-marshal to get the last diff patch, err = json.Marshal(p) if err != nil { - logger.Errorf("failed to marshal patch. %v", err) + logErrorUnlessSensitive("failed to marshal patch", err, patchContainsSensitiveData) return false } // If after all the filtering there is still something in the patch, we reconcile - logger.Infof("controller will reconcile resource %q based on patch: %s", objectName, string(patch)) + text := string(patch) + if patchContainsSensitiveData { + text = "[redacted patch details due to potentially sensitive content]" + } + logger.Infof("controller will reconcile resource %q based on patch: %s", objectName, text) return true } +func logErrorUnlessSensitive(msg string, err error, isSensitive bool) { + if isSensitive { + logger.Errorf("%s. [redacted error due to potentially sensitive content]", msg) + } else { + logger.Errorf("%s. %v", msg, err) + } +} + func isUpgrade(oldLabels, newLabels map[string]string) bool { oldLabelVal, oldLabelKeyExist := oldLabels[cephVersionLabelKey] newLabelVal, newLabelKeyExist := newLabels[cephVersionLabelKey] @@ -623,7 +646,6 @@ func isCMToIgnoreOnDelete(obj runtime.Object) bool { } func isSecretToIgnoreOnUpdate(obj runtime.Object) bool { - // If not a Secret, let's not reconcile s, ok := obj.(*corev1.Secret) if !ok { return false diff --git a/pkg/operator/ceph/controller/predicate_test.go b/pkg/operator/ceph/controller/predicate_test.go index 8609d6c41..eb1a5ab4f 100644 --- a/pkg/operator/ceph/controller/predicate_test.go +++ b/pkg/operator/ceph/controller/predicate_test.go @@ -122,18 +122,18 @@ func TestIsValidEvent(t *testing.T) { } }`) - b := isValidEvent(valid, obj) + b := isValidEvent(valid, obj, false) assert.True(t, b) valid = []byte(`{"foo": "bar"}`) - b = isValidEvent(valid, obj) + b = isValidEvent(valid, obj, false) assert.True(t, b) invalid := []byte(`{ "metadata": {}, "status": {}, }`) - b = isValidEvent(invalid, obj) + b = isValidEvent(invalid, obj, false) assert.False(t, b) }