diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index 9c512c583..8003c98c0 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -360,6 +360,36 @@ jobs: & "$installDir\podman.exe" machine set --rootful & "$installDir\podman.exe" machine start + - name: download kind (Windows) + if: matrix.install-kind == true && runner.os == 'Windows' && (matrix.requires-secret == false || needs.can-read-secret.outputs.secret-set == 'true') + shell: pwsh + run: | + $url = "https://github.com/kubernetes-sigs/kind/releases/download/v0.24.0/kind-windows-amd64" + $path = "$env:RUNNER_TEMP\kind.exe" + + $maxAttempts = 3 + for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) { + try { + Invoke-WebRequest -Uri $url -OutFile $path + break + } + catch { + if ($attempt -eq $maxAttempts) { + throw + } + Start-Sleep -Seconds (5 * $attempt) + } + } + + $expected = "6f724188289cc79395f45afae0f2b85e0d220c2b84c6ed2f5047d9d0c9a67028" + $actual = (Get-FileHash -Path $path -Algorithm SHA256).Hash.ToLower() + + if ($actual -ne $expected) { + throw "SHA256 mismatch for kind.exe. Expected: $expected, Got: $actual" + } + + "$env:RUNNER_TEMP" | Out-File -Append -FilePath $env:GITHUB_PATH -Encoding utf8 + - name: setup kind (Windows) if: matrix.install-kind == true && runner.os == 'Windows' && (matrix.requires-secret == false || needs.can-read-secret.outputs.secret-set == 'true') shell: bash @@ -367,21 +397,9 @@ jobs: KIND_EXPERIMENTAL_PROVIDER: podman DOCKER_HOST: npipe:////./pipe/podman-machine-default run: | - curl -Lo "$RUNNER_TEMP/kind.exe" "https://github.com/kubernetes-sigs/kind/releases/download/v0.24.0/kind-windows-amd64" - expected="6f724188289cc79395f45afae0f2b85e0d220c2b84c6ed2f5047d9d0c9a67028" - actual=$(sha256sum "$RUNNER_TEMP/kind.exe" | awk '{print $1}' | sed 's/^[^a-f0-9]*//') - if [ "$actual" != "$expected" ]; then - echo "SHA256 mismatch for kind.exe! Expected: $expected, Got: $actual" - exit 1 - fi - echo "kind.exe checksum verified: $actual" - export PATH="$RUNNER_TEMP:$PATH" - echo "$RUNNER_TEMP" >> "$GITHUB_PATH" - CLUSTER_NAME=$(python -c "import uuid; print(uuid.uuid4().hex)") kind create cluster --name "$CLUSTER_NAME" --image kindest/node:v1.34.0@sha256:7416a61b42b1662ca6ca89f02028ac133a309a2a30ba309614e8ec94d976dc5a - # NOTE: skevetter/setup-kind does not work on Windows runners - name: setup kind if: matrix.install-kind == true && runner.os != 'Windows' && (matrix.requires-secret == false || needs.can-read-secret.outputs.secret-set == 'true') diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index a72c37d44..6aaa901ae 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -33,7 +33,7 @@ jobs: version: latest install-only: true - - run: go install golang.org/x/tools/cmd/goimports@latest + - run: go install golang.org/x/tools/cmd/goimports@v0.49.0 - uses: actions/cache@v5 with: diff --git a/pkg/ssh/config.go b/pkg/ssh/config.go index a5ab929ce..aacd6b9ed 100644 --- a/pkg/ssh/config.go +++ b/pkg/ssh/config.go @@ -219,6 +219,26 @@ func buildSSHConfigLines(params addHostParams, proxyCmd string) []string { build() } +func sshConfigKeyword(line string) string { + trimmed := strings.TrimSpace(line) + if trimmed == "" || strings.HasPrefix(trimmed, "#") { + return "" + } + + if index := strings.IndexAny(trimmed, " \t="); index >= 0 { + return trimmed[:index] + } + + return trimmed +} + +func isSSHSectionStart(line string) bool { + keyword := sshConfigKeyword(line) + + return strings.EqualFold(keyword, "host") || + strings.EqualFold(keyword, "match") +} + // findInsertPosition finds where to insert new SSH config entry. func findInsertPosition(config string) (int, []string, error) { lineNumber := 0 @@ -229,13 +249,14 @@ func findInsertPosition(config string) (int, []string, error) { scanner := bufio.NewScanner(strings.NewReader(config)) for scanner.Scan() { line := scanner.Text() + trimmed := strings.TrimSpace(line) - if strings.HasPrefix(strings.TrimSpace(line), "Host") && !found { + if isSSHSectionStart(line) && !found { found = true lineNumber = max(lineNumber-commentLines, 0) } - if strings.HasPrefix(strings.TrimSpace(line), "#") { + if strings.HasPrefix(trimmed, "#") { commentLines++ } else { commentLines = 0 diff --git a/pkg/ssh/config_test.go b/pkg/ssh/config_test.go index f7752cc4d..b49334d7f 100644 --- a/pkg/ssh/config_test.go +++ b/pkg/ssh/config_test.go @@ -1,8 +1,12 @@ package ssh import ( + "os" + "path/filepath" + "strings" "testing" + "github.com/skevetter/log" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -205,52 +209,217 @@ Host existinghost }, { name: "Host addition to existing config with DevPod host", - config: `# DevPod Start existingtesthost -Host existingtesthost - ForwardAgent yes - LogLevel error - StrictHostKeyChecking no - UserKnownHostsFile /dev/null - HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa - ProxyCommand "/path/to/exec" ssh --stdio --context testcontext --user testuser testworkspace - User testuser -# DevPod End testhost - -Host existinghost - User existinguser`, - execPath: "/path/to/exec", - host: "testhost", - user: "testuser", - context: "testcontext", - workspace: "testworkspace", - workdir: "", - command: "", - gpgagent: false, - devPodHome: "", - provider: "", - expected: `# DevPod Start testhost -Host testhost - ForwardAgent yes - LogLevel error - StrictHostKeyChecking no - UserKnownHostsFile /dev/null - HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa - ProxyCommand "/path/to/exec" ssh --stdio --context testcontext --user testuser testworkspace - User testuser -# DevPod End testhost -# DevPod Start existingtesthost -Host existingtesthost - ForwardAgent yes - LogLevel error - StrictHostKeyChecking no - UserKnownHostsFile /dev/null - HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa - ProxyCommand "/path/to/exec" ssh --stdio --context testcontext --user testuser testworkspace - User testuser -# DevPod End testhost - -Host existinghost - User existinguser`, + config: "# DevPod Start existingtesthost\n" + + "Host existingtesthost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End existingtesthost\n\n" + + "Host existinghost\n" + + " User existinguser", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "# DevPod Start existingtesthost\n" + + "Host existingtesthost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End existingtesthost\n\n" + + "Host existinghost\n" + + " User existinguser", + }, + { + name: "Host addition with lowercase host entries", + config: "host 192.168.1.1\n User alice\n Port 22\n\nhost myserver\n User bob", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "host 192.168.1.1\n User alice\n Port 22\n\nhost myserver\n User bob", + }, + { + name: "Host addition with uppercase HOST entries", + config: "HOST 192.168.1.1\n User alice\n Port 22\n\nHOST myserver\n User bob", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "HOST 192.168.1.1\n User alice\n Port 22\n\nHOST myserver\n User bob", + }, + { + name: "Host addition with mixed case HoSt entries", + config: "HoSt 192.168.1.1\n User alice\n Port 22\n\nHoSt myserver\n User bob", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "HoSt 192.168.1.1\n User alice\n Port 22\n\nHoSt myserver\n User bob", + }, + { + name: "Host addition with tab separated Host keyword", + config: "Host\t192.168.1.1\n User alice\n Port 22\n\nHost\tmyserver\n User bob", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "Host\t192.168.1.1\n User alice\n Port 22\n\nHost\tmyserver\n User bob", + }, + { + name: "Host addition with equals separated Host keyword", + config: "Host=192.168.1.1\n User alice\n Port 22\n\nHost=myserver\n User bob", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "Host=192.168.1.1\n User alice\n Port 22\n\nHost=myserver\n User bob", + }, + { + name: "Host addition before Match section", + config: "Match exec \"true\"\n User conditional\n Port 22", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "Match exec \"true\"\n User conditional\n Port 22", + }, + { + name: "Host addition does not corrupt existing DevPod block followed by plain host", + config: "host plain-host\n User alice\n\n" + + "# DevPod Start existing.devpod\n" + + "Host existing.devpod\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context ctx --user user existing\n" + + " User user\n" + + "# DevPod End existing.devpod", + execPath: "/path/to/exec", + host: "testhost", + user: "testuser", + context: "testcontext", + workspace: "testworkspace", + expected: "# DevPod Start testhost\n" + + "Host testhost\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context testcontext --user testuser testworkspace\n" + + " User testuser\n" + + "# DevPod End testhost\n" + + "host plain-host\n User alice\n\n" + + "# DevPod Start existing.devpod\n" + + "Host existing.devpod\n" + + " ForwardAgent yes\n" + + " LogLevel error\n" + + " StrictHostKeyChecking no\n" + + " UserKnownHostsFile /dev/null\n" + + " HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa\n" + + " ProxyCommand \"/path/to/exec\" ssh --stdio" + + " --context ctx --user user existing\n" + + " User user\n" + + "# DevPod End existing.devpod", }, { name: "Host addition after top level includes", @@ -327,6 +496,153 @@ Host testhost if tt.config != "" { assert.Contains(s.T(), result, tt.config) } + + if strings.Contains(tt.config, MarkerStartPrefix) { + idxStart := strings.Index(tt.config, MarkerStartPrefix) + lineEnd := strings.Index(tt.config[idxStart:], "\n") + var existingStartMarker string + if lineEnd != -1 { + existingStartMarker = tt.config[idxStart : idxStart+lineEnd] + } else { + existingStartMarker = tt.config[idxStart:] + } + assert.Contains(s.T(), result, existingStartMarker) + assert.Less( + s.T(), + strings.Index(result, MarkerStartPrefix+tt.host), + strings.Index(result, existingStartMarker), + ) + } }) } } + +func (s *SSHConfigTestSuite) TestSSHConfigKeyword() { + tests := []struct { + input string + expected string + }{ + {input: "", expected: ""}, + {input: "# comment", expected: ""}, + {input: " # indented comment", expected: ""}, + {input: "Host myserver", expected: "Host"}, + {input: "host myserver", expected: "host"}, + {input: "HOST myserver", expected: "HOST"}, + {input: "HoSt\tmyserver", expected: "HoSt"}, + {input: "Host=myserver", expected: "Host"}, + {input: "HostName example.com", expected: "HostName"}, + {input: "Match exec \"true\"", expected: "Match"}, + {input: "match host foo", expected: "match"}, + {input: "Port 22", expected: "Port"}, + } + + for _, tt := range tests { + assert.Equal(s.T(), tt.expected, sshConfigKeyword(tt.input)) + } +} + +func (s *SSHConfigTestSuite) TestIsSSHSectionStart() { + tests := []struct { + input string + expected bool + }{ + {input: "Host myserver", expected: true}, + {input: "host myserver", expected: true}, + {input: "HOST myserver", expected: true}, + {input: "HoSt\tmyserver", expected: true}, + {input: "Host=myserver", expected: true}, + {input: "Match exec \"true\"", expected: true}, + {input: "match host foo", expected: true}, + {input: "MATCH all", expected: true}, + {input: "HostName example.com", expected: false}, + {input: "Hostname example.com", expected: false}, + {input: "hostname example.com", expected: false}, + {input: "Port 22", expected: false}, + {input: "# Host commented", expected: false}, + {input: "", expected: false}, + } + + for _, tt := range tests { + assert.Equal(s.T(), tt.expected, isSSHSectionStart(tt.input)) + } +} + +func (s *SSHConfigTestSuite) TestFindInsertPosition() { + s.Run("does not treat HostName as section start", func() { + config := "HostName global.example.com\nHost actual-host\n User example" + pos, lines, err := findInsertPosition(config) + assert.NoError(s.T(), err) + assert.Equal(s.T(), 1, pos) + assert.Equal(s.T(), 3, len(lines)) + }) + + s.Run("inserts before Match section with preceding options", func() { + config := "IdentityFile ~/.ssh/id_ed25519\n\nMatch exec \"true\"\n User conditional" + pos, _, err := findInsertPosition(config) + assert.NoError(s.T(), err) + assert.Equal(s.T(), 2, pos) + }) + + s.Run("inserts before existing DevPod block including start marker", func() { + config := "# DevPod Start existing\nHost existing\n User user\n# DevPod End existing" + pos, _, err := findInsertPosition(config) + assert.NoError(s.T(), err) + assert.Equal(s.T(), 0, pos) + }) + + s.Run("inserts before comments attached to host", func() { + config := "# Server comment\nHost actual-host\n User example" + pos, _, err := findInsertPosition(config) + assert.NoError(s.T(), err) + assert.Equal(s.T(), 0, pos) + }) +} + +func (s *SSHConfigTestSuite) TestConfigureSSHConfig() { + tmpDir := s.T().TempDir() + sshConfigFile := filepath.Join(tmpDir, "config") + + existingConfig := "host lowercase-host\n" + + " User alice\n\n" + + "# DevPod Start existing\n" + + "Host existing\n" + + " User existinguser\n" + + "# DevPod End existing\n\n" + + "Match exec \"true\"\n" + + " User conditional\n" + + err := os.WriteFile(sshConfigFile, []byte(existingConfig), 0o600) + assert.NoError(s.T(), err) + + err = ConfigureSSHConfig(SSHConfigParams{ + SSHConfigPath: sshConfigFile, + Workspace: "myworkspace", + User: "newuser", + Context: "testcontext", + Log: log.Discard, + }) + assert.NoError(s.T(), err) + + content, err := os.ReadFile(sshConfigFile) // #nosec G304 -- test path from t.TempDir + assert.NoError(s.T(), err) + result := string(content) + + newHost := "myworkspace.devpod" + assert.Contains(s.T(), result, MarkerStartPrefix+newHost) + assert.Contains(s.T(), result, MarkerEndPrefix+newHost) + assert.Contains(s.T(), result, "Host "+newHost) + assert.Contains(s.T(), result, "User newuser") + + existingBlock := "# DevPod Start existing\n" + + "Host existing\n" + + " User existinguser\n" + + "# DevPod End existing" + assert.Contains(s.T(), result, existingBlock) + + newStartIdx := strings.Index(result, MarkerStartPrefix+newHost) + firstSectionIdx := strings.Index(result, "host lowercase-host") + existingStartIdx := strings.Index(result, MarkerStartPrefix+"existing") + + assert.Less(s.T(), newStartIdx, firstSectionIdx) + assert.Less(s.T(), newStartIdx, existingStartIdx) +}