github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/scaleway/ssh.go (about)

     1  package scaleway
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"os"
     7  
     8  	packerssh "github.com/hashicorp/packer/communicator/ssh"
     9  	"github.com/hashicorp/packer/helper/multistep"
    10  	"golang.org/x/crypto/ssh"
    11  	"golang.org/x/crypto/ssh/agent"
    12  )
    13  
    14  func commHost(state multistep.StateBag) (string, error) {
    15  	ipAddress := state.Get("server_ip").(string)
    16  	return ipAddress, nil
    17  }
    18  
    19  func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
    20  	config := state.Get("config").(Config)
    21  
    22  	var auth []ssh.AuthMethod
    23  
    24  	if config.Comm.SSHAgentAuth {
    25  		authSock := os.Getenv("SSH_AUTH_SOCK")
    26  		if authSock == "" {
    27  			return nil, fmt.Errorf("SSH_AUTH_SOCK is not set")
    28  		}
    29  
    30  		sshAgent, err := net.Dial("unix", authSock)
    31  		if err != nil {
    32  			return nil, fmt.Errorf("Cannot connect to SSH Agent socket %q: %s", authSock, err)
    33  		}
    34  		auth = []ssh.AuthMethod{
    35  			ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers),
    36  		}
    37  	}
    38  
    39  	if config.Comm.SSHPassword != "" {
    40  		auth = append(auth,
    41  			ssh.Password(config.Comm.SSHPassword),
    42  			ssh.KeyboardInteractive(
    43  				packerssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
    44  		)
    45  	}
    46  
    47  	// Use key based auth if there is a private key in the state bag
    48  	if privateKey, ok := state.GetOk("private_key"); ok {
    49  		signer, err := ssh.ParsePrivateKey([]byte(privateKey.(string)))
    50  		if err != nil {
    51  			return nil, fmt.Errorf("Error setting up SSH config: %s", err)
    52  		}
    53  		auth = append(auth, ssh.PublicKeys(signer))
    54  	}
    55  
    56  	return &ssh.ClientConfig{
    57  		User:            config.Comm.SSHUsername,
    58  		Auth:            auth,
    59  		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    60  	}, nil
    61  }