Documentation
¶
Index ¶
- func Slice[T any](s []T, obfuscator Obfuscator) []string
- type ErrorStrategy
- type HTTPHeaderObfuscator
- func (o HTTPHeaderObfuscator) ObfuscateHeaderMap(headerMap map[string]string) map[string]string
- func (o HTTPHeaderObfuscator) ObfuscateHeaderMultiMap(headerMap map[string][]string) map[string][]string
- func (o HTTPHeaderObfuscator) ObfuscateHeaderValue(headerName, headerValue string) string
- func (o HTTPHeaderObfuscator) ObfuscateHeaderValues(headerName string, headerValues []string) []string
- type HTTPParameterObfuscator
- func (o HTTPParameterObfuscator) ObfuscateParameter(name, value string) string
- func (o HTTPParameterObfuscator) ObfuscateString(s string) string
- func (o HTTPParameterObfuscator) ParseAndObfuscateString(s string) (string, error)
- func (o HTTPParameterObfuscator) UntilLength(prefixLength int) ObfuscatorPrefix
- type HTTPParameterObfuscatorBuilder
- func (b *HTTPParameterObfuscatorBuilder) Build() HTTPParameterObfuscator
- func (b *HTTPParameterObfuscatorBuilder) OnErrorDiscard() *HTTPParameterObfuscatorBuilder
- func (b *HTTPParameterObfuscatorBuilder) OnErrorInclude() *HTTPParameterObfuscatorBuilder
- func (b *HTTPParameterObfuscatorBuilder) OnErrorLog(logger *log.Logger) *HTTPParameterObfuscatorBuilder
- func (b *HTTPParameterObfuscatorBuilder) WithParameter(parameterName string, obfuscator Obfuscator) *HTTPParameterObfuscatorBuilder
- type JSONObfuscator
- type JSONObfuscatorBuilder
- func (b *JSONObfuscatorBuilder) Build() JSONObfuscator
- func (b *JSONObfuscatorBuilder) ForArrays(mode ObfuscationMode) *JSONObfuscatorBuilder
- func (b *JSONObfuscatorBuilder) ForObjects(mode ObfuscationMode) *JSONObfuscatorBuilder
- func (b *JSONObfuscatorBuilder) OnErrorDiscard() *JSONObfuscatorBuilder
- func (b *JSONObfuscatorBuilder) OnErrorInclude() *JSONObfuscatorBuilder
- func (b *JSONObfuscatorBuilder) OnErrorLog(logger *log.Logger) *JSONObfuscatorBuilder
- func (b *JSONObfuscatorBuilder) WithProperty(propertyName string, obfuscator Obfuscator, ...) *JSONObfuscatorBuilder
- type JSONPropertyObfuscationOptions
- type MapObfuscator
- type ObfuscationMode
- type Obfuscator
- func All() Obfuscator
- func AllWithMask(mask string) Obfuscator
- func NewObfuscator(obfuscate func(s string) string) Obfuscator
- func NewObfuscatorOnErrorDiscard(parseAndObfuscate func(s string) (string, error)) Obfuscator
- func NewObfuscatorOnErrorInclude(parseAndObfuscate func(s string) (string, error)) Obfuscator
- func NewObfuscatorOnErrorLog(parseAndObfuscate func(s string) (string, error), logger *log.Logger) Obfuscator
- func None() Obfuscator
- func WithFixedLength(fixedLength int) Obfuscator
- func WithFixedLengthWithMask(fixedLength int, mask string) Obfuscator
- func WithFixedValue(fixedValue string) Obfuscator
- type ObfuscatorPrefix
- type PortionBuilder
- func (pb *PortionBuilder) AtLeastFromEnd(value int) *PortionBuilder
- func (pb *PortionBuilder) AtLeastFromStart(value int) *PortionBuilder
- func (pb *PortionBuilder) Build() Obfuscator
- func (pb *PortionBuilder) FixedTotalLength(value int) *PortionBuilder
- func (pb *PortionBuilder) KeepAtEnd(value int) *PortionBuilder
- func (pb *PortionBuilder) KeepAtStart(value int) *PortionBuilder
- func (pb *PortionBuilder) Mask(mask string) *PortionBuilder
- type SplitPoint
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Slice ¶
func Slice[T any](s []T, obfuscator Obfuscator) []string
Slice obfuscates all elements of a slice using the given obfuscator. Values are converted to string using fmt.Sprintf("%v", value). This allows this function to be used not just for string slices but also other scalar types.
Types ¶
type ErrorStrategy ¶
type ErrorStrategy int
ErrorStrategy represents the strategy to follow when an error occurs while obfuscating a string.
const ( // OnErrorLog will cause obfuscation to stop when an error occurs. The error will be logged. OnErrorLog ErrorStrategy = iota // OnErrorInclude will cause obfuscation to stop when an error occurs. The error will be included in the obfuscation result. OnErrorInclude // OnErrorDiscard will cause obfuscation to stop when an error occurs. The error will not be visible in any way. OnErrorDiscard )
func (ErrorStrategy) String ¶
func (es ErrorStrategy) String() string
String implements the fmt.Stringer interface.
type HTTPHeaderObfuscator ¶
type HTTPHeaderObfuscator struct {
// contains filtered or unexported fields
}
HTTPHeaderObfuscator represents an object that can obfuscate HTTP header values.
func HTTPHeaders ¶
func HTTPHeaders(obfuscators map[string]Obfuscator) HTTPHeaderObfuscator
HTTPHeaders creates a new HTTP header obfuscator.
func (HTTPHeaderObfuscator) ObfuscateHeaderMap ¶
func (o HTTPHeaderObfuscator) ObfuscateHeaderMap(headerMap map[string]string) map[string]string
ObfuscateHeaderMap obfuscates all values in a map where the keys are HTTP header names and the values are the matching HTTP header values.
func (HTTPHeaderObfuscator) ObfuscateHeaderMultiMap ¶
func (o HTTPHeaderObfuscator) ObfuscateHeaderMultiMap(headerMap map[string][]string) map[string][]string
ObfuscateHeaderMultiMap obfuscates all values in a map where the keys are HTTP header names and the values are the matching HTTP header values.
func (HTTPHeaderObfuscator) ObfuscateHeaderValue ¶
func (o HTTPHeaderObfuscator) ObfuscateHeaderValue(headerName, headerValue string) string
ObfuscateHeaderValue obfuscates the value for a single header.
func (HTTPHeaderObfuscator) ObfuscateHeaderValues ¶
func (o HTTPHeaderObfuscator) ObfuscateHeaderValues(headerName string, headerValues []string) []string
ObfuscateHeaderValues obfuscates multiple values for a single header.
type HTTPParameterObfuscator ¶
type HTTPParameterObfuscator struct {
// contains filtered or unexported fields
}
HTTPParameterObfuscator represents an object that can obfuscate HTTP query and form parameter strings, as well as separate parameter values.
func (HTTPParameterObfuscator) ObfuscateParameter ¶
func (o HTTPParameterObfuscator) ObfuscateParameter(name, value string) string
ObfuscateParameter obfuscates the given value for a parameter with the given name.
func (HTTPParameterObfuscator) ObfuscateString ¶
func (o HTTPParameterObfuscator) ObfuscateString(s string) string
ObfuscateString implements the Obfuscator interface.
It is like HTTPParameterObfuscator.ParseAndObfuscateString, but it handles any error internally according to the ErrorStrategy provided when the HTTPParameterObfuscator instance was created.
func (HTTPParameterObfuscator) ParseAndObfuscateString ¶
func (o HTTPParameterObfuscator) ParseAndObfuscateString(s string) (string, error)
ParseAndObfuscateString implements the Obfuscator interface.
func (HTTPParameterObfuscator) UntilLength ¶
func (o HTTPParameterObfuscator) UntilLength(prefixLength int) ObfuscatorPrefix
UntilLength implements the Obfuscator interface.
type HTTPParameterObfuscatorBuilder ¶
type HTTPParameterObfuscatorBuilder struct {
// contains filtered or unexported fields
}
HTTPParameterObfuscatorBuilder is a builder for HTTPParameterObfuscator instances.
func HTTPParameters ¶
func HTTPParameters() *HTTPParameterObfuscatorBuilder
HTTPParameters creates a new builder for HTTP parameter obfuscators.
func (*HTTPParameterObfuscatorBuilder) Build ¶
func (b *HTTPParameterObfuscatorBuilder) Build() HTTPParameterObfuscator
Build creates a new HTTP parameter obfuscator using the contents of the builder.
func (*HTTPParameterObfuscatorBuilder) OnErrorDiscard ¶
func (b *HTTPParameterObfuscatorBuilder) OnErrorDiscard() *HTTPParameterObfuscatorBuilder
OnErrorDiscard sets the strategy to follow when an error occurs while obfuscating a string to OnErrorDiscard.
func (*HTTPParameterObfuscatorBuilder) OnErrorInclude ¶
func (b *HTTPParameterObfuscatorBuilder) OnErrorInclude() *HTTPParameterObfuscatorBuilder
OnErrorInclude sets the strategy to follow when an error occurs while obfuscating a string to OnErrorInclude.
func (*HTTPParameterObfuscatorBuilder) OnErrorLog ¶
func (b *HTTPParameterObfuscatorBuilder) OnErrorLog(logger *log.Logger) *HTTPParameterObfuscatorBuilder
OnErrorLog sets the strategy to follow when an error occurs while obfuscating a string to OnErrorLog. If the given logger is nil, fmt.Printf will be used instead.
func (*HTTPParameterObfuscatorBuilder) WithParameter ¶
func (b *HTTPParameterObfuscatorBuilder) WithParameter(parameterName string, obfuscator Obfuscator) *HTTPParameterObfuscatorBuilder
WithParameter registers a parameter to obfuscate. It uses the given obfuscator for obfuscating any occurrence of a parameter with the given name.
type JSONObfuscator ¶
type JSONObfuscator struct {
// contains filtered or unexported fields
}
JSONObfuscator represents an object that can obfuscate JSON strings.
func (JSONObfuscator) ObfuscateString ¶
func (o JSONObfuscator) ObfuscateString(s string) string
ObfuscateString implements the Obfuscator interface.
It is like JSONObfuscator.ParseAndObfuscateString, but it handles any error internally according to the ErrorStrategy provided when the JSONObfuscator instance was created.
func (JSONObfuscator) ParseAndObfuscateString ¶
func (o JSONObfuscator) ParseAndObfuscateString(s string) (string, error)
ParseAndObfuscateString implements the Obfuscator interface.
func (JSONObfuscator) UntilLength ¶
func (o JSONObfuscator) UntilLength(prefixLength int) ObfuscatorPrefix
UntilLength implements the Obfuscator interface.
type JSONObfuscatorBuilder ¶
type JSONObfuscatorBuilder struct {
// contains filtered or unexported fields
}
JSONObfuscatorBuilder is a builder for JSONObfuscator instances.
func (*JSONObfuscatorBuilder) Build ¶
func (b *JSONObfuscatorBuilder) Build() JSONObfuscator
Build creates a new JSON obfuscator using the contents of the builder.
func (*JSONObfuscatorBuilder) ForArrays ¶
func (b *JSONObfuscatorBuilder) ForArrays(mode ObfuscationMode) *JSONObfuscatorBuilder
ForArrays sets the default obfuscation mode for arrays. This is used for any property for which no explicit obfuscation mode has been given.
func (*JSONObfuscatorBuilder) ForObjects ¶
func (b *JSONObfuscatorBuilder) ForObjects(mode ObfuscationMode) *JSONObfuscatorBuilder
ForObjects sets the default obfuscation mode for objects. This is used for any property for which no explicit obfuscation mode has been given.
func (*JSONObfuscatorBuilder) OnErrorDiscard ¶
func (b *JSONObfuscatorBuilder) OnErrorDiscard() *JSONObfuscatorBuilder
OnErrorDiscard sets the strategy to follow when an error occurs while obfuscating a string to OnErrorDiscard.
func (*JSONObfuscatorBuilder) OnErrorInclude ¶
func (b *JSONObfuscatorBuilder) OnErrorInclude() *JSONObfuscatorBuilder
OnErrorInclude sets the strategy to follow when an error occurs while obfuscating a string to OnErrorInclude.
func (*JSONObfuscatorBuilder) OnErrorLog ¶
func (b *JSONObfuscatorBuilder) OnErrorLog(logger *log.Logger) *JSONObfuscatorBuilder
OnErrorLog sets the strategy to follow when an error occurs while obfuscating a string to OnErrorLog. If the given logger is nil, fmt.Printf will be used instead.
func (*JSONObfuscatorBuilder) WithProperty ¶
func (b *JSONObfuscatorBuilder) WithProperty(propertyName string, obfuscator Obfuscator, options *JSONPropertyObfuscationOptions) *JSONObfuscatorBuilder
WithProperty registers a property to obfuscate. It uses the given obfuscator for obfuscating any occurrence of a property with the given name.
When the value of a property with the given name is an object or array, by default it will be ignored. Additional options can be given that determine how to handle these. See ObfuscationMode for more information.
type JSONPropertyObfuscationOptions ¶
type JSONPropertyObfuscationOptions struct {
// ForObjects defines how to handle matched object properties.
ForObjects ObfuscationMode
// ForArrays defines how to handle matched array properties.
ForArrays ObfuscationMode
}
JSONPropertyObfuscationOptions represents obfuscation options for a single property.
type MapObfuscator ¶
type MapObfuscator[K comparable] struct { // contains filtered or unexported fields }
MapObfuscator represents an object that can obfuscate map values.
func Maps ¶
func Maps[K comparable](obfuscators map[K]Obfuscator) MapObfuscator[K]
Maps creates a new map obfuscator.
func (MapObfuscator[K]) ObfuscateMap ¶
func (o MapObfuscator[K]) ObfuscateMap(m map[K]string) map[K]string
ObfuscateMap obfuscates all values in a map.
func (MapObfuscator[K]) ObfuscateMultiMap ¶
func (o MapObfuscator[K]) ObfuscateMultiMap(m map[K][]string) map[K][]string
ObfuscateMultiMap obfuscates all values in a map.
type ObfuscationMode ¶
type ObfuscationMode int
const ( // Exclude means obfuscators will not be used for complex structures like JSON objects or arrays. // Obfuscation will instead traverse in them, and for any nested properties their own obfuscation rules will be applied. Exclude ObfuscationMode = iota // ExcludeAll means obfuscators will not be used for complex structures like JSON objects or arrays. // Obfuscation will not traverse in them, so any nested properties will not be obfuscated. ExcludeAll // Inherit means obfuscators will be used for any nested property of complex structures like JSON objects or arrays. // Any obfuscation rules for nested properties will be ignored. Inherit // InheritOverridable means obfuscators will be used for any nested property of complex structures like JSON objects or arrays. // For any nested properties their own obfuscation rules will be applied. InheritOverridable )
func (ObfuscationMode) String ¶
func (om ObfuscationMode) String() string
String implements the fmt.Stringer interface.
type Obfuscator ¶
type Obfuscator interface {
// ObfuscateString obfuscates the given string.
//
// If the parser needs to do any parsing that can result in an error, this should be handled according to one of the possible [ErrorStrategy] constants.
ObfuscateString(s string) string
// ParseAndObfuscateString parses the given string and obfuscates the parsed result.
//
// If the obfuscator does not need any parsing, this method will do the same as [ObfuscateString], and the error will be nil.
ParseAndObfuscateString(s string) (string, error)
// UntilLength creates a prefix that can be used to chain another obfuscator to this obfuscator.
// For the part up to the given prefix length, this obfuscator will be used; for any remaining content another obfuscator will be used.
// This makes it possible to easily create complex obfuscators that would otherwise be impossible using any of the other obfuscators provided by this module.
//
// The prefix length needs to be at least 1, and larger than all previous lengths in a method chain.
// In other words, each prefix length must be larger than its direct predecessor.
// This method panics if this pre-condition is not met.
UntilLength(prefixLength int) ObfuscatorPrefix
}
Obfuscator represents an object that can obfuscate strings, making them partly or completely unreadable.
func All ¶
func All() Obfuscator
All returns an obfuscator that replaces all characters in strings with an asterisk (*).
func AllWithMask ¶
func AllWithMask(mask string) Obfuscator
AllWithMask returns an obfuscator that replaces all characters in strings with the given mask.
func NewObfuscator ¶
func NewObfuscator(obfuscate func(s string) string) Obfuscator
NewObfuscator creates a new obfuscator that delegates to the given function.
Calling [Obfuscator.ParseAndObfuscateString] on the result will always return a nil error.
func NewObfuscatorOnErrorDiscard ¶
func NewObfuscatorOnErrorDiscard(parseAndObfuscate func(s string) (string, error)) Obfuscator
NewObfuscatorOnErrorDiscard creates a new obfuscator that delegates to the given function.
When calling [Obfuscator.ObfuscateString] on the result with a string that would cause the given function to return an error, this error is discarded.
func NewObfuscatorOnErrorInclude ¶
func NewObfuscatorOnErrorInclude(parseAndObfuscate func(s string) (string, error)) Obfuscator
NewObfuscatorOnErrorInclude creates a new obfuscator that delegates to the given function.
When calling [Obfuscator.ObfuscateString] on the result with a string that would cause the given function to return an error, this error is appended to the result.
func NewObfuscatorOnErrorLog ¶
func NewObfuscatorOnErrorLog(parseAndObfuscate func(s string) (string, error), logger *log.Logger) Obfuscator
NewObfuscatorOnErrorLog creates a new obfuscator that delegates to the given function.
When calling [Obfuscator.ObfuscateString] on the result with a string that would cause the given function to return an error, this error is logged. If the given logger is nil, fmt.Printf will be used instead.
func None ¶
func None() Obfuscator
None returns an obfuscator that does not obfuscate anything. It can be used as default value.
func WithFixedLength ¶
func WithFixedLength(fixedLength int) Obfuscator
WithFixedLength returns an obfuscator that replaces strings with the given fixed length occurrences of a single asterisk (*).
It panics if the fixed length is negative.
func WithFixedLengthWithMask ¶
func WithFixedLengthWithMask(fixedLength int, mask string) Obfuscator
WithFixedLengthWithMask returns an obfuscator that replaces strings with the given fixed length occurrences of the given mask.
It panics if the fixed length is negative or if the result of (len(mask) * fixedLength) overflows.
func WithFixedValue ¶
func WithFixedValue(fixedValue string) Obfuscator
WithFixedValue returns an obfuscator that replaces strings with the given fixed value.
type ObfuscatorPrefix ¶
type ObfuscatorPrefix struct {
// contains filtered or unexported fields
}
ObfuscatorPrefix represents a prefix of a specific length that uses a specific obfuscator. It can be used to create combined obfuscators that obfuscate strings for the part up to the length of this prefix using the prefix' obfuscator, then the rest with another.
func NewObfuscatorPrefix ¶
func NewObfuscatorPrefix(o Obfuscator, prefixLength int) ObfuscatorPrefix
NewObfuscatorPrefix creates a new ObfuscatorPrefix.
This function is intended only for implementing custom obfuscators. It should not be used directly.
func (ObfuscatorPrefix) Then ¶
func (op ObfuscatorPrefix) Then(other Obfuscator) Obfuscator
Then returns an obfuscator that first uses the obfuscator that was used to create the receiver for the length of the receiver, then another obfuscator.
type PortionBuilder ¶
type PortionBuilder struct {
// contains filtered or unexported fields
}
PortionBuilder represents a builder for obfuscators that obfuscates a specific portion of their input.
func Portion ¶
func Portion() *PortionBuilder
Portion returns a builder for obfuscators that obfuscates a specific portion of their input.
func (*PortionBuilder) AtLeastFromEnd ¶
func (pb *PortionBuilder) AtLeastFromEnd(value int) *PortionBuilder
AtLeastFromEnd sets the minimum number of characters from the end that need to be obfuscated. This will overrule any value for PortionBuilder.KeepAtStart or PortionBuilder.KeepAtEnd. Defaults to 0.
It panics if the given value is negative.
func (*PortionBuilder) AtLeastFromStart ¶
func (pb *PortionBuilder) AtLeastFromStart(value int) *PortionBuilder
AtLeastFromStart sets the minimum number of characters from the start that need to be obfuscated. This will overrule any value for PortionBuilder.KeepAtStart or PortionBuilder.KeepAtEnd. Defaults to 0.
It panics if the given value is negative.
func (*PortionBuilder) Build ¶
func (pb *PortionBuilder) Build() Obfuscator
Build returns an obfuscator that obfuscates a specific portion of their input, using the values configured in this builder.
It panics if a PortionBuilder.FixedTotalLength is set that is smaller than the sum of the (default) values for PortionBuilder.KeepAtStart and PortionBuilder.KeepAtEnd.
func (*PortionBuilder) FixedTotalLength ¶
func (pb *PortionBuilder) FixedTotalLength(value int) *PortionBuilder
FixedTotalLength sets the fixed total length to use for obfuscated contents. When obfuscating, the result will have the mask added until this total length has been reached.
Note: when used in combination with PortionBuilder.KeepAtStart and/or PortionBuilder.KeepAtEnd, this total length must be at least the sum of both other values. When used in combination with both, parts of the input may be repeated in the obfuscated content if the input's length is less than the combined number of characters to keep.
It panics if the given value is negative.
func (*PortionBuilder) KeepAtEnd ¶
func (pb *PortionBuilder) KeepAtEnd(value int) *PortionBuilder
KeepAtEnd sets the number of characters at the end that created obfuscators will skip when obfuscating. Defaults to 0.
It panics if the given value is negative.
func (*PortionBuilder) KeepAtStart ¶
func (pb *PortionBuilder) KeepAtStart(value int) *PortionBuilder
KeepAtStart sets the number of characters at the start that created obfuscators will skip when obfuscating. Defaults to 0.
It panics if the given value is negative.
func (*PortionBuilder) Mask ¶
func (pb *PortionBuilder) Mask(mask string) *PortionBuilder
Mask sets the string to use for masking. Defaults to a single asterisk (*).
It panics if the given mask is empty.
type SplitPoint ¶
type SplitPoint struct {
// contains filtered or unexported fields
}
SplitPoint represents a point in a string to split obfuscation. Like Obfuscator.UntilLength it can be used to combine obfuscators. For instance, to obfuscate email addresses:
localPartObfuscator := obfuscate.Portion()
.KeepAtStart(1)
.KeepAtEnd(1)
.FixedTotalLength(8)
.Build()
domainObfuscator := obfuscate.None()
obfuscator := obfuscate.AtFirst("@").SplitTo(localPrtObfuscator, domainObfuscator)
// Everything before @ will be obfuscated using localPartObfuscator, everything after @ will not be obfuscated.
// Example input: [email protected]
// Example output: t******[email protected]
Unlike Obfuscator.UntilLength it's not possible to chain splitting, but it's possible to nest it:
localPartObfuscator := obfuscate.Portion()
.KeepAtStart(1)
.KeepAtEnd(1)
.FixedTotalLength(8)
.Build()
domainObfuscator := obfuscate.AtLast(".").SplitTo(obfuscate.All(), obfuscate.None())
obfuscator := obfuscate.AtFirst("@").SplitTo(localPartObfuscator, domainObfuscator)
// Everything before @ will be obfuscated using localPartObfuscator, everything after @ will be obfuscated until the last dot
// Example input: [email protected]
// Example output: t******t@*******.org
func AtFirst ¶
func AtFirst(s string) SplitPoint
AtFirst creates a new split point that splits at the first occurrence of a string. This split point is exclusive; the string itself will not be obfuscated.
func AtLast ¶
func AtLast(s string) SplitPoint
AtLast creates a new split point that splits at the last occurrence of a string. This split point is exclusive; the string itself will not be obfuscated.
func AtNth ¶
func AtNth(s string, occurrence int) SplitPoint
AtNth creates a new split point that splits at a specific occurrence of a string. This split point is exclusive; the string itself will not be obfuscated.
AtNth panics if the given zero-based occurrence is negative.
func NewSplitPoint ¶
func NewSplitPoint(splitStart func(s string) int, splitLength int) SplitPoint
NewSplitPoint creates a new split point.
splitStart is a function that takes a string and returns the 0-based index where to split, or a negative value if obfuscation should not be split. This could for example be caused by a string to split on not being found.
splitLength is the length of the split point. If not 0, the substring with this length starting at the calculated split start will not be obfuscated. This function panics if splitLength is negative.
func (SplitPoint) SplitTo ¶
func (sp SplitPoint) SplitTo(beforeSplitPoint, afterSplitPoint Obfuscator) Obfuscator
SplitTo creates an obfuscator that splits obfuscation at this split point. The part of the string before the split point will be obfuscated by the first obfuscator, the part after the split point by the second.