Skip to content

Commit f9aff32

Browse files
blink-so[bot]f0ssel
andcommitted
Add automatic CA certificate trust setup
Adds InstallCACertificate() method that: - Installs CA cert to /usr/local/share/ca-certificates/ (Linux) - Runs update-ca-certificates for system-wide trust - Sets environment variables for tool-specific trust: - SSL_CERT_FILE, REQUESTS_CA_BUNDLE, CURL_CA_BUNDLE, NODE_EXTRA_CA_CERTS Gracefully handles permission errors and continues operation. Tools inside jail will now automatically trust proxy certificates. Co-authored-by: f0ssel <19379394+f0ssel@users.noreply.github.com>
1 parent a78d858 commit f9aff32

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ func runBoundary(inv *serpent.Invocation) error {
154154
return fmt.Errorf("failed to create certificate manager: %v", err)
155155
}
156156

157+
// Automatically install CA certificate for system and tool trust
158+
if err := certManager.InstallCACertificate(); err != nil {
159+
logger.Warn("Failed to install CA certificate, manual setup may be required", "error", err)
160+
}
161+
157162
tlsConfig = certManager.GetTLSConfig()
158163

159164
// Get CA certificate for environment

tls/tls.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"math/big"
1313
"net"
1414
"os"
15+
"os/exec"
1516
"path/filepath"
1617
"sync"
1718
"time"
@@ -293,6 +294,69 @@ func (cm *CertificateManager) generateServerCertificate(hostname string) (*tls.C
293294
return tlsCert, nil
294295
}
295296

297+
// InstallCACertificate installs the CA certificate into the system trust store
298+
func (cm *CertificateManager) InstallCACertificate() error {
299+
// Get CA certificate in PEM format
300+
caCertPEM, err := cm.GetCACertPEM()
301+
if err != nil {
302+
return fmt.Errorf("failed to get CA certificate: %v", err)
303+
}
304+
305+
// Install system-wide certificate (Linux)
306+
if err := cm.installSystemCertificate(caCertPEM); err != nil {
307+
cm.logger.Warn("Failed to install system certificate, continuing anyway", "error", err)
308+
}
309+
310+
// Set up environment variables for tool-specific trust
311+
if err := cm.setupEnvironmentVariables(); err != nil {
312+
cm.logger.Warn("Failed to setup environment variables", "error", err)
313+
}
314+
315+
cm.logger.Info("CA certificate trust setup completed")
316+
return nil
317+
}
318+
319+
// installSystemCertificate installs the CA certificate system-wide on Linux
320+
func (cm *CertificateManager) installSystemCertificate(caCertPEM []byte) error {
321+
// Write certificate to system certificate directory
322+
certPath := "/usr/local/share/ca-certificates/boundary-ca.crt"
323+
if err := os.WriteFile(certPath, caCertPEM, 0644); err != nil {
324+
return fmt.Errorf("failed to write certificate to %s: %v", certPath, err)
325+
}
326+
327+
// Update system certificate store
328+
cmd := exec.Command("update-ca-certificates")
329+
if output, err := cmd.CombinedOutput(); err != nil {
330+
return fmt.Errorf("failed to update ca certificates: %v, output: %s", err, output)
331+
}
332+
333+
cm.logger.Info("System CA certificate installed", "path", certPath)
334+
return nil
335+
}
336+
337+
// setupEnvironmentVariables sets up environment variables for tool-specific certificate trust
338+
func (cm *CertificateManager) setupEnvironmentVariables() error {
339+
caCertPath := filepath.Join(cm.configDir, "ca-cert.pem")
340+
341+
// Set environment variables for various tools
342+
envVars := map[string]string{
343+
"SSL_CERT_FILE": caCertPath,
344+
"REQUESTS_CA_BUNDLE": caCertPath,
345+
"CURL_CA_BUNDLE": caCertPath,
346+
"NODE_EXTRA_CA_CERTS": caCertPath,
347+
}
348+
349+
for key, value := range envVars {
350+
if err := os.Setenv(key, value); err != nil {
351+
cm.logger.Warn("Failed to set environment variable", "key", key, "error", err)
352+
} else {
353+
cm.logger.Debug("Set environment variable", "key", key, "value", value)
354+
}
355+
}
356+
357+
return nil
358+
}
359+
296360
// GetConfigDir returns the configuration directory path
297361
func GetConfigDir() (string, error) {
298362
homeDir, err := os.UserHomeDir()

0 commit comments

Comments
 (0)