2015-06-17 16:39:49 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2015-06-18 00:33:03 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-06-17 16:39:49 +00:00
|
|
|
"log"
|
2015-06-19 00:48:41 +00:00
|
|
|
"strings"
|
2015-06-17 16:39:49 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2015-06-24 22:13:12 +00:00
|
|
|
func pathRoleCreate(b *backend) *framework.Path {
|
2015-06-17 16:39:49 +00:00
|
|
|
log.Printf("Vishal: ssh.sshConnect\n")
|
|
|
|
return &framework.Path{
|
2015-06-24 22:13:12 +00:00
|
|
|
Pattern: "creds/(?P<name>\\w+)",
|
2015-06-17 16:39:49 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2015-06-24 22:13:12 +00:00
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "name of the policy",
|
|
|
|
},
|
2015-06-17 16:39:49 +00:00
|
|
|
"username": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
2015-06-24 22:13:12 +00:00
|
|
|
Description: "username in target",
|
2015-06-17 16:39:49 +00:00
|
|
|
},
|
2015-06-24 22:13:12 +00:00
|
|
|
"ip": &framework.FieldSchema{
|
2015-06-17 16:39:49 +00:00
|
|
|
Type: framework.TypeString,
|
2015-06-24 22:13:12 +00:00
|
|
|
Description: "IP of the target machine",
|
2015-06-17 16:39:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2015-06-24 22:13:12 +00:00
|
|
|
logical.WriteOperation: b.pathRoleCreateWrite,
|
2015-06-17 16:39:49 +00:00
|
|
|
},
|
|
|
|
HelpSynopsis: sshConnectHelpSyn,
|
|
|
|
HelpDescription: sshConnectHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 22:13:12 +00:00
|
|
|
func (b *backend) pathRoleCreateWrite(
|
2015-06-17 16:39:49 +00:00
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-06-24 22:13:12 +00:00
|
|
|
log.Printf("Vishal: ssh.pathRoleCreateWrite\n")
|
|
|
|
|
|
|
|
roleName := d.Get("name").(string)
|
2015-06-19 00:48:41 +00:00
|
|
|
username := d.Get("username").(string)
|
2015-06-24 22:13:12 +00:00
|
|
|
ipAddr := d.Get("ip").(string)
|
2015-06-18 00:33:03 +00:00
|
|
|
|
2015-06-24 22:13:12 +00:00
|
|
|
rolePath := "policy/" + roleName
|
|
|
|
roleEntry, err := req.Storage.Get(rolePath)
|
2015-06-19 00:48:41 +00:00
|
|
|
if err != nil {
|
2015-06-24 22:13:12 +00:00
|
|
|
return nil, fmt.Errorf("error retrieving role: %s", err)
|
|
|
|
}
|
|
|
|
if roleEntry == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Role '%s' not found", roleName)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var role sshRole
|
|
|
|
if err := roleEntry.DecodeJSON(&role); err != nil {
|
2015-06-19 00:48:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-24 22:13:12 +00:00
|
|
|
log.Printf("Vishal: ssh.pathRoleCreateWrite username:%#v address:%#v name:%#v result:%s\n", username, ipAddr, roleName, role)
|
|
|
|
//TODO: do the role verification here
|
|
|
|
|
|
|
|
keyPath := "keys/" + role.KeyName
|
|
|
|
keyEntry, err := req.Storage.Get(keyPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Key '%s' not found error:%s", role.KeyName, err)
|
2015-06-19 00:48:41 +00:00
|
|
|
}
|
2015-06-24 22:13:12 +00:00
|
|
|
|
|
|
|
log.Printf("Vishal: KeyName:%s keyPath:%s\n", role.KeyName, keyPath)
|
2015-06-19 00:48:41 +00:00
|
|
|
var hostKey sshHostKey
|
2015-06-24 22:13:12 +00:00
|
|
|
if err := keyEntry.DecodeJSON(&hostKey); err != nil {
|
2015-06-19 00:48:41 +00:00
|
|
|
return nil, fmt.Errorf("Error reading the host key: %s", err)
|
|
|
|
}
|
|
|
|
log.Printf("Vishal: host key previously configured: \n---------------\n%#v\n--------------\n", hostKey.Key)
|
|
|
|
|
|
|
|
//TODO: Input validation for the commands below
|
2015-06-19 16:59:36 +00:00
|
|
|
hostKeyFileName := "./vault_ssh_" + username + "_" + ipAddr + "_shared.pem"
|
|
|
|
err = ioutil.WriteFile(hostKeyFileName, []byte(hostKey.Key), 0400)
|
2015-06-19 00:48:41 +00:00
|
|
|
|
2015-06-19 16:59:36 +00:00
|
|
|
otkPrivateKeyFileName := "vault_ssh_" + username + "_" + ipAddr + "_otk.pem"
|
|
|
|
otkPublicKeyFileName := otkPrivateKeyFileName + ".pub"
|
|
|
|
rmCmd := "rm -f " + otkPrivateKeyFileName + " " + otkPublicKeyFileName + ";"
|
|
|
|
sshKeygenCmd := "ssh-keygen -f " + otkPrivateKeyFileName + " -t rsa -N ''" + ";"
|
|
|
|
chmodCmd := "chmod 400 " + otkPrivateKeyFileName + ";"
|
|
|
|
scpCmd := "scp -i " + hostKeyFileName + " " + otkPublicKeyFileName + " " + username + "@" + ipAddr + ":~;"
|
|
|
|
|
|
|
|
log.Printf("Vishal: scpCmd: \n", scpCmd)
|
2015-06-19 00:48:41 +00:00
|
|
|
|
|
|
|
localCmdString := strings.Join([]string{
|
|
|
|
rmCmd,
|
|
|
|
sshKeygenCmd,
|
|
|
|
chmodCmd,
|
|
|
|
scpCmd,
|
|
|
|
}, "")
|
|
|
|
err = exec_command(localCmdString)
|
2015-06-18 00:33:03 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Errorf("Running command failed " + err.Error())
|
|
|
|
}
|
2015-06-19 16:59:36 +00:00
|
|
|
log.Printf("Vishal: Creating session\n")
|
2015-06-19 00:48:41 +00:00
|
|
|
session := createSSHPublicKeysSession(username, ipAddr)
|
2015-06-18 00:33:03 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
session.Stdout = &buf
|
2015-06-19 16:59:36 +00:00
|
|
|
log.Printf("Vishal: Installing keys\n")
|
|
|
|
if err := installSshOtkInTarget(session, username, ipAddr); err != nil {
|
2015-06-18 00:33:03 +00:00
|
|
|
fmt.Errorf("Failed to install one-time-key at target machine: " + err.Error())
|
|
|
|
}
|
|
|
|
session.Close()
|
|
|
|
fmt.Println(buf.String())
|
2015-06-19 16:59:36 +00:00
|
|
|
keyBytes, err := ioutil.ReadFile(otkPrivateKeyFileName)
|
2015-06-18 00:33:03 +00:00
|
|
|
oneTimeKey := string(keyBytes)
|
2015-06-19 16:59:36 +00:00
|
|
|
log.Printf("Vishal: Returning:[%s]\n", oneTimeKey)
|
|
|
|
return b.Secret(SecretOneTimeKeyType).Response(map[string]interface{}{
|
2015-06-19 00:48:41 +00:00
|
|
|
"key": oneTimeKey,
|
|
|
|
}, nil), nil
|
2015-06-17 16:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const sshConnectHelpSyn = `
|
|
|
|
sshConnectionHelpSyn
|
|
|
|
`
|
|
|
|
|
|
|
|
const sshConnectHelpDesc = `
|
2015-06-19 00:48:41 +00:00
|
|
|
rshConnectionHelpDesc
|
2015-06-17 16:39:49 +00:00
|
|
|
`
|