Skip to content

Modern Docker Management, Designed for Everyone

fix: use global .ssh/known_hosts path (#2279)

Kyle Mendell0abb8f881072
diff --git a/backend/pkg/gitutil/git.go b/backend/pkg/gitutil/git.go
index d68ba4404..f2d3021f5 100644
--- a/backend/pkg/gitutil/git.go
+++ b/backend/pkg/gitutil/git.go
@@ -44,6 +44,8 @@ const (
SSHHostKeyVerificationStrict = "strict" // Require host key in known_hosts
SSHHostKeyVerificationAcceptNew = "accept_new" // Auto-add unknown host keys
SSHHostKeyVerificationSkip = "skip" // Skip host key verification (insecure)
+ defaultKnownHostsDataDir = "/app/data"
+ defaultKnownHostsPath = "/app/data/.ssh/known_hosts"
)
// AuthConfig holds authentication configuration
@@ -165,18 +167,29 @@ func (c *Client) createAcceptNewHostKeyCallback() (gossh.HostKeyCallback, error)
// getKnownHostsPath returns the path to the known_hosts file
func getKnownHostsPath() string {
+ return getKnownHostsPathInternal(os.Getenv, os.Stat, os.UserHomeDir)
+}
+
+func getKnownHostsPathInternal(getenv func(string) string, stat func(string) (os.FileInfo, error), userHomeDir func() (string, error)) string {
// Check environment variable first
- if path := os.Getenv("SSH_KNOWN_HOSTS"); path != "" {
+ if path := getenv("SSH_KNOWN_HOSTS"); path != "" {
return path
}
- // Fall back to default location
- homeDir, err := os.UserHomeDir()
- if err != nil {
- // Use a fallback in the working directory
- return filepath.Join(os.TempDir(), ".ssh", "known_hosts")
+ // Prefer Arcane's writable persistent data directory when it is available,
+ // which is the case for published container images and PUID/PGID setups.
+ if info, err := stat(defaultKnownHostsDataDir); err == nil && info.IsDir() {
+ return defaultKnownHostsPath
}
- return filepath.Join(homeDir, ".ssh", "known_hosts")
+
+ // Fall back to the user's home directory for local development and CI.
+ homeDir, err := userHomeDir()
+ if err == nil && homeDir != "" {
+ return filepath.Join(homeDir, ".ssh", "known_hosts")
+ }
+
+ // Last resort for environments without a resolvable home directory.
+ return filepath.Join(os.TempDir(), ".ssh", "known_hosts")
}
// addHostKey adds a host key to the known_hosts file
diff --git a/backend/pkg/gitutil/git_test.go b/backend/pkg/gitutil/git_test.go
index ad7a8cc60..f00b75de4 100644
--- a/backend/pkg/gitutil/git_test.go
+++ b/backend/pkg/gitutil/git_test.go
@@ -2,12 +2,14 @@ package git
import (
"context"
+ "errors"
"net"
"os"
"path/filepath"
"strings"
"sync"
"testing"
+ "time"
gossh "golang.org/x/crypto/ssh"
)
@@ -15,27 +17,75 @@ import (
func TestGetKnownHostsPath(t *testing.T) {
t.Run("returns SSH_KNOWN_HOSTS env var when set", func(t *testing.T) {
customPath := "/custom/path/known_hosts"
- t.Setenv("SSH_KNOWN_HOSTS", customPath)
- result := getKnownHostsPath()
+ result := getKnownHostsPathInternal(
+ func(string) string { return customPath },
+ os.Stat,
+ os.UserHomeDir,
+ )
if result != customPath {
t.Errorf("expected %s, got %s", customPath, result)
}
})
- t.Run("returns default path when env var not set", func(t *testing.T) {
- t.Setenv("SSH_KNOWN_HOSTS", "")
+ t.Run("returns Arcane data path when data directory exists", func(t *testing.T) {
+ result := getKnownHostsPathInternal(
+ func(string) string { return "" },
+ func(path string) (os.FileInfo, error) {
+ if path == defaultKnownHostsDataDir {
+ return stubFileInfo{dir: true}, nil
+ }
+ return nil, os.ErrNotExist
+ },
+ func() (string, error) { return "/home/tester", nil },
+ )
+
+ expected := defaultKnownHostsPath
+
+ if result != expected {
+ t.Errorf("expected %s, got %s", expected, result)
+ }
+ })
+
+ t.Run("falls back to home directory when Arcane data directory is unavailable", func(t *testing.T) {
+ homeDir := "/home/tester"
+ result := getKnownHostsPathInternal(
+ func(string) string { return "" },
+ func(string) (os.FileInfo, error) { return nil, os.ErrNotExist },
+ func() (string, error) { return homeDir, nil },
+ )
- result := getKnownHostsPath()
- homeDir, _ := os.UserHomeDir()
expected := filepath.Join(homeDir, ".ssh", "known_hosts")
+ if result != expected {
+ t.Errorf("expected %s, got %s", expected, result)
+ }
+ })
+ t.Run("falls back to temp dir when data directory and home are unavailable", func(t *testing.T) {
+ result := getKnownHostsPathInternal(
+ func(string) string { return "" },
+ func(string) (os.FileInfo, error) { return nil, os.ErrNotExist },
+ func() (string, error) { return "", errors.New("no home directory") },
+ )
+
+ expected := filepath.Join(os.TempDir(), ".ssh", "known_hosts")
if result != expected {
t.Errorf("expected %s, got %s", expected, result)
}
})
}
+type stubFileInfo struct {
+ dir bool
+}
+
+func (s stubFileInfo) Name() string { return "stub" }
+func (s stubFileInfo) Size() int64 { return 0 }
+func (s stubFileInfo) Mode() os.FileMode { return 0o755 }
+func (s stubFileInfo) ModTime() time.Time { return time.Time{} }
+func (s stubFileInfo) IsDir() bool { return s.dir }
+func (s stubFileInfo) Sys() any { return nil }
+
func TestGetSSHHostKeyCallback(t *testing.T) {
client := NewClient("")
diff --git a/backend/pkg/projects/fs_util.go b/backend/pkg/projects/fs_util.go
index c6ed6f2f9..7c1894530 100644
--- a/backend/pkg/projects/fs_util.go
+++ b/backend/pkg/projects/fs_util.go
@@ -123,10 +123,6 @@ func ReadProjectDirectoryFiles(projectPath string, shownFiles map[string]bool, m
return readProjectDirectoryFilesInternal(projectPath, shownFiles, maxDepth, skipDirectories, false)
}
-func ReadProjectDirectoryFilesWithContent(projectPath string, shownFiles map[string]bool, maxDepth int, skipDirectories string) ([]project.IncludeFile, error) {
- return readProjectDirectoryFilesInternal(projectPath, shownFiles, maxDepth, skipDirectories, true)
-}
-
func readProjectDirectoryFilesInternal(projectPath string, shownFiles map[string]bool, maxDepth int, skipDirectories string, includeContent bool) ([]project.IncludeFile, error) {
if maxDepth <= 0 {
maxDepth = config.Load().ProjectScanMaxDepth