redactorgo

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 4 Imported by: ? 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.

README

redactor-go

The fleet's one allowlist secret-scrubber — so no log line, diagnostic bundle, env dump, or JSON receipt ever leaks a known secret.

What

A Redactor built once from registered secret keys (key names whose values are sensitive — PASSWORD, token, …) and registered secret values (literal credentials to mask wherever they appear), then reused to scrub:

  • Env([]string) / EnvMap(map[string]string) — os.Environ()-shaped pairs, masking the value of any secret-keyed entry and any secret value embedded in a non-secret value.
  • Bytes([]byte) / String(string) — unstructured log/file content, masking every registered secret value.
  • JSON([]byte) — a JSON document, masking the whole subtree under any secret-keyed field and any secret value inside any string leaf.

The model is the two complementary halves every scrubber needs: allowlist-by-key (a token field is redacted regardless of content) + denylist-by-value (a concrete leaked credential is redacted wherever it shows up). The Redactor is immutable after New and safe for concurrent use.

Why

Scrubbing secrets out of diagnostics recurs across every log pipeline, bundle producer, and doctor-style tool. One typed Redactor means uniform masking semantics (longest-match-wins, case-insensitive keys, whole-subtree redaction) and one place to register what is secret — never a hand-rolled strings.Replace loop that misses a key or leaks a nested field again.

Install

go get github.com/pleme-io/redactor-go

Usage

r, err := redactorgo.New(
    redactorgo.WithSecretKeys("PASSWORD", "token", "api_key"),
    redactorgo.WithSecretValues(sess.Token()), // a concrete live credential
)
if err != nil { return errs.Exit(err) }

clean := r.Env(os.Environ())            // []string, secret-keyed values masked
safe, _ := r.JSON(receiptBytes)         // []byte, secret subtrees + values masked
log.Info(r.String(rawLine))             // string, secret values masked

Configuration

None — a pure library. Callers that read the secret-key/value sets from config use shikumi-go and pass them via the With… options.

Release

Pull-model (Go modules): an annotated vX.Y.Z tag is the release; pkg.go.dev indexes it. See the GSDS module delivery FSM.

Documentation

Overview

Package redactorgo is the fleet's one allowlist secret-scrubber — so no log line, diagnostic bundle, env dump, or JSON receipt ever leaks a known secret value. A Redactor is built once from a set of registered secret keys (key names whose values are sensitive, e.g. "PASSWORD", "token") and registered secret values (literal strings to mask wherever they appear), then reused to scrub env-var slices, raw byte streams, and JSON documents into a placeholder-substituted copy.

The model is allowlist-by-key + denylist-by-value, the two complementary halves every diagnostic scrubber needs:

  • Secret keys catch structurally-known secrets: a "PASSWORD" env var, a "token" JSON field — the value is redacted regardless of its content.
  • Secret values catch leaked-known secrets: a concrete credential that shows up embedded in an arbitrary string, URL, or log message.

Pure stdlib + errors-go; safe for concurrent use after construction (the Redactor is immutable once New returns).

Index

Constants

View Source
const DefaultMask = "[REDACTED]"

DefaultMask is the placeholder substituted for a redacted value when no WithMask option overrides it.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Redactor)

Option configures a Redactor during New. Options are applied in order, so a later option of the same kind overrides an earlier one.

func WithCaseSensitiveKeys

func WithCaseSensitiveKeys() Option

WithCaseSensitiveKeys makes secret-key matching case-sensitive. By default keys match case-insensitively ("PASSWORD" == "password"), which is the right posture for env-var and JSON-field name matching.

func WithMask

func WithMask(mask string) Option

WithMask overrides the redaction placeholder (default DefaultMask). An empty mask is ignored so a Redactor never silently produces blank output.

func WithSecretKeys

func WithSecretKeys(keys ...string) Option

WithSecretKeys registers key names whose associated values are secret. A key match scrubs the value in env-var pairs ("KEY=value") and in JSON object fields, regardless of the value's content. Matching is case-insensitive by default (see WithCaseSensitiveKeys).

func WithSecretValues

func WithSecretValues(values ...string) Option

WithSecretValues registers literal secret strings to mask wherever they appear in any scrubbed text, including inside larger strings (URLs, log lines, JSON string values). Empty values are ignored.

type Redactor

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

Redactor is an immutable, options-built secret scrubber. Construct it with New; after construction it holds no mutable state and is safe for concurrent use across goroutines.

func New

func New(opts ...Option) (*Redactor, error)

New builds a Redactor from the given options. At least one secret key or secret value must be registered, otherwise the Redactor would be a no-op and the caller almost certainly forgot to register its secrets — that returns a typed, code-carrying error ("redactor_empty") rather than a silent pass.

func (*Redactor) Bytes

func (r *Redactor) Bytes(b []byte) []byte

Bytes scrubs a raw byte stream (e.g. a log buffer or file contents), masking every registered secret value. It does not interpret structure; for structured documents prefer [JSON]. Returns a freshly-allocated slice; the input is never mutated.

func (*Redactor) Env

func (r *Redactor) Env(env []string) []string

Env scrubs a slice of "KEY=VALUE" environment entries (the os.Environ() shape). For each entry whose KEY is a registered secret key, the VALUE is replaced with the mask; for every entry, registered secret values appearing in the VALUE are also masked. KEY names and the "=" separator are preserved. Returns a new slice; the input is never mutated.

func (*Redactor) EnvMap

func (r *Redactor) EnvMap(env map[string]string) map[string]string

EnvMap is the convenience form of [Env] for a map[string]string. The returned map is freshly allocated; the input is never mutated.

func (*Redactor) JSON

func (r *Redactor) JSON(b []byte) ([]byte, error)

JSON scrubs a JSON document: it walks the decoded value, masking the value of any object field whose name is a registered secret key (the whole subtree under a secret key is replaced with the mask), and masking any registered secret value found inside any string leaf. It returns canonical re-encoded JSON. A document that is not valid JSON yields a typed, code-carrying error ("redactor_json"); callers with non-JSON input should use [Bytes] instead.

func (*Redactor) Mask

func (r *Redactor) Mask() string

Mask returns the placeholder this Redactor substitutes for redacted values.

func (*Redactor) String

func (r *Redactor) String(s string) string

String scrubs a single string by masking every registered secret value that appears anywhere within it. Secret keys do not apply here (there is no key context) — use [Env] or [JSON] for key-aware scrubbing.

Jump to

Keyboard shortcuts

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