utils

package
v1.127.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 21 Imported by: 9

README

utils

utils.json

GetInt64(kv utils.JSON, k string) (v int64, err error)
GetTime(kv utils.JSON, k string) (v time.Time, err error)

Documentation

Index

Constants

View Source
const MaskRedactedMarker = "***REDACTED***"

MaskRedactedMarker is what a log shows in place of a secret: a credential field or header, a token, a password, a PIN, an OTP or an image.

Variables

View Source
var (
	// RuleMaskFull hides the whole value: "********".
	RuleMaskFull = MaskRule{}
	// RuleMask2by2 keeps the first 2 and the last 2 of the whole value: "Pegawai Swasta" →
	// "Pe***ta"; a value of 7 or fewer is its first then "***" (MaskFrontBack).
	RuleMask2by2 = MaskRule{Front: 2, Back: 2}
	// RuleMaskNumber2by4 is an account number: "1234567890" → "12***7890".
	RuleMaskNumber2by4 = MaskRule{Kind: MaskKindNumber, Front: 2, Back: 4}
	// RuleMaskNumber5by4 is a national ID or family card number: "3201012345671234" →
	// "32010***1234".
	RuleMaskNumber5by4 = MaskRule{Kind: MaskKindNumber, Front: 5, Back: 4}
	// RuleMaskNumber4by3 is a phone number: "081234567789" → "0812***789".
	RuleMaskNumber4by3 = MaskRule{Kind: MaskKindNumber, Front: 4, Back: 3}
	// RuleMaskNumber6by4 is a card number, the PCI DSS display limit: "4111111111111234" →
	// "411111***1234".
	RuleMaskNumber6by4 = MaskRule{Kind: MaskKindNumber, Front: 6, Back: 4}
	// RuleMaskEmail2by2 is an e-mail address (MaskEmail2by2).
	RuleMaskEmail2by2 = MaskRule{Kind: MaskKindEmail}
	// RuleMaskInitials is a name (MaskInitials).
	RuleMaskInitials = MaskRule{Kind: MaskKindInitials}
	// RuleMaskLocation2Decimals is a coordinate (MaskLocation2Decimals).
	RuleMaskLocation2Decimals = MaskRule{Kind: MaskKindLocation}
	// RuleMaskRedacted is a secret or an image: "***REDACTED***".
	RuleMaskRedacted = MaskRule{Kind: MaskKindRedacted}
)

Named rules for SetMaskRules. RuleMask<front>by<back> keeps that many characters at each end; the number rules drop spaces and dashes first. MaskStrict collapses every rule to RuleMaskFull.

View Source
var MaskEmailKnownEndings = []string{

	".co.id", ".ac.id", ".go.id", ".or.id", ".web.id", ".sch.id", ".net.id", ".my.id",
	".mil.id", ".biz.id", ".ponpes.id", ".desa.id", ".id",

	".com", ".net", ".org", ".edu", ".gov", ".mil", ".int", ".info", ".biz",
	".io", ".co", ".me", ".app", ".dev",

	".co.uk", ".ac.uk", ".org.uk", ".uk",
	".com.sg", ".edu.sg", ".sg",
	".com.my", ".edu.my", ".my",
	".com.au", ".edu.au", ".au",
	".co.jp", ".ac.jp", ".jp",
}

MaskEmailKnownEndings is the list of public domain endings MaskEmail2by2 keeps as written. Everything before the ending is masked, so a sub-domain such as an employer's name in "mail.example.co.id" is not shown. The longest ending a domain matches wins, so ".co.id" is taken over ".id". Each entry starts with a dot. A host may append to it at init.

View Source
var OverrideShowPasswordOnLog = false

OverrideShowPasswordOnLog disables sensitive field masking in logs when true.

Functions

func AnyToBytes

func AnyToBytes(data interface{}) ([]byte, error)

AnyToBytes converts a value of a supported type to a byte slice.

func ArrayToJSON

