2022-01-24 10:08:05 +00:00
|
|
|
package redactor
|
2017-10-02 08:32:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/mitchellh/copystructure"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
2024-01-11 16:06:06 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/types"
|
2019-08-03 01:58:23 +00:00
|
|
|
"mvdan.cc/xurls/v2"
|
2017-10-02 08:32:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-01-24 10:08:05 +00:00
|
|
|
maskShort = "xxxx"
|
|
|
|
maskLarge = maskShort + maskShort + maskShort + maskShort + maskShort + maskShort + maskShort + maskShort
|
|
|
|
tagLoggable = "loggable"
|
|
|
|
tagExport = "export"
|
2017-10-02 08:32:02 +00:00
|
|
|
)
|
|
|
|
|
2022-01-24 10:08:05 +00:00
|
|
|
// Anonymize redacts the configuration fields that do not have an export=true struct tag.
|
|
|
|
// It returns the resulting marshaled configuration.
|
|
|
|
func Anonymize(baseConfig interface{}) (string, error) {
|
|
|
|
return anonymize(baseConfig, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func anonymize(baseConfig interface{}, indent bool) (string, error) {
|
|
|
|
conf, err := do(baseConfig, tagExport, true, indent)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return doOnJSON(conf), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveCredentials redacts the configuration fields that have a loggable=false struct tag.
|
|
|
|
// It returns the resulting marshaled configuration.
|
|
|
|
func RemoveCredentials(baseConfig interface{}) (string, error) {
|
|
|
|
return removeCredentials(baseConfig, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeCredentials(baseConfig interface{}, indent bool) (string, error) {
|
|
|
|
return do(baseConfig, tagLoggable, false, indent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// do marshals the given configuration, while redacting some of the fields
|
|
|
|
// respectively to the given tag.
|
|
|
|
func do(baseConfig interface{}, tag string, redactByDefault, indent bool) (string, error) {
|
2017-10-02 08:32:02 +00:00
|
|
|
anomConfig, err := copystructure.Copy(baseConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
val := reflect.ValueOf(anomConfig)
|
|
|
|
|
2022-01-24 10:08:05 +00:00
|
|
|
err = doOnStruct(val, tag, redactByDefault)
|
2017-10-02 08:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
configJSON, err := marshal(anomConfig, indent)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-01-24 10:08:05 +00:00
|
|
|
return string(configJSON), nil
|
2017-10-02 08:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func doOnJSON(input string) string {
|
2023-09-26 06:28:25 +00:00
|
|
|
return xurls.Relaxed().ReplaceAllString(input, maskLarge)
|
2017-10-02 08:32:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 10:08:05 +00:00
|
|
|
func doOnStruct(field reflect.Value, tag string, redactByDefault bool) error {
|
2020-12-03 14:52:05 +00:00
|
|
|
if field.Type().AssignableTo(reflect.TypeOf(dynamic.PluginConf{})) {
|
|
|
|
resetPlugin(field)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-02 08:32:02 +00:00
|
|
|
switch field.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
if !field.IsNil() {
|
2022-01-24 10:08:05 +00:00
|
|
|
if err := doOnStruct(field.Elem(), tag, redactByDefault); err != nil {
|
2017-10-02 08:32:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
2024-02-19 14:44:03 +00:00
|
|
|
for i := range field.NumField() {
|
2017-10-02 08:32:02 +00:00
|
|
|
fld := field.Field(i)
|
|
|
|
stField := field.Type().Field(i)
|
|
|
|
if !isExported(stField) {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-03 14:52:05 +00:00
|
|
|
|
2022-01-24 10:08:05 +00:00
|
|
|
if stField.Tag.Get(tag) == "false" || stField.Tag.Get(tag) != "true" && redactByDefault {
|
|
|
|
if err := reset(fld, stField.Name); err != nil {
|
|
|
|
return err
|
2017-10-02 08:32:02 +00:00
|
|
|
}
|
2022-01-24 10:08:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// A struct field cannot be set it must be filled as pointer.
|
|
|
|
if fld.Kind() == reflect.Struct {
|
|
|
|
fldPtr := reflect.New(fld.Type())
|
|
|
|
fldPtr.Elem().Set(fld)
|
2020-12-03 14:52:05 +00:00
|
|
|
|
2022-01-24 10:08:05 +00:00
|
|
|
if err := doOnStruct(fldPtr, tag, redactByDefault); err != nil {
|
2017-10-02 08:32:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-24 10:08:05 +00:00
|
|
|
|
|
|
|
fld.Set(fldPtr.Elem())
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := doOnStruct(fld, tag, redactByDefault); err != nil {
|
2020-12-03 14:52:05 +00:00
|
|
|
return err
|
2017-10-02 08:32:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Map:
|
|
|
|
for _, key := range field.MapKeys() {
|
2020-12-03 14:52:05 +00:00
|
|
|
val := field.MapIndex(key)
|
|
|
|
|
|
|
|
// A struct value cannot be set it must be filled as pointer.
|
|
|
|
if val.Kind() == reflect.Struct {
|
|
|
|
valPtr := reflect.New(val.Type())
|
|
|
|
valPtr.Elem().Set(val)
|
|
|
|
|
2022-01-24 10:08:05 +00:00
|
|
|
if err := doOnStruct(valPtr, tag, redactByDefault); err != nil {
|
2020-12-03 14:52:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
field.SetMapIndex(key, valPtr.Elem())
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-24 10:08:05 +00:00
|
|
|
if err := doOnStruct(val, tag, redactByDefault); err != nil {
|
2017-10-02 08:32:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Slice:
|
2024-02-19 14:44:03 +00:00
|
|
|
for j := range field.Len() {
|
2022-01-24 10:08:05 +00:00
|
|
|
if err := doOnStruct(field.Index(j), tag, redactByDefault); err != nil {
|
2017-10-02 08:32:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-30 11:44:05 +00:00
|
|
|
|
2017-10-02 08:32:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func reset(field reflect.Value, name string) error {
|
|
|
|
if !field.CanSet() {
|
|
|
|
return fmt.Errorf("cannot reset field %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch field.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
if !field.IsNil() {
|
|
|
|
field.Set(reflect.Zero(field.Type()))
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
if field.IsValid() {
|
|
|
|
field.Set(reflect.Zero(field.Type()))
|
|
|
|
}
|
|
|
|
case reflect.String:
|
|
|
|
if field.String() != "" {
|
2024-01-11 16:06:06 +00:00
|
|
|
if field.Type().AssignableTo(reflect.TypeOf(types.FileOrContent(""))) {
|
|
|
|
field.Set(reflect.ValueOf(types.FileOrContent(maskShort)))
|
2020-12-03 14:52:05 +00:00
|
|
|
} else {
|
|
|
|
field.Set(reflect.ValueOf(maskShort))
|
|
|
|
}
|
2017-10-02 08:32:02 +00:00
|
|
|
}
|
|
|
|
case reflect.Map:
|
|
|
|
if field.Len() > 0 {
|
|
|
|
field.Set(reflect.MakeMap(field.Type()))
|
|
|
|
}
|
|
|
|
case reflect.Slice:
|
|
|
|
if field.Len() > 0 {
|
2020-10-30 11:44:05 +00:00
|
|
|
switch field.Type().Elem().Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
slice := reflect.MakeSlice(field.Type(), field.Len(), field.Len())
|
2024-02-19 14:44:03 +00:00
|
|
|
for j := range field.Len() {
|
2020-10-30 11:44:05 +00:00
|
|
|
slice.Index(j).SetString(maskShort)
|
|
|
|
}
|
|
|
|
field.Set(slice)
|
|
|
|
default:
|
|
|
|
field.Set(reflect.MakeSlice(field.Type(), 0, 0))
|
|
|
|
}
|
2017-10-02 08:32:02 +00:00
|
|
|
}
|
|
|
|
case reflect.Interface:
|
2021-09-15 08:36:14 +00:00
|
|
|
return fmt.Errorf("reset not supported for interface type (for %s field)", name)
|
2017-10-02 08:32:02 +00:00
|
|
|
default:
|
|
|
|
// Primitive type
|
|
|
|
field.Set(reflect.Zero(field.Type()))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-03 14:52:05 +00:00
|
|
|
// resetPlugin resets the plugin configuration so it keep the plugin name but not its configuration.
|
|
|
|
func resetPlugin(field reflect.Value) {
|
|
|
|
for _, key := range field.MapKeys() {
|
|
|
|
field.SetMapIndex(key, reflect.ValueOf(struct{}{}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// isExported return true is a struct field is exported, else false.
|
2017-10-02 08:32:02 +00:00
|
|
|
func isExported(f reflect.StructField) bool {
|
|
|
|
if f.PkgPath != "" && !f.Anonymous {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshal(anomConfig interface{}, indent bool) ([]byte, error) {
|
|
|
|
if indent {
|
2020-10-30 11:44:05 +00:00
|
|
|
return json.MarshalIndent(anomConfig, "", " ")
|
2017-10-02 08:32:02 +00:00
|
|
|
}
|
|
|
|
return json.Marshal(anomConfig)
|
|
|
|
}
|