2015-06-16 20:58:54 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2015-06-26 01:47:32 +00:00
|
|
|
"fmt"
|
2015-06-16 20:58:54 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2015-07-02 23:00:14 +00:00
|
|
|
const SecretDynamicKeyType = "secret_dynamic_key_type"
|
2015-06-16 20:58:54 +00:00
|
|
|
|
2015-07-01 15:58:49 +00:00
|
|
|
func secretSSHKey(b *backend) *framework.Secret {
|
2015-06-16 20:58:54 +00:00
|
|
|
return &framework.Secret{
|
2015-07-02 23:00:14 +00:00
|
|
|
Type: SecretDynamicKeyType,
|
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-07-01 00:21:41 +00:00
|
|
|
DefaultDuration: 1 * time.Hour,
|
|
|
|
DefaultGracePeriod: 10 * time.Minute,
|
2015-07-01 15:58:49 +00:00
|
|
|
Renew: b.secretSSHKeyRenew,
|
|
|
|
Revoke: b.secretSSHKeyRevoke,
|
2015-06-16 20:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:58:49 +00:00
|
|
|
func (b *backend) secretSSHKeyRenew(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-06-19 00:48:41 +00:00
|
|
|
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-07-01 15:58:49 +00:00
|
|
|
func (b *backend) secretSSHKeyRevoke(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-06-26 01:47:32 +00:00
|
|
|
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")
|
|
|
|
}
|
2015-07-06 20:56:45 +00:00
|
|
|
portRaw, ok := req.Secret.InternalData["port"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing internal data")
|
|
|
|
}
|
|
|
|
port := portRaw.(string)
|
2015-06-26 01:47:32 +00:00
|
|
|
|
2015-07-02 21:23:09 +00:00
|
|
|
// Fetch the host key using the key name
|
2015-07-02 00:35:11 +00:00
|
|
|
hostKeyEntry, err := req.Storage.Get(fmt.Sprintf("keys/%s", hostKeyName))
|
2015-06-26 01:47:32 +00:00
|
|
|
if err != nil {
|
2015-07-02 01:05:52 +00:00
|
|
|
return nil, fmt.Errorf("key '%s' not found error:%s", hostKeyName, err)
|
2015-06-26 01:47:32 +00:00
|
|
|
}
|
|
|
|
var hostKey sshHostKey
|
|
|
|
if err := hostKeyEntry.DecodeJSON(&hostKey); err != nil {
|
2015-07-02 01:05:52 +00:00
|
|
|
return nil, fmt.Errorf("error reading the host key: %s", err)
|
2015-06-26 01:47:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 21:23:09 +00:00
|
|
|
// Transfer the dynamic public key to target machine and use it to remove the entry from authorized_keys file
|
2015-07-06 20:56:45 +00:00
|
|
|
err = uploadPublicKeyScp(dynamicPublicKey, username, ip, port, hostKey.Key)
|
2015-06-26 01:47:32 +00:00
|
|
|
if err != nil {
|
2015-07-02 01:05:52 +00:00
|
|
|
return nil, fmt.Errorf("public key transfer failed: %s", err)
|
2015-06-26 01:47:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 21:23:09 +00:00
|
|
|
// Remove the public key from authorized_keys file in target machine
|
2015-07-06 20:56:45 +00:00
|
|
|
err = uninstallPublicKeyInTarget(username, ip, port, hostKey.Key)
|
2015-06-30 02:00:08 +00:00
|
|
|
if err != nil {
|
2015-07-02 21:23:09 +00:00
|
|
|
return nil, fmt.Errorf("error removing public key from authorized_keys file in target")
|
2015-06-26 01:47:32 +00:00
|
|
|
}
|
2015-06-16 20:58:54 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|