This reverts commit 5211d5de15.
Этот коммит содержится в:
@@ -4,11 +4,9 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -24,23 +22,16 @@ type LogConfigSrc interface {
|
||||
Get() mlog.LoggerConfiguration
|
||||
|
||||
// Set updates the dsn specifying the source and reloads
|
||||
Set(dsn []byte, configStore *Store) (err error)
|
||||
Set(dsn string, configStore *Store) (err error)
|
||||
|
||||
// Close cleans up resources.
|
||||
Close() error
|
||||
}
|
||||
|
||||
func IsEmptyDSN(dsn json.RawMessage) bool {
|
||||
if len(dsn) == 0 || bytes.Equal(dsn, []byte("{}")) || bytes.Equal(dsn, []byte("\"\"")) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// NewLogConfigSrc creates an advanced logging configuration source, backed by a
|
||||
// file, JSON, or database.
|
||||
func NewLogConfigSrc(dsn json.RawMessage, configStore *Store) (LogConfigSrc, error) {
|
||||
if len(dsn) == 0 {
|
||||
// file, JSON string, or database.
|
||||
func NewLogConfigSrc(dsn string, configStore *Store) (LogConfigSrc, error) {
|
||||
if dsn == "" {
|
||||
return nil, errors.New("dsn should not be empty")
|
||||
}
|
||||
|
||||
@@ -48,28 +39,17 @@ func NewLogConfigSrc(dsn json.RawMessage, configStore *Store) (LogConfigSrc, err
|
||||
return nil, errors.New("configStore should not be nil")
|
||||
}
|
||||
|
||||
// check if embedded JSON
|
||||
dsn = strings.TrimSpace(dsn)
|
||||
|
||||
if isJSONMap(dsn) {
|
||||
return newJSONSrc(dsn)
|
||||
}
|
||||
|
||||
// Now we're treating the DSN as a string which may contain escaped JSON or be a filespec.
|
||||
str := strings.TrimSpace(string(dsn))
|
||||
if s, err := strconv.Unquote(str); err == nil {
|
||||
str = s
|
||||
}
|
||||
|
||||
// check if escaped JSON
|
||||
strBytes := []byte(str)
|
||||
if isJSONMap(strBytes) {
|
||||
return newJSONSrc(strBytes)
|
||||
}
|
||||
|
||||
path := dsn
|
||||
// If this is a file based config we need the full path so it can be watched.
|
||||
path := str
|
||||
if strings.HasPrefix(configStore.String(), "file://") && !filepath.IsAbs(path) {
|
||||
if strings.HasPrefix(configStore.String(), "file://") && !filepath.IsAbs(dsn) {
|
||||
configPath := strings.TrimPrefix(configStore.String(), "file://")
|
||||
path = filepath.Join(filepath.Dir(configPath), path)
|
||||
path = filepath.Join(filepath.Dir(configPath), dsn)
|
||||
}
|
||||
|
||||
return newFileSrc(path, configStore)
|
||||
@@ -83,7 +63,7 @@ type jsonSrc struct {
|
||||
cfg mlog.LoggerConfiguration
|
||||
}
|
||||
|
||||
func newJSONSrc(data json.RawMessage) (*jsonSrc, error) {
|
||||
func newJSONSrc(data string) (*jsonSrc, error) {
|
||||
src := &jsonSrc{}
|
||||
return src, src.Set(data, nil)
|
||||
}
|
||||
@@ -96,8 +76,8 @@ func (src *jsonSrc) Get() mlog.LoggerConfiguration {
|
||||
}
|
||||
|
||||
// Set updates the JSON specifying the source and reloads
|
||||
func (src *jsonSrc) Set(data []byte, _ *Store) error {
|
||||
cfg, err := logTargetCfgFromJSON(data)
|
||||
func (src *jsonSrc) Set(data string, _ *Store) error {
|
||||
cfg, err := logTargetCfgFromJSON([]byte(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -132,7 +112,7 @@ func newFileSrc(path string, configStore *Store) (*fileSrc, error) {
|
||||
src := &fileSrc{
|
||||
path: path,
|
||||
}
|
||||
if err := src.Set([]byte(path), configStore); err != nil {
|
||||
if err := src.Set(path, configStore); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return src, nil
|
||||
@@ -148,8 +128,8 @@ func (src *fileSrc) Get() mlog.LoggerConfiguration {
|
||||
// Set updates the dsn specifying the file source and reloads.
|
||||
// The file will be watched for changes and reloaded as needed,
|
||||
// and all listeners notified.
|
||||
func (src *fileSrc) Set(path []byte, configStore *Store) error {
|
||||
data, err := configStore.GetFile(string(path))
|
||||
func (src *fileSrc) Set(path string, configStore *Store) error {
|
||||
data, err := configStore.GetFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4,41 +4,36 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var (
|
||||
validJSON = []byte(`{"file":{ "Type":"file"}}`)
|
||||
badJSON = []byte(`{"file":{ Type="file"}}`)
|
||||
validEscapedJSON = []byte(`"{\"file\":{ \"Type\":\"file\"}}"`)
|
||||
badEscapedJSON = []byte(`"{\"file\":{ Type:\"file\"}}"`)
|
||||
const (
|
||||
validJSON = `{"file":{ "Type":"file"}}`
|
||||
badJSON = `{"file":{ Type="file"}}`
|
||||
)
|
||||
|
||||
func TestNewLogConfigSrc(t *testing.T) {
|
||||
store := NewTestMemoryStore()
|
||||
require.NotNil(t, store)
|
||||
err := store.SetFile("advancedlogging.conf", validJSON)
|
||||
err := store.SetFile("advancedlogging.conf", []byte(validJSON))
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
dsn json.RawMessage
|
||||
dsn string
|
||||
configStore *Store
|
||||
wantErr bool
|
||||
wantType LogConfigSrc
|
||||
}{
|
||||
{name: "empty dsn", dsn: []byte(""), configStore: store, wantErr: true, wantType: nil},
|
||||
{name: "garbage dsn", dsn: []byte("!@wfejwcevioj"), configStore: store, wantErr: true, wantType: nil},
|
||||
{name: "empty dsn", dsn: "", configStore: store, wantErr: true, wantType: nil},
|
||||
{name: "garbage dsn", dsn: "!@wfejwcevioj", configStore: store, wantErr: true, wantType: nil},
|
||||
{name: "valid json dsn", dsn: validJSON, configStore: store, wantErr: false, wantType: &jsonSrc{}},
|
||||
{name: "invalid json dsn", dsn: badJSON, configStore: store, wantErr: true, wantType: nil},
|
||||
{name: "valid escaped json dsn", dsn: validEscapedJSON, configStore: store, wantErr: false, wantType: &jsonSrc{}},
|
||||
{name: "invalid escaped json dsn", dsn: badEscapedJSON, configStore: store, wantErr: true, wantType: nil},
|
||||
{name: "valid filespec dsn", dsn: []byte("advancedlogging.conf"), configStore: store, wantErr: false, wantType: &fileSrc{}},
|
||||
{name: "invalid filespec dsn", dsn: []byte("/nobody/here.conf"), configStore: store, wantErr: true, wantType: nil},
|
||||
{name: "valid filespec dsn", dsn: "advancedlogging.conf", configStore: store, wantErr: false, wantType: &fileSrc{}},
|
||||
{name: "invalid filespec dsn", dsn: "/nobody/here.conf", configStore: store, wantErr: true, wantType: nil},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
@@ -33,8 +33,8 @@ func Migrate(from, to string) error {
|
||||
}
|
||||
|
||||
// Only migrate advanced logging config if it is not embedded JSON.
|
||||
if !isJSONMap(sourceConfig.LogSettings.AdvancedLoggingConfig) {
|
||||
files = append(files, string(sourceConfig.LogSettings.AdvancedLoggingConfig))
|
||||
if !isJSONMap(*sourceConfig.LogSettings.AdvancedLoggingConfig) {
|
||||
files = append(files, *sourceConfig.LogSettings.AdvancedLoggingConfig)
|
||||
}
|
||||
|
||||
files = append(files, sourceConfig.PluginSettings.SignaturePublicKeyFiles...)
|
||||
|
||||
@@ -200,10 +200,9 @@ func stripPassword(dsn, schema string) string {
|
||||
return prefix + dsn[:i+1] + dsn[j:]
|
||||
}
|
||||
|
||||
func isJSONMap(data []byte) bool {
|
||||
func isJSONMap(data string) bool {
|
||||
var m map[string]any
|
||||
err := json.Unmarshal(data, &m)
|
||||
return err == nil
|
||||
return json.Unmarshal([]byte(data), &m) == nil
|
||||
}
|
||||
|
||||
func GetValueByPath(path []string, obj any) (any, bool) {
|
||||
|
||||
@@ -276,7 +276,7 @@ func TestIsJSONMap(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := isJSONMap([]byte(tt.data)); got != tt.want {
|
||||
if got := isJSONMap(tt.data); got != tt.want {
|
||||
t.Errorf("isJSONMap() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
|
||||
Ссылка в новой задаче
Block a user