-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpath_secrets.go
62 lines (52 loc) · 1.49 KB
/
path_secrets.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
package packet
import (
"context"
"fmt"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func (b *backend) pathSecrets() *framework.Secret {
return &framework.Secret{
Type: secretType,
Fields: map[string]*framework.FieldSchema{
"api_key_token": {
Type: framework.TypeString,
Description: "API token",
},
},
Renew: b.operationRenew,
Revoke: b.operationRevoke,
}
}
func (b *backend) operationRevoke(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
idRaw, ok := req.Secret.InternalData["api_key_id"]
if !ok {
return nil, fmt.Errorf("secret is missing ID of the API token")
}
keyID := idRaw.(string)
client, err := b.Client(ctx, req.Storage)
if err != nil {
return nil, err
}
resp, err := client.APIKeys.Delete(keyID)
if (err != nil) && (resp.Response.StatusCode != 404) {
return nil, err
}
return nil, nil
}
func (b *backend) operationRenew(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
defaultLease, maxLease := b.getDefaultAndMaxLease()
resp := &logical.Response{Secret: req.Secret}
resp.Secret.TTL = defaultLease
resp.Secret.MaxTTL = maxLease
return resp, nil
}
func (b *backend) getDefaultAndMaxLease() (time.Duration, time.Duration) {
maxLease := b.system.MaxLeaseTTL()
defaultLease := b.system.DefaultLeaseTTL()
if defaultLease > maxLease {
maxLease = defaultLease
}
return defaultLease, maxLease
}