Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: create ssh conf directory if it doesn't exist
  • Loading branch information
brettkolodny committed May 7, 2025
commit 873e320b5f0d4885924eac61d6dbdda9fd0f4a92
6 changes: 6 additions & 0 deletions cli/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,12 @@ func (r *RootCmd) configSSH() *serpent.Command {
}

if !bytes.Equal(configRaw, configModified) {
// Ensure the parent directory exists before writing the file
sshDir := filepath.Dir(sshConfigFile)
if err := os.MkdirAll(sshDir, os.ModePerm); err != nil {
return xerrors.Errorf("failed to create directory %q: %w", sshDir, err)
}

err = atomic.WriteFile(sshConfigFile, bytes.NewReader(configModified))
if err != nil {
return xerrors.Errorf("write ssh config failed: %w", err)
Expand Down
41 changes: 41 additions & 0 deletions cli/configssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,47 @@ func TestConfigSSH(t *testing.T) {
<-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(os.ModePerm), sshDirInfo.Mode().Perm(), "directory should have 0700 permissions")
}

func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
t.Parallel()

Expand Down
Loading