scrub

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2023 License: MIT Imports: 5 Imported by: 0

README

Tag Go Report Card GoDoc

go-scrub

A scrubbing utility in Golang to mask sensitive fields from a struct prior to logging.

Since the scrubbing utility function needs to work on any Golang struct where its fields and members are known only at runtime, we can leverage "reflect", a powerful package from the Golang standard library, to scrub sensitive fields at any level of a deeply nested structure recursively.

Installation

go get github.com/angel-one/go-scrub@latest

Usage

import "github.com/angel-one/go-scrub"

// Have a struct with some sensitive fields.
type testScrub struct {
    Username string
    Password string
    Codes    []string
}

// Create a struct with some sensitive data.
T := &testScrub{
    Username: "administrator",
    Password: "my_secret_passphrase",
    Codes:    []string{"pass1", "pass2", "pass3"},
}

// Create empty instance of testScrub
emptyT := &testScrub{}

// Create a set of field names to scrub (default is 'password').
fieldsToScrub := map[string]FieldScrubOptioner{
    "password":  FieldScrubOpts{
		MaskingSymbol: "*",
	},
    "codes":      FieldScrubOpts{
		MaskingSymbol: "*",
		PartScrubConf: *NewPartScrubConf(true, 0, 10, 1, 0, 2),
	},
}

scrub.MaskLenVary = true

// Call the util API to get a JSON formatted string with scrubbed field values.
out := scrub.Scrub(emptyT, T, fieldsToScrub, scrub.JSONScrub)

// Log the scrubbed string without worrying about prying eyes!
log.Println(out)
// OUTPUT: {username:administrator Password:******************** Codes:[..... ..... .....]}
  • NOTE: Please reffer to scrub_test.go for all supported scenarios

Contributing

Contributions are most welcome! Please create a new issue and link your PR to it.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFieldScrubOpts = FieldScrubOpts{
	MaskingSymbol: defaultMaskSymbol,
}
View Source
var (

	// MaskLenVary specifies mask length equals DefaultMaskLen or mask length equals to value length
	MaskLenVary bool = false
)

Functions

func Scrub

func Scrub(cloning interface{}, target interface{}, fieldsToScrub map[string]FieldScrubOptioner, dataType DataType) string

Scrub scrubs all the specified string fields in the 'target' struct at any level recursively and returns a DataType formatted string of the scrubbed struct.

A pointer to a new empty instance of the 'target' struct is needed to act as a 'cloning' of the 'target' to avoid race conditions

Types

type DataType

type DataType string

DataType specifies supported formats

const (
	// XMLScrub - support of xml format
	XMLScrub DataType = "xml"
	// JSONScrub - support of json format
	JSONScrub DataType = "json"
)

type FieldScrubOptioner

type FieldScrubOptioner interface {
	GetMaskingSymbol() string
	PartMaskEnabled() bool
	PartMaskMinFldLen() int
	PartMaskMaxFldLen() int
	PartMaskVisibleFrontLen() int
	PartMaskVisibleBackOnlyIfFldLenGreaterThan() int
	PartMaskVisibleBackLen() int
}

FieldScrubOptioner provides an interface for custom masking field options

type FieldScrubOpts

type FieldScrubOpts struct {
	PartScrubConf
	MaskingSymbol string
}

func (FieldScrubOpts) GetMaskingSymbol

func (f FieldScrubOpts) GetMaskingSymbol() string

func (FieldScrubOpts) PartMaskEnabled

func (f FieldScrubOpts) PartMaskEnabled() bool

func (FieldScrubOpts) PartMaskMaxFldLen

func (f FieldScrubOpts) PartMaskMaxFldLen() int

func (FieldScrubOpts) PartMaskMinFldLen

func (f FieldScrubOpts) PartMaskMinFldLen() int

func (FieldScrubOpts) PartMaskVisibleBackLen

func (f FieldScrubOpts) PartMaskVisibleBackLen() int

func (FieldScrubOpts) PartMaskVisibleBackOnlyIfFldLenGreaterThan

func (f FieldScrubOpts) PartMaskVisibleBackOnlyIfFldLenGreaterThan() int

func (FieldScrubOpts) PartMaskVisibleFrontLen

func (f FieldScrubOpts) PartMaskVisibleFrontLen() int

type PartScrubConf

type PartScrubConf struct {
	PartMaskEnabled                    bool
	PartMaskMinFldLen                  int
	PartMaskMaxFldLen                  int
	VisibleFrontLen                    int
	VisibleBackOnlyIfFldLenGreaterThan int
	VisibleBackLen                     int
}

PartScrubConf provides options for partitial field masking

func NewPartScrubConf

func NewPartScrubConf(
	partMaskEnabled bool,
	partMaskMinFldLen int,
	partMaskMaxFldLen int,
	visibleFrontLen int,
	visibleBackOnlyIfFldLenGreaterThan int,
	visibleBackLen int,
) *PartScrubConf

NewPartScrubConf is PartScrubConf constructor

Jump to

Keyboard shortcuts

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