Files
vault/builtin/logical/transit/path_cache_config.go
hashicorp-copywrite[bot] 0b12cdcfd1 [COMPLIANCE] License changes (#22290)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License.

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* add missing license headers

* Update copyright file headers to BUS-1.1

* Fix test that expected exact offset on hcl file

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Sarah Thompson <sthompson@hashicorp.com>
Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
2023-08-10 18:14:03 -07:00

125 lines
3.0 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package transit
import (
"context"
"errors"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func (b *backend) pathCacheConfig() *framework.Path {
return &framework.Path{
Pattern: "cache-config",
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: operationPrefixTransit,
},
Fields: map[string]*framework.FieldSchema{
"size": {
Type: framework.TypeInt,
Required: false,
Default: 0,
Description: `Size of cache, use 0 for an unlimited cache size, defaults to 0`,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathCacheConfigRead,
Summary: "Returns the size of the active cache",
DisplayAttrs: &framework.DisplayAttributes{
OperationSuffix: "cache-configuration",
},
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathCacheConfigWrite,
Summary: "Configures a new cache of the specified size",
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "configure",
OperationSuffix: "cache",
},
},
},
HelpSynopsis: pathCacheConfigHelpSyn,
HelpDescription: pathCacheConfigHelpDesc,
}
}
func (b *backend) pathCacheConfigWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
// get target size
cacheSize := d.Get("size").(int)
if cacheSize != 0 && cacheSize < minCacheSize {
return logical.ErrorResponse("size must be 0 or a value greater or equal to %d", minCacheSize), logical.ErrInvalidRequest
}
// store cache size
entry, err := logical.StorageEntryJSON("config/cache", &configCache{
Size: cacheSize,
})
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
err = b.lm.InitCache(cacheSize)
if err != nil {
return nil, err
}
return &logical.Response{
Data: map[string]interface{}{
"size": cacheSize,
},
}, nil
}
type configCache struct {
Size int `json:"size"`
}
func (b *backend) pathCacheConfigRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
// error if no cache is configured
if !b.lm.GetUseCache() {
return nil, errors.New(
"caching is disabled for this transit mount",
)
}
// Compare current and stored cache sizes. If they are different warn the user.
currentCacheSize := b.lm.GetCacheSize()
storedCacheSize, err := GetCacheSizeFromStorage(ctx, req.Storage)
if err != nil {
return nil, err
}
if currentCacheSize != storedCacheSize {
err = b.lm.InitCache(storedCacheSize)
if err != nil {
return nil, err
}
}
resp := &logical.Response{
Data: map[string]interface{}{
"size": storedCacheSize,
},
}
return resp, nil
}
const pathCacheConfigHelpSyn = `Configure caching strategy`
const pathCacheConfigHelpDesc = `
This path is used to configure and query the cache size of the active cache, a size of 0 means unlimited.
`