#include "../GoogleDemos.hpp" #include #include #include "../../fw/Router.hpp" #include "../../fw/Templates.hpp" #include "google/credentials.h" #include "google/ops/api_state.h" #include "google/ops/call_plan.h" #include "google/ops/calls.h" #include "google/ops/caps.h" #include "google/ops/ops.h" #include "google/resource.h" namespace molo::example::app { // Google Cloud operations (Compute, Cloud Run, Functions, GKE, Build, Artifact // Registry, IAM, Resource Manager, Service Usage, Logging, Monitoring, // Scheduler, Tasks, Workflows, Eventarc, Billing, Asset, Org Policy) over the // real src/google/ops package. GET routes need no credential. Every route that // calls Google is a POST: 422 on bad input (checked first, against the // Discovery-derived call table), 503 google_not_configured without a // credential. Writes and destructive calls are dry runs unless the body says // "confirm":true; enabling or disabling an API is never done by the page on its // own and stays a dry run until a human confirms. namespace { namespace ops = google::ops; using Json = clients::body::Json; constexpr std::size_t kMaxBody = 256 * 1024; constexpr std::size_t kMaxCallsShown = 500; constexpr int kDemoPages = 5; const char kPage[] = R"GOOGLEOPS({% extends "base.html" %} {% block content %}

Google Cloud operations

Compute, Cloud Run, Functions, GKE, Build, Artifact Registry, IAM, Resource Manager, Service Usage, Logging, Monitoring, Scheduler, Tasks, Workflows, Eventarc, Billing, Asset, Org Policy. Reads run; every change is a dry run until you tick confirm. Enabling an API is a human decision: the page only shows the plan.

Checking Google credentials...

API state

Any call


