builder/*: extract key path to ssh.Signer
This commit is contained in:
parent
414bf1748d
commit
76a8221636
@ -1,12 +1,12 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.google.com/p/go.crypto/ssh"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"code.google.com/p/go.crypto/ssh"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
|
commonssh "github.com/mitchellh/packer/common/ssh"
|
||||||
packerssh "github.com/mitchellh/packer/communicator/ssh"
|
packerssh "github.com/mitchellh/packer/communicator/ssh"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func SSHAddress(state multistep.StateBag) (string, error) {
|
func SSHAddress(state multistep.StateBag) (string, error) {
|
||||||
@ -35,7 +35,7 @@ func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*ssh.ClientConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
if config.SSHKeyPath != "" {
|
if config.SSHKeyPath != "" {
|
||||||
signer, err := sshKeyToSigner(config.SSHKeyPath)
|
signer, err := commonssh.FileSigner(config.SSHKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -49,23 +49,3 @@ func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*ssh.ClientConfig
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sshKeyToSigner(path string) (ssh.Signer, error) {
|
|
||||||
f, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
keyBytes, err := ioutil.ReadAll(f)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
signer, err := ssh.ParsePrivateKey(keyBytes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return signer, nil
|
|
||||||
}
|
|
||||||
|
@ -3,9 +3,11 @@ package common
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/packer/packer"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
commonssh "github.com/mitchellh/packer/common/ssh"
|
||||||
|
"github.com/mitchellh/packer/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SSHConfig struct {
|
type SSHConfig struct {
|
||||||
@ -46,7 +48,7 @@ func (c *SSHConfig) Prepare(t *packer.ConfigTemplate) []error {
|
|||||||
if c.SSHKeyPath != "" {
|
if c.SSHKeyPath != "" {
|
||||||
if _, err := os.Stat(c.SSHKeyPath); err != nil {
|
if _, err := os.Stat(c.SSHKeyPath); err != nil {
|
||||||
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
||||||
} else if _, err := sshKeyToSigner(c.SSHKeyPath); err != nil {
|
} else if _, err := commonssh.FileSigner(c.SSHKeyPath); err != nil {
|
||||||
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,17 @@ package qemu
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/multistep"
|
|
||||||
"github.com/mitchellh/packer/common"
|
|
||||||
"github.com/mitchellh/packer/packer"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/mitchellh/multistep"
|
||||||
|
"github.com/mitchellh/packer/common"
|
||||||
|
commonssh "github.com/mitchellh/packer/common/ssh"
|
||||||
|
"github.com/mitchellh/packer/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
const BuilderId = "transcend.qemu"
|
const BuilderId = "transcend.qemu"
|
||||||
@ -352,7 +354,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|||||||
if _, err := os.Stat(b.config.SSHKeyPath); err != nil {
|
if _, err := os.Stat(b.config.SSHKeyPath); err != nil {
|
||||||
errs = packer.MultiErrorAppend(
|
errs = packer.MultiErrorAppend(
|
||||||
errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
||||||
} else if _, err := sshKeyToSigner(b.config.SSHKeyPath); err != nil {
|
} else if _, err := commonssh.FileSigner(b.config.SSHKeyPath); err != nil {
|
||||||
errs = packer.MultiErrorAppend(
|
errs = packer.MultiErrorAppend(
|
||||||
errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package qemu
|
package qemu
|
||||||
|
|
||||||
import (
|
import (
|
||||||
gossh "code.google.com/p/go.crypto/ssh"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
gossh "code.google.com/p/go.crypto/ssh"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
|
commonssh "github.com/mitchellh/packer/common/ssh"
|
||||||
"github.com/mitchellh/packer/communicator/ssh"
|
"github.com/mitchellh/packer/communicator/ssh"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func sshAddress(state multistep.StateBag) (string, error) {
|
func sshAddress(state multistep.StateBag) (string, error) {
|
||||||
@ -24,7 +24,7 @@ func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if config.SSHKeyPath != "" {
|
if config.SSHKeyPath != "" {
|
||||||
signer, err := sshKeyToSigner(config.SSHKeyPath)
|
signer, err := commonssh.FileSigner(config.SSHKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -37,23 +37,3 @@ func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
|||||||
Auth: auth,
|
Auth: auth,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func sshKeyToSigner(path string) (gossh.Signer, error) {
|
|
||||||
f, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
keyBytes, err := ioutil.ReadAll(f)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
signer, err := gossh.ParsePrivateKey(keyBytes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return signer, nil
|
|
||||||
}
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
gossh "code.google.com/p/go.crypto/ssh"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
gossh "code.google.com/p/go.crypto/ssh"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
|
commonssh "github.com/mitchellh/packer/common/ssh"
|
||||||
"github.com/mitchellh/packer/communicator/ssh"
|
"github.com/mitchellh/packer/communicator/ssh"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func SSHAddress(state multistep.StateBag) (string, error) {
|
func SSHAddress(state multistep.StateBag) (string, error) {
|
||||||
@ -23,7 +23,7 @@ func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*gossh.ClientConf
|
|||||||
}
|
}
|
||||||
|
|
||||||
if config.SSHKeyPath != "" {
|
if config.SSHKeyPath != "" {
|
||||||
signer, err := sshKeyToSigner(config.SSHKeyPath)
|
signer, err := commonssh.FileSigner(config.SSHKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -37,23 +37,3 @@ func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*gossh.ClientConf
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sshKeyToSigner(path string) (gossh.Signer, error) {
|
|
||||||
f, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
keyBytes, err := ioutil.ReadAll(f)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
signer, err := gossh.ParsePrivateKey(keyBytes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return signer, nil
|
|
||||||
}
|
|
||||||
|
@ -3,9 +3,11 @@ package common
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/packer/packer"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
commonssh "github.com/mitchellh/packer/common/ssh"
|
||||||
|
"github.com/mitchellh/packer/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SSHConfig struct {
|
type SSHConfig struct {
|
||||||
@ -56,7 +58,7 @@ func (c *SSHConfig) Prepare(t *packer.ConfigTemplate) []error {
|
|||||||
if c.SSHKeyPath != "" {
|
if c.SSHKeyPath != "" {
|
||||||
if _, err := os.Stat(c.SSHKeyPath); err != nil {
|
if _, err := os.Stat(c.SSHKeyPath); err != nil {
|
||||||
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
||||||
} else if _, err := sshKeyToSigner(c.SSHKeyPath); err != nil {
|
} else if _, err := commonssh.FileSigner(c.SSHKeyPath); err != nil {
|
||||||
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
gossh "code.google.com/p/go.crypto/ssh"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
gossh "code.google.com/p/go.crypto/ssh"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
|
commonssh "github.com/mitchellh/packer/common/ssh"
|
||||||
"github.com/mitchellh/packer/communicator/ssh"
|
"github.com/mitchellh/packer/communicator/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -74,7 +75,7 @@ func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientCon
|
|||||||
}
|
}
|
||||||
|
|
||||||
if config.SSHKeyPath != "" {
|
if config.SSHKeyPath != "" {
|
||||||
signer, err := sshKeyToSigner(config.SSHKeyPath)
|
signer, err := commonssh.FileSigner(config.SSHKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -88,23 +89,3 @@ func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientCon
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sshKeyToSigner(path string) (gossh.Signer, error) {
|
|
||||||
f, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
keyBytes, err := ioutil.ReadAll(f)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
signer, err := gossh.ParsePrivateKey(keyBytes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return signer, nil
|
|
||||||
}
|
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
commonssh "github.com/mitchellh/packer/common/ssh"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -51,7 +52,7 @@ func (c *SSHConfig) Prepare(t *packer.ConfigTemplate) []error {
|
|||||||
if c.SSHKeyPath != "" {
|
if c.SSHKeyPath != "" {
|
||||||
if _, err := os.Stat(c.SSHKeyPath); err != nil {
|
if _, err := os.Stat(c.SSHKeyPath); err != nil {
|
||||||
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
||||||
} else if _, err := sshKeyToSigner(c.SSHKeyPath); err != nil {
|
} else if _, err := commonssh.FileSigner(c.SSHKeyPath); err != nil {
|
||||||
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
30
common/ssh/key.go
Normal file
30
common/ssh/key.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package ssh
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"code.google.com/p/go.crypto/ssh"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FileSigner returns an ssh.Signer for a key file.
|
||||||
|
func FileSigner(path string) (ssh.Signer, error) {
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
keyBytes, err := ioutil.ReadAll(f)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
signer, err := ssh.ParsePrivateKey(keyBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return signer, nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user