X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/7a24a37aa9e5ed425550403b68c270316a24d772..60a72f91316ab1d878420adceb98e585c0e3b72f:/lib/boot/nginx.go diff --git a/lib/boot/nginx.go b/lib/boot/nginx.go index b5b712af68..d14d051520 100644 --- a/lib/boot/nginx.go +++ b/lib/boot/nginx.go @@ -8,50 +8,76 @@ import ( "context" "fmt" "io/ioutil" + "net" + "net/url" "os" "os/exec" + "os/user" "path/filepath" "regexp" "git.arvados.org/arvados.git/sdk/go/arvados" ) +// Run an Nginx process that proxies the supervisor's configured +// ExternalURLs to the appropriate InternalURLs. type runNginx struct{} func (runNginx) String() string { return "nginx" } -func (runNginx) Run(ctx context.Context, fail func(error), boot *Booter) error { +func (runNginx) Run(ctx context.Context, fail func(error), super *Supervisor) error { + err := super.wait(ctx, createCertificates{}) + if err != nil { + return err + } vars := map[string]string{ - "SSLCERT": filepath.Join(boot.SourcePath, "services", "api", "tmp", "self-signed.pem"), // TODO: root ca - "SSLKEY": filepath.Join(boot.SourcePath, "services", "api", "tmp", "self-signed.key"), // TODO: root ca - "ACCESSLOG": filepath.Join(boot.tempdir, "nginx_access.log"), - "ERRORLOG": filepath.Join(boot.tempdir, "nginx_error.log"), - "TMPDIR": boot.tempdir, + "LISTENHOST": super.ListenHost, + "SSLCERT": filepath.Join(super.tempdir, "server.crt"), + "SSLKEY": filepath.Join(super.tempdir, "server.key"), + "ACCESSLOG": filepath.Join(super.tempdir, "nginx_access.log"), + "ERRORLOG": filepath.Join(super.tempdir, "nginx_error.log"), + "TMPDIR": super.wwwtempdir, } - var err error for _, cmpt := range []struct { varname string svc arvados.Service }{ - {"CONTROLLER", boot.cluster.Services.Controller}, - {"KEEPWEB", boot.cluster.Services.WebDAV}, - {"KEEPWEBDL", boot.cluster.Services.WebDAVDownload}, - {"KEEPPROXY", boot.cluster.Services.Keepproxy}, - {"GIT", boot.cluster.Services.GitHTTP}, - {"WS", boot.cluster.Services.Websocket}, + {"CONTROLLER", super.cluster.Services.Controller}, + {"KEEPWEB", super.cluster.Services.WebDAV}, + {"KEEPWEBDL", super.cluster.Services.WebDAVDownload}, + {"KEEPPROXY", super.cluster.Services.Keepproxy}, + {"GIT", super.cluster.Services.GitHTTP}, + {"HEALTH", super.cluster.Services.Health}, + {"WORKBENCH1", super.cluster.Services.Workbench1}, + {"WS", super.cluster.Services.Websocket}, } { - vars[cmpt.varname+"PORT"], err = internalPort(cmpt.svc) + port, err := internalPort(cmpt.svc) if err != nil { - return fmt.Errorf("%s internal port: %s (%v)", cmpt.varname, err, cmpt.svc) + return fmt.Errorf("%s internal port: %w (%v)", cmpt.varname, err, cmpt.svc) } - vars[cmpt.varname+"SSLPORT"], err = externalPort(cmpt.svc) + if ok, err := addrIsLocal(net.JoinHostPort(super.ListenHost, port)); !ok || err != nil { + return fmt.Errorf("urlIsLocal() failed for host %q port %q: %v", super.ListenHost, port, err) + } + vars[cmpt.varname+"PORT"] = port + + port, err = externalPort(cmpt.svc) if err != nil { - return fmt.Errorf("%s external port: %s (%v)", cmpt.varname, err, cmpt.svc) + return fmt.Errorf("%s external port: %w (%v)", cmpt.varname, err, cmpt.svc) + } + if ok, err := addrIsLocal(net.JoinHostPort(super.ListenHost, port)); !ok || err != nil { + return fmt.Errorf("urlIsLocal() failed for host %q port %q: %v", super.ListenHost, port, err) } + vars[cmpt.varname+"SSLPORT"] = port } - tmpl, err := ioutil.ReadFile(filepath.Join(boot.SourcePath, "sdk", "python", "tests", "nginx.conf")) + var conftemplate string + if super.ClusterType == "production" { + conftemplate = "/var/lib/arvados/share/nginx.conf" + } else { + conftemplate = filepath.Join(super.SourcePath, "sdk", "python", "tests", "nginx.conf") + } + tmpl, err := ioutil.ReadFile(conftemplate) if err != nil { return err } @@ -61,7 +87,7 @@ func (runNginx) Run(ctx context.Context, fail func(error), boot *Booter) error { } return vars[src[2:len(src)-2]] }) - conffile := filepath.Join(boot.tempdir, "nginx.conf") + conffile := filepath.Join(super.tempdir, "nginx.conf") err = ioutil.WriteFile(conffile, []byte(conf), 0755) if err != nil { return err @@ -75,11 +101,32 @@ func (runNginx) Run(ctx context.Context, fail func(error), boot *Booter) error { } } } + + args := []string{ + "-g", "error_log stderr info;", + "-g", "pid " + filepath.Join(super.wwwtempdir, "nginx.pid") + ";", + "-c", conffile, + } + // Nginx ignores "user www-data;" when running as a non-root + // user... except that it causes it to ignore our other -g + // options. So we still have to decide for ourselves whether + // it's needed. + if u, err := user.Current(); err != nil { + return fmt.Errorf("user.Current(): %w", err) + } else if u.Uid == "0" { + args = append([]string{"-g", "user www-data;"}, args...) + } + + super.waitShutdown.Add(1) go func() { - fail(boot.RunProgram(ctx, ".", nil, nil, nginx, - "-g", "error_log stderr info;", - "-g", "pid "+filepath.Join(boot.tempdir, "nginx.pid")+";", - "-c", conffile)) + defer super.waitShutdown.Done() + fail(super.RunProgram(ctx, ".", runOptions{}, nginx, args...)) }() - return waitForConnect(ctx, boot.cluster.Services.Controller.ExternalURL.Host) + // Choose one of the ports where Nginx should listen, and wait + // here until we can connect. If ExternalURL is https://foo (with no port) then we connect to "foo:https" + testurl := url.URL(super.cluster.Services.Controller.ExternalURL) + if testurl.Port() == "" { + testurl.Host = net.JoinHostPort(testurl.Host, testurl.Scheme) + } + return waitForConnect(ctx, testurl.Host) }