| 1 |
package ssh |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"reflect" |
| 8 |
"runtime" |
| 9 |
"slices" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/go-git/go-billy/v5/osfs" |
| 14 |
"github.com/go-git/go-billy/v5/util" |
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
"golang.org/x/crypto/ssh" |
| 18 |
"golang.org/x/crypto/ssh/testdata" |
| 19 |
|
| 20 |
. "gopkg.in/check.v1" |
| 21 |
) |
| 22 |
|
| 23 |
type ( |
| 24 |
SuiteCommon struct{} |
| 25 |
|
| 26 |
mockKnownHosts struct{} |
| 27 |
mockKnownHostsWithCert struct{} |
| 28 |
) |
| 29 |
|
| 30 |
func (mockKnownHosts) host() string { return "github.com" } |
| 31 |
func (mockKnownHosts) knownHosts() []byte { |
| 32 |
return []byte(`github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==`) |
| 33 |
} |
| 34 |
func (mockKnownHosts) Network() string { return "tcp" } |
| 35 |
func (mockKnownHosts) String() string { return "github.com:22" } |
| 36 |
func (mockKnownHosts) Algorithms() []string { |
| 37 |
return []string{ssh.KeyAlgoRSA, ssh.KeyAlgoRSASHA256, ssh.KeyAlgoRSASHA512} |
| 38 |
} |
| 39 |
|
| 40 |
func (mockKnownHostsWithCert) host() string { return "github.com" } |
| 41 |
func (mockKnownHostsWithCert) knownHosts() []byte { |
| 42 |
return []byte(`@cert-authority github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==`) |
| 43 |
} |
| 44 |
func (mockKnownHostsWithCert) Network() string { return "tcp" } |
| 45 |
func (mockKnownHostsWithCert) String() string { return "github.com:22" } |
| 46 |
func (mockKnownHostsWithCert) Algorithms() []string { |
| 47 |
return []string{ssh.CertAlgoRSASHA512v01, ssh.CertAlgoRSASHA256v01, ssh.CertAlgoRSAv01} |
| 48 |
} |
| 49 |
|
| 50 |
var _ = Suite(&SuiteCommon{}) |
| 51 |
|
| 52 |
func (s *SuiteCommon) TestKeyboardInteractiveName(c *C) { |
| 53 |
a := &KeyboardInteractive{ |
| 54 |
User: "test", |
| 55 |
Challenge: nil, |
| 56 |
} |
| 57 |
c.Assert(a.Name(), Equals, KeyboardInteractiveName) |
| 58 |
} |
| 59 |
|
| 60 |
func (s *SuiteCommon) TestKeyboardInteractiveString(c *C) { |
| 61 |
a := &KeyboardInteractive{ |
| 62 |
User: "test", |
| 63 |
Challenge: nil, |
| 64 |
} |
| 65 |
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", KeyboardInteractiveName)) |
| 66 |
} |
| 67 |
|
| 68 |
func (s *SuiteCommon) TestPasswordName(c *C) { |
| 69 |
a := &Password{ |
| 70 |
User: "test", |
| 71 |
Password: "", |
| 72 |
} |
| 73 |
c.Assert(a.Name(), Equals, PasswordName) |
| 74 |
} |
| 75 |
|
| 76 |
func (s *SuiteCommon) TestPasswordString(c *C) { |
| 77 |
a := &Password{ |
| 78 |
User: "test", |
| 79 |
Password: "", |
| 80 |
} |
| 81 |
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PasswordName)) |
| 82 |
} |
| 83 |
|
| 84 |
func (s *SuiteCommon) TestPasswordCallbackName(c *C) { |
| 85 |
a := &PasswordCallback{ |
| 86 |
User: "test", |
| 87 |
Callback: nil, |
| 88 |
} |
| 89 |
c.Assert(a.Name(), Equals, PasswordCallbackName) |
| 90 |
} |
| 91 |
|
| 92 |
func (s *SuiteCommon) TestPasswordCallbackString(c *C) { |
| 93 |
a := &PasswordCallback{ |
| 94 |
User: "test", |
| 95 |
Callback: nil, |
| 96 |
} |
| 97 |
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PasswordCallbackName)) |
| 98 |
} |
| 99 |
|
| 100 |
func (s *SuiteCommon) TestPublicKeysName(c *C) { |
| 101 |
a := &PublicKeys{ |
| 102 |
User: "test", |
| 103 |
Signer: nil, |
| 104 |
} |
| 105 |
c.Assert(a.Name(), Equals, PublicKeysName) |
| 106 |
} |
| 107 |
|
| 108 |
func (s *SuiteCommon) TestPublicKeysString(c *C) { |
| 109 |
a := &PublicKeys{ |
| 110 |
User: "test", |
| 111 |
Signer: nil, |
| 112 |
} |
| 113 |
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PublicKeysName)) |
| 114 |
} |
| 115 |
|
| 116 |
func (s *SuiteCommon) TestPublicKeysCallbackName(c *C) { |
| 117 |
a := &PublicKeysCallback{ |
| 118 |
User: "test", |
| 119 |
Callback: nil, |
| 120 |
} |
| 121 |
c.Assert(a.Name(), Equals, PublicKeysCallbackName) |
| 122 |
} |
| 123 |
|
| 124 |
func (s *SuiteCommon) TestPublicKeysCallbackString(c *C) { |
| 125 |
a := &PublicKeysCallback{ |
| 126 |
User: "test", |
| 127 |
Callback: nil, |
| 128 |
} |
| 129 |
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PublicKeysCallbackName)) |
| 130 |
} |
| 131 |
func (s *SuiteCommon) TestNewSSHAgentAuth(c *C) { |
| 132 |
if runtime.GOOS == "js" { |
| 133 |
c.Skip("tcp connections are not available in wasm") |
| 134 |
} |
| 135 |
|
| 136 |
if os.Getenv("SSH_AUTH_SOCK") == "" { |
| 137 |
c.Skip("SSH_AUTH_SOCK or SSH_TEST_PRIVATE_KEY are required") |
| 138 |
} |
| 139 |
|
| 140 |
auth, err := NewSSHAgentAuth("foo") |
| 141 |
c.Assert(err, IsNil) |
| 142 |
c.Assert(auth, NotNil) |
| 143 |
} |
| 144 |
|
| 145 |
func (s *SuiteCommon) TestNewSSHAgentAuthNoAgent(c *C) { |
| 146 |
addr := os.Getenv("SSH_AUTH_SOCK") |
| 147 |
err := os.Unsetenv("SSH_AUTH_SOCK") |
| 148 |
c.Assert(err, IsNil) |
| 149 |
|
| 150 |
defer func() { |
| 151 |
err := os.Setenv("SSH_AUTH_SOCK", addr) |
| 152 |
c.Assert(err, IsNil) |
| 153 |
}() |
| 154 |
|
| 155 |
k, err := NewSSHAgentAuth("foo") |
| 156 |
c.Assert(k, IsNil) |
| 157 |
c.Assert(err, ErrorMatches, ".*SSH_AUTH_SOCK.*|.*SSH agent .* not detect.*") |
| 158 |
} |
| 159 |
|
| 160 |
func (*SuiteCommon) TestNewPublicKeys(c *C) { |
| 161 |
auth, err := NewPublicKeys("foo", testdata.PEMBytes["rsa"], "") |
| 162 |
c.Assert(err, IsNil) |
| 163 |
c.Assert(auth, NotNil) |
| 164 |
} |
| 165 |
|
| 166 |
func (*SuiteCommon) TestNewPublicKeysWithEncryptedPEM(c *C) { |
| 167 |
f := testdata.PEMEncryptedKeys[0] |
| 168 |
auth, err := NewPublicKeys("foo", f.PEMBytes, f.EncryptionKey) |
| 169 |
c.Assert(err, IsNil) |
| 170 |
c.Assert(auth, NotNil) |
| 171 |
} |
| 172 |
|
| 173 |
func (*SuiteCommon) TestNewPublicKeysWithEncryptedEd25519PEM(c *C) { |
| 174 |
f := testdata.PEMEncryptedKeys[2] |
| 175 |
auth, err := NewPublicKeys("foo", f.PEMBytes, f.EncryptionKey) |
| 176 |
c.Assert(err, IsNil) |
| 177 |
c.Assert(auth, NotNil) |
| 178 |
} |
| 179 |
|
| 180 |
func (*SuiteCommon) TestNewPublicKeysFromFile(c *C) { |
| 181 |
if runtime.GOOS == "js" { |
| 182 |
c.Skip("not available in wasm") |
| 183 |
} |
| 184 |
|
| 185 |
f, err := util.TempFile(osfs.Default, "", "ssh-test") |
| 186 |
c.Assert(err, IsNil) |
| 187 |
_, err = f.Write(testdata.PEMBytes["rsa"]) |
| 188 |
c.Assert(err, IsNil) |
| 189 |
c.Assert(f.Close(), IsNil) |
| 190 |
defer osfs.Default.Remove(f.Name()) |
| 191 |
|
| 192 |
auth, err := NewPublicKeysFromFile("foo", f.Name(), "") |
| 193 |
c.Assert(err, IsNil) |
| 194 |
c.Assert(auth, NotNil) |
| 195 |
} |
| 196 |
|
| 197 |
func (*SuiteCommon) TestNewPublicKeysWithInvalidPEM(c *C) { |
| 198 |
auth, err := NewPublicKeys("foo", []byte("bar"), "") |
| 199 |
c.Assert(err, NotNil) |
| 200 |
c.Assert(auth, IsNil) |
| 201 |
} |
| 202 |
|
| 203 |
func (*SuiteCommon) TestNewKnownHostsCallback(c *C) { |
| 204 |
if runtime.GOOS == "js" { |
| 205 |
c.Skip("not available in wasm") |
| 206 |
} |
| 207 |
|
| 208 |
var mock = mockKnownHosts{} |
| 209 |
|
| 210 |
f, err := util.TempFile(osfs.Default, "", "known-hosts") |
| 211 |
c.Assert(err, IsNil) |
| 212 |
|
| 213 |
_, err = f.Write(mock.knownHosts()) |
| 214 |
c.Assert(err, IsNil) |
| 215 |
|
| 216 |
err = f.Close() |
| 217 |
c.Assert(err, IsNil) |
| 218 |
|
| 219 |
defer util.RemoveAll(osfs.Default, f.Name()) |
| 220 |
|
| 221 |
f, err = osfs.Default.Open(f.Name()) |
| 222 |
c.Assert(err, IsNil) |
| 223 |
|
| 224 |
defer f.Close() |
| 225 |
|
| 226 |
var hostKey ssh.PublicKey |
| 227 |
scanner := bufio.NewScanner(f) |
| 228 |
for scanner.Scan() { |
| 229 |
fields := strings.Split(scanner.Text(), " ") |
| 230 |
if len(fields) != 3 { |
| 231 |
continue |
| 232 |
} |
| 233 |
if strings.Contains(fields[0], mock.host()) { |
| 234 |
var err error |
| 235 |
hostKey, _, _, _, err = ssh.ParseAuthorizedKey(scanner.Bytes()) |
| 236 |
if err != nil { |
| 237 |
c.Fatalf("error parsing %q: %v", fields[2], err) |
| 238 |
} |
| 239 |
break |
| 240 |
} |
| 241 |
} |
| 242 |
if hostKey == nil { |
| 243 |
c.Fatalf("no hostkey for %s", mock.host()) |
| 244 |
} |
| 245 |
|
| 246 |
clb, err := NewKnownHostsCallback(f.Name()) |
| 247 |
c.Assert(err, IsNil) |
| 248 |
|
| 249 |
err = clb(mock.String(), mock, hostKey) |
| 250 |
c.Assert(err, IsNil) |
| 251 |
} |
| 252 |
|
| 253 |
func (*SuiteCommon) TestNewKnownHostsDbWithoutCert(c *C) { |
| 254 |
if runtime.GOOS == "js" { |
| 255 |
c.Skip("not available in wasm") |
| 256 |
} |
| 257 |
|
| 258 |
var mock = mockKnownHosts{} |
| 259 |
|
| 260 |
f, err := util.TempFile(osfs.Default, "", "known-hosts") |
| 261 |
c.Assert(err, IsNil) |
| 262 |
|
| 263 |
_, err = f.Write(mock.knownHosts()) |
| 264 |
c.Assert(err, IsNil) |
| 265 |
|
| 266 |
err = f.Close() |
| 267 |
c.Assert(err, IsNil) |
| 268 |
|
| 269 |
defer util.RemoveAll(osfs.Default, f.Name()) |
| 270 |
|
| 271 |
f, err = osfs.Default.Open(f.Name()) |
| 272 |
c.Assert(err, IsNil) |
| 273 |
|
| 274 |
defer f.Close() |
| 275 |
|
| 276 |
db, err := NewKnownHostsDb(f.Name()) |
| 277 |
c.Assert(err, IsNil) |
| 278 |
|
| 279 |
algos := db.HostKeyAlgorithms(mock.String()) |
| 280 |
c.Assert(algos, HasLen, len(mock.Algorithms())) |
| 281 |
|
| 282 |
for _, algorithm := range mock.Algorithms() { |
| 283 |
if !slices.Contains(algos, algorithm) { |
| 284 |
c.Error("algos does not contain ", algorithm) |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
func (*SuiteCommon) TestNewKnownHostsDbWithCert(c *C) { |
| 290 |
if runtime.GOOS == "js" { |
| 291 |
c.Skip("not available in wasm") |
| 292 |
} |
| 293 |
|
| 294 |
var mock = mockKnownHostsWithCert{} |
| 295 |
|
| 296 |
f, err := util.TempFile(osfs.Default, "", "known-hosts") |
| 297 |
c.Assert(err, IsNil) |
| 298 |
|
| 299 |
_, err = f.Write(mock.knownHosts()) |
| 300 |
c.Assert(err, IsNil) |
| 301 |
|
| 302 |
err = f.Close() |
| 303 |
c.Assert(err, IsNil) |
| 304 |
|
| 305 |
defer util.RemoveAll(osfs.Default, f.Name()) |
| 306 |
|
| 307 |
f, err = osfs.Default.Open(f.Name()) |
| 308 |
c.Assert(err, IsNil) |
| 309 |
|
| 310 |
defer f.Close() |
| 311 |
|
| 312 |
db, err := NewKnownHostsDb(f.Name()) |
| 313 |
c.Assert(err, IsNil) |
| 314 |
|
| 315 |
algos := db.HostKeyAlgorithms(mock.String()) |
| 316 |
c.Assert(algos, HasLen, len(mock.Algorithms())) |
| 317 |
|
| 318 |
for _, algorithm := range mock.Algorithms() { |
| 319 |
if !slices.Contains(algos, algorithm) { |
| 320 |
c.Error("algos does not contain ", algorithm) |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
func TestHostKeyCallbackHelper(t *testing.T) { |
| 326 |
cb1 := ssh.FixedHostKey(nil) |
| 327 |
tests := []struct { |
| 328 |
name string |
| 329 |
cb ssh.HostKeyCallback |
| 330 |
algos []string |
| 331 |
fallback func(files ...string) (ssh.HostKeyCallback, error) |
| 332 |
cc *ssh.ClientConfig |
| 333 |
want *ssh.ClientConfig |
| 334 |
wantErr string |
| 335 |
}{ |
| 336 |
{ |
| 337 |
name: "keep existing callback if set", |
| 338 |
cb: cb1, |
| 339 |
cc: &ssh.ClientConfig{}, |
| 340 |
want: &ssh.ClientConfig{ |
| 341 |
HostKeyCallback: cb1, |
| 342 |
}, |
| 343 |
}, |
| 344 |
{ |
| 345 |
name: "create new client config is one isn't provided", |
| 346 |
cb: cb1, |
| 347 |
cc: nil, |
| 348 |
want: &ssh.ClientConfig{ |
| 349 |
HostKeyCallback: cb1, |
| 350 |
}, |
| 351 |
}, |
| 352 |
{ |
| 353 |
name: "respect pre-set algos", |
| 354 |
cb: cb1, |
| 355 |
algos: []string{"foo"}, |
| 356 |
cc: &ssh.ClientConfig{}, |
| 357 |
want: &ssh.ClientConfig{ |
| 358 |
HostKeyCallback: cb1, |
| 359 |
HostKeyAlgorithms: []string{"foo"}, |
| 360 |
}, |
| 361 |
}, |
| 362 |
{ |
| 363 |
name: "no callback is set, call fallback", |
| 364 |
cc: &ssh.ClientConfig{}, |
| 365 |
fallback: func(files ...string) (ssh.HostKeyCallback, error) { |
| 366 |
return cb1, nil |
| 367 |
}, |
| 368 |
want: &ssh.ClientConfig{ |
| 369 |
HostKeyCallback: cb1, |
| 370 |
}, |
| 371 |
}, |
| 372 |
{ |
| 373 |
name: "no callback is set with nil client config", |
| 374 |
fallback: func(files ...string) (ssh.HostKeyCallback, error) { |
| 375 |
return cb1, nil |
| 376 |
}, |
| 377 |
want: &ssh.ClientConfig{ |
| 378 |
HostKeyCallback: cb1, |
| 379 |
}, |
| 380 |
}, |
| 381 |
{ |
| 382 |
name: "algos with no callback, call fallback", |
| 383 |
algos: []string{"bar"}, |
| 384 |
cc: &ssh.ClientConfig{}, |
| 385 |
fallback: func(files ...string) (ssh.HostKeyCallback, error) { |
| 386 |
return cb1, nil |
| 387 |
}, |
| 388 |
want: &ssh.ClientConfig{ |
| 389 |
HostKeyCallback: cb1, |
| 390 |
HostKeyAlgorithms: []string{"bar"}, |
| 391 |
}, |
| 392 |
}, |
| 393 |
} |
| 394 |
|
| 395 |
for _, tc := range tests { |
| 396 |
t.Run(tc.name, func(t *testing.T) { |
| 397 |
helper := HostKeyCallbackHelper{ |
| 398 |
HostKeyCallback: tc.cb, |
| 399 |
HostKeyAlgorithms: tc.algos, |
| 400 |
fallback: tc.fallback, |
| 401 |
} |
| 402 |
|
| 403 |
got, gotErr := helper.SetHostKeyCallback(tc.cc) |
| 404 |
|
| 405 |
if tc.wantErr == "" { |
| 406 |
require.NoError(t, gotErr) |
| 407 |
require.NotNil(t, got) |
| 408 |
|
| 409 |
wantFunc := runtime.FuncForPC(reflect.ValueOf(tc.want.HostKeyCallback).Pointer()).Name() |
| 410 |
gotFunc := runtime.FuncForPC(reflect.ValueOf(got.HostKeyCallback).Pointer()).Name() |
| 411 |
assert.Equal(t, wantFunc, gotFunc) |
| 412 |
|
| 413 |
assert.Equal(t, tc.want.HostKeyAlgorithms, got.HostKeyAlgorithms) |
| 414 |
} else { |
| 415 |
assert.ErrorContains(t, gotErr, tc.wantErr) |
| 416 |
assert.Nil(t, got) |
| 417 |
} |
| 418 |
}) |
| 419 |
} |
| 420 |
} |