func ArrayToJSON[T any](arr []T) (string, error)

ArrayToJSON converts a slice of any type to a JSON string.

func AskForConfirmation

func AskForConfirmation(key1 string, key2 string) (err error)

AskForConfirmation prompts the user for two confirmation keys and returns an error if they don't match the provided keys.

func BytesToInt64

func BytesToInt64(b []byte) int64

BytesToInt64 converts a byte slice to an int64.

func ConvertFloat32FromKV

func ConvertFloat32FromKV(kv map[string]any, key string) (r float32, err error)

ConvertFloat32FromKV retrieves a value from map and converts it to float32

func ConvertFloat64FromKV

func ConvertFloat64FromKV(kv map[string]any, key string) (r float64, err error)

ConvertFloat64FromKV retrieves a value from map and converts it to float64

func ConvertInt64FromKV

func ConvertInt64FromKV(kv map[string]any, key string) (r int64, err error)

func ConvertIntFromKV

func ConvertIntFromKV(kv map[string]any, key string) (r int, err error)

ConvertIntFromKV retrieves a value from map and converts it to int

func ConvertToArrayOfMapStringAnyFromAny

func ConvertToArrayOfMapStringAnyFromAny(v any) ([]map[string]any, error)

ConvertToArrayOfMapStringAnyFromAny converts a value of any type to []map[string]any. Handles both []map[string]any (direct) and []interface{} (from JSON unmarshal) cases.

func ConvertToBoolFromKV

func ConvertToBoolFromKV(kv map[string]any, key string) (bool, error)

ConvertToBoolFromKV gets value from map by key and converts it to bool

func ConvertToFloat

func ConvertToFloat(v any) (float64, bool)

func ConvertToFloat32

func ConvertToFloat32(value interface{}) (float32, error)

ConvertToFloat32 converts various types to float32

func ConvertToFloat32FromKV

func ConvertToFloat32FromKV(kv map[string]any, key string) (float32, error)

ConvertToFloat32FromKV gets value from map by key and converts it to float32

func ConvertToFloat64

func ConvertToFloat64(value interface{}) (float64, error)

ConvertToFloat64 converts various types to float64

func ConvertToFloat64FromKV

func ConvertToFloat64FromKV(kv map[string]any, key string) (float64, error)

ConvertToFloat64FromKV gets value from map by key and converts it to float64

func ConvertToInt

func ConvertToInt(value interface{}) (int, error)

ConvertToInt converts various types to int

func ConvertToInt64

func ConvertToInt64(value interface{}) (int64, error)

ConvertToInt64 converts various types to int64

func ConvertToInt64FromKV

func ConvertToInt64FromKV(kv map[string]any, key string) (int64, error)

ConvertToInt64FromKV gets value from map by key and converts it to int64

func ConvertToIntFromKV

func ConvertToIntFromKV(kv map[string]any, key string) (int, error)

ConvertToIntFromKV gets value from map by key and converts it to int

func ConvertToInterfaceArrayInterfaceFromAny

func ConvertToInterfaceArrayInterfaceFromAny(v any) (r any, err error)

ConvertToInterfaceArrayInterfaceFromAny converts a value of any type to a slice of interfaces.

func ConvertToInterfaceBoolFromAny

func ConvertToInterfaceBoolFromAny(v any) (r any, err error)

ConvertToInterfaceBoolFromAny converts a value of any type to a boolean interface.

func ConvertToInterfaceFloat64FromAny

func ConvertToInterfaceFloat64FromAny(v any) (r any, err error)

ConvertToInterfaceFloat64FromAny converts a value of any type to a float64 interface.

func ConvertToInterfaceInt64FromAny

func ConvertToInterfaceInt64FromAny(v any) (r any, err error)

ConvertToInterfaceInt64FromAny converts a value of any type to an int64 interface.

func ConvertToInterfaceIntFromAny

func ConvertToInterfaceIntFromAny(v any) (r any, err error)

