|
| 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