2022-03-22 13:17:50 -06:00
package cli
import (
"context"
2022-03-24 13:21:05 -06:00
"crypto/tls"
"crypto/x509"
2022-03-24 09:07:33 -06:00
"database/sql"
2022-04-11 18:54:30 -05:00
"errors"
2022-03-22 13:17:50 -06:00
"fmt"
2022-05-01 20:31:12 -05:00
"io"
"log"
2022-03-22 13:17:50 -06:00
"net"
"net/http"
2022-05-03 07:48:02 -05:00
"net/http/pprof"
2022-03-22 13:17:50 -06:00
"net/url"
"os"
2022-03-24 09:07:33 -06:00
"os/signal"
2022-06-15 16:02:18 -05:00
"os/user"
2022-03-28 13:57:19 -06:00
"path/filepath"
2022-10-15 04:25:11 +10:00
"regexp"
2022-06-15 16:02:18 -05:00
"strconv"
"strings"
2022-07-27 18:21:21 +03:00
"sync"
2022-03-22 13:17:50 -06:00
"time"
2022-07-31 23:05:35 -05:00
"github.com/coreos/go-oidc/v3/oidc"
2022-03-28 18:19:28 -06:00
"github.com/coreos/go-systemd/daemon"
2022-06-15 16:02:18 -05:00
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
2022-04-23 17:58:57 -05:00
"github.com/google/go-github/v43/github"
2022-06-17 00:26:40 -05:00
"github.com/google/uuid"
2022-08-08 10:09:46 -05:00
"github.com/prometheus/client_golang/prometheus"
2022-11-04 19:03:01 -05:00
"github.com/prometheus/client_golang/prometheus/collectors"
2022-05-03 07:48:02 -05:00
"github.com/prometheus/client_golang/prometheus/promhttp"
2022-06-16 10:09:22 -05:00
"github.com/spf13/afero"
2022-03-28 18:19:28 -06:00
"github.com/spf13/cobra"
2022-10-21 17:08:23 -05:00
"github.com/spf13/viper"
2022-09-16 11:43:22 -05:00
"go.opentelemetry.io/otel/trace"
2022-10-17 20:02:25 -05:00
"golang.org/x/mod/semver"
2022-04-23 17:58:57 -05:00
"golang.org/x/oauth2"
xgithub "golang.org/x/oauth2/github"
2022-06-10 13:38:11 -05:00
"golang.org/x/sync/errgroup"
2022-03-28 18:19:28 -06:00
"golang.org/x/xerrors"
"google.golang.org/api/idtoken"
"google.golang.org/api/option"
2022-08-31 20:09:44 -05:00
"tailscale.com/tailcfg"
2022-05-19 17:43:07 -05:00
2022-03-22 13:17:50 -06:00
"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
2023-01-12 20:08:23 -06:00
"cdr.dev/slog/sloggers/slogjson"
"cdr.dev/slog/sloggers/slogstackdriver"
2022-07-14 17:51:44 -04:00
"github.com/coder/coder/buildinfo"
2022-03-22 13:17:50 -06:00
"github.com/coder/coder/cli/cliui"
2022-03-24 09:07:33 -06:00
"github.com/coder/coder/cli/config"
2022-10-10 15:04:15 -04:00
"github.com/coder/coder/cli/deployment"
2022-03-22 13:17:50 -06:00
"github.com/coder/coder/coderd"
2022-05-11 23:03:02 +01:00
"github.com/coder/coder/coderd/autobuild/executor"
2022-03-25 16:07:45 -05:00
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/databasefake"
2022-09-20 03:39:02 +10:00
"github.com/coder/coder/coderd/database/migrations"
2022-04-14 11:29:40 -04:00
"github.com/coder/coder/coderd/devtunnel"
2022-10-24 19:46:24 -05:00
"github.com/coder/coder/coderd/gitauth"
2022-04-05 19:18:26 -05:00
"github.com/coder/coder/coderd/gitsshkey"
2022-10-15 04:25:11 +10:00
"github.com/coder/coder/coderd/httpapi"
2022-10-23 13:21:49 -05:00
"github.com/coder/coder/coderd/httpmw"
2022-08-08 10:09:46 -05:00
"github.com/coder/coder/coderd/prometheusmetrics"
2022-06-17 00:26:40 -05:00
"github.com/coder/coder/coderd/telemetry"
2022-05-19 17:43:07 -05:00
"github.com/coder/coder/coderd/tracing"
2022-12-01 19:43:28 +02:00
"github.com/coder/coder/coderd/updatecheck"
2022-03-22 13:17:50 -06:00
"github.com/coder/coder/codersdk"
2022-07-14 17:51:44 -04:00
"github.com/coder/coder/cryptorand"
"github.com/coder/coder/provisioner/echo"
2022-03-22 13:17:50 -06:00
"github.com/coder/coder/provisioner/terraform"
"github.com/coder/coder/provisionerd"
2022-11-10 16:37:33 -06:00
"github.com/coder/coder/provisionerd/proto"
2022-03-22 13:17:50 -06:00
"github.com/coder/coder/provisionersdk"
2022-11-10 16:37:33 -06:00
sdkproto "github.com/coder/coder/provisionersdk/proto"
2022-08-31 20:09:44 -05:00
"github.com/coder/coder/tailnet"
2022-03-22 13:17:50 -06:00
)
2022-04-24 23:52:07 -05:00
// nolint:gocyclo
2022-10-21 17:08:23 -05:00
func Server ( vip * viper . Viper , newAPI func ( context . Context , * coderd . Options ) ( * coderd . API , io . Closer , error )) * cobra . Command {
2022-03-22 13:17:50 -06:00
root := & cobra . Command {
2022-05-09 17:42:02 -05:00
Use : "server" ,
Short : "Start a Coder server" ,
2022-03-22 13:17:50 -06:00
RunE : func ( cmd * cobra . Command , args [] string ) error {
2023-01-11 10:22:20 -06:00
// Main command context for managing cancellation of running
// services.
ctx , cancel := context . WithCancel ( cmd . Context ())
defer cancel ()
go dumpHandler ( ctx )
2022-10-21 17:08:23 -05:00
cfg , err := deployment . Config ( cmd . Flags (), vip )
if err != nil {
return xerrors . Errorf ( "getting deployment config: %w" , err )
}
2022-12-16 06:09:19 +10:00
// Validate bind addresses.
if cfg . Address . Value != "" {
cmd . PrintErr ( cliui . Styles . Warn . Render ( "WARN:" ) + " --address and -a are deprecated, please use --http-address and --tls-address instead" )
if cfg . TLS . Enable . Value {
cfg . HTTPAddress . Value = ""
cfg . TLS . Address . Value = cfg . Address . Value
} else {
cfg . HTTPAddress . Value = cfg . Address . Value
cfg . TLS . Address . Value = ""
}
}
if cfg . TLS . Enable . Value && cfg . TLS . Address . Value == "" {
return xerrors . Errorf ( "TLS address must be set if TLS is enabled" )
}
if ! cfg . TLS . Enable . Value && cfg . HTTPAddress . Value == "" {
return xerrors . Errorf ( "either HTTP or TLS must be enabled" )
}
2023-01-05 12:05:20 -06:00
// Disable rate limits if the `--dangerous-disable-rate-limits` flag
// was specified.
loginRateLimit := 60
filesRateLimit := 12
if cfg . RateLimit . DisableAll . Value {
cfg . RateLimit . API . Value = - 1
loginRateLimit = - 1
filesRateLimit = - 1
}
2022-10-10 15:04:15 -04:00
printLogo ( cmd )
2023-01-12 20:08:23 -06:00
logger , logCloser , err := buildLogger ( cmd , cfg )
if err != nil {
return xerrors . Errorf ( "make logger: %w" , err )
2022-11-10 04:58:23 +10:00
}
2023-01-12 20:08:23 -06:00
defer logCloser ()
2022-05-03 16:13:30 -05:00
2022-08-18 16:25:32 +03:00
// Register signals early on so that graceful shutdown can't
// be interrupted by additional signals. Note that we avoid
// shadowing cancel() (from above) here because notifyStop()
// restores default behavior for the signals. This protects
2022-10-24 19:46:24 -05:00
// the shutdown sequence from abruptly terminating things
2022-08-18 16:25:32 +03:00
// like: database migrations, provisioner work, workspace
// cleanup in dev-mode, etc.
//
// To get out of a graceful shutdown, the user can send
// SIGQUIT with ctrl+\ or SIGKILL with `kill -9`.
2022-11-16 16:34:06 -06:00
notifyCtx , notifyStop := signal . NotifyContext ( ctx , InterruptSignals ... )
2022-08-18 16:25:32 +03:00
defer notifyStop ()
2022-12-07 03:05:14 +10:00
// Ensure we have a unique cache directory for this process.
cacheDir := filepath . Join ( cfg . CacheDirectory . Value , uuid . NewString ())
err = os . MkdirAll ( cacheDir , 0 o700 )
if err != nil {
return xerrors . Errorf ( "create cache directory: %w" , err )
}
defer os . RemoveAll ( cacheDir )
2022-07-27 18:21:21 +03:00
// Clean up idle connections at the end, e.g.
// embedded-postgres can leave an idle connection
// which is caught by goleaks.
defer http . DefaultClient . CloseIdleConnections ()
2022-05-20 10:51:06 -05:00
var (
2022-09-16 11:43:22 -05:00
tracerProvider trace . TracerProvider
2022-05-20 10:51:06 -05:00
sqlDriver = "postgres"
)
2022-09-16 11:43:22 -05:00
2022-09-22 11:53:08 -05:00
// Coder tracing should be disabled if telemetry is disabled unless
// --telemetry-trace was explicitly provided.
2022-10-24 20:11:00 -04:00
shouldCoderTrace := cfg . Telemetry . Enable . Value && ! isTest ()
2022-09-22 11:53:08 -05:00
// Only override if telemetryTraceEnable was specifically set.
// By default we want it to be controlled by telemetryEnable.
if cmd . Flags (). Changed ( "telemetry-trace" ) {
2022-10-24 20:11:00 -04:00
shouldCoderTrace = cfg . Telemetry . Trace . Value
2022-09-22 11:53:08 -05:00
}
2022-11-10 04:58:23 +10:00
if cfg . Trace . Enable . Value || shouldCoderTrace || cfg . Trace . HoneycombAPIKey . Value != "" {
2022-09-19 23:35:18 -05:00
sdkTracerProvider , closeTracing , err := tracing . TracerProvider ( ctx , "coderd" , tracing . TracerOpts {
2022-11-09 08:10:48 +10:00
Default : cfg . Trace . Enable . Value ,
Coder : shouldCoderTrace ,
Honeycomb : cfg . Trace . HoneycombAPIKey . Value ,
2022-09-16 11:43:22 -05:00
})
2022-05-19 17:43:07 -05:00
if err != nil {
2022-09-16 11:43:22 -05:00
logger . Warn ( ctx , "start telemetry exporter" , slog . Error ( err ))
2022-05-19 17:43:07 -05:00
} else {
2022-07-27 18:21:21 +03:00
// allow time for traces to flush even if command context is canceled
2022-05-19 17:43:07 -05:00
defer func () {
2022-09-19 23:35:18 -05:00
_ = shutdownWithTimeout ( closeTracing , 5 * time . Second )
2022-05-19 17:43:07 -05:00
}()
2022-05-20 10:51:06 -05:00
2022-09-16 11:43:22 -05:00
d , err := tracing . PostgresDriver ( sdkTracerProvider , "coderd.database" )
2022-05-20 10:51:06 -05:00
if err != nil {
2022-09-16 11:43:22 -05:00
logger . Warn ( ctx , "start postgres tracing driver" , slog . Error ( err ))
2022-05-20 10:51:06 -05:00
} else {
sqlDriver = d
}
2022-09-16 11:43:22 -05:00
tracerProvider = sdkTracerProvider
2022-05-19 17:43:07 -05:00
}
2022-03-28 17:11:52 -05:00
}
2022-03-24 09:07:33 -06:00
2022-06-15 16:02:18 -05:00
config := createConfig ( cmd )
2022-06-17 00:26:40 -05:00
builtinPostgres := false
2022-06-15 16:02:18 -05:00
// Only use built-in if PostgreSQL URL isn't specified!
2022-10-21 17:08:23 -05:00
if ! cfg . InMemoryDatabase . Value && cfg . PostgresURL . Value == "" {
2022-06-15 16:02:18 -05:00
var closeFunc func () error
cmd . Printf ( "Using built-in PostgreSQL (%s)\n" , config . PostgresPath ())
2022-10-21 17:08:23 -05:00
cfg . PostgresURL . Value , closeFunc , err = startBuiltinPostgres ( ctx , config , logger )
2022-06-15 16:02:18 -05:00
if err != nil {
return err
}
2022-06-17 00:26:40 -05:00
builtinPostgres = true
2022-06-15 16:02:18 -05:00
defer func () {
2022-07-27 18:21:21 +03:00
cmd . Printf ( "Stopping built-in PostgreSQL...\n" )
2022-06-15 16:02:18 -05:00
// Gracefully shut PostgreSQL down!
2022-11-29 15:45:14 +02:00
if err := closeFunc (); err != nil {
cmd . Printf ( "Failed to stop built-in PostgreSQL: %v\n" , err )
} else {
cmd . Printf ( "Stopped built-in PostgreSQL\n" )
}
2022-06-15 16:02:18 -05:00
}()
}
2022-12-16 06:09:19 +10:00
var (
httpListener net . Listener
httpURL * url . URL
)
if cfg . HTTPAddress . Value != "" {
httpListener , err = net . Listen ( "tcp" , cfg . HTTPAddress . Value )
if err != nil {
return xerrors . Errorf ( "listen %q: %w" , cfg . HTTPAddress . Value , err )
}
defer httpListener . Close ()
2022-03-24 13:21:05 -06:00
2023-01-09 13:59:23 -06:00
listenAddrStr := httpListener . Addr (). String ()
// For some reason if 0.0.0.0:x is provided as the http address,
// httpListener.Addr().String() likes to return it as an ipv6
// address (i.e. [::]:x). If the input ip is 0.0.0.0, try to
// coerce the output back to ipv4 to make it less confusing.
if strings . Contains ( cfg . HTTPAddress . Value , "0.0.0.0" ) {
listenAddrStr = strings . ReplaceAll ( listenAddrStr , "[::]" , "0.0.0.0" )
}
// We want to print out the address the user supplied, not the
// loopback device.
cmd . Println ( "Started HTTP listener at" , ( & url . URL { Scheme : "http" , Host : listenAddrStr }). String ())
// Set the http URL we want to use when connecting to ourselves.
2022-12-16 06:09:19 +10:00
tcpAddr , tcpAddrValid := httpListener . Addr ().( * net . TCPAddr )
if ! tcpAddrValid {
return xerrors . Errorf ( "invalid TCP address type %T" , httpListener . Addr ())
}
if tcpAddr . IP . IsUnspecified () {
tcpAddr . IP = net . IPv4 ( 127 , 0 , 0 , 1 )
}
httpURL = & url . URL {
Scheme : "http" ,
Host : tcpAddr . String (),
}
}
var (
tlsConfig * tls . Config
httpsListener net . Listener
httpsURL * url . URL
)
2022-10-24 20:11:00 -04:00
if cfg . TLS . Enable . Value {
2022-12-16 06:09:19 +10:00
if cfg . TLS . Address . Value == "" {
return xerrors . New ( "tls address must be set if tls is enabled" )
}
2022-10-17 08:43:30 -05:00
tlsConfig , err = configureTLS (
2022-10-24 20:11:00 -04:00
cfg . TLS . MinVersion . Value ,
cfg . TLS . ClientAuth . Value ,
cfg . TLS . CertFiles . Value ,
cfg . TLS . KeyFiles . Value ,
cfg . TLS . ClientCAFile . Value ,
2022-10-10 15:04:15 -04:00
)
2022-03-24 13:21:05 -06:00
if err != nil {
return xerrors . Errorf ( "configure tls: %w" , err )
}
2022-12-16 06:09:19 +10:00
httpsListenerInner , err := net . Listen ( "tcp" , cfg . TLS . Address . Value )
if err != nil {
return xerrors . Errorf ( "listen %q: %w" , cfg . TLS . Address . Value , err )
}
defer httpsListenerInner . Close ()
httpsListener = tls . NewListener ( httpsListenerInner , tlsConfig )
defer httpsListener . Close ()
2023-01-09 13:59:23 -06:00
listenAddrStr := httpsListener . Addr (). String ()
// For some reason if 0.0.0.0:x is provided as the https
// address, httpsListener.Addr().String() likes to return it as
// an ipv6 address (i.e. [::]:x). If the input ip is 0.0.0.0,
// try to coerce the output back to ipv4 to make it less
// confusing.
if strings . Contains ( cfg . HTTPAddress . Value , "0.0.0.0" ) {
listenAddrStr = strings . ReplaceAll ( listenAddrStr , "[::]" , "0.0.0.0" )
}
// We want to print out the address the user supplied, not the
// loopback device.
cmd . Println ( "Started TLS/HTTPS listener at" , ( & url . URL { Scheme : "https" , Host : listenAddrStr }). String ())
// Set the https URL we want to use when connecting to
// ourselves.
2022-12-16 06:09:19 +10:00
tcpAddr , tcpAddrValid := httpsListener . Addr ().( * net . TCPAddr )
if ! tcpAddrValid {
return xerrors . Errorf ( "invalid TCP address type %T" , httpsListener . Addr ())
}
if tcpAddr . IP . IsUnspecified () {
tcpAddr . IP = net . IPv4 ( 127 , 0 , 0 , 1 )
}
httpsURL = & url . URL {
Scheme : "https" ,
Host : tcpAddr . String (),
}
}
// Sanity check that at least one listener was started.
if httpListener == nil && httpsListener == nil {
return xerrors . New ( "must listen on at least one address" )
}
// Prefer HTTP because it's less prone to TLS errors over localhost.
localURL := httpsURL
if httpURL != nil {
localURL = httpURL
2022-03-24 13:21:05 -06:00
}
2022-12-14 09:44:29 -05:00
ctx , httpClient , err := configureHTTPClient (
ctx ,
cfg . TLS . ClientCertFile . Value ,
cfg . TLS . ClientKeyFile . Value ,
cfg . TLS . ClientCAFile . Value ,
)
if err != nil {
return xerrors . Errorf ( "configure http client: %w" , err )
}
2022-04-14 11:29:40 -04:00
var (
2022-07-27 18:21:21 +03:00
ctxTunnel , closeTunnel = context . WithCancel ( ctx )
2022-10-07 08:05:56 -05:00
tunnel * devtunnel . Tunnel
tunnelErr <- chan error
2022-04-14 11:29:40 -04:00
)
defer closeTunnel ()
2022-10-21 08:09:44 +10:00
// If the access URL is empty, we attempt to run a reverse-proxy
// tunnel to make the initial setup really simple.
2022-10-21 17:08:23 -05:00
if cfg . AccessURL . Value == "" {
2022-10-07 08:05:56 -05:00
cmd . Printf ( "Opening tunnel so workspaces can connect to your deployment. For production scenarios, specify an external access URL\n" )
tunnel , tunnelErr , err = devtunnel . New ( ctxTunnel , logger . Named ( "devtunnel" ))
2022-06-15 16:02:18 -05:00
if err != nil {
return xerrors . Errorf ( "create tunnel: %w" , err )
2022-04-11 18:54:30 -05:00
}
2022-10-21 17:08:23 -05:00
cfg . AccessURL . Value = tunnel . URL
2022-10-21 08:09:44 +10:00
2022-10-21 17:08:23 -05:00
if cfg . WildcardAccessURL . Value == "" {
2022-11-01 12:59:37 -04:00
u , err := parseURL ( tunnel . URL )
2022-10-21 08:09:44 +10:00
if err != nil {
return xerrors . Errorf ( "parse tunnel url: %w" , err )
}
// Suffixed wildcard access URL.
2022-10-21 17:08:23 -05:00
cfg . WildcardAccessURL . Value = fmt . Sprintf ( "*--%s" , u . Hostname ())
2022-10-21 08:09:44 +10:00
}
2022-03-22 13:17:50 -06:00
}
2022-04-14 11:29:40 -04:00
2022-11-01 12:59:37 -04:00
accessURLParsed , err := parseURL ( cfg . AccessURL . Value )
2022-07-31 17:49:25 -05:00
if err != nil {
return xerrors . Errorf ( "parse URL: %w" , err )
}
2022-08-31 20:09:44 -05:00
accessURLPortRaw := accessURLParsed . Port ()
if accessURLPortRaw == "" {
accessURLPortRaw = "80"
if accessURLParsed . Scheme == "https" {
accessURLPortRaw = "443"
}
}
accessURLPort , err := strconv . Atoi ( accessURLPortRaw )
if err != nil {
return xerrors . Errorf ( "parse access URL port: %w" , err )
}
2022-07-31 17:49:25 -05:00
2022-06-10 13:35:51 -05:00
// Warn the user if the access URL appears to be a loopback address.
2022-07-31 17:49:25 -05:00
isLocal , err := isLocalURL ( ctx , accessURLParsed )
2022-06-10 13:35:51 -05:00
if isLocal || err != nil {
2022-06-15 16:02:18 -05:00
reason := "could not be resolved"
2022-06-10 13:35:51 -05:00
if isLocal {
2022-06-15 16:02:18 -05:00
reason = "isn't externally reachable"
2022-06-10 13:35:51 -05:00
}
2022-10-07 08:05:56 -05:00
cmd . Printf ( "%s The access URL %s %s, this may cause unexpected problems when creating workspaces. Generate a unique *.try.coder.app URL by not specifying an access URL.\n" , cliui . Styles . Warn . Render ( "Warning:" ), cliui . Styles . Field . Render ( accessURLParsed . String ()), reason )
2022-03-22 13:17:50 -06:00
}
2022-03-24 09:07:33 -06:00
2022-12-16 06:09:19 +10:00
// Redirect from the HTTP listener to the access URL if:
// 1. The redirect flag is enabled.
// 2. HTTP listening is enabled (obviously).
// 3. TLS is enabled (otherwise they're likely using a reverse proxy
// which can do this instead).
// 4. The access URL has been set manually (not a tunnel).
// 5. The access URL is HTTPS.
shouldRedirectHTTPToAccessURL := cfg . TLS . RedirectHTTP . Value && cfg . HTTPAddress . Value != "" && cfg . TLS . Enable . Value && tunnel == nil && accessURLParsed . Scheme == "https"
2022-10-07 08:05:56 -05:00
// A newline is added before for visibility in terminal output.
cmd . Printf ( "\nView the Web UI: %s\n" , accessURLParsed . String ())
2022-04-05 19:18:26 -05:00
2022-06-15 16:02:18 -05:00
// Used for zero-trust instance identity with Google Cloud.
2022-07-27 18:21:21 +03:00
googleTokenValidator , err := idtoken . NewValidator ( ctx , option . WithoutAuthentication ())
2022-06-15 16:02:18 -05:00
if err != nil {
return err
}
2022-10-21 17:08:23 -05:00
sshKeygenAlgorithm , err := gitsshkey . ParseAlgorithm ( cfg . SSHKeygenAlgorithm . Value )
2022-04-05 19:18:26 -05:00
if err != nil {
2022-10-21 17:08:23 -05:00
return xerrors . Errorf ( "parse ssh keygen algorithm %s: %w" , cfg . SSHKeygenAlgorithm . Value , err )
2022-04-05 19:18:26 -05:00
}
2022-09-02 18:47:25 -05:00
defaultRegion := & tailcfg . DERPRegion {
2022-09-26 12:56:04 -05:00
EmbeddedRelay : true ,
2022-10-24 20:11:00 -04:00
RegionID : cfg . DERP . Server . RegionID . Value ,
RegionCode : cfg . DERP . Server . RegionCode . Value ,
RegionName : cfg . DERP . Server . RegionName . Value ,
2022-08-31 20:09:44 -05:00
Nodes : [] * tailcfg . DERPNode {{
2022-10-24 20:11:00 -04:00
Name : fmt . Sprintf ( "%db" , cfg . DERP . Server . RegionID . Value ),
RegionID : cfg . DERP . Server . RegionID . Value ,
2022-08-31 20:09:44 -05:00
HostName : accessURLParsed . Hostname (),
DERPPort : accessURLPort ,
STUNPort : - 1 ,
ForceHTTP : accessURLParsed . Scheme == "http" ,
}},
2022-09-02 18:47:25 -05:00
}
2022-10-24 20:11:00 -04:00
if ! cfg . DERP . Server . Enable . Value {
2022-09-02 18:47:25 -05:00
defaultRegion = nil
}
2022-10-24 20:11:00 -04:00
derpMap , err := tailnet . NewDERPMap ( ctx , defaultRegion , cfg . DERP . Server . STUNAddresses . Value , cfg . DERP . Config . URL . Value , cfg . DERP . Config . Path . Value )
2022-08-31 20:09:44 -05:00
if err != nil {
return xerrors . Errorf ( "create derp map: %w" , err )
}
2022-10-21 17:08:23 -05:00
appHostname := strings . TrimSpace ( cfg . WildcardAccessURL . Value )
2022-10-15 04:25:11 +10:00
var appHostnameRegex * regexp . Regexp
if appHostname != "" {
appHostnameRegex , err = httpapi . CompileHostnamePattern ( appHostname )
if err != nil {
return xerrors . Errorf ( "parse wildcard access URL %q: %w" , appHostname , err )
}
}
2022-09-23 08:30:32 +10:00
2022-10-24 19:46:24 -05:00
gitAuthConfigs , err := gitauth . ConvertConfig ( cfg . GitAuth . Value , accessURLParsed )
if err != nil {
return xerrors . Errorf ( "parse git auth config: %w" , err )
}
2022-10-23 13:21:49 -05:00
realIPConfig , err := httpmw . ParseRealIPConfig ( cfg . ProxyTrustedHeaders . Value , cfg . ProxyTrustedOrigins . Value )
if err != nil {
return xerrors . Errorf ( "parse real ip config: %w" , err )
}
2022-03-24 09:07:33 -06:00
options := & coderd . Options {
2022-09-01 14:58:23 -05:00
AccessURL : accessURLParsed ,
2022-09-23 08:30:32 +10:00
AppHostname : appHostname ,
2022-10-15 04:25:11 +10:00
AppHostnameRegex : appHostnameRegex ,
2022-09-01 14:58:23 -05:00
Logger : logger . Named ( "coderd" ),
Database : databasefake . New (),
DERPMap : derpMap ,
Pubsub : database . NewPubsubInMemory (),
2022-12-07 03:05:14 +10:00
CacheDir : cacheDir ,
2022-09-01 14:58:23 -05:00
GoogleTokenValidator : googleTokenValidator ,
2022-10-24 19:46:24 -05:00
GitAuthConfigs : gitAuthConfigs ,
2022-10-23 13:21:49 -05:00
RealIPConfig : realIPConfig ,
2022-10-21 17:08:23 -05:00
SecureAuthCookie : cfg . SecureAuthCookie . Value ,
2022-09-01 14:58:23 -05:00
SSHKeygenAlgorithm : sshKeygenAlgorithm ,
TracerProvider : tracerProvider ,
Telemetry : telemetry . NewNoop (),
2022-10-21 17:08:23 -05:00
MetricsCacheRefreshInterval : cfg . MetricsCacheRefreshInterval . Value ,
AgentStatsRefreshInterval : cfg . AgentStatRefreshInterval . Value ,
2022-10-24 20:11:00 -04:00
DeploymentConfig : cfg ,
2022-11-04 19:03:01 -05:00
PrometheusRegistry : prometheus . NewRegistry (),
2023-01-05 12:05:20 -06:00
APIRateLimit : cfg . RateLimit . API . Value ,
LoginRateLimit : loginRateLimit ,
FilesRateLimit : filesRateLimit ,
2022-12-14 09:44:29 -05:00
HTTPClient : httpClient ,
2022-03-24 09:07:33 -06:00
}
2022-10-17 08:43:30 -05:00
if tlsConfig != nil {
options . TLSCertificates = tlsConfig . Certificates
}
2022-03-24 09:07:33 -06:00
2022-12-01 19:43:28 +02:00
if cfg . UpdateCheck . Value {
options . UpdateCheckOptions = & updatecheck . Options {
// Avoid spamming GitHub API checking for updates.
Interval : 24 * time . Hour ,
// Inform server admins of new versions.
Notify : func ( r updatecheck . Result ) {
if semver . Compare ( r . Version , buildinfo . Version ()) > 0 {
options . Logger . Info (
context . Background (),
"new version of coder available" ,
slog . F ( "new_version" , r . Version ),
slog . F ( "url" , r . URL ),
slog . F ( "upgrade_instructions" , "https://coder.com/docs/coder-oss/latest/admin/upgrade" ),
)
}
},
}
}
2022-10-24 20:11:00 -04:00
if cfg . OAuth2 . Github . ClientSecret . Value != "" {
2022-10-10 15:04:15 -04:00
options . GithubOAuth2Config , err = configureGithubOAuth2 ( accessURLParsed ,
2022-10-24 20:11:00 -04:00
cfg . OAuth2 . Github . ClientID . Value ,
cfg . OAuth2 . Github . ClientSecret . Value ,
cfg . OAuth2 . Github . AllowSignups . Value ,
2022-11-15 18:56:46 +02:00
cfg . OAuth2 . Github . AllowEveryone . Value ,
2022-10-24 20:11:00 -04:00
cfg . OAuth2 . Github . AllowedOrgs . Value ,
cfg . OAuth2 . Github . AllowedTeams . Value ,
cfg . OAuth2 . Github . EnterpriseBaseURL . Value ,
2022-10-10 15:04:15 -04:00
)
2022-04-23 17:58:57 -05:00
if err != nil {
return xerrors . Errorf ( "configure github oauth2: %w" , err )
}
}
2022-10-24 20:11:00 -04:00
if cfg . OIDC . ClientSecret . Value != "" {
if cfg . OIDC . ClientID . Value == "" {
2022-07-31 23:05:35 -05:00
return xerrors . Errorf ( "OIDC client ID be set!" )
}
2022-10-24 20:11:00 -04:00
if cfg . OIDC . IssuerURL . Value == "" {
2022-07-31 23:05:35 -05:00
return xerrors . Errorf ( "OIDC issuer URL must be set!" )
}
2022-11-25 10:10:09 +00:00
if cfg . OIDC . IgnoreEmailVerified . Value {
logger . Warn ( ctx , "coder will not check email_verified for OIDC logins" )
}
2022-10-24 20:11:00 -04:00
oidcProvider , err := oidc . NewProvider ( ctx , cfg . OIDC . IssuerURL . Value )
2022-07-31 23:05:35 -05:00
if err != nil {
return xerrors . Errorf ( "configure oidc provider: %w" , err )
}
redirectURL , err := accessURLParsed . Parse ( "/api/v2/users/oidc/callback" )
if err != nil {
return xerrors . Errorf ( "parse oidc oauth callback url: %w" , err )
}
options . OIDCConfig = & coderd . OIDCConfig {
OAuth2Config : & oauth2 . Config {
2022-10-24 20:11:00 -04:00
ClientID : cfg . OIDC . ClientID . Value ,
ClientSecret : cfg . OIDC . ClientSecret . Value ,
2022-07-31 23:05:35 -05:00
RedirectURL : redirectURL . String (),
Endpoint : oidcProvider . Endpoint (),
2022-10-24 20:11:00 -04:00
Scopes : cfg . OIDC . Scopes . Value ,
2022-07-31 23:05:35 -05:00
},
Verifier : oidcProvider . Verifier ( & oidc . Config {
2022-10-24 20:11:00 -04:00
ClientID : cfg . OIDC . ClientID . Value ,
2022-07-31 23:05:35 -05:00
}),
2023-01-04 22:16:31 +01:00
EmailDomain : cfg . OIDC . EmailDomain . Value ,
AllowSignups : cfg . OIDC . AllowSignups . Value ,
UsernameField : cfg . OIDC . UsernameField . Value ,
2022-07-31 23:05:35 -05:00
}
}
2022-10-21 17:08:23 -05:00
if cfg . InMemoryDatabase . Value {
2022-06-15 16:02:18 -05:00
options . Database = databasefake . New ()
options . Pubsub = database . NewPubsubInMemory ()
} else {
2022-11-14 20:10:37 -06:00
logger . Debug ( ctx , "connecting to postgresql" )
2022-10-21 17:08:23 -05:00
sqlDB , err := sql . Open ( sqlDriver , cfg . PostgresURL . Value )
2022-03-24 09:07:33 -06:00
if err != nil {
return xerrors . Errorf ( "dial postgres: %w" , err )
}
2022-07-27 18:21:21 +03:00
defer sqlDB . Close ()
2023-01-06 21:07:22 -06:00
pingCtx , pingCancel := context . WithTimeout ( ctx , 15 * time . Second )
defer pingCancel ()
err = sqlDB . PingContext ( pingCtx )
if err != nil {
return xerrors . Errorf ( "ping postgres: %w" , err )
}
2022-10-17 20:02:25 -05:00
// Ensure the PostgreSQL version is >=13.0.0!
version , err := sqlDB . QueryContext ( ctx , "SHOW server_version;" )
if err != nil {
return xerrors . Errorf ( "get postgres version: %w" , err )
}
if ! version . Next () {
return xerrors . Errorf ( "no rows returned for version select" )
}
var versionStr string
err = version . Scan ( & versionStr )
if err != nil {
return xerrors . Errorf ( "scan version: %w" , err )
}
2022-10-24 19:46:24 -05:00
_ = version . Close ()
2022-10-17 20:02:25 -05:00
versionStr = strings . Split ( versionStr , " " )[ 0 ]
if semver . Compare ( "v" + versionStr , "v13" ) < 0 {
return xerrors . New ( "PostgreSQL version must be v13.0.0 or higher!" )
}
2022-11-14 20:10:37 -06:00
logger . Debug ( ctx , "connected to postgresql" , slog . F ( "version" , versionStr ))
2022-07-27 18:21:21 +03:00
2022-09-20 03:39:02 +10:00
err = migrations . Up ( sqlDB )
2022-03-24 09:07:33 -06:00
if err != nil {
return xerrors . Errorf ( "migrate up: %w" , err )
}
2022-11-09 10:25:25 -06:00
// The default is 0 but the request will fail with a 500 if the DB
// cannot accept new connections, so we try to limit that here.
// Requests will wait for a new connection instead of a hard error
// if a limit is set.
sqlDB . SetMaxOpenConns ( 10 )
// Allow a max of 3 idle connections at a time. Lower values end up
// creating a lot of connection churn. Since each connection uses about
// 10MB of memory, we're allocating 30MB to Postgres connections per
// replica, but is better than causing Postgres to spawn a thread 15-20
// times/sec. PGBouncer's transaction pooling is not the greatest so
// it's not optimal for us to deploy.
//
// This was set to 10 before we started doing HA deployments, but 3 was
// later determined to be a better middle ground as to not use up all
// of PGs default connection limit while simultaneously avoiding a lot
// of connection churn.
sqlDB . SetMaxIdleConns ( 3 )
2022-03-24 09:07:33 -06:00
options . Database = database . New ( sqlDB )
2022-10-21 17:08:23 -05:00
options . Pubsub , err = database . NewPubsub ( ctx , sqlDB , cfg . PostgresURL . Value )
2022-03-24 09:07:33 -06:00
if err != nil {
return xerrors . Errorf ( "create pubsub: %w" , err )
}
2022-07-27 18:21:21 +03:00
defer options . Pubsub . Close ()
2022-03-24 09:07:33 -06:00
}
2022-07-27 18:21:21 +03:00
deploymentID , err := options . Database . GetDeploymentID ( ctx )
2022-06-17 00:26:40 -05:00
if errors . Is ( err , sql . ErrNoRows ) {
err = nil
}
if err != nil {
return xerrors . Errorf ( "get deployment id: %w" , err )
}
if deploymentID == "" {
deploymentID = uuid . NewString ()
2022-07-27 18:21:21 +03:00
err = options . Database . InsertDeploymentID ( ctx , deploymentID )
2022-06-17 00:26:40 -05:00
if err != nil {
return xerrors . Errorf ( "set deployment id: %w" , err )
}
}
2022-06-17 09:02:44 -05:00
// Disable telemetry if the in-memory database is used unless explicitly defined!
2022-10-24 20:11:00 -04:00
if cfg . InMemoryDatabase . Value && ! cmd . Flags (). Changed ( cfg . Telemetry . Enable . Flag ) {
cfg . Telemetry . Enable . Value = false
2022-06-17 09:02:44 -05:00
}
2022-10-24 20:11:00 -04:00
if cfg . Telemetry . Enable . Value {
2022-11-03 20:09:21 +02:00
// Parse the raw telemetry URL!
telemetryURL , err := parseURL ( cfg . Telemetry . URL . Value )
if err != nil {
return xerrors . Errorf ( "parse telemetry url: %w" , err )
}
2022-11-14 10:11:08 -06:00
gitAuth := make ([] telemetry . GitAuth , 0 )
for _ , cfg := range gitAuthConfigs {
gitAuth = append ( gitAuth , telemetry . GitAuth {
Type : string ( cfg . Type ),
})
}
2022-06-17 00:26:40 -05:00
options . Telemetry , err = telemetry . New ( telemetry . Options {
2022-11-14 10:11:08 -06:00
BuiltinPostgres : builtinPostgres ,
DeploymentID : deploymentID ,
Database : options . Database ,
Logger : logger . Named ( "telemetry" ),
URL : telemetryURL ,
Wildcard : cfg . WildcardAccessURL . Value != "" ,
DERPServerRelayURL : cfg . DERP . Server . RelayURL . Value ,
GitAuth : gitAuth ,
GitHubOAuth : cfg . OAuth2 . Github . ClientID . Value != "" ,
OIDCAuth : cfg . OIDC . ClientID . Value != "" ,
OIDCIssuerURL : cfg . OIDC . IssuerURL . Value ,
Prometheus : cfg . Prometheus . Enable . Value ,
STUN : len ( cfg . DERP . Server . STUNAddresses . Value ) != 0 ,
Tunnel : tunnel != nil ,
2022-06-17 00:26:40 -05:00
})
if err != nil {
return xerrors . Errorf ( "create telemetry reporter: %w" , err )
}
defer options . Telemetry . Close ()
}
2022-08-08 10:09:46 -05:00
// This prevents the pprof import from being accidentally deleted.
_ = pprof . Handler
2022-10-24 20:11:00 -04:00
if cfg . Pprof . Enable . Value {
2022-08-08 10:09:46 -05:00
//nolint:revive
2022-10-24 20:11:00 -04:00
defer serveHandler ( ctx , logger , nil , cfg . Pprof . Address . Value , "pprof" )()
2022-08-08 10:09:46 -05:00
}
2022-10-24 20:11:00 -04:00
if cfg . Prometheus . Enable . Value {
2022-11-04 19:03:01 -05:00
options . PrometheusRegistry . MustRegister ( collectors . NewGoCollector ())
options . PrometheusRegistry . MustRegister ( collectors . NewProcessCollector ( collectors . ProcessCollectorOpts {}))
closeUsersFunc , err := prometheusmetrics . ActiveUsers ( ctx , options . PrometheusRegistry , options . Database , 0 )
2022-08-08 10:09:46 -05:00
if err != nil {
return xerrors . Errorf ( "register active users prometheus metric: %w" , err )
}
2022-08-08 20:08:42 -05:00
defer closeUsersFunc ()
2022-11-04 19:03:01 -05:00
closeWorkspacesFunc , err := prometheusmetrics . Workspaces ( ctx , options . PrometheusRegistry , options . Database , 0 )
2022-08-08 20:08:42 -05:00
if err != nil {
return xerrors . Errorf ( "register workspaces prometheus metric: %w" , err )
}
defer closeWorkspacesFunc ()
2022-08-08 10:09:46 -05:00
//nolint:revive
2022-11-04 19:03:01 -05:00
defer serveHandler ( ctx , logger , promhttp . InstrumentMetricHandler (
options . PrometheusRegistry , promhttp . HandlerFor ( options . PrometheusRegistry , promhttp . HandlerOpts {}),
), cfg . Prometheus . Address . Value , "prometheus" )()
2022-08-08 10:09:46 -05:00
}
2022-12-19 18:43:46 +01:00
if cfg . Swagger . Enable . Value {
options . SwaggerEndpoint = cfg . Swagger . Enable . Value
}
2022-10-17 18:36:23 -05:00
// We use a separate coderAPICloser so the Enterprise API
2022-10-17 08:43:30 -05:00
// can have it's own close functions. This is cleaner
// than abstracting the Coder API itself.
2022-10-17 18:36:23 -05:00
coderAPI , coderAPICloser , err := newAPI ( ctx , options )
2022-09-19 23:11:01 -05:00
if err != nil {
return err
}
2022-07-27 18:21:21 +03:00
2022-03-22 13:17:50 -06:00
client := codersdk . New ( localURL )
2022-12-20 05:25:59 +10:00
if localURL . Scheme == "https" && isLocalhost ( localURL . Hostname ()) {
// The certificate will likely be self-signed or for a different
// hostname, so we need to skip verification.
2022-03-24 13:21:05 -06:00
client . HTTPClient . Transport = & http . Transport {
2022-03-24 18:20:13 -06:00
TLSClientConfig : & tls . Config {
//nolint:gosec
InsecureSkipVerify : true ,
},
2022-03-24 13:21:05 -06:00
}
}
2022-12-20 05:25:59 +10:00
defer client . HTTPClient . CloseIdleConnections ()
2022-03-22 13:17:50 -06:00
2022-12-16 06:09:19 +10:00
// This is helpful for tests, but can be silently ignored.
// Coder may be ran as users that don't have permission to write in the homedir,
// such as via the systemd service.
_ = config . URL (). Write ( client . URL . String ())
2022-07-27 18:21:21 +03:00
// Since errCh only has one buffered slot, all routines
// sending on it must be wrapped in a select/default to
// avoid leaving dangling goroutines waiting for the
// channel to be consumed.
2022-04-18 15:11:59 -05:00
errCh := make ( chan error , 1 )
2022-03-24 09:07:33 -06:00
provisionerDaemons := make ([] * provisionerd . Server , 0 )
2022-07-27 18:21:21 +03:00
defer func () {
// We have no graceful shutdown of provisionerDaemons
// here because that's handled at the end of main, this
// is here in case the program exits early.
for _ , daemon := range provisionerDaemons {
_ = daemon . Close ()
}
}()
2022-11-04 19:03:01 -05:00
provisionerdMetrics := provisionerd . NewMetrics ( options . PrometheusRegistry )
2022-11-08 14:19:40 +01:00
for i := 0 ; i < cfg . Provisioner . Daemons . Value ; i ++ {
2022-12-07 03:05:14 +10:00
daemonCacheDir := filepath . Join ( cacheDir , fmt . Sprintf ( "provisioner-%d" , i ))
daemon , err := newProvisionerDaemon ( ctx , coderAPI , provisionerdMetrics , logger , cfg , daemonCacheDir , errCh , false )
2022-03-24 09:07:33 -06:00
if err != nil {
return xerrors . Errorf ( "create provisioner daemon: %w" , err )
}
2022-07-27 18:21:21 +03:00
provisionerDaemons = append ( provisionerDaemons , daemon )
}
shutdownConnsCtx , shutdownConns := context . WithCancel ( ctx )
defer shutdownConns ()
2022-08-29 12:07:49 -05:00
2022-12-16 06:09:19 +10:00
// Wrap the server in middleware that redirects to the access URL if
// the request is not to a local IP.
var handler http . Handler = coderAPI . RootHandler
if shouldRedirectHTTPToAccessURL {
handler = redirectHTTPToAccessURL ( handler , accessURLParsed )
}
// ReadHeaderTimeout is purposefully not enabled. It caused some
// issues with websockets over the dev tunnel.
2022-08-29 12:07:49 -05:00
// See: https://github.com/coder/coder/pull/3730
//nolint:gosec
2022-12-16 06:09:19 +10:00
httpServer := & http . Server {
// These errors are typically noise like "TLS: EOF". Vault does
// similar:
2022-07-27 18:21:21 +03:00
// https://github.com/hashicorp/vault/blob/e2490059d0711635e529a4efcbaa1b26998d6e1c/command/server.go#L2714
2022-08-29 12:07:49 -05:00
ErrorLog : log . New ( io . Discard , "" , 0 ),
2022-12-16 06:09:19 +10:00
Handler : handler ,
2022-07-27 18:21:21 +03:00
BaseContext : func ( _ net . Listener ) context . Context {
return shutdownConnsCtx
},
2022-03-22 13:17:50 -06:00
}
2022-03-24 09:07:33 -06:00
defer func () {
2022-12-16 06:09:19 +10:00
_ = shutdownWithTimeout ( httpServer . Shutdown , 5 * time . Second )
2022-03-24 09:07:33 -06:00
}()
2022-03-22 13:17:50 -06:00
2022-12-16 06:09:19 +10:00
// We call this in the routine so we can kill the other listeners if
// one of them fails.
closeListenersNow := func () {
if httpListener != nil {
_ = httpListener . Close ()
2022-06-10 13:38:11 -05:00
}
2022-12-16 06:09:19 +10:00
if httpsListener != nil {
_ = httpsListener . Close ()
}
if tunnel != nil {
_ = tunnel . Listener . Close ()
}
}
2022-06-10 13:38:11 -05:00
2022-12-16 06:09:19 +10:00
eg := errgroup . Group {}
if httpListener != nil {
2022-07-27 18:21:21 +03:00
eg . Go ( func () error {
2022-12-16 06:09:19 +10:00
defer closeListenersNow ()
return httpServer . Serve ( httpListener )
2022-07-27 18:21:21 +03:00
})
}
2022-12-16 06:09:19 +10:00
if httpsListener != nil {
eg . Go ( func () error {
defer closeListenersNow ()
return httpServer . Serve ( httpsListener )
})
}
if tunnel != nil {
eg . Go ( func () error {
defer closeListenersNow ()
return httpServer . Serve ( tunnel . Listener )
})
}
2022-07-27 18:21:21 +03:00
go func () {
select {
case errCh <- eg . Wait ():
default :
}
2022-03-22 13:17:50 -06:00
}()
2022-07-27 18:21:21 +03:00
hasFirstUser , err := client . HasFirstUser ( ctx )
2023-01-06 21:07:22 -06:00
if err != nil {
cmd . Println ( "\nFailed to check for the first user: " + err . Error ())
} else if ! hasFirstUser {
cmd . Println ( "\nGet started by creating the first user (in a new terminal):" )
2022-07-31 17:49:25 -05:00
cmd . Println ( cliui . Styles . Code . Render ( "coder login " + accessURLParsed . String ()))
2022-03-22 13:17:50 -06:00
}
2022-06-15 16:02:18 -05:00
cmd . Println ( "\n==> Logs will stream in below (press ctrl+c to gracefully exit):" )
2022-03-24 09:07:33 -06:00
// Updates the systemd status from activating to activated.
_ , err = daemon . SdNotify ( false , daemon . SdNotifyReady )
if err != nil {
return xerrors . Errorf ( "notify systemd: %w" , err )
}
2022-10-21 17:08:23 -05:00
autobuildPoller := time . NewTicker ( cfg . AutobuildPollInterval . Value )
2022-05-20 11:57:02 +01:00
defer autobuildPoller . Stop ()
2022-07-27 18:21:21 +03:00
autobuildExecutor := executor . New ( ctx , options . Database , logger , autobuildPoller . C )
2022-05-20 11:57:02 +01:00
autobuildExecutor . Run ()
2022-05-11 23:03:02 +01:00
2022-07-27 18:21:21 +03:00
// Currently there is no way to ask the server to shut
// itself down, so any exit signal will result in a non-zero
// exit of the server.
var exitErr error
2022-03-22 13:17:50 -06:00
select {
2022-08-18 16:25:32 +03:00
case <- notifyCtx . Done ():
exitErr = notifyCtx . Err ()
2022-07-27 18:21:21 +03:00
_ , _ = fmt . Fprintln ( cmd . OutOrStdout (), cliui . Styles . Bold . Render (
"Interrupt caught, gracefully exiting. Use ctrl+\\ to force quit" ,
))
2022-10-07 08:05:56 -05:00
case exitErr = <- tunnelErr :
2022-07-27 18:21:21 +03:00
if exitErr == nil {
exitErr = xerrors . New ( "dev tunnel closed unexpectedly" )
2022-04-14 11:29:40 -04:00
}
2022-07-27 18:21:21 +03:00
case exitErr = <- errCh :
2022-03-22 13:17:50 -06:00
}
2022-07-27 18:21:21 +03:00
if exitErr != nil && ! xerrors . Is ( exitErr , context . Canceled ) {
cmd . Printf ( "Unexpected error, shutting down server: %s\n" , exitErr )
}
// Begin clean shut down stage, we try to shut down services
// gracefully in an order that gives the best experience.
// This procedure should not differ greatly from the order
// of `defer`s in this function, but allows us to inform
// the user about what's going on and handle errors more
// explicitly.
2022-03-24 09:07:33 -06:00
_ , err = daemon . SdNotify ( false , daemon . SdNotifyStopping )
if err != nil {
2022-07-27 18:21:21 +03:00
cmd . Printf ( "Notify systemd failed: %s" , err )
2022-03-24 09:07:33 -06:00
}
2022-07-27 18:21:21 +03:00
// Stop accepting new connections without interrupting
// in-flight requests, give in-flight requests 5 seconds to
// complete.
cmd . Println ( "Shutting down API server..." )
2022-12-16 06:09:19 +10:00
err = shutdownWithTimeout ( httpServer . Shutdown , 3 * time . Second )
2022-07-27 18:21:21 +03:00
if err != nil {
2022-10-07 08:05:56 -05:00
cmd . Printf ( "API server shutdown took longer than 3s: %s\n" , err )
2022-07-27 18:21:21 +03:00
} else {
cmd . Printf ( "Gracefully shut down API server\n" )
}
// Cancel any remaining in-flight requests.
shutdownConns ()
// Shut down provisioners before waiting for WebSockets
// connections to close.
var wg sync . WaitGroup
for i , provisionerDaemon := range provisionerDaemons {
id := i + 1
provisionerDaemon := provisionerDaemon
wg . Add ( 1 )
go func () {
defer wg . Done ()
2022-10-21 17:08:23 -05:00
if ok , _ := cmd . Flags (). GetBool ( varVerbose ); ok {
2022-07-27 18:21:21 +03:00
cmd . Printf ( "Shutting down provisioner daemon %d...\n" , id )
}
2022-09-19 23:35:18 -05:00
err := shutdownWithTimeout ( provisionerDaemon . Shutdown , 5 * time . Second )
2022-07-27 18:21:21 +03:00
if err != nil {
cmd . PrintErrf ( "Failed to shutdown provisioner daemon %d: %s\n" , id , err )
return
}
err = provisionerDaemon . Close ()
if err != nil {
cmd . PrintErrf ( "Close provisioner daemon %d: %s\n" , id , err )
return
}
2022-10-21 17:08:23 -05:00
if ok , _ := cmd . Flags (). GetBool ( varVerbose ); ok {
2022-07-27 18:21:21 +03:00
cmd . Printf ( "Gracefully shut down provisioner daemon %d\n" , id )
}
}()
}
wg . Wait ()
cmd . Println ( "Waiting for WebSocket connections to close..." )
2022-10-17 18:36:23 -05:00
_ = coderAPICloser . Close ()
2022-09-13 13:31:33 -04:00
cmd . Println ( "Done waiting for WebSocket connections" )
2022-07-27 18:21:21 +03:00
// Close tunnel after we no longer have in-flight connections.
2022-10-07 08:05:56 -05:00
if tunnel != nil {
2022-06-15 16:02:18 -05:00
cmd . Println ( "Waiting for tunnel to close..." )
2022-04-14 11:29:40 -04:00
closeTunnel ()
2022-10-07 08:05:56 -05:00
<- tunnelErr
2022-07-27 18:21:21 +03:00
cmd . Println ( "Done waiting for tunnel" )
2022-04-14 11:29:40 -04:00
}
2022-06-17 00:26:40 -05:00
// Ensures a last report can be sent before exit!
options . Telemetry . Close ()
2022-07-27 18:21:21 +03:00
// Trigger context cancellation for any remaining services.
cancel ()
2022-09-19 23:56:51 -05:00
if xerrors . Is ( exitErr , context . Canceled ) {
return nil
}
2022-07-27 18:21:21 +03:00
return exitErr
2022-03-22 13:17:50 -06:00
},
}
2022-03-28 14:26:41 -05:00
2022-12-20 20:51:17 +02:00
var pgRawURL bool
postgresBuiltinURLCmd := & cobra . Command {
2022-06-15 16:02:18 -05:00
Use : "postgres-builtin-url" ,
Short : "Output the connection URL for the built-in PostgreSQL deployment." ,
2022-08-29 14:37:18 +03:00
RunE : func ( cmd * cobra . Command , _ [] string ) error {
2022-06-15 16:02:18 -05:00
cfg := createConfig ( cmd )
url , err := embeddedPostgresURL ( cfg )
if err != nil {
return err
}
2022-12-20 20:51:17 +02:00
if pgRawURL {
_ , _ = fmt . Fprintf ( cmd . OutOrStdout (), "%s\n" , url )
} else {
_ , _ = fmt . Fprintf ( cmd . OutOrStdout (), "%s\n" , cliui . Styles . Code . Render ( fmt . Sprintf ( "psql %q" , url )))
}
2022-06-15 16:02:18 -05:00
return nil
},
2022-12-20 20:51:17 +02:00
}
postgresBuiltinServeCmd := & cobra . Command {
2022-07-14 17:51:44 -04:00
Use : "postgres-builtin-serve" ,
Short : "Run the built-in PostgreSQL deployment." ,
RunE : func ( cmd * cobra . Command , args [] string ) error {
2022-12-20 20:51:17 +02:00
ctx := cmd . Context ()
2022-07-14 17:51:44 -04:00
cfg := createConfig ( cmd )
2022-07-26 17:23:32 +03:00
logger := slog . Make ( sloghuman . Sink ( cmd . ErrOrStderr ()))
2022-10-21 17:08:23 -05:00
if ok , _ := cmd . Flags (). GetBool ( varVerbose ); ok {
2022-07-14 17:51:44 -04:00
logger = logger . Leveled ( slog . LevelDebug )
}
2022-12-20 20:51:17 +02:00
ctx , cancel := signal . NotifyContext ( ctx , InterruptSignals ... )
defer cancel ()
url , closePg , err := startBuiltinPostgres ( ctx , cfg , logger )
2022-07-14 17:51:44 -04:00
if err != nil {
return err
}
defer func () { _ = closePg () }()
2022-12-20 20:51:17 +02:00
if pgRawURL {
_ , _ = fmt . Fprintf ( cmd . OutOrStdout (), "%s\n" , url )
} else {
_ , _ = fmt . Fprintf ( cmd . OutOrStdout (), "%s\n" , cliui . Styles . Code . Render ( fmt . Sprintf ( "psql %q" , url )))
}
2022-07-14 17:51:44 -04:00
2022-12-20 20:51:17 +02:00
<- ctx . Done ()
2022-07-14 17:51:44 -04:00
return nil
},
2022-12-20 20:51:17 +02:00
}
postgresBuiltinURLCmd . Flags (). BoolVar ( & pgRawURL , "raw-url" , false , "Output the raw connection URL instead of a psql command." )
postgresBuiltinServeCmd . Flags (). BoolVar ( & pgRawURL , "raw-url" , false , "Output the raw connection URL instead of a psql command." )
root . AddCommand ( postgresBuiltinURLCmd , postgresBuiltinServeCmd )
2022-07-14 17:51:44 -04:00
2022-10-21 17:08:23 -05:00
deployment . AttachFlags ( root . Flags (), vip , false )
2022-03-22 13:17:50 -06:00
return root
}
2022-11-01 12:59:37 -04:00
// parseURL parses a string into a URL.
func parseURL ( u string ) ( * url . URL , error ) {
2022-07-31 17:49:25 -05:00
var (
hasScheme = strings . HasPrefix ( u , "http:" ) || strings . HasPrefix ( u , "https:" )
)
if ! hasScheme {
2022-11-01 12:59:37 -04:00
return nil , xerrors . Errorf ( "URL %q must have a scheme of either http or https" , u )
2022-07-31 17:49:25 -05:00
}
parsed , err := url . Parse ( u )
if err != nil {
return nil , err
}
return parsed , nil
}
// isLocalURL returns true if the hostname of the provided URL appears to
// resolve to a loopback address.
func isLocalURL ( ctx context . Context , u * url . URL ) ( bool , error ) {
resolver := & net . Resolver {}
ips , err := resolver . LookupIPAddr ( ctx , u . Hostname ())
if err != nil {
return false , err
}
for _ , ip := range ips {
if ip . IP . IsLoopback () {
return true , nil
}
}
return false , nil
}
2022-09-19 23:35:18 -05:00
func shutdownWithTimeout ( shutdown func ( context . Context ) error , timeout time . Duration ) error {
2022-07-27 18:21:21 +03:00
ctx , cancel := context . WithTimeout ( context . Background (), timeout )
defer cancel ()
2022-09-19 23:35:18 -05:00
return shutdown ( ctx )
2022-07-27 18:21:21 +03:00
}
2022-05-19 15:47:45 -07:00
// nolint:revive
2022-09-16 11:43:22 -05:00
func newProvisionerDaemon (
ctx context . Context ,
coderAPI * coderd . API ,
2022-11-04 19:03:01 -05:00
metrics provisionerd . Metrics ,
2022-09-16 11:43:22 -05:00
logger slog . Logger ,
2022-11-08 14:19:40 +01:00
cfg * codersdk . DeploymentConfig ,
2022-12-07 03:05:14 +10:00
cacheDir string ,
2022-09-16 11:43:22 -05:00
errCh chan error ,
dev bool ,
2022-07-27 18:21:21 +03:00
) ( srv * provisionerd . Server , err error ) {
ctx , cancel := context . WithCancel ( ctx )
defer func () {
if err != nil {
cancel ()
}
}()
2022-12-07 03:05:14 +10:00
err = os . MkdirAll ( cacheDir , 0 o700 )
2022-03-30 17:59:54 -05:00
if err != nil {
2022-12-07 03:05:14 +10:00
return nil , xerrors . Errorf ( "mkdir %q: %w" , cacheDir , err )
2022-03-30 17:59:54 -05:00
}
2022-11-22 12:19:32 -06:00
terraformClient , terraformServer := provisionersdk . MemTransportPipe ()
2022-03-22 13:17:50 -06:00
go func () {
2022-07-27 18:21:21 +03:00
<- ctx . Done ()
_ = terraformClient . Close ()
_ = terraformServer . Close ()
}()
go func () {
defer cancel ()
2022-03-22 13:17:50 -06:00
err := terraform . Serve ( ctx , & terraform . ServeOptions {
ServeOptions : & provisionersdk . ServeOptions {
Listener : terraformServer ,
},
2022-12-07 03:05:14 +10:00
CachePath : cacheDir ,
2022-03-28 13:57:19 -06:00
Logger : logger ,
2022-03-22 13:17:50 -06:00
})
2022-06-22 19:11:52 -04:00
if err != nil && ! xerrors . Is ( err , context . Canceled ) {
2022-07-27 18:21:21 +03:00
select {
case errCh <- err :
default :
}
2022-03-22 13:17:50 -06:00
}
}()
2022-03-29 14:59:32 -05:00
tempDir , err := os . MkdirTemp ( "" , "provisionerd" )
2022-03-22 13:17:50 -06:00
if err != nil {
return nil , err
}
2022-03-29 14:59:32 -05:00
2022-05-19 15:47:45 -07:00
provisioners := provisionerd . Provisioners {
2022-11-22 12:19:32 -06:00
string ( database . ProvisionerTypeTerraform ): sdkproto . NewDRPCProvisionerClient ( terraformClient ),
2022-05-19 15:47:45 -07:00
}
// include echo provisioner when in dev mode
if dev {
2022-11-22 12:19:32 -06:00
echoClient , echoServer := provisionersdk . MemTransportPipe ()
2022-05-19 15:47:45 -07:00
go func () {
2022-07-27 18:21:21 +03:00
<- ctx . Done ()
_ = echoClient . Close ()
_ = echoServer . Close ()
}()
go func () {
defer cancel ()
2022-06-16 10:09:22 -05:00
err := echo . Serve ( ctx , afero . NewOsFs (), & provisionersdk . ServeOptions { Listener : echoServer })
2022-05-19 15:47:45 -07:00
if err != nil {
2022-07-27 18:21:21 +03:00
select {
case errCh <- err :
default :
}
2022-05-19 15:47:45 -07:00
}
}()
2022-11-22 12:19:32 -06:00
provisioners [ string ( database . ProvisionerTypeEcho )] = sdkproto . NewDRPCProvisionerClient ( echoClient )
2022-05-19 15:47:45 -07:00
}
2022-12-01 16:54:53 -06:00
debounce := time . Second
2022-11-10 16:37:33 -06:00
return provisionerd . New ( func ( ctx context . Context ) ( proto . DRPCProvisionerDaemonClient , error ) {
// This debounces calls to listen every second. Read the comment
// in provisionerdserver.go to learn more!
2022-12-01 16:54:53 -06:00
return coderAPI . CreateInMemoryProvisionerDaemon ( ctx , debounce )
2022-11-10 16:37:33 -06:00
}, & provisionerd . Options {
2022-11-08 14:19:40 +01:00
Logger : logger ,
2022-12-01 16:54:53 -06:00
JobPollInterval : cfg . Provisioner . DaemonPollInterval . Value ,
JobPollJitter : cfg . Provisioner . DaemonPollJitter . Value ,
JobPollDebounce : debounce ,
2022-11-08 14:19:40 +01:00
UpdateInterval : 500 * time . Millisecond ,
ForceCancelInterval : cfg . Provisioner . ForceCancelInterval . Value ,
Provisioners : provisioners ,
WorkDirectory : tempDir ,
TracerProvider : coderAPI . TracerProvider ,
Metrics : & metrics ,
2022-03-22 13:17:50 -06:00
}), nil
}
2022-03-24 13:21:05 -06:00
2022-04-19 11:40:01 -05:00
// nolint: revive
2022-10-10 15:04:15 -04:00
func printLogo ( cmd * cobra . Command ) {
2023-01-12 20:08:23 -06:00
// Only print the logo in TTYs.
if ! isTTYOut ( cmd ) {
return
}
2022-11-16 20:15:33 +10:00
_ , _ = fmt . Fprintf ( cmd . OutOrStdout (), "%s - Software development on your infrastucture\n" , cliui . Styles . Bold . Render ( "Coder " + buildinfo . Version ()))
2022-03-24 13:21:05 -06:00
}
2022-10-04 21:45:21 +10:00
func loadCertificates ( tlsCertFiles , tlsKeyFiles [] string ) ([] tls . Certificate , error ) {
if len ( tlsCertFiles ) != len ( tlsKeyFiles ) {
return nil , xerrors . New ( "--tls-cert-file and --tls-key-file must be used the same amount of times" )
}
if len ( tlsCertFiles ) == 0 {
return nil , xerrors . New ( "--tls-cert-file is required when tls is enabled" )
}
if len ( tlsKeyFiles ) == 0 {
return nil , xerrors . New ( "--tls-key-file is required when tls is enabled" )
}
certs := make ([] tls . Certificate , len ( tlsCertFiles ))
for i := range tlsCertFiles {
certFile , keyFile := tlsCertFiles [ i ], tlsKeyFiles [ i ]
cert , err := tls . LoadX509KeyPair ( certFile , keyFile )
if err != nil {
return nil , xerrors . Errorf ( "load TLS key pair %d (%q, %q): %w" , i , certFile , keyFile , err )
}
certs [ i ] = cert
}
return certs , nil
}
2022-10-17 08:43:30 -05:00
func configureTLS ( tlsMinVersion , tlsClientAuth string , tlsCertFiles , tlsKeyFiles [] string , tlsClientCAFile string ) ( * tls . Config , error ) {
2022-03-24 18:20:13 -06:00
tlsConfig := & tls . Config {
MinVersion : tls . VersionTLS12 ,
}
2022-03-24 13:21:05 -06:00
switch tlsMinVersion {
case "tls10" :
tlsConfig . MinVersion = tls . VersionTLS10
case "tls11" :
tlsConfig . MinVersion = tls . VersionTLS11
case "tls12" :
tlsConfig . MinVersion = tls . VersionTLS12
case "tls13" :
tlsConfig . MinVersion = tls . VersionTLS13
default :
return nil , xerrors . Errorf ( "unrecognized tls version: %q" , tlsMinVersion )
}
switch tlsClientAuth {
case "none" :
tlsConfig . ClientAuth = tls . NoClientCert
case "request" :
tlsConfig . ClientAuth = tls . RequestClientCert
case "require-any" :
tlsConfig . ClientAuth = tls . RequireAnyClientCert
case "verify-if-given" :
tlsConfig . ClientAuth = tls . VerifyClientCertIfGiven
case "require-and-verify" :
tlsConfig . ClientAuth = tls . RequireAndVerifyClientCert
default :
return nil , xerrors . Errorf ( "unrecognized tls client auth: %q" , tlsClientAuth )
}
2022-10-04 21:45:21 +10:00
certs , err := loadCertificates ( tlsCertFiles , tlsKeyFiles )
if err != nil {
return nil , xerrors . Errorf ( "load certificates: %w" , err )
2022-03-24 13:21:05 -06:00
}
2022-10-17 08:43:30 -05:00
tlsConfig . Certificates = certs
2022-10-04 21:45:21 +10:00
tlsConfig . GetCertificate = func ( hi * tls . ClientHelloInfo ) ( * tls . Certificate , error ) {
// If there's only one certificate, return it.
if len ( certs ) == 1 {
return & certs [ 0 ], nil
}
2022-03-24 13:21:05 -06:00
2022-10-04 21:45:21 +10:00
// Expensively check which certificate matches the client hello.
for _ , cert := range certs {
cert := cert
if err := hi . SupportsCertificate ( & cert ); err == nil {
return & cert , nil
}
}
2022-03-24 13:21:05 -06:00
2022-10-04 21:45:21 +10:00
// Return the first certificate if we have one, or return nil so the
// server doesn't fail.
if len ( certs ) > 0 {
return & certs [ 0 ], nil
}
return nil , nil //nolint:nilnil
}
2022-03-24 13:21:05 -06:00
2022-12-14 09:44:29 -05:00
err = configureCAPool ( tlsClientCAFile , tlsConfig )
if err != nil {
return nil , err
}
return tlsConfig , nil
}
func configureCAPool ( tlsClientCAFile string , tlsConfig * tls . Config ) error {
2022-03-24 13:21:05 -06:00
if tlsClientCAFile != "" {
caPool := x509 . NewCertPool ()
2022-03-29 14:59:32 -05:00
data , err := os . ReadFile ( tlsClientCAFile )
2022-03-24 13:21:05 -06:00
if err != nil {
2022-12-14 09:44:29 -05:00
return xerrors . Errorf ( "read %q: %w" , tlsClientCAFile , err )
2022-03-24 13:21:05 -06:00
}
if ! caPool . AppendCertsFromPEM ( data ) {
2022-12-14 09:44:29 -05:00
return xerrors . Errorf ( "failed to parse CA certificate in tls-client-ca-file" )
2022-03-24 13:21:05 -06:00
}
tlsConfig . ClientCAs = caPool
}
2022-12-14 09:44:29 -05:00
return nil
2022-03-24 13:21:05 -06:00
}
2022-04-18 12:37:01 -05:00
2022-11-15 18:56:46 +02:00
//nolint:revive // Ignore flag-parameter: parameter 'allowEveryone' seems to be a control flag, avoid control coupling (revive)
func configureGithubOAuth2 ( accessURL * url . URL , clientID , clientSecret string , allowSignups , allowEveryone bool , allowOrgs [] string , rawTeams [] string , enterpriseBaseURL string ) ( * coderd . GithubOAuth2Config , error ) {
2022-04-23 17:58:57 -05:00
redirectURL , err := accessURL . Parse ( "/api/v2/users/oauth2/github/callback" )
if err != nil {
return nil , xerrors . Errorf ( "parse github oauth callback url: %w" , err )
}
2022-11-15 18:56:46 +02:00
if allowEveryone && len ( allowOrgs ) > 0 {
return nil , xerrors . New ( "allow everyone and allowed orgs cannot be used together" )
}
if allowEveryone && len ( rawTeams ) > 0 {
return nil , xerrors . New ( "allow everyone and allowed teams cannot be used together" )
}
if ! allowEveryone && len ( allowOrgs ) == 0 {
return nil , xerrors . New ( "allowed orgs is empty: must specify at least one org or allow everyone" )
}
2022-07-08 21:37:18 -05:00
allowTeams := make ([] coderd . GithubOAuth2Team , 0 , len ( rawTeams ))
for _ , rawTeam := range rawTeams {
parts := strings . SplitN ( rawTeam , "/" , 2 )
if len ( parts ) != 2 {
return nil , xerrors . Errorf ( "github team allowlist is formatted incorrectly. got %s; wanted <organization>/<team>" , rawTeam )
}
allowTeams = append ( allowTeams , coderd . GithubOAuth2Team {
Organization : parts [ 0 ],
Slug : parts [ 1 ],
})
}
2022-08-08 20:49:51 -05:00
createClient := func ( client * http . Client ) ( * github . Client , error ) {
if enterpriseBaseURL != "" {
return github . NewEnterpriseClient ( enterpriseBaseURL , "" , client )
}
return github . NewClient ( client ), nil
}
endpoint := xgithub . Endpoint
if enterpriseBaseURL != "" {
enterpriseURL , err := url . Parse ( enterpriseBaseURL )
if err != nil {
return nil , xerrors . Errorf ( "parse enterprise base url: %w" , err )
}
authURL , err := enterpriseURL . Parse ( "/login/oauth/authorize" )
if err != nil {
return nil , xerrors . Errorf ( "parse enterprise auth url: %w" , err )
}
tokenURL , err := enterpriseURL . Parse ( "/login/oauth/access_token" )
if err != nil {
return nil , xerrors . Errorf ( "parse enterprise token url: %w" , err )
}
endpoint = oauth2 . Endpoint {
AuthURL : authURL . String (),
TokenURL : tokenURL . String (),
}
}
2022-04-23 17:58:57 -05:00
return & coderd . GithubOAuth2Config {
OAuth2Config : & oauth2 . Config {
ClientID : clientID ,
ClientSecret : clientSecret ,
2022-08-08 20:49:51 -05:00
Endpoint : endpoint ,
2022-04-23 17:58:57 -05:00
RedirectURL : redirectURL . String (),
Scopes : [] string {
"read:user" ,
"read:org" ,
"user:email" ,
},
},
AllowSignups : allowSignups ,
2022-11-15 18:56:46 +02:00
AllowEveryone : allowEveryone ,
2022-04-23 17:58:57 -05:00
AllowOrganizations : allowOrgs ,
2022-07-08 21:37:18 -05:00
AllowTeams : allowTeams ,
2022-04-23 17:58:57 -05:00
AuthenticatedUser : func ( ctx context . Context , client * http . Client ) ( * github . User , error ) {
2022-08-08 20:49:51 -05:00
api , err := createClient ( client )
if err != nil {
return nil , err
}
user , _ , err := api . Users . Get ( ctx , "" )
2022-04-23 17:58:57 -05:00
return user , err
},
ListEmails : func ( ctx context . Context , client * http . Client ) ([] * github . UserEmail , error ) {
2022-08-08 20:49:51 -05:00
api , err := createClient ( client )
if err != nil {
return nil , err
}
emails , _ , err := api . Users . ListEmails ( ctx , & github . ListOptions {})
2022-04-23 17:58:57 -05:00
return emails , err
},
ListOrganizationMemberships : func ( ctx context . Context , client * http . Client ) ([] * github . Membership , error ) {
2022-08-08 20:49:51 -05:00
api , err := createClient ( client )
if err != nil {
return nil , err
}
memberships , _ , err := api . Organizations . ListOrgMemberships ( ctx , & github . ListOrgMembershipsOptions {
2022-04-23 17:58:57 -05:00
State : "active" ,
2022-07-08 21:37:18 -05:00
ListOptions : github . ListOptions {
PerPage : 100 ,
},
2022-04-23 17:58:57 -05:00
})
return memberships , err
},
2022-07-22 13:54:08 -05:00
TeamMembership : func ( ctx context . Context , client * http . Client , org , teamSlug , username string ) ( * github . Membership , error ) {
2022-08-08 20:49:51 -05:00
api , err := createClient ( client )
if err != nil {
return nil , err
}
team , _ , err := api . Teams . GetTeamMembershipBySlug ( ctx , org , teamSlug , username )
2022-07-12 19:45:43 -05:00
return team , err
2022-07-08 21:37:18 -05:00
},
2022-04-23 17:58:57 -05:00
}, nil
}
2022-05-03 07:48:02 -05:00
func serveHandler ( ctx context . Context , logger slog . Logger , handler http . Handler , addr , name string ) ( closeFunc func ()) {
logger . Debug ( ctx , "http server listening" , slog . F ( "addr" , addr ), slog . F ( "name" , name ))
2022-08-29 12:07:49 -05:00
// ReadHeaderTimeout is purposefully not enabled. It caused some issues with
// websockets over the dev tunnel.
// See: https://github.com/coder/coder/pull/3730
//nolint:gosec
2022-08-21 17:32:53 -05:00
srv := & http . Server {
2022-08-29 12:07:49 -05:00
Addr : addr ,
Handler : handler ,
2022-08-21 17:32:53 -05:00
}
2022-05-03 07:48:02 -05:00
go func () {
err := srv . ListenAndServe ()
if err != nil && ! xerrors . Is ( err , http . ErrServerClosed ) {
logger . Error ( ctx , "http server listen" , slog . F ( "name" , name ), slog . Error ( err ))
}
}()
2022-10-21 08:09:44 +10:00
return func () {
_ = srv . Close ()
}
2022-05-03 07:48:02 -05:00
}
2022-06-10 13:35:51 -05:00
2022-06-15 16:02:18 -05:00
// embeddedPostgresURL returns the URL for the embedded PostgreSQL deployment.
func embeddedPostgresURL ( cfg config . Root ) ( string , error ) {
pgPassword , err := cfg . PostgresPassword (). Read ()
if errors . Is ( err , os . ErrNotExist ) {
pgPassword , err = cryptorand . String ( 16 )
if err != nil {
return "" , xerrors . Errorf ( "generate password: %w" , err )
}
err = cfg . PostgresPassword (). Write ( pgPassword )
if err != nil {
return "" , xerrors . Errorf ( "write password: %w" , err )
}
}
if err != nil && ! errors . Is ( err , os . ErrNotExist ) {
return "" , err
}
pgPort , err := cfg . PostgresPort (). Read ()
if errors . Is ( err , os . ErrNotExist ) {
listener , err := net . Listen ( "tcp4" , "127.0.0.1:0" )
if err != nil {
return "" , xerrors . Errorf ( "listen for random port: %w" , err )
}
_ = listener . Close ()
tcpAddr , valid := listener . Addr ().( * net . TCPAddr )
if ! valid {
return "" , xerrors . Errorf ( "listener returned non TCP addr: %T" , tcpAddr )
}
pgPort = strconv . Itoa ( tcpAddr . Port )
err = cfg . PostgresPort (). Write ( pgPort )
if err != nil {
return "" , xerrors . Errorf ( "write postgres port: %w" , err )
}
}
return fmt . Sprintf ( "postgres://coder@localhost:%s/coder?sslmode=disable&password=%s" , pgPort , pgPassword ), nil
}
func startBuiltinPostgres ( ctx context . Context , cfg config . Root , logger slog . Logger ) ( string , func () error , error ) {
usr , err := user . Current ()
if err != nil {
return "" , nil , err
}
if usr . Uid == "0" {
return "" , nil , xerrors . New ( "The built-in PostgreSQL cannot run as the root user. Create a non-root user and run again!" )
}
// Ensure a password and port have been generated!
connectionURL , err := embeddedPostgresURL ( cfg )
if err != nil {
return "" , nil , err
}
pgPassword , err := cfg . PostgresPassword (). Read ()
if err != nil {
return "" , nil , xerrors . Errorf ( "read postgres password: %w" , err )
}
pgPortRaw , err := cfg . PostgresPort (). Read ()
if err != nil {
return "" , nil , xerrors . Errorf ( "read postgres port: %w" , err )
}
2022-12-20 05:25:59 +10:00
pgPort , err := strconv . ParseUint ( pgPortRaw , 10 , 16 )
2022-06-15 16:02:18 -05:00
if err != nil {
return "" , nil , xerrors . Errorf ( "parse postgres port: %w" , err )
}
stdlibLogger := slog . Stdlib ( ctx , logger . Named ( "postgres" ), slog . LevelDebug )
ep := embeddedpostgres . NewDatabase (
embeddedpostgres . DefaultConfig ().
2022-08-10 10:08:24 -05:00
Version ( embeddedpostgres . V13 ).
2022-06-15 16:02:18 -05:00
BinariesPath ( filepath . Join ( cfg . PostgresPath (), "bin" )).
DataPath ( filepath . Join ( cfg . PostgresPath (), "data" )).
RuntimePath ( filepath . Join ( cfg . PostgresPath (), "runtime" )).
CachePath ( filepath . Join ( cfg . PostgresPath (), "cache" )).
Username ( "coder" ).
Password ( pgPassword ).
Database ( "coder" ).
Port ( uint32 ( pgPort )).
Logger ( stdlibLogger . Writer ()),
)
err = ep . Start ()
if err != nil {
return "" , nil , xerrors . Errorf ( "Failed to start built-in PostgreSQL. Optionally, specify an external deployment with `--postgres-url`: %w" , err )
}
return connectionURL , ep . Stop , nil
}
2022-11-13 15:15:06 -05:00
2022-12-14 09:44:29 -05:00
func configureHTTPClient ( ctx context . Context , clientCertFile , clientKeyFile string , tlsClientCAFile string ) ( context . Context , * http . Client , error ) {
if clientCertFile != "" && clientKeyFile != "" {
certificates , err := loadCertificates ([] string { clientCertFile }, [] string { clientKeyFile })
2022-11-13 15:15:06 -05:00
if err != nil {
2022-12-14 09:44:29 -05:00
return ctx , nil , err
2022-11-13 15:15:06 -05:00
}
2022-12-14 09:44:29 -05:00
tlsClientConfig := & tls . Config { //nolint:gosec
Certificates : certificates ,
}
err = configureCAPool ( tlsClientCAFile , tlsClientConfig )
if err != nil {
return nil , nil , err
}
httpClient := & http . Client {
2022-11-13 15:15:06 -05:00
Transport : & http . Transport {
2022-12-14 09:44:29 -05:00
TLSClientConfig : tlsClientConfig ,
2022-11-13 15:15:06 -05:00
},
2022-12-14 09:44:29 -05:00
}
return context . WithValue ( ctx , oauth2 . HTTPClient , httpClient ), httpClient , nil
2022-11-13 15:15:06 -05:00
}
2022-12-14 09:44:29 -05:00
return ctx , & http . Client {}, nil
2022-11-13 15:15:06 -05:00
}
2022-12-16 06:09:19 +10:00
func redirectHTTPToAccessURL ( handler http . Handler , accessURL * url . URL ) http . Handler {
return http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
if r . TLS == nil {
http . Redirect ( w , r , accessURL . String (), http . StatusTemporaryRedirect )
return
}
handler . ServeHTTP ( w , r )
})
}
2022-12-20 05:25:59 +10:00
// isLocalhost returns true if the host points to the local machine. Intended to
// be called with `u.Hostname()`.
func isLocalhost ( host string ) bool {
return host == "localhost" || host == "127.0.0.1" || host == "::1"
}
2023-01-12 20:08:23 -06:00
func buildLogger ( cmd * cobra . Command , cfg * codersdk . DeploymentConfig ) ( slog . Logger , func (), error ) {
var (
sinks = [] slog . Sink {}
closers = [] func () error {}
)
addSinkIfProvided := func ( sinkFn func ( io . Writer ) slog . Sink , loc string ) error {
switch loc {
case "" :
case "/dev/stdout" :
sinks = append ( sinks , sinkFn ( cmd . OutOrStdout ()))
case "/dev/stderr" :
sinks = append ( sinks , sinkFn ( cmd . ErrOrStderr ()))
default :
fi , err := os . OpenFile ( loc , os . O_WRONLY | os . O_CREATE | os . O_APPEND , 0644 )
if err != nil {
return xerrors . Errorf ( "open log file %q: %w" , loc , err )
}
closers = append ( closers , fi . Close )
sinks = append ( sinks , sinkFn ( fi ))
}
return nil
}
err := addSinkIfProvided ( sloghuman . Sink , cfg . Logging . Human . Value )
if err != nil {
return slog . Logger {}, nil , xerrors . Errorf ( "add human sink: %w" , err )
}
err = addSinkIfProvided ( slogjson . Sink , cfg . Logging . JSON . Value )
if err != nil {
return slog . Logger {}, nil , xerrors . Errorf ( "add json sink: %w" , err )
}
err = addSinkIfProvided ( slogstackdriver . Sink , cfg . Logging . Stackdriver . Value )
if err != nil {
return slog . Logger {}, nil , xerrors . Errorf ( "add stackdriver sink: %w" , err )
}
if cfg . Trace . CaptureLogs . Value {
sinks = append ( sinks , tracing . SlogSink {})
}
level := slog . LevelInfo
if ok , _ := cmd . Flags (). GetBool ( varVerbose ); ok {
level = slog . LevelDebug
}
if len ( sinks ) == 0 {
return slog . Logger {}, nil , xerrors . New ( "no loggers provided" )
}
return slog . Make ( sinks ... ). Leveled ( level ), func () {
for _ , closer := range closers {
_ = closer ()
}
}, nil
}