credsub

package
v0.0.0-...-230ac96 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package credsub implements the per-session credential substitution table that maps fake credentials (the bytes an agent sees in its environment) to real credentials (the bytes sent upstream).

A Table is created at session start, populated with one entry per configured service, and zeroed at session close. It exposes byte-level substitution in both directions:

  • ReplaceFakeToReal is used on outbound request bodies, headers, query strings, and URL paths before they leave agentmon.
  • ReplaceRealToFake is used on inbound response bodies before they reach the agent (when the matched service has scrub_response: true).

Table enforces length preservation (len(fake) == len(real)) and basic collision invariants at Add time; it does NOT enforce that one fake cannot be a substring of another. Callers are responsible for generating fakes with sufficient entropy (the design spec mandates ≥24 random base62 characters) so accidental substring collisions are astronomically unlikely.

Plan 2 of the external-secrets roadmap lands this package in isolation. Providers, session wiring, and the egress flow are implemented in later plans.

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyValue = errors.New("credsub: fake and real must be nonempty")

ErrEmptyValue is returned by Table.Add when either the fake or real slice is zero-length. A zero-length pattern would match every position in any body and is never a valid credential.

View Source
var ErrFakeCollision = errors.New("credsub: fake or real collides with existing entry")

ErrFakeCollision is returned by Table.Add when the new (fake, real) pair would corrupt substitution. The covered cases are:

  • The new fake equals the new real (same Add call). This would leak the real credential into agent-visible space because ReplaceRealToFake on a response containing the real value would return that same value, and the agent would treat it as the fake.
  • The new fake exactly equals an existing entry's fake.
  • The new fake exactly equals an existing entry's real.
  • The new real exactly equals an existing entry's fake.
  • The new real exactly equals an existing entry's real. Two services sharing the same upstream credential makes ReplaceRealToFake ambiguous: the real value would be rewritten to whichever fake was registered first, returning the wrong service token to the agent.

Any of these would cause substitution to double-swap, leak the real credential, or corrupt data, so Add rejects them up front.

View Source
var ErrLengthMismatch = errors.New("credsub: fake and real must have equal length")

ErrLengthMismatch is returned by Table.Add when the length of the fake byte slice does not equal the length of the real byte slice. Length preservation is required for in-place byte rewriting and to avoid recomputing Content-Length at substitution time.

View Source
var ErrServiceExists = errors.New("credsub: service already registered")

ErrServiceExists is returned by Table.Add when a service name is already registered in the table. Each service has at most one (fake, real) pair per session.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	// ServiceName is the logical service this entry belongs to
	// (for example "github" or "anthropic"). Unique within a Table.
	ServiceName string

	// Fake is the bytes the agent sees. Equal length to Real.
	Fake []byte

	// Real is the bytes sent upstream. Equal length to Fake.
	Real []byte

	// AddedAt records when this entry was registered, for
	// diagnostics. Not used by substitution logic.
	AddedAt time.Time
}

Entry is one (fake, real) substitution pair owned by a Table. The Fake and Real byte slices are private copies owned by the Table; callers must not mutate them after Add returns.

type Table

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

Table is a per-session credential substitution table. The zero value is not usable; construct one with New. Table is safe for concurrent use by multiple goroutines.

func New

func New() *Table

New returns an empty, ready-to-use Table.

func (*Table) Add

func (t *Table) Add(serviceName string, fake, real []byte) error

Add registers a (fake, real) substitution pair for a named service.

Add enforces these invariants:

  • len(fake) == len(real) (see ErrLengthMismatch)
  • both slices are nonempty (see ErrEmptyValue)
  • fake != real within the same call (see ErrFakeCollision)
  • serviceName is not already registered (see ErrServiceExists)
  • fake or real does not collide with any existing entry's fake or real (see ErrFakeCollision)

Add COPIES the input slices. Callers may mutate or zero their copies after Add returns.

func (*Table) Contains

func (t *Table) Contains(fake []byte) (Entry, bool)

