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

Support OpenSSH new version key format #513

Merged
merged 11 commits into from
Dec 13, 2019
Merged
  •  
  •  
  •  
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
cli
build
*swp
bin
pkg
scalingo/scalingo
scalingo/scalingo.exe
src/github.com
src/code.google.com
Godeps/Readme

# Executable
scalingo-cli
main

# IDE
.idea/
29 changes: 24 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
name = "github.com/Scalingo/go-utils"
version = "^5"

[[constraint]]
name = "github.com/ScaleFT/sshkeys"
branch = "master"

[[constraint]]
name = "github.com/urfave/cli"
brandon-welsch marked this conversation as resolved.
Show resolved Hide resolved
version = "^1"

[prune]
non-go = true
go-tests = true
Expand Down
115 changes: 0 additions & 115 deletions crypto/sshkeys/decrypt.go

This file was deleted.

56 changes: 0 additions & 56 deletions crypto/sshkeys/decrypt_test.go

This file was deleted.

43 changes: 27 additions & 16 deletions crypto/sshkeys/private_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package sshkeys

import (
"encoding/pem"
"strings"

"github.com/ScaleFT/sshkeys"
"golang.org/x/crypto/ssh"
)
"gopkg.in/errgo.v1"

var (
implementedCiphers = []string{"DES-EDE3-CBC", "AES-128-CBC"}
"github.com/Scalingo/cli/term"
)

type PrivateKey struct {
Expand All @@ -16,27 +17,37 @@ type PrivateKey struct {
PasswordMethod
}

type PasswordMethod func(prompt string) (string, error)

func (p *PrivateKey) Signer() (ssh.Signer, error) {
if p.IsEncrypted() {
if p.PasswordMethod == nil {
p.PasswordMethod = term.Password
}

password, err := p.PasswordMethod("Encrypted SSH Key, password: ")
if err != nil {
return nil, errgo.Mask(err)
brandon-welsch marked this conversation as resolved.
Show resolved Hide resolved
}

return sshkeys.ParseEncryptedPrivateKey(pem.EncodeToMemory(p.Block), []byte(password))
}

return ssh.ParsePrivateKey(pem.EncodeToMemory(p.Block))
}

func (p *PrivateKey) IsEncrypted() bool {
return p.Block.Headers["Proc-Type"] == "4,ENCRYPTED"
return p.Block.Headers["Proc-Type"] == "4,ENCRYPTED" || p.isOpenSSHEncrypted()
brandon-welsch marked this conversation as resolved.
Show resolved Hide resolved
}

func (p *PrivateKey) IsCipherImplemented(cipher string) bool {
for _, c := range implementedCiphers {
if c == cipher {
return true
}
func (p *PrivateKey) isOpenSSHEncrypted() bool {
brandon-welsch marked this conversation as resolved.
Show resolved Hide resolved
if p.Block.Type != "OPENSSH PRIVATE KEY" {
return false
}
return false
}

func DummyPasswordMethod(password string) PasswordMethod {
return func(prompt string) (string, error) {
return prompt, nil
_, err := ssh.ParseRawPrivateKey(pem.EncodeToMemory(p.Block))
if err != nil {
return strings.Contains(err.Error(), "cannot decode encrypted private keys")
}
return false
}

type PasswordMethod func(prompt string) (string, error)
Loading