16306: Fix Nginx not starting when running as non-root.
authorTom Clegg <tom@curii.com>
Tue, 22 Dec 2020 15:19:43 +0000 (10:19 -0500)
committerTom Clegg <tom@curii.com>
Tue, 22 Dec 2020 15:19:43 +0000 (10:19 -0500)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

lib/boot/nginx.go
lib/boot/postgresql.go

index 07ff1fc1d3e330d89c0f69649ed996f2ca5f7866..d49fabe5673d8b7fe78b0e0875a34bcf94658ccf 100644 (file)
@@ -12,6 +12,7 @@ import (
        "net/url"
        "os"
        "os/exec"
+       "os/user"
        "path/filepath"
        "regexp"
 
@@ -100,13 +101,26 @@ func (runNginx) Run(ctx context.Context, fail func(error), super *Supervisor) er
                        }
                }
        }
+
+       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() {
                defer super.waitShutdown.Done()
-               fail(super.RunProgram(ctx, ".", nil, nil, nginx,
-                       "-g", "error_log stderr info;",
-                       "-g", "user www-data; pid "+filepath.Join(super.wwwtempdir, "nginx.pid")+";",
-                       "-c", conffile))
+               fail(super.RunProgram(ctx, ".", nil, nil, nginx, args...))
        }()
        // 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"
index 199a93a9d5f0724df73b47b91d73e6e3a2409a72..fc23eb91320c4c8817f35c90503bfa9cd648711e 100644 (file)
@@ -42,7 +42,7 @@ func (runPostgreSQL) Run(ctx context.Context, fail func(error), super *Superviso
 
        iamroot := false
        if u, err := user.Current(); err != nil {
-               return fmt.Errorf("user.Current(): %s", err)
+               return fmt.Errorf("user.Current(): %w", err)
        } else if u.Uid == "0" {
                iamroot = true
        }