Re-initialize v5 backend after a plugin crash (#17140)

This commit is contained in:
vinay-gopalan 2022-09-19 16:48:45 -07:00 committed by GitHub
parent 5a8a896b5a
commit c548ea39be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -3,7 +3,7 @@ package main
import (
"os"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/builtin/logical/aws"
"github.com/hashicorp/vault/sdk/plugin"

View File

@ -46,7 +46,7 @@ type backend struct {
canary string
}
func (b *backend) reloadBackend(ctx context.Context) error {
func (b *backend) reloadBackend(ctx context.Context, storage logical.Storage) error {
pluginName := b.config.Config["plugin_name"]
pluginType, err := consts.ParsePluginType(b.config.Config["plugin_type"])
if err != nil {
@ -72,6 +72,16 @@ func (b *backend) reloadBackend(ctx context.Context) error {
}
b.Backend = nb
// Re-initialize the backend in case plugin was reloaded
// after it crashed
err = b.Backend.Initialize(ctx, &logical.InitializationRequest{
Storage: storage,
})
if err != nil {
return err
}
return nil
}
@ -88,7 +98,7 @@ func (b *backend) HandleRequest(ctx context.Context, req *logical.Request) (*log
// Reload plugin if it's an rpc.ErrShutdown
b.mu.Lock()
if b.canary == canary {
err := b.reloadBackend(ctx)
err := b.reloadBackend(ctx, req.Storage)
if err != nil {
b.mu.Unlock()
return nil, err
@ -120,7 +130,7 @@ func (b *backend) HandleExistenceCheck(ctx context.Context, req *logical.Request
// Reload plugin if it's an rpc.ErrShutdown
b.mu.Lock()
if b.canary == canary {
err := b.reloadBackend(ctx)
err := b.reloadBackend(ctx, req.Storage)
if err != nil {
b.mu.Unlock()
return false, false, err