| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- // Hostname resolution: which surface a `Host` names and whose project it is.
- //
- // This is a TENANCY BOUNDARY, so almost every test below is about what must NOT
- // resolve. A test suite for this that only checked the happy path would pass
- // just as well against a registry that answered "the first project it could
- // find" for every unknown name - which is the single failure that matters.
- #include <gtest/gtest.h>
- #include "hostnames.hpp"
- using svapi::HostBinding;
- using svapi::HostEntry;
- using svapi::HostKind;
- using svapi::HostRegistry;
- using svapi::normaliseHost;
- using svapi::PathStyle;
- using svapi::validHostname;
- namespace {
- HostRegistry twoTenants(std::vector<std::string>* dropped = nullptr) {
- return HostRegistry::build(
- "admin.smartbotics.ai", {"global-api-01.smartbotics.ai"},
- {{"novitax", {{"nt.kb.smartbotics.ai", PathStyle::Short}}},
- {"justice-pa", {{"pa.smartbotics.ai", PathStyle::Full},
- {"assistant.smartbotics.ai", PathStyle::Short}}}},
- dropped);
- }
- } // namespace
- // --- normalisation: every arm is a spelling a caller controls ----------------
- TEST(NormaliseHost, LowercasesBecauseDnsIsCaseInsensitive) {
- EXPECT_EQ(normaliseHost("NT.KB.Smartbotics.AI"), "nt.kb.smartbotics.ai");
- }
- TEST(NormaliseHost, StripsThePort) {
- EXPECT_EQ(normaliseHost("example.com:8443"), "example.com");
- EXPECT_EQ(normaliseHost("example.com:443"), "example.com");
- }
- TEST(NormaliseHost, AnIPv6LiteralKeepsItsAddressAndLosesOnlyThePort) {
- // The trap: truncating at the FIRST colon turns `[::1]:8080` into `[`,
- // which then matches nothing - or worse, matches a registry entry somebody
- // created by pasting a bracket.
- EXPECT_EQ(normaliseHost("[::1]:8080"), "[::1]");
- EXPECT_EQ(normaliseHost("[2001:db8::5]"), "[2001:db8::5]");
- EXPECT_EQ(normaliseHost("[2001:DB8::5]:443"), "[2001:db8::5]");
- }
- TEST(NormaliseHost, StripsExactlyOneTrailingDot) {
- // `example.com.` is the fully-qualified spelling and resolves identically,
- // so a client using it must not miss.
- EXPECT_EQ(normaliseHost("example.com."), "example.com");
- // But only one: `example.com..` is not a name anybody registered, and
- // normalising it would invent a match.
- EXPECT_EQ(normaliseHost("example.com.."), "example.com.");
- }
- TEST(NormaliseHost, TrimsTheOptionalWhitespaceAHeaderMayCarry) {
- EXPECT_EQ(normaliseHost(" example.com\t"), "example.com");
- }
- // --- what a project may register --------------------------------------------
- TEST(ValidHostname, RefusesAWildcardOutright) {
- // THE one that matters. `*.smartbotics.ai` would catch every subdomain
- // nobody had claimed YET, so a tenancy boundary would move whenever an
- // unrelated DNS record appeared. Wildcards belong in certificate selection,
- // where the wrong match fails a handshake rather than serving data.
- EXPECT_FALSE(validHostname("*.smartbotics.ai"));
- EXPECT_FALSE(validHostname("*"));
- EXPECT_FALSE(validHostname("nt.*.smartbotics.ai"));
- }
- TEST(ValidHostname, RefusesAnythingThatIsNotJustAName) {
- EXPECT_FALSE(validHostname("https://nt.kb.smartbotics.ai"));
- EXPECT_FALSE(validHostname("nt.kb.smartbotics.ai/admin"));
- EXPECT_FALSE(validHostname("nt.kb.smartbotics.ai:8443"));
- EXPECT_FALSE(validHostname("user@nt.kb.smartbotics.ai"));
- EXPECT_FALSE(validHostname("nt kb.smartbotics.ai"));
- }
- TEST(ValidHostname, RefusesASingleLabelSoAnIntranetAliasCannotBecomeATenant) {
- EXPECT_FALSE(validHostname("localhost"));
- EXPECT_FALSE(validHostname("api"));
- EXPECT_TRUE(validHostname("api.example.com"));
- }
- TEST(ValidHostname, RefusesMalformedLabels) {
- EXPECT_FALSE(validHostname(""));
- EXPECT_FALSE(validHostname(".example.com"));
- EXPECT_FALSE(validHostname("example.com.")); // normalise first, then validate
- EXPECT_FALSE(validHostname("a..b.com"));
- EXPECT_FALSE(validHostname("-bad.example.com"));
- EXPECT_FALSE(validHostname("bad-.example.com"));
- EXPECT_FALSE(validHostname(std::string(254, 'a') + ".com"));
- }
- // --- resolution -------------------------------------------------------------
- TEST(HostRegistry, ResolvesEachKind) {
- const HostRegistry r = twoTenants();
- EXPECT_EQ(r.resolve("admin.smartbotics.ai").kind, HostKind::Admin);
- EXPECT_EQ(r.resolve("global-api-01.smartbotics.ai").kind, HostKind::Global);
- const HostBinding nt = r.resolve("nt.kb.smartbotics.ai");
- EXPECT_EQ(nt.kind, HostKind::Project);
- EXPECT_EQ(nt.project, "novitax");
- EXPECT_EQ(nt.pathStyle, PathStyle::Short) << "short is the default shape";
- const HostBinding pa = r.resolve("pa.smartbotics.ai");
- EXPECT_EQ(pa.project, "justice-pa");
- EXPECT_EQ(pa.pathStyle, PathStyle::Full) << "the per-hostname setting is honoured";
- }
- TEST(HostRegistry, OneProjectMayHoldSeveralHostnames) {
- const HostRegistry r = twoTenants();
- EXPECT_EQ(r.resolve("pa.smartbotics.ai").project, "justice-pa");
- EXPECT_EQ(r.resolve("assistant.smartbotics.ai").project, "justice-pa");
- }
- TEST(HostRegistry, AnUnknownHostIsUNKNOWNAndNEVERAProject) {
- // The failure this file exists to prevent. Note the second and third cases:
- // a SUFFIX of a registered name and a PARENT of one must both miss, because
- // a closest-parent match would hand every unclaimed subdomain to whoever
- // owns something above it.
- const HostRegistry r = twoTenants();
- for (const char* h : {"nobody.example.com", "kb.smartbotics.ai", "smartbotics.ai",
- "evil.nt.kb.smartbotics.ai", "nt.kb.smartbotics.ai.evil.com", ""}) {
- const HostBinding b = r.resolve(h);
- EXPECT_EQ(b.kind, HostKind::Unknown) << h;
- EXPECT_TRUE(b.project.empty()) << h << " resolved to project '" << b.project << "'";
- }
- }
- TEST(HostRegistry, ResolutionIsCaseAndPortInsensitive) {
- const HostRegistry r = twoTenants();
- EXPECT_EQ(r.resolve("NT.KB.SMARTBOTICS.AI:8443").project, "novitax");
- EXPECT_EQ(r.resolve("nt.kb.smartbotics.ai.").project, "novitax");
- }
- // --- collisions: the two directions, and they resolve differently -----------
- TEST(HostRegistry, ADeploymentNameCANNOTBeTakenByAProject) {
- // A project claiming the admin hostname would hand it the web UI and the
- // docs catalogue. The deployment keeps its name; the claim is dropped.
- std::vector<std::string> dropped;
- const HostRegistry r = HostRegistry::build(
- "admin.smartbotics.ai", {"global.smartbotics.ai"},
- {{"evil", {{"admin.smartbotics.ai", PathStyle::Short},
- {"global.smartbotics.ai", PathStyle::Short}}}},
- &dropped);
- EXPECT_EQ(r.resolve("admin.smartbotics.ai").kind, HostKind::Admin);
- EXPECT_EQ(r.resolve("global.smartbotics.ai").kind, HostKind::Global);
- EXPECT_TRUE(r.resolve("admin.smartbotics.ai").project.empty());
- EXPECT_EQ(dropped.size(), 2u) << "both claims reported, so the operator can see them";
- }
- TEST(HostRegistry, AHostClaimedByTWOProjectsGoesToNEITHER) {
- // Last-writer-wins would move a tenancy boundary silently. Refusing both
- // makes it a 404 on a name nobody can reach - visible, and recoverable.
- std::vector<std::string> dropped;
- const HostRegistry r = HostRegistry::build(
- "admin.example.com", {},
- {{"alpha", {{"shared.example.com", PathStyle::Short}}},
- {"beta", {{"shared.example.com", PathStyle::Short}}}},
- &dropped);
- const HostBinding b = r.resolve("shared.example.com");
- EXPECT_EQ(b.kind, HostKind::Unknown);
- EXPECT_TRUE(b.project.empty()) << "contested host served '" << b.project << "'";
- EXPECT_EQ(dropped.size(), 1u);
- }
- TEST(HostRegistry, OneProjectListingTheSameHostTwiceIsHarmless) {
- const HostRegistry r = HostRegistry::build(
- "admin.example.com", {},
- {{"alpha", {{"a.example.com", PathStyle::Short}, {"A.example.com.", PathStyle::Full}}}},
- nullptr);
- // Kept, and the FIRST entry's style wins - stated by a test so a later
- // change to "last wins" is a deliberate decision rather than a surprise.
- const HostBinding b = r.resolve("a.example.com");
- EXPECT_EQ(b.project, "alpha");
- EXPECT_EQ(b.pathStyle, PathStyle::Short);
- }
- TEST(HostRegistry, AnInvalidRegisteredHostIsDroppedRatherThanStored) {
- std::vector<std::string> dropped;
- const HostRegistry r = HostRegistry::build(
- "admin.example.com", {},
- {{"alpha", {{"*.example.com", PathStyle::Short}, {"localhost", PathStyle::Short}}}},
- &dropped);
- EXPECT_EQ(r.resolve("*.example.com").kind, HostKind::Unknown);
- EXPECT_EQ(r.resolve("anything.example.com").kind, HostKind::Unknown)
- << "a stored wildcard must not have become a live wildcard";
- EXPECT_EQ(dropped.size(), 2u);
- }
- // --- empty: the ordinary case for a deployment behind someone else's proxy ---
- TEST(HostRegistry, NoHostnamesConfiguredMeansEmptyNotEverythingRefused) {
- // The signal to skip host gating entirely. Without this, adding the feature
- // would 404 every request on every existing deployment - an outage caused
- // by shipping a control, which this codebase has done once already
- // (network_egress defaulting to deny would have done exactly that).
- const HostRegistry r = HostRegistry::build("", {}, {}, nullptr);
- EXPECT_TRUE(r.empty());
- EXPECT_EQ(r.resolve("anything.example.com").kind, HostKind::Unknown);
- }
- TEST(HostRegistry, SizeCountsEveryRegisteredName) {
- const HostRegistry r = twoTenants();
- EXPECT_EQ(r.size(), 5u) << "admin + global + 3 project hostnames";
- EXPECT_FALSE(r.empty());
- }
- // --- serialisation: a hostname has to survive the document round trip -------
- //
- // These live here rather than in test_stores.cpp because they are pure: the
- // store's job is tested there, and what a STORED document means is tested here.
- #include "project_settings.hpp"
- using svapi::ProjectSettings;
- TEST(ProjectSettingsHostnames, RoundTripsHostAndStyle) {
- ProjectSettings p;
- p.project = "novitax";
- p.hostnames = {{"nt.kb.smartbotics.ai", PathStyle::Short},
- {"portal.novitax.example", PathStyle::Full}};
- const ProjectSettings back = ProjectSettings::fromJson(p.toJson());
- ASSERT_EQ(back.hostnames.size(), 2u);
- EXPECT_EQ(back.hostnames[0].host, "nt.kb.smartbotics.ai");
- EXPECT_EQ(back.hostnames[0].pathStyle, PathStyle::Short);
- EXPECT_EQ(back.hostnames[1].host, "portal.novitax.example");
- EXPECT_EQ(back.hostnames[1].pathStyle, PathStyle::Full)
- << "the per-hostname style must survive storage, or every hostname silently "
- "becomes the default shape after a restart";
- }
- TEST(ProjectSettingsHostnames, EmittedAsAnArrayEvenWhenEmpty) {
- // "No hostnames" and "a server that does not know about hostnames" must not
- // look the same to a reader - this file's most repeated trap.
- const nlohmann::json j = ProjectSettings{}.toJson();
- ASSERT_TRUE(j.contains("hostnames"));
- EXPECT_TRUE(j.at("hostnames").is_array());
- EXPECT_TRUE(j.at("hostnames").empty());
- }
- TEST(ProjectSettingsHostnames, AcceptsTheBareStringSpellingAndDefaultsToShort) {
- // So a hand-written document stays readable.
- const nlohmann::json j = {{"project", "p"}, {"hostnames", {"a.example.com"}}};
- const ProjectSettings p = ProjectSettings::fromJson(j);
- ASSERT_EQ(p.hostnames.size(), 1u);
- EXPECT_EQ(p.hostnames[0].host, "a.example.com");
- EXPECT_EQ(p.hostnames[0].pathStyle, PathStyle::Short);
- }
- TEST(ProjectSettingsHostnames, AnUnknownStyleIsShortRatherThanAnError) {
- // A document written by a NEWER server must not take a project offline -
- // the same forward-compatibility rule the function descriptor follows for
- // an unknown key.
- const nlohmann::json j = {{"project", "p"},
- {"hostnames", {{{"host", "a.example.com"}, {"path_style", "sideways"}}}}};
- const ProjectSettings p = ProjectSettings::fromJson(j);
- ASSERT_EQ(p.hostnames.size(), 1u);
- EXPECT_EQ(p.hostnames[0].pathStyle, PathStyle::Short);
- }
- TEST(ProjectSettingsHostnames, AMalformedEntryIsSkippedWithoutLosingTheRest) {
- // A typo in one hostname must not cost the project its embedding overrides.
- const nlohmann::json j = {{"project", "p"},
- {"default_embedding_model", "m"},
- {"hostnames", {7, nlohmann::json::object(), "ok.example.com"}}};
- const ProjectSettings p = ProjectSettings::fromJson(j);
- EXPECT_EQ(p.defaultEmbeddingModel, "m") << "the rest of the document survived";
- ASSERT_EQ(p.hostnames.size(), 1u);
- EXPECT_EQ(p.hostnames[0].host, "ok.example.com");
- }
- // --- the four rules: WHICH PROJECT a request is about -----------------------
- //
- // Mostly refusals, because an over-permissive rule here is a cross-tenant hole
- // and an over-strict one is a 404 somebody reports in an hour.
- using svapi::ProjectResolution;
- using svapi::resolveProject;
- namespace {
- HostBinding projectHost(const char* p, PathStyle s = PathStyle::Short) {
- return HostBinding{HostKind::Project, p, s};
- }
- const HostBinding kAdmin{HostKind::Admin, {}, PathStyle::Full};
- const HostBinding kGlobal{HostKind::Global, {}, PathStyle::Full};
- const HostBinding kUnknown{};
- } // namespace
- TEST(ResolveProject, Rule1_APathOnItsOwnProjectHostIsAccepted) {
- const ProjectResolution r = resolveProject("novitax", projectHost("novitax"), true);
- EXPECT_TRUE(r.ok);
- EXPECT_EQ(r.project, "novitax");
- }
- TEST(ResolveProject, Rule1_APathNamingANOTHERProjectOnAProjectHostIsREFUSED) {
- // THE cross-tenant test. novitax's hostname must not serve justice-pa,
- // however the caller spells the path.
- for (const char* other : {"justice-pa", "toloajto", "", " novitax", "novitax "}) {
- const ProjectResolution r = resolveProject(other, projectHost("novitax"), true);
- if (std::string(other).empty()) continue; // covered by rule 3 below
- EXPECT_FALSE(r.ok) << "a project host served '" << other << "'";
- EXPECT_TRUE(r.project.empty());
- }
- }
- TEST(ResolveProject, Rule2_TheAdminAndGlobalHostsUseThePathUnchanged) {
- // Today's behaviour, which must not move.
- for (const HostBinding& h : {kAdmin, kGlobal}) {
- const ProjectResolution r = resolveProject("justice-pa", h, true);
- EXPECT_TRUE(r.ok);
- EXPECT_EQ(r.project, "justice-pa");
- }
- }
- TEST(ResolveProject, Rule3_TheShortFormOnAShortStyleProjectHostUsesTheHost) {
- const ProjectResolution r = resolveProject("", projectHost("novitax", PathStyle::Short), true);
- EXPECT_TRUE(r.ok);
- EXPECT_EQ(r.project, "novitax");
- }
- TEST(ResolveProject, Rule4_AShortPathOnTheADMINorGLOBALHostIsREFUSED) {
- // The load-bearing refusal. A short path there names NO project, and
- // falling back to a default - the first project, the only project, any
- // project - would be a cross-tenant hole wearing routing's clothes.
- for (const HostBinding& h : {kAdmin, kGlobal}) {
- const ProjectResolution r = resolveProject("", h, true);
- EXPECT_FALSE(r.ok) << "a short path resolved to '" << r.project << "'";
- EXPECT_TRUE(r.project.empty());
- }
- }
- TEST(ResolveProject, Rule4_AShortPathOnAFULLStyleProjectHostIsREFUSED) {
- // path_style is a SETTING, not a hint. A hostname that answered to both
- // shapes would silently make the choice meaningless, and an operator who
- // picked `full` did so to keep the project visible in their own logs.
- const ProjectResolution r = resolveProject("", projectHost("novitax", PathStyle::Full), true);
- EXPECT_FALSE(r.ok);
- }
- TEST(ResolveProject, AnUNKNOWNHostResolvesToNothingEvenWithAPath) {
- // Defence in depth: the pre-routing gate answers this one more cheaply,
- // and this is the arm a route added later cannot forget about.
- EXPECT_FALSE(resolveProject("novitax", kUnknown, true).ok);
- EXPECT_FALSE(resolveProject("", kUnknown, true).ok);
- }
- // --- gating OFF: every existing deployment, and the reason it is a parameter -
- TEST(ResolveProject, WithGatingOffThePathIsTheOnlySourceExactlyAsBefore) {
- // No hostnames configured: the Host header decides nothing. This is what
- // every deployment does today and what the test fixture and devstack do.
- const ProjectResolution r = resolveProject("novitax", kUnknown, false);
- EXPECT_TRUE(r.ok);
- EXPECT_EQ(r.project, "novitax");
- }
- TEST(ResolveProject, WithGatingOffAShortPathIsStillREFUSED) {
- // There is no hostname to complete it, so it names no project. Refusing is
- // the only answer that cannot be a guess.
- EXPECT_FALSE(resolveProject("", kUnknown, false).ok);
- }
- TEST(ResolveProject, UNKNOWNMeansTwoOppositeThingsAndOnlyTheFlagTellsThemApart) {
- // The reason gatingOn is a PARAMETER rather than inferred from the binding.
- // Identical host value, opposite correct answers - so a reader inferring
- // from `host` alone would have to be wrong in one of these two cases, and
- // the dangerous direction is serving the request.
- EXPECT_TRUE(resolveProject("novitax", kUnknown, false).ok) << "gating off: serve it";
- EXPECT_FALSE(resolveProject("novitax", kUnknown, true).ok) << "gating on: an unregistered host";
- }
- // --- write-time collision refusal -------------------------------------------
- using svapi::HostClaimRefusal;
- using svapi::mayClaimHostname;
- namespace {
- const std::unordered_map<std::string, std::vector<HostEntry>> kHeld = {
- {"novitax", {{"nt.kb.smartbotics.ai", PathStyle::Short}}},
- {"justice-pa", {{"pa.smartbotics.ai", PathStyle::Full}}},
- };
- HostClaimRefusal claim(const char* host, const char* project) {
- return mayClaimHostname(host, project, "admin.smartbotics.ai",
- {"global-api-01.smartbotics.ai"}, kHeld);
- }
- } // namespace
- TEST(MayClaimHostname, AFreeNameIsAccepted) {
- EXPECT_EQ(claim("new.smartbotics.ai", "toloajto"), HostClaimRefusal::None);
- }
- TEST(MayClaimHostname, ANameHELDByANOTHERProjectIsTAKEN) {
- EXPECT_EQ(claim("nt.kb.smartbotics.ai", "toloajto"), HostClaimRefusal::Taken);
- // And case or a trailing dot must not get round it - otherwise the refusal
- // is a formality a caller can spell their way past.
- EXPECT_EQ(claim("NT.KB.Smartbotics.AI", "toloajto"), HostClaimRefusal::Taken);
- EXPECT_EQ(claim("nt.kb.smartbotics.ai.", "toloajto"), HostClaimRefusal::Taken);
- }
- TEST(MayClaimHostname, ReClaimingAHostnameYouAlreadyHoldIsIdempotent) {
- // The web UI reads the list, edits one entry and PUTs the whole thing, so a
- // self-collision would make the second save fail.
- EXPECT_EQ(claim("nt.kb.smartbotics.ai", "novitax"), HostClaimRefusal::None);
- }
- TEST(MayClaimHostname, TheDeploymentsOwnNamesAreRESERVED) {
- EXPECT_EQ(claim("admin.smartbotics.ai", "novitax"), HostClaimRefusal::Reserved);
- EXPECT_EQ(claim("global-api-01.smartbotics.ai", "novitax"), HostClaimRefusal::Reserved);
- }
- TEST(MayClaimHostname, AnUnservableNameIsINVALIDRatherThanAccepted) {
- // Distinct from Taken and Reserved because the remedy differs: a caller
- // meeting Invalid must change the VALUE, not negotiate for the name.
- EXPECT_EQ(claim("*.smartbotics.ai", "novitax"), HostClaimRefusal::Invalid);
- EXPECT_EQ(claim("localhost", "novitax"), HostClaimRefusal::Invalid);
- EXPECT_EQ(claim("has space.example.com", "novitax"), HostClaimRefusal::Invalid);
- EXPECT_EQ(claim("", "novitax"), HostClaimRefusal::Invalid);
- }
- TEST(MayClaimHostname, TheWriteTimeAnswerAGREESWithTheRegistrysOwnPrecedence) {
- // The two must never disagree: if a write is accepted, the registry must
- // serve it, and if the registry would drop it, the write must refuse. This
- // asserts the pairing rather than each side separately - a mismatch is how
- // a project gets a 200 for a hostname that silently never works.
- const char* free_ = "fresh.smartbotics.ai";
- ASSERT_EQ(claim(free_, "toloajto"), HostClaimRefusal::None);
- auto held = kHeld;
- held["toloajto"] = {{free_, PathStyle::Short}};
- const HostRegistry r = HostRegistry::build("admin.smartbotics.ai",
- {"global-api-01.smartbotics.ai"}, held, nullptr);
- EXPECT_EQ(r.resolve(free_).project, "toloajto") << "accepted at write time but not served";
- // And the converse, for each refusal reason.
- for (const char* bad : {"nt.kb.smartbotics.ai", "admin.smartbotics.ai", "*.smartbotics.ai"}) {
- EXPECT_NE(claim(bad, "toloajto"), HostClaimRefusal::None) << bad;
- auto h2 = kHeld;
- h2["toloajto"] = {{bad, PathStyle::Short}};
- const HostRegistry r2 = HostRegistry::build("admin.smartbotics.ai",
- {"global-api-01.smartbotics.ai"}, h2, nullptr);
- EXPECT_NE(r2.resolve(bad).project, "toloajto")
- << bad << " was refused at write time but the registry served it to the claimant";
- }
- }
- // --- gating is the DEPLOYMENT's decision, and the way back in ---------------
- //
- // Both of these come from a defect an integration test found rather than from
- // the design: keying gating on "the registry is non-empty" meant attaching the
- // first customer hostname to a project turned gating on service-wide and
- // answered the operator's own next request with a 404.
- using svapi::isDirectAddressHost;
- TEST(HostGating, APROJECTHostnameAloneDoesNOTTurnGatingOn) {
- // The defect. An admin adding a customer hostname must not lock themselves
- // out of the admin UI, and nothing would have connected the two.
- const HostRegistry r = HostRegistry::build(
- "", {}, {{"novitax", {{"nt.kb.smartbotics.ai", PathStyle::Short}}}}, nullptr);
- EXPECT_FALSE(r.gating()) << "a project hostname turned gating on by itself";
- EXPECT_FALSE(r.empty()) << "it is still STORED - inert, not discarded";
- EXPECT_EQ(r.resolve("nt.kb.smartbotics.ai").project, "novitax")
- << "and it still resolves, so switching gating on later needs no re-entry";
- }
- TEST(HostGating, THEDEPLOYMENTNamingItselfIsWhatTurnsGatingOn) {
- EXPECT_TRUE(HostRegistry::build("admin.example.com", {}, {}, nullptr).gating());
- EXPECT_TRUE(HostRegistry::build("", {"global.example.com"}, {}, nullptr).gating());
- EXPECT_FALSE(HostRegistry::build("", {}, {}, nullptr).gating());
- }
- TEST(HostGating, ABareAddressOrLocalhostIsTheOPERATORSOwnAccess) {
- // Recovery: without this, setting admin_hostname is a one-way door that can
- // only be undone by editing the database by hand.
- for (const char* h : {"127.0.0.1", "127.0.0.1:8080", "10.0.0.5", "localhost",
- "localhost:8080", "[::1]", "[::1]:8080"}) {
- EXPECT_TRUE(isDirectAddressHost(h)) << h;
- const HostRegistry r = HostRegistry::build("admin.example.com", {}, {}, nullptr);
- EXPECT_EQ(r.resolve(h).kind, HostKind::Admin) << h;
- }
- }
- TEST(HostGating, ANameIsNOTADirectAddressHoweverNumericItLooks) {
- // THE property that makes the recovery rule safe: it must never be able to
- // shadow a hostname a project registered. A real hostname cannot be all
- // numeric labels, so the two sets are disjoint by construction rather than
- // by the order of a lookup.
- for (const char* h : {"nt.kb.smartbotics.ai", "1.example.com", "123.456.example.com",
- "example.com", "v1.2.3.api.example.com"}) {
- EXPECT_FALSE(isDirectAddressHost(h)) << h;
- }
- // And concretely: a project holding a numeric-looking name still gets it.
- const HostRegistry r = HostRegistry::build(
- "admin.example.com", {}, {{"novitax", {{"1.example.com", PathStyle::Short}}}}, nullptr);
- EXPECT_EQ(r.resolve("1.example.com").project, "novitax")
- << "the direct-address rule shadowed a registered project hostname";
- }
- TEST(HostGating, AShortPathOnTheDirectAddressIsStillREFUSED) {
- // The direct address is the ADMIN surface, so rule 4 applies to it exactly
- // as to a named admin host - it is a way back IN, not a way around the
- // tenancy rules.
- const HostRegistry r = HostRegistry::build("admin.example.com", {}, {}, nullptr);
- EXPECT_FALSE(resolveProject("", r.resolve("127.0.0.1"), true).ok);
- EXPECT_TRUE(resolveProject("novitax", r.resolve("127.0.0.1"), true).ok);
- }
|