|
@@ -0,0 +1,182 @@
|
|
|
|
|
+// The /fn gate: who may call a published HTTP function, and how often.
|
|
|
|
|
+//
|
|
|
|
|
+// Pure by construction - decideFnAuth takes three booleans rather than an
|
|
|
|
|
+// httplib request, so every combination can be enumerated here on the dev
|
|
|
|
|
+// host. The point of the split is that the POLICY question is answerable
|
|
|
|
|
+// without a socket, a key store or a registry.
|
|
|
|
|
+#include "functions/descriptor.hpp"
|
|
|
|
|
+#include "functions/fn_gate.hpp"
|
|
|
|
|
+#include <gtest/gtest.h>
|
|
|
|
|
+
|
|
|
|
|
+using namespace svapi::functions;
|
|
|
|
|
+
|
|
|
|
|
+namespace {
|
|
|
|
|
+
|
|
|
|
|
+nlohmann::json httpOn(const char* extraKey = nullptr,
|
|
|
|
|
+ nlohmann::json extraValue = nlohmann::json()) {
|
|
|
|
|
+ nlohmann::json on{{"id", "hook"}, {"type", "http"}, {"route", "/hook"}};
|
|
|
|
|
+ if (extraKey) on[extraKey] = std::move(extraValue);
|
|
|
|
|
+ return on;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+} // namespace
|
|
|
|
|
+
|
|
|
|
|
+// --- The declared mode --------------------------------------------------
|
|
|
|
|
+
|
|
|
|
|
+TEST(FnAuthDescriptor, DefaultsToPublicSoNothingDeployedGoesDark) {
|
|
|
|
|
+ // The whole reason Public is the default: every function deployed before
|
|
|
|
|
+ // this field existed must keep answering. A default of Key would have
|
|
|
|
|
+ // taken them all offline the moment the field shipped.
|
|
|
|
|
+ const ParseResult r = parseDescriptor(httpOn());
|
|
|
|
|
+ ASSERT_TRUE(r.ok) << r.error;
|
|
|
|
|
+ EXPECT_EQ(r.descriptor.auth, FnAuth::Public);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(FnAuthDescriptor, ParsesKey) {
|
|
|
|
|
+ const ParseResult r = parseDescriptor(httpOn("auth", "key"));
|
|
|
|
|
+ ASSERT_TRUE(r.ok) << r.error;
|
|
|
|
|
+ EXPECT_EQ(r.descriptor.auth, FnAuth::Key);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(FnAuthDescriptor, ParsesAnExplicitPublic) {
|
|
|
|
|
+ const ParseResult r = parseDescriptor(httpOn("auth", "public"));
|
|
|
|
|
+ ASSERT_TRUE(r.ok) << r.error;
|
|
|
|
|
+ EXPECT_EQ(r.descriptor.auth, FnAuth::Public);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// THE important one. An unknown VALUE must be refused, where an unknown KEY is
|
|
|
|
|
+// ignored - a typo must never publish a function its author believed was
|
|
|
|
|
+// gated. Had this defaulted to Public, `auth: 'kye'` would be a silent hole
|
|
|
|
|
+// with a green test suite.
|
|
|
|
|
+TEST(FnAuthDescriptor, RefusesAnUnrecognisedValueRatherThanDefaultingToPublic) {
|
|
|
|
|
+ const ParseResult r = parseDescriptor(httpOn("auth", "kye"));
|
|
|
|
|
+ EXPECT_FALSE(r.ok);
|
|
|
|
|
+ EXPECT_NE(r.error.find("auth"), std::string::npos);
|
|
|
|
|
+ EXPECT_NE(r.error.find("kye"), std::string::npos) << "the error must quote what was written";
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(FnAuthDescriptor, RefusesANonStringValue) {
|
|
|
|
|
+ EXPECT_FALSE(parseDescriptor(httpOn("auth", true)).ok);
|
|
|
|
|
+ EXPECT_FALSE(parseDescriptor(httpOn("auth", 1)).ok);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// The pairing for the refusal above: an unknown KEY is still tolerated, so the
|
|
|
|
|
+// strictness cannot have quietly become "refuse anything unfamiliar", which
|
|
|
|
|
+// would break the forward-compatibility rule parseDescriptor exists to keep.
|
|
|
|
|
+TEST(FnAuthDescriptor, StillIgnoresAnUnknownKeySoANewerScriptLoads) {
|
|
|
|
|
+ nlohmann::json on = httpOn("somethingFromALaterServer", "whatever");
|
|
|
|
|
+ const ParseResult r = parseDescriptor(on);
|
|
|
|
|
+ ASSERT_TRUE(r.ok) << r.error;
|
|
|
|
|
+ EXPECT_EQ(r.descriptor.auth, FnAuth::Public);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(FnAuthDescriptor, SurvivesTheRoundTripThroughTheStoredRow) {
|
|
|
|
|
+ // The gate must not evaporate on a reload: toJson writes it
|
|
|
|
|
+ // unconditionally for exactly this reason.
|
|
|
|
|
+ const ParseResult first = parseDescriptor(httpOn("auth", "key"));
|
|
|
|
|
+ ASSERT_TRUE(first.ok) << first.error;
|
|
|
|
|
+ const nlohmann::json stored = first.descriptor.toJson();
|
|
|
|
|
+ ASSERT_TRUE(stored.contains("auth"));
|
|
|
|
|
+ const ParseResult second = parseDescriptor(stored);
|
|
|
|
|
+ ASSERT_TRUE(second.ok) << second.error;
|
|
|
|
|
+ EXPECT_EQ(second.descriptor.auth, FnAuth::Key);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(FnAuthDescriptor, AuthIsIgnoredForATriggerWithNoCaller) {
|
|
|
|
|
+ // A cron function has nobody to authenticate. The field is accepted and
|
|
|
|
|
+ // simply means nothing there, rather than being an error - the descriptor
|
|
|
|
|
+ // vocabulary is shared across trigger types by design.
|
|
|
|
|
+ nlohmann::json on{{"id", "nightly"}, {"type", "cron"}, {"schedule", "0 3 * * *"}};
|
|
|
|
|
+ const ParseResult r = parseDescriptor(on);
|
|
|
|
|
+ ASSERT_TRUE(r.ok) << r.error;
|
|
|
|
|
+ EXPECT_EQ(r.descriptor.auth, FnAuth::Public);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// --- The decision -------------------------------------------------------
|
|
|
|
|
+
|
|
|
|
|
+TEST(FnGate, APublicFunctionAllowsAnAnonymousCaller) {
|
|
|
|
|
+ EXPECT_TRUE(decideFnAuth(FnAuth::Public, FnAuthFacts{}).allowed());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// The falsifiability pairing for the whole gate. Without it, every refusal
|
|
|
|
|
+// test below would pass just as well against a gate that refused EVERYTHING,
|
|
|
|
|
+// which is a worse defect than the one being fixed: it would take the public
|
|
|
|
|
+// function surface offline.
|
|
|
|
|
+TEST(FnGate, APublicFunctionIgnoresEveryFactIncludingAGarbageKey) {
|
|
|
|
|
+ FnAuthFacts junk;
|
|
|
|
|
+ junk.keyPresent = true;
|
|
|
|
|
+ junk.keyAuthenticates = false;
|
|
|
|
|
+ junk.keyGrantsProject = false;
|
|
|
|
|
+ EXPECT_TRUE(decideFnAuth(FnAuth::Public, junk).allowed())
|
|
|
|
|
+ << "a key sent to a public function must be neither required nor rejected";
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(FnGate, AGatedFunctionRefusesAnAnonymousCallerWith401) {
|
|
|
|
|
+ const FnGateDecision d = decideFnAuth(FnAuth::Key, FnAuthFacts{});
|
|
|
|
|
+ EXPECT_FALSE(d.allowed());
|
|
|
|
|
+ EXPECT_EQ(d.outcome, FnGateOutcome::Unauthenticated);
|
|
|
|
|
+ EXPECT_EQ(d.slug, "unauthorized");
|
|
|
|
|
+ EXPECT_NE(d.message.find("Authorization"), std::string::npos)
|
|
|
|
|
+ << "the message must say how to authenticate, or the author cannot act on it";
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(FnGate, AnUnknownTokenIsINDISTINGUISHABLEfromNoTokenAtAll) {
|
|
|
|
|
+ // Deliberate: telling a prober that the token was well formed but unknown
|
|
|
|
|
+ // tells them which half of the guess was right.
|
|
|
|
|
+ FnAuthFacts absent;
|
|
|
|
|
+ FnAuthFacts wrong;
|
|
|
|
|
+ wrong.keyPresent = true;
|
|
|
|
|
+ const FnGateDecision a = decideFnAuth(FnAuth::Key, absent);
|
|
|
|
|
+ const FnGateDecision b = decideFnAuth(FnAuth::Key, wrong);
|
|
|
|
|
+ EXPECT_EQ(a.outcome, b.outcome);
|
|
|
|
|
+ EXPECT_EQ(a.slug, b.slug);
|
|
|
|
|
+ EXPECT_EQ(a.message, b.message);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(FnGate, ARealKeyForTheWrongProjectIs403NOT401) {
|
|
|
|
|
+ // 401 would tell the caller to try again with a credential, and re-sending
|
|
|
|
|
+ // the same valid key can never help. That distinction is the only thing
|
|
|
|
|
+ // the two statuses carry.
|
|
|
|
|
+ FnAuthFacts f;
|
|
|
|
|
+ f.keyPresent = true;
|
|
|
|
|
+ f.keyAuthenticates = true;
|
|
|
|
|
+ f.keyGrantsProject = false;
|
|
|
|
|
+ const FnGateDecision d = decideFnAuth(FnAuth::Key, f);
|
|
|
|
|
+ EXPECT_EQ(d.outcome, FnGateOutcome::Forbidden);
|
|
|
|
|
+ EXPECT_EQ(d.slug, "forbidden");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(FnGate, AGrantedKeyIsAllowed) {
|
|
|
|
|
+ FnAuthFacts f;
|
|
|
|
|
+ f.keyPresent = true;
|
|
|
|
|
+ f.keyAuthenticates = true;
|
|
|
|
|
+ f.keyGrantsProject = true;
|
|
|
|
|
+ EXPECT_TRUE(decideFnAuth(FnAuth::Key, f).allowed());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// --- The throttle bucket ------------------------------------------------
|
|
|
|
|
+
|
|
|
|
|
+TEST(FnThrottleBucket, IsPerFunctionAndPerProject) {
|
|
|
|
|
+ EXPECT_NE(fnHttpThrottleBucket("a", "fn"), fnHttpThrottleBucket("b", "fn"));
|
|
|
|
|
+ EXPECT_NE(fnHttpThrottleBucket("a", "one"), fnHttpThrottleBucket("a", "two"));
|
|
|
|
|
+ EXPECT_EQ(fnHttpThrottleBucket("a", "fn"), fnHttpThrottleBucket("a", "fn"));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// The bucket is deliberately NOT the function's principal id, which is what
|
|
|
|
|
+// requireCapabilityCore charges for data access. Sharing one bucket would mean
|
|
|
|
|
+// a function doing three reads per request spent four tokens per call, so
|
|
|
|
|
+// adding a read to a script would silently lower its own request ceiling -
|
|
|
|
|
+// the same mistake as charging a listing one token per collection examined.
|
|
|
|
|
+TEST(FnThrottleBucket, IsSEPARATEfromTheDataAccessBudget) {
|
|
|
|
|
+ const std::string project = "proj";
|
|
|
|
|
+ const std::string id = "sendmail";
|
|
|
|
|
+ const std::string httpBucket = fnHttpThrottleBucket(project, id);
|
|
|
|
|
+ // The shapes a principal id takes elsewhere in the server.
|
|
|
|
|
+ EXPECT_NE(httpBucket, id);
|
|
|
|
|
+ EXPECT_NE(httpBucket, project + "/" + id);
|
|
|
|
|
+ EXPECT_NE(httpBucket, "fn:" + project + "/" + id);
|
|
|
|
|
+ EXPECT_NE(httpBucket, "fn:" + project + "/" + id + ":0");
|
|
|
|
|
+ // And it cannot collide with a key id, which is a bare token elsewhere.
|
|
|
|
|
+ EXPECT_NE(httpBucket.find(':'), std::string::npos)
|
|
|
|
|
+ << "the bucket must be namespaced, or it could collide with a key id";
|
|
|
|
|
+}
|