-
Notifications
You must be signed in to change notification settings - Fork 56
/
backend.go
187 lines (168 loc) · 5.93 KB
/
backend.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
// Copyright (C) 2020 Graylog, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the Server Side Public License, version 1,
// as published by MongoDB, Inc.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// Server Side Public License for more details.
//
// You should have received a copy of the Server Side Public License
// along with this program. If not, see
// <http://www.mongodb.com/licensing/server-side-public-license>.
package backends
import (
"bytes"
"fmt"
"github.com/Graylog2/collector-sidecar/helpers"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"time"
"github.com/flynn-archive/go-shlex"
"github.com/Graylog2/collector-sidecar/api/graylog"
"github.com/Graylog2/collector-sidecar/context"
"github.com/Graylog2/collector-sidecar/system"
)
type Backend struct {
Enabled *bool
Id string
ConfigId string
CollectorId string
Name string
ServiceType string
OperatingSystem string
ExecutablePath string
ConfigurationPath string
ExecuteParameters string
ValidationParameters string
Template string
backendStatus system.VerboseStatus
}
func BackendFromResponse(response graylog.ResponseCollectorBackend, configId string, ctx *context.Ctx) *Backend {
return &Backend{
Enabled: helpers.NewTrue(),
Id: response.Id + "-" + configId,
CollectorId: response.Id,
ConfigId: configId,
Name: response.Name + "-" + configId,
ServiceType: response.ServiceType,
OperatingSystem: response.OperatingSystem,
ExecutablePath: response.ExecutablePath,
ConfigurationPath: BuildConfigurationPath(response, configId, ctx),
ExecuteParameters: response.ExecuteParameters,
ValidationParameters: response.ValidationParameters,
backendStatus: system.VerboseStatus{},
}
}
func BuildConfigurationPath(response graylog.ResponseCollectorBackend, configId string, ctx *context.Ctx) string {
return filepath.Join(ctx.UserConfig.CollectorConfigurationDirectory, configId, response.Name+".conf")
}
func (b *Backend) Equals(a *Backend) bool {
return reflect.DeepEqual(a, b)
}
func (b *Backend) EqualSettings(a *Backend) bool {
executeParameters, _ := helpers.Sprintf(
a.ExecuteParameters,
a.ConfigurationPath)
validationParameters, _ := helpers.Sprintf(
a.ValidationParameters,
a.ConfigurationPath)
aBackend := &Backend{
Enabled: b.Enabled,
Id: a.Id,
ConfigId: a.ConfigId,
CollectorId: a.CollectorId,
Name: a.Name,
ServiceType: a.ServiceType,
OperatingSystem: a.OperatingSystem,
ExecutablePath: a.ExecutablePath,
ConfigurationPath: a.ConfigurationPath,
ExecuteParameters: executeParameters,
ValidationParameters: validationParameters,
Template: b.Template,
backendStatus: b.Status(),
}
return b.Equals(aBackend)
}
func (b *Backend) CheckExecutableAgainstAccesslist(context *context.Ctx) error {
if len(context.UserConfig.CollectorBinariesAccesslist) <= 0 {
return nil
}
isListed, err := helpers.PathMatch(b.ExecutablePath, context.UserConfig.CollectorBinariesAccesslist)
if err != nil {
return fmt.Errorf("Can not validate binary path: %s", err)
}
if !isListed.Match {
if isListed.IsLink {
msg := "Couldn't execute collector %s [%s], binary path is not included in `collector_binaries_accesslist' config option."
return fmt.Errorf(msg, isListed.Path, b.ExecutablePath)
} else {
msg := "Couldn't execute collector %s, binary path is not included in `collector_binaries_accesslist' config option."
return fmt.Errorf(msg, isListed.Path)
}
}
return nil
}
func (b *Backend) CheckConfigPathAgainstAccesslist(context *context.Ctx) bool {
configuration, err := helpers.PathMatch(b.ConfigurationPath, context.UserConfig.CollectorBinariesAccesslist)
if err != nil {
log.Errorf("Can not validate configuration path: %s", err)
return false
}
if configuration.Match {
b.SetStatusLogErrorf("Collector configuration %s is in executable path, exclude it from `collector_binaries_accesslist' config option.", b.ConfigurationPath)
return false
}
return true
}
func (b *Backend) ValidateConfigurationFile(context *context.Ctx) (error, string) {
if b.ValidationParameters == "" {
log.Warnf("[%s] Skipping configuration test. No validation command configured.", b.Name)
return nil, ""
}
if err := b.CheckExecutableAgainstAccesslist(context); err != nil {
return err, ""
}
var err error
var quotedArgs []string
if runtime.GOOS == "windows" {
quotedArgs = helpers.CommandLineToArgv(b.ValidationParameters)
} else {
quotedArgs, err = shlex.Split(b.ValidationParameters)
}
if err != nil {
err = fmt.Errorf("Error during configuration validation: %s", err)
return err, ""
}
cmd := exec.Command(b.ExecutablePath, quotedArgs...)
var combinedOutputBuffer bytes.Buffer
cmd.Stdout = &combinedOutputBuffer
cmd.Stderr = &combinedOutputBuffer
if err := cmd.Start(); err != nil {
err = fmt.Errorf("Couldn't start validation command: %s", err)
return err, string(combinedOutputBuffer.Bytes())
}
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(context.UserConfig.CollectorValidationTimeout):
if err := cmd.Process.Kill(); err != nil {
err = fmt.Errorf("Failed to kill validation process: %s", err)
return err, ""
}
return fmt.Errorf("Unable to validate configuration, timeout <%v> reached", context.UserConfig.CollectorValidationTimeout), ""
case err := <-done:
if err != nil {
close(done)
return fmt.Errorf("Collector configuration file is not valid, waiting for the next update."),
string(combinedOutputBuffer.Bytes())
}
return nil, ""
}
}