-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
expand.go
114 lines (103 loc) · 3.35 KB
/
expand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package expandconverter // import "go.opentelemetry.io/collector/confmap/converter/expandconverter"
import (
"context"
"fmt"
"os"
"regexp"
"go.uber.org/zap"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/internal/envvar"
)
type converter struct {
logger *zap.Logger
// Record of which env vars we have logged a warning for
loggedDeprecations map[string]struct{}
}
// NewFactory returns a factory for a confmap.Converter,
// which expands all environment variables for a given confmap.Conf.
func NewFactory() confmap.ConverterFactory {
return confmap.NewConverterFactory(newConverter)
}
func newConverter(set confmap.ConverterSettings) confmap.Converter {
return converter{
loggedDeprecations: make(map[string]struct{}),
logger: set.Logger,
}
}
func (c converter) Convert(_ context.Context, conf *confmap.Conf) error {
var err error
out := make(map[string]any)
for _, k := range conf.AllKeys() {
out[k], err = c.expandStringValues(conf.Get(k))
if err != nil {
return err
}
}
return conf.Merge(confmap.NewFromStringMap(out))
}
func (c converter) expandStringValues(value any) (any, error) {
var err error
switch v := value.(type) {
case string:
return c.expandEnv(v)
case []any:
nslice := make([]any, 0, len(v))
for _, vint := range v {
var nv any
nv, err = c.expandStringValues(vint)
if err != nil {
return nil, err
}
nslice = append(nslice, nv)
}
return nslice, nil
case map[string]any:
nmap := map[string]any{}
for mk, mv := range v {
nmap[mk], err = c.expandStringValues(mv)
if err != nil {
return nil, err
}
}
return nmap, nil
default:
return v, nil
}
}
func (c converter) expandEnv(s string) (string, error) {
var err error
res := os.Expand(s, func(str string) string {
// Matches on $VAR style environment variables
// in order to make sure we don't log a warning for ${VAR}
var regex = regexp.MustCompile(fmt.Sprintf(`\$%s`, regexp.QuoteMeta(str)))
if _, exists := c.loggedDeprecations[str]; !exists && regex.MatchString(s) {
msg := fmt.Sprintf("Variable substitution using $VAR will be deprecated in favor of ${VAR} and ${env:VAR}, please update $%s", str)
c.logger.Warn(msg, zap.String("variable", str))
c.loggedDeprecations[str] = struct{}{}
}
// This allows escaping environment variable substitution via $$, e.g.
// - $FOO will be substituted with env var FOO
// - $$FOO will be replaced with $FOO
// - $$$FOO will be replaced with $ + substituted env var FOO
if str == "$" {
return "$"
}
// For $ENV style environment variables os.Expand returns once it hits a character that isn't an underscore or
// an alphanumeric character - so we cannot detect those malformed environment variables.
// For ${ENV} style variables we can detect those kinds of env var names!
if !envvar.ValidationRegexp.MatchString(str) {
err = fmt.Errorf("environment variable %q has invalid name: must match regex %s", str, envvar.ValidationPattern)
return ""
}
val, exists := os.LookupEnv(str)
if !exists {
c.logger.Warn("Configuration references unset environment variable", zap.String("name", str))
} else if len(val) == 0 {
c.logger.Info("Configuration references empty environment variable", zap.String("name", str))
}
return val
})
return res, err
}