@@ -5,14 +5,16 @@ import (
55 "net"
66 "os"
77 "strings"
8+ "time"
89
910 "github.com/utmstack/UTMStack/agent/config"
1011 "github.com/utmstack/UTMStack/agent/utils"
1112)
1213
1314type Port struct {
14- IsListen bool `json:"enabled"`
15- Port string `json:"value"`
15+ IsListen bool `json:"enabled"`
16+ Port string `json:"value"`
17+ TLSEnabled bool `json:"tls_enabled,omitempty"`
1618}
1719
1820type Integration struct {
@@ -59,7 +61,7 @@ func ConfigureCollectorFirstTime() error {
5961 return WriteCollectorConfig (integrations , config .CollectorFileName )
6062}
6163
62- func ChangeIntegrationStatus (logTyp string , proto string , isEnabled bool ) (string , error ) {
64+ func ChangeIntegrationStatus (logTyp string , proto string , isEnabled bool , tlsOptions ... bool ) (string , error ) {
6365 var port string
6466 cnf , err := ReadCollectorConfig ()
6567 if err != nil {
@@ -78,9 +80,49 @@ func ChangeIntegrationStatus(logTyp string, proto string, isEnabled bool) (strin
7880 case "tcp" :
7981 integration .TCP .IsListen = isEnabled
8082 port = integration .TCP .Port
83+
84+ // Handle TLS configuration if specified
85+ if len (tlsOptions ) > 0 && isEnabled {
86+ if tlsOptions [0 ] {
87+ // Enable TLS
88+ integration .TCP .TLSEnabled = true
89+ mod := GetModule (logTyp )
90+ if mod != nil && mod .IsPortListen (proto ) {
91+ mod .DisablePort (proto )
92+ time .Sleep (100 * time .Millisecond )
93+ err := mod .EnablePort (proto , true )
94+ if err != nil {
95+ return "" , fmt .Errorf ("error enabling TLS on running module: %v" , err )
96+ }
97+ }
98+ } else {
99+ // Disable TLS
100+ integration .TCP .TLSEnabled = false
101+ mod := GetModule (logTyp )
102+ if mod != nil && mod .IsPortListen (proto ) {
103+ mod .DisablePort (proto )
104+ time .Sleep (100 * time .Millisecond )
105+ err := mod .EnablePort (proto , false )
106+ if err != nil {
107+ return "" , fmt .Errorf ("error disabling TLS on running module: %v" , err )
108+ }
109+ }
110+ }
111+ }
112+
113+ // Auto-disable TLS when disabling integration
114+ if ! isEnabled {
115+ integration .TCP .TLSEnabled = false
116+ }
117+
81118 case "udp" :
82119 integration .UDP .IsListen = isEnabled
83120 port = integration .UDP .Port
121+
122+ // TLS validation for UDP
123+ if len (tlsOptions ) > 0 && tlsOptions [0 ] {
124+ return "" , fmt .Errorf ("TLS is not supported for UDP protocol. Use TCP for TLS connections" )
125+ }
84126 }
85127
86128 cnf .Integrations [logTyp ] = integration
@@ -145,7 +187,11 @@ func WriteCollectorConfig(integrations map[string]Integration, filename string)
145187 for name , integration := range integrations {
146188 fileContent += fmt .Sprintf (" \" %s\" : {\n " , name )
147189 if integration .TCP .Port != "" {
148- fileContent += fmt .Sprintf (" \" tcp_port\" : {\" enabled\" : %t, \" value\" : \" %s\" },\n " , integration .TCP .IsListen , integration .TCP .Port )
190+ fileContent += fmt .Sprintf (" \" tcp_port\" : {\" enabled\" : %t, \" value\" : \" %s\" " , integration .TCP .IsListen , integration .TCP .Port )
191+ if integration .TCP .TLSEnabled {
192+ fileContent += fmt .Sprintf (", \" tls_enabled\" : %t" , integration .TCP .TLSEnabled )
193+ }
194+ fileContent += "},\n "
149195 }
150196 if integration .UDP .Port != "" {
151197 fileContent += fmt .Sprintf (" \" udp_port\" : {\" enabled\" : %t, \" value\" : \" %s\" },\n " , integration .UDP .IsListen , integration .UDP .Port )
@@ -184,3 +230,73 @@ func WriteCollectorConfigFromModules(mod []Module, filename string) error {
184230 }
185231 return WriteCollectorConfig (integrations , filename )
186232}
233+
234+ func EnableTLSForIntegration (logTyp string , proto string ) (string , error ) {
235+ cnf , err := ReadCollectorConfig ()
236+ if err != nil {
237+ return "" , fmt .Errorf ("error reading collector config: %v" , err )
238+ }
239+
240+ if valid := config .ValidateModuleType (logTyp ); valid == "nil" {
241+ return "" , fmt .Errorf ("invalid integration: %s" , logTyp )
242+ }
243+
244+ integration := cnf .Integrations [logTyp ]
245+ var port string
246+
247+ switch proto {
248+ case "tcp" :
249+ if integration .TCP .Port == "" {
250+ return "" , fmt .Errorf ("TCP port not configured for %s" , logTyp )
251+ }
252+ port = integration .TCP .Port
253+ integration .TCP .TLSEnabled = true
254+
255+ mod := GetModule (logTyp )
256+ if mod != nil && mod .IsPortListen (proto ) {
257+ mod .DisablePort (proto )
258+ time .Sleep (100 * time .Millisecond )
259+ err := mod .EnablePort (proto , true )
260+ if err != nil {
261+ return port , fmt .Errorf ("error enabling TLS on running module: %v" , err )
262+ }
263+ }
264+ case "udp" :
265+ return "" , fmt .Errorf ("TLS not supported for UDP protocol" )
266+ default :
267+ return "" , fmt .Errorf ("invalid protocol: %s" , proto )
268+ }
269+
270+ cnf .Integrations [logTyp ] = integration
271+ return port , WriteCollectorConfig (cnf .Integrations , config .CollectorFileName )
272+ }
273+
274+ func DisableTLSForIntegration (logTyp string , proto string ) error {
275+ cnf , err := ReadCollectorConfig ()
276+ if err != nil {
277+ return fmt .Errorf ("error reading collector config: %v" , err )
278+ }
279+
280+ integration := cnf .Integrations [logTyp ]
281+ switch proto {
282+ case "tcp" :
283+ integration .TCP .TLSEnabled = false
284+
285+ mod := GetModule (logTyp )
286+ if mod != nil && mod .IsPortListen (proto ) {
287+ mod .DisablePort (proto )
288+ time .Sleep (100 * time .Millisecond )
289+ err := mod .EnablePort (proto , false )
290+ if err != nil {
291+ return fmt .Errorf ("error disabling TLS on running module: %v" , err )
292+ }
293+ }
294+ case "udp" :
295+ return fmt .Errorf ("TLS not supported for UDP protocol" )
296+ default :
297+ return fmt .Errorf ("invalid protocol: %s" , proto )
298+ }
299+
300+ cnf .Integrations [logTyp ] = integration
301+ return WriteCollectorConfig (cnf .Integrations , config .CollectorFileName )
302+ }
0 commit comments