|
|
@@ -3,6 +3,9 @@
|
|
|
#include "embedding_cache.hpp"
|
|
|
#include <thread>
|
|
|
#include <chrono>
|
|
|
+#include <cstdio>
|
|
|
+#include <sys/stat.h>
|
|
|
+#include <unistd.h>
|
|
|
using svapi::testutil::ApiFixture;
|
|
|
|
|
|
TEST_F(ApiFixture, HealthzPublic) { auto r = noAuth().Get("/healthz"); ASSERT_TRUE(r); EXPECT_EQ(r->status, 200); }
|
|
|
@@ -3598,7 +3601,7 @@ TEST_F(ApiFixture, AFilteredSearchREPORTSWhichPathAnsweredIt) {
|
|
|
EXPECT_EQ(j.at("scanned"), 0) << j.dump();
|
|
|
}
|
|
|
|
|
|
-TEST_F(ApiFixture, EverySecretBearingSTOREcreatesANEncryptedCollection) {
|
|
|
+TEST_F(ApiFixture, EachCredentialStoreSetsTheEncryptedFLAG_whichProtectsNothing) {
|
|
|
// Prompted by the database team finding that their built-in
|
|
|
// sensitive-FIELD patterns (`*.password`, `*.api_key`, `*.secret`, ...)
|
|
|
// need a dot before the name, so a TOP-LEVEL field of those names is
|
|
|
@@ -3606,11 +3609,21 @@ TEST_F(ApiFixture, EverySecretBearingSTOREcreatesANEncryptedCollection) {
|
|
|
// top level, and `key` is not in those patterns at any nesting - so if we
|
|
|
// relied on field matching, every credential we hold would be plaintext.
|
|
|
//
|
|
|
- // We do not: each store creates its collection with the COLLECTION-level
|
|
|
- // encrypted flag, which is independent of field names. This pins that,
|
|
|
- // because it is one boolean in one line and the consequence of it flipping
|
|
|
- // is every API key and project secret readable from a stolen data
|
|
|
- // directory.
|
|
|
+ // We do not rely on field matching: each store passes the COLLECTION-level
|
|
|
+ // encrypted flag.
|
|
|
+ //
|
|
|
+ // **AND THAT FLAG PROTECTS NOTHING.** The database team established, by
|
|
|
+ // code reading and a canary, that NOTHING in the write path reads it -
|
|
|
+ // `CollectionOptions::encrypted` is stored and reported and never acted on.
|
|
|
+ // Confirmed here independently: see
|
|
|
+ // AStoredSecretIsCurrentlyPLAINTEXTatRest below, which finds a secret
|
|
|
+ // written through our own route in the data directory.
|
|
|
+ //
|
|
|
+ // So this test asserts only that our stores REQUEST encryption. That is
|
|
|
+ // worth keeping - it is what we control, and it is what will start
|
|
|
+ // working if the database ever honours the flag - but it is NOT evidence
|
|
|
+ // about the bytes, and an earlier version of this test presented it as
|
|
|
+ // though it were. A flag is a proxy; the bytes are the thing.
|
|
|
//
|
|
|
// Each store is pointed at a FRESH collection name rather than asserting
|
|
|
// on the long-lived shared ones. `encrypted` is a CREATION-TIME option, so
|
|
|
@@ -3630,7 +3643,9 @@ TEST_F(ApiFixture, EverySecretBearingSTOREcreatesANEncryptedCollection) {
|
|
|
for (const auto& name : {"enc_probe_keys_" + suffix, "enc_probe_settings_" + suffix}) {
|
|
|
const auto info = db_->client().getCollectionInfo(svapi::qualify("default", name));
|
|
|
ASSERT_TRUE(info.has_value()) << name << " was not created";
|
|
|
- EXPECT_TRUE(info->encrypted) << name << " holds credentials and is NOT encrypted at rest";
|
|
|
+ EXPECT_TRUE(info->encrypted)
|
|
|
+ << name << ": our store did not even REQUEST encryption (which, note, the database "
|
|
|
+ "does not act on - see AStoredSecretIsCurrentlyPLAINTEXTatRest)";
|
|
|
db_->client().dropCollection(svapi::qualify("default", name));
|
|
|
}
|
|
|
|
|
|
@@ -3643,7 +3658,7 @@ TEST_F(ApiFixture, EverySecretBearingSTOREcreatesANEncryptedCollection) {
|
|
|
const auto sec = db_->client().getCollectionInfo(svapi::qualify(project_, "vectorapi_secrets"));
|
|
|
ASSERT_TRUE(sec.has_value());
|
|
|
EXPECT_TRUE(sec->encrypted)
|
|
|
- << "project secrets, including mail passwords, are NOT encrypted at rest";
|
|
|
+ << "the project secret store did not request encryption";
|
|
|
c.Delete(secrets.c_str());
|
|
|
|
|
|
// The falsifying half: a collection with no credentials in it is NOT
|
|
|
@@ -3652,3 +3667,74 @@ TEST_F(ApiFixture, EverySecretBearingSTOREcreatesANEncryptedCollection) {
|
|
|
const auto plain = db_->client().getCollectionInfo(svapi::qualify(project_, "vectorapi_functions"));
|
|
|
if (plain) EXPECT_FALSE(plain->encrypted) << "the function registry should be plaintext";
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+// --- is a credential actually encrypted at rest? ----------------------------
|
|
|
+//
|
|
|
+// The test above asserts getCollectionInfo()->encrypted, which is THE FLAG.
|
|
|
+// The database team then established, by code reading and a canary of their
|
|
|
+// own, that nothing in the write path reads that flag - so the flag is not
|
|
|
+// evidence about the bytes, and a test asserting it was pinning a mechanism
|
|
|
+// that does not exist. That is the same proxy-not-the-thing failure this file
|
|
|
+// is full of notes about, committed by me.
|
|
|
+//
|
|
|
+// This asserts the PROPERTY: write a credential through our own route, then
|
|
|
+// look for it in the database's data directory. It needs the directory to be
|
|
|
+// readable from here, which it is for the bind-mounted dev container, so the
|
|
|
+// test SKIPS rather than fails when it is not - a skip says "not checked",
|
|
|
+// which is honest, where a pass would be a false assurance.
|
|
|
+TEST_F(ApiFixture, AStoredSecretIsCurrentlyPLAINTEXTatRest) {
|
|
|
+ const char* dataDir = "/data/smartbotic-db/data";
|
|
|
+ struct stat st{};
|
|
|
+ if (::stat(dataDir, &st) != 0 || !S_ISDIR(st.st_mode)) {
|
|
|
+ GTEST_SKIP() << "no readable data directory at " << dataDir
|
|
|
+ << "; cannot inspect bytes at rest";
|
|
|
+ }
|
|
|
+
|
|
|
+ // A value no other row could contain, so a hit is unambiguous.
|
|
|
+ const std::string canary =
|
|
|
+ "svapi-at-rest-canary-" + std::to_string(::getpid()) + "-" + std::to_string(::time(nullptr));
|
|
|
+
|
|
|
+ auto c = admin();
|
|
|
+ const std::string path = "/api/v1/projects/" + project_ + "/secrets/AT_REST_PROBE";
|
|
|
+ auto put = c.Put(path.c_str(), nlohmann::json{{"value", canary}}.dump(), "application/json");
|
|
|
+ ASSERT_TRUE(put); ASSERT_EQ(put->status / 100, 2) << put->body;
|
|
|
+
|
|
|
+ // Read it back so we know the write really landed, not merely 2xx.
|
|
|
+ auto back = c.Get(("/api/v1/projects/" + project_ + "/secrets").c_str());
|
|
|
+ ASSERT_TRUE(back); ASSERT_EQ(back->status, 200) << back->body;
|
|
|
+ EXPECT_NE(back->body.find("AT_REST_PROBE"), std::string::npos) << back->body;
|
|
|
+
|
|
|
+ // grep -r for the canary. Exit 0 means FOUND, i.e. plaintext on disk.
|
|
|
+ const std::string cmd = "grep -rlF " + canary + " " + dataDir + " 2>/dev/null | head -3";
|
|
|
+ std::string hits;
|
|
|
+ if (FILE* p = ::popen(cmd.c_str(), "r")) {
|
|
|
+ char buf[512];
|
|
|
+ while (::fgets(buf, sizeof(buf), p) != nullptr) hits += buf;
|
|
|
+ ::pclose(p);
|
|
|
+ }
|
|
|
+ c.Delete(path.c_str());
|
|
|
+
|
|
|
+ // ASSERTS THE DEFECT, deliberately. The property we want does NOT hold:
|
|
|
+ // collection-level `encrypted=true` encrypts nothing, and the database's
|
|
|
+ // sensitive-field patterns need a dot before the name so our top-level
|
|
|
+ // `value` never matched. Measured here: the canary appears in
|
|
|
+ // `wal/wal-current.log` and in the project's `env/data.mdb`.
|
|
|
+ //
|
|
|
+ // Pinned this way round rather than left failing, for two reasons. A
|
|
|
+ // permanently red test gets ignored, and this one is self-correcting: when
|
|
|
+ // the database ships encryption at rest, THIS TEST FAILS and says so,
|
|
|
+ // which is the moment to flip it back to `hits.empty()` and delete this
|
|
|
+ // comment. A silent skip would have hidden the exposure instead.
|
|
|
+ EXPECT_FALSE(hits.empty())
|
|
|
+ << "A secret is NO LONGER plaintext at rest - the database appears to have shipped "
|
|
|
+ "encryption. Flip this assertion back to hits.empty() and remove the known-defect "
|
|
|
+ "framing; the credentials written before that release are still plaintext and need "
|
|
|
+ "rewriting.";
|
|
|
+ if (!hits.empty()) {
|
|
|
+ GTEST_LOG_(WARNING)
|
|
|
+ << "KNOWN DEFECT: a project secret written through our own route is PLAINTEXT in "
|
|
|
+ "the database's data directory, and therefore in every backup of it. Files:\n"
|
|
|
+ << hits;
|
|
|
+ }
|
|
|
+}
|