Documentation
¶
Overview ¶
Package keygen generates synthetic test keys from registry patterns (AD-16).
Every key matches its detector's regex and carries the TOKENHOUNDTEST marker, placed or encoded as the pattern's test_marker declares, so findings from test keys are recognisable and suppressed by default. Keys are generated at test time only and are never written into the repository.
Alternatives are chosen at random, except that an empty-width alternative (^, $, \b, empty) always wins, so boundary groups add no character to a key.
Output depends only on the registry, the detector id and the seed: the PRNG is math/rand/v2 PCG, so the same seed yields byte-identical keys on every OS.
Index ¶
- Constants
- Variables
- func B64(s string) string
- func B64URL(s string) string
- func Expand(reg *registry.Registry, tmpl string, seed uint64) (string, error)
- func HasMarker(key string) bool
- func JSONEsc(s string) string
- func Key(reg *registry.Registry, id string, seed uint64) (string, error)
- func KeyPart(reg *registry.Registry, provider, part string, seed uint64) (string, error)
- func MarkerFor(pat registry.Pattern) []string
- func Pct(s string) string
- type UnknownDetectorError
Constants ¶
const Marker = "TOKENHOUNDTEST"
Marker is the plain-text marker every generated key carries.
Variables ¶
var ( // ErrUnknownDetector: the id names no provider or detector in the registry. ErrUnknownDetector = errors.New("keygen: unknown detector id") // ErrNoMarkerSlot: no character-class repeat in the pattern can hold the marker // in the declared encoding. ErrNoMarkerSlot = errors.New("keygen: pattern has no character-class repeat that can hold the marker") // ErrUnsupportedRegex: the regex uses a construct the generator cannot produce. ErrUnsupportedRegex = errors.New("keygen: unsupported regex construct") // ErrNoMatch: the generated key does not match its own regex. ErrNoMatch = errors.New("keygen: generated key does not match its regex") // ErrTemplate: the fixture template is malformed. ErrTemplate = errors.New("keygen: invalid template") )
Errors returned by Key and Expand.
Functions ¶
func Expand ¶
Expand executes a fixture template. {{key "<id>"}} expands to Key(reg, id, seed); {{key "<id>" N}} yields a different key for each variant N, so one fixture can hold two distinct keys of the same detector. The same id and variant always expand to the same key within one seed.
{{keypart "<provider>" "<part>"}} expands to the key of a credential part, by the part name the registry declares ({{keypart "aws" "secret_access_key"}}); it takes the same optional variant N and is the same key as {{key}} of the part's pattern.
Encoding functions wrap a key for decode fixtures: {{b64 (key "openai")}} is standard padded base64, b64url URL-safe base64 without padding, pct percent-encodes every byte as %XX, and jsonesc writes every - and _ as a \u00XX JSON escape and every / as \/. They nest: {{b64 (b64 (key "openai"))}}.
func Key ¶
Key returns a synthetic key for id, which is a detector id ("openai.project-key") or a provider id or alias ("openai", meaning the provider's first pattern in file order). The same registry, id and seed always yield the same key.
func KeyPart ¶
KeyPart returns a synthetic key for the credential part named part of provider (KeyPart(reg, "aws", "secret_access_key", seed)): the key of the part's pattern.
Types ¶
type UnknownDetectorError ¶
type UnknownDetectorError struct {
ID string
}
UnknownDetectorError names the id that was not found. It wraps ErrUnknownDetector.
func (*UnknownDetectorError) Error ¶
func (e *UnknownDetectorError) Error() string
func (*UnknownDetectorError) Unwrap ¶
func (e *UnknownDetectorError) Unwrap() error
Unwrap returns ErrUnknownDetector.