mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
## Summary Closes PLAT-305. When a provisioner key is deleted, the associated daemon kept operating on its existing WebSocket connection, because authentication was only checked at connection establishment and deletion was a bare `DELETE` with no session invalidation. This adds four layers of defense so a deleted key promptly stops doing work: 1. **Publish on delete.** `deleteProvisionerKey` publishes to a new per-key pubsub channel (`coderd/pubsub.ProvisionerKeyDeletedChannel`) after a successful delete. Publish errors are logged but still return `204`, since layer 3 is the durable backstop. 2. **Subscribe and tear down.** The daemon serve handler subscribes to its key's channel and terminates the DRPC session on a deletion event. Termination is deferred while a job claimed by the session is active: the daemon may finish and report the in-flight job (`UpdateJob`/`CompleteJob` have no key check), and the last active job's completion performs the cancellation. Because Postgres `LISTEN`/`NOTIFY` does not buffer for non-listeners, the handler also performs a synchronous key-existence re-check immediately after subscribing to close the race between auth and subscription. The subscription uses `SubscribeWithErr` so that an `ErrDroppedMessages` signal (emitted when the pubsub listener reconnects) triggers the same key re-check, closing the listener-outage window in which a deletion notification could be missed. 3. **Backstop on acquire.** `AcquireJob` and `AcquireJobWithCancel` verify the key still exists before waiting for a job, and the `Acquirer` claims jobs in a transaction that first locks the worker's deletable key (`LockProvisionerKeyByIDForShare`, a `FOR KEY SHARE` row lock held until commit) before running the `AcquireProvisionerJob` claim, so a claim cannot commit after the key's deletion. This guards against a missed pubsub message. A missing key row surfaces as its own result rather than overloading the claim query's no-rows response: the acquire terminates with `ErrProvisionerKeyDeleted` (terminating the session, with the same active-job deferral) and hands the consumed wakeup to another waiting daemon in the same domain, rather than silently re-parking and starving peers of job postings. 4. **Heartbeat watchdog.** The per-session heartbeat loop (1m interval) also re-checks the key, so even a session whose deletion notification was silently lost terminates within one heartbeat interval instead of living until the connection breaks (same active-job deferral as layer 2). Reserved keys skip the check. A job that is claimed but never delivered (the session or connection dies between the database claim and the stream send) is marked failed immediately on a fresh context, instead of staying assigned to the worker until the job reaper. Reserved keys (built-in, user-auth, PSK) are exempt throughout, since they are not deletable rows. The acquire-time lookup runs as `dbauthz.AsSystemReadProvisionerDaemons`, because the provisionerd role cannot read provisioner keys and a provisioner key's RBAC object is a provisioner daemon. A single key can back many daemons (and span HA replicas), so the per-key channel fans out to invalidate all of them at once. Per-key channels keep the `LISTEN` count proportional to distinct keys rather than waking every daemon on unrelated deletions. ### Known limitations - **`UpdateJob`/`CompleteJob` intentionally have no key check.** By the time those RPCs arrive the work has already run; rejecting completion would strand a build in "running" (until the job reaper fails it) with real infrastructure left unreconciled. Session termination is deferred while a job is active so the completion can be reported; the daemon may not receive the final RPC response when the deferred termination fires, but the job's outcome is already persisted. - **After termination, the daemon process redials and receives 401s until restarted.** The dial-time exit logic only triggers on 403, and the auth middleware returns 401 for an invalid key; this dial behavior predates this PR and is tracked as a follow-up in [PLAT-452](https://linear.app/codercom/issue/PLAT-452) (return 403 for invalid provisioner keys). ## Tests - `coderd/provisionerdserver`: `TestAcquireJob_ProvisionerKeyDeleted` (both RPC variants), `TestAcquireJob_ReservedProvisionerKey`, `TestHeartbeat_ProvisionerKeyDeleted` (heartbeat watchdog cancels the session after key deletion), `TestAcquirer_ProvisionerKeyDeleted` (a dead-key acquiree exits terminally and its clearance is promoted to a peer in the same domain), and `TestTerminateSession_Deferral` (termination is immediate when idle and deferred until the last active job finishes). - `coderd/database`: `TestAcquireProvisionerJob/ProvisionerKeyLock` covers the lock query against real Postgres: it returns the key ID while the row exists and no rows once it is deleted. The lock-then-claim composition is pinned by `TestAcquirer_ProvisionerKeyDeleted`. - `enterprise/coderd`: `TestProvisionerDaemonServe/KeyDeletionClosesSession` asserts an active session closes after its key is deleted. `KeyDeletedDuringSetupClosesSession` covers the post-subscribe re-check when a key is deleted between auth and subscription, and `DroppedMessageClosesSession` covers the `ErrDroppedMessages` re-check when a deletion is missed during a listener outage. ## Validation - `make` pre-commit (gen/fmt/lint/build) passed via git hooks. - Targeted tests pass; existing acquire tests pass with no regression. - Manual: brought up a dev deployment (coder-in-coder) with a Premium license, created a deletable provisioner key, and started an external daemon with `coder provisionerd start`. Confirmed it authenticated via the key and connected, appearing as `idle` in both `coder provisioner list` (with the key name) and the organization Provisioners UI. - Manual, idle teardown: deleted the key while the daemon was idle. The server logged `provisioner key deleted, terminating session`, the daemon's session closed immediately, and it dropped from `coder provisioner list` (then entered the known 401 redial loop, PLAT-452). - Manual, deferred termination: ran a workspace build (tagged template, `sleep 45` in `local-exec`) pinned to the external daemon and deleted the key mid-build. The server logged `deferring session cancellation until active jobs finish`; the heartbeat watchdog re-checked mid-build and re-deferred rather than force-killing. The build ran to completion (`Apply complete`, workspace `Started`) and only then did `canceling session after job completion` fire. The documented caveat reproduced: the daemon lost the final `CompleteJob` ack, and the build outcome was still persisted correctly. <details> <summary>Implementation plan and design decisions</summary> ### Design - **Per-key vs global channel:** chose per-key (`provisioner_key_deleted:<keyID>`) so daemons do not wake on unrelated deletions. The cost is one `LISTEN` per distinct key per replica on the shared listener connection, which is negligible against Coder's existing channels. - **Missing-key behavior on acquire:** returns an error that tears down the acquire rather than silently returning an empty job. - **Subscribe-startup race:** ordering is `authorize -> UpsertProvisionerDaemon -> Subscribe -> GetProvisionerKeyByID`. The post-subscribe re-check handles a deletion that committed before the `LISTEN` registered (Postgres does not buffer notifications for non-listeners; the in-process buffer only smooths bursts and drops on overflow). - **`NewServer` change:** `KeyID` was added to `provisionerdserver.Options` to avoid a positional signature change across call sites. The in-memory (built-in) daemon leaves it unset and is therefore exempt. ### Files - `coderd/pubsub/provisionerkeydeleted.go` (new) — channel helper. - `enterprise/coderd/provisionerkeys.go` — publish on delete. - `enterprise/coderd/provisionerdaemons.go` — subscribe, re-check, cancel session; pass `KeyID`. - `coderd/provisionerdserver/provisionerdserver.go` — `KeyID` option and acquire-time existence check. </details> --- This pull request was created by Coder Agents on behalf of @jscottmiller.
563 lines
21 KiB
Go
563 lines
21 KiB
Go
package codersdk
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/hashicorp/yamux"
|
|
"golang.org/x/exp/maps"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/buildinfo"
|
|
"github.com/coder/coder/v2/codersdk/drpcsdk"
|
|
"github.com/coder/coder/v2/codersdk/wsjson"
|
|
"github.com/coder/coder/v2/provisionerd/proto"
|
|
"github.com/coder/coder/v2/provisionerd/runner"
|
|
"github.com/coder/websocket"
|
|
)
|
|
|
|
type LogSource string
|
|
|
|
type LogLevel string
|
|
|
|
const (
|
|
LogSourceProvisionerDaemon LogSource = "provisioner_daemon"
|
|
LogSourceProvisioner LogSource = "provisioner"
|
|
|
|
LogLevelTrace LogLevel = "trace"
|
|
LogLevelDebug LogLevel = "debug"
|
|
LogLevelInfo LogLevel = "info"
|
|
LogLevelWarn LogLevel = "warn"
|
|
LogLevelError LogLevel = "error"
|
|
)
|
|
|
|
// ProvisionerDaemonStatus represents the status of a provisioner daemon.
|
|
type ProvisionerDaemonStatus string
|
|
|
|
// ProvisionerDaemonStatus enums.
|
|
const (
|
|
ProvisionerDaemonOffline ProvisionerDaemonStatus = "offline"
|
|
ProvisionerDaemonIdle ProvisionerDaemonStatus = "idle"
|
|
ProvisionerDaemonBusy ProvisionerDaemonStatus = "busy"
|
|
)
|
|
|
|
func ProvisionerDaemonStatusEnums() []ProvisionerDaemonStatus {
|
|
return []ProvisionerDaemonStatus{
|
|
ProvisionerDaemonOffline,
|
|
ProvisionerDaemonIdle,
|
|
ProvisionerDaemonBusy,
|
|
}
|
|
}
|
|
|
|
type ProvisionerDaemon struct {
|
|
ID uuid.UUID `json:"id" format:"uuid" table:"id"`
|
|
OrganizationID uuid.UUID `json:"organization_id" format:"uuid" table:"organization id"`
|
|
KeyID uuid.UUID `json:"key_id" format:"uuid" table:"-"`
|
|
CreatedAt time.Time `json:"created_at" format:"date-time" table:"created at"`
|
|
LastSeenAt NullTime `json:"last_seen_at,omitempty" format:"date-time" table:"last seen at"`
|
|
Name string `json:"name" table:"name,default_sort"`
|
|
Version string `json:"version" table:"version"`
|
|
APIVersion string `json:"api_version" table:"api version"`
|
|
Provisioners []ProvisionerType `json:"provisioners" table:"-"`
|
|
Tags map[string]string `json:"tags" table:"tags"`
|
|
|
|
// Optional fields.
|
|
KeyName *string `json:"key_name" table:"key name"`
|
|
Status *ProvisionerDaemonStatus `json:"status" enums:"offline,idle,busy" table:"status"`
|
|
CurrentJob *ProvisionerDaemonJob `json:"current_job" table:"current job,recursive"`
|
|
PreviousJob *ProvisionerDaemonJob `json:"previous_job" table:"previous job,recursive"`
|
|
}
|
|
|
|
type ProvisionerDaemonJob struct {
|
|
ID uuid.UUID `json:"id" format:"uuid" table:"id"`
|
|
Status ProvisionerJobStatus `json:"status" enums:"pending,running,succeeded,canceling,canceled,failed" table:"status"`
|
|
TemplateName string `json:"template_name" table:"template name"`
|
|
TemplateIcon string `json:"template_icon" table:"template icon"`
|
|
TemplateDisplayName string `json:"template_display_name" table:"template display name"`
|
|
}
|
|
|
|
// MatchedProvisioners represents the number of provisioner daemons
|
|
// available to take a job at a specific point in time.
|
|
// Introduced in Coder version 2.18.0.
|
|
type MatchedProvisioners struct {
|
|
// Count is the number of provisioner daemons that matched the given
|
|
// tags. If the count is 0, it means no provisioner daemons matched the
|
|
// requested tags.
|
|
Count int `json:"count"`
|
|
// Available is the number of provisioner daemons that are available to
|
|
// take jobs. This may be less than the count if some provisioners are
|
|
// busy or have been stopped.
|
|
Available int `json:"available"`
|
|
// MostRecentlySeen is the most recently seen time of the set of matched
|
|
// provisioners. If no provisioners matched, this field will be null.
|
|
MostRecentlySeen NullTime `json:"most_recently_seen,omitempty" format:"date-time"`
|
|
}
|
|
|
|
// ProvisionerJobStatus represents the at-time state of a job.
|
|
type ProvisionerJobStatus string
|
|
|
|
// Active returns whether the job is still active or not.
|
|
// It returns true if canceling as well, since the job isn't
|
|
// in an entirely inactive state yet.
|
|
func (p ProvisionerJobStatus) Active() bool {
|
|
return p == ProvisionerJobPending ||
|
|
p == ProvisionerJobRunning ||
|
|
p == ProvisionerJobCanceling
|
|
}
|
|
|
|
const (
|
|
ProvisionerJobPending ProvisionerJobStatus = "pending"
|
|
ProvisionerJobRunning ProvisionerJobStatus = "running"
|
|
ProvisionerJobSucceeded ProvisionerJobStatus = "succeeded"
|
|
ProvisionerJobCanceling ProvisionerJobStatus = "canceling"
|
|
ProvisionerJobCanceled ProvisionerJobStatus = "canceled"
|
|
ProvisionerJobFailed ProvisionerJobStatus = "failed"
|
|
ProvisionerJobUnknown ProvisionerJobStatus = "unknown"
|
|
)
|
|
|
|
func ProvisionerJobStatusEnums() []ProvisionerJobStatus {
|
|
return []ProvisionerJobStatus{
|
|
ProvisionerJobPending,
|
|
ProvisionerJobRunning,
|
|
ProvisionerJobSucceeded,
|
|
ProvisionerJobCanceling,
|
|
ProvisionerJobCanceled,
|
|
ProvisionerJobFailed,
|
|
ProvisionerJobUnknown,
|
|
}
|
|
}
|
|
|
|
// ProvisionerJobInput represents the input for the job.
|
|
type ProvisionerJobInput struct {
|
|
TemplateVersionID *uuid.UUID `json:"template_version_id,omitempty" format:"uuid" table:"template version id"`
|
|
WorkspaceBuildID *uuid.UUID `json:"workspace_build_id,omitempty" format:"uuid" table:"workspace build id"`
|
|
Error string `json:"error,omitempty" table:"-"`
|
|
}
|
|
|
|
// ProvisionerJobMetadata contains metadata for the job.
|
|
type ProvisionerJobMetadata struct {
|
|
TemplateVersionName string `json:"template_version_name" table:"template version name"`
|
|
TemplateID uuid.UUID `json:"template_id" format:"uuid" table:"template id"`
|
|
TemplateName string `json:"template_name" table:"template name"`
|
|
TemplateDisplayName string `json:"template_display_name" table:"template display name"`
|
|
TemplateIcon string `json:"template_icon" table:"template icon"`
|
|
WorkspaceID *uuid.UUID `json:"workspace_id,omitempty" format:"uuid" table:"workspace id"`
|
|
WorkspaceName string `json:"workspace_name,omitempty" table:"workspace name"`
|
|
WorkspaceBuildTransition WorkspaceTransition `json:"workspace_build_transition,omitempty" table:"workspace build transition"`
|
|
}
|
|
|
|
// ProvisionerJobType represents the type of job.
|
|
type ProvisionerJobType string
|
|
|
|
const (
|
|
ProvisionerJobTypeTemplateVersionImport ProvisionerJobType = "template_version_import"
|
|
ProvisionerJobTypeWorkspaceBuild ProvisionerJobType = "workspace_build"
|
|
ProvisionerJobTypeTemplateVersionDryRun ProvisionerJobType = "template_version_dry_run"
|
|
)
|
|
|
|
// JobErrorCode defines the error code returned by job runner.
|
|
type JobErrorCode string
|
|
|
|
const (
|
|
RequiredTemplateVariables JobErrorCode = "REQUIRED_TEMPLATE_VARIABLES"
|
|
InsufficientQuota JobErrorCode = "INSUFFICIENT_QUOTA"
|
|
)
|
|
|
|
// JobIsMissingParameterErrorCode returns whether the error is a missing parameter error.
|
|
// This can indicate to consumers that they should check parameters.
|
|
func JobIsMissingParameterErrorCode(code JobErrorCode) bool {
|
|
return string(code) == runner.MissingParameterErrorCode
|
|
}
|
|
|
|
// JobIsMissingRequiredTemplateVariableErrorCode returns whether the error is a missing a required template
|
|
// variable error. This can indicate to consumers that they need to provide required template variables.
|
|
func JobIsMissingRequiredTemplateVariableErrorCode(code JobErrorCode) bool {
|
|
return string(code) == runner.RequiredTemplateVariablesErrorCode
|
|
}
|
|
|
|
// JobIsInsufficientQuotaErrorCode returns whether the error is an insufficient
|
|
// quota error. This can indicate to consumers that they should explain quota
|
|
// recovery options instead of treating the failure as a generic build error.
|
|
func JobIsInsufficientQuotaErrorCode(code JobErrorCode) bool {
|
|
return string(code) == runner.InsufficientQuotaErrorCode
|
|
}
|
|
|
|
// ProvisionerJob describes the job executed by the provisioning daemon.
|
|
type ProvisionerJob struct {
|
|
ID uuid.UUID `json:"id" format:"uuid" table:"id"`
|
|
CreatedAt time.Time `json:"created_at" format:"date-time" table:"created at"`
|
|
StartedAt *time.Time `json:"started_at,omitempty" format:"date-time" table:"started at"`
|
|
CompletedAt *time.Time `json:"completed_at,omitempty" format:"date-time" table:"completed at"`
|
|
CanceledAt *time.Time `json:"canceled_at,omitempty" format:"date-time" table:"canceled at"`
|
|
Error string `json:"error,omitempty" table:"error"`
|
|
ErrorCode JobErrorCode `json:"error_code,omitempty" enums:"REQUIRED_TEMPLATE_VARIABLES,INSUFFICIENT_QUOTA" table:"error code"`
|
|
Status ProvisionerJobStatus `json:"status" enums:"pending,running,succeeded,canceling,canceled,failed" table:"status"`
|
|
WorkerID *uuid.UUID `json:"worker_id,omitempty" format:"uuid" table:"worker id"`
|
|
WorkerName string `json:"worker_name,omitempty" table:"worker name"`
|
|
FileID uuid.UUID `json:"file_id" format:"uuid" table:"file id"`
|
|
Tags map[string]string `json:"tags" table:"tags"`
|
|
QueuePosition int `json:"queue_position" table:"queue position"`
|
|
QueueSize int `json:"queue_size" table:"queue size"`
|
|
OrganizationID uuid.UUID `json:"organization_id" format:"uuid" table:"organization id"`
|
|
InitiatorID uuid.UUID `json:"initiator_id" format:"uuid" table:"initiator id"`
|
|
Input ProvisionerJobInput `json:"input" table:"input,recursive_inline"`
|
|
Type ProvisionerJobType `json:"type" table:"type"`
|
|
AvailableWorkers []uuid.UUID `json:"available_workers,omitempty" format:"uuid" table:"available workers"`
|
|
Metadata ProvisionerJobMetadata `json:"metadata" table:"metadata,recursive_inline"`
|
|
LogsOverflowed bool `json:"logs_overflowed" table:"logs overflowed"`
|
|
}
|
|
|
|
// ProvisionerJobLog represents the provisioner log entry annotated with source and level.
|
|
type ProvisionerJobLog struct {
|
|
ID int64 `json:"id"`
|
|
CreatedAt time.Time `json:"created_at" format:"date-time"`
|
|
Source LogSource `json:"log_source"`
|
|
Level LogLevel `json:"log_level" enums:"trace,debug,info,warn,error"`
|
|
Stage string `json:"stage"`
|
|
Output string `json:"output"`
|
|
}
|
|
|
|
// Text formats the log entry as human-readable text.
|
|
func (l ProvisionerJobLog) Text() string {
|
|
var sb strings.Builder
|
|
_, _ = sb.WriteString(l.CreatedAt.Format(time.RFC3339))
|
|
_, _ = sb.WriteString(" [")
|
|
_, _ = sb.WriteString(string(l.Level))
|
|
_, _ = sb.WriteString("] [provisioner|")
|
|
_, _ = sb.WriteString(l.Stage)
|
|
_, _ = sb.WriteString("] ")
|
|
_, _ = sb.WriteString(l.Output)
|
|
return sb.String()
|
|
}
|
|
|
|
// provisionerJobLogsAfter streams logs that occurred after a specific time.
|
|
func (c *Client) provisionerJobLogsAfter(ctx context.Context, path string, after int64) (<-chan ProvisionerJobLog, io.Closer, error) {
|
|
afterQuery := ""
|
|
if after != 0 {
|
|
afterQuery = fmt.Sprintf("&after=%d", after)
|
|
}
|
|
followURL, err := c.URL.Parse(fmt.Sprintf("%s?follow%s", path, afterQuery))
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
httpClient := &http.Client{
|
|
Transport: c.HTTPClient.Transport,
|
|
}
|
|
conn, res, err := websocket.Dial(ctx, followURL.String(), &websocket.DialOptions{
|
|
HTTPClient: httpClient,
|
|
HTTPHeader: http.Header{
|
|
SessionTokenHeader: []string{c.SessionToken()},
|
|
},
|
|
CompressionMode: websocket.CompressionDisabled,
|
|
})
|
|
if err != nil {
|
|
if res == nil {
|
|
return nil, nil, err
|
|
}
|
|
return nil, nil, ReadBodyAsError(res)
|
|
}
|
|
d := wsjson.NewDecoder[ProvisionerJobLog](conn, websocket.MessageText, c.logger)
|
|
return d.Chan(), d, nil
|
|
}
|
|
|
|
// ServeProvisionerDaemonRequest are the parameters to call ServeProvisionerDaemon with
|
|
// @typescript-ignore ServeProvisionerDaemonRequest
|
|
type ServeProvisionerDaemonRequest struct {
|
|
// ID is a unique ID for a provisioner daemon.
|
|
// Deprecated: this field has always been ignored.
|
|
ID uuid.UUID `json:"id" format:"uuid"`
|
|
// Name is the human-readable unique identifier for the daemon.
|
|
Name string `json:"name" example:"my-cool-provisioner-daemon"`
|
|
// Organization is the organization for the URL. If no orgID is provided,
|
|
// then it is assumed to use the default organization.
|
|
Organization uuid.UUID `json:"organization" format:"uuid"`
|
|
// Provisioners is a list of provisioner types hosted by the provisioner daemon
|
|
Provisioners []ProvisionerType `json:"provisioners"`
|
|
// Tags is a map of key-value pairs that tag the jobs this provisioner daemon can handle
|
|
Tags map[string]string `json:"tags"`
|
|
// PreSharedKey is an authentication key to use on the API instead of the normal session token from the client.
|
|
PreSharedKey string `json:"pre_shared_key"`
|
|
// ProvisionerKey is an authentication key to use on the API instead of the normal session token from the client.
|
|
ProvisionerKey string `json:"provisioner_key"`
|
|
}
|
|
|
|
// ServeProvisionerDaemon returns the gRPC service for a provisioner daemon
|
|
// implementation. The context is during dial, not during the lifetime of the
|
|
// client. Client should be closed after use.
|
|
func (c *Client) ServeProvisionerDaemon(ctx context.Context, req ServeProvisionerDaemonRequest) (proto.DRPCProvisionerDaemonClient, error) {
|
|
orgParam := req.Organization.String()
|
|
if req.Organization == uuid.Nil {
|
|
orgParam = DefaultOrganization
|
|
}
|
|
|
|
serverURL, err := c.URL.Parse(fmt.Sprintf("/api/v2/organizations/%s/provisionerdaemons/serve", orgParam))
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("parse url: %w", err)
|
|
}
|
|
query := serverURL.Query()
|
|
query.Add("version", proto.CurrentVersion.String())
|
|
query.Add("name", req.Name)
|
|
query.Add("version", proto.CurrentVersion.String())
|
|
|
|
for _, provisioner := range req.Provisioners {
|
|
query.Add("provisioner", string(provisioner))
|
|
}
|
|
for key, value := range req.Tags {
|
|
query.Add("tag", fmt.Sprintf("%s=%s", key, value))
|
|
}
|
|
serverURL.RawQuery = query.Encode()
|
|
httpClient := &http.Client{
|
|
Transport: c.HTTPClient.Transport,
|
|
}
|
|
headers := http.Header{}
|
|
|
|
headers.Set(BuildVersionHeader, buildinfo.Version())
|
|
|
|
if req.ProvisionerKey != "" {
|
|
headers.Set(ProvisionerDaemonKey, req.ProvisionerKey)
|
|
}
|
|
if req.PreSharedKey != "" {
|
|
headers.Set(ProvisionerDaemonPSK, req.PreSharedKey)
|
|
}
|
|
if req.ProvisionerKey == "" && req.PreSharedKey == "" {
|
|
// Use session token if we don't have a PSK or provisioner key.
|
|
headers.Set(SessionTokenHeader, c.SessionToken())
|
|
}
|
|
|
|
conn, res, err := websocket.Dial(ctx, serverURL.String(), &websocket.DialOptions{
|
|
HTTPClient: httpClient,
|
|
// Need to disable compression to avoid a data-race.
|
|
CompressionMode: websocket.CompressionDisabled,
|
|
HTTPHeader: headers,
|
|
})
|
|
if err != nil {
|
|
if res == nil {
|
|
return nil, err
|
|
}
|
|
return nil, ReadBodyAsError(res)
|
|
}
|
|
config := yamux.DefaultConfig()
|
|
config.LogOutput = io.Discard
|
|
// Use background context because caller should close the client.
|
|
_, wsNetConn := WebsocketNetConn(context.Background(), conn, websocket.MessageBinary)
|
|
conn.SetReadLimit(drpcsdk.YamuxDefaultStreamWindowSize)
|
|
session, err := yamux.Client(wsNetConn, config)
|
|
if err != nil {
|
|
_ = conn.Close(websocket.StatusGoingAway, "")
|
|
_ = wsNetConn.Close()
|
|
return nil, xerrors.Errorf("multiplex client: %w", err)
|
|
}
|
|
return proto.NewDRPCProvisionerDaemonClient(drpcsdk.MultiplexedConn(session)), nil
|
|
}
|
|
|
|
type ProvisionerKeyTags map[string]string
|
|
|
|
func (p ProvisionerKeyTags) String() string {
|
|
keys := maps.Keys(p)
|
|
slices.Sort(keys)
|
|
tags := []string{}
|
|
for _, key := range keys {
|
|
tags = append(tags, fmt.Sprintf("%s=%s", key, p[key]))
|
|
}
|
|
return strings.Join(tags, " ")
|
|
}
|
|
|
|
type ProvisionerKey struct {
|
|
ID uuid.UUID `json:"id" table:"-" format:"uuid"`
|
|
CreatedAt time.Time `json:"created_at" table:"created at" format:"date-time"`
|
|
OrganizationID uuid.UUID `json:"organization" table:"-" format:"uuid"`
|
|
Name string `json:"name" table:"name,default_sort"`
|
|
Tags ProvisionerKeyTags `json:"tags" table:"tags"`
|
|
// HashedSecret - never include the access token in the API response
|
|
}
|
|
|
|
type ProvisionerKeyDaemons struct {
|
|
Key ProvisionerKey `json:"key"`
|
|
Daemons []ProvisionerDaemon `json:"daemons"`
|
|
}
|
|
|
|
const (
|
|
ProvisionerKeyIDBuiltIn = "00000000-0000-0000-0000-000000000001"
|
|
ProvisionerKeyIDUserAuth = "00000000-0000-0000-0000-000000000002"
|
|
ProvisionerKeyIDPSK = "00000000-0000-0000-0000-000000000003"
|
|
)
|
|
|
|
var (
|
|
ProvisionerKeyUUIDBuiltIn = uuid.MustParse(ProvisionerKeyIDBuiltIn)
|
|
ProvisionerKeyUUIDUserAuth = uuid.MustParse(ProvisionerKeyIDUserAuth)
|
|
ProvisionerKeyUUIDPSK = uuid.MustParse(ProvisionerKeyIDPSK)
|
|
)
|
|
|
|
const (
|
|
ProvisionerKeyNameBuiltIn = "built-in"
|
|
ProvisionerKeyNameUserAuth = "user-auth"
|
|
ProvisionerKeyNamePSK = "psk"
|
|
)
|
|
|
|
func ReservedProvisionerKeyNames() []string {
|
|
return []string{
|
|
ProvisionerKeyNameBuiltIn,
|
|
ProvisionerKeyNameUserAuth,
|
|
ProvisionerKeyNamePSK,
|
|
}
|
|
}
|
|
|
|
// IsReservedProvisionerKey reports whether the given ID is one of the reserved
|
|
// provisioner keys (built-in, user-auth, PSK). Reserved keys are created by the
|
|
// system and cannot be deleted.
|
|
func IsReservedProvisionerKey(id uuid.UUID) bool {
|
|
switch id {
|
|
case ProvisionerKeyUUIDBuiltIn, ProvisionerKeyUUIDUserAuth, ProvisionerKeyUUIDPSK:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// IsDeletableProvisionerKey reports whether the given ID identifies a
|
|
// provisioner key that can be deleted. The zero value and reserved keys cannot
|
|
// be deleted.
|
|
func IsDeletableProvisionerKey(id uuid.UUID) bool {
|
|
return id != uuid.Nil && !IsReservedProvisionerKey(id)
|
|
}
|
|
|
|
type CreateProvisionerKeyRequest struct {
|
|
Name string `json:"name"`
|
|
Tags map[string]string `json:"tags"`
|
|
}
|
|
|
|
type CreateProvisionerKeyResponse struct {
|
|
Key string `json:"key"`
|
|
}
|
|
|
|
// CreateProvisionerKey creates a new provisioner key for an organization.
|
|
func (c *Client) CreateProvisionerKey(ctx context.Context, organizationID uuid.UUID, req CreateProvisionerKeyRequest) (CreateProvisionerKeyResponse, error) {
|
|
res, err := c.Request(ctx, http.MethodPost,
|
|
fmt.Sprintf("/api/v2/organizations/%s/provisionerkeys", organizationID.String()),
|
|
req,
|
|
)
|
|
if err != nil {
|
|
return CreateProvisionerKeyResponse{}, xerrors.Errorf("make request: %w", err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusCreated {
|
|
return CreateProvisionerKeyResponse{}, ReadBodyAsError(res)
|
|
}
|
|
var resp CreateProvisionerKeyResponse
|
|
return resp, ReadBodyAsJSON(res, &resp)
|
|
}
|
|
|
|
// ListProvisionerKeys lists all provisioner keys for an organization.
|
|
func (c *Client) ListProvisionerKeys(ctx context.Context, organizationID uuid.UUID) ([]ProvisionerKey, error) {
|
|
res, err := c.Request(ctx, http.MethodGet,
|
|
fmt.Sprintf("/api/v2/organizations/%s/provisionerkeys", organizationID.String()),
|
|
nil,
|
|
)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("make request: %w", err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, ReadBodyAsError(res)
|
|
}
|
|
var resp []ProvisionerKey
|
|
return resp, ReadBodyAsJSON(res, &resp)
|
|
}
|
|
|
|
// GetProvisionerKey returns the provisioner key.
|
|
func (c *Client) GetProvisionerKey(ctx context.Context, pk string) (ProvisionerKey, error) {
|
|
res, err := c.Request(ctx, http.MethodGet,
|
|
fmt.Sprintf("/api/v2/provisionerkeys/%s", pk), nil,
|
|
func(req *http.Request) {
|
|
req.Header.Add(ProvisionerDaemonKey, pk)
|
|
},
|
|
)
|
|
if err != nil {
|
|
return ProvisionerKey{}, xerrors.Errorf("request to fetch provisioner key failed: %w", err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
return ProvisionerKey{}, ReadBodyAsError(res)
|
|
}
|
|
var resp ProvisionerKey
|
|
return resp, ReadBodyAsJSON(res, &resp)
|
|
}
|
|
|
|
// ListProvisionerKeyDaemons lists all provisioner keys with their associated daemons for an organization.
|
|
func (c *Client) ListProvisionerKeyDaemons(ctx context.Context, organizationID uuid.UUID) ([]ProvisionerKeyDaemons, error) {
|
|
res, err := c.Request(ctx, http.MethodGet,
|
|
fmt.Sprintf("/api/v2/organizations/%s/provisionerkeys/daemons", organizationID.String()),
|
|
nil,
|
|
)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("make request: %w", err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, ReadBodyAsError(res)
|
|
}
|
|
var resp []ProvisionerKeyDaemons
|
|
return resp, ReadBodyAsJSON(res, &resp)
|
|
}
|
|
|
|
// DeleteProvisionerKey deletes a provisioner key.
|
|
func (c *Client) DeleteProvisionerKey(ctx context.Context, organizationID uuid.UUID, name string) error {
|
|
res, err := c.Request(ctx, http.MethodDelete,
|
|
fmt.Sprintf("/api/v2/organizations/%s/provisionerkeys/%s", organizationID.String(), name),
|
|
nil,
|
|
)
|
|
if err != nil {
|
|
return xerrors.Errorf("make request: %w", err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusNoContent {
|
|
return ReadBodyAsError(res)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ConvertWorkspaceStatus(jobStatus ProvisionerJobStatus, transition WorkspaceTransition) WorkspaceStatus {
|
|
switch jobStatus {
|
|
case ProvisionerJobPending:
|
|
return WorkspaceStatusPending
|
|
case ProvisionerJobRunning:
|
|
switch transition {
|
|
case WorkspaceTransitionStart:
|
|
return WorkspaceStatusStarting
|
|
case WorkspaceTransitionStop:
|
|
return WorkspaceStatusStopping
|
|
case WorkspaceTransitionDelete:
|
|
return WorkspaceStatusDeleting
|
|
}
|
|
case ProvisionerJobSucceeded:
|
|
switch transition {
|
|
case WorkspaceTransitionStart:
|
|
return WorkspaceStatusRunning
|
|
case WorkspaceTransitionStop:
|
|
return WorkspaceStatusStopped
|
|
case WorkspaceTransitionDelete:
|
|
return WorkspaceStatusDeleted
|
|
}
|
|
case ProvisionerJobCanceling:
|
|
return WorkspaceStatusCanceling
|
|
case ProvisionerJobCanceled:
|
|
return WorkspaceStatusCanceled
|
|
case ProvisionerJobFailed:
|
|
return WorkspaceStatusFailed
|
|
}
|
|
|
|
// return error status since we should never get here
|
|
return WorkspaceStatusFailed
|
|
}
|