-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement secret redacter for printing snapshots (#1686)
* Implement secret redacter for printing snapshots
- Loading branch information
1 parent
101fcc3
commit 1476698
Showing
14 changed files
with
207 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
changelog: | ||
- type: NEW_FEATURE | ||
description: Implement a utility to print snapshot content with the Secret content redacted | ||
issueLink: https://github.com/solo-io/gloo/issues/1679 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package syncutil | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1" | ||
) | ||
|
||
const Redacted = "[REDACTED]" | ||
|
||
// stringify the contents of the snapshot | ||
// | ||
// NOTE that if any of the top-level fields of the snapshot is a SecretList, then the secrets will be | ||
// stringified by printing just their name and namespace, and "REDACTED" for their data. Secrets may | ||
// contain sensitive data like TLS private keys, so be sure to use this whenever you'd like to | ||
// stringify a snapshot rather than Go's %v formatter | ||
func StringifySnapshot(snapshot interface{}) string { | ||
snapshotStruct := reflect.ValueOf(snapshot).Elem() | ||
stringBuilder := strings.Builder{} | ||
|
||
for i := 0; i < snapshotStruct.NumField(); i++ { | ||
fieldName := snapshotStruct.Type().Field(i).Name | ||
fieldValue := snapshotStruct.Field(i).Interface() | ||
|
||
stringBuilder.Write([]byte(fieldName)) | ||
stringBuilder.Write([]byte(":")) | ||
|
||
if secretList, ok := fieldValue.(v1.SecretList); ok { | ||
stringBuilder.Write([]byte("[")) | ||
|
||
var redactedSecrets []string | ||
secretList.Each(func(s *v1.Secret) { | ||
redactedSecret := fmt.Sprintf( | ||
"%v{name: %s namespace: %s data: %s}", | ||
reflect.TypeOf(s), | ||
s.Metadata.Name, | ||
s.Metadata.Namespace, | ||
Redacted, | ||
) | ||
|
||
redactedSecrets = append(redactedSecrets, redactedSecret) | ||
}) | ||
|
||
stringBuilder.Write([]byte(strings.Join(redactedSecrets, " '"))) | ||
stringBuilder.Write([]byte("]")) | ||
} else { | ||
stringBuilder.Write([]byte(fmt.Sprintf("%v", fieldValue))) | ||
} | ||
stringBuilder.Write([]byte("\n")) | ||
} | ||
|
||
return stringBuilder.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package syncutil_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/solo-io/gloo/pkg/utils/syncutil" | ||
v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1" | ||
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core" | ||
) | ||
|
||
var _ = Describe("Log Redacter", func() { | ||
var ( | ||
secretName = "my-test-secret" | ||
secretNamespace = "my-secret-namespace" | ||
privateKey = "RSA PRIVATE KEY CONTENT" | ||
|
||
noSecretsSnapshot = &v1.SetupSnapshot{ | ||
Settings: []*v1.Settings{{ | ||
Metadata: core.Metadata{ | ||
Name: "settings", | ||
Namespace: "ns", | ||
}, | ||
}}, | ||
} | ||
snapshotWithSecrets = &v1.ApiSnapshot{ | ||
Endpoints: []*v1.Endpoint{{ | ||
Metadata: core.Metadata{ | ||
Name: "endpoint", | ||
Namespace: "ns", | ||
}, | ||
}}, | ||
Secrets: []*v1.Secret{{ | ||
Kind: &v1.Secret_Tls{Tls: &v1.TlsSecret{ | ||
PrivateKey: privateKey, | ||
}}, | ||
Metadata: core.Metadata{ | ||
Name: secretName, | ||
Namespace: secretNamespace, | ||
}, | ||
}}, | ||
} | ||
) | ||
|
||
It("does not redact anything when no secrets", func() { | ||
Expect(syncutil.StringifySnapshot(noSecretsSnapshot)).NotTo(ContainSubstring(syncutil.Redacted)) | ||
}) | ||
|
||
It("contains redacted content when secrets are present", func() { | ||
s := syncutil.StringifySnapshot(snapshotWithSecrets) | ||
|
||
Expect(s).To(ContainSubstring(syncutil.Redacted)) | ||
Expect(s).NotTo(ContainSubstring(privateKey)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package syncutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestSyncUtils(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Sync Utils Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters