86-row call table checked against Google discovery documents, typed services, local approval ledger, live read-only tests, /agent/registry demo. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
247 lines
11 KiB
C++
247 lines
11 KiB
C++
#include "google/registry/agent_identity.h"
|
|
|
|
#include "google/errors.h"
|
|
#include "google/registry/caps.h"
|
|
#include "google/registry/ids.h"
|
|
|
|
namespace molo::google::registry {
|
|
|
|
using clients::body::Json;
|
|
|
|
namespace {
|
|
bool ends_with(const std::string& s, const std::string& suf) {
|
|
return s.size() >= suf.size() && s.compare(s.size() - suf.size(), suf.size(), suf) == 0;
|
|
}
|
|
|
|
void require_principal_ref(const std::string& p) {
|
|
bool ok = p.rfind("principal://", 0) == 0 || p.rfind("principalSet://", 0) == 0;
|
|
if (!ok || p.size() > RegistryCaps::kMaxPrincipalChars)
|
|
throw GoogleError::invalid("principal must be principal:// or principalSet://");
|
|
for (unsigned char c : p)
|
|
if (c <= 0x20 || c == '"' || c == '\\') throw GoogleError::invalid("principal has bad characters");
|
|
}
|
|
|
|
// service.googleapis.com/resource.verb, e.g. storage.googleapis.com/buckets.delete
|
|
void require_permission(const std::string& p) {
|
|
std::size_t slash = p.find('/');
|
|
if (slash == std::string::npos || slash == 0 || slash + 1 >= p.size() || p.size() > 256 ||
|
|
!ends_with(p.substr(0, slash), ".googleapis.com") || p.find('/', slash + 1) != std::string::npos)
|
|
throw GoogleError::invalid("permission must look like service.googleapis.com/resource.verb");
|
|
for (char c : p)
|
|
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' ||
|
|
c == '/' || c == '-' || c == '_' || c == '*'))
|
|
throw GoogleError::invalid("permission has bad characters");
|
|
}
|
|
|
|
Json string_array(const std::vector<std::string>& v) {
|
|
Json a = Json::array();
|
|
for (const auto& s : v) a.push(Json(s));
|
|
return a;
|
|
}
|
|
|
|
void require_digits(const std::string& v, const char* what) {
|
|
if (v.empty() || v.size() > 20) throw GoogleError::invalid(std::string(what) + " must be numeric");
|
|
for (char c : v)
|
|
if (c < '0' || c > '9') throw GoogleError::invalid(std::string(what) + " must be numeric");
|
|
}
|
|
} // namespace
|
|
|
|
Json DenyRuleSpec::to_json() const {
|
|
Ids::require_text(description, RegistryCaps::kMaxDescription, "description");
|
|
if (denied_principals.empty() || denied_principals.size() > RegistryCaps::kMaxDenyPrincipals ||
|
|
exception_principals.size() > RegistryCaps::kMaxDenyPrincipals)
|
|
throw GoogleError::invalid("a deny rule needs 1..32 denied principals");
|
|
if (denied_permissions.empty() || denied_permissions.size() > RegistryCaps::kMaxDenyPermissions ||
|
|
exception_permissions.size() > RegistryCaps::kMaxDenyPermissions)
|
|
throw GoogleError::invalid("a deny rule needs 1..64 denied permissions");
|
|
for (const auto& p : denied_principals) require_principal_ref(p);
|
|
for (const auto& p : exception_principals) require_principal_ref(p);
|
|
for (const auto& p : denied_permissions) require_permission(p);
|
|
for (const auto& p : exception_permissions) require_permission(p);
|
|
Json deny = Json::object();
|
|
deny.set("deniedPrincipals", string_array(denied_principals));
|
|
deny.set("deniedPermissions", string_array(denied_permissions));
|
|
if (!exception_principals.empty()) deny.set("exceptionPrincipals", string_array(exception_principals));
|
|
if (!exception_permissions.empty())
|
|
deny.set("exceptionPermissions", string_array(exception_permissions));
|
|
Json rule = Json::object();
|
|
if (!description.empty()) rule.set("description", Json(description));
|
|
rule.set("denyRule", deny);
|
|
return rule;
|
|
}
|
|
|
|
Json DenyPolicySpec::to_json() const {
|
|
Ids::require_text(display_name, RegistryCaps::kMaxDisplayName, "displayName");
|
|
if (rules.empty() || rules.size() > RegistryCaps::kMaxPolicyRules)
|
|
throw GoogleError::invalid("a deny policy needs 1..5 rules");
|
|
Json j = Json::object();
|
|
if (!display_name.empty()) j.set("displayName", Json(display_name));
|
|
Json r = Json::array();
|
|
for (const auto& rule : rules) r.push(rule.to_json());
|
|
j.set("rules", r);
|
|
return j;
|
|
}
|
|
|
|
Json AccessBoundarySpec::to_json() const {
|
|
Ids::require_text(display_name, RegistryCaps::kMaxDisplayName, "displayName");
|
|
Ids::require_text(rule_description, RegistryCaps::kMaxDescription, "rule description");
|
|
if (resources.empty() || resources.size() > RegistryCaps::kMaxPolicyTargets)
|
|
throw GoogleError::invalid("a boundary needs 1..16 resources");
|
|
for (const auto& r : resources) {
|
|
Ids::require_text(r, 512, "resource");
|
|
if (r.empty() || r.find(' ') != std::string::npos)
|
|
throw GoogleError::invalid("boundary resource is malformed");
|
|
}
|
|
Json rule = Json::object();
|
|
rule.set("resources", string_array(resources));
|
|
rule.set("effect", Json("ALLOW"));
|
|
if (!rule_description.empty()) rule.set("description", Json(rule_description));
|
|
Json rules = Json::array();
|
|
rules.push(rule);
|
|
Json details = Json::object();
|
|
details.set("rules", rules);
|
|
Json j = Json::object();
|
|
if (!display_name.empty()) j.set("displayName", Json(display_name));
|
|
j.set("details", details);
|
|
return j;
|
|
}
|
|
|
|
ServiceEndpoint AgentIdentity::default_endpoint(const std::string& location) {
|
|
return endpoints::aiplatform(location);
|
|
}
|
|
|
|
AgentIdentity::AgentIdentity(std::shared_ptr<GoogleClient> client, CloudContext context)
|
|
: RegistryService(std::move(client), default_endpoint(context.location), context) {}
|
|
|
|
Json AgentIdentity::engines() const {
|
|
return runner_.list("aiplatform.v1.reasoningEngines.list", "reasoningEngines",
|
|
with("parent", context_.parent()));
|
|
}
|
|
|
|
IdentityInfo AgentIdentity::identity(const std::string& engine_id) const {
|
|
Json doc = runner_.invoke("aiplatform.v1.reasoningEngines.get",
|
|
with("name", context_.child("reasoningEngines", engine_id)));
|
|
IdentityInfo info;
|
|
info.engine = string_at(doc, "name");
|
|
if (const Json* spec = doc.find("spec"); spec && spec->is_object()) {
|
|
info.identity_type = string_at(*spec, "identityType");
|
|
info.effective = string_at(*spec, "effectiveIdentity");
|
|
}
|
|
info.agent_identity = info.identity_type == "AGENT_IDENTITY";
|
|
if (info.agent_identity && !info.effective.empty())
|
|
info.principal = info.effective.rfind("principal://", 0) == 0
|
|
? info.effective
|
|
: "principal://" + info.effective;
|
|
return info;
|
|
}
|
|
|
|
Json AgentIdentity::engine_iam_policy(const std::string& engine_id) const {
|
|
return runner_.invoke("aiplatform.v1.reasoningEngines.getIamPolicy",
|
|
with("resource", context_.child("reasoningEngines", engine_id)));
|
|
}
|
|
|
|
std::vector<AuditEvent> AgentIdentity::audit(const std::string& principal, int limit) const {
|
|
if (limit < 1 || limit > static_cast<int>(RegistryCaps::kMaxAuditEntries))
|
|
throw GoogleError::invalid("limit must be 1..500");
|
|
CallArgs a;
|
|
a.body = Json::object();
|
|
context_.parent(); // Validates the project.
|
|
a.body.set("resourceNames", [&] {
|
|
Json r = Json::array();
|
|
r.push(Json("projects/" + context_.project));
|
|
return r;
|
|
}());
|
|
a.body.set("filter", Json(AuditMapper::filter_for_agent(principal)));
|
|
a.body.set("orderBy", Json("timestamp desc"));
|
|
a.body.set("pageSize", Json(limit));
|
|
Json reply = runner_.invoke("logging.v2.entries.list", a);
|
|
const Json* entries = reply.find("entries");
|
|
return AuditMapper::map_all(entries ? *entries : Json::array());
|
|
}
|
|
|
|
std::string AgentIdentity::attachment_for_project(const std::string& n) {
|
|
require_digits(n, "project number");
|
|
return "cloudresourcemanager.googleapis.com%2Fprojects%2F" + n;
|
|
}
|
|
|
|
std::string AgentIdentity::attachment_for_organization(const std::string& org) {
|
|
require_digits(org, "organization id");
|
|
return "cloudresourcemanager.googleapis.com%2Forganizations%2F" + org;
|
|
}
|
|
|
|
std::string AgentIdentity::policy_parent(const std::string& attachment) {
|
|
if (attachment.find("%2F") == std::string::npos || attachment.size() > 256)
|
|
throw GoogleError::invalid("attachment point must be URL-encoded");
|
|
return "policies/" + attachment + "/denypolicies";
|
|
}
|
|
|
|
std::string AgentIdentity::policy_name(const std::string& attachment, const std::string& id) {
|
|
Ids::require_segment(id, "policy id");
|
|
return policy_parent(attachment) + "/" + id;
|
|
}
|
|
|
|
std::string AgentIdentity::boundary_parent(const std::string& org_id) {
|
|
require_digits(org_id, "organization id");
|
|
return "organizations/" + org_id + "/locations/global";
|
|
}
|
|
|
|
Json AgentIdentity::deny_policies(const std::string& attachment) const {
|
|
return runner_.list("iam.v2.policies.listPolicies", "policies",
|
|
with("parent", policy_parent(attachment)));
|
|
}
|
|
|
|
Json AgentIdentity::deny_policy(const std::string& attachment, const std::string& id) const {
|
|
return runner_.invoke("iam.v2.policies.get", with("name", policy_name(attachment, id)));
|
|
}
|
|
|
|
Json AgentIdentity::create_deny_policy(const std::string& attachment, const std::string& id,
|
|
const DenyPolicySpec& spec) const {
|
|
Ids::require_segment(id, "policy id");
|
|
CallArgs a = with("parent", policy_parent(attachment));
|
|
a.body = spec.to_json();
|
|
a.query["policyId"] = id;
|
|
if (exists([&] { return deny_policy(attachment, id); }))
|
|
throw GoogleError(409, "ALREADY_EXISTS", "deny policy " + id + " already exists");
|
|
runner_.mutate("iam.v2.policies.createPolicy", a);
|
|
return deny_policy(attachment, id);
|
|
}
|
|
|
|
Json AgentIdentity::delete_deny_policy(const std::string& attachment, const std::string& id,
|
|
const std::string& etag) const {
|
|
deny_policy(attachment, id);
|
|
CallArgs a = with("name", policy_name(attachment, id));
|
|
put_query(a, "etag", etag);
|
|
return runner_.mutate("iam.v2.policies.delete", a);
|
|
}
|
|
|
|
Json AgentIdentity::boundaries(const std::string& org_id) const {
|
|
return runner_.list("iam.v3.principalAccessBoundaryPolicies.list", "principalAccessBoundaryPolicies",
|
|
with("parent", boundary_parent(org_id)));
|
|
}
|
|
|
|
Json AgentIdentity::boundary(const std::string& org_id, const std::string& id) const {
|
|
Ids::require_segment(id, "policy id");
|
|
return runner_.invoke("iam.v3.principalAccessBoundaryPolicies.get",
|
|
with("name", boundary_parent(org_id) + "/principalAccessBoundaryPolicies/" + id));
|
|
}
|
|
|
|
Json AgentIdentity::create_boundary(const std::string& org_id, const std::string& id,
|
|
const AccessBoundarySpec& spec) const {
|
|
Ids::require_segment(id, "policy id");
|
|
CallArgs a = with("parent", boundary_parent(org_id));
|
|
a.body = spec.to_json();
|
|
a.query["principalAccessBoundaryPolicyId"] = id;
|
|
if (exists([&] { return boundary(org_id, id); }))
|
|
throw GoogleError(409, "ALREADY_EXISTS", "boundary " + id + " already exists");
|
|
runner_.mutate("iam.v3.principalAccessBoundaryPolicies.create", a);
|
|
return boundary(org_id, id);
|
|
}
|
|
|
|
Json AgentIdentity::delete_boundary(const std::string& org_id, const std::string& id) const {
|
|
boundary(org_id, id);
|
|
return runner_.mutate("iam.v3.principalAccessBoundaryPolicies.delete",
|
|
with("name", boundary_parent(org_id) + "/principalAccessBoundaryPolicies/" + id));
|
|
}
|
|
|
|
} // namespace molo::google::registry
|