Documentation
¶
Overview ¶
Package masker creates masked copies of structured Go values.
For credential-bearing output, construct a dedicated frozen secure instance:
m, err := masker.NewSecure(
masker.WithMaskField("tenantCredential", masker.MaskTypeRedact),
)
if err != nil {
return err
}
masked, err := m.Mask(value)
New creates a general independent instance. Configure it with options or registration methods, then call Freeze before sharing it across goroutines. Package-level helpers delegate to the mutable Default instance and are kept for compatibility; libraries should not use Default as global configuration.
The secure profile fully replaces recognized credential fields with a fixed marker. Preserve-ends and hash rules remain available for explicit non-secret use cases. Masking is field-based and does not reliably discover secrets in arbitrary prose, so callers should omit untrusted free-form strings and must not fall back to raw values when masking fails.
Cache-disabled Mask calls can run concurrently. Cache-enabled Mask calls are serialized to contain mutable reflection destinations in the wrapped dependency. Configuration methods are synchronized, and Freeze prevents subsequent changes.
Index ¶
- Constants
- Variables
- func Float64(tag string, value float64) (float64, error)
- func Int(tag string, value int) (int, error)
- func Mask[T any](target T) (ret T, err error)
- func MaskChar() string
- func MaskPreserveEnds(arg string, value string) (string, error)
- func MaskRedact(_ string, value string) (string, error)
- func MaskRedactAny(_ string, value any) (any, error)
- func RegisterMaskAnyFunc(maskType string, maskFunc MaskAnyFunc)
- func RegisterMaskField(fieldName, maskType string)
- func RegisterMaskFloat64Func(maskType string, maskFunc MaskFloat64Func)
- func RegisterMaskIntFunc(maskType string, maskFunc MaskIntFunc)
- func RegisterMaskStringFunc(maskType string, maskFunc MaskStringFunc)
- func RegisterMaskUintFunc(maskType string, maskFunc MaskUintFunc)
- func SetMaskChar(s string)
- func String(tag, value string) (string, error)
- func Uint(tag string, value uint) (uint, error)
- type MaskAnyFunc
- type MaskFloat64Func
- type MaskIntFunc
- type MaskStringFunc
- type MaskUintFunc
- type Masker
- func (m *Masker) Cache(enabled bool) error
- func (m *Masker) Float64(tag string, value float64) (float64, error)
- func (m *Masker) Freeze() *Masker
- func (m *Masker) Frozen() bool
- func (m *Masker) Int(tag string, value int) (int, error)
- func (m *Masker) Mask(target any) (any, error)
- func (m *Masker) MaskChar() string
- func (m *Masker) MaskFilledString(arg, value string) (string, error)
- func (m *Masker) MaskFixedString(arg, value string) (string, error)
- func (m *Masker) MaskHashString(arg, value string) (string, error)
- func (m *Masker) MaskRandomFloat64(arg string, value float64) (float64, error)
- func (m *Masker) MaskRandomInt(arg string, value int) (int, error)
- func (m *Masker) MaskZero(arg string, value any) (any, error)
- func (m *Masker) RegisterMaskAnyFunc(maskType string, maskFunc MaskAnyFunc) error
- func (m *Masker) RegisterMaskField(fieldName, maskType string) error
- func (m *Masker) RegisterMaskFloat64Func(maskType string, maskFunc MaskFloat64Func) error
- func (m *Masker) RegisterMaskIntFunc(maskType string, maskFunc MaskIntFunc) error
- func (m *Masker) RegisterMaskStringFunc(maskType string, maskFunc MaskStringFunc) error
- func (m *Masker) RegisterMaskUintFunc(maskType string, maskFunc MaskUintFunc) error
- func (m *Masker) SetMaskChar(char string) error
- func (m *Masker) SetTagName(name string) error
- func (m *Masker) String(tag, value string) (string, error)
- func (m *Masker) Uint(tag string, value uint) (uint, error)
- type Option
- func WithCache(enabled bool) Option
- func WithMaskAnyFunc(maskType string, maskFunc MaskAnyFunc) Option
- func WithMaskChar(char string) Option
- func WithMaskField(fieldName, maskType string) Option
- func WithMaskFloat64Func(maskType string, maskFunc MaskFloat64Func) Option
- func WithMaskIntFunc(maskType string, maskFunc MaskIntFunc) Option
- func WithMaskStringFunc(maskType string, maskFunc MaskStringFunc) Option
- func WithMaskUintFunc(maskType string, maskFunc MaskUintFunc) Option
- func WithProfile(profile Profile) Option
- func WithTagName(name string) Option
- type Profile
Constants ¶
const ( MaskTypePreserveEnds = "preserveEnds" MaskTypeRedact = "redact" RedactedValue = "[REDACTED]" )
Variables ¶
var ( // ErrFrozen is returned when configuration is changed after Freeze. ErrFrozen = errors.New("masker configuration is frozen") // ErrInvalidOption is returned when an option cannot produce a valid masker. ErrInvalidOption = errors.New("invalid masker option") )
var Default = mustNew()
Default backs the package-level compatibility helpers. New integrations should construct and freeze an independent Masker instead of mutating Default.
Functions ¶
func MaskChar ¶ added in v0.2.0
func MaskChar() string
MaskChar returns the current character used for masking. from default masker.
func MaskPreserveEnds ¶ added in v0.1.0
func MaskRedact ¶ added in v0.2.0
MaskRedact replaces any non-empty string with a fixed marker that does not disclose the original value or its length.
func MaskRedactAny ¶ added in v0.2.0
MaskRedactAny replaces strings and byte slices with RedactedValue and returns the zero value for other types. The returned value retains the input type so reflection-based struct masking remains assignable.
func RegisterMaskAnyFunc ¶ added in v0.2.0
func RegisterMaskAnyFunc(maskType string, maskFunc MaskAnyFunc)
RegisterMaskAnyFunc registers a masking function that can be applied to any type. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure. from default masker.
func RegisterMaskField ¶ added in v0.2.0
func RegisterMaskField(fieldName, maskType string)
RegisterMaskField allows you to register a mask tag to be applied to the value of a struct field or map key that matches the fieldName. If a mask tag is set on the struct field, it will take precedence. from default masker.
func RegisterMaskFloat64Func ¶ added in v0.2.0
func RegisterMaskFloat64Func(maskType string, maskFunc MaskFloat64Func)
RegisterMaskFloat64Func registers a masking function for float64 values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure. from default masker.
func RegisterMaskIntFunc ¶ added in v0.2.0
func RegisterMaskIntFunc(maskType string, maskFunc MaskIntFunc)
RegisterMaskIntFunc registers a masking function for int values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure. from default masker.
func RegisterMaskStringFunc ¶ added in v0.2.0
func RegisterMaskStringFunc(maskType string, maskFunc MaskStringFunc)
RegisterMaskStringFunc registers a masking function for string values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure. from default masker.
func RegisterMaskUintFunc ¶ added in v0.2.0
func RegisterMaskUintFunc(maskType string, maskFunc MaskUintFunc)
RegisterMaskUintFunc registers a masking function for uint values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure. from default masker.
func SetMaskChar ¶ added in v0.2.0
func SetMaskChar(s string)
SetMaskChar changes the character used for masking from default masker.
Types ¶
type MaskAnyFunc ¶
type MaskAnyFunc = mask.MaskAnyFunc
type MaskFloat64Func ¶
type MaskFloat64Func = mask.MaskFloat64Func
type MaskIntFunc ¶
type MaskIntFunc = mask.MaskIntFunc
type MaskStringFunc ¶
type MaskStringFunc = mask.MaskStringFunc
type MaskUintFunc ¶
type MaskUintFunc = mask.MaskUintFunc
type Masker ¶
type Masker struct {
// contains filtered or unexported fields
}
Masker wraps go-mask with independent configuration and a concurrency-safe lifecycle. Configuration methods take an exclusive lock; masking is safe for concurrent use. Freeze prevents later configuration changes.
func New ¶ added in v0.2.0
New constructs an independent masker with built-in functions and the default field profile. Options are applied before the instance becomes visible.
func NewSecure ¶ added in v0.2.0
NewSecure constructs and freezes an independent security-profiled masker. Callers can supply additional options to override defaults before freezing.
func (*Masker) Freeze ¶ added in v0.2.0
Freeze prevents subsequent configuration changes. Masking remains available.
func (*Masker) Mask ¶
Mask returns a deep masked copy. Calls are serialized when the wrapped dependency's reflection cache is enabled because that cache reuses mutable destinations. Cache-disabled calls may execute concurrently.
func (*Masker) MaskFilledString ¶
func (*Masker) MaskFixedString ¶
func (*Masker) MaskRandomFloat64 ¶
func (*Masker) RegisterMaskAnyFunc ¶
func (m *Masker) RegisterMaskAnyFunc(maskType string, maskFunc MaskAnyFunc) error
func (*Masker) RegisterMaskField ¶
RegisterMaskField registers a rule for common naming variants of fieldName.
func (*Masker) RegisterMaskFloat64Func ¶
func (m *Masker) RegisterMaskFloat64Func(maskType string, maskFunc MaskFloat64Func) error
func (*Masker) RegisterMaskIntFunc ¶
func (m *Masker) RegisterMaskIntFunc(maskType string, maskFunc MaskIntFunc) error
func (*Masker) RegisterMaskStringFunc ¶
func (m *Masker) RegisterMaskStringFunc(maskType string, maskFunc MaskStringFunc) error
func (*Masker) RegisterMaskUintFunc ¶
func (m *Masker) RegisterMaskUintFunc(maskType string, maskFunc MaskUintFunc) error
func (*Masker) SetMaskChar ¶
SetMaskChar changes the character used for masking.
func (*Masker) SetTagName ¶
SetTagName changes the struct tag name used for masking.
type Option ¶ added in v0.2.0
type Option func(*config) error
Option configures a newly constructed Masker.
func WithCache ¶ added in v0.2.0
WithCache enables or disables the wrapped masker's reflection cache.
func WithMaskAnyFunc ¶ added in v0.2.0
func WithMaskAnyFunc(maskType string, maskFunc MaskAnyFunc) Option
WithMaskAnyFunc registers a custom masking function for any supported value.
func WithMaskChar ¶ added in v0.2.0
WithMaskChar changes the character used by filled and preserve-ends masks.
func WithMaskField ¶ added in v0.2.0
WithMaskField registers a masking rule for a struct field or string map key.
func WithMaskFloat64Func ¶ added in v0.2.0
func WithMaskFloat64Func(maskType string, maskFunc MaskFloat64Func) Option
WithMaskFloat64Func registers a custom float64 masking function.
func WithMaskIntFunc ¶ added in v0.2.0
func WithMaskIntFunc(maskType string, maskFunc MaskIntFunc) Option
WithMaskIntFunc registers a custom int masking function.
func WithMaskStringFunc ¶ added in v0.2.0
func WithMaskStringFunc(maskType string, maskFunc MaskStringFunc) Option
WithMaskStringFunc registers a custom string masking function.
func WithMaskUintFunc ¶ added in v0.2.0
func WithMaskUintFunc(maskType string, maskFunc MaskUintFunc) Option
WithMaskUintFunc registers a custom uint masking function.
func WithProfile ¶ added in v0.2.0
WithProfile selects a set of preconfigured field rules.
func WithTagName ¶ added in v0.2.0
WithTagName changes the struct tag used for masking rules.
type Profile ¶ added in v0.2.0
type Profile string
Profile selects the preconfigured field rules installed by New.
const ( // ProfileDefault preserves the package's historical field behavior. ProfileDefault Profile = "default" // ProfileSecure fully redacts credential-bearing fields with a fixed marker. ProfileSecure Profile = "secure" // ProfileNone installs no field-name rules. Built-in masking functions remain available. ProfileNone Profile = "none" )