2023-01-17 10:58:00 -06:00
|
|
|
//go:build !slim
|
|
|
|
|
|
2022-09-19 23:11:01 -05:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2022-10-17 08:43:30 -05:00
|
|
|
"database/sql"
|
2023-09-07 15:49:49 +01:00
|
|
|
"encoding/base64"
|
2022-10-17 08:43:30 -05:00
|
|
|
"errors"
|
|
|
|
|
"io"
|
|
|
|
|
"net/url"
|
2022-09-19 23:11:01 -05:00
|
|
|
|
2022-10-17 08:43:30 -05:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
"tailscale.com/derp"
|
|
|
|
|
"tailscale.com/types/key"
|
2022-09-19 23:11:01 -05:00
|
|
|
|
2026-01-08 15:24:11 +04:00
|
|
|
agplcoderd "github.com/coder/coder/v2/coderd"
|
2024-02-28 08:14:11 -08:00
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/cryptorand"
|
2025-10-29 07:00:24 -06:00
|
|
|
"github.com/coder/coder/v2/enterprise/aibridged"
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/enterprise/audit"
|
|
|
|
|
"github.com/coder/coder/v2/enterprise/audit/backends"
|
|
|
|
|
"github.com/coder/coder/v2/enterprise/coderd"
|
2023-08-31 17:59:53 +02:00
|
|
|
"github.com/coder/coder/v2/enterprise/coderd/dormancy"
|
2025-08-20 23:38:09 +10:00
|
|
|
"github.com/coder/coder/v2/enterprise/coderd/usage"
|
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/trialer"
|
|
|
|
|
"github.com/coder/coder/v2/tailnet"
|
2024-10-31 17:55:42 -05:00
|
|
|
"github.com/coder/quartz"
|
2024-03-15 11:24:38 -05:00
|
|
|
"github.com/coder/serpent"
|
2022-09-19 23:11:01 -05:00
|
|
|
)
|
|
|
|
|
|
2024-03-17 09:45:26 -05:00
|
|
|
func (r *RootCmd) Server(_ func()) *serpent.Command {
|
2023-09-01 16:32:21 +03:00
|
|
|
cmd := r.RootCmd.Server(func(ctx context.Context, options *agplcoderd.Options) (*agplcoderd.API, io.Closer, error) {
|
2023-03-07 15:10:01 -06:00
|
|
|
if options.DeploymentValues.DERP.Server.RelayURL.String() != "" {
|
|
|
|
|
_, err := url.Parse(options.DeploymentValues.DERP.Server.RelayURL.String())
|
2022-10-17 08:43:30 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, xerrors.Errorf("derp-server-relay-address must be a valid HTTP URL: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-27 00:07:14 +11:00
|
|
|
// Always generate a mesh key, even if the built-in DERP server is
|
|
|
|
|
// disabled. This mesh key is still used by workspace proxies running
|
|
|
|
|
// HA.
|
|
|
|
|
var meshKey string
|
|
|
|
|
err := options.Database.InTx(func(tx database.Store) error {
|
|
|
|
|
// This will block until the lock is acquired, and will be
|
|
|
|
|
// automatically released when the transaction ends.
|
|
|
|
|
err := tx.AcquireLock(ctx, database.LockIDEnterpriseDeploymentSetup)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("acquire lock: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
meshKey, err = tx.GetDERPMeshKey(ctx)
|
|
|
|
|
if err == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
return xerrors.Errorf("get DERP mesh key: %w", err)
|
|
|
|
|
}
|
|
|
|
|
meshKey, err = cryptorand.String(32)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("generate DERP mesh key: %w", err)
|
|
|
|
|
}
|
|
|
|
|
err = tx.InsertDERPMeshKey(ctx, meshKey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("insert DERP mesh key: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
if meshKey == "" {
|
|
|
|
|
return nil, nil, xerrors.New("mesh key is empty")
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-21 16:53:41 -05:00
|
|
|
if options.DeploymentValues.DERP.Server.Enable {
|
|
|
|
|
options.DERPServer = derp.NewServer(key.NewNode(), tailnet.Logger(options.Logger.Named("derp")))
|
|
|
|
|
options.DERPServer.SetMeshKey(meshKey)
|
2024-02-28 08:14:11 -08:00
|
|
|
}
|
|
|
|
|
|
2023-08-31 13:32:28 -05:00
|
|
|
options.Auditor = audit.NewAuditor(
|
|
|
|
|
options.Database,
|
|
|
|
|
audit.DefaultFilter,
|
2023-04-04 12:11:10 -05:00
|
|
|
backends.NewPostgres(options.Database, true),
|
|
|
|
|
backends.NewSlog(options.Logger),
|
|
|
|
|
)
|
2022-10-19 02:00:45 -05:00
|
|
|
|
2022-11-16 17:09:49 -06:00
|
|
|
options.TrialGenerator = trialer.New(options.Database, "https://v2-licensor.coder.com/trial", coderd.Keys)
|
|
|
|
|
|
2022-10-10 15:04:15 -04:00
|
|
|
o := &coderd.Options{
|
2023-07-20 06:35:41 -07:00
|
|
|
Options: options,
|
|
|
|
|
AuditLogging: true,
|
2025-07-15 14:36:06 +10:00
|
|
|
ConnectionLogging: true,
|
2023-07-20 06:35:41 -07:00
|
|
|
BrowserOnly: options.DeploymentValues.BrowserOnly.Value(),
|
|
|
|
|
SCIMAPIKey: []byte(options.DeploymentValues.SCIMAPIKey.Value()),
|
|
|
|
|
RBAC: true,
|
|
|
|
|
DERPServerRelayAddress: options.DeploymentValues.DERP.Server.RelayURL.String(),
|
|
|
|
|
DERPServerRegionID: int(options.DeploymentValues.DERP.Server.RegionID.Value()),
|
|
|
|
|
ProxyHealthInterval: options.DeploymentValues.ProxyHealthStatusInterval.Value(),
|
|
|
|
|
DefaultQuietHoursSchedule: options.DeploymentValues.UserQuietHoursSchedule.DefaultSchedule.Value(),
|
2023-08-04 12:32:28 +04:00
|
|
|
ProvisionerDaemonPSK: options.DeploymentValues.Provisioner.DaemonPSK.Value(),
|
2023-08-31 17:59:53 +02:00
|
|
|
|
2024-10-31 17:55:42 -05:00
|
|
|
CheckInactiveUsersCancelFunc: dormancy.CheckInactiveUsers(ctx, options.Logger, quartz.NewReal(), options.Database, options.Auditor),
|
2022-10-10 15:04:15 -04:00
|
|
|
}
|
2022-10-19 02:00:45 -05:00
|
|
|
|
2023-09-07 15:49:49 +01:00
|
|
|
if encKeys := options.DeploymentValues.ExternalTokenEncryptionKeys.Value(); len(encKeys) != 0 {
|
|
|
|
|
keys := make([][]byte, 0, len(encKeys))
|
|
|
|
|
for idx, ek := range encKeys {
|
|
|
|
|
dk, err := base64.StdEncoding.DecodeString(ek)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, xerrors.Errorf("decode external-token-encryption-key %d: %w", idx, err)
|
|
|
|
|
}
|
|
|
|
|
keys = append(keys, dk)
|
|
|
|
|
}
|
|
|
|
|
cs, err := dbcrypt.NewCiphers(keys...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, xerrors.Errorf("initialize encryption: %w", err)
|
|
|
|
|
}
|
|
|
|
|
o.ExternalTokenEncryption = cs
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 23:38:09 +10:00
|
|
|
if o.LicenseKeys == nil {
|
|
|
|
|
o.LicenseKeys = coderd.Keys
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closers := &multiCloser{}
|
|
|
|
|
|
|
|
|
|
// Create the enterprise API.
|
2022-10-10 15:04:15 -04:00
|
|
|
api, err := coderd.New(ctx, o)
|
2022-09-19 23:11:01 -05:00
|
|
|
if err != nil {
|
2022-10-17 08:43:30 -05:00
|
|
|
return nil, nil, err
|
2022-09-19 23:11:01 -05:00
|
|
|
}
|
2025-08-20 23:38:09 +10:00
|
|
|
closers.Add(api)
|
|
|
|
|
|
|
|
|
|
// Start the enterprise usage publisher routine. This won't do anything
|
|
|
|
|
// unless the deployment is licensed and one of the licenses has usage
|
|
|
|
|
// publishing enabled.
|
|
|
|
|
publisher := usage.NewTallymanPublisher(ctx, options.Logger, options.Database, o.LicenseKeys,
|
|
|
|
|
usage.PublisherWithHTTPClient(api.HTTPClient),
|
|
|
|
|
)
|
|
|
|
|
err = publisher.Start()
|
|
|
|
|
if err != nil {
|
|
|
|
|
_ = closers.Close()
|
|
|
|
|
return nil, nil, xerrors.Errorf("start usage publisher: %w", err)
|
|
|
|
|
}
|
|
|
|
|
closers.Add(publisher)
|
|
|
|
|
|
2025-09-25 16:37:28 +02:00
|
|
|
// In-memory aibridge daemon.
|
2025-09-27 01:13:06 +10:00
|
|
|
// TODO(@deansheather): the lifecycle of the aibridged server is
|
|
|
|
|
// probably better managed by the enterprise API type itself. Managing
|
|
|
|
|
// it in the API type means we can avoid starting it up when the license
|
|
|
|
|
// is not entitled to the feature.
|
|
|
|
|
var aibridgeDaemon *aibridged.Server
|
2025-09-25 16:37:28 +02:00
|
|
|
if options.DeploymentValues.AI.BridgeConfig.Enabled {
|
2025-10-29 06:18:38 -06:00
|
|
|
aibridgeDaemon, err = newAIBridgeDaemon(api)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, xerrors.Errorf("create aibridged: %w", err)
|
2025-09-25 16:37:28 +02:00
|
|
|
}
|
2025-10-29 06:18:38 -06:00
|
|
|
|
|
|
|
|
api.RegisterInMemoryAIBridgedHTTPHandler(aibridgeDaemon)
|
|
|
|
|
|
|
|
|
|
// When running as an in-memory daemon, the HTTP handler is wired into the
|
|
|
|
|
// coderd API and therefore is subject to its context. Calling Close() on
|
|
|
|
|
// aibridged will NOT affect in-flight requests but those will be closed once
|
|
|
|
|
// the API server is itself shutdown.
|
|
|
|
|
closers.Add(aibridgeDaemon)
|
2025-09-25 16:37:28 +02:00
|
|
|
}
|
|
|
|
|
|
2025-12-29 15:31:51 +00:00
|
|
|
// In-memory AI Bridge Proxy daemon
|
|
|
|
|
if options.DeploymentValues.AI.BridgeProxyConfig.Enabled.Value() {
|
|
|
|
|
aiBridgeProxyServer, err := newAIBridgeProxyDaemon(api)
|
|
|
|
|
if err != nil {
|
|
|
|
|
_ = closers.Close()
|
|
|
|
|
return nil, nil, xerrors.Errorf("create aibridgeproxyd: %w", err)
|
|
|
|
|
}
|
|
|
|
|
closers.Add(aiBridgeProxyServer)
|
2025-12-29 20:00:06 +02:00
|
|
|
|
|
|
|
|
// Register the handler so coderd can serve the proxy endpoints.
|
|
|
|
|
api.RegisterInMemoryAIBridgeProxydHTTPHandler(aiBridgeProxyServer.Handler())
|
2025-12-29 15:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-20 23:38:09 +10:00
|
|
|
return api.AGPL, closers, nil
|
2022-09-19 23:11:01 -05:00
|
|
|
})
|
2023-09-07 15:49:49 +01:00
|
|
|
|
|
|
|
|
cmd.AddSubcommands(
|
|
|
|
|
r.dbcryptCmd(),
|
|
|
|
|
)
|
2022-09-19 23:11:01 -05:00
|
|
|
return cmd
|
|
|
|
|
}
|
2025-08-20 23:38:09 +10:00
|
|
|
|
|
|
|
|
type multiCloser struct {
|
|
|
|
|
closers []io.Closer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ io.Closer = &multiCloser{}
|
|
|
|
|
|
|
|
|
|
func (m *multiCloser) Add(closer io.Closer) {
|
|
|
|
|
m.closers = append(m.closers, closer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *multiCloser) Close() error {
|
|
|
|
|
var errs []error
|
|
|
|
|
for _, closer := range m.closers {
|
|
|
|
|
if err := closer.Close(); err != nil {
|
|
|
|
|
errs = append(errs, xerrors.Errorf("close %T: %w", closer, err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return errors.Join(errs...)
|
|
|
|
|
}
|