Skip to content

Commit 2ed920a

Browse files
Copilotmatifali
andcommitted
Fix SSH config location in snap environments
Added realHomeDir() function that respects SNAP_REAL_HOME environment variable to ensure config-ssh writes to the correct location in snap environments. Added test to verify the behavior. Co-authored-by: matifali <10648092+matifali@users.noreply.github.com>
1 parent 1487e6a commit 2ed920a

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

cli/configssh.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func (r *RootCmd) configSSH() *serpent.Command {
291291
}
292292
}
293293
root := r.createConfig()
294-
homedir, err := os.UserHomeDir()
294+
homedir, err := realHomeDir()
295295
if err != nil {
296296
return xerrors.Errorf("user home dir failed: %w", err)
297297
}
@@ -880,6 +880,20 @@ func currentBinPath(w io.Writer) (string, error) {
880880
return exePath, nil
881881
}
882882

883+
// realHomeDir returns the user's actual home directory.
884+
// In snap environments, os.UserHomeDir() returns the snap's isolated home
885+
// directory (e.g., ~/snap/coder/x3/), but we need the actual user's home
886+
// directory for SSH config. This function checks for SNAP_REAL_HOME first,
887+
// which contains the actual home directory in snap environments.
888+
func realHomeDir() (string, error) {
889+
// In snap environments, SNAP_REAL_HOME contains the actual user home.
890+
if snapHome := os.Getenv("SNAP_REAL_HOME"); snapHome != "" {
891+
return snapHome, nil
892+
}
893+
// Fall back to the standard method for non-snap environments.
894+
return os.UserHomeDir()
895+
}
896+
883897
// diffBytes takes two byte slices and diffs them as if they were in a
884898
// file named name.
885899
// nolint: revive // Color is an option, not a control coupling.

cli/configssh_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,3 +753,54 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
753753
})
754754
}
755755
}
756+
757+
func TestConfigSSH_SnapEnvironment(t *testing.T) {
758+
if runtime.GOOS == "windows" {
759+
t.Skip("Snap is not available on Windows")
760+
}
761+
762+
client := coderdtest.New(t, nil)
763+
_ = coderdtest.CreateFirstUser(t, client)
764+
765+
// Create a temporary directory that simulates the real home
766+
realHome := t.TempDir()
767+
realSSHDir := filepath.Join(realHome, ".ssh")
768+
err := os.MkdirAll(realSSHDir, 0o700)
769+
require.NoError(t, err)
770+
realSSHConfig := filepath.Join(realSSHDir, "config")
771+
772+
// Create a separate directory that simulates the snap home
773+
snapHome := t.TempDir()
774+
775+
// Set SNAP_REAL_HOME to point to the real home directory
776+
t.Setenv("SNAP_REAL_HOME", realHome)
777+
// Set HOME to the snap directory to simulate snap environment
778+
t.Setenv("HOME", snapHome)
779+
780+
// Run config-ssh with default path (~/. ssh/config)
781+
// It should use SNAP_REAL_HOME and write to realSSHConfig
782+
args := []string{
783+
"config-ssh",
784+
"--yes", // Skip confirmation prompts
785+
}
786+
inv, root := clitest.New(t, args...)
787+
clitest.SetupConfig(t, client, root)
788+
789+
err = inv.Run()
790+
require.NoError(t, err, "config-ssh should succeed in snap environment")
791+
792+
// Verify that the config file was written to the REAL home directory,
793+
// not the snap home directory
794+
_, err = os.Stat(realSSHConfig)
795+
require.NoError(t, err, "config file should exist in real home directory")
796+
797+
// Verify the config file contains the expected coder section
798+
content, err := os.ReadFile(realSSHConfig)
799+
require.NoError(t, err)
800+
require.Contains(t, string(content), "# ------------START-CODER-----------", "config should contain coder section")
801+
802+
// Verify that nothing was written to the snap home directory
803+
snapSSHConfig := filepath.Join(snapHome, ".ssh", "config")
804+
_, err = os.Stat(snapSSHConfig)
805+
require.True(t, os.IsNotExist(err), "config file should NOT exist in snap home directory")
806+
}

0 commit comments

Comments
 (0)