|
|
@@ -3669,6 +3669,91 @@ TEST_F(ApiFixture, EachCredentialStoreSetsTheEncryptedFLAG_whichProtectsNothing)
|
|
|
}
|
|
|
|
|
|
|
|
|
+// --- the flag was ASKED FOR and never APPLIED -------------------------------
|
|
|
+//
|
|
|
+// Measured on the dev database: `vectorapi_keys` and `vectorapi_settings`
|
|
|
+// report `encrypted: no`, even though KeyStore::bootstrap and
|
|
|
+// SettingsStore::bootstrap have passed `encrypted=true` since the first commit
|
|
|
+// that introduced them. The cause was a presence guard - `if
|
|
|
+// (!getCollectionInfo(...))` - which skipped the create for a collection that
|
|
|
+// already existed, which is EXACTLY the case that needed it: an earlier write
|
|
|
+// had created the collection implicitly, with the default flag of false.
|
|
|
+//
|
|
|
+// So the request was real, the guard swallowed it, and `getCollectionInfo`
|
|
|
+// honestly reported false while our code looked like it had asked. Nothing in
|
|
|
+// the suite noticed, because the existing flag test deliberately points each
|
|
|
+// store at a FRESH collection name - which is the right call for testing the
|
|
|
+// creating path, and is precisely why it could not see this.
|
|
|
+//
|
|
|
+// That matters more than a tidy flag now: database 2.17.0 encrypts a
|
|
|
+// collection's rows based on this flag, and the built-in sensitive-NAME
|
|
|
+// patterns cannot cover for it, because they match a whole path segment and we
|
|
|
+// store the secrets under `key` and `openai_api_key`. A collection left plain
|
|
|
+// stays plaintext through the 2.17.0 migration and forever after.
|
|
|
+//
|
|
|
+// The fix is to call createCollection unconditionally. 2.17.0 turns encryption
|
|
|
+// ON for an existing collection (never off); older servers treat the re-call
|
|
|
+// as a no-op.
|
|
|
+TEST_F(ApiFixture, BootstrapAsksForEncryptionOnAnAlreadyEXISTINGCollection) {
|
|
|
+ const std::string suffix = std::to_string(::getpid()) + "_" + std::to_string(::time(nullptr));
|
|
|
+ const std::string target = "enc_upgrade_target_" + suffix;
|
|
|
+ const std::string control = "enc_upgrade_control_" + suffix;
|
|
|
+ const std::string qTarget = svapi::qualify("default", target);
|
|
|
+ const std::string qControl = svapi::qualify("default", control);
|
|
|
+
|
|
|
+ // Both collections are created PLAIN and by hand, standing in for one the
|
|
|
+ // database made implicitly before our store ever ran.
|
|
|
+ ASSERT_TRUE(db_->client().createCollection(qTarget, 0, /*encrypted*/ false, 0, 0));
|
|
|
+ ASSERT_TRUE(db_->client().createCollection(qControl, 0, /*encrypted*/ false, 0, 0));
|
|
|
+ ASSERT_FALSE(db_->client().getCollectionInfo(qTarget)->encrypted)
|
|
|
+ << "the premise of this test is a collection that starts PLAIN";
|
|
|
+
|
|
|
+ // A row that must survive the re-call. Re-issuing createCollection against
|
|
|
+ // a populated collection is the part of this fix that could do harm, so it
|
|
|
+ // is asserted rather than reasoned about.
|
|
|
+ db_->client().upsert(qTarget, nlohmann::json{{"key_id", "pre-existing"}}, "pre-existing");
|
|
|
+ const uint64_t before = db_->client().count(qTarget);
|
|
|
+ ASSERT_EQ(before, 1u);
|
|
|
+
|
|
|
+ // The store runs against the collection that already exists.
|
|
|
+ svapi::KeyStore keys(*db_, target);
|
|
|
+ keys.bootstrap("");
|
|
|
+
|
|
|
+ // Safety first, and this half holds on EVERY server version.
|
|
|
+ EXPECT_TRUE(db_->client().get(qTarget, "pre-existing").has_value())
|
|
|
+ << "re-issuing createCollection destroyed a pre-existing document";
|
|
|
+ EXPECT_GE(db_->client().count(qTarget), before)
|
|
|
+ << "re-issuing createCollection lost rows";
|
|
|
+
|
|
|
+ const auto target_info = db_->client().getCollectionInfo(qTarget);
|
|
|
+ const auto control_info = db_->client().getCollectionInfo(qControl);
|
|
|
+ ASSERT_TRUE(target_info.has_value());
|
|
|
+ ASSERT_TRUE(control_info.has_value());
|
|
|
+
|
|
|
+ // The control is what makes the assertion mean something: it is never
|
|
|
+ // bootstrapped, so if BOTH collections were encrypted the cause would be
|
|
|
+ // something other than our call, and if both stay plain the server simply
|
|
|
+ // cannot do this yet.
|
|
|
+ EXPECT_FALSE(control_info->encrypted)
|
|
|
+ << "a collection nothing bootstrapped became encrypted; this test is "
|
|
|
+ "not measuring what it claims to";
|
|
|
+
|
|
|
+ if (!target_info->encrypted) {
|
|
|
+ db_->client().dropCollection(qTarget);
|
|
|
+ db_->client().dropCollection(qControl);
|
|
|
+ GTEST_SKIP() << "this server does not switch encryption on for an existing collection "
|
|
|
+ "(database 2.17.0 and later do); our side asked for it, which is all "
|
|
|
+ "this code controls - verify the bytes with `smartbotic-db-cli "
|
|
|
+ "encryption-verify` once 2.17.0 is deployed";
|
|
|
+ }
|
|
|
+
|
|
|
+ // Reached only on 2.17.0+: the upgrade happened, driven by our re-call.
|
|
|
+ EXPECT_TRUE(target_info->encrypted);
|
|
|
+ db_->client().dropCollection(qTarget);
|
|
|
+ db_->client().dropCollection(qControl);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
// --- is a credential actually encrypted at rest? ----------------------------
|
|
|
//
|
|
|
// The test above asserts getCollectionInfo()->encrypted, which is THE FLAG.
|