open-vault/builtin/logical/ssh/secret_dynamic_key.go

114 lines
3.2 KiB
Go
Raw Normal View History

package ssh
import (
"fmt"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
const SecretDynamicKeyType = "secret_dynamic_key_type"
func secretDynamicKey(b *backend) *framework.Secret {
return &framework.Secret{
Type: SecretDynamicKeyType,
Fields: map[string]*framework.FieldSchema{
"username": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Username in host",
},
"ip": &framework.FieldSchema{
Type: framework.TypeString,
2015-07-27 20:42:03 +00:00
Description: "IP address of host",
},
},
Renew: b.secretDynamicKeyRenew,
Revoke: b.secretDynamicKeyRevoke,
}
}
2015-07-27 20:42:03 +00:00
func (b *backend) secretDynamicKeyRenew(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
f := framework.LeaseExtend(0, 0, b.System())
return f(req, d)
}
2015-07-27 20:42:03 +00:00
func (b *backend) secretDynamicKeyRevoke(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
2015-07-27 19:03:10 +00:00
adminUserRaw, ok := req.Secret.InternalData["admin_user"]
if !ok {
return nil, fmt.Errorf("secret is missing internal data")
}
adminUser, ok := adminUserRaw.(string)
if !ok {
return nil, fmt.Errorf("secret is missing internal data")
}
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")
}
2015-07-27 19:03:10 +00:00
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")
}
2015-07-27 19:03:10 +00:00
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")
}
2015-07-27 19:03:10 +00:00
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-27 19:03:10 +00:00
installScriptRaw, ok := req.Secret.InternalData["install_script"]
if !ok {
return nil, fmt.Errorf("secret is missing internal data")
}
installScript := installScriptRaw.(string)
if !ok {
return nil, fmt.Errorf("secret is missing internal data")
}
portRaw, ok := req.Secret.InternalData["port"]
if !ok {
return nil, fmt.Errorf("secret is missing internal data")
}
2015-08-13 15:46:55 +00:00
port := int(portRaw.(float64))
2015-07-02 21:23:09 +00:00
// Fetch the host key using the key name
hostKey, err := b.getKey(req.Storage, hostKeyName)
if err != nil {
return nil, fmt.Errorf("key '%s' not found error:%s", hostKeyName, err)
}
if hostKey == nil {
return nil, fmt.Errorf("key '%s' not found", hostKeyName)
}
2015-07-02 21:23:09 +00:00
// Remove the public key from authorized_keys file in target machine
// The last param 'false' indicates that the key should be uninstalled.
err = b.installPublicKeyInTarget(adminUser, username, ip, port, hostKey.Key, dynamicPublicKey, installScript, false)
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")
}
return nil, nil
}