Move struct DynamicOptions to package pkg/server (#5625)
* move struct DynamicOptions to package pkg/server/dynamic_options.go Signed-off-by: wenhaozhou <wenhaozhou@yunify.com> * update test types Signed-off-by: wenhaozhou <wenhaozhou@yunify.com> --------- Signed-off-by: wenhaozhou <wenhaozhou@yunify.com>
This commit is contained in:
57
pkg/server/options/dynamic_options.go
Normal file
57
pkg/server/options/dynamic_options.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package options
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DynamicOptions accept dynamic configuration, the type of key MUST be string
|
||||
type DynamicOptions map[string]interface{}
|
||||
|
||||
func (o DynamicOptions) MarshalJSON() ([]byte, error) {
|
||||
data, err := json.Marshal(desensitize(o))
|
||||
return data, err
|
||||
}
|
||||
|
||||
var (
|
||||
sensitiveKeys = [...]string{"password", "secret"}
|
||||
)
|
||||
|
||||
// isSensitiveData returns whether the input string contains sensitive information
|
||||
func isSensitiveData(key string) bool {
|
||||
for _, v := range sensitiveKeys {
|
||||
if strings.Contains(strings.ToLower(key), v) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// desensitize returns the desensitized data
|
||||
func desensitize(data map[string]interface{}) map[string]interface{} {
|
||||
output := make(map[string]interface{})
|
||||
for k, v := range data {
|
||||
if isSensitiveData(k) {
|
||||
continue
|
||||
}
|
||||
switch v := v.(type) {
|
||||
case map[interface{}]interface{}:
|
||||
output[k] = desensitize(convert(v))
|
||||
default:
|
||||
output[k] = v
|
||||
}
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
// convert returns formatted data
|
||||
func convert(m map[interface{}]interface{}) map[string]interface{} {
|
||||
output := make(map[string]interface{})
|
||||
for k, v := range m {
|
||||
switch k := k.(type) {
|
||||
case string:
|
||||
output[k] = v
|
||||
}
|
||||
}
|
||||
return output
|
||||
}
|
||||
Reference in New Issue
Block a user