|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: BUSL-1.1 |
| 3 | + |
| 4 | +package database |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "sync" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/vault/sdk/helper/consts" |
| 12 | + "github.com/hashicorp/vault/sdk/helper/pluginutil" |
| 13 | + "github.com/hashicorp/vault/sdk/logical" |
| 14 | + "github.com/hashicorp/vault/sdk/queue" |
| 15 | +) |
| 16 | + |
| 17 | +func newSystemViewWrapper(view logical.SystemView) logical.SystemView { |
| 18 | + return &systemViewWrapper{ |
| 19 | + view, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +type systemViewWrapper struct { |
| 24 | + logical.SystemView |
| 25 | +} |
| 26 | + |
| 27 | +var _ logical.ExtendedSystemView = (*systemViewWrapper)(nil) |
| 28 | + |
| 29 | +func (s *systemViewWrapper) RequestWellKnownRedirect(ctx context.Context, src, dest string) error { |
| 30 | + panic("nope") |
| 31 | +} |
| 32 | + |
| 33 | +func (s *systemViewWrapper) DeregisterWellKnownRedirect(ctx context.Context, src string) bool { |
| 34 | + panic("nope") |
| 35 | +} |
| 36 | + |
| 37 | +func (s *systemViewWrapper) Auditor() logical.Auditor { |
| 38 | + panic("nope") |
| 39 | +} |
| 40 | + |
| 41 | +func (s *systemViewWrapper) ForwardGenericRequest(ctx context.Context, request *logical.Request) (*logical.Response, error) { |
| 42 | + panic("nope") |
| 43 | +} |
| 44 | + |
| 45 | +func (s *systemViewWrapper) APILockShouldBlockRequest() (bool, error) { |
| 46 | + panic("nope") |
| 47 | +} |
| 48 | + |
| 49 | +func (s *systemViewWrapper) GetPinnedPluginVersion(ctx context.Context, pluginType consts.PluginType, pluginName string) (*pluginutil.PinnedVersion, error) { |
| 50 | + return nil, pluginutil.ErrPinnedVersionNotFound |
| 51 | +} |
| 52 | + |
| 53 | +func (s *systemViewWrapper) LookupPluginVersion(ctx context.Context, pluginName string, pluginType consts.PluginType, version string) (*pluginutil.PluginRunner, error) { |
| 54 | + return &pluginutil.PluginRunner{ |
| 55 | + Name: mockv5, |
| 56 | + Type: consts.PluginTypeDatabase, |
| 57 | + Builtin: true, |
| 58 | + BuiltinFactory: New, |
| 59 | + }, nil |
| 60 | +} |
| 61 | + |
| 62 | +func getDbBackend(t *testing.T) (*databaseBackend, logical.Storage) { |
| 63 | + t.Helper() |
| 64 | + config := logical.TestBackendConfig() |
| 65 | + config.System = newSystemViewWrapper(config.System) |
| 66 | + config.StorageView = &logical.InmemStorage{} |
| 67 | + // Create and init the backend ourselves instead of using a Factory because |
| 68 | + // the factory function kicks off threads that cause racy tests. |
| 69 | + b := Backend(config) |
| 70 | + if err := b.Setup(context.Background(), config); err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + b.schedule = &TestSchedule{} |
| 74 | + b.credRotationQueue = queue.New() |
| 75 | + b.populateQueue(context.Background(), config.StorageView) |
| 76 | + |
| 77 | + return b, config.StorageView |
| 78 | +} |
| 79 | + |
| 80 | +// TestGetConnectionRaceCondition checks that GetConnection always returns the same instance, even when asked |
| 81 | +// by multiple goroutines in parallel. |
| 82 | +func TestGetConnectionRaceCondition(t *testing.T) { |
| 83 | + ctx := context.Background() |
| 84 | + b, s := getDbBackend(t) |
| 85 | + defer b.Cleanup(ctx) |
| 86 | + configureDBMount(t, s) |
| 87 | + |
| 88 | + goroutines := 16 |
| 89 | + |
| 90 | + wg := sync.WaitGroup{} |
| 91 | + wg.Add(goroutines) |
| 92 | + dbis := make([]*dbPluginInstance, goroutines) |
| 93 | + errs := make([]error, goroutines) |
| 94 | + for i := 0; i < goroutines; i++ { |
| 95 | + go func(i int) { |
| 96 | + defer wg.Done() |
| 97 | + dbis[i], errs[i] = b.GetConnection(ctx, s, mockv5) |
| 98 | + }(i) |
| 99 | + } |
| 100 | + wg.Wait() |
| 101 | + for i := 0; i < goroutines; i++ { |
| 102 | + if errs[i] != nil { |
| 103 | + t.Fatal(errs[i]) |
| 104 | + } |
| 105 | + if dbis[0] != dbis[i] { |
| 106 | + t.Fatal("Error: database instances did not match") |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments