2022-01-13 22:55:28 +00:00
|
|
|
package coderd
|
|
|
|
|
|
|
|
import (
|
2022-04-08 14:35:29 +00:00
|
|
|
"context"
|
2022-04-19 13:48:13 +00:00
|
|
|
"crypto/x509"
|
2022-04-08 14:35:29 +00:00
|
|
|
"fmt"
|
2022-06-13 18:14:22 +00:00
|
|
|
"io"
|
2022-01-13 22:55:28 +00:00
|
|
|
"net/http"
|
2022-02-28 18:26:01 +00:00
|
|
|
"net/url"
|
2022-06-21 16:53:36 +00:00
|
|
|
"path/filepath"
|
2022-02-20 22:29:16 +00:00
|
|
|
"sync"
|
2022-03-22 19:17:50 +00:00
|
|
|
"time"
|
2022-01-13 22:55:28 +00:00
|
|
|
|
2022-06-13 18:14:22 +00:00
|
|
|
"github.com/andybalholm/brotli"
|
2022-01-31 03:13:14 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-05-03 12:48:02 +00:00
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2022-06-13 18:14:22 +00:00
|
|
|
"github.com/klauspost/compress/zstd"
|
2022-04-18 22:40:25 +00:00
|
|
|
"github.com/pion/webrtc/v3"
|
2022-06-13 18:14:22 +00:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2022-05-03 21:10:19 +00:00
|
|
|
"golang.org/x/xerrors"
|
2022-02-21 20:36:29 +00:00
|
|
|
"google.golang.org/api/idtoken"
|
2022-01-20 13:46:51 +00:00
|
|
|
|
2022-01-13 22:55:28 +00:00
|
|
|
"cdr.dev/slog"
|
2022-04-07 17:18:58 +00:00
|
|
|
"github.com/coder/coder/buildinfo"
|
2022-03-28 19:31:03 +00:00
|
|
|
"github.com/coder/coder/coderd/awsidentity"
|
2022-03-25 21:07:45 +00:00
|
|
|
"github.com/coder/coder/coderd/database"
|
2022-04-06 00:18:26 +00:00
|
|
|
"github.com/coder/coder/coderd/gitsshkey"
|
2022-03-25 21:07:45 +00:00
|
|
|
"github.com/coder/coder/coderd/httpapi"
|
|
|
|
"github.com/coder/coder/coderd/httpmw"
|
2022-05-03 21:10:19 +00:00
|
|
|
"github.com/coder/coder/coderd/rbac"
|
2022-06-17 05:26:40 +00:00
|
|
|
"github.com/coder/coder/coderd/telemetry"
|
2022-05-19 22:43:07 +00:00
|
|
|
"github.com/coder/coder/coderd/tracing"
|
2022-04-18 22:40:25 +00:00
|
|
|
"github.com/coder/coder/coderd/turnconn"
|
2022-06-04 20:13:37 +00:00
|
|
|
"github.com/coder/coder/coderd/wsconncache"
|
2022-04-07 17:18:58 +00:00
|
|
|
"github.com/coder/coder/codersdk"
|
2022-01-18 21:13:19 +00:00
|
|
|
"github.com/coder/coder/site"
|
2022-01-13 22:55:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Options are requires parameters for Coder to start.
|
|
|
|
type Options struct {
|
2022-04-18 22:40:25 +00:00
|
|
|
AccessURL *url.URL
|
|
|
|
Logger slog.Logger
|
|
|
|
Database database.Store
|
|
|
|
Pubsub database.Pubsub
|
2022-03-31 17:31:06 +00:00
|
|
|
|
2022-06-21 16:53:36 +00:00
|
|
|
// CacheDir is used for caching files served by the API.
|
|
|
|
CacheDir string
|
|
|
|
|
2022-04-18 22:40:25 +00:00
|
|
|
AgentConnectionUpdateFrequency time.Duration
|
2022-04-22 20:27:55 +00:00
|
|
|
// APIRateLimit is the minutely throughput rate limit per user or ip.
|
|
|
|
// Setting a rate limit <0 will disable the rate limiter across the entire
|
|
|
|
// app. Specific routes may have their own limiters.
|
|
|
|
APIRateLimit int
|
|
|
|
AWSCertificates awsidentity.Certificates
|
2022-06-04 20:13:37 +00:00
|
|
|
Authorizer rbac.Authorizer
|
2022-04-22 20:27:55 +00:00
|
|
|
AzureCertificates x509.VerifyOptions
|
|
|
|
GoogleTokenValidator *idtoken.Validator
|
2022-04-23 22:58:57 +00:00
|
|
|
GithubOAuth2Config *GithubOAuth2Config
|
2022-04-22 20:27:55 +00:00
|
|
|
ICEServers []webrtc.ICEServer
|
|
|
|
SecureAuthCookie bool
|
|
|
|
SSHKeygenAlgorithm gitsshkey.Algorithm
|
2022-06-17 05:26:40 +00:00
|
|
|
Telemetry telemetry.Reporter
|
2022-04-22 20:27:55 +00:00
|
|
|
TURNServer *turnconn.Server
|
2022-05-19 22:43:07 +00:00
|
|
|
TracerProvider *sdktrace.TracerProvider
|
2022-01-13 22:55:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
// New constructs a Coder API handler.
|
|
|
|
func New(options *Options) *API {
|
2022-03-22 19:17:50 +00:00
|
|
|
if options.AgentConnectionUpdateFrequency == 0 {
|
|
|
|
options.AgentConnectionUpdateFrequency = 3 * time.Second
|
|
|
|
}
|
2022-04-22 20:27:55 +00:00
|
|
|
if options.APIRateLimit == 0 {
|
|
|
|
options.APIRateLimit = 512
|
|
|
|
}
|
2022-05-03 21:10:19 +00:00
|
|
|
if options.Authorizer == nil {
|
|
|
|
var err error
|
|
|
|
options.Authorizer, err = rbac.NewAuthorizer()
|
|
|
|
if err != nil {
|
|
|
|
// This should never happen, as the unit tests would fail if the
|
|
|
|
// default built in authorizer failed.
|
|
|
|
panic(xerrors.Errorf("rego authorize panic: %w", err))
|
|
|
|
}
|
|
|
|
}
|
2022-05-26 03:14:08 +00:00
|
|
|
|
2022-06-21 16:53:36 +00:00
|
|
|
siteCacheDir := options.CacheDir
|
|
|
|
if siteCacheDir != "" {
|
|
|
|
siteCacheDir = filepath.Join(siteCacheDir, "site")
|
|
|
|
}
|
|
|
|
binFS, err := site.ExtractOrReadBinFS(siteCacheDir, site.FS())
|
|
|
|
if err != nil {
|
|
|
|
panic(xerrors.Errorf("read site bin failed: %w", err))
|
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
api := &API{
|
2022-06-04 20:13:37 +00:00
|
|
|
Options: options,
|
|
|
|
Handler: r,
|
2022-06-21 16:53:36 +00:00
|
|
|
siteHandler: site.Handler(site.FS(), binFS),
|
2022-05-26 03:14:08 +00:00
|
|
|
}
|
2022-06-04 20:13:37 +00:00
|
|
|
api.workspaceAgentCache = wsconncache.New(api.dialWorkspaceAgent, 0)
|
2022-05-26 03:14:08 +00:00
|
|
|
|
2022-04-23 22:58:57 +00:00
|
|
|
apiKeyMiddleware := httpmw.ExtractAPIKey(options.Database, &httpmw.OAuth2Configs{
|
|
|
|
Github: options.GithubOAuth2Config,
|
|
|
|
})
|
2022-05-03 21:10:19 +00:00
|
|
|
|
2022-05-03 12:48:02 +00:00
|
|
|
r.Use(
|
|
|
|
func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
next.ServeHTTP(middleware.NewWrapResponseWriter(w, r.ProtoMajor), r)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
httpmw.Prometheus,
|
2022-05-26 03:14:08 +00:00
|
|
|
tracing.HTTPMW(api.TracerProvider, "coderd.http"),
|
2022-05-03 12:48:02 +00:00
|
|
|
)
|
|
|
|
|
2022-06-04 20:13:37 +00:00
|
|
|
apps := func(r chi.Router) {
|
|
|
|
r.Use(
|
|
|
|
httpmw.RateLimitPerMinute(options.APIRateLimit),
|
|
|
|
apiKeyMiddleware,
|
|
|
|
httpmw.ExtractUserParam(api.Database),
|
|
|
|
)
|
|
|
|
r.Get("/*", api.workspaceAppsProxyPath)
|
|
|
|
}
|
|
|
|
// %40 is the encoded character of the @ symbol. VS Code Web does
|
|
|
|
// not handle character encoding properly, so it's safe to assume
|
|
|
|
// other applications might not as well.
|
|
|
|
r.Route("/%40{user}/{workspacename}/apps/{workspaceapp}", apps)
|
|
|
|
r.Route("/@{user}/{workspacename}/apps/{workspaceapp}", apps)
|
|
|
|
|
2022-01-13 22:55:28 +00:00
|
|
|
r.Route("/api/v2", func(r chi.Router) {
|
2022-05-03 14:22:08 +00:00
|
|
|
r.NotFound(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
|
|
|
|
Message: "Route not found.",
|
|
|
|
})
|
|
|
|
})
|
2022-04-04 22:32:05 +00:00
|
|
|
r.Use(
|
|
|
|
// Specific routes can specify smaller limits.
|
2022-04-22 20:27:55 +00:00
|
|
|
httpmw.RateLimitPerMinute(options.APIRateLimit),
|
2022-05-26 03:14:08 +00:00
|
|
|
debugLogRequest(api.Logger),
|
2022-04-04 22:32:05 +00:00
|
|
|
)
|
2022-01-13 22:55:28 +00:00
|
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
2022-01-20 13:46:51 +00:00
|
|
|
httpapi.Write(w, http.StatusOK, httpapi.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
//nolint:gocritic
|
2022-01-13 22:55:28 +00:00
|
|
|
Message: "👋",
|
|
|
|
})
|
|
|
|
})
|
2022-05-27 14:59:13 +00:00
|
|
|
// All CSP errors will be logged
|
|
|
|
r.Post("/csp/reports", api.logReportCSPViolations)
|
|
|
|
|
2022-04-07 17:18:58 +00:00
|
|
|
r.Route("/buildinfo", func(r chi.Router) {
|
|
|
|
r.Get("/", func(rw http.ResponseWriter, r *http.Request) {
|
2022-04-12 15:17:33 +00:00
|
|
|
httpapi.Write(rw, http.StatusOK, codersdk.BuildInfoResponse{
|
2022-04-07 17:18:58 +00:00
|
|
|
ExternalURL: buildinfo.ExternalURL(),
|
|
|
|
Version: buildinfo.Version(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2022-03-07 17:40:54 +00:00
|
|
|
r.Route("/files", func(r chi.Router) {
|
2022-04-04 22:32:05 +00:00
|
|
|
r.Use(
|
2022-04-23 22:58:57 +00:00
|
|
|
apiKeyMiddleware,
|
2022-04-04 22:32:05 +00:00
|
|
|
// This number is arbitrary, but reading/writing
|
|
|
|
// file content is expensive so it should be small.
|
|
|
|
httpmw.RateLimitPerMinute(12),
|
|
|
|
)
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/{hash}", api.fileByHash)
|
|
|
|
r.Post("/", api.postFile)
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
2022-05-26 16:20:54 +00:00
|
|
|
r.Route("/provisionerdaemons", func(r chi.Router) {
|
|
|
|
r.Use(
|
|
|
|
apiKeyMiddleware,
|
|
|
|
)
|
|
|
|
r.Get("/", api.provisionerDaemons)
|
|
|
|
})
|
2022-05-27 16:19:13 +00:00
|
|
|
r.Route("/organizations", func(r chi.Router) {
|
2022-01-25 19:52:58 +00:00
|
|
|
r.Use(
|
2022-04-23 22:58:57 +00:00
|
|
|
apiKeyMiddleware,
|
2022-01-25 19:52:58 +00:00
|
|
|
)
|
2022-05-27 16:19:13 +00:00
|
|
|
r.Post("/", api.postOrganizations)
|
|
|
|
r.Route("/{organization}", func(r chi.Router) {
|
|
|
|
r.Use(
|
|
|
|
httpmw.ExtractOrganizationParam(options.Database),
|
|
|
|
)
|
|
|
|
r.Get("/", api.organization)
|
|
|
|
r.Post("/templateversions", api.postTemplateVersionsByOrganization)
|
|
|
|
r.Route("/templates", func(r chi.Router) {
|
|
|
|
r.Post("/", api.postTemplateByOrganization)
|
|
|
|
r.Get("/", api.templatesByOrganization)
|
|
|
|
r.Get("/{templatename}", api.templateByOrganizationAndName)
|
2022-04-25 21:11:03 +00:00
|
|
|
})
|
2022-06-03 19:36:08 +00:00
|
|
|
r.Post("/workspaces", api.postWorkspacesByOrganization)
|
2022-05-27 16:19:13 +00:00
|
|
|
r.Route("/members", func(r chi.Router) {
|
|
|
|
r.Get("/roles", api.assignableOrgRoles)
|
|
|
|
r.Route("/{user}", func(r chi.Router) {
|
|
|
|
r.Use(
|
|
|
|
httpmw.ExtractUserParam(options.Database),
|
|
|
|
httpmw.ExtractOrganizationMemberParam(options.Database),
|
|
|
|
)
|
|
|
|
r.Put("/roles", api.putMemberRoles)
|
|
|
|
})
|
2022-04-29 14:04:19 +00:00
|
|
|
})
|
|
|
|
})
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
|
|
|
r.Route("/parameters/{scope}/{id}", func(r chi.Router) {
|
2022-05-31 13:06:42 +00:00
|
|
|
r.Use(apiKeyMiddleware)
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Post("/", api.postParameter)
|
|
|
|
r.Get("/", api.parameters)
|
2022-03-07 17:40:54 +00:00
|
|
|
r.Route("/{name}", func(r chi.Router) {
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Delete("/", api.deleteParameter)
|
2022-01-23 05:58:10 +00:00
|
|
|
})
|
2022-01-20 13:46:51 +00:00
|
|
|
})
|
2022-04-06 17:42:40 +00:00
|
|
|
r.Route("/templates/{template}", func(r chi.Router) {
|
2022-01-24 17:07:42 +00:00
|
|
|
r.Use(
|
2022-04-23 22:58:57 +00:00
|
|
|
apiKeyMiddleware,
|
2022-04-06 17:42:40 +00:00
|
|
|
httpmw.ExtractTemplateParam(options.Database),
|
2022-01-24 17:07:42 +00:00
|
|
|
)
|
2022-05-17 20:00:48 +00:00
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/", api.template)
|
|
|
|
r.Delete("/", api.deleteTemplate)
|
2022-06-08 14:14:57 +00:00
|
|
|
r.Patch("/", api.patchTemplateMeta)
|
2022-03-07 17:40:54 +00:00
|
|
|
r.Route("/versions", func(r chi.Router) {
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/", api.templateVersionsByTemplate)
|
|
|
|
r.Patch("/", api.patchActiveTemplateVersion)
|
|
|
|
r.Get("/{templateversionname}", api.templateVersionByName)
|
2022-01-25 19:52:58 +00:00
|
|
|
})
|
|
|
|
})
|
2022-04-06 17:42:40 +00:00
|
|
|
r.Route("/templateversions/{templateversion}", func(r chi.Router) {
|
2022-03-07 17:40:54 +00:00
|
|
|
r.Use(
|
2022-04-23 22:58:57 +00:00
|
|
|
apiKeyMiddleware,
|
2022-04-06 17:42:40 +00:00
|
|
|
httpmw.ExtractTemplateVersionParam(options.Database),
|
2022-03-07 17:40:54 +00:00
|
|
|
)
|
2022-01-25 19:52:58 +00:00
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/", api.templateVersion)
|
|
|
|
r.Patch("/cancel", api.patchCancelTemplateVersion)
|
|
|
|
r.Get("/schema", api.templateVersionSchema)
|
|
|
|
r.Get("/parameters", api.templateVersionParameters)
|
|
|
|
r.Get("/resources", api.templateVersionResources)
|
|
|
|
r.Get("/logs", api.templateVersionLogs)
|
2022-06-01 14:44:53 +00:00
|
|
|
r.Route("/dry-run", func(r chi.Router) {
|
|
|
|
r.Post("/", api.postTemplateVersionDryRun)
|
|
|
|
r.Get("/{jobID}", api.templateVersionDryRun)
|
|
|
|
r.Get("/{jobID}/resources", api.templateVersionDryRunResources)
|
|
|
|
r.Get("/{jobID}/logs", api.templateVersionDryRunLogs)
|
|
|
|
r.Patch("/{jobID}/cancel", api.patchTemplateVersionDryRunCancel)
|
|
|
|
})
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
|
|
|
r.Route("/users", func(r chi.Router) {
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/first", api.firstUser)
|
|
|
|
r.Post("/first", api.postFirstUser)
|
|
|
|
r.Post("/login", api.postLogin)
|
|
|
|
r.Get("/authmethods", api.userAuthMethods)
|
2022-04-23 22:58:57 +00:00
|
|
|
r.Route("/oauth2", func(r chi.Router) {
|
|
|
|
r.Route("/github", func(r chi.Router) {
|
|
|
|
r.Use(httpmw.ExtractOAuth2(options.GithubOAuth2Config))
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/callback", api.userOAuth2Github)
|
2022-04-23 22:58:57 +00:00
|
|
|
})
|
|
|
|
})
|
2022-03-07 17:40:54 +00:00
|
|
|
r.Group(func(r chi.Router) {
|
2022-05-03 21:10:19 +00:00
|
|
|
r.Use(
|
|
|
|
apiKeyMiddleware,
|
|
|
|
)
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Post("/", api.postUser)
|
|
|
|
r.Get("/", api.users)
|
2022-05-27 20:47:03 +00:00
|
|
|
r.Post("/logout", api.postLogout)
|
2022-05-03 21:10:19 +00:00
|
|
|
// These routes query information about site wide roles.
|
|
|
|
r.Route("/roles", func(r chi.Router) {
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/", api.assignableSiteRoles)
|
2022-05-03 21:10:19 +00:00
|
|
|
})
|
2022-03-07 17:40:54 +00:00
|
|
|
r.Route("/{user}", func(r chi.Router) {
|
|
|
|
r.Use(httpmw.ExtractUserParam(options.Database))
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/", api.userByName)
|
|
|
|
r.Put("/profile", api.putUserProfile)
|
2022-05-16 20:29:27 +00:00
|
|
|
r.Route("/status", func(r chi.Router) {
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Put("/suspend", api.putUserStatus(database.UserStatusSuspended))
|
2022-05-31 13:06:42 +00:00
|
|
|
r.Put("/activate", api.putUserStatus(database.UserStatusActive))
|
2022-05-16 20:29:27 +00:00
|
|
|
})
|
2022-05-06 14:20:08 +00:00
|
|
|
r.Route("/password", func(r chi.Router) {
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Put("/", api.putUserPassword)
|
2022-05-06 14:20:08 +00:00
|
|
|
})
|
2022-05-03 21:10:19 +00:00
|
|
|
// These roles apply to the site wide permissions.
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Put("/roles", api.putUserRoles)
|
|
|
|
r.Get("/roles", api.userRoles)
|
2022-05-03 21:10:19 +00:00
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Post("/authorization", api.checkPermissions)
|
2022-05-12 20:56:23 +00:00
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Post("/keys", api.postAPIKey)
|
2022-03-07 17:40:54 +00:00
|
|
|
r.Route("/organizations", func(r chi.Router) {
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/", api.organizationsByUser)
|
|
|
|
r.Get("/{organizationname}", api.organizationByUserAndName)
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
2022-06-10 16:08:50 +00:00
|
|
|
r.Route("/workspace/{workspacename}", func(r chi.Router) {
|
|
|
|
r.Get("/", api.workspaceByOwnerAndName)
|
|
|
|
r.Get("/builds/{buildnumber}", api.workspaceBuildByBuildNumber)
|
|
|
|
})
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/gitsshkey", api.gitSSHKey)
|
|
|
|
r.Put("/gitsshkey", api.regenerateGitSSHKey)
|
2022-01-24 17:07:42 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2022-04-11 21:06:15 +00:00
|
|
|
r.Route("/workspaceagents", func(r chi.Router) {
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Post("/azure-instance-identity", api.postWorkspaceAuthAzureInstanceIdentity)
|
|
|
|
r.Post("/aws-instance-identity", api.postWorkspaceAuthAWSInstanceIdentity)
|
|
|
|
r.Post("/google-instance-identity", api.postWorkspaceAuthGoogleInstanceIdentity)
|
2022-04-11 21:06:15 +00:00
|
|
|
r.Route("/me", func(r chi.Router) {
|
2022-03-07 17:40:54 +00:00
|
|
|
r.Use(httpmw.ExtractWorkspaceAgent(options.Database))
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/metadata", api.workspaceAgentMetadata)
|
|
|
|
r.Get("/listen", api.workspaceAgentListen)
|
|
|
|
r.Get("/gitsshkey", api.agentGitSSHKey)
|
|
|
|
r.Get("/turn", api.workspaceAgentTurn)
|
|
|
|
r.Get("/iceservers", api.workspaceAgentICEServers)
|
2022-06-24 15:25:01 +00:00
|
|
|
r.Get("/wireguardlisten", api.workspaceAgentWireguardListener)
|
|
|
|
r.Post("/keys", api.postWorkspaceAgentKeys)
|
|
|
|
r.Get("/derp", api.derpMap)
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
2022-04-11 21:06:15 +00:00
|
|
|
r.Route("/{workspaceagent}", func(r chi.Router) {
|
2022-03-07 17:40:54 +00:00
|
|
|
r.Use(
|
2022-04-23 22:58:57 +00:00
|
|
|
apiKeyMiddleware,
|
2022-04-11 21:06:15 +00:00
|
|
|
httpmw.ExtractWorkspaceAgentParam(options.Database),
|
2022-06-10 14:46:48 +00:00
|
|
|
httpmw.ExtractWorkspaceParam(options.Database),
|
2022-03-07 17:40:54 +00:00
|
|
|
)
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/", api.workspaceAgent)
|
2022-06-24 15:25:01 +00:00
|
|
|
r.Post("/peer", api.postWorkspaceAgentWireguardPeer)
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/dial", api.workspaceAgentDial)
|
|
|
|
r.Get("/turn", api.workspaceAgentTurn)
|
|
|
|
r.Get("/pty", api.workspaceAgentPTY)
|
|
|
|
r.Get("/iceservers", api.workspaceAgentICEServers)
|
2022-06-24 15:25:01 +00:00
|
|
|
r.Get("/derp", api.derpMap)
|
2022-02-21 20:36:29 +00:00
|
|
|
})
|
|
|
|
})
|
2022-04-11 21:06:15 +00:00
|
|
|
r.Route("/workspaceresources/{workspaceresource}", func(r chi.Router) {
|
|
|
|
r.Use(
|
2022-04-23 22:58:57 +00:00
|
|
|
apiKeyMiddleware,
|
2022-04-11 21:06:15 +00:00
|
|
|
httpmw.ExtractWorkspaceResourceParam(options.Database),
|
|
|
|
httpmw.ExtractWorkspaceParam(options.Database),
|
|
|
|
)
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/", api.workspaceResource)
|
2022-04-11 21:06:15 +00:00
|
|
|
})
|
2022-05-18 15:09:07 +00:00
|
|
|
r.Route("/workspaces", func(r chi.Router) {
|
2022-02-12 19:34:04 +00:00
|
|
|
r.Use(
|
2022-04-23 22:58:57 +00:00
|
|
|
apiKeyMiddleware,
|
2022-02-12 19:34:04 +00:00
|
|
|
)
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/", api.workspaces)
|
2022-05-18 15:09:07 +00:00
|
|
|
r.Route("/{workspace}", func(r chi.Router) {
|
|
|
|
r.Use(
|
|
|
|
httpmw.ExtractWorkspaceParam(options.Database),
|
|
|
|
)
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/", api.workspace)
|
2022-05-18 15:09:07 +00:00
|
|
|
r.Route("/builds", func(r chi.Router) {
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/", api.workspaceBuilds)
|
|
|
|
r.Post("/", api.postWorkspaceBuilds)
|
|
|
|
r.Get("/{workspacebuildname}", api.workspaceBuildByName)
|
2022-05-18 15:09:07 +00:00
|
|
|
})
|
|
|
|
r.Route("/autostart", func(r chi.Router) {
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Put("/", api.putWorkspaceAutostart)
|
2022-05-18 15:09:07 +00:00
|
|
|
})
|
2022-05-19 19:09:27 +00:00
|
|
|
r.Route("/ttl", func(r chi.Router) {
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Put("/", api.putWorkspaceTTL)
|
2022-05-18 15:09:07 +00:00
|
|
|
})
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/watch", api.watchWorkspace)
|
2022-05-26 17:08:11 +00:00
|
|
|
r.Put("/extend", api.putExtendWorkspace)
|
2022-04-07 09:03:35 +00:00
|
|
|
})
|
2022-02-12 19:34:04 +00:00
|
|
|
})
|
2022-03-07 17:40:54 +00:00
|
|
|
r.Route("/workspacebuilds/{workspacebuild}", func(r chi.Router) {
|
2022-02-12 19:34:04 +00:00
|
|
|
r.Use(
|
2022-04-23 22:58:57 +00:00
|
|
|
apiKeyMiddleware,
|
2022-03-07 17:40:54 +00:00
|
|
|
httpmw.ExtractWorkspaceBuildParam(options.Database),
|
|
|
|
httpmw.ExtractWorkspaceParam(options.Database),
|
2022-02-12 19:34:04 +00:00
|
|
|
)
|
2022-05-26 03:14:08 +00:00
|
|
|
r.Get("/", api.workspaceBuild)
|
|
|
|
r.Patch("/cancel", api.patchCancelWorkspaceBuild)
|
|
|
|
r.Get("/logs", api.workspaceBuildLogs)
|
|
|
|
r.Get("/resources", api.workspaceBuildResources)
|
|
|
|
r.Get("/state", api.workspaceBuildState)
|
2022-02-12 19:34:04 +00:00
|
|
|
})
|
2022-01-13 22:55:28 +00:00
|
|
|
})
|
2022-06-13 18:14:22 +00:00
|
|
|
|
|
|
|
r.NotFound(compressHandler(http.HandlerFunc(api.siteHandler.ServeHTTP)).ServeHTTP)
|
2022-05-26 03:14:08 +00:00
|
|
|
return api
|
2022-01-13 22:55:28 +00:00
|
|
|
}
|
2022-02-01 22:15:26 +00:00
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
type API struct {
|
2022-02-20 22:29:16 +00:00
|
|
|
*Options
|
|
|
|
|
2022-06-04 20:13:37 +00:00
|
|
|
Handler chi.Router
|
|
|
|
siteHandler http.Handler
|
|
|
|
websocketWaitMutex sync.Mutex
|
|
|
|
websocketWaitGroup sync.WaitGroup
|
|
|
|
workspaceAgentCache *wsconncache.Cache
|
2022-02-01 22:15:26 +00:00
|
|
|
}
|
2022-04-08 14:35:29 +00:00
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
// Close waits for all WebSocket connections to drain before returning.
|
2022-06-04 20:13:37 +00:00
|
|
|
func (api *API) Close() error {
|
2022-05-26 03:14:08 +00:00
|
|
|
api.websocketWaitMutex.Lock()
|
|
|
|
api.websocketWaitGroup.Wait()
|
|
|
|
api.websocketWaitMutex.Unlock()
|
2022-06-04 20:13:37 +00:00
|
|
|
|
|
|
|
return api.workspaceAgentCache.Close()
|
2022-05-26 03:14:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 14:35:29 +00:00
|
|
|
func debugLogRequest(log slog.Logger) func(http.Handler) http.Handler {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Debug(context.Background(), fmt.Sprintf("%s %s", r.Method, r.URL.Path))
|
|
|
|
next.ServeHTTP(rw, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-13 18:14:22 +00:00
|
|
|
|
|
|
|
func compressHandler(h http.Handler) http.Handler {
|
|
|
|
cmp := middleware.NewCompressor(5,
|
|
|
|
"text/*",
|
|
|
|
"application/*",
|
|
|
|
"image/*",
|
|
|
|
)
|
|
|
|
cmp.SetEncoder("br", func(w io.Writer, level int) io.Writer {
|
|
|
|
return brotli.NewWriterLevel(w, level)
|
|
|
|
})
|
|
|
|
cmp.SetEncoder("zstd", func(w io.Writer, level int) io.Writer {
|
|
|
|
zw, err := zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(level)))
|
|
|
|
if err != nil {
|
|
|
|
panic("invalid zstd compressor: " + err.Error())
|
|
|
|
}
|
|
|
|
return zw
|
|
|
|
})
|
|
|
|
|
|
|
|
return cmp.Handler(h)
|
|
|
|
}
|