mirror of
https://github.com/coder/coder.git
synced 2025-07-12 00:14:10 +00:00
fix: create ssh directory if it doesn't already exist when running coder config-ssh
(#17711)
Closes [coder/internal#623](https://github.com/coder/internal/issues/623) > [!WARNING] > PR co-authored by Claude Code
This commit is contained in:
@ -440,6 +440,11 @@ func (r *RootCmd) configSSH() *serpent.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(configRaw, configModified) {
|
if !bytes.Equal(configRaw, configModified) {
|
||||||
|
sshDir := filepath.Dir(sshConfigFile)
|
||||||
|
if err := os.MkdirAll(sshDir, 0700); err != nil {
|
||||||
|
return xerrors.Errorf("failed to create directory %q: %w", sshDir, err)
|
||||||
|
}
|
||||||
|
|
||||||
err = atomic.WriteFile(sshConfigFile, bytes.NewReader(configModified))
|
err = atomic.WriteFile(sshConfigFile, bytes.NewReader(configModified))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return xerrors.Errorf("write ssh config failed: %w", err)
|
return xerrors.Errorf("write ssh config failed: %w", err)
|
||||||
|
@ -169,6 +169,47 @@ func TestConfigSSH(t *testing.T) {
|
|||||||
<-copyDone
|
<-copyDone
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigSSH_MissingDirectory(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Skip("See coder/internal#117")
|
||||||
|
}
|
||||||
|
|
||||||
|
client := coderdtest.New(t, nil)
|
||||||
|
_ = coderdtest.CreateFirstUser(t, client)
|
||||||
|
|
||||||
|
// Create a temporary directory but don't create .ssh subdirectory
|
||||||
|
tmpdir := t.TempDir()
|
||||||
|
sshConfigPath := filepath.Join(tmpdir, ".ssh", "config")
|
||||||
|
|
||||||
|
// Run config-ssh with a non-existent .ssh directory
|
||||||
|
args := []string{
|
||||||
|
"config-ssh",
|
||||||
|
"--ssh-config-file", sshConfigPath,
|
||||||
|
"--yes", // Skip confirmation prompts
|
||||||
|
}
|
||||||
|
inv, root := clitest.New(t, args...)
|
||||||
|
clitest.SetupConfig(t, client, root)
|
||||||
|
|
||||||
|
err := inv.Run()
|
||||||
|
require.NoError(t, err, "config-ssh should succeed with non-existent directory")
|
||||||
|
|
||||||
|
// Verify that the .ssh directory was created
|
||||||
|
sshDir := filepath.Dir(sshConfigPath)
|
||||||
|
_, err = os.Stat(sshDir)
|
||||||
|
require.NoError(t, err, ".ssh directory should exist")
|
||||||
|
|
||||||
|
// Verify that the config file was created
|
||||||
|
_, err = os.Stat(sshConfigPath)
|
||||||
|
require.NoError(t, err, "config file should exist")
|
||||||
|
|
||||||
|
// Check that the directory has proper permissions (0700)
|
||||||
|
sshDirInfo, err := os.Stat(sshDir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, os.FileMode(0700), sshDirInfo.Mode().Perm(), "directory should have 0700 permissions")
|
||||||
|
}
|
||||||
|
|
||||||
func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
|
func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user