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

     1  package lin
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/builder/azure/common/constants"
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  	"golang.org/x/crypto/ssh"
     9  )
    10  
    11  func SSHHost(state multistep.StateBag) (string, error) {
    12  	host := state.Get(constants.SSHHost).(string)
    13  	return host, nil
    14  }
    15  
    16  // SSHConfig returns a function that can be used for the SSH communicator
    17  // config for connecting to the instance created over SSH using the generated
    18  // private key.
    19  func SSHConfig(username string) func(multistep.StateBag) (*ssh.ClientConfig, error) {
    20  	return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
    21  		privateKey := state.Get(constants.PrivateKey).(string)
    22  
    23  		signer, err := ssh.ParsePrivateKey([]byte(privateKey))
    24  		if err != nil {
    25  			return nil, fmt.Errorf("Error setting up SSH config: %s", err)
    26  		}
    27  
    28  		return &ssh.ClientConfig{
    29  			User: username,
    30  			Auth: []ssh.AuthMethod{
    31  				ssh.PublicKeys(signer),
    32  			},
    33  			HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    34  		}, nil
    35  	}
    36  }