{% endblock %} )GOOGLEOPS"; fw::Response json_out(const Json& j, int status = 200) { fw::Response res = fw::JsonResponse(j.dump(), status); res.headers.set("Cache-Control", "no-store"); return res; } [[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); } std::string project_hint() { return google::Credentials::env("GOOGLE_CLOUD_PROJECT"); } [[noreturn]] void rethrow(const google::GoogleError& e) { if (e.http_status() == 503 && e.status() == "NOT_CONFIGURED") throw fw::HttpError(503, "google_not_configured", e.message()); if (e.service().empty() && e.status() == "INVALID_ARGUMENT") throw fw::HttpError(422, "validation_error", e.message()); if (auto off = ops::ApiState::disabled(e, project_hint())) throw fw::HttpError(502, "api_not_enabled", off->loud()); throw fw::HttpError(502, "google_error", e.status() + ": " + e.message()); } template auto guarded(F&& f) -> decltype(f()) { try { return f(); } catch (const google::GoogleError& e) { rethrow(e); } } Json parse_body(const fw::Request& req) { if (req.body.size() > kMaxBody) throw fw::HttpError(413, "payload_too_large", "body too large"); if (req.body.empty()) return Json::object(); try { Json j = Json::parse(req.body); if (!j.is_object()) bad("body must be a JSON object"); return j; } catch (const clients::body::JsonError&) { bad("body is not valid JSON"); } } std::string text(const Json& body, const char* key, bool required = false) { const Json* v = body.find(key); if (!v || v->is_null()) { if (required) bad(std::string(key) + " is required"); return ""; } if (!v->is_string()) bad(std::string(key) + " must be a string"); return v->as_string(); } bool flag(const Json& body, const char* key) { const Json* v = body.find(key); if (!v || v->is_null()) return false; if (!v->is_bool()) bad(std::string(key) + " must be true or false"); return v->as_bool(); } std::string project_of(const Json& body) { std::string p = text(body, "project"); if (p.empty()) p = project_hint(); if (p.empty()) bad("project is required (body.project or GOOGLE_CLOUD_PROJECT)"); if (!ops::valid_name_segment(p)) bad("project is not a valid project id"); return p; } // Local checks failing as 422 (library "invalid" = bad input). template auto local(F&& f) -> decltype(f()) { return guarded(std::forward(f)); } class OpsRoutes { public: explicit OpsRoutes(std::shared_ptr client) : client_(std::move(client)) {} fw::Response status(const fw::Request&) const { Json o = Json::object(); o.set("configured", Json(client_->configured())); o.set("project_set", Json(!project_hint().empty())); o.set("products", Json(static_cast(ops::product_count()))); o.set("calls", Json(static_cast(ops::call_count()))); o.set("max_pages", Json(kDemoPages)); return json_out(o); } // The discovery-derived table, optionally one product: ?product=run fw::Response calls(const fw::Request& req) const { std::string only = req.query_or("product", ""); Json arr = Json::array(); Json prods = Json::array(); for (std::size_t i = 0; i < ops::product_count(); ++i) { Json p = Json::object(); p.set("name", Json(ops::products()[i].name)); p.set("version", Json(ops::products()[i].version)); p.set("revision", Json(ops::products()[i].revision)); prods.push(p); } std::size_t shown = 0; for (std::size_t i = 0; i < ops::call_count() && shown < kMaxCallsShown; ++i) { const ops::CallSpec& c = ops::call_table()[i]; if (!only.empty() && c.product() != only) continue; Json row = Json::object(); row.set("rpc", Json(c.rpc)); row.set("http", Json(c.http)); row.set("path", Json(c.path)); row.set("safety", Json(c.safe() == ops::Safety::kRead ? "read" : c.safe() == ops::Safety::kWrite ? "write" : "destructive")); row.set("list", Json(c.is_list())); row.set("body_fields", Json(c.body_fields)); row.set("query", Json(c.query_params)); arr.push(row); ++shown; } Json o = Json::object(); o.set("products", prods); o.set("calls", arr); return json_out(o); } // Any table row. Body: {"rpc","path":{},"query":{},"body":{},"confirm":bool}. fw::Response call(const fw::Request& req) const { Json b = parse_body(req); std::string rpc = text(b, "rpc", true); bool confirm = flag(b, "confirm"); ops::Args args = local([&] { return ops::Args::from_json(b); }); const ops::CallSpec* spec = ops::find_call(rpc); if (!spec) bad("unknown call: " + rpc); ops::CallPlan plan = local([&] { return ops::CallPlan::build(*spec, args); }); if (spec->safe() != ops::Safety::kRead) { ops::Outcome out; out.plan = plan; if (!confirm) return json_out(out.to_json()); // dry run: no credential needed need_credential(spec->product()); return guarded([&] { return json_out(ops::OpsClient(client_).execute(rpc, args, true).to_json()); }); } need_credential(spec->product()); return guarded([&] { ops::OpsClient c(client_); Json o = Json::object(); o.set("rpc", Json(rpc)); if (spec->is_list()) o.set("items", c.list(rpc, args, kDemoPages)); else o.set("response", c.read(rpc, args)); return json_out(o); }); } // Service Usage: list, get, batch_get, enable, disable. enable and disable // are dry runs unless confirm is true. fw::Response services(const fw::Request& req) const { Json b = parse_body(req); std::string action = text(b, "action", true); std::string project = project_of(b); std::string parent = local([&] { return ops::Names::project(project); }); if (action == "list") { std::string state = text(b, "state"); if (!state.empty() && state != "ENABLED" && state != "DISABLED") bad("state must be ENABLED or DISABLED"); std::string filter = state.empty() ? "" : "state:" + state; need_credential("serviceusage"); return guarded([&] { ops::ServiceUsage su(client_); Json items = su.list_services(parent, filter); Json arr = Json::array(); for (const Json& item : items.as_array()) { ops::ServiceState s = ops::ServiceState::from_json(item); Json row = Json::object(); row.set("api", Json(s.api)); row.set("state", Json(s.state)); row.set("title", Json(s.title)); arr.push(row); } Json o = Json::object(); o.set("services", arr); return json_out(o); }); } if (action == "get" || action == "enable" || action == "disable") { std::string name = local([&] { return ops::ServiceUsage::service_name(project, text(b, "api", true)); }); if (action == "get") { need_credential("serviceusage"); return guarded([&] { return json_out(ops::ServiceUsage(client_).service(name)); }); } bool confirm = flag(b, "confirm"); ops::ServiceUsage su(client_); ops::Outcome out = local([&] { return action == "enable" ? su.enable(name, false) : su.disable(name, false, false); }); if (!confirm) return json_out(out.to_json()); need_credential("serviceusage"); return guarded([&] { return json_out((action == "enable" ? su.enable(name, true) : su.disable(name, false, true)) .to_json()); }); } if (action == "batch_get") { const Json* apis = b.find("apis"); if (!apis || !apis->is_array()) bad("apis must be an array of API names"); std::vector names; for (const Json& a : apis->as_array()) { if (!a.is_string()) bad("apis must be strings"); names.push_back(local([&] { return ops::ServiceUsage::service_name(project, a.as_string()); })); } if (names.empty() || names.size() > ops::OpsCaps::kMaxBatchNames) bad("apis must hold 1..20 names"); need_credential("serviceusage"); return guarded([&] { return json_out(ops::ServiceUsage(client_).batch_get(parent, names)); }); } bad("action must be list, get, batch_get, enable or disable"); } // Resource Manager: get, search. fw::Response projects(const fw::Request& req) const { Json b = parse_body(req); std::string action = text(b, "action", true); if (action == "get") { std::string name = local([&] { return ops::Names::project(project_of(b)); }); need_credential("cloudresourcemanager"); return guarded([&] { return json_out(ops::ResourceManager(client_).project(name)); }); } if (action == "search") { std::string query = text(b, "query"); if (query.size() > 256) bad("query too long"); need_credential("cloudresourcemanager"); return guarded([&] { Json o = Json::object(); o.set("projects", ops::ResourceManager(client_).search_projects(query)); return json_out(o); }); } bad("action must be get or search"); } // IAM: service accounts, roles, permission test; signing via IAM Credentials. fw::Response iam(const fw::Request& req) const { Json b = parse_body(req); std::string action = text(b, "action", true); std::string project = project_of(b); std::string parent = local([&] { return ops::Names::project(project); }); if (action == "service_accounts") { need_credential("iam"); return guarded([&] { Json o = Json::object(); o.set("accounts", ops::Iam(client_).service_accounts(parent)); return json_out(o); }); } if (action == "roles") { need_credential("iam"); return guarded([&] { Json o = Json::object(); o.set("roles", ops::Iam(client_).project_roles(parent)); return json_out(o); }); } if (action == "test_permissions" || action == "sign_jwt" || action == "sign_blob") { std::string account = text(b, "account", true); std::string name = local([&] { std::string n = "projects/" + project + "/serviceAccounts/" + account; ops::check_resource_name("account", n, "projects/*/serviceAccounts/*"); return n; }); if (action == "test_permissions") { const Json* perms = b.find("permissions"); if (!perms || !perms->is_array() || perms->as_array().empty() || perms->as_array().size() > 100) bad("permissions must be an array of 1..100 strings"); std::vector list; for (const Json& p : perms->as_array()) { if (!p.is_string()) bad("permissions must be strings"); list.push_back(p.as_string()); } need_credential("iam"); return guarded([&] { return json_out(ops::Iam(client_).test_permissions(name, list)); }); } std::string payload = text(b, "payload", true); if (payload.size() > ops::OpsCaps::kMaxSignPayloadBytes) bad("payload too large"); need_credential("iamcredentials"); return guarded([&] { ops::IamCredentials cr(client_); return json_out(action == "sign_jwt" ? cr.sign_jwt(name, payload) : cr.sign_blob(name, payload)); }); } bad("action must be service_accounts, roles, test_permissions, sign_jwt or sign_blob"); } // Logging: recent entries of a project. fw::Response logs(const fw::Request& req) const { Json b = parse_body(req); std::string parent = local([&] { return ops::Names::project(project_of(b)); }); std::string filter = text(b, "filter"); int limit = 20; if (const Json* l = b.find("limit"); l && !l->is_null()) { if (!l->is_number()) bad("limit must be a number"); limit = static_cast(l->as_int()); } if (limit < 1 || limit > 100) bad("limit must be 1..100"); if (filter.size() > 2048) bad("filter too long"); need_credential("logging"); return guarded([&] { Json o = Json::object(); o.set("entries", ops::CloudLogging(client_).entries({parent}, filter, "timestamp desc", limit)); return json_out(o); }); } private: void need_credential(const std::string& product) const { if (!client_->configured()) { google::NotConfiguredError e(product); rethrow(e); } } std::shared_ptr client_; }; } // namespace void register_google_ops_demo(fw::Router& router, Services& services) { if (!services.google) services.google = google::GoogleClient::from_env(); auto client = services.google; fw::Environment* env = services.templates.get(); services.templates->add_template("google_ops.html", kPage); services.add_feature("google-ops", "/google/ops"); auto routes = std::make_shared(client); router.get("/google/ops", "google-ops-page", [env](fw::Request req) { (void)req; fw::TemplateCtx ctx; ctx["title"] = std::string("Google Cloud operations"); return fw::Response(200, env->render("google_ops.html", ctx), "text/html; charset=utf-8"); }); using Handler = fw::Response (OpsRoutes::*)(const fw::Request&) const; auto get = [&](const char* path, const char* name, Handler h) { router.get(path, name, [routes, h](fw::Request req) { return ((*routes).*h)(req); }); }; auto post = [&](const char* path, const char* name, Handler h) { router.post(path, name, [routes, h](fw::Request req) { return ((*routes).*h)(req); }); }; get("/google/ops/status", "google-ops-status", &OpsRoutes::status); get("/google/ops/calls", "google-ops-calls", &OpsRoutes::calls); post("/google/ops/call", "google-ops-call", &OpsRoutes::call); post("/google/ops/services", "google-ops-services", &OpsRoutes::services); post("/google/ops/projects", "google-ops-projects", &OpsRoutes::projects); post("/google/ops/iam", "google-ops-iam", &OpsRoutes::iam); post("/google/ops/logs", "google-ops-logs", &OpsRoutes::logs); } } // namespace molo::example::app