diff --git a/internal/pgha/harden.go b/internal/pgha/harden.go index f73e1362..3dd0ed37 100644 --- a/internal/pgha/harden.go +++ b/internal/pgha/harden.go @@ -160,6 +160,13 @@ func (s *Setup) tlsInShared(ctx context.Context, sec Secrets) error { } } } + if len(hba) == 0 { + // Kept in each member's own file, by a cluster set up before + // they moved here: made hostssl there. + if err := s.memberHostssl(ctx); err != nil { + return err + } + } want := sslParameters() same := slices.Equal(hba, next) for k, v := range want { @@ -184,6 +191,50 @@ func (s *Setup) tlsInShared(ctx context.Context, sec Secrets) error { return fmt.Errorf("no member's Patroni answered: %w", lastErr) } +// memberHostssl makes the remote rules in each member's own +// /etc/patroni/config.yml hostssl, and reloads Patroni there, which rewrites +// pg_hba.conf and reloads PostgreSQL without restarting it. +func (s *Setup) memberHostssl(ctx context.Context) error { + for _, d := range s.DB { + out, err := d.Remote.Run(ctx, `set -e +test -f /etc/patroni/config.yml || { echo missing; exit 0; } +r=$(sudo python3 - <<'PY' +import yaml +p = "/etc/patroni/config.yml" +c = yaml.safe_load(open(p)) +rules = c.get("postgresql", {}).get("pg_hba") or [] +def hostssl(line): + f = line.split() + if len(f) < 4 or f[0] != "host" or f[3].startswith("127.") or f[3] == "::1/128": + return line + return "hostssl" + line[len("host"):] +new = [hostssl(l) for l in rules] +if new == rules: + print("same") +else: + c["postgresql"]["pg_hba"] = new + open(p + ".new", "w").write("# llmgw: the control planes' store, kept by Patroni, its leader lock in etcd.\n" + + yaml.safe_dump(c, default_flow_style=False, sort_keys=False)) + print("changed") +PY +) +if [ "$r" = changed ]; then + sudo chown root:postgres /etc/patroni/config.yml.new + sudo chmod 640 /etc/patroni/config.yml.new + sudo mv -f /etc/patroni/config.yml.new /etc/patroni/config.yml + sudo systemctl reload patroni +fi +echo "$r"`, nil) + if err != nil { + return fmt.Errorf("on %s: %v: %s", d.Name, err, strings.TrimSpace(string(out))) + } + if strings.TrimSpace(lastLine(out)) == "changed" { + s.emit("tls", true, "%s's own access rules are hostssl", d.Name) + } + } + return nil +} + // patchConfig changes the cluster's shared configuration, with the REST // password. func patchConfig(ctx context.Context, host, password string, body map[string]any) error {