Use private memfd for PostgreSQL passfile
Foundation / checks (ecs) (push) Canceled after 0s
Foundation / checks (network) (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 49s
Foundation / checks (client) (push) Canceled after 48s
Foundation / checks (deploy) (push) Canceled after 53s

This commit is contained in:
aevolution-gitops-bot
2026-10-05 10:44:21 -04:00
parent f30c63de75
commit 029bec3e2d
2 changed files with 66 additions and 3 deletions
@@ -67,10 +67,10 @@ spec:
exec /usr/local/bin/evo-server --profile lineage "${retained[@]}" "${inputs[@]}" \
"${m10[@]}" \
--world "$REGION_WORLD" --region "$REGION_ID" --frame "$REGION_FRAME" \
--pg-service /run/postgres/pg_service.conf --pg-passfile /dev/stdin \
--pg-service /run/postgres/pg_service.conf --pg-passfile /run/postgres/pgpass \
--objects /objects --objects-reserve-mib "$REGION_RESERVE_MIB" \
--build "$REGION_BUILD" --lease-profile default \
--flush-ms 1000 --drain-ms 20000 --ops-listen 0.0.0.0:9100 < /run/postgres/pgpass
--flush-ms 1000 --drain-ms 20000 --ops-listen 0.0.0.0:9100
env:
- {name: REGION_WORLD, value: set-by-overlay}
- {name: REGION_ID, value: set-by-overlay}
+64 -1
View File
@@ -6,7 +6,12 @@
#include <cstdlib>
#include <fstream>
#include <libpq-fe.h>
#include <linux/memfd.h>
#include <poll.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utility>
#include <vector>
namespace evo::persistence {
@@ -31,6 +36,56 @@ struct Clear {
};
using Connection = std::unique_ptr<PGconn, Finish>;
using Result = std::unique_ptr<PGresult, Clear>;
struct PrivatePassfile {
int fd{-1};
explicit PrivatePassfile(int value) : fd(value) {}
~PrivatePassfile() {
if (fd >= 0)
::close(fd);
}
PrivatePassfile(const PrivatePassfile&) = delete;
PrivatePassfile& operator=(const PrivatePassfile&) = delete;
PrivatePassfile(PrivatePassfile&& other) noexcept : fd(std::exchange(other.fd, -1)) {}
PrivatePassfile& operator=(PrivatePassfile&& other) noexcept {
if (this != &other) {
if (fd >= 0)
::close(fd);
fd = std::exchange(other.fd, -1);
}
return *this;
}
[[nodiscard]] std::string path() const {
return "/proc/self/fd/" + std::to_string(fd);
}
};
std::expected<PrivatePassfile, PgError> private_passfile(const std::string& source) {
std::ifstream in(source, std::ios::binary);
if (!in)
return std::unexpected{PgError::config};
std::string contents(4097, '\0');
in.read(contents.data(), static_cast<std::streamsize>(contents.size()));
contents.resize(static_cast<std::size_t>(in.gcount()));
if (contents.empty() || contents.size() > 4096 || contents.find('\0') != std::string::npos)
return std::unexpected{PgError::config};
const int fd = static_cast<int>(::syscall(SYS_memfd_create, "evo-pgpass", MFD_CLOEXEC));
if (fd < 0 || ::fchmod(fd, S_IRUSR | S_IWUSR) != 0) {
if (fd >= 0)
::close(fd);
return std::unexpected{PgError::config};
}
std::size_t written{};
while (written < contents.size()) {
const auto count = ::write(fd, contents.data() + written, contents.size() - written);
if (count <= 0) {
::close(fd);
return std::unexpected{PgError::config};
}
written += static_cast<std::size_t>(count);
}
if (::lseek(fd, 0, SEEK_SET) < 0)
return std::unexpected{PgError::config};
return PrivatePassfile{fd};
}
bool wait(PGconn* c, short events, Clock::time_point deadline) {
for (;;) {
const auto left =
@@ -150,6 +205,14 @@ Postgres::connect(const PgOptions& o, std::chrono::milliseconds timeout) {
inet_pton(AF_INET6, o.hostaddr.c_str(), address.data()) != 1))) ||
(local && !o.hostaddr.empty()))
return std::unexpected{fail(PgError::config)};
std::optional<PrivatePassfile> private_file;
if (!o.password_file.empty()) {
auto made = private_passfile(o.password_file);
if (!made)
return std::unexpected{fail(made.error())};
private_file.emplace(std::move(*made));
}
const std::string passfile = private_file ? private_file->path() : std::string{};
const char* keys[] = {"host", "hostaddr", "port", "dbname", "user", "passfile",
"sslrootcert", "sslmode", "gssencmode", "options", nullptr};
const char* values[] = {o.host.c_str(),
@@ -157,7 +220,7 @@ Postgres::connect(const PgOptions& o, std::chrono::milliseconds timeout) {
o.port.c_str(),
o.database.c_str(),
o.user.c_str(),
o.password_file.c_str(),
passfile.c_str(),
o.ca_file.c_str(),
local ? "disable" : "verify-full",
"disable",