Skip to content

Commit 20d40b5

Browse files
zhming0DrJosh9000
authored andcommitted
Merge pull request #3676 from buildkite/ming/pb-1068-refactor-1
A-1118: Replace addRepositoryHostToSSHKnownHosts with StrictHostKeyChecking=accept-new
2 parents e7b28a0 + 9072c73 commit 20d40b5

13 files changed

Lines changed: 173 additions & 507 deletions

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ require (
3535
github.com/denisbrodbeck/machineid v1.0.1
3636
github.com/dustin/go-humanize v1.0.1
3737
github.com/dustinkirkland/golang-petname v0.0.0-20260215035315-f0c533e9ce9b
38-
github.com/gliderlabs/ssh v0.3.8
3938
github.com/go-chi/chi/v5 v5.3.0
4039
github.com/gofrs/flock v0.13.0
4140
github.com/google/go-cmp v0.7.0
@@ -60,7 +59,6 @@ require (
6059
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.43.0
6160
go.opentelemetry.io/otel/sdk v1.43.0
6261
go.opentelemetry.io/otel/trace v1.43.0
63-
golang.org/x/crypto v0.52.0
6462
golang.org/x/net v0.55.0
6563
golang.org/x/oauth2 v0.36.0
6664
golang.org/x/sync v0.20.0
@@ -85,7 +83,6 @@ require (
8583
github.com/agnivade/levenshtein v1.2.1 // indirect
8684
github.com/alexflint/go-arg v1.5.1 // indirect
8785
github.com/alexflint/go-scalar v1.2.0 // indirect
88-
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
8986
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.10 // indirect
9087
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.23 // indirect
9188
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.23 // indirect
@@ -149,6 +146,7 @@ require (
149146
go.opentelemetry.io/proto/otlp v1.10.0 // indirect
150147
go.uber.org/multierr v1.11.0 // indirect
151148
go.yaml.in/yaml/v2 v2.4.4 // indirect
149+
golang.org/x/crypto v0.52.0 // indirect
152150
golang.org/x/mod v0.35.0 // indirect
153151
golang.org/x/text v0.37.0 // indirect
154152
golang.org/x/time v0.15.0 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+W
5151
github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
5252
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
5353
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
54-
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
55-
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
5654
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
5755
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
5856
github.com/aws/aws-sdk-go-v2 v1.41.7 h1:DWpAJt66FmnnaRIOT/8ASTucrvuDPZASqhhLey6tLY8=
@@ -162,8 +160,6 @@ github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8
162160
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
163161
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
164162
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
165-
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
166-
github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
167163
github.com/go-chi/chi/v5 v5.3.0 h1:halUjDxhshgXHMrao5bB8eNBXo/rnzwr8m5m36glehM=
168164
github.com/go-chi/chi/v5 v5.3.0/go.mod h1:R+tYY2hNuVUUjxoPtqUdgBqevM9s9njzkTLutVsOCto=
169165
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=

internal/job/checkout.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -843,10 +843,6 @@ func (e *Executor) defaultCheckoutPhase(ctx context.Context) (retErr error) {
843843
})
844844
defer func() { tracetools.FinishWithError(span, retErr) }()
845845

846-
if e.SSHKeyscan {
847-
addRepositoryHostToSSHKnownHosts(ctx, e.shell, e.Repository)
848-
}
849-
850846
var mirrorDir string
851847

852848
// If we can, get a mirror of the git repository to use for reference later
@@ -995,11 +991,6 @@ func (e *Executor) defaultCheckoutPhase(ctx context.Context) (retErr error) {
995991
} else {
996992
mirrorSubmodules := e.GitMirrorsPath != ""
997993
for _, repository := range submoduleRepos {
998-
// submodules might need their fingerprints verified too
999-
if e.SSHKeyscan {
1000-
addRepositoryHostToSSHKnownHosts(ctx, e.shell, repository)
1001-
}
1002-
1003994
if !mirrorSubmodules {
1004995
continue
1005996
}

internal/job/executor.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -829,24 +829,6 @@ func dirForRepository(repository string) string {
829829
return badCharsRE.ReplaceAllString(repository, "-")
830830
}
831831

832-
// Given a repository, it will add the host to the set of SSH known_hosts on the machine
833-
func addRepositoryHostToSSHKnownHosts(ctx context.Context, sh *shell.Shell, repository string) {
834-
if osutil.FileExists(repository) {
835-
return
836-
}
837-
838-
knownHosts, err := findKnownHosts(sh)
839-
if err != nil {
840-
sh.Warningf("Failed to find SSH known_hosts file: %v", err)
841-
return
842-
}
843-
844-
if err = knownHosts.AddFromRepository(ctx, repository); err != nil {
845-
sh.Warningf("Error adding to known_hosts: %v", err)
846-
return
847-
}
848-
}
849-
850832
// setUp is run before all the phases run. It's responsible for initializing the
851833
// job environment
852834
func (e *Executor) setUp(ctx context.Context) (retErr error) {
@@ -899,6 +881,13 @@ func (e *Executor) setUp(ctx context.Context) (retErr error) {
899881
// Disable any interactive Git/SSH prompting
900882
e.shell.Env.Set("GIT_TERMINAL_PROMPT", "0")
901883

884+
// Configure SSH to automatically accept new host keys (TOFU - Trust On First Use)
885+
// This replaces the previous ssh-keyscan approach with a simpler mechanism that
886+
// achieves the same security model: accept new hosts, reject changed keys.
887+
if e.SSHKeyscan {
888+
e.configureSSHKeyChecking()
889+
}
890+
902891
// Fetch and set secrets before environment hook execution
903892
if e.Secrets != "" {
904893
if err := e.fetchAndSetSecrets(ctx); err != nil {

internal/job/integration/checkout_git_mirrors_integration_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,6 @@ func TestCheckingOutWithSSHKeyscan_WithGitMirrors(t *testing.T) {
330330
t.Fatalf("EnableGitMirrors() error = %v", err)
331331
}
332332

333-
tester.MustMock(t, "ssh-keyscan").
334-
Expect("github.com").
335-
AndWriteToStdout("github.com ssh-rsa xxx=").
336-
AndExitWith(0)
337-
338333
git := tester.MustMock(t, "git")
339334
git.IgnoreUnexpectedInvocations()
340335

@@ -362,10 +357,6 @@ func TestCheckingOutWithoutSSHKeyscan_WithGitMirrors(t *testing.T) {
362357
t.Fatalf("EnableGitMirrors() error = %v", err)
363358
}
364359

365-
tester.MustMock(t, "ssh-keyscan").
366-
Expect("github.com").
367-
NotCalled()
368-
369360
env := []string{
370361
"BUILDKITE_REPO=https://github.com/buildkite/bash-example.git",
371362
"BUILDKITE_SSH_KEYSCAN=false",
@@ -387,10 +378,6 @@ func TestCheckingOutWithSSHKeyscanAndUnscannableRepo_WithGitMirrors(t *testing.T
387378
t.Fatalf("EnableGitMirrors() error = %v", err)
388379
}
389380

390-
tester.MustMock(t, "ssh-keyscan").
391-
Expect("github.com").
392-
NotCalled()
393-
394381
git := tester.MustMock(t, "git")
395382
git.IgnoreUnexpectedInvocations()
396383

internal/job/integration/checkout_integration_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -765,11 +765,6 @@ func TestCheckingOutWithSSHKeyscan(t *testing.T) {
765765
}
766766
defer tester.Close()
767767

768-
tester.MustMock(t, "ssh-keyscan").
769-
Expect("github.com").
770-
AndWriteToStdout("github.com ssh-rsa xxx=").
771-
AndExitWith(0)
772-
773768
git := tester.MustMock(t, "git")
774769
git.IgnoreUnexpectedInvocations()
775770

@@ -793,10 +788,6 @@ func TestCheckingOutWithoutSSHKeyscan(t *testing.T) {
793788
}
794789
defer tester.Close()
795790

796-
tester.MustMock(t, "ssh-keyscan").
797-
Expect("github.com").
798-
NotCalled()
799-
800791
env := []string{
801792
"BUILDKITE_REPO=https://github.com/buildkite/bash-example.git",
802793
"BUILDKITE_SSH_KEYSCAN=false",
@@ -814,10 +805,6 @@ func TestCheckingOutWithSSHKeyscanAndUnscannableRepo(t *testing.T) {
814805
}
815806
defer tester.Close()
816807

817-
tester.MustMock(t, "ssh-keyscan").
818-
Expect("github.com").
819-
NotCalled()
820-
821808
git := tester.MustMock(t, "git")
822809
git.IgnoreUnexpectedInvocations()
823810

internal/job/integration/job_api_integration_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,7 @@ func TestBootstrapRunsJobAPI(t *testing.T) {
142142
}
143143
})
144144

145-
tester.RunAndCheck(t)
145+
// Disable SSH keyscan since this test isn't about SSH and it modifies
146+
// GIT_SSH_COMMAND, causing an env mismatch between the Job API and the process.
147+
tester.RunAndCheck(t, "BUILDKITE_SSH_KEYSCAN=false")
146148
}

internal/job/knownhosts.go

Lines changed: 0 additions & 157 deletions
This file was deleted.

0 commit comments

Comments
 (0)