2015-06-26 18:08:03 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2015-08-06 18:48:19 +00:00
|
|
|
"bytes"
|
2015-06-26 18:08:03 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
2015-06-30 02:00:08 +00:00
|
|
|
"io"
|
2015-07-02 01:26:42 +00:00
|
|
|
"net"
|
|
|
|
"strings"
|
2015-08-06 18:48:19 +00:00
|
|
|
"time"
|
2015-07-02 01:26:42 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
2015-06-26 18:08:03 +00:00
|
|
|
|
2015-08-06 18:48:19 +00:00
|
|
|
commssh "github.com/mitchellh/packer/communicator/ssh"
|
2015-06-26 18:08:03 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
2015-07-27 20:42:03 +00:00
|
|
|
// Transfers the file to the target machine by establishing an SSH
|
|
|
|
// session with the target. Uses the public key authentication method
|
|
|
|
// and hence the parameter 'key' takes in the private key. The fileName
|
|
|
|
// parameter takes an absolute path.
|
2015-07-29 18:21:36 +00:00
|
|
|
func uploadPublicKeyScp(publicKey, publicKeyFileName, username, ip, port, key string) error {
|
2015-07-06 20:56:45 +00:00
|
|
|
session, err := createSSHPublicKeysSession(username, ip, port, key)
|
2015-06-30 02:00:08 +00:00
|
|
|
if err != nil {
|
2015-06-30 20:30:13 +00:00
|
|
|
return err
|
2015-06-30 02:00:08 +00:00
|
|
|
}
|
|
|
|
if session == nil {
|
2015-07-02 01:05:52 +00:00
|
|
|
return fmt.Errorf("invalid session object")
|
2015-06-30 02:00:08 +00:00
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
go func() {
|
|
|
|
w, _ := session.StdinPipe()
|
2015-07-29 18:21:36 +00:00
|
|
|
fmt.Fprintln(w, "C0644", len(publicKey), publicKeyFileName)
|
2015-07-02 21:23:09 +00:00
|
|
|
io.Copy(w, strings.NewReader(publicKey))
|
2015-06-30 02:00:08 +00:00
|
|
|
fmt.Fprint(w, "\x00")
|
|
|
|
w.Close()
|
|
|
|
}()
|
2015-07-31 17:24:23 +00:00
|
|
|
session.Run(fmt.Sprintf("scp -vt %s", publicKeyFileName))
|
2015-06-30 02:00:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-27 20:42:03 +00:00
|
|
|
// Creates a SSH session object which can be used to run commands
|
|
|
|
// in the target machine. The session will use public key authentication
|
|
|
|
// method with port 22.
|
2015-07-06 20:56:45 +00:00
|
|
|
func createSSHPublicKeysSession(username, ipAddr, port, hostKey string) (*ssh.Session, error) {
|
2015-07-02 01:26:42 +00:00
|
|
|
if username == "" {
|
|
|
|
return nil, fmt.Errorf("missing username")
|
|
|
|
}
|
|
|
|
if ipAddr == "" {
|
|
|
|
return nil, fmt.Errorf("missing ip address")
|
|
|
|
}
|
|
|
|
if hostKey == "" {
|
|
|
|
return nil, fmt.Errorf("missing host key")
|
2015-06-30 20:30:13 +00:00
|
|
|
}
|
2015-06-26 18:08:03 +00:00
|
|
|
signer, err := ssh.ParsePrivateKey([]byte(hostKey))
|
|
|
|
if err != nil {
|
2015-07-02 01:05:52 +00:00
|
|
|
return nil, fmt.Errorf("parsing Private Key failed: %s", err)
|
2015-06-26 18:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := &ssh.ClientConfig{
|
|
|
|
User: username,
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.PublicKeys(signer),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:56:45 +00:00
|
|
|
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", ipAddr, port), config)
|
2015-06-26 18:08:03 +00:00
|
|
|
if err != nil {
|
2015-06-30 20:30:13 +00:00
|
|
|
return nil, err
|
2015-06-26 18:08:03 +00:00
|
|
|
}
|
|
|
|
if client == nil {
|
2015-07-02 01:05:52 +00:00
|
|
|
return nil, fmt.Errorf("invalid client object: %s", err)
|
2015-06-26 18:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
session, err := client.NewSession()
|
|
|
|
if err != nil {
|
2015-06-30 20:30:13 +00:00
|
|
|
return nil, err
|
2015-06-26 18:08:03 +00:00
|
|
|
}
|
2015-06-30 02:00:08 +00:00
|
|
|
return session, nil
|
2015-06-26 18:08:03 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 21:23:09 +00:00
|
|
|
// Creates a new RSA key pair with key length of 2048.
|
2015-07-27 20:42:03 +00:00
|
|
|
// The private key will be of pem format and the public key will be
|
|
|
|
// of OpenSSH format.
|
2015-07-29 18:21:36 +00:00
|
|
|
func generateRSAKeys(keyBits int) (publicKeyRsa string, privateKeyRsa string, err error) {
|
|
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, keyBits)
|
2015-06-26 18:08:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("error generating RSA key-pair: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:30:13 +00:00
|
|
|
privateKeyRsa = string(pem.EncodeToMemory(&pem.Block{
|
2015-06-26 18:08:03 +00:00
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
|
|
|
}))
|
|
|
|
|
|
|
|
sshPublicKey, err := ssh.NewPublicKey(privateKey.Public())
|
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("error generating RSA key-pair: %s", err)
|
|
|
|
}
|
2015-06-30 20:30:13 +00:00
|
|
|
publicKeyRsa = "ssh-rsa " + base64.StdEncoding.EncodeToString(sshPublicKey.Marshal())
|
|
|
|
return
|
2015-06-26 18:08:03 +00:00
|
|
|
}
|
2015-07-02 01:26:42 +00:00
|
|
|
|
2015-07-27 20:42:03 +00:00
|
|
|
// Concatenates the public present in that target machine's home
|
|
|
|
// folder to ~/.ssh/authorized_keys file
|
2015-08-06 18:48:19 +00:00
|
|
|
func installPublicKeyInTarget(adminUser, publicKeyFileName, username, ip, port, hostkey string) error {
|
|
|
|
session, err := createSSHPublicKeysSession(adminUser, ip, port, hostkey)
|
2015-07-02 21:23:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create SSH Session using public keys: %s", err)
|
|
|
|
}
|
|
|
|
if session == nil {
|
|
|
|
return fmt.Errorf("invalid session object")
|
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
|
2015-07-10 22:18:02 +00:00
|
|
|
authKeysFileName := fmt.Sprintf("/home/%s/.ssh/authorized_keys", username)
|
2015-08-06 19:50:12 +00:00
|
|
|
scriptFileName := fmt.Sprintf("%s.sh", publicKeyFileName)
|
2015-07-02 21:23:09 +00:00
|
|
|
|
2015-08-06 18:48:19 +00:00
|
|
|
// Give execute permissions to install script, run and delete it.
|
|
|
|
chmodCmd := fmt.Sprintf("chmod +x %s", scriptFileName)
|
2015-08-06 19:50:12 +00:00
|
|
|
scriptCmd := fmt.Sprintf("./%s install %s %s", scriptFileName, publicKeyFileName, authKeysFileName)
|
2015-08-06 18:48:19 +00:00
|
|
|
rmCmd := fmt.Sprintf("rm -f %s", scriptFileName)
|
|
|
|
targetCmd := fmt.Sprintf("%s;%s;%s", chmodCmd, scriptCmd, rmCmd)
|
2015-07-10 15:56:14 +00:00
|
|
|
|
|
|
|
session.Run(targetCmd)
|
2015-07-02 21:23:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-27 20:42:03 +00:00
|
|
|
// Removes the installed public key from the authorized_keys file
|
|
|
|
// in target machine
|
2015-07-29 18:21:36 +00:00
|
|
|
func uninstallPublicKeyInTarget(adminUser, publicKeyFileName, username, ip, port, hostKey string) error {
|
2015-07-27 19:03:10 +00:00
|
|
|
session, err := createSSHPublicKeysSession(adminUser, ip, port, hostKey)
|
2015-07-02 21:23:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create SSH Session using public keys: %s", err)
|
|
|
|
}
|
|
|
|
if session == nil {
|
|
|
|
return fmt.Errorf("invalid session object")
|
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
|
2015-07-10 22:18:02 +00:00
|
|
|
authKeysFileName := fmt.Sprintf("/home/%s/.ssh/authorized_keys", username)
|
2015-08-06 19:50:12 +00:00
|
|
|
scriptFileName := fmt.Sprintf("%s.sh", publicKeyFileName)
|
2015-07-02 21:23:09 +00:00
|
|
|
|
2015-08-06 19:50:12 +00:00
|
|
|
// Give execute permissions to install script, run and delete it.
|
|
|
|
chmodCmd := fmt.Sprintf("chmod +x %s", scriptFileName)
|
|
|
|
scriptCmd := fmt.Sprintf("./%s uninstall %s %s", scriptFileName, publicKeyFileName, authKeysFileName)
|
|
|
|
rmCmd := fmt.Sprintf("rm -f %s", scriptFileName)
|
|
|
|
targetCmd := fmt.Sprintf("%s;%s;%s", chmodCmd, scriptCmd, rmCmd)
|
|
|
|
|
|
|
|
session.Run(targetCmd)
|
2015-07-02 21:23:09 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-27 20:42:03 +00:00
|
|
|
// Takes an IP address and role name and checks if the IP is part
|
|
|
|
// of CIDR blocks belonging to the role.
|
2015-07-02 21:23:09 +00:00
|
|
|
func roleContainsIP(s logical.Storage, roleName string, ip string) (bool, error) {
|
2015-07-02 01:26:42 +00:00
|
|
|
if roleName == "" {
|
|
|
|
return false, fmt.Errorf("missing role name")
|
|
|
|
}
|
2015-07-27 20:42:03 +00:00
|
|
|
|
2015-07-02 01:26:42 +00:00
|
|
|
if ip == "" {
|
|
|
|
return false, fmt.Errorf("missing ip")
|
|
|
|
}
|
2015-07-27 20:42:03 +00:00
|
|
|
|
2015-07-02 01:26:42 +00:00
|
|
|
roleEntry, err := s.Get(fmt.Sprintf("policy/%s", roleName))
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error retrieving role '%s'", err)
|
|
|
|
}
|
|
|
|
if roleEntry == nil {
|
|
|
|
return false, fmt.Errorf("role '%s' not found", roleName)
|
|
|
|
}
|
2015-07-27 20:42:03 +00:00
|
|
|
|
2015-07-02 01:26:42 +00:00
|
|
|
var role sshRole
|
|
|
|
if err := roleEntry.DecodeJSON(&role); err != nil {
|
|
|
|
return false, fmt.Errorf("error decoding role '%s'", roleName)
|
|
|
|
}
|
2015-07-02 21:23:09 +00:00
|
|
|
|
|
|
|
if matched, err := cidrContainsIP(ip, role.CIDR); err != nil {
|
|
|
|
return false, err
|
|
|
|
} else {
|
|
|
|
return matched, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 20:42:03 +00:00
|
|
|
// Returns true if the IP supplied by the user is part of the comma
|
|
|
|
// separated CIDR blocks
|
2015-07-02 21:23:09 +00:00
|
|
|
func cidrContainsIP(ip, cidr string) (bool, error) {
|
|
|
|
for _, item := range strings.Split(cidr, ",") {
|
2015-07-02 01:26:42 +00:00
|
|
|
_, cidrIPNet, err := net.ParseCIDR(item)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("invalid cidr entry '%s'", item)
|
|
|
|
}
|
2015-07-02 21:23:09 +00:00
|
|
|
if cidrIPNet.Contains(net.ParseIP(ip)) {
|
|
|
|
return true, nil
|
2015-07-02 01:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-02 21:23:09 +00:00
|
|
|
return false, nil
|
2015-07-02 01:26:42 +00:00
|
|
|
}
|
2015-08-06 18:48:19 +00:00
|
|
|
|
|
|
|
func scpUpload(username, ip, port, hostkey, fileName, fileContent string) error {
|
|
|
|
signer, err := ssh.ParsePrivateKey([]byte(hostkey))
|
|
|
|
clientConfig := &ssh.ClientConfig{
|
|
|
|
User: username,
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.PublicKeys(signer),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
connfunc := func() (net.Conn, error) {
|
|
|
|
c, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", ip, port), 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tcpConn, ok := c.(*net.TCPConn); ok {
|
|
|
|
tcpConn.SetKeepAlive(true)
|
|
|
|
tcpConn.SetKeepAlivePeriod(5 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
config := &commssh.Config{
|
|
|
|
SSHConfig: clientConfig,
|
|
|
|
Connection: connfunc,
|
|
|
|
Pty: false,
|
|
|
|
DisableAgent: true,
|
|
|
|
}
|
|
|
|
comm, err := commssh.New(fmt.Sprintf("%s:%s", ip, port), config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error connecting to target: %s", err)
|
|
|
|
}
|
|
|
|
comm.Upload(fileName, bytes.NewBufferString(fileContent), nil)
|
|
|
|
return nil
|
|
|
|
}
|