-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
307 lines (273 loc) · 6.39 KB
/
config.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"fmt"
"net/url"
"strings"
"text/template"
log "github.com/Sirupsen/logrus"
"github.com/go-ini/ini"
)
const (
minUploadDelay = 200
mainSectionName = "main"
logOutputKey = "log_output"
logLevelKey = "log_level"
sourceKey = "source"
groupKey = "group"
streamKey = "stream"
cloudwatchFormatKey = "cloudwatch_format"
syslogFormatKey = "syslog_format"
queueSizeKey = "queue_size"
uploadDelayKey = "upload_delay"
debugLevelOption = "debug"
infoLevelOption = "info"
errorLevelOption = "error"
syslogOutputOption = "syslog"
nullOutputOption = "null"
stdoutOutputOption = "stdout"
stderrOutputOption = "stderr"
)
type (
logoutput uint8
upload_delay uint16
queue_size uint16
)
type Configuration interface {
GetMain() *MainCfg
GetFlows() []*FlowCfg
Validate() error
}
type MainCfg struct {
LogLevel string `ini:"log_level"`
LogOutput string `ini:"log_output"`
}
type FlowCfg struct {
Group string `ini:"group"`
Stream string `ini:"stream"`
SyslogFormat string `ini:"syslog_format"`
CloudwatchFormat string `ini:"cloudwatch_format"`
Source string `ini:"source"`
UploadDelay upload_delay `ini:"upload_delay"`
QueueSize queue_size `ini:"queue_size"`
}
const (
stdErr logoutput = iota
stdOut
sysLog
null
)
var strToOutput = map[string]logoutput{
syslogOutputOption: sysLog,
nullOutputOption: null,
stdoutOutputOption: stdOut,
stderrOutputOption: stdErr,
}
var validOutputOptions = []string{
syslogOutputOption,
nullOutputOption,
stdoutOutputOption,
stderrOutputOption,
}
var strToLevel = map[string]log.Level{
debugLevelOption: log.DebugLevel,
infoLevelOption: log.InfoLevel,
errorLevelOption: log.ErrorLevel,
}
var validLevelOptions = []string{
debugLevelOption,
infoLevelOption,
errorLevelOption,
}
type IniConfig struct {
config *ini.File
}
func NewIniConfig(file string) Configuration {
config, err := ini.Load(file)
if err != nil {
log.Fatalf("could not read config file %s", err)
}
// Remove unused default section
config.DeleteSection(ini.DEFAULT_SECTION)
return &IniConfig{config: config}
}
func (cfg IniConfig) GetMain() *MainCfg {
main := new(MainCfg)
// Set default values
main.LogLevel = "error"
main.LogOutput = "syslog"
err := cfg.config.Section(mainSectionName).MapTo(main)
if err != nil {
log.Fatalf("could not map section %s: %s", mainSectionName, err)
}
return main
}
// Return all flow configurations
func (cfg IniConfig) GetFlows() (flows []*FlowCfg) {
for _, section := range cfg.config.Sections() {
if section.Name() != mainSectionName {
flow := new(FlowCfg)
// Set default values
flow.UploadDelay = minUploadDelay
flow.QueueSize = 50000
err := section.MapTo(flow)
if err != nil {
log.Fatalf("could not map section %s: %s", mainSectionName, err)
}
flows = append(flows, flow)
}
}
return
}
func (cfg IniConfig) Validate() error {
if err := validateMainCfg(cfg.GetMain()); err != nil {
return fmt.Errorf("error while validating main section: %s", err)
}
for _, flow := range cfg.GetFlows() {
if err := validateFlowCfg(flow); err != nil {
return fmt.Errorf("error while validating flow section: %s", err)
}
}
return nil
}
func validateMainCfg(cfg *MainCfg) error {
if err := validateLogLevel(cfg.LogLevel); err != nil {
return fmt.Errorf("log_level %s", err)
}
if err := validateLogOutput(cfg.LogOutput); err != nil {
return fmt.Errorf("log_output %s", err)
}
return nil
}
func validateFlowCfg(cfg *FlowCfg) error {
if err := validateQueueSize(cfg.QueueSize); err != nil {
return err
}
if err := validateGroup(cfg.Group); err != nil {
return err
}
if err := validateStrean(cfg.Stream); err != nil {
return err
}
if err := validateUploadDelay(cfg.UploadDelay); err != nil {
return err
}
if err := validateSource(cfg.Source); err != nil {
return err
}
if err := validateCloudwatchFormat(cfg.CloudwatchFormat); err != nil {
return err
}
if err := validateSyslogFormat(cfg.SyslogFormat); err != nil {
return err
}
return nil
}
// Validate source URL
func validateSource(value string) error {
if value == "" {
return errEmptyValue
}
uri, err := url.Parse(value)
if err != nil {
return err
}
// Valid schemes
var schemes = map[string]bool{
"udp": true,
}
// Check for valid scheme
if !schemes[uri.Scheme] {
return errInvalidScheme
}
return nil
}
/*
http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_CreateLogGroup.html
Log group names can be between 1 and 512 characters long.
Allowed characters are a-z, A-Z, 0-9, '_' (underscore), '-' (hyphen), '/' (forward slash), and '.' (period).
*/
func validateGroup(value string) error {
if value == "" {
return errEmptyValue
}
if len(value) > 512 {
return errNameTooLong
}
for _, char := range value {
if !strings.Contains("_-/.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", string(char)) {
return errInvalidValue
}
}
return nil
}
/*
http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_CreateLogStream.html
Log stream names can be between 1 and 512 characters long
The ':' colon character is not allowed.
*/
func validateStrean(value string) error {
if value == "" {
return errEmptyValue
}
if len(value) > 512 {
return errNameTooLong
}
if strings.Contains(value, ":") {
return errInvalidValue
}
return nil
}
func validateSyslogFormat(value string) error {
if value == "" {
return errEmptyValue
}
if _, ok := parserFunctions[value]; !ok {
return errInvalidFormat
}
return nil
}
func validateCloudwatchFormat(value string) error {
if value == "" {
return errEmptyValue
}
_, err := template.New("").Parse(value)
if err != nil {
return err
}
return nil
}
func validateQueueSize(value queue_size) error {
return nil
}
func validateUploadDelay(value upload_delay) error {
if value < minUploadDelay {
return errTooSmall
}
return nil
}
func validateLogOutput(value string) error {
if value == "" {
return errEmptyValue
}
if !strIn(validOutputOptions, value) {
return errInvalidValue
}
return nil
}
func validateLogLevel(value string) error {
if value == "" {
return errEmptyValue
}
if !strIn(validLevelOptions, value) {
return errInvalidValue
}
return nil
}
func strIn(haystack []string, needle string) bool {
for _, elem := range haystack {
if elem == needle {
return true
}
}
return false
}