|
|
@@ -5160,3 +5160,238 @@ TEST_F(ApiFixture, ThePagingFieldsArePRESENTEvenWhenNothingMatches) {
|
|
|
EXPECT_FALSE(body["has_more"].get<bool>());
|
|
|
EXPECT_TRUE(body["documents"].empty());
|
|
|
}
|
|
|
+
|
|
|
+// ===========================================================================
|
|
|
+// PER-PROJECT HOSTNAMES (SV-132). The tenancy boundary, end to end.
|
|
|
+//
|
|
|
+// Everything above this point exercises the FULL path form on a Host nobody
|
|
|
+// registered, which is every deployment today. These tests are the only place
|
|
|
+// the SHORT form and the host gate are exercised at all, so they are written as
|
|
|
+// PAIRINGS throughout: a test that only asserted the refusal would pass just as
|
|
|
+// well against a server that refused everything, which is a strictly worse
|
|
|
+// defect than the one being prevented.
|
|
|
+//
|
|
|
+// Each test restores the empty registry on the way out, because host gating is
|
|
|
+// SERVICE-WIDE and a leaked hostname would 404 every later test in this serial
|
|
|
+// suite - and the failure would look like the later test's own bug.
|
|
|
+// ===========================================================================
|
|
|
+
|
|
|
+namespace {
|
|
|
+
|
|
|
+// RAII: put the registry back to EMPTY however the test leaves.
|
|
|
+//
|
|
|
+// Host gating is SERVICE-WIDE, so a leaked hostname would 404 every later test
|
|
|
+// in this serial suite - and the failure would surface as the later test's own
|
|
|
+// bug, in a different file, with nothing pointing here. It takes the port and
|
|
|
+// key as values rather than the fixture because the fixture's members are
|
|
|
+// protected and this has to live outside the class.
|
|
|
+struct HostGatingGuard {
|
|
|
+ int port;
|
|
|
+ std::string key;
|
|
|
+ std::string project;
|
|
|
+ ~HostGatingGuard() {
|
|
|
+ httplib::Client c("127.0.0.1", port);
|
|
|
+ c.set_default_headers({{"Authorization", "Bearer " + key}});
|
|
|
+ (void)c.Put(("/api/v1/projects/" + project + "/settings/hostnames").c_str(),
|
|
|
+ nlohmann::json{{"hostnames", nlohmann::json::array()}}.dump(),
|
|
|
+ "application/json");
|
|
|
+ (void)c.Put("/api/v1/settings",
|
|
|
+ nlohmann::json{{"admin_hostname", ""},
|
|
|
+ {"global_hostnames", nlohmann::json::array()}}.dump(),
|
|
|
+ "application/json");
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+} // namespace
|
|
|
+
|
|
|
+TEST_F(ApiFixture, AProjectHostnameIsStoredAndTheRouteSaysWhetherItIsSERVED) {
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/settings/hostnames";
|
|
|
+
|
|
|
+ // Stored while gating is OFF: `served` is the field that stops the list
|
|
|
+ // looking identical in force and not in force.
|
|
|
+ auto put = admin().Put(base.c_str(),
|
|
|
+ nlohmann::json{{"hostnames", {{{"host", "tenant.example.com"}, {"path_style", "short"}}}}}.dump(),
|
|
|
+ "application/json");
|
|
|
+ ASSERT_TRUE(put); ASSERT_EQ(put->status, 200) << put->body;
|
|
|
+ auto j = nlohmann::json::parse(put->body);
|
|
|
+ ASSERT_EQ(j.at("hostnames").size(), 1u);
|
|
|
+ EXPECT_EQ(j.at("hostnames")[0].at("host"), "tenant.example.com");
|
|
|
+ EXPECT_EQ(j.at("hostnames")[0].at("path_style"), "short");
|
|
|
+ EXPECT_FALSE(j.at("served").get<bool>()) << "no deployment hostname is set, so nothing is gated yet";
|
|
|
+
|
|
|
+ // It SURVIVES, which is the regression this pins: hasAnyOverrides() has to
|
|
|
+ // count hostnames, or the PUT's own removeProject() deletes the document it
|
|
|
+ // just wrote and the hostname silently does not exist.
|
|
|
+ auto get = admin().Get(base.c_str());
|
|
|
+ ASSERT_EQ(get->status, 200);
|
|
|
+ ASSERT_EQ(nlohmann::json::parse(get->body).at("hostnames").size(), 1u)
|
|
|
+ << "the hostname did not survive the write - hasAnyOverrides() is probably ignoring it";
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(ApiFixture, AProjectHostnameServesItsOWNProjectOnTheSHORTPathAndNOTAnotherProjects) {
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
+ const std::string other = "svapitest_otherhost";
|
|
|
+ db_->dropProject(other);
|
|
|
+ ASSERT_EQ(admin().Post("/api/v1/projects", nlohmann::json{{"name", other}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/hostnames").c_str(),
|
|
|
+ nlohmann::json{{"hostnames", {{{"host", "tenant.example.com"}, {"path_style", "short"}}}}}.dump(),
|
|
|
+ "application/json")->status, 200);
|
|
|
+ // Turning gating ON is a separate step, and it is what makes the rest real.
|
|
|
+ ASSERT_EQ(admin().Put("/api/v1/settings",
|
|
|
+ nlohmann::json{{"admin_hostname", "admin.example.com"}}.dump(), "application/json")->status, 200);
|
|
|
+
|
|
|
+ const std::string key = adminKey_;
|
|
|
+
|
|
|
+ // (1) THE SHORT FORM WORKS on the project hostname, and resolves to that
|
|
|
+ // project. This is the arm nothing else in the suite covers.
|
|
|
+ auto shortForm = request("GET", "/api/v1/collections", "", "", key,
|
|
|
+ {{"Host", "tenant.example.com"}});
|
|
|
+ ASSERT_TRUE(shortForm);
|
|
|
+ EXPECT_EQ(shortForm->status, 200) << "the short form did not resolve: " << shortForm->body;
|
|
|
+
|
|
|
+ // (2) THE FULL FORM naming its OWN project also works there.
|
|
|
+ auto ownFull = request("GET", "/api/v1/projects/" + project_ + "/collections", "", "", key,
|
|
|
+ {{"Host", "tenant.example.com"}});
|
|
|
+ ASSERT_TRUE(ownFull);
|
|
|
+ EXPECT_EQ(ownFull->status, 200) << ownFull->body;
|
|
|
+
|
|
|
+ // (3) THE CROSS-TENANT REFUSAL - the whole point. The SAME admin key, which
|
|
|
+ // is granted every project, naming ANOTHER project on this hostname. The
|
|
|
+ // key is deliberately held constant so this cannot pass for the wrong
|
|
|
+ // reason: it is the hostname refusing, not the authorization.
|
|
|
+ auto crossTenant = request("GET", "/api/v1/projects/" + other + "/collections", "", "", key,
|
|
|
+ {{"Host", "tenant.example.com"}});
|
|
|
+ ASSERT_TRUE(crossTenant);
|
|
|
+ EXPECT_EQ(crossTenant->status, 404)
|
|
|
+ << "a project hostname served ANOTHER project to a key that happens to hold both: "
|
|
|
+ << crossTenant->body;
|
|
|
+
|
|
|
+ // (4) THE PAIRING for (3): the same request on the ADMIN hostname works.
|
|
|
+ // Without this, (3) would pass against a server that had simply broken the
|
|
|
+ // route for everyone.
|
|
|
+ auto adminHost = request("GET", "/api/v1/projects/" + other + "/collections", "", "", key,
|
|
|
+ {{"Host", "admin.example.com"}});
|
|
|
+ ASSERT_TRUE(adminHost);
|
|
|
+ EXPECT_EQ(adminHost->status, 200)
|
|
|
+ << "the admin hostname must still reach every project: " << adminHost->body;
|
|
|
+
|
|
|
+ // (5) A SHORT path on the ADMIN hostname names NO project and is refused -
|
|
|
+ // rule 4, the load-bearing one. Falling back to a default here would be the
|
|
|
+ // cross-tenant hole wearing routing's clothes.
|
|
|
+ auto shortOnAdmin = request("GET", "/api/v1/collections", "", "", key,
|
|
|
+ {{"Host", "admin.example.com"}});
|
|
|
+ ASSERT_TRUE(shortOnAdmin);
|
|
|
+ EXPECT_EQ(shortOnAdmin->status, 404)
|
|
|
+ << "a short path on the admin hostname resolved to some project: " << shortOnAdmin->body;
|
|
|
+
|
|
|
+ // (6) AN UNREGISTERED Host is refused outright, with a valid key.
|
|
|
+ auto unknownHost = request("GET", "/api/v1/projects/" + project_ + "/collections", "", "", key,
|
|
|
+ {{"Host", "nobody.example.com"}});
|
|
|
+ ASSERT_TRUE(unknownHost);
|
|
|
+ EXPECT_EQ(unknownHost->status, 404) << unknownHost->body;
|
|
|
+
|
|
|
+ // (7) THE ADMIN SURFACE IS NARROWER on a project hostname: no web UI, no
|
|
|
+ // docs catalogue - the surface withdrawn from the customer-facing name in
|
|
|
+ // 0.15.0 because it publishes a list of what to come back and try. Paired
|
|
|
+ // against the admin hostname so this cannot pass by everything 404ing.
|
|
|
+ auto docsOnProject = request("GET", "/docs", "", "", key, {{"Host", "tenant.example.com"}});
|
|
|
+ ASSERT_TRUE(docsOnProject);
|
|
|
+ EXPECT_EQ(docsOnProject->status, 404) << "the docs catalogue is served on a project hostname";
|
|
|
+ auto healthOnProject = request("GET", "/healthz", "", "", key, {{"Host", "tenant.example.com"}});
|
|
|
+ ASSERT_TRUE(healthOnProject);
|
|
|
+ EXPECT_EQ(healthOnProject->status, 200)
|
|
|
+ << "/healthz must stay reachable everywhere so a monitor needs no credential";
|
|
|
+
|
|
|
+ db_->dropProject(other);
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(ApiFixture, AHostnameHeldByAnotherProjectIsREFUSEDAtWriteTime) {
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
+ const std::string other = "svapitest_hostclash";
|
|
|
+ db_->dropProject(other);
|
|
|
+ ASSERT_EQ(admin().Post("/api/v1/projects", nlohmann::json{{"name", other}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/hostnames").c_str(),
|
|
|
+ nlohmann::json{{"hostnames", {"taken.example.com"}}}.dump(), "application/json")->status, 200);
|
|
|
+
|
|
|
+ // Refused at write time rather than resolved at read time: the read-time
|
|
|
+ // resolution is "serve neither", which would turn this typo into an outage
|
|
|
+ // for the project that had the name first.
|
|
|
+ auto clash = admin().Put(("/api/v1/projects/" + other + "/settings/hostnames").c_str(),
|
|
|
+ nlohmann::json{{"hostnames", {"taken.example.com"}}}.dump(), "application/json");
|
|
|
+ ASSERT_TRUE(clash);
|
|
|
+ EXPECT_EQ(clash->status, 422) << clash->body;
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(clash->body).at("error").at("code"), "hostname_taken");
|
|
|
+
|
|
|
+ // PAIRING: a free name on the same project in the same breath succeeds, so
|
|
|
+ // the refusal is about the collision and not about the project or the route.
|
|
|
+ auto free_ = admin().Put(("/api/v1/projects/" + other + "/settings/hostnames").c_str(),
|
|
|
+ nlohmann::json{{"hostnames", {"free.example.com"}}}.dump(), "application/json");
|
|
|
+ ASSERT_TRUE(free_);
|
|
|
+ EXPECT_EQ(free_->status, 200) << free_->body;
|
|
|
+
|
|
|
+ // And re-claiming one's OWN name is idempotent, so the web UI's
|
|
|
+ // read-edit-PUT-the-whole-list cycle does not fail on its second save.
|
|
|
+ auto again = admin().Put(("/api/v1/projects/" + project_ + "/settings/hostnames").c_str(),
|
|
|
+ nlohmann::json{{"hostnames", {"taken.example.com"}}}.dump(), "application/json");
|
|
|
+ ASSERT_TRUE(again);
|
|
|
+ EXPECT_EQ(again->status, 200) << again->body;
|
|
|
+
|
|
|
+ (void)admin().Put(("/api/v1/projects/" + other + "/settings/hostnames").c_str(),
|
|
|
+ nlohmann::json{{"hostnames", nlohmann::json::array()}}.dump(), "application/json");
|
|
|
+ db_->dropProject(other);
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(ApiFixture, AWildcardAndTheDeploymentsOwnNameAreRefusedWithTHEIROWNSlugs) {
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/settings/hostnames";
|
|
|
+
|
|
|
+ // Three slugs rather than one, because the remedies differ: change the
|
|
|
+ // value, negotiate for the name, or you cannot have it at all.
|
|
|
+ auto wild = admin().Put(base.c_str(),
|
|
|
+ nlohmann::json{{"hostnames", {"*.example.com"}}}.dump(), "application/json");
|
|
|
+ ASSERT_TRUE(wild); EXPECT_EQ(wild->status, 422);
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(wild->body).at("error").at("code"), "invalid_hostname")
|
|
|
+ << "a wildcard must be refused in the REGISTRY - it belongs in a certificate, where a "
|
|
|
+ "wrong match fails a handshake instead of serving data";
|
|
|
+
|
|
|
+ ASSERT_EQ(admin().Put("/api/v1/settings",
|
|
|
+ nlohmann::json{{"admin_hostname", "admin.example.com"}}.dump(), "application/json")->status, 200);
|
|
|
+ auto reserved = request("PUT", base, nlohmann::json{{"hostnames", {"admin.example.com"}}}.dump(),
|
|
|
+ "application/json", adminKey_, {{"Host", "admin.example.com"}});
|
|
|
+ ASSERT_TRUE(reserved); EXPECT_EQ(reserved->status, 422) << reserved->body;
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(reserved->body).at("error").at("code"), "hostname_reserved")
|
|
|
+ << "a project claiming the admin hostname would be handed the web UI and the docs catalogue";
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(ApiFixture, TheGlobalStatsRouteSurvivesTheProjectRouteWideningBesideIt) {
|
|
|
+ // A REGRESSION TEST FOR A COLLISION CLASS, not for one route.
|
|
|
+ //
|
|
|
+ // `/api/v1/projects/(p)/stats` must NOT be widened to the optional-project
|
|
|
+ // form, because its short shape is `/api/v1/stats` - the GLOBAL admin stats
|
|
|
+ // route registered 11 lines below it in the same file. Widened, the project
|
|
|
+ // handler swallows every request to the global one and it becomes
|
|
|
+ // unreachable. The same trap applies to the bare `/api/v1/projects/(p)`,
|
|
|
+ // whose short shape is the literal `/api/v1`.
|
|
|
+ //
|
|
|
+ // This asserts the BEHAVIOUR rather than the pattern text, so it still
|
|
|
+ // fires if someone widens the route in a different spelling.
|
|
|
+ auto r = admin().Get("/api/v1/stats");
|
|
|
+ ASSERT_TRUE(r); ASSERT_EQ(r->status, 200) << r->body;
|
|
|
+ auto j = nlohmann::json::parse(r->body);
|
|
|
+ EXPECT_TRUE(j.contains("total_documents"))
|
|
|
+ << "/api/v1/stats answered with something other than the global stats shape - the "
|
|
|
+ "per-project stats route has probably been widened and is now swallowing it: " << r->body;
|
|
|
+ EXPECT_FALSE(j.contains("project"))
|
|
|
+ << "/api/v1/stats was answered by the PER-PROJECT handler with an empty project: " << r->body;
|
|
|
+
|
|
|
+ // PAIRING: the per-project route still works in its full form, so this
|
|
|
+ // cannot pass because project stats broke.
|
|
|
+ auto p = admin().Get(("/api/v1/projects/" + project_ + "/stats").c_str());
|
|
|
+ ASSERT_TRUE(p); ASSERT_EQ(p->status, 200) << p->body;
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(p->body).at("project"), project_);
|
|
|
+}
|