Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix confKey API can't replace url/passwd #2538

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion internal/meta/yamlConfigMeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
if nil != err {
return fmt.Errorf(`%s%s.%v`, getMsg(language, source, "type_conversion_fail"), plgName, err)
}

reqField = replacePasswdForConfig("source", plgName, confKey, reqField)
var cfgOps conf.ConfigOperator
var found bool

Expand Down Expand Up @@ -238,6 +238,7 @@
if nil != err {
return fmt.Errorf(`%s%s.%v`, getMsg(language, sink, "type_conversion_fail"), plgName, err)
}
reqField = replacePasswdForConfig("sink", plgName, confKey, reqField)

var cfgOps conf.ConfigOperator
var found bool
Expand Down Expand Up @@ -654,3 +655,30 @@
}
return configResponse
}

func ReplacePasswdForConfig(typ, name, resourceID string, config map[string]interface{}) map[string]interface{} {
ConfigManager.lock.RLock()
defer ConfigManager.lock.RUnlock()
return replacePasswdForConfig(typ, name, resourceID, config)
}

// replacePasswdForConfig reload password from resources if the config both include password(as fake password) and resourceId
func replacePasswdForConfig(typ, name, resourceID string, config map[string]interface{}) map[string]interface{} {
var configOperatorKey string
switch typ {
case "sink":
configOperatorKey = fmt.Sprintf(SinkCfgOperatorKeyTemplate, name)
case "source":
configOperatorKey = fmt.Sprintf(SourceCfgOperatorKeyTemplate, name)
case "connection":
configOperatorKey = fmt.Sprintf(ConnectionCfgOperatorKeyTemplate, name)

Check warning on line 674 in internal/meta/yamlConfigMeta.go

View check run for this annotation

Codecov / codecov/patch

internal/meta/yamlConfigMeta.go#L673-L674

Added lines #L673 - L674 were not covered by tests
}
cfgOp, ok := ConfigManager.cfgOperators[configOperatorKey]
if ok {
if resource, ok := cfgOp.CopyUpdatableConfContent()[resourceID]; ok {
config = hidden.ReplacePasswd(resource, config)
config = hidden.ReplaceUrl(resource, config)
}
}
return config
}
25 changes: 25 additions & 0 deletions internal/meta/yamlConfigMeta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
package meta

import (
"encoding/json"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/lf-edge/ekuiper/internal/conf"
)

Expand Down Expand Up @@ -72,3 +75,25 @@ func TestYamlConfigMeta_Ops(t *testing.T) {
t.Error("should overwrite exist config key")
}
}

func TestConfKeyReplace(t *testing.T) {
a1 := map[string]interface{}{
"url": "mysql://root:[email protected]:3306/test",
"key": "value",
}
bs, err := json.Marshal(a1)
require.NoError(t, err)
err = AddSinkConfKey("sql", "mysql", "", bs)
require.NoError(t, err)
a2 := map[string]interface{}{
"url": "mysql://root:******@127.0.0.1:3306/test",
"key": "value",
}
replaced := replacePasswdForConfig("sink", "sql", "mysql", a2)
require.Equal(t, a1, replaced)

err = AddSourceConfKey("sql", "mysql", "", bs)
require.NoError(t, err)
replaced = replacePasswdForConfig("source", "sql", "mysql", a2)
require.Equal(t, a1, replaced)
}
18 changes: 1 addition & 17 deletions internal/server/rule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/lf-edge/ekuiper/pkg/api"
"github.com/lf-edge/ekuiper/pkg/cast"
"github.com/lf-edge/ekuiper/pkg/errorx"
"github.com/lf-edge/ekuiper/pkg/hidden"
"github.com/lf-edge/ekuiper/pkg/infra"
)

Expand Down Expand Up @@ -161,22 +160,7 @@ func recoverRule(r *api.Rule) string {
func replacePasswdForConfig(typ string, name string, config map[string]interface{}) map[string]interface{} {
if r, ok := config["resourceId"]; ok {
if resourceId, ok := r.(string); ok {
var configOperatorKey string
switch typ {
case "sink":
configOperatorKey = fmt.Sprintf(meta.SinkCfgOperatorKeyTemplate, name)
case "source":
configOperatorKey = fmt.Sprintf(meta.SourceCfgOperatorKeyTemplate, name)
case "connection":
configOperatorKey = fmt.Sprintf(meta.ConnectionCfgOperatorKeyTemplate, name)
}
cfgOp, ok := meta.GetConfOperator(configOperatorKey)
if ok {
if resource, ok := cfgOp.CopyUpdatableConfContent()[resourceId]; ok {
config = hidden.ReplacePasswd(resource, config)
config = hidden.ReplaceUrl(resource, config)
}
}
return meta.ReplacePasswdForConfig(typ, name, resourceId, config)
}
}
return config
Expand Down
Loading