-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a new type for security-sensitive fields
We should take extra care not to log security-sensitive fields. Introduce a new type `Sensitive` for exactly this purpose. When this type is converted to String or JSON bytes, its value is redacted. Signed-off-by: Yannis Zarkadas <[email protected]>
- Loading branch information
1 parent
d10906e
commit b8788fd
Showing
4 changed files
with
75 additions
and
12 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
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,5 @@ | ||
// Package common contains helpers for common things that are needed when | ||
// developing Go programs. The goal is to standardize on using high-quality | ||
// implementations that follow best-practices. | ||
|
||
package common |
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 common | ||
|
||
import ( | ||
"encoding" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// ProtectedString is a type for security-ProtectedString fields. | ||
// It hides its value when printed or marshalled to JSON. | ||
// Used to hide ProtectedString fields from loggers. | ||
type ProtectedString struct { | ||
value string | ||
} | ||
|
||
var message = "<protected>" | ||
|
||
func NewProtectedString(val string) ProtectedString { | ||
return ProtectedString{ | ||
value: val, | ||
} | ||
} | ||
|
||
// Reveal returns the secret value. | ||
func (p ProtectedString) Reveal() string { | ||
return p.value | ||
} | ||
|
||
// String returns the string representation of the type. Override it to avoid | ||
// logging the secret value. | ||
func (p ProtectedString) String() string { | ||
return message | ||
} | ||
|
||
var _ fmt.Stringer = (*ProtectedString)(nil) | ||
|
||
// MarshalJSON returns the JSON representation of the type. Many loggers will | ||
// log JSON representation of types. Override it to avoid logging the secret | ||
// value. | ||
func (p *ProtectedString) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(&message) | ||
} | ||
|
||
var _ json.Marshaler = (*ProtectedString)(nil) | ||
|
||
// UnmarshalText can unmarshal a textual representation of a ProtectedString. | ||
// Needed for use with the envconfig library: | ||
// https://github.com/kelseyhightower/envconfig | ||
func (p *ProtectedString) UnmarshalText(text []byte) error { | ||
p.value = string(text) | ||
return nil | ||
} | ||
|
||
var _ encoding.TextUnmarshaler = (*ProtectedString)(nil) |
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