github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/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  
     9  	"github.com/mitchellh/multistep"
    10  	"github.com/mitchellh/packer/builder/azure/common/constants"
    11  	"golang.org/x/crypto/ssh"
    12  )
    13  
    14  func SSHHost(state multistep.StateBag) (string, error) {
    15  	host := state.Get(constants.SSHHost).(string)
    16  	return host, nil
    17  }
    18  
    19  // SSHConfig returns a function that can be used for the SSH communicator
    20  // config for connecting to the instance created over SSH using the generated
    21  // private key.
    22  func SSHConfig(username string) func(multistep.StateBag) (*ssh.ClientConfig, error) {
    23  	return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
    24  		privateKey := state.Get(constants.PrivateKey).(string)
    25  
    26  		signer, err := ssh.ParsePrivateKey([]byte(privateKey))
    27  		if err != nil {
    28  			return nil, fmt.Errorf("Error setting up SSH config: %s", err)
    29  		}
    30  
    31  		return &ssh.ClientConfig{
    32  			User: username,
    33  			Auth: []ssh.AuthMethod{
    34  				ssh.PublicKeys(signer),
    35  			},
    36  		}, nil
    37  	}
    38  }