forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 2
/
secret_dynamic_key.go
115 lines (101 loc) · 3.34 KB
/
secret_dynamic_key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package ssh
import (
"fmt"
"time"
"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,
Description: "IP address of host",
},
},
DefaultDuration: 0, // this will use sysview's value
DefaultGracePeriod: 2 * time.Minute,
Renew: b.secretDynamicKeyRenew,
Revoke: b.secretDynamicKeyRevoke,
}
}
func (b *backend) secretDynamicKeyRenew(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
f := framework.LeaseExtend(b.System().DefaultLeaseTTL(), b.System().MaxLeaseTTL(), false)
return f(req, d)
}
func (b *backend) secretDynamicKeyRevoke(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
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")
}
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")
}
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")
}
port := int(portRaw.(float64))
// 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)
}
// 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)
if err != nil {
return nil, fmt.Errorf("error removing public key from authorized_keys file in target")
}
return nil, nil
}