-
Notifications
You must be signed in to change notification settings - Fork 308
Restrict concurrent password hashes #5437
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
084f3b8
internal/auth/password: add resizable pool
johanbrandhorst 13dd346
internal/auth/password: add concurrency limit
johanbrandhorst 49c9ddd
internal/cmd/config: allow configuring hashing limits
johanbrandhorst c1c2005
internal/cmd: set password hashing constraints
johanbrandhorst 5e23cd3
CHANGELOG: add notice about password concurrency limit
johanbrandhorst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package password | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
|
|
||
| "github.com/hashicorp/boundary/internal/errors" | ||
| "github.com/hashicorp/go-secure-stdlib/permitpool" | ||
| ) | ||
|
|
||
| // resizablePermitPool is a permit pool that can be resized at runtime. | ||
| type resizablePermitPool struct { | ||
| pool *permitpool.Pool | ||
| // lock is used to synchronize access to the permit pool | ||
| // This is an RWMutex to allow an unlimited number of readers | ||
| // and a single writer, since allowing a single reader or writer | ||
| // would effectively make the pool useless. | ||
| lock *sync.RWMutex | ||
| } | ||
|
|
||
| // newResizablePermitPool creates a new resizable permit pool with n permits. | ||
| func newResizablePermitPool(n int) *resizablePermitPool { | ||
| return &resizablePermitPool{ | ||
| pool: permitpool.New(n), | ||
| lock: &sync.RWMutex{}, | ||
| } | ||
| } | ||
|
|
||
| // SetPermit sets the number of permits available in the pool. | ||
| func (r *resizablePermitPool) SetPermits(n int) error { | ||
| const op = "resizablePermitPool.SetPermits" | ||
| if n <= 0 { | ||
| return errors.New(context.Background(), errors.InvalidParameter, op, "n must be greater than 0") | ||
| } | ||
| // Taking a write lock ensures there are no currently acquired permits | ||
| r.lock.Lock() | ||
| defer r.lock.Unlock() | ||
| r.pool = permitpool.New(n) | ||
| return nil | ||
| } | ||
|
|
||
| // Do executes the provided function with a permit acquired from the pool. | ||
| // If the context is canceled while waiting to acquire a permit, an error is returned. | ||
| func (r *resizablePermitPool) Do(ctx context.Context, fn func()) error { | ||
| const op = "resizablePermitPool.Do" | ||
| // We need to ensure both the Acquire and Release happen while the read lock is held, | ||
| // so that the pool cannot be resized in between. | ||
| r.lock.RLock() | ||
| defer r.lock.RUnlock() | ||
| if err := r.pool.Acquire(ctx); err != nil { | ||
| return errors.Wrap(ctx, err, op, errors.WithMsg("failed to acquire permit")) | ||
| } | ||
| defer r.pool.Release() | ||
| fn() | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // 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() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There were two "Bug Fixes" sections in the changelog under "Next", so I moved this one up to the one above.