This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpath_rotate_root.go
75 lines (63 loc) · 1.96 KB
/
path_rotate_root.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
package splunk
import (
"context"
"fmt"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/splunk/vault-plugin-splunk/clients/splunk"
)
func (b *backend) pathRotateRoot() *framework.Path {
return &framework.Path{
Pattern: "rotate-root/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeString,
Description: "Name of this Splunk connection",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.rotateRootUpdateHandler,
},
HelpSynopsis: pathRotateRootHelpSyn,
HelpDescription: pathRotateRootHelpDesc,
}
}
func (b *backend) rotateRootUpdateHandler(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
oldConfig, err := connectionConfigLoad(ctx, req.Storage, name)
if err != nil {
return nil, err
}
conn, err := b.ensureConnection(ctx, oldConfig)
if err != nil {
return nil, err
}
config := *oldConfig
passwd, err := uuid.GenerateUUID()
if err != nil {
return nil, fmt.Errorf("error generating new password %w", err)
}
config.Password = passwd
opts := splunk.UpdateUserOptions{
OldPassword: oldConfig.Password,
Password: config.Password,
}
// XXX write WAL in case we restart between successful update and store
if _, _, err := conn.AccessControl.Authentication.Users.Update(config.Username, &opts); err != nil {
return nil, fmt.Errorf("error updating password: %w", err)
}
if err := config.store(ctx, req.Storage, name); err != nil {
return nil, err
}
resp := &logical.Response{
Data: config.toMinimalResponseData(),
}
return resp, nil
}
const pathRotateRootHelpSyn = `
Request to rotate the Splunk credentials for a Splunk connection.
`
const pathRotateRootHelpDesc = `
This path attempts to rotate the root credentials for the given Splunk connection.
`