ConvertToInterfaceIntFromAny converts a value of any type to an integer interface.

func ConvertToInterfaceStringFromAny

func ConvertToInterfaceStringFromAny(v any) (r any, err error)

ConvertToInterfaceStringFromAny converts a value of any type to a string interface.

func ConvertToMapStringInterfaceFromAny

func ConvertToMapStringInterfaceFromAny(v any) (r any, err error)

ConvertToMapStringInterfaceFromAny converts a value of any type to a map[string]any interface.

func ConvertToStringFromKV

func ConvertToStringFromKV(kv map[string]any, key string) (string, error)

ConvertToStringFromKV gets value from map by key and converts it to string

func Diff

func Diff[T comparable](first []T, second []T) (included, missing []T)

Diff returns the intersection and difference between two arrays Returns:

  • included: values from first that exist in second
  • missing: values from first that do NOT exist in second

func DiffJsonFieldValues

func DiffJsonFieldValues[T comparable](valuesToCheck []T, jsonData []map[string]any, fieldName string) (included, missing []T)

DiffJsonFieldValues checks values existence between valuesToCheck and jsonData[fieldName] Returns:

  • included: values from valuesToCheck that exist in jsonData[fieldName]
  • missing: values from valuesToCheck that do NOT exist in jsonData[fieldName]

func ExtractMapValue

func ExtractMapValue[T any](m *map[string]any, key string) (exists bool, value T, err error)

ExtractMapValue retrieves a value from a map and deletes the key.

func FindCommonValues

func FindCommonValues[K comparable, V comparable](arrays1, arrays2 []map[K]any, key K) []V

K is the type for the map key (usually string) V is the type for the values we're comparing (must be comparable) FindCommonValues finds common values in a specific key between two slices of maps.

func FindCommonValuesInMapString

func FindCommonValuesInMapString[V comparable](arrays1, arrays2 []map[string]any, key string) []V

FindCommonValuesInMapString is a specialization of FindCommonValues for maps with string keys.

func Float64sToJSON

func Float64sToJSON(arr []float64) string

Float64sToJSON converts a slice of float64s to a JSON string.

func FloatFitsInt64 added in v1.126.5

func FloatFitsInt64(f float64) bool

FloatFitsInt64 reports whether f is a whole number an int64 can hold. The bounds are checked on the float before any conversion: Go leaves an out-of-range float-to-int conversion undefined, so the converted value cannot be inspected afterwards.

func GetAllActualBindingAddress

func GetAllActualBindingAddress(configuredBindingAddress string) []string

GetAllActualBindingAddress returns a list of actual binding addresses based on a configured address. If the configured IP is not found on the machine, it returns all available IPs with the configured port.

func GetAllMachineIP4s

func GetAllMachineIP4s() []string

GetAllMachineIP4s retrieves all non-loopback IPv4 addresses of the machine.

func GetArrayFromV

func GetArrayFromV(v any) (r []any, err error)

GetArrayFromV converts an `any` type to a slice of `any`.

Accepts a string as well as []byte, for the same reason as GetJSONFromV.

func GetBoolFromKV

func GetBoolFromKV(kv map[string]any, key string) (r bool, err error)

GetBoolFromKV retrieves a bool value for the given key.

func GetBuildTime

func GetBuildTime() string

GetBuildTime retrieves the VCS build time from the build info.

func GetBytesFromKV

func GetBytesFromKV(kv map[string]any, key string) (r []byte, err error)

GetBytesFromKV retrieves a []byte (byte slice) value for the given key.

func GetFloat64FromKV

func GetFloat64FromKV(kv map[string]any, key string) (r float64, err error)

GetFloat64FromKV retrieves a float64 value for the given key.

func GetInt64FromKV

func GetInt64FromKV(kv map[string]any, key string) (r int64, err error)

GetInt64FromKV retrieves an int64 value for the given key from a map[string]any. It relies on the generic GetVFromKV function for key lookup and type assertion.

