@@ -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,52 @@ 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+ if ! utils .CheckIfPathExist (config .IntegrationCertPath ) || ! utils .CheckIfPathExist (config .IntegrationKeyPath ) {
88+ return "" , fmt .Errorf ("TLS certificates not found. Please load certificates first" )
89+ }
90+ // Enable TLS
91+ integration .TCP .TLSEnabled = true
92+ mod := GetModule (logTyp )
93+ if mod != nil && mod .IsPortListen (proto ) {
94+ mod .DisablePort (proto )
95+ time .Sleep (100 * time .Millisecond )
96+ err := mod .EnablePort (proto , true )
97+ if err != nil {
98+ return "" , fmt .Errorf ("error enabling TLS on running module: %v" , err )
99+ }
100+ }
101+ } else {
102+ // Disable TLS
103+ integration .TCP .TLSEnabled = false
104+ mod := GetModule (logTyp )
105+ if mod != nil && mod .IsPortListen (proto ) {
106+ mod .DisablePort (proto )
107+ time .Sleep (100 * time .Millisecond )
108+ err := mod .EnablePort (proto , false )
109+ if err != nil {
110+ return "" , fmt .Errorf ("error disabling TLS on running module: %v" , err )
111+ }
112+ }
113+ }
114+ }
115+
116+ // Auto-disable TLS when disabling integration
117+ if ! isEnabled {
118+ integration .TCP .TLSEnabled = false
119+ }
120+
81121 case "udp" :
82122 integration .UDP .IsListen = isEnabled
83123 port = integration .UDP .Port
124+
125+ // TLS validation for UDP
126+ if len (tlsOptions ) > 0 && tlsOptions [0 ] {
127+ return "" , fmt .Errorf ("TLS is not supported for UDP protocol. Use TCP for TLS connections" )
128+ }
84129 }
85130
86131 cnf .Integrations [logTyp ] = integration
@@ -145,7 +190,11 @@ func WriteCollectorConfig(integrations map[string]Integration, filename string)
145190 for name , integration := range integrations {
146191 fileContent += fmt .Sprintf (" \" %s\" : {\n " , name )
147192 if integration .TCP .Port != "" {
148- fileContent += fmt .Sprintf (" \" tcp_port\" : {\" enabled\" : %t, \" value\" : \" %s\" },\n " , integration .TCP .IsListen , integration .TCP .Port )
193+ fileContent += fmt .Sprintf (" \" tcp_port\" : {\" enabled\" : %t, \" value\" : \" %s\" " , integration .TCP .IsListen , integration .TCP .Port )
194+ if integration .TCP .TLSEnabled {
195+ fileContent += fmt .Sprintf (", \" tls_enabled\" : %t" , integration .TCP .TLSEnabled )
196+ }
197+ fileContent += "},\n "
149198 }
150199 if integration .UDP .Port != "" {
151200 fileContent += fmt .Sprintf (" \" udp_port\" : {\" enabled\" : %t, \" value\" : \" %s\" },\n " , integration .UDP .IsListen , integration .UDP .Port )
@@ -184,3 +233,74 @@ func WriteCollectorConfigFromModules(mod []Module, filename string) error {
184233 }
185234 return WriteCollectorConfig (integrations , filename )
186235}
236+
237+ func EnableTLSForIntegration (logTyp string , proto string ) (string , error ) {
238+ cnf , err := ReadCollectorConfig ()
239+ if err != nil {
240+ return "" , fmt .Errorf ("error reading collector config: %v" , err )
241+ }
242+
243+ if valid := config .ValidateModuleType (logTyp ); valid == "nil" {
244+ return "" , fmt .Errorf ("invalid integration: %s" , logTyp )
245+ }
246+
247+ integration := cnf .Integrations [logTyp ]
248+ var port string
249+
250+ switch proto {
251+ case "tcp" :
252+ if integration .TCP .Port == "" {
253+ return "" , fmt .Errorf ("TCP port not configured for %s" , logTyp )
254+ }
255+
256+ port = integration .TCP .Port
257+ integration .TCP .TLSEnabled = true
258+
259+ mod := GetModule (logTyp )
260+ if mod != nil && mod .IsPortListen (proto ) {
261+ mod .DisablePort (proto )
262+ time .Sleep (100 * time .Millisecond )
263+ err := mod .EnablePort (proto , true )
264+ if err != nil {
265+ return port , fmt .Errorf ("error enabling TLS on running module: %v" , err )
266+ }
267+ }
268+ case "udp" :
269+ return "" , fmt .Errorf ("TLS not supported for UDP protocol" )
270+ default :
271+ return "" , fmt .Errorf ("invalid protocol: %s" , proto )
272+ }
273+
274+ cnf .Integrations [logTyp ] = integration
275+ return port , WriteCollectorConfig (cnf .Integrations , config .CollectorFileName )
276+ }
277+
278+ func DisableTLSForIntegration (logTyp string , proto string ) error {
279+ cnf , err := ReadCollectorConfig ()
280+ if err != nil {
281+ return fmt .Errorf ("error reading collector config: %v" , err )
282+ }
283+
284+ integration := cnf .Integrations [logTyp ]
285+ switch proto {
286+ case "tcp" :
287+ integration .TCP .TLSEnabled = false
288+
289+ mod := GetModule (logTyp )
290+ if mod != nil && mod .IsPortListen (proto ) {
291+ mod .DisablePort (proto )
292+ time .Sleep (100 * time .Millisecond )
293+ err := mod .EnablePort (proto , false )
294+ if err != nil {
295+ return fmt .Errorf ("error disabling TLS on running module: %v" , err )
296+ }
297+ }
298+ case "udp" :
299+ return fmt .Errorf ("TLS not supported for UDP protocol" )
300+ default :
301+ return fmt .Errorf ("invalid protocol: %s" , proto )
302+ }
303+
304+ cnf .Integrations [logTyp ] = integration
305+ return WriteCollectorConfig (cnf .Integrations , config .CollectorFileName )
306+ }
0 commit comments