Files
tasks/common/masker/masker.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

84 lines
2.1 KiB
Go

package masker
import (
"reflect"
"gopkg.in/yaml.v3"
)
const passwordMask = "******"
var (
DefaultFieldNames = []string{"Password", "KeyData"}
DefaultYAMLFieldNames = []string{"password", "keyData"}
)
// MaskYaml replace password values with mask and returns copy of the string.
// Does recursive replacement for entire yamlStr.
func MaskYaml(yamlStr string, fieldNamesToMask []string) (string, error) {
fns := make(map[string]struct{}, len(fieldNamesToMask))
for _, fieldName := range fieldNamesToMask {
fns[fieldName] = struct{}{}
}
var parsedYaml map[string]interface{}
err := yaml.Unmarshal([]byte(yamlStr), &parsedYaml)
if err != nil {
return yamlStr, err
}
maskMap(parsedYaml, fns)
strBytes, err := yaml.Marshal(parsedYaml)
if err != nil {
return yamlStr, err
}
return string(strBytes), nil
}
// MaskStruct replace password values with mask and returns copy of the strct.
// Original strct value is not modified. Doesn't go recursively through strct properties.
func MaskStruct(strct interface{}, fieldNamesToMask []string) interface{} {
strctV := reflect.ValueOf(strct)
if strct == nil || (strctV.Kind() == reflect.Ptr && strctV.IsNil()) {
return strct
}
for t := reflect.TypeOf(strct); t.Kind() == reflect.Ptr; t = t.Elem() {
strctV = strctV.Elem()
}
// strctV is not a pointer now. Create a copy using assignment.
strctCopy := strctV.Interface()
strctCopyPV := pointerTo(strctCopy)
strctCopyV := strctCopyPV.Elem()
for _, passwordFieldName := range fieldNamesToMask {
passwordF := strctCopyV.FieldByName(passwordFieldName)
if passwordF.CanSet() && passwordF.Kind() == reflect.String {
passwordF.SetString(passwordMask)
}
}
return strctCopyPV.Interface()
}
func pointerTo(val interface{}) reflect.Value {
valPtr := reflect.New(reflect.TypeOf(val))
valPtr.Elem().Set(reflect.ValueOf(val))
return valPtr
}
func maskMap(m map[string]interface{}, fns map[string]struct{}) {
for key, value := range m {
if _, ok := fns[key]; ok {
m[key] = passwordMask
}
if valueMap, ok := value.(map[string]interface{}); ok {
maskMap(valueMap, fns)
}
}
}