source.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package conf
  15. import (
  16. "strings"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. )
  20. func GetSourceConf(sourceType string, options *ast.Options) map[string]interface{} {
  21. confkey := options.CONF_KEY
  22. yamlOps, err := conf.NewConfigOperatorFromSourceYaml(sourceType)
  23. if err != nil {
  24. conf.Log.Warnf("fail to parse yaml for source %s. Return error %v", sourceType, err)
  25. }
  26. props := make(map[string]interface{})
  27. cfg := yamlOps.CopyConfContent()
  28. if len(cfg) == 0 {
  29. conf.Log.Warnf("fail to parse yaml for source %s. Return an empty configuration", sourceType)
  30. } else {
  31. def, ok := cfg["default"]
  32. if !ok {
  33. conf.Log.Warnf("default config_key not found")
  34. } else {
  35. props = def
  36. }
  37. // config keys in etc folder will transform to lowercase
  38. // while those in data will not
  39. if c, ok := cfg[strings.ToLower(confkey)]; ok {
  40. for k, v := range c {
  41. props[k] = v
  42. }
  43. } else if c, ok := cfg[confkey]; ok {
  44. for k, v := range c {
  45. props[k] = v
  46. }
  47. } else {
  48. conf.Log.Warnf("fail to find config key %s for source %s", confkey, sourceType)
  49. }
  50. }
  51. f := options.FORMAT
  52. if f == "" {
  53. f = "json"
  54. }
  55. props["format"] = strings.ToLower(f)
  56. props["key"] = options.KEY
  57. conf.Log.Debugf("get conf for %s with conf key %s: %v", sourceType, confkey, printable(props))
  58. return props
  59. }
  60. func printable(m map[string]interface{}) map[string]interface{} {
  61. printableMap := make(map[string]interface{})
  62. for k, v := range m {
  63. if strings.EqualFold(k, "password") {
  64. printableMap[k] = "*"
  65. } else {
  66. if vm, ok := v.(map[string]interface{}); ok {
  67. printableMap[k] = printable(vm)
  68. } else {
  69. printableMap[k] = v
  70. }
  71. }
  72. }
  73. return printableMap
  74. }