-
Notifications
You must be signed in to change notification settings - Fork 10
/
path_rotate_creds.go
107 lines (88 loc) · 2.64 KB
/
path_rotate_creds.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package plugin
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
rotateRolePath = "rotate-role/"
)
func (b *backend) pathRotateCredentials() *framework.Path {
return &framework.Path{
Pattern: rotateRolePath + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeString,
Description: "Name of the static role",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathRotateCredentialsUpdate,
ForwardPerformanceStandby: true,
ForwardPerformanceSecondary: true,
},
},
HelpSynopsis: pathRotateCredentialsUpdateHelpSyn,
HelpDescription: pathRotateCredentialsUpdateHelpDesc,
}
}
func (b *backend) pathRotateCredentialsUpdate(ctx context.Context, req *logical.Request, fieldData *framework.FieldData) (*logical.Response, error) {
cred := make(map[string]interface{})
config, err := readConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
return nil, errors.New("the config is currently unset")
}
roleName := fieldData.Get("name").(string)
b.credLock.Lock()
defer b.credLock.Unlock()
role, err := b.readRole(ctx, req.Storage, roleName)
if err != nil {
return nil, err
}
if role == nil {
return nil, fmt.Errorf("role %s does not exist", roleName)
}
if !role.LastVaultRotation.IsZero() {
credIfc, found := b.credCache.Get(roleName)
if found {
b.Logger().Debug("checking cached credential")
cred = credIfc.(map[string]interface{})
} else {
b.Logger().Debug("checking stored credential")
entry, err := req.Storage.Get(ctx, storageKey+"/"+roleName)
if err != nil {
return nil, err
}
// If the creds aren't in storage, but roles are and we've created creds before,
// this is an unexpected state and something has gone wrong.
// Let's be explicit and error about this.
if entry == nil {
b.Logger().Warn("should have the creds for %+v but they're not found", role)
} else {
if err := entry.DecodeJSON(&cred); err != nil {
return nil, err
}
b.credCache.SetDefault(roleName, cred)
}
}
}
_, err = b.generateAndReturnCreds(ctx, config, req.Storage, roleName, role, cred)
if err != nil {
return nil, err
}
return nil, nil
}
const pathRotateCredentialsUpdateHelpSyn = `
Request to rotate the role's credentials.
`
const pathRotateCredentialsUpdateHelpDesc = `
This path attempts to rotate the role's credentials.
`