2015-06-16 20:58:54 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2015-06-26 01:47:32 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-06-16 20:58:54 +00:00
|
|
|
"log"
|
2015-06-26 01:47:32 +00:00
|
|
|
"strings"
|
2015-06-16 20:58:54 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2015-06-19 16:59:36 +00:00
|
|
|
const SecretOneTimeKeyType = "secret_one_type_key_type"
|
2015-06-16 20:58:54 +00:00
|
|
|
|
2015-06-24 22:13:12 +00:00
|
|
|
func secretSshKey(b *backend) *framework.Secret {
|
2015-06-16 20:58:54 +00:00
|
|
|
log.Printf("Vishal: ssh.secretPrivateKey\n")
|
|
|
|
return &framework.Secret{
|
2015-06-19 16:59:36 +00:00
|
|
|
Type: SecretOneTimeKeyType,
|
2015-06-16 20:58:54 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"username": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Username in host",
|
|
|
|
},
|
|
|
|
"ip": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "ip address of host",
|
|
|
|
},
|
|
|
|
},
|
2015-06-26 01:47:32 +00:00
|
|
|
DefaultDuration: 10 * time.Second, //TODO: change this
|
|
|
|
DefaultGracePeriod: 10 * time.Second, //TODO: change this
|
2015-06-24 22:13:12 +00:00
|
|
|
Renew: b.secretSshKeyRenew,
|
|
|
|
Revoke: b.secretSshKeyRevoke,
|
2015-06-16 20:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 22:13:12 +00:00
|
|
|
func (b *backend) secretSshKeyRenew(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-06-19 00:48:41 +00:00
|
|
|
log.Printf("Vishal: ssh.secretPrivateKeyRenew\n")
|
|
|
|
lease, err := b.Lease(req.Storage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if lease == nil {
|
|
|
|
lease = &configLease{Lease: 1 * time.Hour}
|
|
|
|
}
|
|
|
|
f := framework.LeaseExtend(lease.Lease, lease.LeaseMax, false)
|
|
|
|
return f(req, d)
|
|
|
|
}
|
|
|
|
|
2015-06-24 22:13:12 +00:00
|
|
|
func (b *backend) secretSshKeyRevoke(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-06-26 01:47:32 +00:00
|
|
|
log.Printf("Vishal: ssh.secretPrivateKeyRevoke req: %#v\n", req)
|
|
|
|
//fetch the values from secret
|
|
|
|
usernameRaw, ok := req.Secret.InternalData["username"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing internal data")
|
|
|
|
}
|
|
|
|
username, ok := usernameRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing internal data")
|
|
|
|
}
|
|
|
|
ipRaw, ok := req.Secret.InternalData["ip"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing internal data")
|
|
|
|
}
|
|
|
|
ip, ok := ipRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing internal data")
|
|
|
|
}
|
|
|
|
hostKeyNameRaw, ok := req.Secret.InternalData["host_key_name"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing internal data")
|
|
|
|
}
|
|
|
|
hostKeyName := hostKeyNameRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing internal data")
|
|
|
|
}
|
|
|
|
dynamicPublicKeyRaw, ok := req.Secret.InternalData["dynamic_public_key"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing internal data")
|
|
|
|
}
|
|
|
|
dynamicPublicKey := dynamicPublicKeyRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing internal data")
|
|
|
|
}
|
|
|
|
log.Printf("Vishal: username:%s ip:%s keyName:%s\n", username, ip, hostKeyName)
|
|
|
|
|
|
|
|
//fetch the host key using the key name
|
|
|
|
hostKeyPath := "keys/" + hostKeyName
|
|
|
|
hostKeyEntry, err := req.Storage.Get(hostKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Key '%s' not found error:%s", hostKeyName, err)
|
|
|
|
}
|
|
|
|
var hostKey sshHostKey
|
|
|
|
if err := hostKeyEntry.DecodeJSON(&hostKey); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error reading the host key: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//write host key to file and use it as argument to scp command
|
|
|
|
hostKeyFileName := "./vault_ssh_" + username + "_" + ip + "_shared.pem"
|
|
|
|
err = ioutil.WriteFile(hostKeyFileName, []byte(hostKey.Key), 0400)
|
|
|
|
|
|
|
|
//write dynamicPublicKey to file and use it as argument to scp command
|
|
|
|
otkPrivateKeyFileName := "vault_ssh_" + username + "_" + ip + "_otk.pem"
|
|
|
|
otkPublicKeyFileName := otkPrivateKeyFileName + ".pub"
|
|
|
|
err = ioutil.WriteFile(otkPublicKeyFileName, []byte(dynamicPublicKey), 0400)
|
|
|
|
|
|
|
|
//transfer the dynamic public key to target machine and use it to remove the entry from authorized_keys file
|
|
|
|
scpCmd := "scp -i " + hostKeyFileName + " " + otkPublicKeyFileName + " " + username + "@" + ip + ":~;"
|
|
|
|
err = exec_command(scpCmd)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Errorf("Running command scp failed " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
authKeysFileName := "~/.ssh/authorized_keys"
|
|
|
|
tempKeysFileName := "~/temp_authorized_keys"
|
|
|
|
|
|
|
|
//commands to be run on target machine
|
|
|
|
grepCmd := "grep -vFf " + otkPublicKeyFileName + " " + authKeysFileName + " > " + tempKeysFileName + ";"
|
|
|
|
catCmdRemoveDuplicate := "cat " + tempKeysFileName + " > " + authKeysFileName + ";"
|
|
|
|
rmCmd := "rm -f " + tempKeysFileName + " " + otkPublicKeyFileName + ";"
|
|
|
|
remoteCmdString := strings.Join([]string{
|
|
|
|
grepCmd,
|
|
|
|
catCmdRemoveDuplicate,
|
|
|
|
rmCmd,
|
|
|
|
}, "")
|
|
|
|
|
|
|
|
//connect to target machine
|
|
|
|
session := createSSHPublicKeysSession(username, ip, hostKey.Key)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
session.Stdout = &buf
|
|
|
|
if err := session.Run(remoteCmdString); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
session.Close()
|
|
|
|
fmt.Println(buf.String())
|
2015-06-16 20:58:54 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|