func GetIntFromKV

func GetIntFromKV(kv map[string]any, key string) (r int, err error)

GetIntFromKV retrieves an int value (default 32 or 64 bit) for the given key.

func GetKVFromKV

func GetKVFromKV(kv map[string]any, key string) (r map[string]any, err error)

GetKVFromKV retrieves a nested map[string]any (KV) from a map by key. This is an alias to GetJSONFromKV since JSON is defined as map[string]any.

func GetMapStringStringFromKV

func GetMapStringStringFromKV(kv map[string]any, key string) (r map[string]string, err error)

GetMapStringStringFromKV retrieves a map[string]string value for the given key.

func GetMapStringStringFromV

func GetMapStringStringFromV(v any) (r map[string]string, err error)

GetMapStringStringFromV converts any value to map[string]string. Handles: direct map[string]string, []byte or string (JSON), map[string]any (converts values to string).

func GetMapValue

func GetMapValue[T any](m map[string]any, key string) (exist bool, value T, err error)

GetMapValue safely retrieves and type-asserts a value from a map[string]any. Returns: - exists: True if the key exists in the map - value: The typed value if key exists and type assertion succeeds, nil otherwise - err: Error if type assertion fails for existing key

func GetMapValueFromJSONs

func GetMapValueFromJSONs[T any](a []map[string]any, key string) (values []T, error error)

GetMapValueFromJSONs extracts values for a given key from a slice of maps.

func GetStringFromKV

func GetStringFromKV(kv map[string]any, key string) (r string, err error)

GetStringFromKV retrieves a string value for the given key from a map[string]any. It relies on the generic GetVFromKV function to perform the key lookup and type assertion.

func GetStringFromMapStringString

func GetStringFromMapStringString(kv map[string]string, key string) (r string, err error)

func GetStringFromMapStringStringDefault

func GetStringFromMapStringStringDefault(kv map[string]string, key string, defaultValue string) (r string)

func GetTimeFromKV

func GetTimeFromKV(kv map[string]any, key string) (r time.Time, err error)

GetTimeFromKV retrieves a time.Time value for the given key.

func GetUint64FromKV

func GetUint64FromKV(kv map[string]any, key string) (r uint64, err error)

GetUint64FromKV retrieves a uint64 value for the given key.

func GetVFromKV

func GetVFromKV[T any](kv map[string]any, key string) (r T, err error)

GetVFromKV retrieves a value of a specific generic type T from a map[string]any. Error messages are structured for client-side parsing and localization.

func GetValueFromNestedMap

func GetValueFromNestedMap(data map[string]interface{}, key string) (interface{}, error)

GetValueFromNestedMap retrieves a value from a nested map using a dot-separated key. It traverses the map based on the keys provided in the dot-separated string. For example, given a map `{"a": {"b": 1}}` and a key `"a.b"`, it will return `1`. If any key in the path does not exist, it returns an error.

func IfFloatIsInt

func IfFloatIsInt(f float64) bool

IfFloatIsInt reports whether f is a finite whole number: no fractional part in either sign, and neither NaN nor an infinity. It says nothing about whether the value fits an int64; FloatFitsInt64 adds that.

The earlier test, f - float64(int64(f)) > 0, saw only a positive remainder, so a negative fraction such as -5.5 passed and was truncated downstream.

func Int64SliceToStrings

func Int64SliceToStrings(nums []int64) []string

Int64SliceToStrings converts a slice of int64 to a slice of strings.

func Int64ToBytes

func Int64ToBytes(v int64) []byte

Int64ToBytes encodes v as an 8-byte big-endian slice.

func Int64ToString

func Int64ToString(i int64) string

Int64ToString converts an int64 to a string.

func Int64sIsContain

func Int64sIsContain(arr []int64, v int64) bool

Int64sIsContain checks if a slice of int64s contains a specific value.

func Int64sToJSON

func Int64sToJSON(arr []int64) string

