-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_config_lease.go
153 lines (125 loc) · 3.86 KB
/
path_config_lease.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package exoscale
import (
"context"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const configLeaseStoragePath = "config/lease"
const (
pathConfigLeaseHelpSyn = "Configure the backend-specific secrets lease parameters"
pathConfigLeaseHelpDesc = `Manages the default secrets lease duration.
Can be overridden by the role settings.
⚠️ WARNING⚠️ This setting only applies to legacy IAM access key,
new API keys should take advantage of the "vault secrets tune" command:
- vault secrets tune -default-lease-ttl=4m -max-lease-ttl=8m exoscale
- vault read sys/mounts/exoscale/tune
If not configured, global system lease values are applied to generated
secrets.
(note: it is not possible to configure a lease duration greater than the
system's defaults)
`
)
func (b *exoscaleBackend) pathConfigLease() *framework.Path {
return &framework.Path{
Pattern: "config/lease",
Fields: map[string]*framework.FieldSchema{
"ttl": {
Type: framework.TypeDurationSecond,
Description: `Duration of issued API key secrets
If not set or set to 0, will use system default`,
},
"max_ttl": {
Type: framework.TypeDurationSecond,
Description: `Duration after which the issued API key secrets are not allowed to be renewed
If not set or set to 0, will use system default`,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{Callback: b.pathLeaseRead},
logical.UpdateOperation: &framework.PathOperation{Callback: b.pathLeaseWrite},
logical.DeleteOperation: &framework.PathOperation{Callback: b.pathLeaseDelete},
},
HelpSynopsis: pathConfigLeaseHelpSyn,
HelpDescription: pathConfigLeaseHelpDesc,
}
}
func getLeaseConfig(ctx context.Context, storage logical.Storage) (leaseConfig, error) {
var lc leaseConfig
entry, err := storage.Get(ctx, configLeaseStoragePath)
if err != nil {
return leaseConfig{}, err
}
if entry == nil {
return leaseConfig{}, nil
}
if err := entry.DecodeJSON(&lc); err != nil {
return leaseConfig{}, err
}
return lc, nil
}
func (b *exoscaleBackend) pathLeaseRead(
ctx context.Context,
req *logical.Request,
_ *framework.FieldData,
) (*logical.Response, error) {
lc, err := getLeaseConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
res := &logical.Response{
Data: map[string]interface{}{
"ttl": int64(lc.TTL.Seconds()),
"max_ttl": int64(lc.MaxTTL.Seconds()),
},
}
res.AddWarning(`This setting only applies to legacy IAM access key,
new API keys should take advantage of the "vault secrets tune" command`)
return res, nil
}
func (b *exoscaleBackend) pathLeaseWrite(
ctx context.Context,
req *logical.Request,
data *framework.FieldData,
) (*logical.Response, error) {
lc, err := getLeaseConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
if ttl, ok := data.GetOk("ttl"); ok {
lc.TTL = time.Duration(ttl.(int)) * time.Second
}
if maxTTL, ok := data.GetOk("max_ttl"); ok {
lc.MaxTTL = time.Duration(maxTTL.(int)) * time.Second
}
entry, err := logical.StorageEntryJSON(configLeaseStoragePath, lc)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
res := &logical.Response{
Data: map[string]interface{}{
"ttl": int64(lc.TTL.Seconds()),
"max_ttl": int64(lc.MaxTTL.Seconds()),
},
}
res.AddWarning(`This setting only applies to legacy IAM access key,
new API keys should take advantage of the "vault secrets tune" command`)
return res, nil
}
func (b *exoscaleBackend) pathLeaseDelete(
ctx context.Context,
req *logical.Request,
_ *framework.FieldData,
) (*logical.Response, error) {
if err := req.Storage.Delete(ctx, configLeaseStoragePath); err != nil {
return nil, err
}
return nil, nil
}
type leaseConfig struct {
TTL time.Duration `json:"ttl"`
MaxTTL time.Duration `json:"max_ttl"`
}