Config UI: keep mqtt and influx secrets on edit (part 3) (#20404)
This commit is contained in:
parent
fd48bf35c4
commit
30e9fa6318
5 changed files with 101 additions and 127 deletions
|
|
@ -7,6 +7,7 @@ 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"
|
||||
|
|
@ -79,39 +80,24 @@ func masked(s any) string {
|
|||
}
|
||||
|
||||
type Mqtt struct {
|
||||
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"`
|
||||
mqtt.Config `mapstructure:",squash"`
|
||||
Topic string `json:"topic"`
|
||||
}
|
||||
|
||||
// Redacted implements the redactor interface used by the tee publisher
|
||||
func (m Mqtt) Redacted() any {
|
||||
return struct {
|
||||
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"`
|
||||
}{
|
||||
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),
|
||||
return Mqtt{
|
||||
Config: mqtt.Config{
|
||||
Broker: m.Broker,
|
||||
User: m.User,
|
||||
Password: masked(m.Password),
|
||||
ClientID: m.ClientID,
|
||||
Insecure: m.Insecure,
|
||||
CaCert: masked(m.CaCert),
|
||||
ClientCert: masked(m.ClientCert),
|
||||
ClientKey: masked(m.ClientKey),
|
||||
},
|
||||
Topic: m.Topic,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -128,22 +114,14 @@ type Influx struct {
|
|||
|
||||
// Redacted implements the redactor interface used by the tee publisher
|
||||
func (c Influx) Redacted() any {
|
||||
return struct {
|
||||
URL string `json:"url"`
|
||||
Database string `json:"database"`
|
||||
Org string `json:"org"`
|
||||
User string `json:"user"`
|
||||
Insecure bool `json:"insecure"`
|
||||
Password string `json:"password,omitempty"`
|
||||
Token string `json:"token,omitempty"`
|
||||
}{
|
||||
return Influx{
|
||||
URL: c.URL,
|
||||
Database: c.Database,
|
||||
Token: masked(c.Token),
|
||||
Org: c.Org,
|
||||
User: c.User,
|
||||
Insecure: c.Insecure,
|
||||
Password: masked(c.Password),
|
||||
Token: masked(c.Token),
|
||||
Insecure: c.Insecure,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,10 +4,12 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"reflect"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"dario.cat/mergo"
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/config"
|
||||
|
|
@ -284,3 +286,24 @@ func testInstance(instance any) map[string]testResult {
|
|||
|
||||
return res
|
||||
}
|
||||
|
||||
// mergeMaskedAny similar to mergeMasked but for interfaces
|
||||
func mergeMaskedAny(old, new any) error {
|
||||
return mergo.Merge(new, old, mergo.WithTransformers(&maskedTransformer{}))
|
||||
}
|
||||
|
||||
type maskedTransformer struct{}
|
||||
|
||||
func (maskedTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
|
||||
if typ.Kind() != reflect.String {
|
||||
return nil
|
||||
}
|
||||
|
||||
return func(dst, src reflect.Value) error {
|
||||
if dst.String() == masked {
|
||||
dst.Set(src)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import (
|
|||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/evcc-io/evcc/api/globalconfig"
|
||||
"github.com/evcc-io/evcc/plugin/mqtt"
|
||||
"github.com/evcc-io/evcc/util/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
|
@ -44,3 +46,60 @@ func TestConfigReqMarshalToMap(t *testing.T) {
|
|||
"deviceProduct": "product",
|
||||
}, res)
|
||||
}
|
||||
|
||||
type testStruct struct {
|
||||
Field1 string
|
||||
Field2 int
|
||||
}
|
||||
|
||||
func TestMergeMaskedAny(t *testing.T) {
|
||||
tests := []struct {
|
||||
old any
|
||||
new, expected *testStruct
|
||||
}{
|
||||
{
|
||||
old: nil,
|
||||
new: &testStruct{"newValue1", 42},
|
||||
expected: &testStruct{"newValue1", 42},
|
||||
},
|
||||
{
|
||||
old: &testStruct{"oldValue1", 24},
|
||||
new: &testStruct{"newValue1", 42},
|
||||
expected: &testStruct{"newValue1", 42},
|
||||
},
|
||||
{
|
||||
old: &testStruct{"oldValue1", 24},
|
||||
new: &testStruct{masked, 42},
|
||||
expected: &testStruct{"oldValue1", 42},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
require.NoError(t, mergeMaskedAny(tc.old, tc.new))
|
||||
assert.Equal(t, tc.expected, tc.new)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSquashedMergeMaskedAny(t *testing.T) {
|
||||
old := globalconfig.Mqtt{
|
||||
Config: mqtt.Config{
|
||||
Broker: "host",
|
||||
User: "user",
|
||||
},
|
||||
Topic: "test",
|
||||
}
|
||||
{
|
||||
new := old
|
||||
new.User = masked
|
||||
|
||||
require.NoError(t, mergeMaskedAny(old, &new))
|
||||
assert.Equal(t, "user", new.User)
|
||||
}
|
||||
{
|
||||
new := old
|
||||
new.User = "new"
|
||||
|
||||
require.NoError(t, mergeMaskedAny(old, &new))
|
||||
assert.Equal(t, "new", new.User)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,11 +9,8 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/server/db/settings"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/fatih/structs"
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
"github.com/gorilla/mux"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
|
@ -87,7 +84,7 @@ func settingsSetJsonHandler(key string, valueChan chan<- util.Param, newStruc fu
|
|||
|
||||
oldStruc := newStruc()
|
||||
if err := settings.Json(key, &oldStruc); err == nil {
|
||||
if err := mergeSettings(oldStruc, struc); err != nil {
|
||||
if err := mergeMaskedAny(oldStruc, struc); err != nil {
|
||||
jsonError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -112,24 +109,3 @@ func settingsDeleteJsonHandler(key string, valueChan chan<- util.Param, struc an
|
|||
jsonResult(w, true)
|
||||
}
|
||||
}
|
||||
|
||||
func mergeSettings(old any, new any) error {
|
||||
redactable, ok := old.(api.Redactor)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
newMap := structs.Map(new)
|
||||
oldMap := structs.Map(old)
|
||||
redactedMap := structs.Map(redactable.Redacted())
|
||||
|
||||
for k, v := range newMap {
|
||||
if rv, ok := redactedMap[k]; ok && v == rv {
|
||||
if ov, ok := oldMap[k]; ok {
|
||||
newMap[k] = ov
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mapstructure.Decode(newMap, &new)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,62 +0,0 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMergeSettings(t *testing.T) {
|
||||
tests := []struct {
|
||||
old any
|
||||
new *RedactedStruct
|
||||
expected *RedactedStruct
|
||||
}{
|
||||
{
|
||||
old: nil,
|
||||
new: &RedactedStruct{"newValue1", 42},
|
||||
expected: &RedactedStruct{"newValue1", 42},
|
||||
},
|
||||
{
|
||||
old: &TestStruct{"oldValue1", 24},
|
||||
new: &RedactedStruct{"newValue1", 42},
|
||||
expected: &RedactedStruct{"newValue1", 42},
|
||||
},
|
||||
{
|
||||
old: &RedactedStruct{"oldValue1", 24},
|
||||
new: &RedactedStruct{"redacted", 42},
|
||||
expected: &RedactedStruct{"oldValue1", 42},
|
||||
},
|
||||
{
|
||||
old: &RedactedStruct{"oldValue1", 24},
|
||||
new: &RedactedStruct{"newValue1", 42},
|
||||
expected: &RedactedStruct{"newValue1", 42},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
mergeSettings(tc.old, tc.new)
|
||||
assert.Equal(t, tc.expected.Field1, tc.new.Field1)
|
||||
assert.Equal(t, tc.expected.Field2, tc.new.Field2)
|
||||
}
|
||||
}
|
||||
|
||||
type TestStruct struct {
|
||||
Field1 string
|
||||
Field2 int
|
||||
}
|
||||
|
||||
type RedactedStruct struct {
|
||||
Field1 string
|
||||
Field2 int
|
||||
}
|
||||
|
||||
func (t *RedactedStruct) Redacted() any {
|
||||
return struct {
|
||||
Field1 string
|
||||
Field2 int
|
||||
}{
|
||||
Field1: "redacted",
|
||||
Field2: t.Field2,
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue