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
|
|
|
|
2016-08-24 19:04:04 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-06-16 20:58:54 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
2016-08-24 19:04:04 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-06-16 20:58:54 +00:00
|
|
|
)
|
|
|
|
|
2015-07-02 23:00:14 +00:00
|
|
|
const SecretDynamicKeyType = "secret_dynamic_key_type"
|
2015-06-16 20:58:54 +00:00
|
|
|
|
2015-07-23 22:12:13 +00:00
|
|
|
func secretDynamicKey(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,
|
2015-07-27 20:42:03 +00:00
|
|
|
Description: "IP address of host",
|
2015-06-16 20:58:54 +00:00
|
|
|
},
|
|
|
|
},
|
2016-01-29 22:44:09 +00:00
|
|
|
|
|
|
|
Renew: b.secretDynamicKeyRenew,
|
|
|
|
Revoke: b.secretDynamicKeyRevoke,
|
2015-06-16 20:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 20:42:03 +00:00
|
|
|
func (b *backend) secretDynamicKeyRenew(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-01-29 22:44:09 +00:00
|
|
|
f := framework.LeaseExtend(0, 0, b.System())
|
2015-06-19 00:48:41 +00:00
|
|
|
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) {
|
2016-08-24 19:04:04 +00:00
|
|
|
type sec struct {
|
|
|
|
AdminUser string `mapstructure:"admin_user"`
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
IP string `mapstructure:"ip"`
|
|
|
|
HostKeyName string `mapstructure:"host_key_name"`
|
|
|
|
DynamicPublicKey string `mapstructure:"dynamic_public_key"`
|
|
|
|
InstallScript string `mapstructure:"install_script"`
|
|
|
|
Port int `mapstructure:"port"`
|
2015-07-27 19:03:10 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 19:04:04 +00:00
|
|
|
intSec := &sec{}
|
|
|
|
err := mapstructure.Decode(req.Secret.InternalData, intSec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("secret internal data could not be decoded: {{err}}", err)
|
2015-07-06 20:56:45 +00:00
|
|
|
}
|
2015-06-26 01:47:32 +00:00
|
|
|
|
2015-07-02 21:23:09 +00:00
|
|
|
// Fetch the host key using the key name
|
2016-08-24 19:04:04 +00:00
|
|
|
hostKey, err := b.getKey(req.Storage, intSec.HostKeyName)
|
2015-06-26 01:47:32 +00:00
|
|
|
if err != nil {
|
2016-10-18 16:46:54 +00:00
|
|
|
return nil, fmt.Errorf("key %q not found error: %v", intSec.HostKeyName, err)
|
2015-06-26 01:47:32 +00:00
|
|
|
}
|
2015-08-13 21:18:30 +00:00
|
|
|
if hostKey == nil {
|
2016-10-18 16:46:54 +00:00
|
|
|
return nil, fmt.Errorf("key %q not found", intSec.HostKeyName)
|
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-08-13 17:36:31 +00:00
|
|
|
// The last param 'false' indicates that the key should be uninstalled.
|
2016-08-24 19:04:04 +00:00
|
|
|
err = b.installPublicKeyInTarget(intSec.AdminUser, intSec.Username, intSec.IP, intSec.Port, hostKey.Key, intSec.DynamicPublicKey, intSec.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")
|
2015-06-26 01:47:32 +00:00
|
|
|
}
|
2015-06-16 20:58:54 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|