diff --git a/CHANGELOG.md b/CHANGELOG.md index 334149e5..01cdb7e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -108,6 +108,12 @@ APIs change between minors without a deprecation period. one itself. Its listener binds the address the codes name rather than every interface, and a joined machine is known by the address it connected from, not the one it claimed. +- **The setup's sudo grant cannot outlive it.** Given a sudo password, the + setup writes a temporary `NOPASSWD` grant and removes it when it + finishes; a setup that crashed or was interrupted left it for good. The + grant now schedules its own removal within the hour as it is written, and + the password is dropped once the grant stands in for it. An address given + to the system ssh can no longer be read as one of its options. ### Everything that can be changed on a running cluster, from the console diff --git a/internal/onboard/gossh.go b/internal/onboard/gossh.go index cc95a405..4eb94491 100644 --- a/internal/onboard/gossh.go +++ b/internal/onboard/gossh.go @@ -227,6 +227,9 @@ func (g *GoSSH) hostKeyCallback() (ssh.HostKeyCallback, error) { // sudoersDrop is the grant PrepareSudo writes and RevokeSudo removes. const sudoersDrop = "/etc/sudoers.d/llmgw-setup" +// sudoGrantLife is how long the grant lasts if nothing removes it sooner. +const sudoGrantLife = time.Hour + // CanSudo reports whether the user can sudo without a password now. func (g *GoSSH) CanSudo(ctx context.Context) bool { _, err := g.Run(ctx, "sudo -n true", nil) @@ -245,17 +248,14 @@ func (g *GoSSH) PrepareSudo(ctx context.Context) error { if g.SudoPassword == "" { return fmt.Errorf("%s cannot use sudo without a password, and none was given", g.User) } - line := fmt.Sprintf("%s ALL=(ALL) NOPASSWD:ALL", g.User) - // Checked by visudo before it is put in place: a broken file in - // sudoers.d breaks sudo for everyone on the machine. - inner := fmt.Sprintf("umask 337; printf '%%s\\n' %s %s > %[3]s.new && visudo -cf %[3]s.new >/dev/null && mv -f %[3]s.new %[3]s", - shellQuote("# llmgw setup: removed when it finishes"), shellQuote(line), sudoersDrop) - script := "sudo -S -p '' sh -c " + shellQuote(inner) + script := "sudo -S -p '' sh -c " + shellQuote(sudoGrantScript(g.User)) if out, err := g.Run(ctx, script, []byte(g.SudoPassword+"\n")); err != nil { return fmt.Errorf("sudo with the password given failed on %s: %v: %s", g.Addr, err, strings.TrimSpace(string(out))) } g.mu.Lock() g.sudoGrant = true + // Not needed again: the grant stands in for it from here on. + g.SudoPassword = "" g.mu.Unlock() return nil } @@ -276,3 +276,21 @@ func (g *GoSSH) RevokeSudo(ctx context.Context) error { g.mu.Unlock() return nil } + +// sudoGrantScript writes the grant, run as root. +func sudoGrantScript(user string) string { + line := fmt.Sprintf("%s ALL=(ALL) NOPASSWD:ALL", user) + // Checked by visudo before it is put in place: a broken file in + // sudoers.d breaks sudo for everyone on the machine. + // + // And it removes itself an hour later whatever happens to this process: + // RevokeSudo is called when the setup finishes, and a setup that crashes + // or is interrupted never gets there — which would leave a permanent + // passwordless root behind. + inner := fmt.Sprintf("umask 337; printf '%%s\\n' %s %s > %[3]s.new && visudo -cf %[3]s.new >/dev/null && mv -f %[3]s.new %[3]s && "+ + "{ systemd-run --quiet --on-active=%[4]d /bin/rm -f %[3]s >/dev/null 2>&1 || "+ + "(setsid nohup sh -c 'sleep %[4]d; rm -f %[3]s' >/dev/null 2>&1 &) ; }", + shellQuote("# llmgw setup: removed when it finishes, or within the hour"), shellQuote(line), sudoersDrop, + int(sudoGrantLife.Seconds())) + return inner +} diff --git a/internal/onboard/onboard_test.go b/internal/onboard/onboard_test.go index 2b2ae559..95e0a192 100644 --- a/internal/onboard/onboard_test.go +++ b/internal/onboard/onboard_test.go @@ -8,6 +8,7 @@ import ( "errors" "net/http" "net/http/httptest" + "os/exec" "regexp" "runtime" "strings" @@ -375,3 +376,16 @@ func TestTheFabricsGivenAreCheckedAsDeclared(t *testing.T) { t.Errorf("doctor was not told the fabric: %v", m.scripts) } } + +func TestTheSudoGrantRemovesItselfWithinTheHour(t *testing.T) { + inner := sudoGrantScript("ilker") + if !strings.Contains(inner, "--on-active=3600") || !strings.Contains(inner, "sleep 3600; rm -f "+sudoersDrop) { + t.Errorf("no removal scheduled: %s", inner) + } + if !strings.Contains(inner, "visudo -cf") { + t.Error("written without visudo's check") + } + if out, err := exec.Command("sh", "-n", "-c", inner).CombinedOutput(); err != nil { + t.Errorf("not shell: %v %s\n%s", err, out, inner) + } +} diff --git a/internal/onboard/ssh.go b/internal/onboard/ssh.go index 85fdcc17..62349819 100644 --- a/internal/onboard/ssh.go +++ b/internal/onboard/ssh.go @@ -41,7 +41,9 @@ func (s *SSH) Run(ctx context.Context, script string, stdin []byte) ([]byte, err if s.User != "" { target = s.User + "@" + s.Addr } - args := append(append([]string{}, sshOptions...), target, "sh", "-c", shellQuote(script)) + // "--" before the target: an address beginning with "-" is not read as + // an option. + args := append(append([]string{}, sshOptions...), "--", target, "sh", "-c", shellQuote(script)) cmd := exec.CommandContext(ctx, "ssh", args...) if stdin != nil { cmd.Stdin = bytes.NewReader(stdin)