X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/9d706659aa362728270f02b7d18d12b1f68f3e36..HEAD:/lib/boot/nginx.go diff --git a/lib/boot/nginx.go b/lib/boot/nginx.go index e67bc1d900..338a6b5bcc 100644 --- a/lib/boot/nginx.go +++ b/lib/boot/nginx.go @@ -5,6 +5,7 @@ package boot import ( + "bytes" "context" "fmt" "io/ioutil" @@ -14,8 +15,10 @@ import ( "os/exec" "path/filepath" "regexp" + "strings" "git.arvados.org/arvados.git/sdk/go/arvados" + "github.com/sirupsen/logrus" ) // Run an Nginx process that proxies the supervisor's configured @@ -31,8 +34,21 @@ func (runNginx) Run(ctx context.Context, fail func(error), super *Supervisor) er if err != nil { return err } + extListenHost := "0.0.0.0" + if super.ClusterType == "test" { + // Our dynamic port number assignment strategy (choose + // an available port, write it in a config file, and + // have another process/goroutine bind to it) is prone + // to races when used by concurrent supervisors. In + // test mode we don't accept remote connections, so we + // can avoid collisions by using the per-cluster + // loopback address instead of 0.0.0.0. + extListenHost = super.ListenHost + } vars := map[string]string{ - "LISTENHOST": super.ListenHost, + "LISTENHOST": extListenHost, + "UPSTREAMHOST": super.ListenHost, + "INTERNALSUBNETS": internalSubnets(super.logger), "SSLCERT": filepath.Join(super.tempdir, "server.crt"), "SSLKEY": filepath.Join(super.tempdir, "server.key"), "ACCESSLOG": filepath.Join(super.tempdir, "nginx_access.log"), @@ -42,7 +58,10 @@ func (runNginx) Run(ctx context.Context, fail func(error), super *Supervisor) er } u := url.URL(super.cluster.Services.Controller.ExternalURL) ctrlHost := u.Hostname() - if f, err := os.Open("/var/lib/acme/live/" + ctrlHost + "/privkey"); err == nil { + if strings.HasPrefix(super.cluster.TLS.Certificate, "file:/") && strings.HasPrefix(super.cluster.TLS.Key, "file:/") { + vars["SSLCERT"] = filepath.Clean(super.cluster.TLS.Certificate[5:]) + vars["SSLKEY"] = filepath.Clean(super.cluster.TLS.Key[5:]) + } else if f, err := os.Open("/var/lib/acme/live/" + ctrlHost + "/privkey"); err == nil { f.Close() vars["SSLCERT"] = "/var/lib/acme/live/" + ctrlHost + "/cert" vars["SSLKEY"] = "/var/lib/acme/live/" + ctrlHost + "/privkey" @@ -55,7 +74,6 @@ func (runNginx) Run(ctx context.Context, fail func(error), super *Supervisor) er {"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}, {"WORKBENCH2", super.cluster.Services.Workbench2}, @@ -116,7 +134,7 @@ func (runNginx) Run(ctx context.Context, fail func(error), super *Supervisor) er } } - configs := "error_log stderr info; " + configs := "error_log stderr warn; " configs += "pid " + filepath.Join(super.wwwtempdir, "nginx.pid") + "; " configs += "user www-data; " @@ -134,3 +152,27 @@ func (runNginx) Run(ctx context.Context, fail func(error), super *Supervisor) er } return waitForConnect(ctx, testurl.Host) } + +// Return 0 or more local subnets as "geo" fragments for Nginx config, +// e.g., "1.2.3.0/24 0; 10.1.0.0/16 0;". +func internalSubnets(logger logrus.FieldLogger) string { + iproutes, err := exec.Command("ip", "route").CombinedOutput() + if err != nil { + logger.Warnf("treating all clients as external because `ip route` failed: %s (%q)", err, iproutes) + return "" + } + subnets := "" + for _, line := range bytes.Split(iproutes, []byte("\n")) { + fields := strings.Fields(string(line)) + if len(fields) > 2 && fields[1] == "dev" { + // lan example: + // 192.168.86.0/24 dev ens3 proto kernel scope link src 192.168.86.196 + // gcp example (private subnet): + // 10.47.0.0/24 dev eth0 proto kernel scope link src 10.47.0.5 + // gcp example (no private subnet): + // 10.128.0.1 dev ens4 scope link + subnets += fields[0] + " 0; " + } + } + return subnets +}