Files
retoor 0595284fe3 feat(doos): add container runtime, audit cases, example demo and tests
- New rootless doos container runtime: sandbox, cgroup, network, images, hardening, davmount, CLI plus molo integration
- 18 CVE audit reproductions with READMEs and Makefiles
- Example DoosDemo routes, IndexCatalog, static assets, spec update
- CLI serve on 0.0.0.0:8000, arg parser, dev server improvements
- Runtime HTTP fixes, supervisor isolation backend
- Tests for doos, example doos routes, CLI serve; suite count 261 to 265
2026-11-06 04:36:00 +01:00

159 lines
5.8 KiB
C++

#include "App.hpp"
#include <cstdlib>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "../fw/Http.hpp"
#include "../fw/Router.hpp"
#include "../fw/Templates.hpp"
#include "../fw/Validate.hpp"
#include "doos/caps.h"
#include "doos/cli.hpp"
#include "doos/container.hpp"
#include "doos/network.hpp"
#include "doos/util.hpp"
namespace molo::example::app {
namespace {
// Demo store: DOOS_HOME when the harness sets it (tests point it at scratch),
// else build/doos-demo. Never the user's real store.
void use_demo_home() {
const char* h = ::getenv("DOOS_HOME");
if (!h || !*h) ::setenv("DOOS_HOME", "build/doos-demo", 1);
}
std::string input_or(const std::map<std::string, std::string>& input, const char* key) {
auto it = input.find(key);
return it == input.end() ? "" : it->second;
}
// One {"ok":true,<key>:<value>} body from run_json, unwrapped to <key>:<value>
// so /doos/status composes the single renderer instead of forking its fields.
std::string run_inner(const std::vector<std::string>& argv) {
std::string body, err;
int rc = molo::doos::run_json(argv, body, err);
if (rc != 0) throw std::runtime_error(err.empty() ? "doos status failed" : err);
const std::string pre = "{\"ok\":true,";
if (body.rfind(pre, 0) != 0 || body.size() < pre.size() + 2) {
throw std::runtime_error("bad doos json");
}
return body.substr(pre.size(), body.size() - pre.size() - 2); // strip trailing "}\n"
}
} // namespace
void register_doos_demo(fw::Router& router, Services& services) {
use_demo_home();
fw::Environment* env = services.templates.get();
router.get("/doos", "doos-page", [env](fw::Request req) {
(void)req;
fw::TemplateCtx ctx;
ctx["title"] = std::string("Sandboxed containers");
ctx["heading"] = std::string("Sandboxed containers (doos)");
molo::doos::Store store = molo::doos::Store::fromEnvironment();
fw::TemplateList items;
for (const auto& c : store.list()) {
items.push_back({{"name", c.name()},
{"kind", std::string(c.runningPid() ? "running" : "stopped") + " - " +
molo::doos::Network::name(c.net()) + " - " +
(c.imageMode() ? c.image() : "host")}});
}
ctx["items"] = items;
ctx["snippet"] = std::to_string(items.size()) + " containers (demo store)";
return fw::Response(200, env->render("page.html", ctx), "text/html; charset=utf-8");
});
router.get("/doos/status", "doos-status", [](fw::Request req) {
(void)req;
try {
std::string s = "{" + run_inner({"list"}) + "," + run_inner({"image", "list"}) + "," +
run_inner({"davmount", "list"}) + "," + run_inner({"version"}) + "}";
return fw::JsonResponse(s, 200);
} catch (const std::exception& e) {
return fw::JsonResponse("{\"error\":\"" + fw::json_escape(e.what()) + "\"}", 500);
}
});
router.post("/doos/create", "doos-create", [](fw::Request req) {
auto input = fw::parse_json_object_flat(req.body);
if (input.empty()) input = fw::parse_form(req.body);
std::vector<std::string> failures;
std::string name = input_or(input, "name");
if (name.empty()) failures.push_back("name is required");
molo::doos::Options opts;
try {
if (!input_or(input, "size").empty()) {
opts.size = molo::doos::parseSize(input_or(input, "size"));
}
} catch (const std::exception& e) {
failures.push_back(e.what());
}
try {
if (!input_or(input, "memory").empty()) {
opts.memory = molo::doos::parseSize(input_or(input, "memory"));
}
} catch (const std::exception& e) {
failures.push_back(e.what());
}
try {
if (!input_or(input, "net").empty()) {
opts.net = molo::doos::Network::parse(input_or(input, "net"));
}
} catch (const std::exception& e) {
failures.push_back(e.what());
}
if (!failures.empty()) {
std::ostringstream o;
o << "{\"errors\":[";
for (std::size_t i = 0; i < failures.size(); ++i) {
if (i) o << ",";
o << "\"" << fw::json_escape(failures[i]) << "\"";
}
o << "]}";
return fw::JsonResponse(o.str(), 422);
}
try {
molo::doos::Store store = molo::doos::Store::fromEnvironment();
auto [c, created] = store.getOrCreate(name, opts);
std::ostringstream o;
o << "{\"name\":\"" << molo::doos::json_escape(c.name()) << "\",\"created\":"
<< (created ? "true" : "false") << "}";
return fw::JsonResponse(o.str(), 200);
} catch (const std::exception& e) {
return fw::JsonResponse("{\"errors\":[\"" + fw::json_escape(e.what()) + "\"]}", 422);
}
});
router.post("/doos/delete", "doos-delete", [](fw::Request req) {
auto input = fw::parse_json_object_flat(req.body);
if (input.empty()) input = fw::parse_form(req.body);
std::string name = input_or(input, "name");
if (name.empty()) return fw::JsonResponse("{\"errors\":[\"name is required\"]}", 422);
bool confirm = input_or(input, "confirm") == "yes";
try {
molo::doos::Store store = molo::doos::Store::fromEnvironment();
std::string report = store.remove(name, confirm);
std::ostringstream o;
o << "{\"name\":\"" << molo::doos::json_escape(name) << "\",\"dry_run\":"
<< (confirm ? "false" : "true") << ",\"report\":\"" << molo::doos::json_escape(report)
<< "\"}";
return fw::JsonResponse(o.str(), 200);
} catch (const std::exception& e) {
std::string msg = e.what();
int code = msg.find("no such container") != std::string::npos ? 404 : 422;
std::string key = code == 404 ? "error" : "errors";
std::string val = code == 404 ? "\"" + fw::json_escape(msg) + "\""
: "[\"" + fw::json_escape(msg) + "\"]";
return fw::JsonResponse("{\"" + key + "\":" + val + "}", code);
}
});
}
} // namespace molo::example::app