Revert "Config UI: keep mqtt and influx secrets (part 2) (#20340)"
This reverts commit 2e5f1808d7.
This commit is contained in:
parent
5d93b23572
commit
fd48bf35c4
4 changed files with 43 additions and 89 deletions
|
|
@ -7,7 +7,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/plugin/mqtt"
|
||||
"github.com/evcc-io/evcc/push"
|
||||
"github.com/evcc-io/evcc/server/eebus"
|
||||
"github.com/evcc-io/evcc/util/config"
|
||||
|
|
@ -80,18 +79,39 @@ func masked(s any) string {
|
|||
}
|
||||
|
||||
type Mqtt struct {
|
||||
mqtt.Config
|
||||
Topic string `json:"topic"`
|
||||
Broker string `json:"broker"`
|
||||
Topic string `json:"topic"`
|
||||
User string `json:"user"`
|
||||
ClientID string `json:"clientID"`
|
||||
Insecure bool `json:"insecure"`
|
||||
Password string `json:"password"`
|
||||
CaCert string `json:"caCert"`
|
||||
ClientCert string `json:"clientCert"`
|
||||
ClientKey string `json:"clientKey"`
|
||||
}
|
||||
|
||||
// Redacted implements the redactor interface used by the tee publisher
|
||||
func (m Mqtt) Redacted() any {
|
||||
return struct {
|
||||
mqtt.Config
|
||||
Topic string `json:"topic"`
|
||||
Broker string `json:"broker"`
|
||||
Topic string `json:"topic"`
|
||||
User string `json:"user,omitempty"`
|
||||
ClientID string `json:"clientID,omitempty"`
|
||||
Insecure bool `json:"insecure,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
CaCert string `json:"caCert,omitempty"`
|
||||
ClientCert string `json:"clientCert,omitempty"`
|
||||
ClientKey string `json:"clientKey,omitempty"`
|
||||
}{
|
||||
m.Config.Redacted(),
|
||||
m.Topic,
|
||||
Broker: m.Broker,
|
||||
Topic: m.Topic,
|
||||
User: m.User,
|
||||
ClientID: m.ClientID,
|
||||
Insecure: m.Insecure,
|
||||
Password: masked(m.Password),
|
||||
CaCert: masked(m.CaCert),
|
||||
ClientCert: masked(m.ClientCert),
|
||||
ClientKey: masked(m.ClientKey),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,33 +28,12 @@ func ClientID() string {
|
|||
type Config struct {
|
||||
Broker string `json:"broker"`
|
||||
User string `json:"user"`
|
||||
Password string `json:"password,omitempty"`
|
||||
ClientID string `json:"clientID,omitempty"`
|
||||
Insecure bool `json:"insecure,omitempty"`
|
||||
CaCert string `json:"caCert,omitempty"`
|
||||
ClientCert string `json:"clientCert,omitempty"`
|
||||
ClientKey string `json:"clientKey,omitempty"`
|
||||
}
|
||||
|
||||
func masked(s any) string {
|
||||
if s != "" {
|
||||
return "***"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Redacted implements the redactor interface
|
||||
func (c Config) Redacted() Config {
|
||||
return Config{
|
||||
Broker: c.Broker,
|
||||
User: c.User,
|
||||
Password: masked(c.Password),
|
||||
ClientID: c.ClientID,
|
||||
Insecure: c.Insecure,
|
||||
CaCert: masked(c.CaCert),
|
||||
ClientCert: masked(c.ClientCert),
|
||||
ClientKey: masked(c.ClientKey),
|
||||
}
|
||||
Password string `json:"password"`
|
||||
ClientID string `json:"clientID"`
|
||||
Insecure bool `json:"insecure"`
|
||||
CaCert string `json:"caCert"`
|
||||
ClientCert string `json:"clientCert"`
|
||||
ClientKey string `json:"clientKey"`
|
||||
}
|
||||
|
||||
// Client encapsulates mqtt publish/subscribe functions
|
||||
|
|
|
|||
|
|
@ -123,23 +123,13 @@ func mergeSettings(old any, new any) error {
|
|||
oldMap := structs.Map(old)
|
||||
redactedMap := structs.Map(redactable.Redacted())
|
||||
|
||||
newMap = mergeRedacted(oldMap, newMap, redactedMap)
|
||||
return mapstructure.Decode(newMap, &new)
|
||||
}
|
||||
|
||||
func mergeRedacted(old, new, redacted map[string]any) map[string]any {
|
||||
for k, v := range new {
|
||||
rv := redacted[k]
|
||||
ov := old[k]
|
||||
if nv, ok := v.(map[string]any); ok {
|
||||
rv, ok := rv.(map[string]any)
|
||||
ov, ok2 := ov.(map[string]any)
|
||||
if ok && ok2 {
|
||||
new[k] = mergeRedacted(ov, nv, rv)
|
||||
for k, v := range newMap {
|
||||
if rv, ok := redactedMap[k]; ok && v == rv {
|
||||
if ov, ok := oldMap[k]; ok {
|
||||
newMap[k] = ov
|
||||
}
|
||||
} else if rv == v {
|
||||
new[k] = ov
|
||||
}
|
||||
}
|
||||
return new
|
||||
|
||||
return mapstructure.Decode(newMap, &new)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ import (
|
|||
func TestMergeSettings(t *testing.T) {
|
||||
tests := []struct {
|
||||
old any
|
||||
new any
|
||||
expected any
|
||||
new *RedactedStruct
|
||||
expected *RedactedStruct
|
||||
}{
|
||||
{
|
||||
old: nil,
|
||||
|
|
@ -32,29 +32,12 @@ func TestMergeSettings(t *testing.T) {
|
|||
new: &RedactedStruct{"newValue1", 42},
|
||||
expected: &RedactedStruct{"newValue1", 42},
|
||||
},
|
||||
{
|
||||
old: &NestedStruct{RedactedStruct{"oldValue1", 35}, 24},
|
||||
new: &NestedStruct{RedactedStruct{"newValue1", 45}, 42},
|
||||
expected: &NestedStruct{RedactedStruct{"newValue1", 45}, 42},
|
||||
},
|
||||
{
|
||||
old: &NestedStruct{RedactedStruct{"oldValue1", 35}, 24},
|
||||
new: &NestedStruct{RedactedStruct{"redacted", 45}, 0},
|
||||
expected: &NestedStruct{RedactedStruct{"oldValue1", 45}, 24},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
mergeSettings(tc.old, tc.new)
|
||||
switch expected := tc.expected.(type) {
|
||||
case *RedactedStruct:
|
||||
assert.Equal(t, expected.Field1, tc.new.(*RedactedStruct).Field1)
|
||||
assert.Equal(t, expected.Field2, tc.new.(*RedactedStruct).Field2)
|
||||
case *NestedStruct:
|
||||
assert.Equal(t, expected.Field1.Field1, tc.new.(*NestedStruct).Field1.Field1)
|
||||
assert.Equal(t, expected.Field1.Field2, tc.new.(*NestedStruct).Field1.Field2)
|
||||
assert.Equal(t, expected.Field2, tc.new.(*NestedStruct).Field2)
|
||||
}
|
||||
assert.Equal(t, tc.expected.Field1, tc.new.Field1)
|
||||
assert.Equal(t, tc.expected.Field2, tc.new.Field2)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -68,11 +51,6 @@ type RedactedStruct struct {
|
|||
Field2 int
|
||||
}
|
||||
|
||||
type NestedStruct struct {
|
||||
Field1 RedactedStruct
|
||||
Field2 int
|
||||
}
|
||||
|
||||
func (t *RedactedStruct) Redacted() any {
|
||||
return struct {
|
||||
Field1 string
|
||||
|
|
@ -82,16 +60,3 @@ func (t *RedactedStruct) Redacted() any {
|
|||
Field2: t.Field2,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *NestedStruct) Redacted() any {
|
||||
return struct {
|
||||
Field1 RedactedStruct
|
||||
Field2 int
|
||||
}{
|
||||
Field1: t.Field1.Redacted().(struct {
|
||||
Field1 string
|
||||
Field2 int
|
||||
}),
|
||||
Field2: 0,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue