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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
package secretsengine
import (
"context"
"errors"
"fmt"
"time"
"git.tardisproject.uk/tcmal/vault-plugin-kerberos-secrets/config"
"git.tardisproject.uk/tcmal/vault-plugin-kerberos-secrets/password"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
rotateRootPath = "rotate-root"
rotateRolePath = "rotate-static-role/"
)
func pathRotateCredentials(b *krbBackend) []*framework.Path {
return []*framework.Path{
{
Pattern: rotateRootPath,
Fields: map[string]*framework.FieldSchema{},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathRotateRootCredentialsUpdate,
ForwardPerformanceStandby: true,
ForwardPerformanceSecondary: true,
},
},
HelpSynopsis: "Request to rotate the root credentials Vault uses for the kerberos administrator account.",
HelpDescription: "This path attempts to rotate the root credentials of the administrator account " +
"used by Vault to manage credentials.",
},
{
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.pathRotateRoleCredentialsUpdate,
ForwardPerformanceStandby: true,
ForwardPerformanceSecondary: true,
},
},
HelpSynopsis: "Request to rotate the credentials for a static user account.",
HelpDescription: "This path attempts to rotate the credentials for the given static role.",
},
}
}
func (b *krbBackend) pathRotateRootCredentialsUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
if _, hasTimeout := ctx.Deadline(); !hasTimeout {
var cancel func()
ctx, cancel = context.WithTimeout(ctx, defaultCtxTimeout)
defer cancel()
}
config, err := getConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
return nil, errors.New("the config is currently unset")
}
client, err := b.getClient(ctx, req.Storage)
if err != nil {
return nil, err
}
newPassword := password.Generate()
// Update the password remotely.
if err := (*client).SetPassword(config.Username, newPassword); err != nil {
return nil, err
}
// Update the password locally.
config.Password = newPassword
if pwdStoringErr := storePassword(ctx, req.Storage, config); pwdStoringErr != nil {
return nil, fmt.Errorf("unable to update password due to storage err: %s", pwdStoringErr)
// TODO: deal with this more gracefully
}
b.reset()
// Respond with a 204.
return nil, nil
}
func (b *krbBackend) pathRotateRoleCredentialsUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
if name == "" {
return logical.ErrorResponse("empty role name attribute given"), nil
}
ictx, _ := context.WithTimeout(context.Background(), time.Minute*60)
go b.doRotation(ictx, name, req.Storage)
return nil, nil
}
func (b *krbBackend) doRotation(ctx context.Context, name string, storage logical.Storage) {
log := b.Logger().With("role", name)
log.Debug("starting to rotate role")
for {
err, retry := b.attemptRotation(ctx, name, storage) // TODO
if err == nil {
log.Debug("credentials rotated")
return
}
log.Error("error rotating credentials", "error", err)
if !retry {
log.Error("unrecoverable error rotating credentials")
}
timer := time.NewTimer(30 * time.Second)
select {
case <-timer.C:
continue
case <-ctx.Done():
log.Error("timeout rotating credentials")
return
}
}
}
func (b *krbBackend) attemptRotation(ctx context.Context, name string, storage logical.Storage) (error, bool) {
role, err := b.getRole(ctx, storage, name)
if err != nil {
return fmt.Errorf("error fetching role from storage: %e", err), true
}
if role == nil {
return fmt.Errorf("role does not exist: %s", name), false
}
c, err := b.getClient(ctx, storage)
if err != nil {
return fmt.Errorf("error getting client: %e", err), true
}
newPassword := password.Generate()
err = (*c).SetPassword(role.Principal, newPassword)
if err != nil {
return fmt.Errorf("error setting password: %e", err), true
}
role.Password = newPassword
role.LastVaultRotation = time.Now()
err = setRole(ctx, storage, name, role)
if err != nil {
return fmt.Errorf("rotated password but could not save back to storage"), true
// TODO: deal with this more gracefully
}
return nil, false
}
func storePassword(ctx context.Context, s logical.Storage, cfg *config.Config) error {
entry, err := logical.StorageEntryJSON(configStoragePath, cfg)
if err != nil {
return err
}
return s.Put(ctx, entry)
}
|