-
Notifications
You must be signed in to change notification settings - Fork 905
fix(coderd): ensure correct RBAC when enqueueing notifications #15478
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
9 commits
Select commit
Hold shift + click to select a range
6f1edbd
chore(coderd): make fake notifications enqueuer assert RBAC
johnstcn b39d87c
fix(rbac): update policy to allow provisionerd and autostart to creat…
johnstcn 3a277a5
chore: update tests after previous changes
johnstcn 7dab1de
fixup! chore: update tests after previous changes
johnstcn c247f4b
fix: add dbauthz.AsNotifier() to a few missing places
johnstcn 962144c
late night cian is not smart
johnstcn fbd1e4f
adjust locking
johnstcn 15e97bf
found some magic smoke!
johnstcn fc242db
just in case
johnstcn 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. review: moved to its own package to avoid import cycle |
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,99 @@ | ||
package notificationstest | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/google/uuid" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/coder/coder/v2/coderd/database/dbauthz" | ||
"github.com/coder/coder/v2/coderd/rbac" | ||
"github.com/coder/coder/v2/coderd/rbac/policy" | ||
) | ||
|
||
type FakeEnqueuer struct { | ||
authorizer rbac.Authorizer | ||
mu sync.Mutex | ||
sent []*FakeNotification | ||
} | ||
|
||
type FakeNotification struct { | ||
UserID, TemplateID uuid.UUID | ||
Labels map[string]string | ||
Data map[string]any | ||
CreatedBy string | ||
Targets []uuid.UUID | ||
} | ||
|
||
// TODO: replace this with actual calls to dbauthz. | ||
// See: https://github.com/coder/coder/issues/15481 | ||
func (f *FakeEnqueuer) assertRBACNoLock(ctx context.Context) { | ||
dannykopping marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if f.mu.TryLock() { | ||
panic("Developer error: do not call assertRBACNoLock outside of a mutex lock!") | ||
} | ||
|
||
// If we get here, we are locked. | ||
if f.authorizer == nil { | ||
f.authorizer = rbac.NewStrictCachingAuthorizer(prometheus.NewRegistry()) | ||
} | ||
|
||
act, ok := dbauthz.ActorFromContext(ctx) | ||
if !ok { | ||
panic("Developer error: no actor in context, you may need to use dbauthz.AsNotifier(ctx)") | ||
} | ||
|
||
for _, a := range []policy.Action{policy.ActionCreate, policy.ActionRead} { | ||
err := f.authorizer.Authorize(ctx, act, a, rbac.ResourceNotificationMessage) | ||
if err == nil { | ||
return | ||
} | ||
|
||
if rbac.IsUnauthorizedError(err) { | ||
panic(fmt.Sprintf("Developer error: not authorized to %s %s. "+ | ||
"Ensure that you are using dbauthz.AsXXX with an actor that has "+ | ||
"policy.ActionCreate on rbac.ResourceNotificationMessage", a, rbac.ResourceNotificationMessage.Type)) | ||
} | ||
panic("Developer error: failed to check auth:" + err.Error()) | ||
} | ||
} | ||
|
||
func (f *FakeEnqueuer) Enqueue(ctx context.Context, userID, templateID uuid.UUID, labels map[string]string, createdBy string, targets ...uuid.UUID) (*uuid.UUID, error) { | ||
return f.EnqueueWithData(ctx, userID, templateID, labels, nil, createdBy, targets...) | ||
} | ||
|
||
func (f *FakeEnqueuer) EnqueueWithData(ctx context.Context, userID, templateID uuid.UUID, labels map[string]string, data map[string]any, createdBy string, targets ...uuid.UUID) (*uuid.UUID, error) { | ||
return f.enqueueWithDataLock(ctx, userID, templateID, labels, data, createdBy, targets...) | ||
} | ||
|
||
func (f *FakeEnqueuer) enqueueWithDataLock(ctx context.Context, userID, templateID uuid.UUID, labels map[string]string, data map[string]any, createdBy string, targets ...uuid.UUID) (*uuid.UUID, error) { | ||
f.mu.Lock() | ||
defer f.mu.Unlock() | ||
f.assertRBACNoLock(ctx) | ||
|
||
f.sent = append(f.sent, &FakeNotification{ | ||
UserID: userID, | ||
TemplateID: templateID, | ||
Labels: labels, | ||
Data: data, | ||
CreatedBy: createdBy, | ||
Targets: targets, | ||
}) | ||
|
||
id := uuid.New() | ||
dannykopping marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return &id, nil | ||
} | ||
|
||
func (f *FakeEnqueuer) Clear() { | ||
f.mu.Lock() | ||
defer f.mu.Unlock() | ||
|
||
f.sent = nil | ||
} | ||
|
||
func (f *FakeEnqueuer) Sent() []*FakeNotification { | ||
f.mu.Lock() | ||
defer f.mu.Unlock() | ||
return append([]*FakeNotification{}, f.sent...) | ||
mtojek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
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.
review: no changes are needed to
lifecycle_executor.go
as it has its own actor to which I've added the required permissions