generated from nginxinc/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use SecretBytes type for cert values to prevent accidental print…
…ing (#147) We save the values of the provided certs that we retrieve from Kubernetes secrets in the `Certificates` attribute on the `Certificates` struct. This is sensitive information that we want to make sure stays out of the logs and any stack traces. A common approach to this is to create a type definition for sensitive values that implements `Stringer` and `JSON` interfaces and cast the sensitive data to that value. Fixes issues #145
- Loading branch information
Showing
6 changed files
with
95 additions
and
32 deletions.
There are no files selected for viewing
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 @@ | ||
golang 1.19.13 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// Wraps byte slices which potentially could contain | ||
// sensitive data that should not be output to the logs. | ||
// This will output [REDACTED] if attempts are made | ||
// to print this type in logs, serialize to JSON, or | ||
// otherwise convert it to a string. | ||
// Usage: core.SecretBytes(myByteSlice) | ||
type SecretBytes []byte | ||
|
||
func (sb SecretBytes) String() string { | ||
return "[REDACTED]" | ||
} | ||
|
||
func (sb SecretBytes) MarshalJSON() ([]byte, error) { | ||
return json.Marshal("[REDACTED]") | ||
} |
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,31 @@ | ||
/* | ||
* Copyright 2023 F5 Inc. All rights reserved. | ||
* Use of this source code is governed by the Apache License that can be found in the LICENSE file. | ||
*/ | ||
|
||
package core | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestSecretBytesToString(t *testing.T) { | ||
sensitive := SecretBytes([]byte("If you can see this we have a problem")) | ||
|
||
expected := "foo [REDACTED] bar" | ||
result := fmt.Sprintf("foo %v bar", sensitive) | ||
if result != expected { | ||
t.Errorf("Expected %s, got %s", expected, result) | ||
} | ||
} | ||
|
||
func TestSecretBytesToJSON(t *testing.T) { | ||
sensitive, _ := json.Marshal(SecretBytes([]byte("If you can see this we have a problem"))) | ||
expected := `foo "[REDACTED]" bar` | ||
result := fmt.Sprintf("foo %v bar", string(sensitive)) | ||
if result != expected { | ||
t.Errorf("Expected %s, got %s", expected, result) | ||
} | ||
} |