Ask for a password on Patroni's API and give the store TLS, new clusters and existing ones in place
Patroni's REST API accepted changes from anyone on port 8008, and PostgreSQL ran with sslmode=disable between machines. Setup now keeps a fourth secret for the API (carried to members of older clusters), issues each member a certificate from the cluster's authority, writes restapi.authentication into each member's config and reloads it, and turns ssl on with hostssl remote rules through an authenticated PATCH of the shared configuration. The DSN becomes sslmode=verify-full against the authority's certificate whenever the cluster has TLS, so a control plane without the authority's key reconnects too. server add authenticates its PATCH and lets the new control plane in by hostssl. Tried on the e2e cluster: unauthenticated PATCH 401, plaintext refused, replication streaming, the control plane on verify-full. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
9d961c3fc8
commit
2b29016d5b
8 files changed
+558
-51
No files matched your search
@@ -307,7 +307,11 @@ func (sa *serverAdd) storeAccess(ctx context.Context) error {
|
||||
case slices.Contains(hosts, sa.ip):
|
||||
sa.kept("store: %s is one of its members", sa.ip)
|
||||
case len(hosts) > 1:
|
||||
changed, err := pgha.AllowClient(ctx, hosts, sa.ip)
|
||||
rest := ""
|
||||
if b, err := os.ReadFile(patroniRestFile(sa.etcDir)); err == nil { //nolint:gosec // our own secret
|
||||
rest = strings.TrimSpace(string(b))
|
||||
}
|
||||
changed, err := pgha.AllowClient(ctx, hosts, sa.ip, rest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
|
||||
@@ -58,13 +59,26 @@ func (in *serverInit) ensurePatroni(ctx context.Context) (err error) {
|
||||
setup.Witness = &pgha.Node{Member: pgha.Member{Name: name, IP: self}, Remote: pgha.Local{}}
|
||||
setup.Clients = []string{self}
|
||||
|
||||
cfg, err := config.Load(in.configPath())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// The cluster's authority signs each member's certificate, and the
|
||||
// control plane verifies the member it reaches against it: where its
|
||||
// key is on this machine, and the server has the certificate to read.
|
||||
setup.CAFile = cfg.AgentTLS.CA
|
||||
if ca, cerr := loadCA(in.pkiDir); cerr == nil && cfg.AgentTLS.CA != "" {
|
||||
setup.CA = ca
|
||||
} else {
|
||||
in.kept("store: the authority's key is not here, so no member certificates are signed from this machine; " +
|
||||
"run this on the control plane that holds it to turn TLS on")
|
||||
}
|
||||
|
||||
dsn, err := setup.Run(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg, err := config.Load(in.configPath())
|
||||
if err != nil {
|
||||
if err := in.writeSecret(patroniRestFile(in.etcDir), setup.RestPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
current := cfg.Auth.Store
|
||||
@@ -180,3 +194,21 @@ func ipv4Of(host string) (string, error) {
|
||||
}
|
||||
return "", fmt.Errorf("%s has no IPv4 address", host)
|
||||
}
|
||||
|
||||
// patroniRestFile holds what Patroni's API asks for before a change, on the
|
||||
// control plane: for `server add`, which lets a second one in.
|
||||
func patroniRestFile(etcDir string) string { return filepath.Join(etcDir, "secrets", "patroni-rest") }
|
||||
|
||||
// writeSecret writes a secret root alone reads, unless it is there already.
|
||||
func (in *serverInit) writeSecret(path, value string) error {
|
||||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
if b, err := os.ReadFile(path); err == nil && strings.TrimSpace(string(b)) == value { //nolint:gosec // our own secret
|
||||
return nil
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o750); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, []byte(value+"\n"), 0o600)
|
||||
}
|
||||
Reference in new issue
Block a user