mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-11-04 04:28:08 +00:00 
			
		
		
		
	Add context to storage backends and wire it through a lot of places (#3817)
This commit is contained in:
		
				
					committed by
					
						
						Jeff Mitchell
					
				
			
			
				
	
			
			
			
						parent
						
							2864fbd697
						
					
				
				
					commit
					8142b42d95
				
			@@ -1,6 +1,7 @@
 | 
			
		||||
package postgresql
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
@@ -12,9 +13,9 @@ import (
 | 
			
		||||
	"github.com/hashicorp/vault/logical/framework"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
 | 
			
		||||
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
 | 
			
		||||
	b := Backend(conf)
 | 
			
		||||
	if err := b.Setup(conf); err != nil {
 | 
			
		||||
	if err := b.Setup(ctx, conf); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return b, nil
 | 
			
		||||
@@ -62,7 +63,7 @@ type backend struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DB returns the database connection.
 | 
			
		||||
func (b *backend) DB(s logical.Storage) (*sql.DB, error) {
 | 
			
		||||
func (b *backend) DB(ctx context.Context, s logical.Storage) (*sql.DB, error) {
 | 
			
		||||
	b.logger.Trace("postgres/db: enter")
 | 
			
		||||
	defer b.logger.Trace("postgres/db: exit")
 | 
			
		||||
 | 
			
		||||
@@ -80,7 +81,7 @@ func (b *backend) DB(s logical.Storage) (*sql.DB, error) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Otherwise, attempt to make connection
 | 
			
		||||
	entry, err := s.Get("config/connection")
 | 
			
		||||
	entry, err := s.Get(ctx, "config/connection")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -124,7 +125,7 @@ func (b *backend) DB(s logical.Storage) (*sql.DB, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ResetDB forces a connection next time DB() is called.
 | 
			
		||||
func (b *backend) ResetDB() {
 | 
			
		||||
func (b *backend) ResetDB(_ context.Context) {
 | 
			
		||||
	b.logger.Trace("postgres/resetdb: enter")
 | 
			
		||||
	defer b.logger.Trace("postgres/resetdb: exit")
 | 
			
		||||
 | 
			
		||||
@@ -138,16 +139,16 @@ func (b *backend) ResetDB() {
 | 
			
		||||
	b.db = nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (b *backend) invalidate(key string) {
 | 
			
		||||
func (b *backend) invalidate(ctx context.Context, key string) {
 | 
			
		||||
	switch key {
 | 
			
		||||
	case "config/connection":
 | 
			
		||||
		b.ResetDB()
 | 
			
		||||
		b.ResetDB(ctx)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Lease returns the lease information
 | 
			
		||||
func (b *backend) Lease(s logical.Storage) (*configLease, error) {
 | 
			
		||||
	entry, err := s.Get("config/lease")
 | 
			
		||||
func (b *backend) Lease(ctx context.Context, s logical.Storage) (*configLease, error) {
 | 
			
		||||
	entry, err := s.Get(ctx, "config/lease")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user