Skip to content

Commit 34a5941

Browse files
backport of commit 83534ec (#30569)
Co-authored-by: kpcraig <[email protected]>
1 parent d6611bd commit 34a5941

File tree

2 files changed

+142
-3
lines changed

2 files changed

+142
-3
lines changed

sdk/plugin/mock/backend.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func Backend() *backend {
5656
errorPaths(&b),
5757
kvPaths(&b),
5858
[]*framework.Path{
59+
pathConfig(&b),
5960
pathInternal(&b),
6061
pathSpecial(&b),
6162
pathRaw(&b),
@@ -67,9 +68,10 @@ func Backend() *backend {
6768
"special",
6869
},
6970
},
70-
Secrets: []*framework.Secret{},
71-
Invalidate: b.invalidate,
72-
BackendType: logical.TypeLogical,
71+
Secrets: []*framework.Secret{},
72+
Invalidate: b.invalidate,
73+
BackendType: logical.TypeLogical,
74+
RotateCredential: b.rotateRootCredential,
7375
}
7476
b.internal = MockPluginDefaultInternalValue
7577
b.RunningVersion = "v0.0.0+mock"
@@ -128,3 +130,9 @@ func expectInternalValue(t *testing.T, client *api.Client, mountPath, expected s
128130
t.Fatalf("expected %q but got %q", expected, resp.Data["value"].(string))
129131
}
130132
}
133+
134+
func (b *backend) rotateRootCredential(ctx context.Context, req *logical.Request) error {
135+
b.Logger().Debug("mock rotateRootCredential")
136+
b.internal = "rotated"
137+
return nil
138+
}

sdk/plugin/mock/path_config.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package mock
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/vault/sdk/framework"
10+
"github.com/hashicorp/vault/sdk/helper/automatedrotationutil"
11+
"github.com/hashicorp/vault/sdk/logical"
12+
"github.com/hashicorp/vault/sdk/rotation"
13+
)
14+
15+
// pathConfig is used to test auto rotation.
16+
func pathConfig(b *backend) *framework.Path {
17+
p := &framework.Path{
18+
Pattern: "config",
19+
Fields: map[string]*framework.FieldSchema{},
20+
Callbacks: map[logical.Operation]framework.OperationFunc{
21+
logical.CreateOperation: b.pathConfigUpdate,
22+
logical.UpdateOperation: b.pathConfigUpdate,
23+
logical.ReadOperation: b.pathConfigRead,
24+
},
25+
ExistenceCheck: b.pathConfigExistenceCheck,
26+
}
27+
automatedrotationutil.AddAutomatedRotationFields(p.Fields)
28+
return p
29+
}
30+
31+
func (b *backend) pathConfigUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
32+
conf, err := b.configEntry(ctx, req.Storage)
33+
if err != nil {
34+
return nil, err
35+
}
36+
if conf == nil {
37+
conf = &config{}
38+
}
39+
40+
if err := conf.ParseAutomatedRotationFields(data); err != nil {
41+
return logical.ErrorResponse(err.Error()), nil
42+
}
43+
44+
if conf.ShouldDeregisterRotationJob() {
45+
deregisterReq := &rotation.RotationJobDeregisterRequest{
46+
MountPoint: req.MountPoint,
47+
ReqPath: req.Path,
48+
}
49+
50+
b.Logger().Debug("Deregistering rotation job", "mount", req.MountPoint+req.Path)
51+
if err := b.System().DeregisterRotationJob(ctx, deregisterReq); err != nil {
52+
return logical.ErrorResponse("error deregistering rotation job: %s", err), nil
53+
}
54+
} else if conf.ShouldRegisterRotationJob() {
55+
cfgReq := &rotation.RotationJobConfigureRequest{
56+
MountPoint: req.MountPoint,
57+
ReqPath: req.Path,
58+
RotationSchedule: conf.RotationSchedule,
59+
RotationWindow: conf.RotationWindow,
60+
RotationPeriod: conf.RotationPeriod,
61+
}
62+
63+
b.Logger().Debug("Registering rotation job", "mount", req.MountPoint+req.Path)
64+
if _, err = b.System().RegisterRotationJob(ctx, cfgReq); err != nil {
65+
return logical.ErrorResponse("error registering rotation job: %s", err), nil
66+
}
67+
}
68+
69+
entry, err := logical.StorageEntryJSON("config", conf)
70+
if err != nil {
71+
return nil, err
72+
}
73+
if err := req.Storage.Put(ctx, entry); err != nil {
74+
return nil, err
75+
}
76+
return nil, nil
77+
}
78+
79+
func (b *backend) pathConfigRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
80+
conf, err := b.configEntry(ctx, req.Storage)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
if conf == nil {
86+
return nil, nil
87+
}
88+
89+
configData := map[string]interface{}{}
90+
conf.PopulateAutomatedRotationData(configData)
91+
92+
return &logical.Response{
93+
Data: configData,
94+
}, nil
95+
}
96+
97+
func (b *backend) pathConfigDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
98+
if err := req.Storage.Delete(ctx, "config"); err != nil {
99+
return nil, err
100+
}
101+
return nil, nil
102+
}
103+
104+
func (b *backend) pathConfigExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
105+
entry, err := b.configEntry(ctx, req.Storage)
106+
if err != nil {
107+
return false, err
108+
}
109+
return entry != nil, nil
110+
}
111+
112+
// Fetch the client configuration required to access the AWS API.
113+
func (b *backend) configEntry(ctx context.Context, s logical.Storage) (*config, error) {
114+
entry, err := s.Get(ctx, "config")
115+
if err != nil {
116+
return nil, err
117+
}
118+
if entry == nil {
119+
return nil, nil
120+
}
121+
122+
var result config
123+
if err := entry.DecodeJSON(&result); err != nil {
124+
return nil, err
125+
}
126+
return &result, nil
127+
}
128+
129+
type config struct {
130+
automatedrotationutil.AutomatedRotationParams
131+
}

0 commit comments

Comments
 (0)