github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/alicloud/ecs/ssh_helper.go (about) 1 package ecs 2 3 import ( 4 "fmt" 5 "net" 6 "os" 7 "time" 8 9 packerssh "github.com/hashicorp/packer/communicator/ssh" 10 "github.com/hashicorp/packer/helper/multistep" 11 "golang.org/x/crypto/ssh" 12 "golang.org/x/crypto/ssh/agent" 13 ) 14 15 var ( 16 // modified in tests 17 sshHostSleepDuration = time.Second 18 ) 19 20 type alicloudSSHHelper interface { 21 } 22 23 // SSHHost returns a function that can be given to the SSH communicator 24 func SSHHost(e alicloudSSHHelper, private bool) func(multistep.StateBag) (string, error) { 25 return func(state multistep.StateBag) (string, error) { 26 ipAddress := state.Get("ipaddress").(string) 27 return ipAddress, nil 28 } 29 } 30 31 // SSHConfig returns a function that can be used for the SSH communicator 32 // config for connecting to the instance created over SSH using the private key 33 // or password. 34 func SSHConfig(useAgent bool, username, password string) func(multistep.StateBag) (*ssh.ClientConfig, error) { 35 return func(state multistep.StateBag) (*ssh.ClientConfig, error) { 36 if useAgent { 37 authSock := os.Getenv("SSH_AUTH_SOCK") 38 if authSock == "" { 39 return nil, fmt.Errorf("SSH_AUTH_SOCK is not set") 40 } 41 42 sshAgent, err := net.Dial("unix", authSock) 43 if err != nil { 44 return nil, fmt.Errorf("Cannot connect to SSH Agent socket %q: %s", authSock, err) 45 } 46 47 return &ssh.ClientConfig{ 48 User: username, 49 Auth: []ssh.AuthMethod{ 50 ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers), 51 }, 52 HostKeyCallback: ssh.InsecureIgnoreHostKey(), 53 }, nil 54 } 55 56 privateKey, hasKey := state.GetOk("privateKey") 57 if hasKey { 58 59 signer, err := ssh.ParsePrivateKey([]byte(privateKey.(string))) 60 if err != nil { 61 return nil, fmt.Errorf("Error setting up SSH config: %s", err) 62 } 63 return &ssh.ClientConfig{ 64 User: username, 65 Auth: []ssh.AuthMethod{ 66 ssh.PublicKeys(signer), 67 }, 68 HostKeyCallback: ssh.InsecureIgnoreHostKey(), 69 }, nil 70 71 } else { 72 return &ssh.ClientConfig{ 73 User: username, 74 Auth: []ssh.AuthMethod{ 75 ssh.Password(password), 76 ssh.KeyboardInteractive( 77 packerssh.PasswordKeyboardInteractive(password)), 78 }, 79 HostKeyCallback: ssh.InsecureIgnoreHostKey(), 80 }, nil 81 } 82 } 83 }