Files
moloapp/src/supplychain/attest/Signer.cpp
T
retoor 3c4334cec9 feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security,
data, app, realtime, supervisor, clients, CLI, example plus
enterprise tenancy, audit, operability, governance,
supplychain. Postgres via docker compose for tests.
2026-10-23 18:20:04 +02:00

80 lines
2.4 KiB
C++

#include "../attest/Signer.hpp"
#include <cstdio>
#include <cstdlib>
#if __has_include("cli/embed/sha256.hpp")
#include "cli/embed/sha256.hpp"
#elif __has_include("embed/sha256.hpp")
#include "embed/sha256.hpp"
#else
#include "../adapters/cli/embed/sha256.hpp" // ADAPTER fallback for isolated builds
#endif
namespace molo::supply::attest {
std::string Signer::quote_path(std::string_view path) {
std::string out = "'";
for (char c : path) {
if (c == '\'') {
out += "'\\''";
} else {
out.push_back(c);
}
}
out += "'";
return out;
}
bool Signer::sign(const std::string& blob_path, const std::string& bundle_path,
std::string& error) {
if (blob_path.empty() || bundle_path.empty()) {
error = "blob and bundle paths must not be empty";
return false;
}
const char* fake = std::getenv("MOLO_FAKE_COSIGN");
if (fake != nullptr && std::string(fake) == "1") {
std::string digest;
if (!molo::embed::Sha256::hex_of_file(blob_path, digest)) {
error = "fake sign: cannot hash blob '" + blob_path + "'";
return false;
}
std::FILE* f = std::fopen(bundle_path.c_str(), "wb");
if (f == nullptr) {
error = "fake sign: cannot write bundle '" + bundle_path + "'";
return false;
}
std::string bundle = "{\"fakeBundle\":true,\"digest\":\"" + digest +
"\",\"signature\":\"FAKE-" + digest.substr(0, 16) + "\"}\n";
std::size_t wrote = std::fwrite(bundle.data(), 1, bundle.size(), f);
bool ok = wrote == bundle.size() && std::fclose(f) == 0;
if (!ok) error = "fake sign: failed writing bundle '" + bundle_path + "'";
return ok;
}
std::string cmd = "cosign sign-blob --yes --bundle " + quote_path(bundle_path) + " " +
quote_path(blob_path) + " 2>&1";
std::FILE* pipe = ::popen(cmd.c_str(), "r");
if (pipe == nullptr) {
error = "cannot spawn cosign";
return false;
}
std::string output;
char chunk[1024];
std::size_t n = 0;
while ((n = std::fread(chunk, 1, sizeof(chunk), pipe)) > 0) {
if (output.size() + n > 2048) {
output.append(chunk, 2048 - output.size());
break;
}
output.append(chunk, n);
}
int status = ::pclose(pipe);
if (status != 0) {
error = "cosign sign-blob failed (status " + std::to_string(status) + "): " + output;
return false;
}
return true;
}
} // namespace molo::supply::attest