/
configstore.go
148 lines (134 loc) · 3.81 KB
/
configstore.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright 2022 David MacKinnon (blaedd@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package plugin
import (
"regexp"
"strconv"
"strings"
"time"
"unicode"
"unsafe"
"github.com/fluent/fluent-bit-go/output"
"github.com/rs/zerolog"
)
var (
// sn is a Regexp for potentially sensitive fields we shouldn't log
sn = regexp.MustCompile(`(?i:pass|secret|key|hash)`)
// configKeySet is a variable pointing to the function we use to retrieve config keys from the actual
// configuration file. Mainly here so we can easily mock it out for tests.
configKeyGet = output.FLBPluginConfigKey
)
// FLBConfigStore provides access to the fluent-bit configuration for the plugin.
type FLBConfigStore struct {
l *zerolog.Logger
ctx unsafe.Pointer
}
func NewFLBConfigStore(ctx unsafe.Pointer, l *zerolog.Logger) FLBConfigStore {
return FLBConfigStore{
l: l,
ctx: ctx,
}
}
func (f *FLBConfigStore) getKey(name string) string {
return configKeyGet(f.ctx, name)
}
func (f *FLBConfigStore) logGetKey(n string, rv interface{}, ok bool) {
if e := f.l.Debug(); !e.Enabled() {
return
}
var (
evt = zerolog.Dict().Str("name", n).Bool("found", ok)
msg string
)
if ok {
if sn.Match([]byte(n)) {
evt = evt.Str("value", "********")
} else {
switch v := rv.(type) {
case bool:
evt = evt.Bool("value", v)
case int:
evt = evt.Int("value", v)
case string:
evt = evt.Str("value", v)
case []string:
evt = evt.Strs("value", v)
case time.Duration:
evt = evt.Dur("value", v)
default:
evt = evt.Interface("value", rv)
}
}
// Redact sensitive sounding fields.
msg = "found config key"
} else {
msg = "did not find config key"
}
f.l.Debug().Dict("configkey", evt).Msg(msg)
}
// Bool retrieves a boolean from the plugin configuration.
//
// The value and if the value was found are returned.
func (f *FLBConfigStore) Bool(name string) (bool, bool) {
sv := f.getKey(name)
b, err := strconv.ParseBool(sv)
ok := err == nil && sv != ""
f.logGetKey(name, b, ok)
return b, ok
}
// Duration retrieves a [time.Duration] from the plugin configuration.
//
// The value and if the value was found are returned.
func (f *FLBConfigStore) Duration(name string) (time.Duration, bool) {
sv := f.getKey(name)
d, err := time.ParseDuration(sv)
ok := err == nil && sv != ""
f.logGetKey(name, d, ok)
return d, ok
}
// Int retrieves an integer from the plugin configuration.
//
// The value and if the value was found are returned.
func (f *FLBConfigStore) Int(name string) (int, bool) {
sv := f.getKey(name)
i, err := strconv.Atoi(sv)
ok := err == nil && sv != ""
f.logGetKey(name, i, ok)
return i, ok
}
// String retrieves a string from the plugin configuration.
//
// The value and if the value was found are returned.
func (f *FLBConfigStore) String(name string) (string, bool) {
sv := f.getKey(name)
ok := sv != ""
f.logGetKey(name, sv, ok)
return sv, ok
}
// Strings retrieves a list of strings from the plugin configuration.
//
// These should be comma seperated in the configuration file. The value and if the value was found are returned.
func (f *FLBConfigStore) Strings(name string) ([]string, bool) {
sv := f.getKey(name)
spt := func(c rune) bool {
return unicode.IsSpace(c) || c == ','
}
ss := strings.FieldsFunc(sv, spt)
ok := len(ss) > 0
f.logGetKey(name, ss, ok)
return ss, ok
}