Merge branch '16265-security-updates' into dependabot/bundler/apps/workbench/loofah...
[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         "os"
13         "os/exec"
14         "path/filepath"
15         "regexp"
16
17         "git.arvados.org/arvados.git/sdk/go/arvados"
18 )
19
20 // Run an Nginx process that proxies the supervisor's configured
21 // ExternalURLs to the appropriate InternalURLs.
22 type runNginx struct{}
23
24 func (runNginx) String() string {
25         return "nginx"
26 }
27
28 func (runNginx) Run(ctx context.Context, fail func(error), super *Supervisor) error {
29         vars := map[string]string{
30                 "LISTENHOST": super.ListenHost,
31                 "SSLCERT":    filepath.Join(super.SourcePath, "services", "api", "tmp", "self-signed.pem"), // TODO: root ca
32                 "SSLKEY":     filepath.Join(super.SourcePath, "services", "api", "tmp", "self-signed.key"), // TODO: root ca
33                 "ACCESSLOG":  filepath.Join(super.tempdir, "nginx_access.log"),
34                 "ERRORLOG":   filepath.Join(super.tempdir, "nginx_error.log"),
35                 "TMPDIR":     super.tempdir,
36         }
37         var err error
38         for _, cmpt := range []struct {
39                 varname string
40                 svc     arvados.Service
41         }{
42                 {"CONTROLLER", super.cluster.Services.Controller},
43                 {"KEEPWEB", super.cluster.Services.WebDAV},
44                 {"KEEPWEBDL", super.cluster.Services.WebDAVDownload},
45                 {"KEEPPROXY", super.cluster.Services.Keepproxy},
46                 {"GIT", super.cluster.Services.GitHTTP},
47                 {"WORKBENCH1", super.cluster.Services.Workbench1},
48                 {"WS", super.cluster.Services.Websocket},
49         } {
50                 port, err := internalPort(cmpt.svc)
51                 if err != nil {
52                         return fmt.Errorf("%s internal port: %s (%v)", cmpt.varname, err, cmpt.svc)
53                 }
54                 if ok, err := addrIsLocal(net.JoinHostPort(super.ListenHost, port)); !ok || err != nil {
55                         return fmt.Errorf("urlIsLocal() failed for host %q port %q: %v", super.ListenHost, port, err)
56                 }
57                 vars[cmpt.varname+"PORT"] = port
58
59                 port, err = externalPort(cmpt.svc)
60                 if err != nil {
61                         return fmt.Errorf("%s external port: %s (%v)", cmpt.varname, err, cmpt.svc)
62                 }
63                 if ok, err := addrIsLocal(net.JoinHostPort(super.ListenHost, port)); !ok || err != nil {
64                         return fmt.Errorf("urlIsLocal() failed for host %q port %q: %v", super.ListenHost, port, err)
65                 }
66                 vars[cmpt.varname+"SSLPORT"] = port
67         }
68         tmpl, err := ioutil.ReadFile(filepath.Join(super.SourcePath, "sdk", "python", "tests", "nginx.conf"))
69         if err != nil {
70                 return err
71         }
72         conf := regexp.MustCompile(`{{.*?}}`).ReplaceAllStringFunc(string(tmpl), func(src string) string {
73                 if len(src) < 4 {
74                         return src
75                 }
76                 return vars[src[2:len(src)-2]]
77         })
78         conffile := filepath.Join(super.tempdir, "nginx.conf")
79         err = ioutil.WriteFile(conffile, []byte(conf), 0755)
80         if err != nil {
81                 return err
82         }
83         nginx := "nginx"
84         if _, err := exec.LookPath(nginx); err != nil {
85                 for _, dir := range []string{"/sbin", "/usr/sbin", "/usr/local/sbin"} {
86                         if _, err = os.Stat(dir + "/nginx"); err == nil {
87                                 nginx = dir + "/nginx"
88                                 break
89                         }
90                 }
91         }
92         super.waitShutdown.Add(1)
93         go func() {
94                 defer super.waitShutdown.Done()
95                 fail(super.RunProgram(ctx, ".", nil, nil, nginx,
96                         "-g", "error_log stderr info;",
97                         "-g", "pid "+filepath.Join(super.tempdir, "nginx.pid")+";",
98                         "-c", conffile))
99         }()
100         return waitForConnect(ctx, super.cluster.Services.Controller.ExternalURL.Host)
101 }