Skip to content

Commit 2b1de2f

Browse files
committed
fix: propagate errors from known_hosts setup instead of silently ignoring them
When a user provides known_hosts data in the auth secret, errors during temp file creation, writing, or callback setup were silently swallowed, causing silent fallback to no host key verification. Now these errors are returned so the operator knows when known_hosts setup fails.
1 parent 079edc1 commit 2b1de2f

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

internal/git/manager.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,22 @@ func (m *managerImpl) getSSHClientOptions(ctx context.Context, authSecret map[st
160160
var tempFilePath string
161161
if knownHostsData, ok := authSecret["known_hosts"]; ok {
162162
tmpFile, err := os.CreateTemp("", "known_hosts-*")
163-
if err == nil {
164-
if _, err := tmpFile.Write(knownHostsData); err == nil {
165-
_ = tmpFile.Close()
166-
tempFilePath = tmpFile.Name()
167-
cb, err := gitssh.NewKnownHostsCallback(tempFilePath)
168-
if err == nil {
169-
auth.HostKeyCallback = cb
170-
}
171-
}
163+
if err != nil {
164+
return nil, "", fmt.Errorf("failed to create known_hosts temp file: %w", err)
165+
}
166+
if _, err := tmpFile.Write(knownHostsData); err != nil {
167+
_ = tmpFile.Close()
168+
_ = os.Remove(tmpFile.Name())
169+
return nil, "", fmt.Errorf("failed to write known_hosts temp file: %w", err)
170+
}
171+
_ = tmpFile.Close()
172+
tempFilePath = tmpFile.Name()
173+
cb, err := gitssh.NewKnownHostsCallback(tempFilePath)
174+
if err != nil {
175+
_ = os.Remove(tempFilePath)
176+
return nil, "", fmt.Errorf("failed to configure known_hosts callback: %w", err)
172177
}
178+
auth.HostKeyCallback = cb
173179
} else {
174180
logger.Info("SSH host key verification is disabled, provide known_hosts in auth secret to enable verification")
175181
auth.HostKeyCallback = gossh.InsecureIgnoreHostKey()

internal/git/manager_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package git
22

33
import (
44
"context"
5+
"os"
56
"testing"
67

78
"github.com/go-git/go-git/v6/plumbing/transport"
@@ -90,6 +91,25 @@ func TestGetClientOptions_SSHWithPrivateKey(t *testing.T) {
9091
}
9192
}
9293

94+
func TestGetClientOptions_SSHWithPrivateKeyAndKnownHosts(t *testing.T) {
95+
m := &managerImpl{}
96+
secret := map[string][]byte{
97+
"sshPrivateKey": []byte(testEd25519PrivateKey),
98+
"known_hosts": []byte("github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl\n"),
99+
}
100+
opts, tmpFile, err := m.getClientOptions(context.Background(), sshScheme, secret)
101+
if err != nil {
102+
t.Fatalf("unexpected error: %v", err)
103+
}
104+
if len(opts) != 1 {
105+
t.Fatalf("expected 1 option, got %d", len(opts))
106+
}
107+
if tmpFile == "" {
108+
t.Fatal("expected temp known_hosts file path, got empty string")
109+
}
110+
defer os.Remove(tmpFile)
111+
}
112+
93113
func TestGetClientOptions_SSHWithInvalidKey(t *testing.T) {
94114
m := &managerImpl{}
95115
secret := map[string][]byte{"sshPrivateKey": []byte("not-a-valid-key")}

0 commit comments

Comments
 (0)