Skip to content

Commit 859f87f

Browse files
committed
fix: log warning when SSH host key verification is disabled
When InsecureIgnoreHostKey is used (either because no SSH credentials are provided or because known_hosts data is missing from the auth secret), log an informational message so operators can identify and remediate insecure configurations.
1 parent 32f8e52 commit 859f87f

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

internal/git/manager.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
gitssh "github.com/go-git/go-git/v6/plumbing/transport/ssh"
1616
"github.com/prometheus/client_golang/prometheus"
1717
gossh "golang.org/x/crypto/ssh"
18+
"sigs.k8s.io/controller-runtime/pkg/log"
1819
)
1920

2021
const (
@@ -54,7 +55,7 @@ func (m *managerImpl) CloneRepository(ctx context.Context, repoUrl, subPath, ref
5455
return nil, fmt.Errorf("failed to create temporary directory: %w", err)
5556
}
5657

57-
clientOpts, tempFile, err := m.getClientOptions(parsedURL.Scheme, auth)
58+
clientOpts, tempFile, err := m.getClientOptions(ctx, parsedURL.Scheme, auth)
5859
if err != nil {
5960
return nil, fmt.Errorf("failed to configure auth: %w", err)
6061
}
@@ -89,9 +90,9 @@ func (m *managerImpl) CloneRepository(ctx context.Context, repoUrl, subPath, ref
8990
}, nil
9091
}
9192

92-
func (m *managerImpl) getClientOptions(scheme string, authSecret map[string][]byte) ([]client.Option, string, error) {
93+
func (m *managerImpl) getClientOptions(ctx context.Context, scheme string, authSecret map[string][]byte) ([]client.Option, string, error) {
9394
if scheme == "ssh" {
94-
return m.getSSHClientOptions(authSecret)
95+
return m.getSSHClientOptions(ctx, authSecret)
9596
}
9697
opts := m.getHTTPClientOptions(authSecret)
9798
return opts, "", nil
@@ -137,9 +138,12 @@ func ensureKnownHostsExists() error {
137138
return nil
138139
}
139140

140-
func (m *managerImpl) getSSHClientOptions(authSecret map[string][]byte) ([]client.Option, string, error) {
141+
func (m *managerImpl) getSSHClientOptions(ctx context.Context, authSecret map[string][]byte) ([]client.Option, string, error) {
142+
logger := log.FromContext(ctx)
143+
141144
privateKey, hasKey := authSecret["sshPrivateKey"]
142145
if !hasKey {
146+
logger.Info("SSH host key verification is disabled, no SSH credentials provided in auth secret")
143147
return []client.Option{
144148
client.WithSSHAuth(&gitssh.Password{
145149
User: "git",
@@ -153,8 +157,6 @@ func (m *managerImpl) getSSHClientOptions(authSecret map[string][]byte) ([]clien
153157
if err != nil {
154158
return nil, "", fmt.Errorf("failed to parse SSH private key: %w", err)
155159
}
156-
auth.HostKeyCallback = gossh.InsecureIgnoreHostKey()
157-
158160
var tempFilePath string
159161
if knownHostsData, ok := authSecret["known_hosts"]; ok {
160162
tmpFile, err := os.CreateTemp("", "known_hosts-*")
@@ -168,6 +170,8 @@ func (m *managerImpl) getSSHClientOptions(authSecret map[string][]byte) ([]clien
168170
}
169171
}
170172
}
173+
} else {
174+
logger.Info("SSH host key verification is disabled, provide known_hosts in auth secret to enable verification")
171175
}
172176

173177
return []client.Option{client.WithSSHAuth(auth)}, tempFilePath, nil

internal/git/manager_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package git
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/go-git/go-git/v6/plumbing/transport"
@@ -20,7 +21,7 @@ JtdGRlLmNzYgECAw==
2021
func TestGetClientOptions_HTTPToken(t *testing.T) {
2122
m := &managerImpl{}
2223
secret := map[string][]byte{"token": []byte("my-token")}
23-
opts, tmpFile, err := m.getClientOptions("https", secret)
24+
opts, tmpFile, err := m.getClientOptions(context.Background(), "https", secret)
2425
if err != nil {
2526
t.Fatalf("unexpected error: %v", err)
2627
}
@@ -35,7 +36,7 @@ func TestGetClientOptions_HTTPToken(t *testing.T) {
3536
func TestGetClientOptions_HTTPUsernamePassword(t *testing.T) {
3637
m := &managerImpl{}
3738
secret := map[string][]byte{"username": []byte("user"), "password": []byte("pass")}
38-
opts, _, err := m.getClientOptions("http", secret)
39+
opts, _, err := m.getClientOptions(context.Background(), "http", secret)
3940
if err != nil {
4041
t.Fatalf("unexpected error: %v", err)
4142
}
@@ -46,7 +47,7 @@ func TestGetClientOptions_HTTPUsernamePassword(t *testing.T) {
4647

4748
func TestGetClientOptions_HTTPEmpty(t *testing.T) {
4849
m := &managerImpl{}
49-
opts, _, err := m.getClientOptions("https", nil)
50+
opts, _, err := m.getClientOptions(context.Background(), "https", nil)
5051
if err != nil {
5152
t.Fatalf("unexpected error: %v", err)
5253
}
@@ -57,7 +58,7 @@ func TestGetClientOptions_HTTPEmpty(t *testing.T) {
5758

5859
func TestGetClientOptions_SSHNoSecret(t *testing.T) {
5960
m := &managerImpl{}
60-
opts, _, err := m.getClientOptions(sshScheme, nil)
61+
opts, _, err := m.getClientOptions(context.Background(), sshScheme, nil)
6162
if err != nil {
6263
t.Fatalf("unexpected error: %v", err)
6364
}
@@ -68,7 +69,7 @@ func TestGetClientOptions_SSHNoSecret(t *testing.T) {
6869

6970
func TestGetClientOptions_SSHEmptySecret(t *testing.T) {
7071
m := &managerImpl{}
71-
opts, _, err := m.getClientOptions(sshScheme, map[string][]byte{})
72+
opts, _, err := m.getClientOptions(context.Background(), sshScheme, map[string][]byte{})
7273
if err != nil {
7374
t.Fatalf("unexpected error: %v", err)
7475
}
@@ -80,7 +81,7 @@ func TestGetClientOptions_SSHEmptySecret(t *testing.T) {
8081
func TestGetClientOptions_SSHWithPrivateKey(t *testing.T) {
8182
m := &managerImpl{}
8283
secret := map[string][]byte{"sshPrivateKey": []byte(testEd25519PrivateKey)}
83-
opts, _, err := m.getClientOptions(sshScheme, secret)
84+
opts, _, err := m.getClientOptions(context.Background(), sshScheme, secret)
8485
if err != nil {
8586
t.Fatalf("unexpected error: %v", err)
8687
}
@@ -92,7 +93,7 @@ func TestGetClientOptions_SSHWithPrivateKey(t *testing.T) {
9293
func TestGetClientOptions_SSHWithInvalidKey(t *testing.T) {
9394
m := &managerImpl{}
9495
secret := map[string][]byte{"sshPrivateKey": []byte("not-a-valid-key")}
95-
_, _, err := m.getClientOptions(sshScheme, secret)
96+
_, _, err := m.getClientOptions(context.Background(), sshScheme, secret)
9697
if err == nil {
9798
t.Fatal("expected error for invalid SSH key, got nil")
9899
}

0 commit comments

Comments
 (0)