Files
moloapp/example/app/agent/ArmorDemo.cpp
T

399 lines
16 KiB
C++
Raw Normal View History

#include "../AgentDemos.hpp"
#include <algorithm>
#include <cctype>
#include <map>
#include <memory>
#include "../../fw/Router.hpp"
#include "../../fw/Templates.hpp"
#include "../../fw/Validate.hpp"
#include "clients/body/json.h"
#include "google/armor/armor_util.h"
#include "google/armor/caps.h"
#include "google/armor/dashboard.h"
#include "google/armor/findings.h"
#include "google/armor/guarded_model.h"
#include "google/armor/model_armor.h"
#include "google/credentials.h"
#include "google/endpoints.h"
namespace molo::example::app {
// Agent Platform: armor. Model Armor (templates, floor settings, sanitize),
// Security Command Center findings for agents, the local Agent Security
// dashboard summary, and a GuardedModel demo over a local echo model.
// Auth: Model Armor and Security Command Center refuse API keys; they need
// an OAuth access token (GOOGLE_ACCESS_TOKEN). See src/google/armor/SPEC.md.
namespace {
using Json = clients::body::Json;
namespace armor = google::armor;
using Form = std::map<std::string, std::string>;
std::string field(const Form& in, const char* key) {
auto it = in.find(key);
return it == in.end() ? std::string() : it->second;
}
[[noreturn]] void bad(const std::string& message) {
throw fw::HttpError(422, "validation_error", message);
}
// Maps Google part errors onto the example's JSON error format.
[[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") bad(e.message());
throw fw::HttpError(502, "google_error", e.status() + ": " + e.message());
}
template <typename F>
auto guarded(F&& f) -> decltype(f()) {
try {
return f();
} catch (const google::GoogleError& e) {
rethrow(e);
}
}
fw::Response json_out(const Json& doc) {
fw::Response res = fw::JsonResponse(doc.dump(), 200);
res.headers.set("Cache-Control", "no-store");
return res;
}
int int_field(const Form& in, const char* key, int fallback, int max) {
std::string v = field(in, key);
if (v.empty()) return fallback;
for (char c : v)
if (!std::isdigit(static_cast<unsigned char>(c))) bad(std::string(key) + " must be a number");
if (v.size() > 6) bad(std::string(key) + " is too large");
int n = std::stoi(v);
if (n > max) bad(std::string(key) + " must be at most " + std::to_string(max));
return n;
}
bool flag(const Form& in, const char* key) {
std::string v = field(in, key);
return v == "true" || v == "1" || v == "on" || v == "yes";
}
armor::Confidence confidence_of(const std::string& v) {
if (v.empty()) return armor::Confidence::kUnspecified;
armor::Confidence c = armor::confidence_from(v);
if (c == armor::Confidence::kUnspecified) bad("confidence must be LOW_AND_ABOVE, MEDIUM_AND_ABOVE or HIGH");
return c;
}
// "HATE_SPEECH:HIGH,DANGEROUS" -> rai filters.
std::vector<armor::RaiFilter> rai_of(const std::string& spec) {
std::vector<armor::RaiFilter> out;
std::size_t start = 0;
while (!spec.empty() && start <= spec.size()) {
std::size_t comma = spec.find(',', start);
std::string item = spec.substr(start, comma == std::string::npos ? std::string::npos : comma - start);
std::size_t colon = item.find(':');
armor::RaiFilter f;
f.type = item.substr(0, colon);
if (colon != std::string::npos) f.confidence = confidence_of(item.substr(colon + 1));
out.push_back(f);
if (comma == std::string::npos) break;
start = comma + 1;
}
return out;
}
Json template_json(const armor::Template& t) {
Json o = Json::object();
o.set("name", t.name);
o.set("create_time", t.create_time);
o.set("update_time", t.update_time);
o.set("body", t.to_json());
return o;
}
// Local model for the guard demo: no Google call, so only Model Armor is
// exercised. Replies with the prompt it received.
class EchoModel : public google::LanguageModel {
public:
google::GenerateResponse generate(const google::GenerateRequest& request) override {
google::GenerateResponse r;
google::Candidate c;
c.content = google::Content::model("Echo: " + armor::GuardedModel::prompt_text(request));
r.candidates.push_back(c);
return r;
}
void stream(const google::GenerateRequest& request,
const std::function<bool(const google::GenerateResponse&)>& on_chunk) override {
on_chunk(generate(request));
}
};
const char kArmorPage[] = R"ARMOR({% extends "base.html" %}
{% block content %}
<h1>Agent security</h1>
<p class="hint">Model Armor screens prompts and answers for prompt injection,
jailbreaks, sensitive data, malicious links and unsafe content. Security
Command Center collects agent threat and anomaly findings. Both need a Google
OAuth access token, not an API key.</p>
<p id="a-config" class="hint">Checking Google credentials...</p>
<style>
.armor label{display:block;margin:.5rem 0}
.armor input,.armor textarea,.armor select{width:100%;box-sizing:border-box}
.armor pre.out{overflow-x:auto;white-space:pre-wrap;word-break:break-word}
</style>
<section class="armor">
<label>Action
<select id="a-action">
<option value="sanitize/prompt">Check a prompt</option>
<option value="sanitize/response">Check an answer</option>
<option value="guard">Guarded echo model</option>
<option value="templates/list">List templates</option>
<option value="templates/get">Show a template</option>
<option value="templates/create">Create a template</option>
<option value="templates/delete">Delete a template</option>
<option value="floor/get">Floor settings</option>
<option value="findings">Agent findings</option>
<option value="dashboard">Security summary</option>
</select></label>
<label>Template id <input id="a-template" type="text" maxlength="128" placeholder="my-template"></label>
<label>Text <textarea id="a-text" rows="4" maxlength="4000" placeholder="Text to check"></textarea></label>
<label>Scope <input id="a-scope" type="text" maxlength="140" placeholder="organizations/123"></label>
<label>Severity
<select id="a-severity"><option value="">any</option><option>CRITICAL</option>
<option>HIGH</option><option>MEDIUM</option><option>LOW</option></select></label>
<button id="a-run" type="button" class="primary">Run</button>
<p id="a-error" class="error" role="alert"></p>
<pre id="a-out" class="out"></pre>
</section>
<script src="/embedded/agent_armor.js"></script>
{% endblock %}
)ARMOR";
} // namespace
void register_agent_armor_demo(fw::Router& router, Services& services) {
if (!services.google) services.google = google::GoogleClient::from_env();
auto client = services.google;
services.templates->add_template("agent_armor.html", kArmorPage);
services.add_feature("agent-armor", "/agent/armor");
fw::Environment* env = services.templates.get();
// Built per request so GOOGLE_CLOUD_PROJECT / LOCATION changes are seen.
auto armor_of = [client]() {
return std::make_shared<armor::ModelArmor>(client, google::CloudContext::from_env());
};
auto template_id = [](const Form& in) {
std::string id = field(in, "template_id");
if (!google::valid_segment(id)) bad("template_id must be 1-128 of A-Z a-z 0-9 . _ -");
return id;
};
router.get("/agent/armor", "agent-armor-page", [env](fw::Request req) {
(void)req;
fw::TemplateCtx ctx;
ctx["title"] = std::string("Agent security");
return fw::Response(200, env->render("agent_armor.html", ctx), "text/html; charset=utf-8");
});
router.get("/agent/armor/status", "agent-armor-status", [client](fw::Request req) {
(void)req;
google::CloudContext ctx = google::CloudContext::from_env();
Json o = Json::object();
o.set("configured", client->configured());
o.set("auth", "oauth_access_token");
o.set("auth_env", "GOOGLE_ACCESS_TOKEN");
o.set("api_keys_accepted", false);
o.set("project_set", !ctx.project.empty());
o.set("location", ctx.location);
o.set("model_armor_host", armor::ModelArmor::endpoint_for(ctx.valid() ? ctx.location : "us-central1").base_url);
o.set("security_center_host", armor::SecurityFindings::endpoint().base_url);
Json filters = Json::array();
for (auto k : {armor::FilterKind::kResponsibleAi, armor::FilterKind::kSensitiveData,
armor::FilterKind::kPromptInjectionJailbreak, armor::FilterKind::kMaliciousUri,
armor::FilterKind::kCsam, armor::FilterKind::kVirusScan})
filters.push(armor::filter_kind_name(k));
o.set("filters", filters);
Json caps = Json::object();
caps.set("max_text_chars", static_cast<long>(armor::ArmorCaps::kMaxTextChars));
caps.set("max_findings", static_cast<long>(armor::ArmorCaps::kMaxFindings));
caps.set("max_finding_pages", static_cast<long>(armor::ArmorCaps::kMaxFindingPages));
o.set("caps", caps);
fw::Response res = fw::JsonResponse(o.dump(), 200);
res.headers.set("Cache-Control", "no-store");
return res;
});
router.post("/agent/armor/sanitize/prompt", "agent-armor-sanitize-prompt",
[armor_of, template_id](fw::Request req) {
Form in = fw::parse_json_object_flat(req.body);
std::string id = template_id(in);
std::string text = field(in, "text");
if (text.empty()) bad("text is required");
return guarded([&] { return json_out(armor_of()->sanitize_user_prompt(id, text).to_json()); });
});
router.post("/agent/armor/sanitize/response", "agent-armor-sanitize-response",
[armor_of, template_id](fw::Request req) {
Form in = fw::parse_json_object_flat(req.body);
std::string id = template_id(in);
std::string text = field(in, "text");
if (text.empty()) bad("text is required");
std::string prompt = field(in, "prompt");
return guarded(
[&] { return json_out(armor_of()->sanitize_model_response(id, text, prompt).to_json()); });
});
// GuardedModel over the local echo model: shows the typed block result.
router.post("/agent/armor/guard", "agent-armor-guard", [armor_of, template_id](fw::Request req) {
Form in = fw::parse_json_object_flat(req.body);
std::string id = template_id(in);
std::string text = field(in, "text");
if (text.empty()) bad("text is required");
return guarded([&] {
auto guard = armor::GuardedModel::with_armor(std::make_shared<EchoModel>(), armor_of(), id);
google::GenerateRequest request;
request.contents.push_back(google::Content::user(text));
Json o = Json::object();
try {
o.set("blocked", false);
o.set("reply", guard->generate(request).text());
} catch (const armor::BlockedError& e) {
o.set("blocked", true);
o.set("stage", e.stage() == armor::GuardStage::kPrompt ? "prompt" : "response");
o.set("incomplete", e.incomplete());
o.set("result", e.result().to_json());
}
return json_out(o);
});
});
router.post("/agent/armor/templates/list", "agent-armor-templates-list", [armor_of](fw::Request req) {
Form in = fw::parse_json_object_flat(req.body);
int size = int_field(in, "page_size", 0, armor::ArmorCaps::kMaxTemplatePageSize);
return guarded([&] {
armor::TemplatePage page = armor_of()->list_templates(size);
Json items = Json::array();
for (const auto& t : page.templates) items.push(template_json(t));
Json u = Json::array();
for (const auto& x : page.unreachable) u.push(x);
Json o = Json::object();
o.set("templates", items);
o.set("unreachable", u);
return json_out(o);
});
});
router.post("/agent/armor/templates/get", "agent-armor-templates-get",
[armor_of, template_id](fw::Request req) {
Form in = fw::parse_json_object_flat(req.body);
std::string id = template_id(in);
return guarded([&] { return json_out(template_json(armor_of()->get_template(id))); });
});
// Form fields: template_id, pi (confidence or empty), rai ("HATE_SPEECH:HIGH,DANGEROUS"),
// malicious_uri, sdp_basic, enforcement.
router.post("/agent/armor/templates/create", "agent-armor-templates-create",
[armor_of, template_id](fw::Request req) {
Form in = fw::parse_json_object_flat(req.body);
std::string id = template_id(in);
armor::Template t;
t.filter.rai = rai_of(field(in, "rai"));
if (!field(in, "pi").empty()) {
t.filter.prompt_injection = true;
t.filter.prompt_injection_confidence = confidence_of(field(in, "pi"));
}
t.filter.malicious_uri = flag(in, "malicious_uri");
if (flag(in, "sdp_basic")) t.filter.sdp = armor::SdpMode::kBasic;
t.metadata.enforcement = field(in, "enforcement");
try {
t.validate();
} catch (const google::GoogleError& e) {
rethrow(e);
}
return guarded([&] { return json_out(template_json(armor_of()->create_template(id, t))); });
});
// Destructive: needs confirm=delete in the body.
router.post("/agent/armor/templates/delete", "agent-armor-templates-delete",
[armor_of, template_id](fw::Request req) {
Form in = fw::parse_json_object_flat(req.body);
std::string id = template_id(in);
if (field(in, "confirm") != "delete") bad("confirm must be \"delete\"");
return guarded([&] {
armor_of()->delete_template(id);
Json o = Json::object();
o.set("deleted", id);
return json_out(o);
});
});
router.post("/agent/armor/floor/get", "agent-armor-floor-get", [armor_of](fw::Request req) {
Form in = fw::parse_json_object_flat(req.body);
std::string scope = field(in, "scope");
if (!scope.empty() && !armor::valid_scope(scope)) bad("scope must be projects/ID, folders/ID or organizations/ID");
return guarded([&] {
armor::FloorSetting f = armor_of()->get_floor_setting(scope);
Json o = Json::object();
o.set("name", f.name);
o.set("body", f.to_json());
return json_out(o);
});
});
auto query_of = [](const Form& in) {
armor::FindingQuery q;
q.scope = field(in, "scope");
if (q.scope.empty()) {
google::CloudContext ctx = google::CloudContext::from_env();
if (!ctx.project.empty()) q.scope = "projects/" + ctx.project;
}
if (!field(in, "source").empty()) q.source = field(in, "source");
q.severity = field(in, "severity");
q.state = field(in, "state");
if (!field(in, "category").empty()) q.categories = {field(in, "category")};
q.order_by = field(in, "order_by");
q.page_size = int_field(in, "page_size", 0, armor::ArmorCaps::kMaxFindingsPageSize);
q.max_pages = static_cast<std::size_t>(int_field(in, "max_pages", 1, armor::ArmorCaps::kMaxFindingPages));
if (q.max_pages == 0) bad("max_pages must be at least 1");
try {
q.validate();
} catch (const google::GoogleError& e) {
rethrow(e);
}
return q;
};
router.post("/agent/armor/findings", "agent-armor-findings", [client, query_of](fw::Request req) {
Form in = fw::parse_json_object_flat(req.body);
armor::FindingQuery q = query_of(in);
q.resource_contains = field(in, "resource_contains");
return guarded([&] {
armor::SecurityFindings scc(client);
armor::FindingPage page = scc.list(q);
Json items = Json::array();
for (const auto& f : page.findings) items.push(f.to_json());
Json o = Json::object();
o.set("findings", items);
o.set("truncated", page.truncated);
o.set("total_size", static_cast<long>(page.total_size));
return json_out(o);
});
});
router.post("/agent/armor/dashboard", "agent-armor-dashboard", [client, query_of](fw::Request req) {
Form in = fw::parse_json_object_flat(req.body);
armor::FindingQuery q = query_of(in);
q.max_pages = std::max<std::size_t>(q.max_pages, 1);
return guarded([&] {
armor::SecurityFindings scc(client);
armor::AgentSecurityDashboard dash(scc);
return json_out(dash.summarize(q).to_json());
});
});
}
} // namespace molo::example::app