MCPcopy Index your code
hub / github.com/cli/cli / checkAndUpdateOldKeyPair

Function checkAndUpdateOldKeyPair

pkg/cmd/codespace/ssh.go:426–468  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
426func 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

Callers 1

generateAutomaticSSHKeysFunction · 0.85

Calls 3

LocalPublicKeysMethod · 0.80
BaseMethod · 0.80
JoinMethod · 0.80

Tested by

no test coverage detected