2022-04-11 21:06:15 +00:00
|
|
|
package httpmw
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2022-06-10 14:46:48 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2022-04-11 21:06:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type workspaceAgentParamContextKey struct{}
|
|
|
|
|
|
|
|
// WorkspaceAgentParam returns the workspace agent from the ExtractWorkspaceAgentParam handler.
|
|
|
|
func WorkspaceAgentParam(r *http.Request) database.WorkspaceAgent {
|
|
|
|
user, ok := r.Context().Value(workspaceAgentParamContextKey{}).(database.WorkspaceAgent)
|
|
|
|
if !ok {
|
|
|
|
panic("developer error: agent middleware not provided")
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExtractWorkspaceAgentParam grabs a workspace agent from the "workspaceagent" URL parameter.
|
|
|
|
func ExtractWorkspaceAgentParam(db database.Store) func(http.Handler) http.Handler {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2023-07-19 16:11:11 +00:00
|
|
|
agentUUID, parsed := ParseUUIDParam(rw, r, "workspaceagent")
|
2022-04-11 21:06:15 +00:00
|
|
|
if !parsed {
|
|
|
|
return
|
|
|
|
}
|
2022-06-24 15:25:01 +00:00
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
agent, err := db.GetWorkspaceAgentByID(ctx, agentUUID)
|
2023-12-12 21:14:32 +00:00
|
|
|
if httpapi.Is404Error(err) {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
|
2023-12-12 21:14:32 +00:00
|
|
|
Message: "Agent doesn't exist with that id, or you do not have access to it.",
|
2022-04-11 21:06:15 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching workspace agent.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-11 21:06:15 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-06-24 15:25:01 +00:00
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
resource, err := db.GetWorkspaceResourceByID(ctx, agent.ResourceID)
|
2022-04-11 21:06:15 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching workspace resource.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-11 21:06:15 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
job, err := db.GetProvisionerJobByID(ctx, resource.JobID)
|
2022-04-11 21:06:15 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-11 21:06:15 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if job.Type != database.ProvisionerJobTypeWorkspaceBuild {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
2022-04-11 21:06:15 +00:00
|
|
|
Message: "Workspace agents can only be fetched for builds.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-09-21 22:07:00 +00:00
|
|
|
build, err := db.GetWorkspaceBuildByJobID(ctx, job.ID)
|
2022-04-11 21:06:15 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching workspace build.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-11 21:06:15 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx = context.WithValue(ctx, workspaceAgentParamContextKey{}, agent)
|
2022-06-10 14:46:48 +00:00
|
|
|
chi.RouteContext(ctx).URLParams.Add("workspace", build.WorkspaceID.String())
|
2022-04-11 21:06:15 +00:00
|
|
|
next.ServeHTTP(rw, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|