cfgredact

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 13 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

cfgredact

Hide secrets in config values before they leave a trust boundary.

import "github.com/ivaltryek/cfgredact"

var r cfgredact.Redactor // defaults: DefaultPattern, "[redacted]"

r.String(`{"user": "a", "password": "x"}`)
// {"password":"[redacted]","user":"a"}

r.String("[auth]\nuser = a\npsk = x\n")
// [auth]
// user = a
// psk = [redacted]

What it does

  • Accepts one config value as a string (String) or a parsed tree (Tree). Tree walks map[string]any, map[string]string, map[any]any, []any and []string. Other types come back unchanged.
  • Tries JSON, then JSONC, then YAML maps and lists, then INI/env lines, then YAML plain text. The first parser that accepts the value wins. INI-like text that YAML reads as a map, such as a=!psk: x, is YAML.
  • Hides the value under each key whose name matches the secret rule, at any depth.
  • Hides value in a map whose name or key is a secret name, as in env entries and Argo CD parameters.
  • Treats every nested string as a config value of its own (JSON inside YAML, and so on).
  • Hides PEM private-key blocks and the password in scheme://user:pass@host. Set SkipPEM or SkipURLPassword to keep them.

Output

  • If nothing is hidden, the value comes back byte for byte.
  • If something is hidden, the value is re-encoded in its own format: JSONC becomes JSON, and YAML stays YAML. Comments, indentation and key order are lost.
  • A comment that names a secret also triggers a re-encode, which drops the comment.
  • If a YAML re-encode would fold a secret line, such as --psk=x inside a multi-line > or plain scalar, into the line above, the value becomes the marker.
  • A value that no parser accepts, such as a Helm template, keeps its lines when every line is a plain key: value, key = value, - item, [section], {{ }} or # comment line. A secret key's value becomes the marker, and a comment that names a secret is hidden. Anything else, such as a script, becomes the marker. So does a stream of several YAML documents that holds a secret.
  • An object with a repeated key is re-encoded with the last value, as the parsers read it. The other values are dropped, because one may hold a secret.
  • Plain text, such as uses token auth, keeps its bytes. If a YAML comment in it names a secret, the comment is dropped. Multi-line plain text with such a comment becomes the marker.
  • A comment-only re-encode of YAML keeps each scalar as written (0755 stays 0755).
  • Each line key: value, key = value or export KEY=value with a secret key is hidden to the end of the line, as rehearse did. In JSON and JSONC, other keys on that line are kept: only the secret's value is hidden.
  • Strings nested more than 16 levels deep become the marker.

What it does not do

  • It does not scan values for secrets by shape (entropy, token formats). A secret under an innocent key name is not hidden.
  • It has no Kubernetes knowledge.

Configuration

r := cfgredact.Redactor{
	Secret: regexp.MustCompile(`(?i)(password|token)`), // nil: DefaultPattern
	Marker: "***",                                     // "": DefaultMarker
}

Secret also runs on raw text: comments, lines and values that no parser accepts. It must match a secret name anywhere in a text, so do not anchor it with ^ or $.

A Redactor has no mutable state and is safe for concurrent use.

Parity with rehearse

cfgredact replaced redact.ConfigValue in rehearse. legacy_test.go keeps that code. FuzzDiff checks that cfgredact hides every secret it hid. Run it for longer before a release. Without -fuzzminimizetime, input minimizing stalls the run for up to a minute at a time.

go test -run '^$' -fuzz FuzzDiff -fuzztime 60s -fuzzminimizetime 1s

Design notes: docs/design.md.

License

MIT

Documentation

Overview

Package cfgredact hides secrets in config values before they leave a trust boundary. A value can be JSON, JSONC, YAML, or INI and env lines. Secrets are found by key name, at any depth, and in strings nested inside other config values.

r := cfgredact.Redactor{Secret: regexp.MustCompile(`(?i)password`)}
r.String(`{"user": "a", "password": "x"}`) // {"password":"[redacted]","user":"a"}

A value with nothing to hide comes back byte for byte. A redacted value is re-encoded in its own format, so comments and key order are lost. A value that names a secret but that no parser accepts becomes the marker.

Index

Constants

View Source
const DefaultMarker = "[redacted]"

DefaultMarker replaces a hidden value.

View Source
const DefaultPattern = `(?i)(passw|psk|secret|token|credential|private.?key|api.?key|access.?key|auth.?key|setup.?key)`

DefaultPattern matches secret names. It is the rule when Redactor.Secret is nil.

Variables

This section is empty.

Functions

This section is empty.

Types

type Redactor

type Redactor struct {
	// Secret matches secret names. Nil means DefaultPattern. It also runs
	// on raw text: on comments, on lines and on values that no parser
	// accepts. So it must match a secret name anywhere in a text. Do not
	// anchor it with ^ or $.
	Secret *regexp.Regexp
	// Marker replaces a hidden value. Empty means DefaultMarker.
	Marker string
	// SkipPEM keeps PEM private-key blocks.
	SkipPEM bool
	// SkipURLPassword keeps the password in scheme://user:pass@host.
	SkipURLPassword bool
}

Redactor hides secrets. The zero value uses the defaults. A Redactor is safe for concurrent use.

func (Redactor) String

func (r Redactor) String(s string) string

String hides the secrets in one config value.

func (Redactor) Tree

func (r Redactor) Tree(v any) any

Tree hides the secrets in a parsed value: map[string]any, map[string]string, map[any]any, []any, []string and scalars. Strings are handled as config values. Other types come back unchanged. Tree does not change v.

Jump to

Keyboard shortcuts

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