packer-cn/builder/azure/common/lin/ssh.go

39 lines
1.1 KiB
Go
Raw Normal View History

// Copyright (c) Microsoft Corporation. All rights reserved.
2016-03-10 20:46:22 -05:00
// Licensed under the MIT License. See the LICENSE file in builder/azure for license information.
package lin
import (
"fmt"
2016-03-10 20:46:22 -05:00
"github.com/mitchellh/multistep"
2016-03-10 20:46:22 -05:00
"github.com/mitchellh/packer/builder/azure/common/constants"
"golang.org/x/crypto/ssh"
)
func SSHHost(state multistep.StateBag) (string, error) {
host := state.Get(constants.SSHHost).(string)
return host, nil
}
// SSHConfig returns a function that can be used for the SSH communicator
// config for connecting to the instance created over SSH using the generated
// private key.
func SSHConfig(username string) func(multistep.StateBag) (*ssh.ClientConfig, error) {
return func(state multistep.StateBag) (*ssh.ClientConfig, error) {
privateKey := state.Get(constants.PrivateKey).(string)
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
return &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
}, nil
}
}