Int64sToJSON converts a slice of int64s to a JSON string.

func Int64sToStrings

func Int64sToStrings(arr []int64) []string

Int64sToStrings converts a slice of int64s to a slice of strings.

func IntsToJSON

func IntsToJSON(arr []int) string

IntsToJSON converts a slice of ints to a JSON string.

func IsCredentialHeader added in v1.126.3

func IsCredentialHeader(name string) bool

IsCredentialHeader reports whether a header carries a credential: it is in the built-in list, in the host's list, or its name holds one of the credential keywords.

func IsSensitiveField

func IsSensitiveField(fieldName string) bool

IsSensitiveField checks if a field name contains sensitive keywords. This is a unified function to detect sensitive data across the codebase.

func IsValuesMatch

func IsValuesMatch(a, b any) bool

func JSONToBytes

func JSONToBytes(v JSON) ([]byte, error)

JSONToBytes converts a JSON object to a byte slice.

func JSONToMapStringString

func JSONToMapStringString(kv JSON) (r map[string]string)

JSONToMapStringString converts a JSON object to a map[string]string.

func JSONToString

func JSONToString(v JSON) (string, error)

JSONToString converts a JSON object to a string.

func Mask2by2 added in v1.126.5

func Mask2by2(s string) string

Mask2by2 keeps the first 2 and the last 2 runes of the whole value.

func MaskDefaultDeny

func MaskDefaultDeny() bool

MaskDefaultDeny reports the current default, so a host can log which posture it started in.

func MaskEmail2by2 added in v1.126.5

func MaskEmail2by2(s string) string

MaskEmail2by2 masks an e-mail address. The local part keeps its first 2 and last 2 runes with a fixed "***" between, or only its first rune when it has 4 or fewer. The domain is split into a known public ending (MaskEmailKnownEndings, the longest match, kept as written) and the rest before it, sub-domains included, which keeps its first 2 runes then "***", or only its first rune when it has 2 or fewer. A domain with no known ending keeps its first 2 runes then "***". A value with no "@" is "***".

adinda@example.co.id      → ad***da@ex***.co.id
x@mail.example.co.id      → x***@ma***.co.id
adinda@example.xyz        → ad***da@ex***

func MaskFrontBack added in v1.126.5

func MaskFrontBack(s string, front, back int) string

MaskFrontBack keeps the first front and the last back runes of s with a fixed "***" between them. A value of front+back+3 runes or fewer keeps only its first rune: "PNS-1" with 2 and 2 is "P***", "Pegawai" is "P***", "Pegawai S" is "Pe*** S". With front 0 a short value keeps nothing. An empty value is "***".

func MaskFull added in v1.126.5

func MaskFull(string) string

MaskFull hides the whole value: "********".

func MaskHeaderValue added in v1.126.3

func MaskHeaderValue(name, value string) string

MaskHeaderValue returns the value a dump may show for a header: "***REDACTED***" for a credential header, the value itself otherwise. The debug override (IsDebug with OverrideShowPasswordOnLog) shows every header raw, as it does for a body.

func MaskInitials added in v1.126.5

func MaskInitials(s string) string

MaskInitials is a name: the first rune of each word followed by a fixed "***", "Budi Santoso" → "B*** S***". The rest of the word, however long, is three asterisks, so the mask gives away neither the word nor its length. An empty or all-whitespace value is fully masked.

func MaskLocation2Decimals added in v1.126.5

func MaskLocation2Decimals(value any) any

MaskLocation2Decimals is a coordinate rounded to two decimals (about 1 km). A number stays a number (as a float64); a string is read as comma-separated coordinates and rounded term by term, keeping the string form. Anything that is not a coordinate is fully masked.

func MaskNumber added in v1.126.5

func MaskNumber(s string, front, back int) string

MaskNumber is MaskFrontBack on a number after its spaces and dashes are dropped, so the kept characters are digits and the grouping, which would show the length, is gone: "0812-3456-7789" with 4 and 3 is "0812***789".

