Files
llmgw/internal/pgha/hba.go
T
Ilker ManapandClaude Opus 5.5 2b29016d5b 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>
2026-10-04 10:11:28 +02:00

87 lines
2.6 KiB
Go

package pgha
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"slices"
"strconv"
"strings"
"time"
)
// ClientLine is the access rule that lets a control plane at ip in.
func ClientLine(ip string) string {
return fmt.Sprintf("host %s %s %s/32 scram-sha-256", Database, Role, ip)
}
// AllowClient lets a control plane at ip into the cluster's database: one
// change to the cluster's shared configuration through Patroni's API, which
// every member applies. It says whether anything changed.
//
// A cluster set up before the rules moved into the shared configuration
// keeps them in each member's own file, which this cannot reach; that is
// said, with what to add, rather than half done.
//
// password is what Patroni's API asks for before a change (RestUser); a
// cluster whose rules are hostssl lets the new control plane in by TLS too.
func AllowClient(ctx context.Context, hosts []string, ip, password string) (bool, error) {
client := &http.Client{Timeout: 5 * time.Second}
var lastErr error
for _, h := range hosts {
base := "http://" + net.JoinHostPort(h, strconv.Itoa(restPort))
cfg, err := getConfig(ctx, client, base)
if err != nil {
lastErr = err
continue
}
var hba []string
if pg, ok := cfg["postgresql"].(map[string]any); ok {
if list, ok := pg["pg_hba"].([]any); ok {
for _, l := range list {
if s, ok := l.(string); ok {
hba = append(hba, s)
}
}
}
}
if len(hba) == 0 {
return false, fmt.Errorf("this cluster keeps its access rules in each member's "+
"/etc/patroni/config.yml, not in its shared configuration: add %q to pg_hba there "+
"on every member, and run `patronictl reload llmgw`", ClientLine(ip))
}
line := ClientLine(ip)
if slices.ContainsFunc(hba, func(l string) bool { return strings.HasPrefix(l, "hostssl ") }) {
line = hostssl(line)
}
if slices.Contains(hba, line) {
return false, nil
}
if err := patchConfig(ctx, client, base, password,
map[string]any{"postgresql": map[string]any{"pg_hba": append(hba, line)}}); err != nil {
return false, fmt.Errorf("Patroni on %s: %w", h, err)
}
return true, nil
}
return false, fmt.Errorf("no member's Patroni answered: %w", lastErr)
}
func getConfig(ctx context.Context, client *http.Client, base string) (map[string]any, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, base+"/config", nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var cfg map[string]any
if err := json.NewDecoder(resp.Body).Decode(&cfg); err != nil {
return nil, fmt.Errorf("%s/config: %w", base, err)
}
return cfg, nil
}