-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathconfiguration.go
More file actions
306 lines (267 loc) · 8.56 KB
/
configuration.go
File metadata and controls
306 lines (267 loc) · 8.56 KB
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
package modules
import (
"fmt"
"net"
"os"
"strings"
"time"
"github.com/utmstack/UTMStack/agent/config"
"github.com/utmstack/UTMStack/agent/utils"
)
type Port struct {
IsListen bool `json:"enabled"`
Port string `json:"value"`
TLSEnabled bool `json:"tls_enabled,omitempty"`
}
type Integration struct {
TCP Port `json:"tcp_port,omitempty"`
UDP Port `json:"udp_port,omitempty"`
}
type CollectorConfiguration struct {
Integrations map[string]Integration `json:"integrations"`
}
func ReadCollectorConfig() (CollectorConfiguration, error) {
cnf := CollectorConfiguration{}
if !utils.CheckIfPathExist(config.CollectorFileName) {
err := utils.ReadJson(config.CollectorFileNameOld, &cnf)
if err != nil {
return CollectorConfiguration{}, err
}
err = WriteCollectorConfig(cnf.Integrations, config.CollectorFileName)
if err != nil {
return CollectorConfiguration{}, err
}
os.Remove(config.CollectorFileNameOld)
} else {
err := utils.ReadJson(config.CollectorFileName, &cnf)
if err != nil {
return cnf, err
}
}
return cnf, nil
}
func ConfigureCollectorFirstTime() error {
integrations := make(map[string]Integration)
for logTyp, ports := range config.ProtoPorts {
newIntegration := Integration{}
newIntegration.TCP.IsListen = false
newIntegration.TCP.Port = ports.TCP
newIntegration.UDP.IsListen = false
newIntegration.UDP.Port = ports.UDP
integrations[string(logTyp)] = newIntegration
}
return WriteCollectorConfig(integrations, config.CollectorFileName)
}
func ChangeIntegrationStatus(logTyp string, proto string, isEnabled bool, tlsOptions ...bool) (string, error) {
var port string
cnf, err := ReadCollectorConfig()
if err != nil {
return "", fmt.Errorf("error reading collector config: %v", err)
}
if proto != "tcp" && proto != "udp" {
return "", fmt.Errorf("invalid protocol: %s", proto)
}
if valid := config.ValidateModuleType(logTyp); valid == "nil" {
return "", fmt.Errorf("invalid integration: %s", logTyp)
}
integration := cnf.Integrations[logTyp]
switch proto {
case "tcp":
integration.TCP.IsListen = isEnabled
port = integration.TCP.Port
// Handle TLS configuration if specified
if len(tlsOptions) > 0 && isEnabled {
if tlsOptions[0] {
if !utils.CheckIfPathExist(config.IntegrationCertPath) || !utils.CheckIfPathExist(config.IntegrationKeyPath) {
return "", fmt.Errorf("TLS certificates not found. Please load certificates first")
}
// Enable TLS
integration.TCP.TLSEnabled = true
mod := GetModule(logTyp)
if mod != nil && mod.IsPortListen(proto) {
mod.DisablePort(proto)
time.Sleep(100 * time.Millisecond)
err := mod.EnablePort(proto, true)
if err != nil {
return "", fmt.Errorf("error enabling TLS on running module: %v", err)
}
}
} else {
// Disable TLS
integration.TCP.TLSEnabled = false
mod := GetModule(logTyp)
if mod != nil && mod.IsPortListen(proto) {
mod.DisablePort(proto)
time.Sleep(100 * time.Millisecond)
err := mod.EnablePort(proto, false)
if err != nil {
return "", fmt.Errorf("error disabling TLS on running module: %v", err)
}
}
}
}
// Auto-disable TLS when disabling integration
if !isEnabled {
integration.TCP.TLSEnabled = false
}
case "udp":
integration.UDP.IsListen = isEnabled
port = integration.UDP.Port
// TLS validation for UDP
if len(tlsOptions) > 0 && tlsOptions[0] {
return "", fmt.Errorf("TLS is not supported for UDP protocol. Use TCP for TLS connections")
}
}
cnf.Integrations[logTyp] = integration
return port, WriteCollectorConfig(cnf.Integrations, config.CollectorFileName)
}
func ChangePort(logTyp string, proto string, port string) (string, error) {
var old string
cnf, err := ReadCollectorConfig()
if err != nil {
return "", fmt.Errorf("error reading collector config: %v", err)
}
if proto != "tcp" && proto != "udp" {
return "", fmt.Errorf("invalid protocol: %s", proto)
}
if valid := config.ValidateModuleType(logTyp); valid == "nil" {
return "", fmt.Errorf("invalid integration: %s", logTyp)
}
if changeValid := ValidateChangeInPort(port, logTyp); !changeValid {
return "", fmt.Errorf("change in port %s protocol %s not allowed for %s or out range %s-%s", port, proto, logTyp, config.PortRangeMin, config.PortRangeMax)
}
if !IsPortAvailable(port, proto, &cnf, logTyp) {
return "", fmt.Errorf("port %s is already in use", port)
}
integration := cnf.Integrations[logTyp]
switch proto {
case "tcp":
old = integration.TCP.Port
integration.TCP.Port = port
case "udp":
old = integration.UDP.Port
integration.UDP.Port = port
}
cnf.Integrations[logTyp] = integration
return old, WriteCollectorConfig(cnf.Integrations, config.CollectorFileName)
}
func IsPortAvailable(port string, proto string, cnf *CollectorConfiguration, currentIntegration string) bool {
for integration, integrationConfig := range cnf.Integrations {
if integration != currentIntegration {
if integrationConfig.TCP.Port == port || integrationConfig.UDP.Port == port {
return false
}
}
}
listener, err := net.Listen(proto, ":"+port)
if err != nil {
return false
}
listener.Close()
return true
}
func WriteCollectorConfig(integrations map[string]Integration, filename string) error {
fileContent := "{\n \"integrations\": {\n"
for name, integration := range integrations {
fileContent += fmt.Sprintf(" \"%s\": {\n", name)
if integration.TCP.Port != "" {
fileContent += fmt.Sprintf(" \"tcp_port\": {\"enabled\": %t, \"value\": \"%s\"", integration.TCP.IsListen, integration.TCP.Port)
if integration.TCP.TLSEnabled {
fileContent += fmt.Sprintf(", \"tls_enabled\": %t", integration.TCP.TLSEnabled)
}
fileContent += "},\n"
}
if integration.UDP.Port != "" {
fileContent += fmt.Sprintf(" \"udp_port\": {\"enabled\": %t, \"value\": \"%s\"},\n", integration.UDP.IsListen, integration.UDP.Port)
}
if strings.HasSuffix(fileContent, ",\n") {
fileContent = fileContent[:len(fileContent)-2] + "\n"
}
fileContent += " },\n"
}
if strings.HasSuffix(fileContent, ",\n") {
fileContent = fileContent[:len(fileContent)-2] + "\n"
}
fileContent += " }\n}\n"
err := os.WriteFile(filename, []byte(fileContent), 0644)
if err != nil {
return err
}
return nil
}
func WriteCollectorConfigFromModules(mod []Module, filename string) error {
integrations := make(map[string]Integration)
for _, m := range mod {
integrations[string(m.GetDataType())] = Integration{
TCP: Port{
IsListen: m.IsPortListen("tcp"),
Port: m.GetPort("tcp"),
},
UDP: Port{
IsListen: m.IsPortListen("udp"),
Port: m.GetPort("udp"),
},
}
}
return WriteCollectorConfig(integrations, filename)
}
func EnableTLSForIntegration(logTyp string, proto string) (string, error) {
cnf, err := ReadCollectorConfig()
if err != nil {
return "", fmt.Errorf("error reading collector config: %v", err)
}
if valid := config.ValidateModuleType(logTyp); valid == "nil" {
return "", fmt.Errorf("invalid integration: %s", logTyp)
}
integration := cnf.Integrations[logTyp]
var port string
switch proto {
case "tcp":
if integration.TCP.Port == "" {
return "", fmt.Errorf("TCP port not configured for %s", logTyp)
}
port = integration.TCP.Port
integration.TCP.TLSEnabled = true
mod := GetModule(logTyp)
if mod != nil && mod.IsPortListen(proto) {
mod.DisablePort(proto)
time.Sleep(100 * time.Millisecond)
err := mod.EnablePort(proto, true)
if err != nil {
return port, fmt.Errorf("error enabling TLS on running module: %v", err)
}
}
case "udp":
return "", fmt.Errorf("TLS not supported for UDP protocol")
default:
return "", fmt.Errorf("invalid protocol: %s", proto)
}
cnf.Integrations[logTyp] = integration
return port, WriteCollectorConfig(cnf.Integrations, config.CollectorFileName)
}
func DisableTLSForIntegration(logTyp string, proto string) error {
cnf, err := ReadCollectorConfig()
if err != nil {
return fmt.Errorf("error reading collector config: %v", err)
}
integration := cnf.Integrations[logTyp]
switch proto {
case "tcp":
integration.TCP.TLSEnabled = false
mod := GetModule(logTyp)
if mod != nil && mod.IsPortListen(proto) {
mod.DisablePort(proto)
time.Sleep(100 * time.Millisecond)
err := mod.EnablePort(proto, false)
if err != nil {
return fmt.Errorf("error disabling TLS on running module: %v", err)
}
}
case "udp":
return fmt.Errorf("TLS not supported for UDP protocol")
default:
return fmt.Errorf("invalid protocol: %s", proto)
}
cnf.Integrations[logTyp] = integration
return WriteCollectorConfig(cnf.Integrations, config.CollectorFileName)
}