Skip to content

Commit fbe3a81

Browse files
blink-so[bot]f0ssel
andcommitted
fix: use original user's home directory for CA certificate storage
When running under sudo, the CA certificate was being stored in root's home directory but the subprocess (running as original user) couldn't access it, causing certificate verification errors. Now GetConfigDir() detects sudo execution and uses the original user's home directory, ensuring the subprocess can access the CA certificate. Fixes: curl: (77) error setting certificate verify locations Co-authored-by: f0ssel <[email protected]>
1 parent 4d50f36 commit fbe3a81

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

tls/tls.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"math/big"
1313
"net"
1414
"os"
15+
"os/user"
1516
"path/filepath"
1617
"sync"
1718
"time"
@@ -295,9 +296,28 @@ func (cm *CertificateManager) generateServerCertificate(hostname string) (*tls.C
295296

296297
// GetConfigDir returns the configuration directory path
297298
func GetConfigDir() (string, error) {
298-
homeDir, err := os.UserHomeDir()
299-
if err != nil {
300-
return "", fmt.Errorf("failed to get user home directory: %v", err)
299+
// When running under sudo, use the original user's home directory
300+
// so the subprocess can access the CA certificate files
301+
var homeDir string
302+
if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" {
303+
// Get original user's home directory
304+
if user, err := user.Lookup(sudoUser); err == nil {
305+
homeDir = user.HomeDir
306+
} else {
307+
// Fallback to current user if lookup fails
308+
var err2 error
309+
homeDir, err2 = os.UserHomeDir()
310+
if err2 != nil {
311+
return "", fmt.Errorf("failed to get user home directory: %v", err2)
312+
}
313+
}
314+
} else {
315+
// Normal case - use current user's home
316+
var err error
317+
homeDir, err = os.UserHomeDir()
318+
if err != nil {
319+
return "", fmt.Errorf("failed to get user home directory: %v", err)
320+
}
301321
}
302322

303323
// Use platform-specific config directory

0 commit comments

Comments
 (0)