mirror of
https://github.com/coder/coder.git
synced 2026-10-16 14:48:56 +08:00
Removes the Tasks API and task orchestration from coderd and codersdk: the `/api/v2/tasks` handlers, task middleware and search filters, task name generation, autobuild pause/resume logic, task notification enqueues, telemetry, and the codersdk task types. Regenerates apidoc, the API reference docs, and `typesGenerated.ts`. **Breaking change:** the Tasks API is removed; requests to `/api/v2/tasks` fail. Tasks was deprecated in v2.34. **Stack context:** Coder Tasks removal stack (CODAGT-642): #28743 frontend → #28788 CLI → #28789 API → #28790 provisioner → #29084 notifications → #29085 build reasons → #29086 tables → #28750 permissions. The last four PRs replace the single data-drop migration that used to live in #28750; each carries one concern and its own migration number. **Why:** with the UI (#28743) and CLI (#28788) gone, this deletes the server surface. Database tables, RBAC, and notification templates stay until the top of the stack so every layer builds and migrates independently. **What to scrutinize:** - The `enable-ai-tasks` option is removed outright (review feedback): configs still carrying `enableAITasks` fail to parse until the key is deleted. - provisionerdserver now writes `HasAITask: false` on template/build flags; the column and these lines are dropped in #29085. - `codersdk.ResourceTypeTask` stays so audit logs recorded before the removal keep rendering. **Validation:** `go build ./...`, `go vet ./...`, `go test ./codersdk/...`, site biome and tsc, CLI goldens regenerated, pre-commit hooks. **Size:** +242/-10,792 over 76 files; additions are regenerated docs/goldens and test fixture updates. > Created by Xum (AI agent) on behalf of @ibetitsmike.
146 lines
4.8 KiB
Go
146 lines
4.8 KiB
Go
package httpmw
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
"github.com/coder/coder/v2/coderd/rbac"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
type workspaceAgentContextKey struct{}
|
|
|
|
func WorkspaceAgentOptional(r *http.Request) (database.WorkspaceAgent, bool) {
|
|
user, ok := r.Context().Value(workspaceAgentContextKey{}).(database.WorkspaceAgent)
|
|
return user, ok
|
|
}
|
|
|
|
// WorkspaceAgent returns the workspace agent from the ExtractAgent handler.
|
|
func WorkspaceAgent(r *http.Request) database.WorkspaceAgent {
|
|
user, ok := WorkspaceAgentOptional(r)
|
|
if !ok {
|
|
panic("developer error: agent middleware not provided or was made optional")
|
|
}
|
|
return user
|
|
}
|
|
|
|
type latestBuildContextKey struct{}
|
|
|
|
func latestBuildOptional(r *http.Request) (database.WorkspaceBuild, bool) {
|
|
wb, ok := r.Context().Value(latestBuildContextKey{}).(database.WorkspaceBuild)
|
|
return wb, ok
|
|
}
|
|
|
|
// LatestBuild returns the Latest Build from the ExtractLatestBuild handler.
|
|
func LatestBuild(r *http.Request) database.WorkspaceBuild {
|
|
wb, ok := latestBuildOptional(r)
|
|
if !ok {
|
|
panic("developer error: agent middleware not provided or was made optional")
|
|
}
|
|
return wb
|
|
}
|
|
|
|
type ExtractWorkspaceAgentAndLatestBuildConfig struct {
|
|
DB database.Store
|
|
// Optional indicates whether the middleware should be optional. If true, any
|
|
// requests without the a token or with an invalid token will be allowed to
|
|
// continue and no workspace agent will be set on the request context.
|
|
Optional bool
|
|
}
|
|
|
|
// ExtractWorkspaceAgentAndLatestBuild requires authentication using a valid agent token.
|
|
func ExtractWorkspaceAgentAndLatestBuild(opts ExtractWorkspaceAgentAndLatestBuildConfig) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
// optionalWrite wraps httpapi.Write but runs the next handler if the
|
|
// token is optional.
|
|
//
|
|
// It should be used when the token is not provided or is invalid, but not
|
|
// when there are other errors.
|
|
optionalWrite := func(code int, response codersdk.Response) {
|
|
if opts.Optional {
|
|
next.ServeHTTP(rw, r)
|
|
return
|
|
}
|
|
httpapi.Write(ctx, rw, code, response)
|
|
}
|
|
|
|
tokenValue := APITokenFromRequest(r)
|
|
if tokenValue == "" {
|
|
optionalWrite(http.StatusUnauthorized, codersdk.Response{
|
|
Message: fmt.Sprintf("Cookie %q must be provided.", codersdk.SessionTokenCookie),
|
|
})
|
|
return
|
|
}
|
|
token, err := uuid.Parse(tokenValue)
|
|
if err != nil {
|
|
optionalWrite(http.StatusUnauthorized, codersdk.Response{
|
|
Message: "Workspace agent token invalid.",
|
|
Detail: fmt.Sprintf("An agent token must be a valid UUIDv4. (len %d)", len(tokenValue)),
|
|
})
|
|
return
|
|
}
|
|
|
|
//nolint:gocritic // System needs to be able to get workspace agents.
|
|
row, err := opts.DB.GetAuthenticatedWorkspaceAgentAndBuildByAuthToken(dbauthz.AsSystemRestricted(ctx), token)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
optionalWrite(http.StatusUnauthorized, codersdk.Response{
|
|
Message: "Workspace agent not authorized.",
|
|
Detail: "The agent cannot authenticate until the workspace provision job has been completed. If the job is no longer running, this agent is invalid.",
|
|
})
|
|
return
|
|
}
|
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
|
Message: "Internal error checking workspace agent authorization.",
|
|
Detail: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
subject, userStatus, err := UserRBACSubject(
|
|
ctx,
|
|
opts.DB,
|
|
row.WorkspaceTable.OwnerID,
|
|
rbac.WorkspaceAgentScope(rbac.WorkspaceAgentScopeParams{
|
|
WorkspaceID: row.WorkspaceTable.ID,
|
|
OwnerID: row.WorkspaceTable.OwnerID,
|
|
TemplateID: row.WorkspaceTable.TemplateID,
|
|
VersionID: row.WorkspaceBuild.TemplateVersionID,
|
|
BlockUserData: row.WorkspaceAgent.APIKeyScope == database.AgentKeyScopeEnumNoUserData,
|
|
}),
|
|
)
|
|
if err != nil {
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
|
Message: "Internal error with workspace agent authorization context.",
|
|
Detail: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
if userStatus != database.UserStatusActive {
|
|
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{
|
|
Message: fmt.Sprintf("User is not active (status = %q). Contact an admin to reactivate your account.", userStatus),
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx = context.WithValue(ctx, workspaceAgentContextKey{}, row.WorkspaceAgent)
|
|
ctx = context.WithValue(ctx, latestBuildContextKey{}, row.WorkspaceBuild)
|
|
// Also set the dbauthz actor for the request.
|
|
ctx = dbauthz.As(ctx, subject)
|
|
next.ServeHTTP(rw, r.WithContext(ctx))
|
|
})
|
|
}
|
|
}
|