Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce minimum cache size for transit backend #12418

Merged
merged 13 commits into from Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions builtin/logical/transit/backend.go
Expand Up @@ -68,6 +68,12 @@ func Backend(ctx context.Context, conf *logical.BackendConfig) (*backend, error)
if err != nil {
return nil, fmt.Errorf("Error retrieving cache size from storage: %w", err)
}

minCacheSize := 10
divyapola5 marked this conversation as resolved.
Show resolved Hide resolved
if cacheSize != 0 && cacheSize < minCacheSize {
b.Logger().Warn("size %d is less than default minimum %d. Cache size is set to %d", cacheSize, minCacheSize, minCacheSize)
divyapola5 marked this conversation as resolved.
Show resolved Hide resolved
cacheSize = minCacheSize
}
}

var err error
Expand Down
12 changes: 8 additions & 4 deletions builtin/logical/transit/path_cache_config.go
Expand Up @@ -3,7 +3,7 @@ package transit
import (
"context"
"errors"

"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
Expand Down Expand Up @@ -48,6 +48,12 @@ func (b *backend) pathCacheConfigWrite(ctx context.Context, req *logical.Request
if cacheSize < 0 {
return logical.ErrorResponse("size must be greater or equal to 0"), logical.ErrInvalidRequest
}
minCacheSize := 10
var resp = &logical.Response{}
if cacheSize != 0 && cacheSize < minCacheSize {
resp.AddWarning(fmt.Sprintf("size %d is less than default minimum %d. Cache size is set to %d", cacheSize, minCacheSize, minCacheSize))
divyapola5 marked this conversation as resolved.
Show resolved Hide resolved
cacheSize = minCacheSize
}

// store cache size
entry, err := logical.StorageEntryJSON("config/cache", &configCache{
Expand All @@ -60,9 +66,7 @@ func (b *backend) pathCacheConfigWrite(ctx context.Context, req *logical.Request
return nil, err
}

resp := &logical.Response{
Warnings: []string{"cache configurations will be applied when this backend is restarted"},
}
resp.AddWarning("cache configurations will be applied when this backend is restarted")

return resp, nil
}
Expand Down
3 changes: 3 additions & 0 deletions changelog/12418.txt
@@ -0,0 +1,3 @@
```release-note:bug
Enforce minimum cache size for transit backend
```
2 changes: 1 addition & 1 deletion website/content/api-docs/secret/transit.mdx
Expand Up @@ -1307,7 +1307,7 @@ using the [`/sys/plugins/reload/backend`][sys-plugin-reload-backend] endpoint.

- `size` `(int: 0)` - Specifies the size in terms of number of entries. A size of
`0` means unlimited. A _Least Recently Used_ (LRU) caching strategy is used for a
non-zero cache size.
non-zero cache size. Must be 0 (default) or a value greater or equal to 10 (minimum cache size).
victorr marked this conversation as resolved.
Show resolved Hide resolved

### Sample Payload

Expand Down