Contains reports whether a byte sequence is a registered fake in the table. It performs an EXACT match (not a substring search). If found, it returns a deep-copied Entry (Fake and Real are fresh slices the caller may freely retain or mutate) and true; otherwise it returns a zero Entry and false.

func (*Table) ContainsFake

func (t *Table) ContainsFake(data []byte) (string, bool)

ContainsFake scans data for any registered fake as a substring. Unlike Contains (which requires an exact match), ContainsFake uses bytes.Contains to detect fakes embedded in larger payloads such as JSON request bodies.

Returns the service name of the first matching entry and true if a fake is found; returns "" and false otherwise. When multiple entries match, the first one registered wins.

Only the service name is returned so that callers never receive a copy of the Real credential bytes, which Zero() would be unable to wipe.

func (*Table) FakeForService

func (t *Table) FakeForService(serviceName string) ([]byte, bool)

FakeForService returns the fake byte sequence registered for a service. The returned slice is a copy; the caller may retain or mutate it without affecting the Table. Returns (nil, false) if no entry is registered for the given service name.

func (*Table) Len

func (t *Table) Len() int

Len returns the number of entries currently in the table.

func (*Table) RealForService

func (t *Table) RealForService(serviceName string) ([]byte, bool)

RealForService returns the real byte sequence registered for a service. The returned slice is a deep copy; the caller may retain or mutate it. Callers should zero the returned slice when done to avoid leaving credential material in memory.

This method is intended for internal proxy use only (e.g. header injection). Returns (nil, false) if no entry is registered.

func (*Table) ReplaceFakeToReal

func (t *Table) ReplaceFakeToReal(body []byte) []byte

ReplaceFakeToReal returns a copy of body with every occurrence of every registered fake replaced by its matching real. If body contains no registered fake, the original slice is returned unchanged (the result may alias body in that case); otherwise a freshly allocated slice is returned. Callers must treat the result as authoritative and not assume aliasing either way.

The scan walks the ORIGINAL body once, checking at each position whether any entry's fake starts there, and emitting the matching real if so. This is critical: applying entry replacements sequentially with bytes.ReplaceAll on the evolving output would cascade — bytes produced by an earlier replacement could match a later entry's fake even though those bytes were not in the original body. The single-pass scan over the original body prevents this.

When two entries' fakes both match at the same position (one is a prefix of the other), the longer match wins. Add does not prevent such substring overlaps, so this rule makes the output independent of registration order.

Length-preservation: because Add enforces len(fake) == len(real), the output length always equals len(body).

Complexity: O(N · |body|) where N is the number of entries.

func (*Table) ReplaceRealToFake

func (t *Table) ReplaceRealToFake(body []byte) []byte

ReplaceRealToFake returns a copy of body with every occurrence of every registered real replaced by its matching fake. This is used by the egress flow when a service has scrub_response: true and the proxy needs to rewrite a response body before returning it to the agent, ensuring the agent never sees the real credential even if the upstream echoed it back.

As with ReplaceFakeToReal, the scan walks the ORIGINAL body once and uses leftmost-longest matching to avoid cascading rewrites and to make the result independent of registration order. See the ReplaceFakeToReal doc comment for the reasoning.

If body contains no registered real, the original slice is returned unchanged (the result may alias body in that case); otherwise a freshly allocated slice is returned. Callers must treat the result as authoritative and not assume aliasing either way.

Length-preservation: because Add enforces len(fake) == len(real), the output length always equals len(body).

Complexity: O(N · |body|) where N is the number of entries.

func (*Table) Zero

func (t *Table) Zero()

Zero wipes every Fake and Real byte buffer owned by the table (overwriting them with 0x00) and drops all entries. It is intended to be called on session close so credential material does not linger in process memory after the session ends.

Zero is safe to call multiple times and on an empty table. After Zero returns, the Table is empty and may be reused (though in practice Tables are one-per-session and discarded after Zero).

Jump to

Keyboard shortcuts

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