16306: Make sure that the non-passenger nginx process runs as the
[arvados.git] / lib / boot / nginx.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package boot
6
7 import (
8         "context"
9         "fmt"
10         "io/ioutil"
11         "net"
12         "net/url"
13         "os"
14         "os/exec"
15         "path/filepath"
16         "regexp"
17
18         "git.arvados.org/arvados.git/sdk/go/arvados"
19 )
20
21 // Run an Nginx process that proxies the supervisor's configured
22 // ExternalURLs to the appropriate InternalURLs.
23 type runNginx struct{}
24
25 func (runNginx) String() string {
26         return "nginx"
27 }
28
29 func (runNginx) Run(ctx context.Context, fail func(error), super *Supervisor) error {
30         err := super.wait(ctx, createCertificates{})
31         if err != nil {
32                 return err
33         }
34         vars := map[string]string{
35                 "LISTENHOST": super.ListenHost,
36                 "SSLCERT":    filepath.Join(super.tempdir, "server.crt"),
37                 "SSLKEY":     filepath.Join(super.tempdir, "server.key"),
38                 "ACCESSLOG":  filepath.Join(super.tempdir, "nginx_access.log"),
39                 "ERRORLOG":   filepath.Join(super.tempdir, "nginx_error.log"),
40                 "TMPDIR":     super.wwwtempdir,
41         }
42         for _, cmpt := range []struct {
43                 varname string
44                 svc     arvados.Service
45         }{
46                 {"CONTROLLER", super.cluster.Services.Controller},
47                 {"KEEPWEB", super.cluster.Services.WebDAV},
48                 {"KEEPWEBDL", super.cluster.Services.WebDAVDownload},
49                 {"KEEPPROXY", super.cluster.Services.Keepproxy},
50                 {"GIT", super.cluster.Services.GitHTTP},
51                 {"HEALTH", super.cluster.Services.Health},
52                 {"WORKBENCH1", super.cluster.Services.Workbench1},
53                 {"WS", super.cluster.Services.Websocket},
54         } {
55                 port, err := internalPort(cmpt.svc)
56                 if err != nil {
57                         return fmt.Errorf("%s internal port: %w (%v)", cmpt.varname, err, cmpt.svc)
58                 }
59                 if ok, err := addrIsLocal(net.JoinHostPort(super.ListenHost, port)); !ok || err != nil {
60                         return fmt.Errorf("urlIsLocal() failed for host %q port %q: %v", super.ListenHost, port, err)
61                 }
62                 vars[cmpt.varname+"PORT"] = port
63
64                 port, err = externalPort(cmpt.svc)
65                 if err != nil {
66                         return fmt.Errorf("%s external port: %w (%v)", cmpt.varname, err, cmpt.svc)
67                 }
68                 if ok, err := addrIsLocal(net.JoinHostPort(super.ListenHost, port)); !ok || err != nil {
69                         return fmt.Errorf("urlIsLocal() failed for host %q port %q: %v", super.ListenHost, port, err)
70                 }
71                 vars[cmpt.varname+"SSLPORT"] = port
72         }
73         var conftemplate string
74         if super.ClusterType == "production" {
75                 conftemplate = "/var/lib/arvados/share/nginx.conf"
76         } else {
77                 conftemplate = filepath.Join(super.SourcePath, "sdk", "python", "tests", "nginx.conf")
78         }
79         tmpl, err := ioutil.ReadFile(conftemplate)
80         if err != nil {
81                 return err
82         }
83         conf := regexp.MustCompile(`{{.*?}}`).ReplaceAllStringFunc(string(tmpl), func(src string) string {
84                 if len(src) < 4 {
85                         return src
86                 }
87                 return vars[src[2:len(src)-2]]
88         })
89         conffile := filepath.Join(super.tempdir, "nginx.conf")
90         err = ioutil.WriteFile(conffile, []byte(conf), 0755)
91         if err != nil {
92                 return err
93         }
94         nginx := "nginx"
95         if _, err := exec.LookPath(nginx); err != nil {
96                 for _, dir := range []string{"/sbin", "/usr/sbin", "/usr/local/sbin"} {
97                         if _, err = os.Stat(dir + "/nginx"); err == nil {
98                                 nginx = dir + "/nginx"
99                                 break
100                         }
101                 }
102         }
103         super.waitShutdown.Add(1)
104         go func() {
105                 defer super.waitShutdown.Done()
106                 fail(super.RunProgram(ctx, ".", nil, nil, nginx,
107                         "-g", "error_log stderr info;",
108                         "-g", "user www-data; pid "+filepath.Join(super.wwwtempdir, "nginx.pid")+";",
109                         "-c", conffile))
110         }()
111         // Choose one of the ports where Nginx should listen, and wait
112         // here until we can connect. If ExternalURL is https://foo (with no port) then we connect to "foo:https"
113         testurl := url.URL(super.cluster.Services.Controller.ExternalURL)
114         if testurl.Port() == "" {
115                 testurl.Host = net.JoinHostPort(testurl.Host, testurl.Scheme)
116         }
117         return waitForConnect(ctx, testurl.Host)
118 }