2022-04-11 16:06:15 -05:00
|
|
|
package httpmw
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2022-06-10 10:46:48 -04:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
|
2026-01-08 10:29:41 +04:00
|
|
|
"cdr.dev/slog/v3"
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
2025-04-15 13:27:23 +02:00
|
|
|
"github.com/coder/coder/v2/coderd/httpmw/loggermw"
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2022-04-11 16:06:15 -05:00
|
|
|
)
|
|
|
|
|
|
2026-01-19 12:36:33 +00:00
|
|
|
type workspaceAgentAndWorkspaceParamContextKey struct{}
|
2022-04-11 16:06:15 -05:00
|
|
|
|
2026-01-19 12:36:33 +00:00
|
|
|
// WorkspaceAgentAndWorkspaceParam returns the workspace agent and its associated workspace from the ExtractWorkspaceAgentParam handler.
|
|
|
|
|
func WorkspaceAgentAndWorkspaceParam(r *http.Request) database.GetWorkspaceAgentAndWorkspaceByIDRow {
|
|
|
|
|
aw, ok := r.Context().Value(workspaceAgentAndWorkspaceParamContextKey{}).(database.GetWorkspaceAgentAndWorkspaceByIDRow)
|
2022-04-11 16:06:15 -05:00
|
|
|
if !ok {
|
|
|
|
|
panic("developer error: agent middleware not provided")
|
|
|
|
|
}
|
2026-01-19 12:36:33 +00:00
|
|
|
return aw
|
2022-04-11 16:06:15 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-19 12:36:33 +00:00
|
|
|
// ExtractWorkspaceAgentAndWorkspaceParam grabs a workspace agent and its associated workspace from the "workspaceagent" URL parameter.
|
|
|
|
|
func ExtractWorkspaceAgentAndWorkspaceParam(db database.Store) func(http.Handler) http.Handler {
|
2022-04-11 16:06:15 -05:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 17:07:00 -05:00
|
|
|
ctx := r.Context()
|
2023-07-19 11:11:11 -05:00
|
|
|
agentUUID, parsed := ParseUUIDParam(rw, r, "workspaceagent")
|
2022-04-11 16:06:15 -05:00
|
|
|
if !parsed {
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-06-24 10:25:01 -05:00
|
|
|
|
2026-01-19 12:36:33 +00:00
|
|
|
agentWithWorkspace, err := db.GetWorkspaceAgentAndWorkspaceByID(ctx, agentUUID)
|
2023-12-12 15:14:32 -06:00
|
|
|
if httpapi.Is404Error(err) {
|
2022-09-21 17:07:00 -05:00
|
|
|
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
|
2023-12-12 15:14:32 -06:00
|
|
|
Message: "Agent doesn't exist with that id, or you do not have access to it.",
|
2022-04-11 16:06:15 -05:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-06-24 10:25:01 -05:00
|
|
|
|
2026-01-19 12:36:33 +00:00
|
|
|
ctx = context.WithValue(ctx, workspaceAgentAndWorkspaceParamContextKey{}, agentWithWorkspace)
|
|
|
|
|
chi.RouteContext(ctx).URLParams.Add("workspace", agentWithWorkspace.WorkspaceTable.ID.String())
|
2025-04-15 13:27:23 +02:00
|
|
|
|
|
|
|
|
if rlogger := loggermw.RequestLoggerFromContext(ctx); rlogger != nil {
|
|
|
|
|
rlogger.WithFields(
|
2026-01-19 12:36:33 +00:00
|
|
|
slog.F("workspace_name", agentWithWorkspace.WorkspaceTable.Name),
|
|
|
|
|
slog.F("agent_name", agentWithWorkspace.WorkspaceAgent.Name),
|
2025-04-15 13:27:23 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 16:06:15 -05:00
|
|
|
next.ServeHTTP(rw, r.WithContext(ctx))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|