func MaskNumber2by4 added in v1.126.5

func MaskNumber2by4(s string) string

MaskNumber2by4 is an account number: the first 2 and the last 4 digits.

func MaskNumber4by3 added in v1.126.5

func MaskNumber4by3(s string) string

MaskNumber4by3 is a phone number: the first 4 (the operator prefix) and the last 3.

func MaskNumber5by4 added in v1.126.5

func MaskNumber5by4(s string) string

MaskNumber5by4 is a national ID or family card number: the first 5 digits (the region of registration) and the last 4.

func MaskNumber6by4 added in v1.126.5

func MaskNumber6by4(s string) string

MaskNumber6by4 is a card number: the first 6 (the issuer) and the last 4.

func MaskRedacted added in v1.126.5

func MaskRedacted(string) string

MaskRedacted is the marker for a secret, a token or an image: "***REDACTED***".

func MaskSensitiveValue

func MaskSensitiveValue(fieldName string, value interface{}) interface{}

MaskSensitiveValue masks a value for logging.

  • Credential fields (IsSensitiveField): ALWAYS "***REDACTED***".
  • PII fields (SetMaskRules): masked by the rule's kind (partial Front+Back, e-mail, initials or location) unless SetMaskStrict → full.
  • Unmatched fields: logged as-is under default-ALLOW, masked under default-DENY unless SetLogAllowedFields names them (SetMaskDefaultDeny).
  • Requires BOTH dxlib.IsDebug AND OverrideShowPasswordOnLog to show raw values (prevents accidental exposure from a single env flip).

func MustConvertToInterfaceStringFromAny

func MustConvertToInterfaceStringFromAny(v any) (r any)

MustConvertToInterfaceStringFromAny converts a value of any type to a string interface, panicking on error.

func NowAsString

func NowAsString() string

NowAsString returns the current UTC time as a string in RFC3339 format.

func RemoveDuplicates

func RemoveDuplicates[T comparable](slice []T) []T

RemoveDuplicates removes duplicate values from a slice of any comparable type

func SetCredentialHeaders added in v1.126.3

func SetCredentialHeaders(names []string)

SetCredentialHeaders replaces the host's own list of credential headers (called once at init). Names are matched case-insensitively. The built-in list and the credential keywords stay in force; this only adds.

func SetLogAllowedFields

func SetLogAllowedFields(names []string)

SetLogAllowedFields replaces the set of field names that stay readable under default-deny. Matched EXACTLY (lowercased), deliberately unlike maskRules: a deny rule should be fuzzy so it catches `foto_ktp` from `ktp`, but an ALLOW rule must not, or a substring like `id` would silently un-mask `national_id`.

func SetMaskDefaultDeny

func SetMaskDefaultDeny(b bool)

SetMaskDefaultDeny chooses what happens to a field the rules do not mention: false = log it (default, current behaviour), true = mask it unless SetLogAllowedFields names it. Called once by the host at init from its own configuration; see maskDefaultDeny for why this is a switch.

func SetMaskRules

func SetMaskRules(rules map[string]MaskRule)

SetMaskRules replaces the PII field→rule map (called once by the host at init).

func SetMaskStrict

func SetMaskStrict(b bool)

SetMaskStrict forces full masking of PII regardless of per-field rules (prod hardening).

func SetValueInNestedMap

func SetValueInNestedMap(data map[string]interface{}, key string, value interface{})

SetValueInNestedMap sets a value in a nested map using a dot-separated key. It creates nested maps if they don't exist.

func ShouldStrictJSONToMapStringString

func ShouldStrictJSONToMapStringString(kv JSON) (r map[string]string, err error)

ShouldStrictJSONToMapStringString strictly converts a JSON object to a map[string]string, returning an error if any value is not a string.

func StringIsInSlice

func StringIsInSlice(str string, list []string) bool

StringIsInSlice checks if a string exists in a slice of strings.

func StringsHasCommonItem

