Skip to content

Commit 4e9ba40

Browse files
blink-so[bot]f0ssel
andcommitted
fix: ensure proper directory ownership for CA certificate storage
When running under sudo, the config directory was created by root but the subprocess runs as the original user, causing permission issues. Now the directory ownership is changed to the original user after creation, ensuring the subprocess can access certificate files. Co-authored-by: f0ssel <[email protected]>
1 parent fbe3a81 commit 4e9ba40

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

tls/tls.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os"
1515
"os/user"
1616
"path/filepath"
17+
"strconv"
1718
"sync"
1819
"time"
1920
)
@@ -142,6 +143,22 @@ func (cm *CertificateManager) generateCA(keyPath, certPath string) error {
142143
return fmt.Errorf("failed to create config directory: %v", err)
143144
}
144145

146+
// When running under sudo, ensure the directory is owned by the original user
147+
if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" {
148+
if sudoUID := os.Getenv("SUDO_UID"); sudoUID != "" {
149+
if sudoGID := os.Getenv("SUDO_GID"); sudoGID != "" {
150+
uid, err1 := strconv.Atoi(sudoUID)
151+
gid, err2 := strconv.Atoi(sudoGID)
152+
if err1 == nil && err2 == nil {
153+
// Change ownership of the config directory to the original user
154+
if err := os.Chown(cm.configDir, uid, gid); err != nil {
155+
cm.logger.Warn("Failed to change config directory ownership", "error", err)
156+
}
157+
}
158+
}
159+
}
160+
}
161+
145162
// Generate private key
146163
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
147164
if err != nil {

0 commit comments

Comments
 (0)