Support json.RawMessage in configuration env overrides (#23610)

* support json.RawMessage in env overrides
This commit is contained in:
Doug Lauder 2023-06-07 17:21:58 -04:00 committed by GitHub
parent 803d0c6e60
commit ac3c0da8ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 48 deletions

View File

@ -111,13 +111,17 @@ func (s *Server) configureAudit(adt *audit.Audit, bAllowAdvancedLogging bool) er
var logConfigSrc config.LogConfigSrc
dsn := s.platform.Config().ExperimentalAuditSettings.GetAdvancedLoggingConfig()
if bAllowAdvancedLogging && !utils.IsEmptyJSON(dsn) {
var err error
logConfigSrc, err = config.NewLogConfigSrc(dsn, s.platform.GetConfigStore())
if err != nil {
return fmt.Errorf("invalid config source for audit, %w", err)
if bAllowAdvancedLogging {
if !utils.IsEmptyJSON(dsn) {
var err error
logConfigSrc, err = config.NewLogConfigSrc(dsn, s.platform.GetConfigStore())
if err != nil {
return fmt.Errorf("invalid config source for audit, %w", err)
}
mlog.Debug("Loaded audit configuration", mlog.String("source", string(dsn)))
} else {
s.Log().Debug("Advanced logging config not provided for audit")
}
mlog.Debug("Loaded audit configuration", mlog.String("source", string(dsn)))
}
// ExperimentalAuditSettings provides basic file audit (E0, E10); logConfigSrc provides advanced config (E20).

View File

@ -129,6 +129,8 @@ func (ps *PlatformService) ConfigureLogger(name string, logger *mlog.Logger, log
return fmt.Errorf("invalid config source for %s, %w", name, err)
}
ps.logger.Info("Loaded configuration for "+name, mlog.String("source", string(dsn)))
} else {
ps.logger.Debug("Advanced logging config not provided for " + name)
}
cfg, err := config.MloggerConfigFromLoggerConfig(logSettings, logConfigSrc, getPath)

View File

@ -72,7 +72,11 @@ func applyEnvKey(key, value string, rValueSubject reflect.Value) {
if err == nil {
rFieldValue.Set(reflect.ValueOf(intVal))
}
case reflect.SliceOf(reflect.TypeOf("")).Kind():
case reflect.Slice:
if rFieldValue.Type() == reflect.TypeOf(json.RawMessage{}) {
rFieldValue.Set(reflect.ValueOf([]byte(value)))
break
}
rFieldValue.Set(reflect.ValueOf(strings.Split(value, " ")))
case reflect.Map:
target := reflect.New(rFieldValue.Type()).Interface()

View File

@ -1336,31 +1336,23 @@ func (s *LogSettings) SetDefaults() {
}
if utils.IsEmptyJSON(s.AdvancedLoggingJSON) {
// copy any non-empty AdvancedLoggingConfig (deprecated) to the new field.
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
s.AdvancedLoggingJSON = utils.StringPtrToJSON(s.AdvancedLoggingConfig)
} else {
s.AdvancedLoggingJSON = []byte("{}")
}
s.AdvancedLoggingJSON = []byte("{}")
}
// temporarily let AdvancedLoggingConfig take precedence.
if s.AdvancedLoggingConfig == nil {
s.AdvancedLoggingConfig = NewString("")
}
//s.AdvancedLoggingConfig = nil
}
// GetAdvancedLoggingConfig returns the advanced logging config as a []byte.
// AdvancedLoggingJSON takes precident over the deprecated AdvancedLoggingConfig.
// AdvancedLoggingJSON takes precedence over the deprecated AdvancedLoggingConfig.
func (s *LogSettings) GetAdvancedLoggingConfig() []byte {
// temporarily let AdvancedLoggingConfig take precedence.
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
return []byte(*s.AdvancedLoggingConfig)
}
if !utils.IsEmptyJSON(s.AdvancedLoggingJSON) {
return s.AdvancedLoggingJSON
}
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
return []byte(*s.AdvancedLoggingConfig)
}
return []byte("{}")
}
@ -1406,31 +1398,23 @@ func (s *ExperimentalAuditSettings) SetDefaults() {
}
if utils.IsEmptyJSON(s.AdvancedLoggingJSON) {
// copy any non-empty AdvancedLoggingConfig (deprecated) to the new field.
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
s.AdvancedLoggingJSON = utils.StringPtrToJSON(s.AdvancedLoggingConfig)
} else {
s.AdvancedLoggingJSON = []byte("{}")
}
s.AdvancedLoggingJSON = []byte("{}")
}
// temporarily let AdvancedLoggingConfig take precedence.
if s.AdvancedLoggingConfig == nil {
s.AdvancedLoggingConfig = NewString("")
}
//s.AdvancedLoggingConfig = nil
}
// GetAdvancedLoggingConfig returns the advanced logging config as a []byte.
// AdvancedLoggingJSON takes precident over the deprecated AdvancedLoggingConfig.
// AdvancedLoggingJSON takes precedence over the deprecated AdvancedLoggingConfig.
func (s *ExperimentalAuditSettings) GetAdvancedLoggingConfig() []byte {
// temporarily let AdvancedLoggingConfig take precedence.
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
return []byte(*s.AdvancedLoggingConfig)
}
if !utils.IsEmptyJSON(s.AdvancedLoggingJSON) {
return s.AdvancedLoggingJSON
}
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
return []byte(*s.AdvancedLoggingConfig)
}
return []byte("{}")
}
@ -1481,30 +1465,23 @@ func (s *NotificationLogSettings) SetDefaults() {
}
if utils.IsEmptyJSON(s.AdvancedLoggingJSON) {
// copy any non-empty AdvancedLoggingConfig (deprecated) to the new field.
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
s.AdvancedLoggingJSON = utils.StringPtrToJSON(s.AdvancedLoggingConfig)
} else {
s.AdvancedLoggingJSON = []byte("{}")
}
s.AdvancedLoggingJSON = []byte("{}")
}
// temporarily let AdvancedLoggingConfig take precedence.
if s.AdvancedLoggingConfig == nil {
s.AdvancedLoggingConfig = NewString("")
}
//s.AdvancedLoggingConfig = nil
}
// GetAdvancedLoggingConfig returns the advanced logging config as a []byte.
// AdvancedLoggingJSON takes precident over the deprecated AdvancedLoggingConfig.
// AdvancedLoggingJSON takes precedence over the deprecated AdvancedLoggingConfig.
func (s *NotificationLogSettings) GetAdvancedLoggingConfig() []byte {
// temporarily let AdvancedLoggingConfig take precedence.
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
return []byte(*s.AdvancedLoggingConfig)
}
if !utils.IsEmptyJSON(s.AdvancedLoggingJSON) {
return s.AdvancedLoggingJSON
}
if s.AdvancedLoggingConfig != nil && !utils.IsEmptyJSON([]byte(*s.AdvancedLoggingConfig)) {
return []byte(*s.AdvancedLoggingConfig)
}
return []byte("{}")
}

View File

@ -7,6 +7,7 @@ import (
"bytes"
"encoding/json"
"strings"
"unicode"
"github.com/pkg/errors"
)
@ -57,7 +58,19 @@ func NewHumanizedJSONError(err error, data []byte, offset int64) *HumanizedJSONE
}
func IsEmptyJSON(j json.RawMessage) bool {
if len(j) == 0 || bytes.Equal(j, []byte("{}")) || bytes.Equal(j, []byte("\"\"")) || bytes.Equal(j, []byte("[]")) {
if len(j) == 0 {
return true
}
// remove all whitespace
jj := make([]byte, 0, len(j))
for _, b := range j {
if !unicode.IsSpace(rune(b)) {
jj = append(jj, b)
}
}
if len(jj) == 0 || bytes.Equal(jj, []byte("{}")) || bytes.Equal(jj, []byte("\"\"")) || bytes.Equal(jj, []byte("[]")) {
return true
}
return false

View File

@ -283,6 +283,11 @@ func TestIsJSONEmpty(t *testing.T) {
[]byte("\"hello\""),
false,
},
{
"whitespace still empty",
[]byte(" \n { \t } "),
true,
},
}
for _, testCase := range testCases {