func StringsHasCommonItem(arr1, arr2 []string) bool

StringsHasCommonItem checks if two string slices have any common items.

func StringsIsContain

func StringsIsContain(arr []string, v string) bool

StringsIsContain checks if a slice of strings contains a specific value.

func StringsToJSON

func StringsToJSON(arr []string) string

StringsToJSON converts a slice of strings to a JSON string.

func TCPAddressCanConnect

func TCPAddressCanConnect(address string) bool

TCPAddressCanConnect checks if a TCP connection can be established to a given address.

func TCPIPPortCanConnect

func TCPIPPortCanConnect(ip string, port string) bool

TCPIPPortCanConnect checks if a TCP connection can be established to a given IP and port.

func TimeSubToString

func TimeSubToString(t1 any, t2 any) (r string)

TimeSubToString returns the string representation of the duration between two time.Time objects.

func TsIsContain

func TsIsContain[T comparable](arr []T, v T) bool

TsIsContain checks if a slice of a comparable type contains a specific value.

func TsIsInSlice

func TsIsInSlice[T comparable](v []T, aSlice []T) bool

TsIsInSlice checks if all elements of a slice exist in another slice.

func TypeAsString

func TypeAsString(v any) string

TypeAsString returns the type of a variable as a string.

func Uint32ToBytes

func Uint32ToBytes(v uint32) []byte

Uint32ToBytes encodes v as a 4-byte big-endian slice.

func WriteHeadersForLog added in v1.126.3

func WriteHeadersForLog(h http.Header, exclude map[string]bool) string

WriteHeadersForLog renders h as "Name: value" lines, one per value, in name order, with every credential value masked and any line break inside a value turned into a space so a value cannot forge a line of its own. Names in exclude are left out (a dump writes Host and the framing headers itself). It returns the text, ready to append to a dump.

Types

type JSON

type JSON = map[string]any

JSON is a type alias for map[string]any, representing a JSON object.

func GetJSONFromKV

func GetJSONFromKV(kv map[string]any, key string) (r JSON, err error)

GetJSONFromKV retrieves a JSON object from a map by key.

func GetJSONFromV

func GetJSONFromV(v any) (r JSON, err error)

GetJSONFromV converts an `any` type to a JSON object.

A JSON/CLOB column does not arrive as []byte on every engine: Oracle hands a CLOB back as a Go string, and row-type normalisation turns a []byte text column into a string before this runs. Accept both encodings of the same document rather than rejecting one of them as "not JSON".

func MapStringStringToJSON

func MapStringStringToJSON(kv map[string]string) (r JSON)

MapStringStringToJSON converts a map[string]string to a JSON object.

func MaskForLog added in v1.126.2

func MaskForLog(data JSON) JSON

MaskForLog returns a deep copy of data with every value masked the way MaskSensitiveValue masks it, at every depth: nested objects, arrays, and objects inside arrays all get the same treatment, so a body that puts everything under one "params" key is masked the same as a flat one. The credential keywords, the host's PII rules (SetMaskRules, SetMaskStrict) and the default-deny switch (SetMaskDefaultDeny, SetLogAllowedFields) all apply, each to the leaf field's own name.

A key that matches a credential keyword masks whatever it holds, object or array, whole as "***REDACTED***". A key that matches a PII rule and holds an object masks it whole as "********": there is no front or back of a nested object to reveal, and fail-closed is the safer reading. The one exception is MaskKindLocation, which walks the object and rounds its numeric leaves. An array's elements take the array's key, so {"phone_numbers": ["0812...", "0813..."]} masks each number under the rule that matches phone_numbers, and an object inside that array is masked whole.

Use it for anything written to a log; it never changes the data it is given.

func MaskSensitiveDataInJSON

func MaskSensitiveDataInJSON(data JSON) JSON

MaskSensitiveDataInJSON recursively masks sensitive fields in a JSON structure. Returns a deep copy with sensitive values replaced by "***REDACTED***".

