// Registry gap closing: lists that report truncation loudly, v1alpha twins of // every v1 collection, operations of the gateway and policy hosts, IAM on // authz policies, deny policy update, boundary patch and bindings search. // Local only: the paging state machine is fed synthetic documents (it is local // logic, not a claim about Google's replies) and requests are inspected as the // mock saw them. Nothing answers. #include #include #include #include #include "framework.h" #include "clients/transports/mock_transport.h" #include "google/credentials.h" #include "google/registry/agent_gateway.h" #include "google/registry/agent_identity.h" #include "google/registry/agent_registry.h" #include "google/registry/approvals.h" #include "google/registry/call_runner.h" #include "google/registry/caps.h" #include "google/registry/gateway_policy.h" using namespace molo::google; using namespace molo::google::registry; using molo::clients::body::Json; using molo::clients::transports::MockTransport; namespace { struct Rig { MockTransport* mock; std::shared_ptr client; explicit Rig(bool with_credential = true) { auto t = std::make_unique(); mock = t.get(); client = std::make_shared( std::move(t), with_credential ? Credentials::bearer("tok") : nullptr); } CloudContext ctx() const { CloudContext c; c.project = "p"; c.location = "us-central1"; return c; } }; bool has(const std::string& hay, const std::string& needle) { return hay.find(needle) != std::string::npos; } // Runs fn (which fails: the mock has no expectation) and returns the first // request that reached the transport during it. MockTransport::SeenRequest first_sent(Rig& rig, const std::function& fn) { std::size_t before = rig.mock->seen().size(); try { fn(); } catch (...) { } auto all = rig.mock->seen(); if (all.size() <= before) FAIL("no request reached the transport"); return all[before]; } template bool invalid_without_traffic(Rig& r, F&& f) { std::size_t before = r.mock->send_count(); try { f(); } catch (const GoogleError& e) { return e.status() == "INVALID_ARGUMENT" && r.mock->send_count() == before; } return false; } Json page(int items, const std::string& next) { Json arr = Json::array(); for (int i = 0; i < items; ++i) { Json o = Json::object(); o.set("name", Json("x/" + std::to_string(i))); arr.push(o); } Json doc = Json::object(); doc.set("agents", arr); if (!next.empty()) doc.set("nextPageToken", Json(next)); return doc; } } // namespace TEST(registry_lists, pager_follows_pages_until_the_server_stops) { ListPager p("agents", 10, 1000); ASSERT_TRUE(p.feed(page(100, "t1"))); ASSERT_EQ(p.token(), std::string("t1")); ASSERT_FALSE(p.feed(page(7, ""))); ASSERT_EQ(p.result().size(), std::size_t(107)); ASSERT_EQ(p.result().pages, std::size_t(2)); ASSERT_FALSE(p.result().truncated); ASSERT_EQ(p.result().next_page_token, std::string()); } TEST(registry_lists, pager_reports_truncation_at_the_page_cap_loudly) { ListPager p("agents", 3, 1000); ASSERT_TRUE(p.feed(page(100, "t1"))); ASSERT_TRUE(p.feed(page(100, "t2"))); ASSERT_FALSE(p.feed(page(100, "t3"))); // third page used, a fourth exists ASSERT_TRUE(p.result().truncated); ASSERT_EQ(p.result().next_page_token, std::string("t3")); ASSERT_EQ(p.result().size(), std::size_t(300)); Json j = p.result().to_json(); ASSERT_TRUE(j.find("truncated")->as_bool()); ASSERT_EQ(j.find("next_page_token")->as_string(), std::string("t3")); ASSERT_EQ(j.find("count")->as_int(), 300); ASSERT_EQ(j.find("cap_items")->as_int(), 1000); } TEST(registry_lists, pager_reports_truncation_at_the_item_cap_loudly) { ListPager p("agents", 100, 250); ASSERT_TRUE(p.feed(page(100, "t1"))); ASSERT_TRUE(p.feed(page(100, "t2"))); ASSERT_FALSE(p.feed(page(100, "t3"))); // 300 returned, 250 allowed ASSERT_TRUE(p.result().truncated); ASSERT_EQ(p.result().size(), std::size_t(250)); ASSERT_EQ(p.result().next_page_token, std::string()); // no exact continuation } TEST(registry_lists, a_list_that_ends_exactly_at_the_cap_is_not_truncated) { ListPager p("agents", 2, 200); ASSERT_TRUE(p.feed(page(100, "t1"))); ASSERT_FALSE(p.feed(page(100, ""))); ASSERT_FALSE(p.result().truncated); ASSERT_EQ(p.result().size(), std::size_t(200)); } TEST(registry_lists, caps_are_the_stated_ceiling) { ASSERT_EQ(RegistryCaps::kMaxItems, std::size_t(1000)); ASSERT_EQ(RegistryCaps::kMaxPages, std::size_t(10)); ListPager p("agents", RegistryCaps::kMaxPages, RegistryCaps::kMaxItems); for (std::size_t i = 0; i + 1 < RegistryCaps::kMaxPages; ++i) ASSERT_TRUE(p.feed(page(100, "t" + std::to_string(i)))); ASSERT_FALSE(p.feed(page(100, "more"))); ASSERT_TRUE(p.result().truncated); // 1000 items and more existed } TEST(registry_lists, the_caller_cannot_page_by_hand) { Rig rig; AgentRegistry reg(rig.client, rig.ctx()); ASSERT_TRUE(invalid_without_traffic(rig, [&] { CallArgs a; a.vars["parent"] = "projects/p/locations/us-central1"; a.query["pageToken"] = "x"; reg.runner().list("agentregistry.v1.agents.list", "agents", a); })); ASSERT_TRUE(invalid_without_traffic(rig, [&] { reg.list(AssetKind::kAgents, {{"pageToken", "x"}}); })); } TEST(registry_lists, a_first_page_request_carries_the_stated_page_size) { Rig rig; AgentRegistry reg(rig.client, rig.ctx()); auto r = first_sent(rig, [&] { reg.list(AssetKind::kAgents); }); ASSERT_EQ(r.method, std::string("GET")); ASSERT_TRUE(has(r.target, "/v1/projects/p/locations/us-central1/agents?pageSize=100")); } TEST(registry_lists, approved_listing_keeps_the_truncation_flag_in_its_type) { // list_approved returns ListResult: the flag cannot be dropped silently. static_assert(std::is_same_v().list_approved( AssetKind::kAgents, std::declval())), ListResult>); static_assert(std::is_same_v().list()), ListResult>); static_assert(std::is_same_v().list()), ListResult>); static_assert(std::is_same_v().deny_policies("a%2Fb")), ListResult>); ASSERT_TRUE(true); } TEST(registry_versions, v1alpha_twins_exist_for_every_v1_collection) { std::set ids; for (const CallSpec& c : Calls::all()) ids.insert(c.id); std::size_t twins = 0; for (const CallSpec& c : Calls::all()) { std::string id = c.id; const std::string v1 = "agentregistry.v1."; if (id.rfind(v1, 0) != 0) continue; ASSERT_TRUE(ids.count("agentregistry.v1alpha." + id.substr(v1.size())) == 1); ++twins; } ASSERT_TRUE(twins >= 35); } TEST(registry_versions, version_picks_the_host_path_and_rows) { Rig rig; AgentRegistry v1(rig.client, rig.ctx()); AgentRegistry alpha(rig.client, rig.ctx(), RegistryVersion::kV1Alpha); ASSERT_EQ(v1.version(), std::string("v1")); ASSERT_EQ(alpha.version(), std::string("v1alpha")); ASSERT_EQ(alpha.default_endpoint(RegistryVersion::kV1Alpha).base_url, std::string("https://agentregistry.googleapis.com/v1alpha")); auto r = first_sent(rig, [&] { alpha.list(AssetKind::kMcpServers); }); ASSERT_TRUE(has(r.target, "/v1alpha/projects/p/locations/us-central1/mcpServers?pageSize=100")); r = first_sent(rig, [&] { alpha.get(AssetKind::kAgents, "a1"); }); ASSERT_TRUE(has(r.target, "/v1alpha/projects/p/locations/us-central1/agents/a1")); r = first_sent(rig, [&] { alpha.operations(); }); ASSERT_TRUE(has(r.target, "/v1alpha/projects/p/locations/us-central1/operations")); r = first_sent(rig, [&] { v1.operations(); }); ASSERT_TRUE(has(r.target, "/v1/projects/p/locations/us-central1/operations")); // Skills exist in v1alpha only for CRUD, whichever version is chosen. r = first_sent(rig, [&] { v1.get(AssetKind::kSkills, "s1"); }); ASSERT_TRUE(has(r.target, "/v1alpha/projects/p/locations/us-central1/skills/s1")); // Validation still happens before traffic in v1alpha. ASSERT_TRUE(invalid_without_traffic(rig, [&] { alpha.get(AssetKind::kAgents, "a/b"); })); } TEST(registry_operations, gateway_and_policy_hosts_wrap_all_four_operation_methods) { Rig rig; AgentGateway gw(rig.client, rig.ctx()); GatewayPolicies pol(rig.client, rig.ctx()); auto r = first_sent(rig, [&] { gw.operations(); }); ASSERT_TRUE(has(r.target, "/v1/projects/p/locations/us-central1/operations")); r = first_sent(rig, [&] { pol.operations(); }); ASSERT_TRUE(has(r.target, "/v1/projects/p/locations/us-central1/operations")); // cancel and delete read the operation first. r = first_sent(rig, [&] { gw.cancel_operation("op-1"); }); ASSERT_EQ(r.method, std::string("GET")); ASSERT_TRUE(has(r.target, "/operations/op-1")); r = first_sent(rig, [&] { pol.delete_operation("op-1"); }); ASSERT_EQ(r.method, std::string("GET")); ASSERT_TRUE(invalid_without_traffic(rig, [&] { gw.cancel_operation("a/b"); })); ASSERT_TRUE(invalid_without_traffic(rig, [&] { gw.delete_operation(".."); })); ASSERT_TRUE(invalid_without_traffic(rig, [&] { pol.cancel_operation(""); })); ASSERT_TRUE(invalid_without_traffic(rig, [&] { pol.delete_operation("x?y"); })); Rig off(false); AgentGateway n(off.client, off.ctx()); ASSERT_THROW(n.operations(), NotConfiguredError); ASSERT_EQ(off.mock->send_count(), std::size_t(0)); } TEST(registry_policies, iam_on_authz_policies_is_validated_and_read_first) { Rig rig; GatewayPolicies pol(rig.client, rig.ctx()); auto r = first_sent(rig, [&] { pol.set_iam_policy("pol-1", Json::parse("{\"bindings\":[]}")); }); ASSERT_EQ(r.method, std::string("GET")); // read before write ASSERT_TRUE(has(r.target, "/authzPolicies/pol-1")); r = first_sent(rig, [&] { pol.test_iam_permissions("pol-1", {"networksecurity.authzPolicies.get"}); }); ASSERT_EQ(r.method, std::string("POST")); ASSERT_TRUE(has(r.target, "/authzPolicies/pol-1:testIamPermissions")); ASSERT_TRUE(has(r.body, "\"permissions\":[\"networksecurity.authzPolicies.get\"]")); ASSERT_TRUE(invalid_without_traffic(rig, [&] { pol.set_iam_policy("pol-1", Json::array()); })); ASSERT_TRUE(invalid_without_traffic(rig, [&] { pol.set_iam_policy("a/b", Json::object()); })); ASSERT_TRUE(invalid_without_traffic(rig, [&] { pol.test_iam_permissions("pol-1", {}); })); ASSERT_TRUE(invalid_without_traffic(rig, [&] { pol.test_iam_permissions("pol-1", std::vector(65, "p.q")); })); } TEST(registry_identity, deny_policy_update_boundary_patch_and_operations) { Rig rig; AgentIdentity id(rig.client, rig.ctx()); std::string attach = AgentIdentity::attachment_for_project("123"); DenyPolicySpec spec; spec.display_name = "deny"; DenyRuleSpec rule; rule.denied_principals = {"principalSet://goog/group/a@b.c"}; rule.denied_permissions = {"storage.googleapis.com/buckets.delete"}; spec.rules = {rule}; spec.etag = "abc"; ASSERT_TRUE(has(spec.to_json().dump(), "\"etag\":\"abc\"")); spec.etag.clear(); ASSERT_FALSE(has(spec.to_json().dump(), "etag")); // update reads first (needs the etag and a clear 404). auto r = first_sent(rig, [&] { id.update_deny_policy(attach, "p1", spec); }); ASSERT_EQ(r.method, std::string("GET")); ASSERT_TRUE(has(r.target, "/v2/policies/" + attach + "/denypolicies/p1")); ASSERT_TRUE(invalid_without_traffic(rig, [&] { id.update_deny_policy("plain", "p1", spec); })); ASSERT_TRUE(invalid_without_traffic(rig, [&] { id.update_deny_policy(attach, "a/b", spec); })); r = first_sent(rig, [&] { id.deny_policy_operation("policies/" + attach + "/operations/o1"); }); ASSERT_TRUE(has(r.target, "/v2/policies/" + attach + "/operations/o1")); ASSERT_TRUE(invalid_without_traffic(rig, [&] { id.deny_policy_operation("operations/o1"); })); ASSERT_TRUE(invalid_without_traffic(rig, [&] { id.deny_policy_operation("policies/x"); })); r = first_sent(rig, [&] { id.engine_operation("op9"); }); ASSERT_TRUE(has(r.target, "/v1/projects/p/locations/us-central1/operations/op9")); ASSERT_TRUE(invalid_without_traffic(rig, [&] { id.engine_operation("a/b"); })); AccessBoundarySpec b; b.display_name = "pab"; b.resources = {"//cloudresourcemanager.googleapis.com/projects/p"}; r = first_sent(rig, [&] { id.update_boundary("42", "pab1", b, {"displayName"}); }); ASSERT_EQ(r.method, std::string("GET")); // exists first ASSERT_TRUE(has(r.target, "/v3/organizations/42/locations/global/principalAccessBoundaryPolicies/pab1")); ASSERT_TRUE(invalid_without_traffic(rig, [&] { id.update_boundary("42", "pab1", b, {}); })); ASSERT_TRUE(invalid_without_traffic(rig, [&] { id.update_boundary("4x", "pab1", b, {"displayName"}); })); ASSERT_TRUE(invalid_without_traffic(rig, [&] { id.update_boundary("42", "pab1", b, {"name"}); })); r = first_sent(rig, [&] { id.boundary_bindings("42", "pab1"); }); ASSERT_TRUE(has(r.target, "/v3/organizations/42/locations/global/principalAccessBoundaryPolicies/pab1:searchPolicyBindings?pageSize=100")); ASSERT_TRUE(invalid_without_traffic(rig, [&] { id.boundary_bindings("42", "a/b"); })); }