- Notifications
You must be signed in to change notification settings - Fork928
chore: reorder prebuilt workspace authorization logic#18506
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
a4b2e8d
471d3e8
1f0e33a
2c387ce
4b8775f
f3f0183
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -151,26 +151,28 @@ func (q *querier) authorizeContext(ctx context.Context, action policy.Action, ob | ||
// authorizePrebuiltWorkspace handles authorization for workspace resource types. | ||
// prebuilt_workspaces are a subset of workspaces, currently limited to | ||
// supporting delete operations. This function first attempts normal workspace | ||
// authorization. If that fails, the action is delete or update and the workspace | ||
// is a prebuild, a prebuilt-specific authorization is attempted. | ||
// Note: Delete operations of workspaces requires both update and delete | ||
// permissions. | ||
func (q *querier) authorizePrebuiltWorkspace(ctx context.Context, action policy.Action, workspace database.Workspace) error { | ||
// Try default workspace authorization first | ||
var workspaceErr error | ||
if workspaceErr = q.authorizeContext(ctx, action, workspace); workspaceErr == nil { | ||
return nil | ||
} | ||
// Special handling for prebuilt workspace deletion | ||
if (action == policy.ActionUpdate || action == policy.ActionDelete) && workspace.IsPrebuild() { | ||
var prebuiltErr error | ||
if prebuiltErr = q.authorizeContext(ctx, action, workspace.AsPrebuild()); prebuiltErr == nil { | ||
return nil | ||
} | ||
return xerrors.Errorf("authorize context: %w", errors.Join(workspaceErr, prebuiltErr)) | ||
ssncferreira marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
return xerrors.Errorf("authorize context: %w", errors.Join(workspaceErr)) | ||
ssncferreira marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
type authContextKey struct{} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -5590,7 +5590,17 @@ func (s *MethodTestSuite) TestAuthorizePrebuiltWorkspace() { | ||
Reason: database.BuildReasonInitiator, | ||
TemplateVersionID: tv.ID, | ||
JobID: pj.ID, | ||
}). | ||
// Simulate a fallback authorization flow: | ||
// - First, the default workspace authorization fails (simulated by returning an error). | ||
// - Then, authorization is retried using the prebuilt workspace object, which succeeds. | ||
// The test asserts that both authorization attempts occur in the correct order. | ||
WithSuccessAuthorizer(func(ctx context.Context, subject rbac.Subject, action policy.Action, obj rbac.Object) error { | ||
if obj.Type == rbac.ResourceWorkspace.Type { | ||
return xerrors.Errorf("not authorized for workspace type") | ||
} | ||
return nil | ||
}).Asserts(w, policy.ActionDelete, w.AsPrebuild(), policy.ActionDelete) | ||
ssncferreira marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
})) | ||
s.Run("PrebuildUpdate/InsertWorkspaceBuildParameters", s.Subtest(func(db database.Store, check *expects) { | ||
u := dbgen.User(s.T(), db, database.User{}) | ||
@@ -5619,6 +5629,16 @@ func (s *MethodTestSuite) TestAuthorizePrebuiltWorkspace() { | ||
}) | ||
check.Args(database.InsertWorkspaceBuildParametersParams{ | ||
WorkspaceBuildID: wb.ID, | ||
}). | ||
// Simulate a fallback authorization flow: | ||
// - First, the default workspace authorization fails (simulated by returning an error). | ||
// - Then, authorization is retried using the prebuilt workspace object, which succeeds. | ||
// The test asserts that both authorization attempts occur in the correct order. | ||
WithSuccessAuthorizer(func(ctx context.Context, subject rbac.Subject, action policy.Action, obj rbac.Object) error { | ||
if obj.Type == rbac.ResourceWorkspace.Type { | ||
return xerrors.Errorf("not authorized for workspace type") | ||
} | ||
return nil | ||
}).Asserts(w, policy.ActionUpdate, w.AsPrebuild(), policy.ActionUpdate) | ||
})) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -393,17 +393,16 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) { | ||
ctx, | ||
tx, | ||
func(action policy.Action, object rbac.Objecter) bool { | ||
if auth := api.Authorize(r, action, object); auth { | ||
return true | ||
} | ||
// Special handling for prebuilt workspace deletion | ||
if object.RBACObject().Type == rbac.ResourceWorkspace.Type && action == policy.ActionDelete { | ||
if workspaceObj, ok := object.(database.Workspace); ok && workspaceObj.IsPrebuild() { | ||
return api.Authorize(r, action, workspaceObj.AsPrebuild()) | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
} | ||
return false | ||
}, | ||
audit.WorkspaceBuildBaggageFromRequest(r), | ||
) | ||
Uh oh!
There was an error while loading.Please reload this page.