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

Mask the Kubernetes Secrets data from dry-run and apply logs #420

Merged
merged 2 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion controllers/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (r *KustomizationReconciler) reconcile(
source.GetArtifact().Revision,
meta.ReconciliationFailedReason,
err.Error(),
), err
), stripSensitiveData(err)
}

// prune
Expand Down
13 changes: 13 additions & 0 deletions controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package controllers

import (
"errors"
"regexp"
"strings"
)

Expand Down Expand Up @@ -77,3 +79,14 @@ func containsString(slice []string, s string) bool {
}
return false
}

func stripSensitiveData(err error) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not important for this PR, as we are foreseeing that it will be refactored soon (or handled differently), but in cases like this I would pick maskSensitiveData over the current function name.

As a strip would literally take all data out, instead of replacing (= masking) it.

r := regexp.MustCompile(`(v1.Secret.(StringData|Data):) (.*)`)
newErr := r.ReplaceAllString(err.Error(), "$1 [ ** REDACTED ** ]")

// strip data from bigger context
r = regexp.MustCompile(`((stringData|data)\":{)(.*)(})`)
newErr = r.ReplaceAllString(newErr, "$1 [ ** REDACTED ** ] $4")

return errors.New(newErr)
}
30 changes: 30 additions & 0 deletions controllers/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"errors"
"strings"
"testing"
)
Expand Down Expand Up @@ -54,3 +55,32 @@ error: error validating data: unknown field "ima ge" in io.k8s.api.core.v1.Cont
})
}
}

func TestStripSensitiveData(t *testing.T) {
tests := []struct {
name string
in error
expected error
}{
{
"stringData",
errors.New("apply failed: Error from server (BadRequest): error when creating \"0f1563ce-8273-4879-99dd-f6f58629cc2d.yaml\": Secret in version \"v1\" cannot be handled as a Secret: v1.Secret.StringData: ReadString: expects \" or n, but found 0, error found in #10 byte of ...|\"secret\":0}}\n|..., bigger context ...|\"namespace\":\"sensitive-data-dkgvw\"},\"stringData\":{\"secret\":0}}\n|...\n"),
errors.New("apply failed: Error from server (BadRequest): error when creating \"0f1563ce-8273-4879-99dd-f6f58629cc2d.yaml\": Secret in version \"v1\" cannot be handled as a Secret: v1.Secret.StringData: [ ** REDACTED ** ]\n|..., bigger context ...|\"namespace\":\"sensitive-data-dkgvw\"},\"stringData\":{ [ ** REDACTED ** ] }\n|...\n"),
},
{
"data",
errors.New("apply failed: Error from server (BadRequest): error when creating \"0f1563ce-8273-4879-99dd-f6f58629cc2d.yaml\": Secret in version \"v1\" cannot be handled as a Secret: v1.Secret.Data: ReadString: expects \" or n, but found 0, error found in #10 byte of ...|\"secret\":0}}\n|..., bigger context ...|\"namespace\":\"sensitive-data-dkgvw\"},\"data\":{\"secret\":0}}\n|...\n"),
errors.New("apply failed: Error from server (BadRequest): error when creating \"0f1563ce-8273-4879-99dd-f6f58629cc2d.yaml\": Secret in version \"v1\" cannot be handled as a Secret: v1.Secret.Data: [ ** REDACTED ** ]\n|..., bigger context ...|\"namespace\":\"sensitive-data-dkgvw\"},\"data\":{ [ ** REDACTED ** ] }\n|...\n"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expected := stripSensitiveData(tt.in)

if expected.Error() != tt.expected.Error() {
t.Errorf("\nexpected:\n%q\ngot:\n%q\n", tt.expected.Error(), expected.Error())
}
})
}
}