secret

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package secret holds values that must never reach a log: passwords, keys, tokens. Every way of printing one (fmt, slog, JSON) writes [redacted]; only an explicit Reveal gives the value, at the one place that needs it.

A Keyring seals values under the instance's secret key for storage, so only an instance holding the key reads them back, and derives the keys of other uses from it.

Index

Constants

View Source
const Base62Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

Base62Alphabet is the alphabet credential values are drawn from: letters and digits, safe in a header, a URL and a shell without quoting.

View Source
const MinKeyLength = 32

MinKeyLength is the fewest bytes a secret key may have. Every key the keyring uses is derived from the secret key, and derivation adds no strength: a short key would make sealed secrets and session signatures guessable.

Variables

View Source
var ErrCannotOpen = errors.New("the sealed value cannot be opened")

ErrCannotOpen is every failure to open a sealed value alike: another key, another purpose, a tampered value or no sealed value at all, since telling them apart would help whoever tampers.

View Source
var ErrSecretKeyChanged = errors.New("FLAGNETIC_SECRET_KEY does not open this instance's canary: " +
	"it is not the key the instance was set up with, and what the instance sealed cannot be read without that key; " +
	"start with the original key")

ErrSecretKeyChanged is a secret key that does not open the canary the instance stored: not the key the instance was set up with.

Functions

func Base62

func Base62(random io.Reader, n int) string

Base62 draws n characters of Base62Alphabet from random without bias: a byte is used only below 248, the largest multiple of 62 a byte holds, since reducing the bytes above it would favour the first characters. It is the one generator of the platform's credential values.

func IsBase62

func IsBase62(s string) bool

IsBase62 reports whether s holds only characters of Base62Alphabet.

Types

type Binding added in v0.5.0

type Binding string

Binding names the row a sealed value belongs to, so a sealed value copied into another row, by whoever can write the database, does not open there.

func Row added in v0.5.0

func Row(table, id string) Binding

Row returns the binding of the row of table with identifier id.

type Keyring

type Keyring struct {
	// contains filtered or unexported fields
}

Keyring seals and opens values, and derives keys, under the instance's secret key.

func NewKeyring

func NewKeyring(secretKey String) *Keyring

NewKeyring returns the keyring of secretKey.

func (*Keyring) CheckCanary

func (k *Keyring) CheckCanary(canary string) error

CheckCanary returns ErrSecretKeyChanged unless the secret key opens canary.

func (*Keyring) Key

func (k *Keyring) Key(use Use) []byte

Key returns the key derived for use, the same for the same secret key.

func (*Keyring) NewCanary

func (k *Keyring) NewCanary() (string, error)

NewCanary returns a new canary: random bytes sealed under the secret key, stored once by the instance and opened on every start.

func (*Keyring) Open

func (k *Keyring) Open(purpose Purpose, sealed string, binding Binding) ([]byte, error)

Open returns the plaintext of a value sealed for purpose and binding, or ErrCannotOpen. A value in the version before rows were bound opens whatever the binding.

func (*Keyring) Rebind added in v0.5.0

func (k *Keyring) Rebind(purpose Purpose, sealed string, binding Binding) (string, error)

Rebind returns sealed bound to binding: a value sealed before rows were bound is opened and sealed again for binding, so that the write of its row leaves it bound. A value already bound, an empty one and one that does not open are returned as they are: rebinding never makes an unrelated write of the row fail over a value that only its own use can judge.

func (*Keyring) Seal

func (k *Keyring) Seal(purpose Purpose, plaintext []byte, binding Binding) (string, error)

Seal encrypts plaintext for purpose with AES-256-GCM under a fresh random nonce, authenticating binding with it, and returns "v2:" and the base64url of the nonce and the ciphertext.

type Purpose

type Purpose string

Purpose names what a sealed value is for. Each purpose seals under a key of its own, so a value sealed for one purpose never opens as another.

const (
	// PurposeCanary seals the canary.
	PurposeCanary Purpose = "canary"
	// PurposeWebhookSecret seals the secrets signing webhook calls.
	PurposeWebhookSecret Purpose = "webhook secret"
	// PurposeSinkClickHousePassword seals the password of a ClickHouse sink.
	PurposeSinkClickHousePassword Purpose = "sink_clickhouse_password"
	// PurposeSinkObjectStorageSecret seals the secret access key of an
	// object storage sink.
	PurposeSinkObjectStorageSecret Purpose = "sink_object_storage_secret"
	// PurposeSinkWebhookSecret seals the secret signing a webhook sink's
	// calls.
	PurposeSinkWebhookSecret Purpose = "sink_webhook_secret" //nolint:gosec // a label, not a credential
	// PurposeOIDCClientSecret seals the client secrets of OIDC auth providers.
	PurposeOIDCClientSecret Purpose = "oidc client secret" //nolint:gosec // a label, not a credential
	// PurposeSMTPPassword seals the password the instance authenticates to
	// its SMTP server with.
	PurposeSMTPPassword Purpose = "smtp password"
	// PurposeSCIMToken seals the hashes of SCIM tokens, which are compared by
	// opening each one rather than found by an index.
	PurposeSCIMToken Purpose = "scim token" //nolint:gosec // a label, not a credential
	// PurposeSAMLSPKey seals the private keys the instance presents to SAML
	// identity providers.
	PurposeSAMLSPKey Purpose = "saml sp key"
)

The purposes values are sealed for.

type String

type String struct {
	// contains filtered or unexported fields
}

String is a secret string. Its zero value is the empty secret.

func New

func New(v string) String

New wraps v.

func (String) GoString

func (String) GoString() string

GoString returns [redacted], for %#v.

func (String) IsZero

func (s String) IsZero() bool

IsZero reports whether the secret is empty.

func (String) LogValue

func (String) LogValue() slog.Value

LogValue returns [redacted].

func (String) MarshalText

func (String) MarshalText() ([]byte, error)

MarshalText returns [redacted], so an encoded configuration hides it too.

func (String) Reveal

func (s String) Reveal() string

Reveal returns the value. Call it only where the value is used, never to log.

func (String) String

func (String) String() string

String returns [redacted].

type Use

type Use string

Use names a key derived from the secret key for something other than sealing.

const UseSessionCookie Use = "session cookie"

UseSessionCookie keys the HMAC signing session cookies.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL