Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: actually handle passphrase protected keys #326

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var loginCmd = &cobra.Command{
func publicKey(path string, skipAgent bool) (ssh.AuthMethod, func() error) {
noopCloseFunc := func() error { return nil }

if skipAgent != true {
if !skipAgent {
// Connect to SSH agent to ask for unencrypted private keys
if sshAgentConn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
sshAgent := agent.NewClient(sshAgentConn)
Expand All @@ -46,20 +46,23 @@ func publicKey(path string, skipAgent bool) (ssh.AuthMethod, func() error) {
// Try to look for an unencrypted private key
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
fmt.Println("Error was:", err.Error())
fmt.Println("Lagoon CLI cannot decode private keys, you will need to add your private key to your ssh-agent.")
os.Exit(1)
} else if err == nil {
// return unencrypted private key
fmt.Printf("Enter passphrase for %s:", path)
bytePassword, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
fmt.Println("Error was:", err.Error())
fmt.Println("Lagoon CLI could not decode private key, you will need to add your private key to your ssh-agent.")
os.Exit(1)
}
fmt.Println()
signer, err = ssh.ParsePrivateKeyWithPassphrase(key, bytePassword)
if err != nil {
fmt.Println("Error was:", err.Error())
fmt.Println("Lagoon CLI could not decode private key, you will need to add your private key to your ssh-agent.")
os.Exit(1)
}
return ssh.PublicKeys(signer), noopCloseFunc
}

// Handle encrypted private keys
fmt.Println("Found an encrypted private key!")
fmt.Printf("Enter passphrase for '%s': ", path)
bytePassword, err := terminal.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
signer, err = ssh.ParsePrivateKeyWithPassphrase(key, bytePassword)
// return unencrypted private key
return ssh.PublicKeys(signer), noopCloseFunc
}

Expand Down
Loading