checkAndUpdateOldKeyPair handles backward compatibility with the old keypair names. If the old public and private keys both exist they are renamed to the new name. The return value is non-nil only if the rename happens.
(sshContext ssh.Context)
| 424 | // If the old public and private keys both exist they are renamed to the new name. |
| 425 | // The return value is non-nil only if the rename happens. |
| 426 | func checkAndUpdateOldKeyPair(sshContext ssh.Context) *ssh.KeyPair { |
| 427 | publicKeys, err := sshContext.LocalPublicKeys() |
| 428 | if err != nil { |
| 429 | return nil |
| 430 | } |
| 431 | |
| 432 | for _, publicKey := range publicKeys { |
| 433 | if filepath.Base(publicKey) != automaticPrivateKeyNameOld+".pub" { |
| 434 | continue |
| 435 | } |
| 436 | |
| 437 | privateKey := strings.TrimSuffix(publicKey, ".pub") |
| 438 | _, err := os.Stat(privateKey) |
| 439 | if err != nil { |
| 440 | continue |
| 441 | } |
| 442 | |
| 443 | // Both old public and private keys exist, rename them to the new name |
| 444 | |
| 445 | sshDir := filepath.Dir(publicKey) |
| 446 | |
| 447 | publicKeyNew := filepath.Join(sshDir, automaticPrivateKeyName+".pub") |
| 448 | err = os.Rename(publicKey, publicKeyNew) |
| 449 | if err != nil { |
| 450 | return nil |
| 451 | } |
| 452 | |
| 453 | privateKeyNew := filepath.Join(sshDir, automaticPrivateKeyName) |
| 454 | err = os.Rename(privateKey, privateKeyNew) |
| 455 | if err != nil { |
| 456 | return nil |
| 457 | } |
| 458 | |
| 459 | keyPair := &ssh.KeyPair{ |
| 460 | PublicKeyPath: publicKeyNew, |
| 461 | PrivateKeyPath: privateKeyNew, |
| 462 | } |
| 463 | |
| 464 | return keyPair |
| 465 | } |
| 466 | |
| 467 | return nil |
| 468 | } |
| 469 | |
| 470 | // firstConfiguredKeyPair reads the effective configuration for a localhost |
| 471 | // connection and returns the first valid key pair which would be tried for authentication |
no test coverage detected