2022-08-22 15:02:50 -07:00
package coderd
import (
2022-08-29 16:45:40 -07:00
"context"
2022-09-19 23:11:01 -05:00
"crypto/ed25519"
2023-07-26 09:21:04 -07:00
"fmt"
"math"
2022-09-19 23:11:01 -05:00
"net/http"
2023-07-26 09:21:04 -07:00
"net/url"
"strconv"
"strings"
2022-09-19 23:11:01 -05:00
"sync"
"time"
2022-08-29 16:45:40 -07:00
2024-08-12 16:01:22 -05:00
"github.com/coder/coder/v2/buildinfo"
2024-01-29 12:17:31 +04:00
"github.com/coder/coder/v2/coderd/appearance"
2024-04-17 13:30:32 -04:00
"github.com/coder/coder/v2/coderd/database"
2024-08-23 16:21:58 -05:00
"github.com/coder/coder/v2/coderd/entitlements"
2024-08-30 11:19:36 -05:00
"github.com/coder/coder/v2/coderd/idpsync"
2024-02-13 09:31:20 -05:00
agplportsharing "github.com/coder/coder/v2/coderd/portsharing"
2025-08-07 11:21:17 -05:00
"github.com/coder/coder/v2/coderd/pproflabel"
2025-04-25 11:07:15 +02:00
agplprebuilds "github.com/coder/coder/v2/coderd/prebuilds"
2024-05-15 09:46:35 -05:00
"github.com/coder/coder/v2/coderd/rbac/policy"
2025-07-22 13:39:26 +10:00
"github.com/coder/coder/v2/coderd/wsbuilder"
2025-07-15 14:36:06 +10:00
"github.com/coder/coder/v2/enterprise/coderd/connectionlog"
2024-08-30 11:19:36 -05:00
"github.com/coder/coder/v2/enterprise/coderd/enidpsync"
2024-02-13 09:31:20 -05:00
"github.com/coder/coder/v2/enterprise/coderd/portsharing"
2025-08-07 11:21:17 -05:00
"github.com/coder/quartz"
2024-01-29 12:17:31 +04:00
2022-08-22 15:02:50 -07:00
"golang.org/x/xerrors"
2023-07-26 09:21:04 -07:00
"tailscale.com/tailcfg"
2022-08-22 15:02:50 -07:00
2022-09-19 23:11:01 -05:00
"github.com/cenkalti/backoff/v4"
"github.com/go-chi/chi/v5"
2023-01-13 16:07:15 -06:00
"github.com/prometheus/client_golang/prometheus"
2022-09-19 23:11:01 -05:00
"cdr.dev/slog"
2024-07-24 13:38:21 -03:00
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd"
agplaudit "github.com/coder/coder/v2/coderd/audit"
2025-07-15 14:36:06 +10:00
agplconnectionlog "github.com/coder/coder/v2/coderd/connectionlog"
2023-10-18 17:07:21 -05:00
agpldbauthz "github.com/coder/coder/v2/coderd/database/dbauthz"
2024-04-17 13:30:32 -04:00
"github.com/coder/coder/v2/coderd/database/dbtime"
2023-11-24 15:06:51 +00:00
"github.com/coder/coder/v2/coderd/healthcheck"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/coderd/rbac"
agplschedule "github.com/coder/coder/v2/coderd/schedule"
"github.com/coder/coder/v2/codersdk"
2023-10-18 17:07:21 -05:00
"github.com/coder/coder/v2/enterprise/coderd/dbauthz"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/enterprise/coderd/license"
2025-04-25 11:07:15 +02:00
"github.com/coder/coder/v2/enterprise/coderd/prebuilds"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/enterprise/coderd/proxyhealth"
"github.com/coder/coder/v2/enterprise/coderd/schedule"
2023-09-07 15:49:49 +01:00
"github.com/coder/coder/v2/enterprise/dbcrypt"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/enterprise/derpmesh"
"github.com/coder/coder/v2/enterprise/replicasync"
"github.com/coder/coder/v2/enterprise/tailnet"
"github.com/coder/coder/v2/provisionerd/proto"
agpltailnet "github.com/coder/coder/v2/tailnet"
2022-08-22 15:02:50 -07:00
)
2022-09-19 23:11:01 -05:00
// New constructs an Enterprise coderd API instance.
// This handler is designed to wrap the AGPL Coder code and
// layer Enterprise functionality on top as much as possible.
2023-06-13 20:18:31 +03:00
func New ( ctx context . Context , options * Options ) ( _ * API , err error ) {
2022-09-19 23:11:01 -05:00
if options . EntitlementsUpdateInterval == 0 {
options . EntitlementsUpdateInterval = 10 * time . Minute
}
2023-09-07 15:49:49 +01:00
if options . LicenseKeys == nil {
options . LicenseKeys = Keys
2022-09-19 23:11:01 -05:00
}
2022-10-15 02:46:38 +10:00
if options . Options == nil {
options . Options = & coderd . Options {}
}
2023-01-13 16:07:15 -06:00
if options . PrometheusRegistry == nil {
options . PrometheusRegistry = prometheus . NewRegistry ()
}
2022-10-15 02:46:38 +10:00
if options . Options . Authorizer == nil {
2023-02-15 08:56:07 -06:00
options . Options . Authorizer = rbac . NewCachingAuthorizer ( options . PrometheusRegistry )
2025-04-10 10:36:27 -07:00
if buildinfo . IsDev () {
options . Authorizer = rbac . Recorder ( options . Authorizer )
}
2022-10-15 02:46:38 +10:00
}
2024-04-17 13:30:32 -04:00
if options . ReplicaErrorGracePeriod == 0 {
// This will prevent the error from being shown for a minute
// from when an additional replica was started.
options . ReplicaErrorGracePeriod = time . Minute
}
2024-08-26 13:05:03 -05:00
if options . Entitlements == nil {
options . Entitlements = entitlements . New ()
}
2023-07-20 06:35:41 -07:00
2022-09-19 23:11:01 -05:00
ctx , cancelFunc := context . WithCancel ( ctx )
2023-04-24 10:25:35 -05:00
2023-09-07 15:49:49 +01:00
if options . ExternalTokenEncryption == nil {
options . ExternalTokenEncryption = make ([] dbcrypt . Cipher , 0 )
}
// Database encryption is an enterprise feature, but as checking license entitlements
// depends on the database, we end up in a chicken-and-egg situation. To avoid this,
// we always enable it but only soft-enforce it.
if len ( options . ExternalTokenEncryption ) > 0 {
var keyDigests [] string
for _ , cipher := range options . ExternalTokenEncryption {
keyDigests = append ( keyDigests , cipher . HexDigest ())
}
options . Logger . Info ( ctx , "database encryption enabled" , slog . F ( "keys" , keyDigests ))
}
cryptDB , err := dbcrypt . New ( ctx , options . Database , options . ExternalTokenEncryption ... )
if err != nil {
cancelFunc ()
// If we fail to initialize the database, it's likely that the
// database is encrypted with an unknown external token encryption key.
// This is a fatal error.
var derr * dbcrypt . DecryptFailedError
if xerrors . As ( err , & derr ) {
2024-07-10 21:31:37 +03:00
return nil , xerrors . Errorf ( "database encrypted with unknown key, either add the key or see https://coder.com/docs/admin/encryption#disabling-encryption: %w" , derr )
2023-09-07 15:49:49 +01:00
}
return nil , xerrors . Errorf ( "init database encryption: %w" , err )
}
2024-08-23 16:21:58 -05:00
2023-09-07 15:49:49 +01:00
options . Database = cryptDB
2024-09-11 13:43:50 -05:00
if options . IDPSync == nil {
options . IDPSync = enidpsync . NewSync ( options . Logger , options . RuntimeConfig , options . Entitlements , idpsync . FromDeploymentValues ( options . DeploymentValues ))
}
2025-07-15 14:36:06 +10:00
if options . ConnectionLogger == nil {
options . ConnectionLogger = connectionlog . NewConnectionLogger (
connectionlog . NewDBBackend ( options . Database ),
connectionlog . NewSlogBackend ( options . Logger ),
)
}
2023-09-07 15:49:49 +01:00
api := & API {
2024-08-26 13:05:03 -05:00
ctx : ctx ,
cancel : cancelFunc ,
Options : options ,
2023-08-04 12:32:28 +04:00
provisionerDaemonAuth : & provisionerDaemonAuth {
psk : options . ProvisionerDaemonPSK ,
authorizer : options . Authorizer ,
2024-07-25 10:22:55 -04:00
db : options . Database ,
2023-08-04 12:32:28 +04:00
},
2024-08-23 16:21:58 -05:00
licenseMetricsCollector : & license . MetricsCollector {
2024-08-26 13:05:03 -05:00
Entitlements : options . Entitlements ,
2024-08-23 16:21:58 -05:00
},
2022-09-19 23:11:01 -05:00
}
2024-03-25 20:01:42 +01:00
// This must happen before coderd initialization!
options . PostAuthAdditionalHeadersFunc = api . writeEntitlementWarningsHeader
api . AGPL = coderd . New ( options . Options )
2023-06-13 20:18:31 +03:00
defer func () {
if err != nil {
_ = api . Close ()
}
}()
2022-10-17 08:43:30 -05:00
2023-12-14 15:52:52 -06:00
api . AGPL . Options . ParseLicenseClaims = func ( rawJWT string ) ( email string , trial bool , err error ) {
c , err := license . ParseClaims ( rawJWT , Keys )
if err != nil {
return "" , false , err
}
return c . Subject , c . Trial , nil
}
2023-06-30 11:32:35 -04:00
api . AGPL . SiteHandler . RegionsFetcher = func ( ctx context . Context ) ( any , error ) {
// If the user can read the workspace proxy resource, return that.
// If not, always default to the regions.
2023-10-18 17:07:21 -05:00
actor , ok := agpldbauthz . ActorFromContext ( ctx )
2024-05-15 09:46:35 -05:00
if ok && api . Authorizer . Authorize ( ctx , actor , policy . ActionRead , rbac . ResourceWorkspaceProxy ) == nil {
2023-06-30 11:32:35 -04:00
return api . fetchWorkspaceProxies ( ctx )
}
return api . fetchRegions ( ctx )
}
2024-07-02 01:50:52 +10:00
api . tailnetService , err = tailnet . NewClientService ( agpltailnet . ClientServiceOptions {
Logger : api . Logger . Named ( "tailnetclient" ),
CoordPtr : & api . AGPL . TailnetCoordinator ,
DERPMapUpdateFrequency : api . Options . DERPMapUpdateFrequency ,
DERPMapFn : api . AGPL . DERPMap ,
NetworkTelemetryHandler : api . AGPL . NetworkTelemetryBatcher . Handler ,
2024-08-20 17:16:49 +10:00
ResumeTokenProvider : api . AGPL . CoordinatorResumeTokenProvider ,
2024-07-02 01:50:52 +10:00
})
2024-01-18 10:10:36 +04:00
if err != nil {
api . Logger . Fatal ( api . ctx , "failed to initialize tailnet client service" , slog . Error ( err ))
}
2023-02-02 13:53:48 -06:00
2022-09-19 23:11:01 -05:00
oauthConfigs := & httpmw . OAuth2Configs {
Github : options . GithubOAuth2Config ,
OIDC : options . OIDCConfig ,
}
2023-03-08 06:38:11 +11:00
apiKeyMiddleware := httpmw . ExtractAPIKeyMW ( httpmw . ExtractAPIKeyConfig {
2024-03-25 20:01:42 +01:00
DB : options . Database ,
2024-10-31 17:55:42 -05:00
ActivateDormantUser : coderd . ActivateDormantUser ( options . Logger , & api . AGPL . Auditor , options . Database ),
2024-03-25 20:01:42 +01:00
OAuth2Configs : oauthConfigs ,
RedirectToLogin : false ,
2024-04-10 10:34:49 -05:00
DisableSessionExpiryRefresh : options . DeploymentValues . Sessions . DisableExpiryRefresh . Value (),
2024-03-25 20:01:42 +01:00
Optional : false ,
SessionTokenFunc : nil , // Default behavior
PostAuthAdditionalHeadersFunc : options . PostAuthAdditionalHeadersFunc ,
2022-09-23 08:30:32 +10:00
})
2023-06-30 10:41:29 -08:00
apiKeyMiddlewareOptional := httpmw . ExtractAPIKeyMW ( httpmw . ExtractAPIKeyConfig {
2024-03-25 20:01:42 +01:00
DB : options . Database ,
OAuth2Configs : oauthConfigs ,
RedirectToLogin : false ,
2024-04-10 10:34:49 -05:00
DisableSessionExpiryRefresh : options . DeploymentValues . Sessions . DisableExpiryRefresh . Value (),
2024-03-25 20:01:42 +01:00
Optional : true ,
SessionTokenFunc : nil , // Default behavior
PostAuthAdditionalHeadersFunc : options . PostAuthAdditionalHeadersFunc ,
2023-06-30 10:41:29 -08:00
})
2022-09-23 08:30:32 +10:00
2023-06-08 10:30:15 -05:00
deploymentID , err := options . Database . GetDeploymentID ( ctx )
if err != nil {
return nil , xerrors . Errorf ( "failed to get deployment ID: %w" , err )
}
2024-02-23 17:48:24 +01:00
api . AGPL . RefreshEntitlements = func ( ctx context . Context ) error {
return api . refreshEntitlements ( ctx )
}
2024-02-20 15:58:43 -08:00
2022-09-19 23:11:01 -05:00
api . AGPL . APIHandler . Group ( func ( r chi . Router ) {
r . Get ( "/entitlements" , api . serveEntitlements )
2023-04-25 07:37:52 -07:00
// /regions overrides the AGPL /regions endpoint
r . Group ( func ( r chi . Router ) {
r . Use ( apiKeyMiddleware )
r . Get ( "/regions" , api . regions )
})
2022-10-17 08:43:30 -05:00
r . Route ( "/replicas" , func ( r chi . Router ) {
r . Use ( apiKeyMiddleware )
r . Get ( "/" , api . replicas )
})
2025-07-15 14:55:34 +10:00
r . Route ( "/connectionlog" , func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
api . RequireFeatureMW ( codersdk . FeatureConnectionLog ),
)
r . Get ( "/" , api . connectionLogs )
})
2022-09-19 23:11:01 -05:00
r . Route ( "/licenses" , func ( r chi . Router ) {
r . Use ( apiKeyMiddleware )
2023-08-22 09:26:43 -05:00
r . Post ( "/refresh-entitlements" , api . postRefreshEntitlements )
2022-09-19 23:11:01 -05:00
r . Post ( "/" , api . postLicense )
r . Get ( "/" , api . licenses )
r . Delete ( "/{id}" , api . deleteLicense )
})
2023-04-20 16:59:45 -07:00
r . Route ( "/applications/reconnecting-pty-signed-token" , func ( r chi . Router ) {
r . Use ( apiKeyMiddleware )
r . Post ( "/" , api . reconnectingPTYSignedToken )
})
2023-04-04 15:07:29 -05:00
r . Route ( "/workspaceproxies" , func ( r chi . Router ) {
r . Use (
2024-07-16 13:27:12 -04:00
api . RequireFeatureMW ( codersdk . FeatureWorkspaceProxy ),
2023-04-04 15:07:29 -05:00
)
2023-04-17 14:57:21 -05:00
r . Group ( func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
)
r . Post ( "/" , api . postWorkspaceProxy )
r . Get ( "/" , api . workspaceProxies )
})
r . Route ( "/me" , func ( r chi . Router ) {
r . Use (
httpmw . ExtractWorkspaceProxy ( httpmw . ExtractWorkspaceProxyConfig {
DB : options . Database ,
Optional : false ,
}),
)
2023-07-19 11:11:11 -05:00
r . Get ( "/coordinate" , api . workspaceProxyCoordinate )
2023-04-17 14:57:21 -05:00
r . Post ( "/issue-signed-app-token" , api . workspaceProxyIssueSignedAppToken )
2023-08-16 15:22:00 +03:00
r . Post ( "/app-stats" , api . workspaceProxyReportAppStats )
2023-04-20 09:48:47 -05:00
r . Post ( "/register" , api . workspaceProxyRegister )
2023-07-26 09:21:04 -07:00
r . Post ( "/deregister" , api . workspaceProxyDeregister )
2024-09-26 21:01:49 +01:00
r . Get ( "/crypto-keys" , api . workspaceProxyCryptoKeys )
2023-04-20 09:48:47 -05:00
})
r . Route ( "/{workspaceproxy}" , func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
2023-06-08 10:30:15 -05:00
httpmw . ExtractWorkspaceProxyParam ( api . Database , deploymentID , api . AGPL . PrimaryWorkspaceProxy ),
2023-04-20 09:48:47 -05:00
)
2023-05-09 13:46:50 -05:00
r . Get ( "/" , api . workspaceProxy )
r . Patch ( "/" , api . patchWorkspaceProxy )
2023-04-20 09:48:47 -05:00
r . Delete ( "/" , api . deleteWorkspaceProxy )
2023-04-17 14:57:21 -05:00
})
2023-04-04 15:07:29 -05:00
})
2024-07-25 16:07:53 -05:00
r . Group ( func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
api . RequireFeatureMW ( codersdk . FeatureMultipleOrganizations ),
)
r . Post ( "/organizations" , api . postOrganizations )
})
r . Group ( func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
api . RequireFeatureMW ( codersdk . FeatureMultipleOrganizations ),
httpmw . ExtractOrganizationParam ( api . Database ),
)
r . Patch ( "/organizations/{organization}" , api . patchOrganization )
r . Delete ( "/organizations/{organization}" , api . deleteOrganization )
})
2024-08-05 13:42:11 -05:00
r . Group ( func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
api . RequireFeatureMW ( codersdk . FeatureCustomRoles ),
httpmw . ExtractOrganizationParam ( api . Database ),
)
2024-08-13 12:53:47 -05:00
r . Post ( "/organizations/{organization}/members/roles" , api . postOrgRoles )
r . Put ( "/organizations/{organization}/members/roles" , api . putOrgRoles )
2024-08-07 12:37:55 -05:00
r . Delete ( "/organizations/{organization}/members/roles/{roleName}" , api . deleteOrgRole )
2024-09-17 10:38:42 -05:00
})
2024-11-08 08:44:14 -06:00
r . Group ( func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
)
2024-11-18 14:31:39 -06:00
r . Route ( "/settings/idpsync" , func ( r chi . Router ) {
r . Route ( "/organization" , func ( r chi . Router ) {
r . Get ( "/" , api . organizationIDPSyncSettings )
r . Patch ( "/" , api . patchOrganizationIDPSyncSettings )
2025-01-30 13:55:17 -07:00
r . Patch ( "/config" , api . patchOrganizationIDPSyncConfig )
2025-01-30 10:52:50 -07:00
r . Patch ( "/mapping" , api . patchOrganizationIDPSyncMapping )
2024-11-18 14:31:39 -06:00
})
2025-01-30 10:52:50 -07:00
2024-11-18 14:31:39 -06:00
r . Get ( "/available-fields" , api . deploymentIDPSyncClaimFields )
2025-01-08 21:07:02 +00:00
r . Get ( "/field-values" , api . deploymentIDPSyncClaimFieldValues )
2024-11-08 08:44:14 -06:00
})
})
2024-09-17 10:38:42 -05:00
r . Group ( func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
httpmw . ExtractOrganizationParam ( api . Database ),
)
2024-09-16 11:01:37 -06:00
r . Route ( "/organizations/{organization}/settings" , func ( r chi . Router ) {
r . Get ( "/idpsync/groups" , api . groupIDPSyncSettings )
r . Patch ( "/idpsync/groups" , api . patchGroupIDPSyncSettings )
2025-01-31 12:14:24 -07:00
r . Patch ( "/idpsync/groups/config" , api . patchGroupIDPSyncConfig )
r . Patch ( "/idpsync/groups/mapping" , api . patchGroupIDPSyncMapping )
2024-09-17 10:38:42 -05:00
r . Get ( "/idpsync/roles" , api . roleIDPSyncSettings )
r . Patch ( "/idpsync/roles" , api . patchRoleIDPSyncSettings )
2025-01-31 12:14:24 -07:00
r . Patch ( "/idpsync/roles/config" , api . patchRoleIDPSyncConfig )
r . Patch ( "/idpsync/roles/mapping" , api . patchRoleIDPSyncMapping )
2025-01-30 10:52:50 -07:00
r . Get ( "/idpsync/available-fields" , api . organizationIDPSyncClaimFields )
2025-01-08 21:07:02 +00:00
r . Get ( "/idpsync/field-values" , api . organizationIDPSyncClaimFieldValues )
2024-09-16 11:01:37 -06:00
})
2024-08-05 13:42:11 -05:00
})
2024-08-21 09:25:20 -05:00
r . Group ( func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
httpmw . ExtractOrganizationParam ( api . Database ),
// Intentionally using ExtractUser instead of ExtractMember.
// It is possible for a member to be removed from an org, in which
// case their orphaned workspaces still exist. We only need
// the user_id for the query.
httpmw . ExtractUserParam ( api . Database ),
)
r . Get ( "/organizations/{organization}/members/{user}/workspace-quota" , api . workspaceQuota )
})
2022-10-10 15:37:06 -05:00
r . Route ( "/organizations/{organization}/groups" , func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
2022-10-27 16:49:35 -05:00
api . templateRBACEnabledMW ,
2022-10-10 15:37:06 -05:00
httpmw . ExtractOrganizationParam ( api . Database ),
)
r . Post ( "/" , api . postGroupByOrganization )
2023-01-11 16:05:42 +01:00
r . Get ( "/" , api . groupsByOrganization )
2022-10-27 16:49:35 -05:00
r . Route ( "/{groupName}" , func ( r chi . Router ) {
r . Use (
httpmw . ExtractGroupByNameParam ( api . Database ),
)
2023-01-13 12:27:21 +01:00
r . Get ( "/" , api . groupByOrganization )
2022-10-27 16:49:35 -05:00
})
2022-10-10 15:37:06 -05:00
})
2024-11-20 18:04:47 +01:00
r . Route ( "/provisionerkeys" , func ( r chi . Router ) {
r . Use (
httpmw . ExtractProvisionerDaemonAuthenticated ( httpmw . ExtractProvisionerAuthConfig {
DB : api . Database ,
Optional : false ,
}),
)
r . Get ( "/{provisionerkey}" , api . fetchProvisionerKey )
})
2024-07-16 13:27:12 -04:00
r . Route ( "/organizations/{organization}/provisionerkeys" , func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
httpmw . ExtractOrganizationParam ( api . Database ),
2024-11-15 14:43:32 -07:00
api . RequireFeatureMW ( codersdk . FeatureExternalProvisionerDaemons ),
2024-07-16 13:27:12 -04:00
)
r . Get ( "/" , api . provisionerKeys )
r . Post ( "/" , api . postProvisionerKey )
2024-09-16 16:02:08 -04:00
r . Get ( "/daemons" , api . provisionerKeyDaemons )
2024-07-16 13:27:12 -04:00
r . Route ( "/{provisionerkey}" , func ( r chi . Router ) {
r . Use (
httpmw . ExtractProvisionerKeyParam ( options . Database ),
)
r . Delete ( "/" , api . deleteProvisionerKey )
})
})
2023-08-04 12:32:28 +04:00
// TODO: provisioner daemons are not scoped to organizations in the database, so placing them
// under an organization route doesn't make sense. In order to allow the /serve endpoint to
// work with a pre-shared key (PSK) without an API key, these routes will simply ignore the
// value of {organization}. That is, the route will work with any organization ID, whether or
// not it exits. This doesn't leak any information about the existence of organizations, so is
// fine from a security perspective, but might be a little surprising.
//
// We may in future decide to scope provisioner daemons to organizations, so we'll keep the API
// route as is.
2025-01-14 18:40:26 +02:00
r . Route ( "/organizations/{organization}/provisionerdaemons/serve" , func ( r chi . Router ) {
2022-11-16 16:34:06 -06:00
r . Use (
api . provisionerDaemonsEnabledMW ,
2024-03-04 15:15:41 -06:00
apiKeyMiddlewareOptional ,
httpmw . ExtractProvisionerDaemonAuthenticated ( httpmw . ExtractProvisionerAuthConfig {
2024-09-27 14:06:16 -05:00
DB : api . Database ,
Optional : true ,
PSK : api . ProvisionerDaemonPSK ,
2024-07-25 10:22:55 -04:00
}),
2024-03-04 15:15:41 -06:00
// Either a user auth or provisioner auth is required
// to move forward.
httpmw . RequireAPIKeyOrProvisionerDaemonAuth (),
httpmw . ExtractOrganizationParam ( api . Database ),
2022-11-16 16:34:06 -06:00
)
2025-01-14 18:40:26 +02:00
r . Get ( "/" , api . provisionerDaemonServe )
2022-11-16 16:34:06 -06:00
})
2022-10-10 15:37:06 -05:00
r . Route ( "/templates/{template}/acl" , func ( r chi . Router ) {
r . Use (
2022-10-12 04:51:41 +10:00
api . templateRBACEnabledMW ,
2022-10-10 15:37:06 -05:00
apiKeyMiddleware ,
httpmw . ExtractTemplateParam ( api . Database ),
)
2023-07-26 10:33:48 -04:00
r . Get ( "/available" , api . templateAvailablePermissions )
2022-10-10 15:37:06 -05:00
r . Get ( "/" , api . templateACL )
r . Patch ( "/" , api . patchTemplateACL )
})
2024-08-15 13:40:15 -05:00
r . Route ( "/groups" , func ( r chi . Router ) {
2022-10-10 15:37:06 -05:00
r . Use (
2022-10-12 04:51:41 +10:00
api . templateRBACEnabledMW ,
2022-10-10 15:37:06 -05:00
apiKeyMiddleware ,
)
2024-08-15 13:40:15 -05:00
r . Get ( "/" , api . groups )
r . Route ( "/{group}" , func ( r chi . Router ) {
r . Use (
httpmw . ExtractGroupParam ( api . Database ),
)
r . Get ( "/" , api . group )
r . Patch ( "/" , api . patchGroup )
r . Delete ( "/" , api . deleteGroup )
})
2022-10-10 15:37:06 -05:00
})
2022-09-30 14:01:20 -04:00
r . Route ( "/workspace-quota" , func ( r chi . Router ) {
2022-11-14 11:57:33 -06:00
r . Use (
apiKeyMiddleware ,
)
2022-09-30 14:01:20 -04:00
r . Route ( "/{user}" , func ( r chi . Router ) {
2023-10-10 16:13:00 +04:00
r . Use ( httpmw . ExtractUserParam ( options . Database ))
2024-08-21 09:25:20 -05:00
r . Get ( "/" , api . workspaceQuotaByUser )
2022-09-30 14:01:20 -04:00
})
})
2023-01-04 15:31:45 -06:00
r . Route ( "/appearance" , func ( r chi . Router ) {
2023-06-30 10:41:29 -08:00
r . Group ( func ( r chi . Router ) {
r . Use (
apiKeyMiddlewareOptional ,
2024-03-14 12:27:32 -04:00
httpmw . ExtractWorkspaceAgentAndLatestBuild ( httpmw . ExtractWorkspaceAgentAndLatestBuildConfig {
2023-06-30 10:41:29 -08:00
DB : options . Database ,
Optional : true ,
}),
httpmw . RequireAPIKeyOrWorkspaceAgent (),
)
r . Get ( "/" , api . appearance )
})
r . Group ( func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
)
r . Put ( "/" , api . putAppearance )
})
2022-12-06 12:38:38 -06:00
})
2023-07-20 06:35:41 -07:00
r . Route ( "/users/{user}/quiet-hours" , func ( r chi . Router ) {
r . Use (
2023-08-29 11:35:05 -07:00
api . autostopRequirementEnabledMW ,
2023-07-20 06:35:41 -07:00
apiKeyMiddleware ,
2023-10-10 16:13:00 +04:00
httpmw . ExtractUserParam ( options . Database ),
2023-07-20 06:35:41 -07:00
)
r . Get ( "/" , api . userQuietHoursSchedule )
r . Put ( "/" , api . putUserQuietHoursSchedule )
})
2025-07-02 17:05:42 +02:00
r . Route ( "/prebuilds" , func ( r chi . Router ) {
r . Use (
apiKeyMiddleware ,
api . RequireFeatureMW ( codersdk . FeatureWorkspacePrebuilds ),
)
r . Get ( "/settings" , api . prebuildsSettings )
r . Put ( "/settings" , api . putPrebuildsSettings )
})
2024-08-05 16:18:45 +02:00
// The /notifications base route is mounted by the AGPL router, so we can't group it here.
// Additionally, because we have a static route for /notifications/templates/system which conflicts
// with the below route, we need to register this route without any mounts or groups to make both work.
r . With (
apiKeyMiddleware ,
httpmw . ExtractNotificationTemplateParam ( options . Database ),
). Put ( "/notifications/templates/{notification_template}/method" , api . updateNotificationTemplateMethod )
2022-09-19 23:11:01 -05:00
})
2022-09-20 15:16:26 -05:00
if len ( options . SCIMAPIKey ) != 0 {
api . AGPL . RootHandler . Route ( "/scim/v2" , func ( r chi . Router ) {
2023-02-14 08:27:06 -06:00
r . Use (
2024-10-25 13:23:12 -04:00
api . RequireFeatureMW ( codersdk . FeatureSCIM ),
2023-02-14 08:27:06 -06:00
)
2024-10-25 18:27:34 -04:00
r . Get ( "/ServiceProviderConfig" , api . scimServiceProviderConfig )
2022-09-20 15:16:26 -05:00
r . Post ( "/Users" , api . scimPostUser )
r . Route ( "/Users" , func ( r chi . Router ) {
r . Get ( "/" , api . scimGetUsers )
r . Post ( "/" , api . scimPostUser )
r . Get ( "/{id}" , api . scimGetUser )
r . Patch ( "/{id}" , api . scimPatchUser )
2024-12-12 08:11:13 -06:00
r . Put ( "/{id}" , api . scimPutUser )
2022-09-20 15:16:26 -05:00
})
2024-10-25 13:23:12 -04:00
r . NotFound ( func ( w http . ResponseWriter , r * http . Request ) {
u := r . URL . String ()
httpapi . Write ( r . Context (), w , http . StatusNotFound , codersdk . Response {
Message : fmt . Sprintf ( "SCIM endpoint %s not found" , u ),
Detail : "This endpoint is not implemented. If it is correct and required, please contact support." ,
})
})
2022-09-20 15:16:26 -05:00
})
2024-10-24 16:26:16 -04:00
} else {
// Show a helpful 404 error. Because this is not under the /api/v2 routes,
// the frontend is the fallback. A html page is not a helpful error for
// a SCIM provider. This JSON has a call to action that __may__ resolve
// the issue.
// Using Mount to cover all subroute possibilities.
api . AGPL . RootHandler . Mount ( "/scim/v2" , http . Handler ( http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
httpapi . Write ( r . Context (), w , http . StatusNotFound , codersdk . Response {
Message : "SCIM is disabled, please contact your administrator if you believe this is an error" ,
Detail : "SCIM endpoints are disabled if no SCIM is configured. Configure 'CODER_SCIM_AUTH_HEADER' to enable." ,
})
})))
2022-09-20 15:16:26 -05:00
}
2024-03-07 22:31:40 -08:00
meshTLSConfig , err := replicasync . CreateDERPMeshTLSConfig ( options . AccessURL . Hostname (), options . TLSCertificates )
if err != nil {
return nil , xerrors . Errorf ( "create DERP mesh TLS config: %w" , err )
2022-10-17 08:43:30 -05:00
}
2024-03-21 16:53:41 -05:00
// We always want to run the replica manager even if we don't have DERP
// enabled, since it's used to detect other coder servers for licensing.
2022-10-17 08:43:30 -05:00
api . replicaManager , err = replicasync . New ( ctx , options . Logger , options . Database , options . Pubsub , & replicasync . Options {
2025-03-26 01:56:39 -05:00
ID : api . AGPL . ID ,
RelayAddress : options . DERPServerRelayAddress ,
// #nosec G115 - DERP region IDs are small and fit in int32
2023-07-26 09:21:04 -07:00
RegionID : int32 ( options . DERPServerRegionID ),
TLSConfig : meshTLSConfig ,
UpdateInterval : options . ReplicaSyncUpdateInterval ,
2022-10-17 08:43:30 -05:00
})
if err != nil {
return nil , xerrors . Errorf ( "initialize replica: %w" , err )
}
2024-03-21 16:53:41 -05:00
if api . DERPServer != nil {
api . derpMesh = derpmesh . New ( options . Logger . Named ( "derpmesh" ), api . DERPServer , meshTLSConfig )
}
2022-10-17 08:43:30 -05:00
2023-12-19 14:40:22 -06:00
// Moon feature init. Proxyhealh is a go routine to periodically check
// the health of all workspace proxies.
api . ProxyHealth , err = proxyhealth . New ( & proxyhealth . Options {
Interval : options . ProxyHealthInterval ,
DB : api . Database ,
Logger : options . Logger . Named ( "proxyhealth" ),
Client : api . HTTPClient ,
Prometheus : api . PrometheusRegistry ,
})
if err != nil {
return nil , xerrors . Errorf ( "initialize proxy health: %w" , err )
2023-04-24 10:25:35 -05:00
}
2023-12-19 14:40:22 -06:00
go api . ProxyHealth . Run ( ctx )
// Force the initial loading of the cache. Do this in a go routine in case
// the calls to the workspace proxies hang and this takes some time.
go api . forceWorkspaceProxyHealthUpdate ( ctx )
// Use proxy health to return the healthy workspace proxy hostnames.
f := api . ProxyHealth . ProxyHosts
api . AGPL . WorkspaceProxyHostsFn . Store ( & f )
// Wire this up to healthcheck.
var fetchUpdater healthcheck . WorkspaceProxiesFetchUpdater = & workspaceProxiesFetchUpdater {
fetchFunc : api . fetchWorkspaceProxies ,
updateFunc : api . ProxyHealth . ForceUpdate ,
}
api . AGPL . WorkspaceProxiesFetchUpdater . Store ( & fetchUpdater )
2023-04-24 10:25:35 -05:00
2024-08-23 16:21:58 -05:00
err = api . PrometheusRegistry . Register ( api . licenseMetricsCollector )
2023-10-13 10:10:16 +02:00
if err != nil {
return nil , xerrors . Errorf ( "unable to register license metrics collector" )
}
2022-10-17 08:43:30 -05:00
err = api . updateEntitlements ( ctx )
2022-09-19 23:11:01 -05:00
if err != nil {
return nil , xerrors . Errorf ( "update entitlements: %w" , err )
}
go api . runEntitlementsLoop ( ctx )
return api , nil
}
type Options struct {
* coderd . Options
2025-07-15 14:36:06 +10:00
RBAC bool
AuditLogging bool
ConnectionLogging bool
2022-09-22 10:14:22 -05:00
// Whether to block non-browser connections.
2022-11-14 11:57:33 -06:00
BrowserOnly bool
SCIMAPIKey [] byte
2022-09-30 14:01:20 -04:00
2023-09-07 15:49:49 +01:00
ExternalTokenEncryption [] dbcrypt . Cipher
2022-10-17 08:43:30 -05:00
// Used for high availability.
2023-07-26 09:21:04 -07:00
ReplicaSyncUpdateInterval time . Duration
2024-04-17 13:30:32 -04:00
ReplicaErrorGracePeriod time . Duration
2023-07-26 09:21:04 -07:00
DERPServerRelayAddress string
DERPServerRegionID int
2022-10-17 08:43:30 -05:00
2023-07-20 06:35:41 -07:00
// Used for user quiet hours schedules.
DefaultQuietHoursSchedule string // cron schedule, if empty user quiet hours schedules are disabled
2022-09-19 23:11:01 -05:00
EntitlementsUpdateInterval time . Duration
2023-04-24 10:25:35 -05:00
ProxyHealthInterval time . Duration
2023-09-07 15:49:49 +01:00
LicenseKeys map [ string ] ed25519 . PublicKey
2023-08-04 12:32:28 +04:00
// optional pre-shared key for authentication of external provisioner daemons
ProvisionerDaemonPSK string
2023-08-31 17:59:53 +02:00
CheckInactiveUsersCancelFunc func ()
2022-09-19 23:11:01 -05:00
}
type API struct {
AGPL * coderd . API
* Options
2023-04-24 10:25:35 -05:00
// ctx is canceled immediately on shutdown, it can be used to abort
// interruptible tasks.
ctx context . Context
cancel context . CancelFunc
2022-10-17 08:43:30 -05:00
// Detects multiple Coder replicas running at the same time.
replicaManager * replicasync . Manager
// Meshes DERP connections from multiple replicas.
derpMesh * derpmesh . Mesh
2023-04-25 07:37:52 -07:00
// ProxyHealth checks the reachability of all workspace proxies.
ProxyHealth * proxyhealth . ProxyHealth
2022-10-17 08:43:30 -05:00
2023-08-04 12:32:28 +04:00
provisionerDaemonAuth * provisionerDaemonAuth
2023-10-13 10:10:16 +02:00
2024-08-23 16:21:58 -05:00
licenseMetricsCollector * license . MetricsCollector
2024-01-18 10:10:36 +04:00
tailnetService * tailnet . ClientService
2022-09-19 23:11:01 -05:00
}
2024-03-25 20:01:42 +01:00
// writeEntitlementWarningsHeader writes the entitlement warnings to the response header
// for all authenticated users with roles. If there are no warnings, this header will not be written.
//
// This header is used by the CLI to display warnings to the user without having
// to make additional requests!
2024-03-29 10:14:27 -05:00
func ( api * API ) writeEntitlementWarningsHeader ( a rbac . Subject , header http . Header ) {
2024-09-02 17:54:39 +10:00
err := api . AGPL . HTTPAuth . Authorizer . Authorize ( api . ctx , a , policy . ActionRead , rbac . ResourceDeploymentConfig )
2024-03-25 20:01:42 +01:00
if err != nil {
return
}
2024-08-26 13:05:03 -05:00
api . Entitlements . WriteEntitlementWarningHeaders ( header )
2024-03-25 20:01:42 +01:00
}
2022-09-19 23:11:01 -05:00
func ( api * API ) Close () error {
2023-08-22 09:26:43 -05:00
// Replica manager should be closed first. This is because the replica
// manager updates the replica's table in the database when it closes.
// This tells other Coderds that it is now offline.
2023-06-13 20:18:31 +03:00
if api . replicaManager != nil {
_ = api . replicaManager . Close ()
}
2023-08-22 09:26:43 -05:00
api . cancel ()
2023-06-13 20:18:31 +03:00
if api . derpMesh != nil {
_ = api . derpMesh . Close ()
}
2023-08-31 17:59:53 +02:00
if api . Options . CheckInactiveUsersCancelFunc != nil {
api . Options . CheckInactiveUsersCancelFunc ()
}
2025-04-25 11:07:15 +02:00
2022-09-19 23:11:01 -05:00
return api . AGPL . Close ()
}
func ( api * API ) updateEntitlements ( ctx context . Context ) error {
2024-10-05 06:58:43 +04:00
return api . Entitlements . Update ( ctx , func ( ctx context . Context ) ( codersdk . Entitlements , error ) {
replicas := api . replicaManager . AllPrimary ()
agedReplicas := make ([] database . Replica , 0 , len ( replicas ))
for _ , replica := range replicas {
// If a replica is less than the update interval old, we don't
// want to display a warning. In the open-source version of Coder,
// Kubernetes Pods will start up before shutting down the other,
// and we don't want to display a warning in that case.
//
// Only display warnings for long-lived replicas!
if dbtime . Now (). Sub ( replica . StartedAt ) < api . ReplicaErrorGracePeriod {
continue
2024-08-23 16:21:58 -05:00
}
2024-10-05 06:58:43 +04:00
agedReplicas = append ( agedReplicas , replica )
2022-09-19 23:11:01 -05:00
}
2024-10-05 06:58:43 +04:00
reloadedEntitlements , err := license . Entitlements (
ctx , api . Database ,
len ( agedReplicas ), len ( api . ExternalAuthConfigs ), api . LicenseKeys , map [ codersdk . FeatureName ] bool {
codersdk . FeatureAuditLog : api . AuditLogging ,
2025-07-15 14:36:06 +10:00
codersdk . FeatureConnectionLog : api . ConnectionLogging ,
2024-10-05 06:58:43 +04:00
codersdk . FeatureBrowserOnly : api . BrowserOnly ,
codersdk . FeatureSCIM : len ( api . SCIMAPIKey ) != 0 ,
codersdk . FeatureMultipleExternalAuth : len ( api . ExternalAuthConfigs ) > 1 ,
codersdk . FeatureTemplateRBAC : api . RBAC ,
codersdk . FeatureExternalTokenEncryption : len ( api . ExternalTokenEncryption ) > 0 ,
codersdk . FeatureExternalProvisionerDaemons : true ,
codersdk . FeatureAdvancedTemplateScheduling : true ,
codersdk . FeatureWorkspaceProxy : true ,
codersdk . FeatureUserRoleManagement : true ,
codersdk . FeatureAccessControl : true ,
codersdk . FeatureControlSharedPorts : true ,
})
if err != nil {
return codersdk . Entitlements {}, err
2022-09-22 10:14:22 -05:00
}
2024-10-05 06:58:43 +04:00
if reloadedEntitlements . RequireTelemetry && ! api . DeploymentValues . Telemetry . Enable . Value () {
api . Logger . Error ( ctx , "license requires telemetry enabled" )
return codersdk . Entitlements {}, entitlements . ErrLicenseRequiresTelemetry
}
featureChanged := func ( featureName codersdk . FeatureName ) ( initial , changed , enabled bool ) {
return api . Entitlements . FeatureChanged ( featureName , reloadedEntitlements . Features [ featureName ])
}
shouldUpdate := func ( initial , changed , enabled bool ) bool {
// Avoid an initial tick on startup unless the feature is enabled.
return changed || ( initial && enabled )
}
if initial , changed , enabled := featureChanged ( codersdk . FeatureAuditLog ); shouldUpdate ( initial , changed , enabled ) {
auditor := agplaudit . NewNop ()
if enabled {
auditor = api . AGPL . Options . Auditor
2023-08-21 21:55:39 -05:00
}
2024-10-05 06:58:43 +04:00
api . AGPL . Auditor . Store ( & auditor )
2022-09-30 14:01:20 -04:00
}
2025-07-15 14:36:06 +10:00
if initial , changed , enabled := featureChanged ( codersdk . FeatureConnectionLog ); shouldUpdate ( initial , changed , enabled ) {
connectionLogger := agplconnectionlog . NewNop ()
if enabled {
connectionLogger = api . AGPL . Options . ConnectionLogger
}
api . AGPL . ConnectionLogger . Store ( & connectionLogger )
}
2024-10-05 06:58:43 +04:00
if initial , changed , enabled := featureChanged ( codersdk . FeatureBrowserOnly ); shouldUpdate ( initial , changed , enabled ) {
var handler func ( rw http . ResponseWriter ) bool
if enabled {
handler = api . shouldBlockNonBrowserConnections
2023-07-20 06:35:41 -07:00
}
2024-10-05 06:58:43 +04:00
api . AGPL . WorkspaceClientCoordinateOverride . Store ( & handler )
}
if initial , changed , enabled := featureChanged ( codersdk . FeatureTemplateRBAC ); shouldUpdate ( initial , changed , enabled ) {
if enabled {
committer := committer {
Log : api . Logger . Named ( "quota_committer" ),
Database : api . Database ,
}
qcPtr := proto . QuotaCommitter ( & committer )
api . AGPL . QuotaCommitter . Store ( & qcPtr )
2023-07-20 06:35:41 -07:00
} else {
2024-10-05 06:58:43 +04:00
api . AGPL . QuotaCommitter . Store ( nil )
}
}
if initial , changed , enabled := featureChanged ( codersdk . FeatureAdvancedTemplateScheduling ); shouldUpdate ( initial , changed , enabled ) {
if enabled {
2024-12-02 21:02:36 +00:00
templateStore := schedule . NewEnterpriseTemplateScheduleStore ( api . AGPL . UserQuietHoursScheduleStore , api . NotificationsEnqueuer , api . Logger . Named ( "template.schedule-store" ), api . Clock )
2024-10-05 06:58:43 +04:00
templateStoreInterface := agplschedule . TemplateScheduleStore ( templateStore )
api . AGPL . TemplateScheduleStore . Store ( & templateStoreInterface )
if api . DefaultQuietHoursSchedule == "" {
api . Logger . Warn ( ctx , "template autostop requirement will default to UTC midnight as the default user quiet hours schedule. Set a custom default quiet hours schedule using CODER_QUIET_HOURS_DEFAULT_SCHEDULE to avoid this warning" )
api . DefaultQuietHoursSchedule = "CRON_TZ=UTC 0 0 * * *"
}
quietHoursStore , err := schedule . NewEnterpriseUserQuietHoursScheduleStore ( api . DefaultQuietHoursSchedule , api . DeploymentValues . UserQuietHoursSchedule . AllowUserCustom . Value ())
if err != nil {
api . Logger . Error ( ctx , "unable to set up enterprise user quiet hours schedule store, template autostop requirements will not be applied to workspace builds" , slog . Error ( err ))
} else {
api . AGPL . UserQuietHoursScheduleStore . Store ( & quietHoursStore )
}
} else {
templateStore := agplschedule . NewAGPLTemplateScheduleStore ()
api . AGPL . TemplateScheduleStore . Store ( & templateStore )
quietHoursStore := agplschedule . NewAGPLUserQuietHoursScheduleStore ()
2023-07-20 06:35:41 -07:00
api . AGPL . UserQuietHoursScheduleStore . Store ( & quietHoursStore )
}
2023-03-08 01:14:58 +11:00
}
2024-10-05 06:58:43 +04:00
if initial , changed , enabled := featureChanged ( codersdk . FeatureHighAvailability ); shouldUpdate ( initial , changed , enabled ) {
var coordinator agpltailnet . Coordinator
2025-07-09 09:46:31 +02:00
if enabled {
2024-10-05 06:58:43 +04:00
haCoordinator , err := tailnet . NewPGCoord ( api . ctx , api . Logger , api . Pubsub , api . Database )
if err != nil {
api . Logger . Error ( ctx , "unable to set up high availability coordinator" , slog . Error ( err ))
// If we try to setup the HA coordinator and it fails, nothing
// is actually changing.
} else {
coordinator = haCoordinator
2022-10-17 08:43:30 -05:00
}
2024-10-05 06:58:43 +04:00
api . replicaManager . SetCallback ( func () {
// Only update DERP mesh if the built-in server is enabled.
if api . Options . DeploymentValues . DERP . Server . Enable {
addresses := make ([] string , 0 )
for _ , replica := range api . replicaManager . Regional () {
// Don't add replicas with an empty relay address.
if replica . RelayAddress == "" {
continue
}
addresses = append ( addresses , replica . RelayAddress )
}
api . derpMesh . SetAddresses ( addresses , false )
}
2025-07-24 15:13:15 +10:00
_ = api . updateEntitlements ( api . ctx )
2024-10-05 06:58:43 +04:00
})
} else {
coordinator = agpltailnet . NewCoordinator ( api . Logger )
if api . Options . DeploymentValues . DERP . Server . Enable {
api . derpMesh . SetAddresses ([] string {}, false )
}
api . replicaManager . SetCallback ( func () {
// If the amount of replicas change, so should our entitlements.
// This is to display a warning in the UI if the user is unlicensed.
2025-07-24 15:13:15 +10:00
_ = api . updateEntitlements ( api . ctx )
2024-10-05 06:58:43 +04:00
})
}
// Recheck changed in case the HA coordinator failed to set up.
if coordinator != nil {
oldCoordinator := * api . AGPL . TailnetCoordinator . Swap ( & coordinator )
err := oldCoordinator . Close ()
if err != nil {
api . Logger . Error ( ctx , "close old tailnet coordinator" , slog . Error ( err ))
}
2022-10-17 08:43:30 -05:00
}
}
2024-10-05 06:58:43 +04:00
if initial , changed , enabled := featureChanged ( codersdk . FeatureWorkspaceProxy ); shouldUpdate ( initial , changed , enabled ) {
if enabled {
fn := derpMapper ( api . Logger , api . ProxyHealth )
api . AGPL . DERPMapper . Store ( & fn )
} else {
api . AGPL . DERPMapper . Store ( nil )
}
2023-07-26 09:21:04 -07:00
}
2024-10-05 06:58:43 +04:00
if initial , changed , enabled := featureChanged ( codersdk . FeatureAccessControl ); shouldUpdate ( initial , changed , enabled ) {
var acs agpldbauthz . AccessControlStore = agpldbauthz . AGPLTemplateAccessControlStore {}
if enabled {
acs = dbauthz . EnterpriseTemplateAccessControlStore {}
}
api . AGPL . AccessControlStore . Store ( & acs )
2023-10-18 17:07:21 -05:00
}
2024-10-05 06:58:43 +04:00
if initial , changed , enabled := featureChanged ( codersdk . FeatureAppearance ); shouldUpdate ( initial , changed , enabled ) {
if enabled {
f := newAppearanceFetcher (
api . Database ,
api . DeploymentValues . Support . Links . Value ,
api . DeploymentValues . DocsURL . String (),
buildinfo . Version (),
)
api . AGPL . AppearanceFetcher . Store ( & f )
} else {
f := appearance . NewDefaultFetcher ( api . DeploymentValues . DocsURL . String ())
api . AGPL . AppearanceFetcher . Store ( & f )
}
2024-01-29 12:17:31 +04:00
}
2024-10-05 06:58:43 +04:00
if initial , changed , enabled := featureChanged ( codersdk . FeatureControlSharedPorts ); shouldUpdate ( initial , changed , enabled ) {
var ps agplportsharing . PortSharer = agplportsharing . DefaultPortSharer
if enabled {
ps = portsharing . NewEnterprisePortSharer ()
}
api . AGPL . PortSharer . Store ( & ps )
2024-02-13 09:31:20 -05:00
}
2025-04-25 11:07:15 +02:00
if initial , changed , enabled := featureChanged ( codersdk . FeatureWorkspacePrebuilds ); shouldUpdate ( initial , changed , enabled ) {
reconciler , claimer := api . setupPrebuilds ( enabled )
if current := api . AGPL . PrebuildsReconciler . Load (); current != nil {
stopCtx , giveUp := context . WithTimeoutCause ( context . Background (), time . Second * 30 , xerrors . New ( "gave up waiting for reconciler to stop" ))
defer giveUp ()
( * current ). Stop ( stopCtx , xerrors . New ( "entitlements change" ))
}
api . AGPL . PrebuildsReconciler . Store ( & reconciler )
2025-08-07 11:21:17 -05:00
// TODO: Should this context be the api.ctx context? To cancel when
// the API (and entire app) is closed via shutdown?
pproflabel . Go ( context . Background (), pproflabel . Service ( pproflabel . ServicePrebuildReconciler ), reconciler . Run )
2025-04-25 11:07:15 +02:00
api . AGPL . PrebuildsClaimer . Store ( & claimer )
}
2024-10-05 06:58:43 +04:00
// External token encryption is soft-enforced
featureExternalTokenEncryption := reloadedEntitlements . Features [ codersdk . FeatureExternalTokenEncryption ]
featureExternalTokenEncryption . Enabled = len ( api . ExternalTokenEncryption ) > 0
if featureExternalTokenEncryption . Enabled && featureExternalTokenEncryption . Entitlement != codersdk . EntitlementEntitled {
msg := fmt . Sprintf ( "%s is enabled (due to setting external token encryption keys) but your license is not entitled to this feature." , codersdk . FeatureExternalTokenEncryption . Humanize ())
api . Logger . Warn ( ctx , msg )
reloadedEntitlements . Warnings = append ( reloadedEntitlements . Warnings , msg )
}
reloadedEntitlements . Features [ codersdk . FeatureExternalTokenEncryption ] = featureExternalTokenEncryption
2025-07-22 13:39:26 +10:00
// If there's a license installed, we will use the enterprise build
// limit checker.
// This checker currently only enforces the managed agent limit.
if reloadedEntitlements . HasLicense {
var checker wsbuilder . UsageChecker = api
api . AGPL . BuildUsageChecker . Store ( & checker )
} else {
// Don't check any usage, just like AGPL.
var checker wsbuilder . UsageChecker = wsbuilder . NoopUsageChecker {}
api . AGPL . BuildUsageChecker . Store ( & checker )
}
2024-10-05 06:58:43 +04:00
return reloadedEntitlements , nil
})
2022-09-19 23:11:01 -05:00
}
2025-07-22 13:39:26 +10:00
var _ wsbuilder . UsageChecker = & API {}
func ( api * API ) CheckBuildUsage ( ctx context . Context , store database . Store , templateVersion * database . TemplateVersion ) ( wsbuilder . UsageCheckResponse , error ) {
// We assume that if this function is called, a valid license is installed.
// When there are no licenses installed, a noop usage checker is used
// instead.
// If the template version doesn't have an AI task, we don't need to check
// usage.
if ! templateVersion . HasAITask . Valid || ! templateVersion . HasAITask . Bool {
return wsbuilder . UsageCheckResponse {
Permitted : true ,
}, nil
}
// Otherwise, we need to check that we haven't breached the managed agent
// limit.
managedAgentLimit , ok := api . Entitlements . Feature ( codersdk . FeatureManagedAgentLimit )
if ! ok || ! managedAgentLimit . Enabled || managedAgentLimit . Limit == nil || managedAgentLimit . UsagePeriod == nil {
return wsbuilder . UsageCheckResponse {
Permitted : false ,
Message : "Your license is not entitled to managed agents. Please contact sales to continue using managed agents." ,
}, nil
}
// This check is intentionally not committed to the database. It's fine if
// it's not 100% accurate or allows for minor breaches due to build races.
2025-07-22 16:03:35 +10:00
// nolint:gocritic // Requires permission to read all workspaces to read managed agent count.
managedAgentCount , err := store . GetManagedAgentCount ( agpldbauthz . AsSystemRestricted ( ctx ), database . GetManagedAgentCountParams {
2025-07-22 13:39:26 +10:00
StartTime : managedAgentLimit . UsagePeriod . Start ,
EndTime : managedAgentLimit . UsagePeriod . End ,
})
if err != nil {
return wsbuilder . UsageCheckResponse {}, xerrors . Errorf ( "get managed agent count: %w" , err )
}
if managedAgentCount >= * managedAgentLimit . Limit {
return wsbuilder . UsageCheckResponse {
Permitted : false ,
Message : "You have breached the managed agent limit in your license. Please contact sales to continue using managed agents." ,
}, nil
}
return wsbuilder . UsageCheckResponse {
Permitted : true ,
}, nil
}
2023-07-26 09:21:04 -07:00
// getProxyDERPStartingRegionID returns the starting region ID that should be
// used for workspace proxies. A proxy's actual region ID is the return value
// from this function + it's RegionID field.
//
// Two ints are returned, the first is the starting region ID for proxies, and
// the second is the maximum region ID that already exists in the DERP map.
func getProxyDERPStartingRegionID ( derpMap * tailcfg . DERPMap ) ( sID int64 , mID int64 ) {
var maxRegionID int64
for _ , region := range derpMap . Regions {
rid := int64 ( region . RegionID )
if rid > maxRegionID {
maxRegionID = rid
}
}
if maxRegionID < 0 {
maxRegionID = 0
}
// Round to the nearest 10,000 with a sufficient buffer of at least 2,000.
// The buffer allows for future "fixed" regions to be added to the base DERP
// map without conflicting with proxy region IDs (standard DERP maps usually
// use incrementing IDs for new regions).
//
// Example:
// maxRegionID = -2_000 -> startingRegionID = 10_000
// maxRegionID = 8_000 -> startingRegionID = 10_000
// maxRegionID = 8_500 -> startingRegionID = 20_000
// maxRegionID = 12_000 -> startingRegionID = 20_000
// maxRegionID = 20_000 -> startingRegionID = 30_000
const roundStartingRegionID = 10_000
const startingRegionIDBuffer = 2_000
// Add the buffer first.
startingRegionID := maxRegionID + startingRegionIDBuffer
// Round UP to the nearest 10,000. Go's math.Ceil rounds up to the nearest
// integer, so we need to divide by 10,000 first and then multiply by
// 10,000.
startingRegionID = int64 ( math . Ceil ( float64 ( startingRegionID ) / roundStartingRegionID ) * roundStartingRegionID )
// This should never be hit but it's here just in case.
if startingRegionID < roundStartingRegionID {
startingRegionID = roundStartingRegionID
}
return startingRegionID , maxRegionID
}
var (
lastDerpConflictMutex sync . Mutex
lastDerpConflictLog time . Time
)
2023-08-10 12:04:17 -07:00
func derpMapper ( logger slog . Logger , proxyHealth * proxyhealth . ProxyHealth ) func ( * tailcfg . DERPMap ) * tailcfg . DERPMap {
2023-07-26 09:21:04 -07:00
return func ( derpMap * tailcfg . DERPMap ) * tailcfg . DERPMap {
derpMap = derpMap . Clone ()
// Find the starting region ID that we'll use for proxies. This must be
// deterministic based on the derp map.
startingRegionID , largestRegionID := getProxyDERPStartingRegionID ( derpMap )
if largestRegionID >= 1 << 32 {
// Enforce an upper bound on the region ID. This shouldn't be hit in
// practice, but it's a good sanity check.
lastDerpConflictMutex . Lock ()
shouldLog := lastDerpConflictLog . IsZero () || time . Since ( lastDerpConflictLog ) > time . Minute
if shouldLog {
lastDerpConflictLog = time . Now ()
}
lastDerpConflictMutex . Unlock ()
if shouldLog {
logger . Warn (
context . Background (),
"existing DERP region IDs are too large, proxy region IDs will not be populated in the derp map. Please ensure that all DERP region IDs are less than 2^32" ,
slog . F ( "largest_region_id" , largestRegionID ),
2023-07-26 10:30:47 -07:00
slog . F ( "max_region_id" , int64 ( 1 << 32 - 1 )),
2023-07-26 09:21:04 -07:00
)
return derpMap
}
}
// Add all healthy proxies to the DERP map.
statusMap := proxyHealth . HealthStatus ()
statusLoop :
for _ , status := range statusMap {
if status . Status != proxyhealth . Healthy || ! status . Proxy . DerpEnabled {
// Only add healthy proxies with DERP enabled to the DERP map.
continue
}
u , err := url . Parse ( status . Proxy . Url )
if err != nil {
// Not really any need to log, the proxy should be unreachable
// anyways and filtered out by the above condition.
continue
}
port := u . Port ()
if port == "" {
port = "80"
if u . Scheme == "https" {
port = "443"
}
}
portInt , err := strconv . Atoi ( port )
if err != nil {
// Not really any need to log, the proxy should be unreachable
// anyways and filtered out by the above condition.
continue
}
// Sanity check that the region ID and code is unique.
//
// This should be impossible to hit as the IDs are enforced to be
// unique by the database and the computed ID is greater than any
// existing ID in the DERP map.
regionID := int ( startingRegionID ) + int ( status . Proxy . RegionID )
regionCode := fmt . Sprintf ( "coder_%s" , strings . ToLower ( status . Proxy . Name ))
2023-08-01 20:18:46 -07:00
regionName := status . Proxy . DisplayName
if regionName == "" {
regionName = status . Proxy . Name
}
2023-07-26 09:21:04 -07:00
for _ , r := range derpMap . Regions {
if r . RegionID == regionID || r . RegionCode == regionCode {
// Log a warning if we haven't logged one in the last
// minute.
lastDerpConflictMutex . Lock ()
shouldLog := lastDerpConflictLog . IsZero () || time . Since ( lastDerpConflictLog ) > time . Minute
if shouldLog {
lastDerpConflictLog = time . Now ()
}
lastDerpConflictMutex . Unlock ()
if shouldLog {
logger . Warn ( context . Background (),
2023-08-01 08:50:43 -07:00
"proxy region ID or code conflict, ignoring workspace proxy for DERP map" ,
2023-07-26 09:21:04 -07:00
slog . F ( "proxy_id" , status . Proxy . ID ),
slog . F ( "proxy_name" , status . Proxy . Name ),
slog . F ( "proxy_display_name" , status . Proxy . DisplayName ),
slog . F ( "proxy_url" , status . Proxy . Url ),
slog . F ( "proxy_region_id" , status . Proxy . RegionID ),
slog . F ( "proxy_computed_region_id" , regionID ),
slog . F ( "proxy_computed_region_code" , regionCode ),
)
}
continue statusLoop
}
}
derpMap . Regions [ regionID ] = & tailcfg . DERPRegion {
// EmbeddedRelay ONLY applies to the primary.
EmbeddedRelay : false ,
RegionID : regionID ,
RegionCode : regionCode ,
2023-08-01 20:18:46 -07:00
RegionName : regionName ,
2023-08-10 12:04:17 -07:00
Nodes : [] * tailcfg . DERPNode {
{
Name : fmt . Sprintf ( "%da" , regionID ),
RegionID : regionID ,
HostName : u . Hostname (),
DERPPort : portInt ,
STUNPort : - 1 ,
ForceHTTP : u . Scheme == "http" ,
},
},
2023-07-26 09:21:04 -07:00
}
}
return derpMap
}
}
2023-01-11 16:05:42 +01:00
// @Summary Get entitlements
// @ID get-entitlements
// @Security CoderSessionToken
// @Produce json
// @Tags Enterprise
// @Success 200 {object} codersdk.Entitlements
// @Router /entitlements [get]
2022-09-19 23:11:01 -05:00
func ( api * API ) serveEntitlements ( rw http . ResponseWriter , r * http . Request ) {
2022-09-21 17:07:00 -05:00
ctx := r . Context ()
2024-08-26 13:05:03 -05:00
httpapi . Write ( ctx , rw , http . StatusOK , api . Entitlements . AsJSON ())
2022-09-19 23:11:01 -05:00
}
func ( api * API ) runEntitlementsLoop ( ctx context . Context ) {
eb := backoff . NewExponentialBackOff ()
eb . MaxElapsedTime = 0 // retry indefinitely
b := backoff . WithContext ( eb , ctx )
updates := make ( chan struct {}, 1 )
subscribed := false
2023-08-22 09:26:43 -05:00
defer func () {
2023-08-22 12:32:37 -06:00
// If this function ends, it means the context was canceled and this
2023-08-22 09:26:43 -05:00
// coderd is shutting down. In this case, post a pubsub message to
// tell other coderd's to resync their entitlements. This is required to
// make sure things like replica counts are updated in the UI.
// Ignore the error, as this is just a best effort. If it fails,
// the system will eventually recover as replicas timeout
// if their heartbeats stop. The best effort just tries to update the
// UI faster if it succeeds.
_ = api . Pubsub . Publish ( PubsubEventLicenses , [] byte ( "going away" ))
}()
2022-09-19 23:11:01 -05:00
for {
select {
case <- ctx . Done ():
return
default :
// pass
}
if ! subscribed {
cancel , err := api . Pubsub . Subscribe ( PubsubEventLicenses , func ( _ context . Context , _ [] byte ) {
// don't block. If the channel is full, drop the event, as there is a resync
// scheduled already.
select {
case updates <- struct {}{}:
// pass
default :
// pass
}
})
if err != nil {
api . Logger . Warn ( ctx , "failed to subscribe to license updates" , slog . Error ( err ))
select {
case <- ctx . Done ():
return
case <- time . After ( b . NextBackOff ()):
}
continue
}
// nolint: revive
defer cancel ()
subscribed = true
api . Logger . Debug ( ctx , "successfully subscribed to pubsub" )
}
2022-09-24 14:55:17 -05:00
api . Logger . Debug ( ctx , "syncing licensed entitlements" )
2022-09-19 23:11:01 -05:00
err := api . updateEntitlements ( ctx )
if err != nil {
api . Logger . Warn ( ctx , "failed to get feature entitlements" , slog . Error ( err ))
time . Sleep ( b . NextBackOff ())
continue
}
b . Reset ()
api . Logger . Debug ( ctx , "synced licensed entitlements" )
select {
case <- ctx . Done ():
return
case <- time . After ( api . EntitlementsUpdateInterval ):
continue
case <- updates :
api . Logger . Debug ( ctx , "got pubsub update" )
continue
}
}
}
2022-10-10 15:37:06 -05:00
2024-05-15 09:46:35 -05:00
func ( api * API ) Authorize ( r * http . Request , action policy . Action , object rbac . Objecter ) bool {
2022-10-10 15:37:06 -05:00
return api . AGPL . HTTPAuth . Authorize ( r , action , object )
}
2025-04-25 11:07:15 +02:00
// nolint:revive // featureEnabled is a legit control flag.
func ( api * API ) setupPrebuilds ( featureEnabled bool ) ( agplprebuilds . ReconciliationOrchestrator , agplprebuilds . Claimer ) {
2025-06-26 15:54:52 +02:00
if ! featureEnabled {
api . Logger . Warn ( context . Background (), "prebuilds not enabled; ensure you have a premium license" ,
slog . F ( "feature_enabled" , featureEnabled ))
2025-04-25 11:07:15 +02:00
return agplprebuilds . DefaultReconciler , agplprebuilds . DefaultClaimer
}
2025-06-23 12:35:15 -05:00
reconciler := prebuilds . NewStoreReconciler ( api . Database , api . Pubsub , api . AGPL . FileCache , api . DeploymentValues . Prebuilds ,
2025-07-22 13:39:26 +10:00
api . Logger . Named ( "prebuilds" ), quartz . NewReal (), api . PrometheusRegistry , api . NotificationsEnqueuer , api . AGPL . BuildUsageChecker )
2025-04-30 16:26:30 +02:00
return reconciler , prebuilds . NewEnterpriseClaimer ( api . Database )
2025-04-25 11:07:15 +02:00
}