Skip to content

Commit 9d2fbea

Browse files
blink-so[bot]f0ssel
andcommitted
Move TLS setup logic to CertificateManager method
- Add SetupTLSAndWriteCACert method to CertificateManager - Combines getting TLS config, CA cert PEM, and writing CA cert to file - Returns TLS config, CA cert path, and CA cert PEM in one call - Update CLI to use the new method instead of separate calls - Reduces complexity in CLI Run function - Better encapsulation of TLS-related setup logic - Remove unused filepath import from CLI - Clean separation between TLS setup and CLI orchestration Co-authored-by: f0ssel <[email protected]>
1 parent d487044 commit 9d2fbea

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

cli/cli.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"log/slog"
88
"os"
99
"os/signal"
10-
"path/filepath"
1110
"strings"
1211
"syscall"
1312

@@ -155,21 +154,13 @@ func Run(config Config, args []string) error {
155154
logger.Error("Failed to create certificate manager", "error", err)
156155
return fmt.Errorf("failed to create certificate manager: %v", err)
157156
}
158-
tlsConfig = certManager.GetTLSConfig()
159157

160-
// Get CA certificate for environment
161-
caCertPEM, err := certManager.GetCACertPEM()
158+
// Setup TLS config and write CA certificate to file
159+
var caCertPath string
160+
tlsConfig, caCertPath, _, err = certManager.SetupTLSAndWriteCACert()
162161
if err != nil {
163-
logger.Error("Failed to get CA certificate", "error", err)
164-
return fmt.Errorf("failed to get CA certificate: %v", err)
165-
}
166-
167-
// Write CA certificate to a temporary file for tools that need a file path
168-
caCertPath := filepath.Join(configDir, "ca-cert.pem")
169-
err = os.WriteFile(caCertPath, caCertPEM, 0644)
170-
if err != nil {
171-
logger.Error("Failed to write CA certificate file", "error", err)
172-
return fmt.Errorf("failed to write CA certificate file: %v", err)
162+
logger.Error("Failed to setup TLS and CA certificate", "error", err)
163+
return fmt.Errorf("failed to setup TLS and CA certificate: %v", err)
173164
}
174165

175166
// Set standard CA certificate environment variables for common tools

tls/tls.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ func (cm *CertificateManager) GetCACertPEM() ([]byte, error) {
6262
}), nil
6363
}
6464

65+
// SetupTLSAndWriteCACert sets up TLS config and writes CA certificate to file
66+
// Returns the TLS config, CA cert path, and CA cert PEM data
67+
func (cm *CertificateManager) SetupTLSAndWriteCACert() (*tls.Config, string, []byte, error) {
68+
// Get TLS config
69+
tlsConfig := cm.GetTLSConfig()
70+
71+
// Get CA certificate PEM
72+
caCertPEM, err := cm.GetCACertPEM()
73+
if err != nil {
74+
return nil, "", nil, fmt.Errorf("failed to get CA certificate: %v", err)
75+
}
76+
77+
// Write CA certificate to file
78+
caCertPath := filepath.Join(cm.configDir, "ca-cert.pem")
79+
err = os.WriteFile(caCertPath, caCertPEM, 0644)
80+
if err != nil {
81+
return nil, "", nil, fmt.Errorf("failed to write CA certificate file: %v", err)
82+
}
83+
84+
return tlsConfig, caCertPath, caCertPEM, nil
85+
}
86+
6587
// loadOrGenerateCA loads existing CA or generates a new one
6688
func (cm *CertificateManager) loadOrGenerateCA() error {
6789
caKeyPath := filepath.Join(cm.configDir, "ca-key.pem")

0 commit comments

Comments
 (0)