Documentation
¶
Index ¶
- Constants
- Variables
- func Base58Decode(input string) ([]byte, error)
- func Base58Encode(input []byte) string
- func CalculateEntropy(data []byte) float64
- func Equal[X any, Y any](s1 Secret[X], s2 Secret[Y]) bool
- func SanitizeFlagError(err error) error
- func SetKey(key []byte)
- func Wipe(data []byte)
- type EncryptedSecret
- func (s *EncryptedSecret[T]) Destroy()
- func (s *EncryptedSecret[T]) Empty() bool
- func (s *EncryptedSecret[T]) Equal(sec Secret[T]) bool
- func (s EncryptedSecret[T]) GoString() string
- func (s *EncryptedSecret[T]) Hash() ([32]byte, error)
- func (s *EncryptedSecret[T]) ID() (string, error)
- func (s EncryptedSecret[T]) MarshalJSON() ([]byte, error)
- func (s *EncryptedSecret[T]) MarshalSecret() (text []byte, err error)
- func (s EncryptedSecret[T]) MarshalText() ([]byte, error)
- func (s *EncryptedSecret[T]) Secret() T
- func (s *EncryptedSecret[T]) Set(txt string) error
- func (s EncryptedSecret[T]) String() string
- func (s *EncryptedSecret[T]) Type() string
- func (s *EncryptedSecret[T]) UnmarshalJSON(data []byte) error
- func (s *EncryptedSecret[T]) UnmarshalText(text []byte) error
- type GuardedSecret
- func (s *GuardedSecret[T]) Destroy()
- func (s *GuardedSecret[T]) Empty() bool
- func (s *GuardedSecret[T]) Equal(sec Secret[T]) bool
- func (s *GuardedSecret[T]) GoString() string
- func (s *GuardedSecret[T]) Hash() ([32]byte, error)
- func (s *GuardedSecret[T]) ID() (string, error)
- func (s *GuardedSecret[T]) MarshalJSON() ([]byte, error)
- func (s *GuardedSecret[T]) MarshalSecret() ([]byte, error)
- func (s *GuardedSecret[T]) MarshalText() ([]byte, error)
- func (s *GuardedSecret[T]) Secret() T
- func (s *GuardedSecret[T]) Set(txt string) error
- func (s *GuardedSecret[T]) String() string
- func (s *GuardedSecret[T]) Type() string
- func (s *GuardedSecret[T]) UnmarshalJSON(data []byte) error
- func (s *GuardedSecret[T]) UnmarshalText(text []byte) error
- type PlainSecret
- func (s *PlainSecret[T]) Destroy()
- func (s *PlainSecret[T]) Empty() bool
- func (s *PlainSecret[T]) Equal(sec Secret[T]) bool
- func (s PlainSecret[T]) GoString() string
- func (s *PlainSecret[T]) Hash() ([32]byte, error)
- func (s *PlainSecret[T]) ID() (string, error)
- func (s PlainSecret[T]) MarshalJSON() ([]byte, error)
- func (s *PlainSecret[T]) MarshalSecret() ([]byte, error)
- func (s PlainSecret[T]) MarshalText() (text []byte, err error)
- func (s *PlainSecret[T]) Secret() T
- func (s *PlainSecret[T]) Set(txt string) error
- func (s PlainSecret[T]) String() string
- func (s *PlainSecret[T]) Type() string
- func (s *PlainSecret[T]) UnmarshalJSON(data []byte) error
- func (s *PlainSecret[T]) UnmarshalText(text []byte) error
- type Secret
- type Serializable
- type Singleton
- type StringSecret
- func (s *StringSecret) Destroy()
- func (s *StringSecret) Empty() bool
- func (s *StringSecret) Equal(sec Secret[[]byte]) bool
- func (s StringSecret) GoString() string
- func (s *StringSecret) Hash() ([32]byte, error)
- func (s *StringSecret) ID() (string, error)
- func (s StringSecret) MarshalJSON() ([]byte, error)
- func (s *StringSecret) MarshalSecret() (text []byte, err error)
- func (s StringSecret) MarshalText() (text []byte, err error)
- func (s *StringSecret) Secret() []byte
- func (s *StringSecret) Set(val string) error
- func (s StringSecret) String() string
- func (s *StringSecret) Type() string
- func (s *StringSecret) UnmarshalJSON(data []byte) error
- func (s *StringSecret) UnmarshalText(text []byte) error
Constants ¶
const MaxBase58DecodeLen = 1 << 16
MaxBase58DecodeLen bounds the input accepted by Base58Decode. Decoding is quadratic in the input length, so unbounded untrusted input could be used for denial of service.
Variables ¶
var ( // ErrInvalidValue is returned (wrapped) when input cannot be parsed into // a secret. The underlying parser error is deliberately dropped because // it may quote parts of the secret. ErrInvalidValue = errors.New("secrets: invalid value") // ErrNilSecret is returned when a value is written to a nil secret // pointer, so the write cannot be silently lost. ErrNilSecret = errors.New("secrets: write to nil secret") )
Functions ¶
func Base58Decode ¶
func Base58Encode ¶
func CalculateEntropy ¶
func Equal ¶
Equal reports whether both secrets hold the same plaintext bytes, comparing their hashes in constant time. Empty secrets are never equal.
func SanitizeFlagError ¶
SanitizeFlagError removes the raw argument from a pflag pflag.InvalidValueError when the flag holds a secret, so the value does not end up on stderr or in logs. Other errors are returned unchanged.
With cobra, install it once on the root command:
cmd.SetFlagErrorFunc(func(_ *cobra.Command, err error) error {
return secrets.SanitizeFlagError(err)
})
A pflag.FlagSet using pflag.ExitOnError (the default for pflag.CommandLine) prints the unsanitized error itself before exiting; use pflag.ContinueOnError and sanitize the returned error instead.
func SetKey ¶
func SetKey(key []byte)
SetKey sets the AES-256 key used by all EncryptedSecret values. It must be called before any EncryptedSecret is created and panics if the key is already initialized or is not exactly 32 bytes. The key is moved into a locked enclave and the caller's slice is wiped, also when SetKey panics.
Types ¶
type EncryptedSecret ¶
type EncryptedSecret[T any] struct { // contains filtered or unexported fields }
EncryptedSecret holds the value AES-256-GCM encrypted with the package key (see SetKey); it is decrypted on every read.
EncryptedSecret is not safe for concurrent use while it is being mutated (Set, UnmarshalText, UnmarshalJSON, Destroy): Destroy wipes the ciphertext in place, so a concurrent read fails or panics. Use GuardedSecret when concurrent mutation is needed.
func NewEncryptedSecretFromEnv ¶
func NewEncryptedSecretFromEnv(name string) *EncryptedSecret[[]byte]
NewEncryptedSecretFromEnv encrypts the value of the environment variable name and unsets it. The original value may still remain in the process environment block (e.g. /proc/self/environ on Linux) and in the immutable string returned by os.Getenv, neither of which can be wiped.
func (*EncryptedSecret[T]) Destroy ¶
func (s *EncryptedSecret[T]) Destroy()
func (*EncryptedSecret[T]) Empty ¶
func (s *EncryptedSecret[T]) Empty() bool
func (*EncryptedSecret[T]) Equal ¶
func (s *EncryptedSecret[T]) Equal(sec Secret[T]) bool
func (EncryptedSecret[T]) GoString ¶
func (s EncryptedSecret[T]) GoString() string
GoString masks the value for %#v, which bypasses String.
func (*EncryptedSecret[T]) Hash ¶
func (s *EncryptedSecret[T]) Hash() ([32]byte, error)
Hash returns an unsalted SHA-256 hash of the plaintext. For low-entropy secrets (e.g. human-chosen passwords) it allows offline dictionary attacks and must not be exposed to untrusted parties.
func (*EncryptedSecret[T]) ID ¶
func (s *EncryptedSecret[T]) ID() (string, error)
ID returns a short Base58 identifier derived from EncryptedSecret.Hash. See Hash for the dictionary-attack caveat.
func (EncryptedSecret[T]) MarshalJSON ¶
func (s EncryptedSecret[T]) MarshalJSON() ([]byte, error)
MarshalJSON returns the masked value; the ciphertext is not decrypted.
func (*EncryptedSecret[T]) MarshalSecret ¶
func (s *EncryptedSecret[T]) MarshalSecret() (text []byte, err error)
MarshalSecret returns the decrypted plaintext bytes, in the same serialization as PlainSecret.MarshalSecret. The caller owns the returned slice and should wipe it after use.
func (EncryptedSecret[T]) MarshalText ¶
func (s EncryptedSecret[T]) MarshalText() ([]byte, error)
func (*EncryptedSecret[T]) Secret ¶
func (s *EncryptedSecret[T]) Secret() T
func (*EncryptedSecret[T]) Set ¶
func (s *EncryptedSecret[T]) Set(txt string) error
func (EncryptedSecret[T]) String ¶
func (s EncryptedSecret[T]) String() string
func (*EncryptedSecret[T]) Type ¶
func (s *EncryptedSecret[T]) Type() string
func (*EncryptedSecret[T]) UnmarshalJSON ¶
func (s *EncryptedSecret[T]) UnmarshalJSON(data []byte) error
func (*EncryptedSecret[T]) UnmarshalText ¶
func (s *EncryptedSecret[T]) UnmarshalText(text []byte) error
UnmarshalText parses and encrypts text; empty text clears the secret. Parse errors are replaced by an ErrInvalidValue error that does not quote the input.
type GuardedSecret ¶
type GuardedSecret[T any] struct { // contains filtered or unexported fields }
GuardedSecret stores the serialized plaintext in a memguard Enclave: mlock'd, guard-paged, excluded from swap and core dumps, and encrypted at rest using a per-process in-memory key managed entirely by memguard. No user-supplied encryption key is required.
Use GuardedSecret when you want the strongest in-process memory protection and do not need to persist or share the encrypted form. For a user-supplied key or persistence use EncryptedSecret.
GuardedSecret is safe for concurrent use.
Caveats ¶
- Calling memguard.Purge rotates memguard's session key, after which every existing GuardedSecret is permanently undecryptable and Secret() panics.
- Every read (Secret, MarshalSecret, ID) briefly mlocks a page; extremely high parallel read fan-out may hit RLIMIT_MEMLOCK, on which memguard panics. Read once and reuse the value in hot paths.
Custom T types ¶
Any custom T whose UnmarshalText/UnmarshalJSON retains the input slice must copy it — the library wipes the decrypted buffer after unmarshaling.
func NewGuardedSecret ¶
func NewGuardedSecret[T any](val T) *GuardedSecret[T]
NewGuardedSecret seals val into a GuardedSecret. Panics if the value cannot be serialized.
func (*GuardedSecret[T]) Destroy ¶
func (s *GuardedSecret[T]) Destroy()
Destroy drops the sealed value; the secret reads as empty afterwards. The plaintext cannot be recovered from the discarded ciphertext.
func (*GuardedSecret[T]) Empty ¶
func (s *GuardedSecret[T]) Empty() bool
func (*GuardedSecret[T]) Equal ¶
func (s *GuardedSecret[T]) Equal(sec Secret[T]) bool
func (*GuardedSecret[T]) GoString ¶
func (s *GuardedSecret[T]) GoString() string
GoString masks the value for %#v, which bypasses String.
func (*GuardedSecret[T]) Hash ¶
func (s *GuardedSecret[T]) Hash() ([32]byte, error)
Hash returns an unsalted SHA-256 hash of the plaintext. For low-entropy secrets (e.g. human-chosen passwords) it allows offline dictionary attacks and must not be exposed to untrusted parties.
func (*GuardedSecret[T]) ID ¶
func (s *GuardedSecret[T]) ID() (string, error)
ID returns a short Base58 identifier derived from GuardedSecret.Hash. See Hash for the dictionary-attack caveat.
func (*GuardedSecret[T]) MarshalJSON ¶
func (s *GuardedSecret[T]) MarshalJSON() ([]byte, error)
func (*GuardedSecret[T]) MarshalSecret ¶
func (s *GuardedSecret[T]) MarshalSecret() ([]byte, error)
MarshalSecret returns a copy of the plaintext bytes from protected memory. The caller should wipe the returned slice after use.
func (*GuardedSecret[T]) MarshalText ¶
func (s *GuardedSecret[T]) MarshalText() ([]byte, error)
func (*GuardedSecret[T]) Secret ¶
func (s *GuardedSecret[T]) Secret() T
Secret decrypts and returns the protected value. Panics on decryption failure (tampered enclave or a purged memguard session).
func (*GuardedSecret[T]) Set ¶
func (s *GuardedSecret[T]) Set(txt string) error
Set implements pflag.Value.
func (*GuardedSecret[T]) String ¶
func (s *GuardedSecret[T]) String() string
String returns a fixed-width mask so that logs never reveal the value or its length.
func (*GuardedSecret[T]) Type ¶
func (s *GuardedSecret[T]) Type() string
Type implements pflag.Value.
func (*GuardedSecret[T]) UnmarshalJSON ¶
func (s *GuardedSecret[T]) UnmarshalJSON(data []byte) error
func (*GuardedSecret[T]) UnmarshalText ¶
func (s *GuardedSecret[T]) UnmarshalText(text []byte) error
type PlainSecret ¶
type PlainSecret[T any] struct { // contains filtered or unexported fields }
PlainSecret holds the value unencrypted in ordinary heap memory; it only masks the value in fmt, text and JSON output.
A *jwt.Token is parsed with jwt.Parser.ParseUnverified: its signature is not checked, so verify it before trusting its claims.
PlainSecret is not safe for concurrent use while it is being mutated (Set, UnmarshalText, UnmarshalJSON, Destroy).
func (*PlainSecret[T]) Destroy ¶
func (s *PlainSecret[T]) Destroy()
Destroy clears the value. A []byte value is wiped in place first; other types (notably strings, which are immutable) are only dropped.
func (*PlainSecret[T]) Empty ¶
func (s *PlainSecret[T]) Empty() bool
func (*PlainSecret[T]) Equal ¶
func (s *PlainSecret[T]) Equal(sec Secret[T]) bool
func (PlainSecret[T]) GoString ¶
func (s PlainSecret[T]) GoString() string
GoString masks the value for %#v, which bypasses String.
func (*PlainSecret[T]) Hash ¶
func (s *PlainSecret[T]) Hash() ([32]byte, error)
Hash returns an unsalted SHA-256 hash of the plaintext. For low-entropy secrets (e.g. human-chosen passwords) it allows offline dictionary attacks and must not be exposed to untrusted parties.
func (*PlainSecret[T]) ID ¶
func (s *PlainSecret[T]) ID() (string, error)
ID returns a short Base58 identifier derived from PlainSecret.Hash. See Hash for the dictionary-attack caveat.
func (PlainSecret[T]) MarshalJSON ¶
func (s PlainSecret[T]) MarshalJSON() ([]byte, error)
func (*PlainSecret[T]) MarshalSecret ¶
func (s *PlainSecret[T]) MarshalSecret() ([]byte, error)
MarshalSecret returns the plaintext bytes. The returned buffer is a copy owned by the caller, who may wipe it after use. Intermediate string conversions are avoided because immutable string copies can never be wiped.
func (PlainSecret[T]) MarshalText ¶
func (s PlainSecret[T]) MarshalText() (text []byte, err error)
func (*PlainSecret[T]) Secret ¶
func (s *PlainSecret[T]) Secret() T
func (*PlainSecret[T]) Set ¶
func (s *PlainSecret[T]) Set(txt string) error
func (PlainSecret[T]) String ¶
func (s PlainSecret[T]) String() string
func (*PlainSecret[T]) Type ¶
func (s *PlainSecret[T]) Type() string
func (*PlainSecret[T]) UnmarshalJSON ¶
func (s *PlainSecret[T]) UnmarshalJSON(data []byte) error
func (*PlainSecret[T]) UnmarshalText ¶
func (s *PlainSecret[T]) UnmarshalText(text []byte) error
UnmarshalText parses text into the secret; empty text clears it. Parse errors are replaced by an ErrInvalidValue error that does not quote the input.
type Secret ¶
type Secret[T any] interface { encoding.TextMarshaler encoding.TextUnmarshaler json.Unmarshaler json.Marshaler pflag.Value Secret() T Empty() bool String() string Hash() ([32]byte, error) ID() (string, error) Equal(s Secret[T]) bool MarshalSecret() (text []byte, err error) Destroy() pflag.Value }
func NewEncryptedSecret ¶
func NewStringFlag ¶
NewStringFlag encrypts val into a new SecretStringFlag. Panics if the value cannot be encrypted.
func NewStringSecret ¶
type Serializable ¶
type Serializable interface {
encoding.TextMarshaler
encoding.TextUnmarshaler
}
type Singleton ¶
type Singleton[T any] struct { // contains filtered or unexported fields }
func NewSingleton ¶
type StringSecret ¶
type StringSecret []byte
StringSecret is a string value encrypted with AES-256-GCM, using the same package key (and SetKey) as EncryptedSecret. Unlike EncryptedSecret it needs no wrapper struct: the slice itself is the ciphertext.
See EncryptedSecret for the key-management, memory-exposure and concurrency caveats that apply here too.
func (StringSecret) GoString ¶
func (s StringSecret) GoString() string
GoString masks the value for %#v, which would otherwise print the ciphertext bytes.
func (*StringSecret) Hash ¶
func (s *StringSecret) Hash() ([32]byte, error)
Hash returns an unsalted SHA-256 hash of the plaintext. For low-entropy secrets (e.g. human-chosen passwords) it allows offline dictionary attacks and must not be exposed to untrusted parties.
func (*StringSecret) ID ¶
func (s *StringSecret) ID() (string, error)
func (StringSecret) MarshalJSON ¶
func (s StringSecret) MarshalJSON() ([]byte, error)
func (*StringSecret) MarshalSecret ¶
func (s *StringSecret) MarshalSecret() (text []byte, err error)
func (StringSecret) MarshalText ¶
func (s StringSecret) MarshalText() (text []byte, err error)
func (*StringSecret) Secret ¶
func (s *StringSecret) Secret() []byte
func (*StringSecret) Set ¶
func (s *StringSecret) Set(val string) error
func (StringSecret) String ¶
func (s StringSecret) String() string
func (*StringSecret) Type ¶
func (s *StringSecret) Type() string
func (*StringSecret) UnmarshalJSON ¶
func (s *StringSecret) UnmarshalJSON(data []byte) error
func (*StringSecret) UnmarshalText ¶
func (s *StringSecret) UnmarshalText(text []byte) error
UnmarshalText implements Secret.