github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/common/lin/ssh.go (about)

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