#include "google/armor/findings.h" #include #include "google/armor/armor_util.h" #include "google/armor/caps.h" #include "google/errors.h" #include "google/resource.h" namespace molo::google::armor { using clients::body::Json; namespace { const Json* member(const Json& doc, const char* key) { return doc.is_object() ? doc.find(key) : nullptr; } std::string text_of(const Json& doc, const char* key) { const Json* v = member(doc, key); return v && v->is_string() ? v->as_string() : std::string(); } // Category names, resource fragments: letters, digits and a few separators. bool safe_value(const std::string& v) { if (v.empty() || v.size() > ArmorCaps::kMaxFilterValueChars) return false; for (char c : v) if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c == '.' || c == '/' || c == ':')) return false; return true; } // Comma separated field paths: letters, digits, '_' and '.'. bool safe_path_list(const std::string& v) { if (v.empty() || v.size() > ArmorCaps::kMaxFilterValueChars) return false; for (char c : v) if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '.' || c == ',')) return false; return true; } bool one_of(const std::string& v, std::initializer_list allowed) { return std::any_of(allowed.begin(), allowed.end(), [&](const char* a) { return v == a; }); } void validate_order_by(const std::string& order_by) { std::size_t start = 0; while (start <= order_by.size()) { std::size_t comma = order_by.find(',', start); std::string part = order_by.substr( start, comma == std::string::npos ? std::string::npos : comma - start); std::size_t space = part.find(' '); std::string field = part.substr(0, space); std::string suffix = space == std::string::npos ? "" : part.substr(space + 1); // Fields the API documents for orderBy (securitycenter v2 findings.list). if (!one_of(field, {"name", "parent", "state", "category", "resourceName", "eventTime", "createTime", "severity", "findingClass"}) || (!suffix.empty() && suffix != "desc")) throw GoogleError::invalid("bad order_by"); if (comma == std::string::npos) break; start = comma + 1; } } std::vector strings_of(const Json& doc, const char* key, const char* field) { std::vector out; const Json* arr = member(doc, key); if (!arr || !arr->is_array()) return out; for (const Json& x : arr->as_array()) if (out.size() < ArmorCaps::kMaxDetailsPerFilter) out.push_back(text_of(x, field)); return out; } } // namespace Json Finding::to_json() const { Json o = Json::object(); o.set("name", name); o.set("category", category); o.set("state", state); o.set("severity", severity); o.set("class", finding_class); o.set("mute", mute); o.set("resource", resource_name); o.set("event_time", event_time); o.set("description", description); o.set("external_uri", external_uri); if (is_agent_finding()) { Json a = Json::object(); a.set("id", agent_id); a.set("display_name", agent_display_name); a.set("anomaly", has_anomaly); if (has_anomaly) { a.set("confidence", anomaly_confidence); Json d = Json::array(); for (const auto& x : anomaly_detectors) { Json item = Json::object(); item.set("detector_id", x.detector_id); item.set("display_name", x.display_name); item.set("severity", x.severity); item.set("explanation", x.explanation); item.set("recommendation", x.recommendation); d.push(item); } a.set("detectors", d); } o.set("agent", a); } if (!cve_id.empty()) o.set("cve", cve_id); return o; } Finding Finding::from_json(const Json& doc) { Finding f; f.name = text_of(doc, "name"); f.parent = text_of(doc, "parent"); f.category = text_of(doc, "category"); f.state = text_of(doc, "state"); f.severity = text_of(doc, "severity"); f.finding_class = text_of(doc, "findingClass"); f.mute = text_of(doc, "mute"); f.resource_name = text_of(doc, "resourceName"); f.event_time = text_of(doc, "eventTime"); f.create_time = text_of(doc, "createTime"); f.description = text_of(doc, "description"); f.external_uri = text_of(doc, "externalUri"); f.module_name = text_of(doc, "moduleName"); if (const Json* a = member(doc, "agent")) { f.agent_id = text_of(*a, "id"); f.agent_display_name = text_of(*a, "displayName"); } if (const Json* an = member(doc, "agentAnomaly")) { f.has_anomaly = true; if (const Json* c = member(*an, "confidenceScore"); c && c->is_number()) f.anomaly_confidence = c->as_number(); f.anomaly_invocation_ids = strings_of(*an, "invocationReferences", "invocationId"); if (const Json* det = member(*an, "detectorReferences"); det && det->is_array()) for (const Json& d : det->as_array()) { if (f.anomaly_detectors.size() >= ArmorCaps::kMaxDetailsPerFilter) break; f.anomaly_detectors.push_back({text_of(d, "detectorId"), text_of(d, "displayName"), text_of(d, "severity"), text_of(d, "explanation"), text_of(d, "recommendation")}); } } f.agent_session_ids = strings_of(doc, "agentSessions", "sessionId"); if (const Json* v = member(doc, "vulnerability")) { f.vulnerability = *v; if (const Json* cve = member(*v, "cve")) f.cve_id = text_of(*cve, "id"); } if (const Json* m = member(doc, "aiModel")) f.ai_model = *m; if (const Json* e = member(doc, "agentDataAccessEvents")) f.agent_data_access_events = *e; return f; } void FindingQuery::validate() const { if (!valid_scope(scope)) throw GoogleError::invalid("bad findings scope"); if (source != "-" && !valid_segment(source)) throw GoogleError::invalid("bad source id"); if (!location.empty() && !valid_segment(location)) throw GoogleError::invalid("bad location"); if (categories.size() > ArmorCaps::kMaxFilterCategories) throw GoogleError::invalid("too many categories"); for (const auto& c : categories) if (!safe_value(c)) throw GoogleError::invalid("bad category"); if (!state.empty() && !one_of(state, {"ACTIVE", "INACTIVE"})) throw GoogleError::invalid("state must be ACTIVE or INACTIVE"); if (!severity.empty() && !one_of(severity, {"CRITICAL", "HIGH", "MEDIUM", "LOW"})) throw GoogleError::invalid("severity must be CRITICAL, HIGH, MEDIUM or LOW"); if (!resource_contains.empty() && !safe_value(resource_contains)) throw GoogleError::invalid("bad resource filter"); if (!finding_class.empty() && !safe_value(finding_class)) throw GoogleError::invalid("bad finding class"); if (!order_by.empty()) validate_order_by(order_by); if (!field_mask.empty() && !safe_path_list(field_mask)) throw GoogleError::invalid("bad field mask"); if (page_size < 0 || page_size > ArmorCaps::kMaxFindingsPageSize) throw GoogleError::invalid("page_size must be 0 to 1000"); if (max_pages == 0 || max_pages > ArmorCaps::kMaxFindingPages) throw GoogleError::invalid("max_pages must be 1 to 10"); if (max_results > ArmorCaps::kMaxFindings) throw GoogleError::invalid("max_results must be at most 5000"); } std::string FindingQuery::parent_path() const { validate(); std::string path = "/" + scope + "/sources/" + source; if (!location.empty()) path += "/locations/" + location; return path; } std::string FindingQuery::filter() const { validate(); std::vector terms; if (!categories.empty()) { std::string group = "("; for (std::size_t i = 0; i < categories.size(); ++i) { if (i) group += " OR "; group += "category=\"" + categories[i] + "\""; } terms.push_back(group + ")"); } if (!state.empty()) terms.push_back("state=\"" + state + "\""); if (!severity.empty()) terms.push_back("severity=\"" + severity + "\""); if (!resource_contains.empty()) terms.push_back("resourceName:\"" + resource_contains + "\""); std::string out; for (const auto& t : terms) out += (out.empty() ? "" : " AND ") + t; return out; } Json SecuritySource::to_json() const { Json o = Json::object(); o.set("name", name); o.set("display_name", display_name); o.set("description", description); o.set("canonical_name", canonical_name); return o; } ServiceEndpoint SecurityFindings::endpoint() { return endpoints::custom("securitycenter", "https://securitycenter.googleapis.com/v2"); } SecurityFindings::SecurityFindings(std::shared_ptr client) : Service(std::move(client), endpoint()) {} FindingPage SecurityFindings::list(const FindingQuery& query) { std::string filter = query.filter(); // Validates everything. std::string base = query.parent_path() + "/findings"; int size = query.page_size > 0 ? query.page_size : ArmorCaps::kDefaultFindingsPageSize; std::string q = "?pageSize=" + std::to_string(size); if (!filter.empty()) q += "&filter=" + query_escape(filter); if (!query.order_by.empty()) q += "&orderBy=" + query_escape(query.order_by); if (!query.field_mask.empty()) q += "&fieldMask=" + query_escape(query.field_mask); std::size_t cap = query.max_results ? query.max_results : ArmorCaps::kMaxFindings; FindingPage page; std::string token; for (std::size_t pages = 0; pages < query.max_pages; ++pages) { std::string path = base + q; if (!token.empty()) path += "&pageToken=" + query_escape(token); Json doc = fetch(path); if (pages == 0) if (const Json* t = doc.find("totalSize"); t && t->is_number()) page.total_size = t->as_int(); if (const Json* items = doc.find("listFindingsResults"); items && items->is_array()) for (const Json& item : items->as_array()) { const Json* f = member(item, "finding"); if (!f) continue; Finding finding = Finding::from_json(*f); if (!query.finding_class.empty() && finding.finding_class != query.finding_class) continue; if (page.findings.size() >= cap) { page.truncated = true; return page; } page.findings.push_back(std::move(finding)); } const Json* next = doc.find("nextPageToken"); if (!next || !next->is_string() || next->as_string().empty()) return page; token = next->as_string(); } page.truncated = true; // A next page existed beyond max_pages. return page; } std::vector SecurityFindings::group(const FindingQuery& query, const std::string& group_by) { std::string filter = query.filter(); std::string path = query.parent_path() + "/findings:group"; if (!safe_path_list(group_by)) throw GoogleError::invalid("bad group_by"); Json body = Json::object(); body.set("groupBy", group_by); if (!filter.empty()) body.set("filter", filter); int size = query.page_size > 0 ? query.page_size : ArmorCaps::kDefaultFindingsPageSize; body.set("pageSize", size); std::vector out; std::string token; for (std::size_t pages = 0; pages < query.max_pages; ++pages) { if (!token.empty()) body.set("pageToken", token); Json doc = call(path, body); if (const Json* rows = doc.find("groupByResults"); rows && rows->is_array()) for (const Json& r : rows->as_array()) { if (out.size() >= ArmorCaps::kMaxGroupResults) return out; FindingGroup g; // count is an int64 string on the wire. const Json* c = member(r, "count"); if (c && c->is_string()) g.count = std::strtol(c->as_string().c_str(), nullptr, 10); else if (c && c->is_number()) g.count = c->as_int(); if (const Json* p = member(r, "properties")) g.properties = *p; out.push_back(std::move(g)); } const Json* next = doc.find("nextPageToken"); if (!next || !next->is_string() || next->as_string().empty()) break; token = next->as_string(); } return out; } std::vector SecurityFindings::list_sources(const std::string& scope) { if (!valid_scope(scope)) throw GoogleError::invalid("bad sources scope"); std::vector out; std::string token; for (int pages = 0; pages < 5; ++pages) { std::string path = "/" + scope + "/sources?pageSize=100"; if (!token.empty()) path += "&pageToken=" + query_escape(token); Json doc = fetch(path); if (const Json* items = doc.find("sources"); items && items->is_array()) for (const Json& s : items->as_array()) { if (out.size() >= ArmorCaps::kMaxSources) return out; out.push_back({text_of(s, "name"), text_of(s, "displayName"), text_of(s, "description"), text_of(s, "canonicalName")}); } const Json* next = doc.find("nextPageToken"); if (!next || !next->is_string() || next->as_string().empty()) break; token = next->as_string(); } return out; } SecuritySource SecurityFindings::get_source(const std::string& name) { // organizations/{id}/sources/{id} (the only scope with a get method). std::size_t slash = name.find("/sources/"); if (slash == std::string::npos || name.rfind("organizations/", 0) != 0 || !valid_scope(name.substr(0, slash)) || !valid_segment(name.substr(slash + 9))) throw GoogleError::invalid("source name must be organizations/{id}/sources/{id}"); Json s = fetch("/" + name); return {text_of(s, "name"), text_of(s, "displayName"), text_of(s, "description"), text_of(s, "canonicalName")}; } } // namespace molo::google::armor