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

     1  package profitbricks
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/communicator/ssh"
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  	gossh "golang.org/x/crypto/ssh"
     9  )
    10  
    11  func commHost(state multistep.StateBag) (string, error) {
    12  	ipAddress := state.Get("server_ip").(string)
    13  	return ipAddress, nil
    14  }
    15  
    16  func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
    17  	config := state.Get("config").(*Config)
    18  	var privateKey string
    19  
    20  	var auth []gossh.AuthMethod
    21  
    22  	if config.Comm.SSHPassword != "" {
    23  		auth = []gossh.AuthMethod{
    24  			gossh.Password(config.Comm.SSHPassword),
    25  			gossh.KeyboardInteractive(
    26  				ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
    27  		}
    28  	}
    29  
    30  	if config.Comm.SSHPrivateKey != "" {
    31  		if priv, ok := state.GetOk("privateKey"); ok {
    32  			privateKey = priv.(string)
    33  		}
    34  		signer, err := gossh.ParsePrivateKey([]byte(privateKey))
    35  		if err != nil {
    36  			return nil, fmt.Errorf("Error setting up SSH config: %s", err)
    37  		}
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  
    42  		auth = append(auth, gossh.PublicKeys(signer))
    43  	}
    44  	return &gossh.ClientConfig{
    45  		User:            config.Comm.SSHUsername,
    46  		Auth:            auth,
    47  		HostKeyCallback: gossh.InsecureIgnoreHostKey(),
    48  	}, nil
    49  }