jsonmask

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: MIT Imports: 9 Imported by: 1

README

go-json-mask

go-json-mask is a simple, customizable Go library for masking JSON sensitive information.

Installation

go get github.com/bolom009/go-json-mask

Json value types

go-json-mask support masking for all JSON value types, also users could create their own necessary masking functions.

type masks description
string hash, filled hash - masks the string with sha1
filled - masks the string with the same number of masking characters or by passed length
int random int masks the integer value by default range (1000) or by passed
float random float masks the float value by default range (1000.3) or by passed, consists from two parts XXX.XXX
array all types support (string, int, float, object, array)
boolean - ignored
null - ignored

How to use

package main

import (
	"fmt"
	"log"

	"github.com/bolom009/go-json-mask"
)

func main() {
	mask := jsonmask.NewJSONMask("key1", "/metadata/labels/key2", "/metadata/labels/key3[1]")
	mask.RegisterMaskStringFunc(jsonmask.MaskHashString())

	v := `{
      "name": "HelloWorld",
      "age": 999,
      "metadata": {
        "labels": {
          "key1": "value1",
          "key2": "value2",
          "key3": ["one", "two"]
        },
        "annotations": {
          "key1": "value1"
        }
      }
    }`

	res, err := mask.Mask(v)
	if err != nil {
		log.Fatal(err)
	}
	
	fmt.Println(res)
}

Output:

{"name":"HelloWorld","age":999,"metadata":{"labels":{"key1":"8107759ababcbfa34bcb02bc4309caf6354982ab","key2":"43f7aa390f1a0265fc2de7010133951c0718a67e", "key3":["one", "ad782ecdac770fc6eb9a62e44f90873fb97fb26b"]},"annotations":{"key1":"8107759ababcbfa34bcb02bc4309caf6354982ab"}}}

Benchmarks

BenchmarkNewJSONMaskHashString-16    343420	      3341 ns/op	    1929 B/op	      47 allocs/op
BenchmarkNewJSONMaskFilledString-16  335380	      3125 ns/op	    1771 B/op	      46 allocs/op
BenchmarkNewJSONMaskInt-16    	     355957	      3167 ns/op	    1717 B/op	      44 allocs/op
BenchmarkNewJSONMaskFloat64-16       353574	      3215 ns/op	    1785 B/op	      46 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JsonMask

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

JsonMask is a struct that defines the masking process

func NewJSONMask

func NewJSONMask(fields ...string) *JsonMask

NewJSONMask initializes a JsonMask Mask fields: 1. Global (a,b,c) - will mask all encountered json fields (nested fields will be masked entirely) 2. XPath (/a/b/c) - will mask only specified json fields by xpath

func (*JsonMask) Mask

func (j *JsonMask) Mask(value string) (string, error)

Mask method for masking JSON fields globally or by xpath

func (*JsonMask) RegisterMaskFloat64Func

func (j *JsonMask) RegisterMaskFloat64Func(fn MaskFloat64Func)

RegisterMaskFloat64Func method for adding MaskFloat64Func to JsonMask

func (*JsonMask) RegisterMaskIntFunc

func (j *JsonMask) RegisterMaskIntFunc(fn MaskIntFunc)

RegisterMaskIntFunc method for adding MaskIntFunc to JsonMask

func (*JsonMask) RegisterMaskStringFunc

func (j *JsonMask) RegisterMaskStringFunc(fn MaskStringFunc)

RegisterMaskStringFunc method for adding MaskStringFunc to JsonMask

type MaskFloat64Func

type MaskFloat64Func func(path string, value float64) (float64, error)

list of func type that must be satisfied to add a custom mask

func MaskRandomFloat64

func MaskRandomFloat64(arg ...string) MaskFloat64Func

MaskRandomFloat64 converts a float64 to a random number in range (default 1000.3) if you pass "1000.3" to arg, it sets a random number in the range of 0.000 to 999.999

type MaskIntFunc

type MaskIntFunc func(path string, value int) (int, error)

list of func type that must be satisfied to add a custom mask

func MaskRandomInt

func MaskRandomInt(arg ...int) MaskIntFunc

MaskRandomInt masks converts an integer (int) into a random number in range (default 1000)

type MaskStringFunc

type MaskStringFunc func(path, value string) (string, error)

list of func type that must be satisfied to add a custom mask

func MaskFilledString

func MaskFilledString(maskChar string, length ...int) MaskStringFunc

MaskFilledString masks the string length of the value with the same length or by passed length

func MaskHashString

func MaskHashString() MaskStringFunc

MaskHashString masks and hashes (sha1) a string

type SettableType added in v1.0.1

type SettableType interface {
	int | string
}

Jump to

Keyboard shortcuts

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