Only the built-in credential keywords (IsSensitiveField) apply here, at any depth of nested objects; arrays are copied as they are. It serves configuration.FilterSensitiveData, whose readers expect the host's PII rules and default-deny posture NOT to apply (a DENY host's config dump would otherwise go blank). A log dump of request or response data goes through MaskForLog instead.

func StringToJSON

func StringToJSON(s string) (JSON, error)

StringToJSON converts a JSON string to a JSON object.

type MaskKind added in v1.126.2

type MaskKind int

MaskKind selects how a PII field is masked. The zero value, MaskKindPartial, is the original Front+Back rule, so a host's existing rules keep their meaning.

const (
	// MaskKindPartial keeps Front leading and Back trailing characters of the whole value with a
	// fixed "***" between them (MaskFrontBack). {Front: 0, Back: 0} (or MaskStrict) is the full
	// "********".
	MaskKindPartial MaskKind = iota
	// MaskKindEmail is MaskEmail2by2: "adinda@mail.example.co.id" becomes "ad***da@ma***.co.id".
	MaskKindEmail
	// MaskKindInitials keeps the first letter of each word and masks the rest of it with three
	// asterisks, three whatever the word's length: "Budi Santoso" becomes "B*** S***". Words
	// are split on any run of whitespace, so surrounding and doubled spaces vanish; a
	// one-letter word is "A***" too, so a word's length cannot be read off the mask; a
	// hyphenated name is one word. An empty value is fully masked.
	MaskKindInitials
	// MaskKindLocation rounds a coordinate to two decimals (about 1 km), so an area is logged,
	// not a doorstep. It applies to a number, a "lat,lng" string, and, unlike the other kinds,
	// to the numeric leaves of an object or array under the key, so {"location": {"lat": ..,
	// "lng": ..}} is walked rather than masked whole. A value that is not a coordinate is fully
	// masked.
	MaskKindLocation
	// MaskKindNumber is MaskKindPartial for a number written with spaces or dashes: they are
	// dropped first, so only digits are kept at the ends (MaskNumber).
	MaskKindNumber
	// MaskKindRedacted replaces the value with "***REDACTED***", the marker for a secret.
	MaskKindRedacted
)

type MaskRule

type MaskRule struct {
	Front, Back int
	Kind        MaskKind
}

MaskRule says how a PII field is masked for a log. Front and Back belong to MaskKindPartial and MaskKindNumber; the other kinds ignore them. Build it with keyed fields, or take a named rule (RuleMask2by2, RuleMaskEmail2by2 and the others in mask_rules.go).

Directories

Path Synopsis
crypto
aes
x25519
Package x25519 performs X25519 key agreement (RFC 7748) on raw 32-byte keys, as peers exchange them on the wire.
Package x25519 performs X25519 key agreement (RFC 7748) on raw 32-byte keys, as peers exchange them on the wire.
tls
Package tls builds the crypto/tls configuration for the API server and for the process's outbound HTTP and WebSocket clients from a configuration block, so that every hop that terminates or originates TLS in a dxlib process reads its trust the same way and from the same code.
Package tls builds the crypto/tls configuration for the API server and for the process's outbound HTTP and WebSocket clients from a configuration block, so that every hop that terminates or originates TLS in a dxlib process reads its trust the same way and from the same code.
tlstest
Package tlstest builds a throwaway PKI in memory for tests: a root, an intermediate, and leaves with whatever SANs, validity windows and key sizes a test needs, written to PEM files on demand.
Package tlstest builds a throwaway PKI in memory for tests: a root, an intermediate, and leaves with whatever SANs, validity windows and key sizes a test needs, written to PEM files on demand.
Package xlsx writes a one-sheet .xlsx workbook of text cells, streaming the rows into an io.Writer.
Package xlsx writes a one-sheet .xlsx workbook of text cells, streaming the rows into an io.Writer.

Jump to

Keyboard shortcuts

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