Files
threshold/internal/auth/password/permitpool_test.go
T
Johan Brandhorst-Satzkorn 40cb9f5c9e Restrict concurrent password hashes (#5437)
* internal/auth/password: add resizable pool

The resizable pool wraps the permit pool with a mutex
to allow it to be resized at runtime.

* internal/auth/password: add concurrency limit

The default concurrency limit is 1, and it can be
set using the SetHashingPermits function.

* internal/cmd/config: allow configuring hashing limits

The new concurrent_password_hash_workers configuration
value can be used to loosen the constraints on the
userpass auth method hashing operation.

* internal/cmd: set password hashing constraints

Sets the password hashing concurrency constraints on
startup and config reload.

* CHANGELOG: add notice about password concurrency limit
2025-01-22 13:31:42 -08:00

54 lines
1.0 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package password
import (
"context"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestPermitPool(t *testing.T) {
pool := newResizablePermitPool(1)
wg := &sync.WaitGroup{}
start := make(chan struct{})
ctx := context.Background()
// Start 5 goroutines all trying to acquire the permit at the same time
for i := range 5 {
wg.Add(1)
go func() {
defer wg.Done()
<-start
t.Log("Goroutine " + strconv.Itoa(i) + " starting")
err := pool.Do(ctx, func() {
// Do some expensive operation
time.Sleep(10 * time.Millisecond)
})
assert.NoError(t, err)
t.Log("Goroutine " + strconv.Itoa(i) + " finished")
}()
}
// Also start a few goroutines that attempt to resize the pool
for i := range 5 {
wg.Add(1)
go func() {
defer wg.Done()
<-start
t.Log("Resizing pool " + strconv.Itoa(i))
err := pool.SetPermits(2)
t.Log("Resized pool" + strconv.Itoa(i))
assert.NoError(t, err)
}()
}
close(start)
wg.Wait()
}