Connect from libpq service files and match the real Secret layout

The maintainer's login Secrets carry pg_service.conf ([evolution]),
pgpass and ca.crt. Add a strict read_pg_service (known keys only,
verify-full unless a local socket, bounded size) behind --pg-service in
evo-server and evo-world-clone, mount each login Secret whole in the
store Jobs instead of reading aevolution-db, and fix the migrate Job's
PGSERVICE to the [evolution] section. Record the store node choice,
per-environment databases and the backup plan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-09-29 14:12:26 -04:00
co-authored by Claude Opus 5.5
parent 2d950060db
commit 8d594f28af
12 changed files with 221 additions and 31 deletions
+79
View File
@@ -4,11 +4,20 @@
#include <cerrno>
#include <climits>
#include <cstdlib>
#include <fstream>
#include <libpq-fe.h>
#include <poll.h>
#include <vector>
namespace evo::persistence {
namespace {
std::string_view trim(std::string_view s) {
while (!s.empty() && (s.front() == ' ' || s.front() == '\t' || s.front() == '\r'))
s.remove_prefix(1);
while (!s.empty() && (s.back() == ' ' || s.back() == '\t' || s.back() == '\r'))
s.remove_suffix(1);
return s;
}
using Clock = std::chrono::steady_clock;
struct Finish {
void operator()(PGconn* p) const {
@@ -52,6 +61,76 @@ struct Postgres::Impl {
};
Postgres::Postgres(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {}
Postgres::~Postgres() = default;
std::expected<PgOptions, PgError> read_pg_service(const std::string& file,
std::string_view section) {
constexpr std::size_t limit = 4096;
std::ifstream in(file, std::ios::binary);
if (!in)
return std::unexpected{PgError::config};
std::string text(limit + 1, '\0');
in.read(text.data(), static_cast<std::streamsize>(text.size()));
text.resize(static_cast<std::size_t>(in.gcount()));
if (text.size() > limit || section.empty())
return std::unexpected{PgError::config};
PgOptions o;
std::string sslmode;
bool inside = false, found = false;
std::vector<std::string> seen;
std::string_view rest = text;
while (!rest.empty()) {
const auto end = rest.find('\n');
const auto line = trim(rest.substr(0, end));
rest.remove_prefix(end == std::string_view::npos ? rest.size() : end + 1);
if (line.empty() || line.front() == '#')
continue;
if (line.front() == '[') {
if (line.back() != ']')
return std::unexpected{PgError::config};
inside = line.substr(1, line.size() - 2) == section;
if (inside && found)
return std::unexpected{PgError::config}; // Repeated section.
found = found || inside;
continue;
}
if (!inside)
continue;
const auto equals = line.find('=');
if (equals == std::string_view::npos)
return std::unexpected{PgError::config};
const std::string key{trim(line.substr(0, equals))};
const std::string value{trim(line.substr(equals + 1))};
if (std::ranges::find(seen, key) != seen.end())
return std::unexpected{PgError::config};
seen.push_back(key);
if (key == "host")
o.host = value;
else if (key == "hostaddr")
o.hostaddr = value;
else if (key == "port")
o.port = value;
else if (key == "dbname")
o.database = value;
else if (key == "user")
o.user = value;
else if (key == "sslrootcert")
o.ca_file = value;
else if (key == "sslmode")
sslmode = value;
else if (key == "connect_timeout") {
if (value.empty() ||
!std::ranges::all_of(value, [](char ch) { return ch >= '0' && ch <= '9'; }))
return std::unexpected{PgError::config};
} else {
return std::unexpected{PgError::config}; // password, passfile, options, ...
}
}
const bool local = !o.host.empty() && o.host.front() == '/';
if (!found ||
(!sslmode.empty() && sslmode != "verify-full" && !(local && sslmode == "disable")))
return std::unexpected{PgError::config};
return o;
}
std::expected<std::unique_ptr<Postgres>, PgFailure>
Postgres::connect(const PgOptions& o, std::chrono::milliseconds timeout) {
if (!duration(timeout) || o.host.empty() || o.database.empty() || o.user.empty() ||