summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShreyas Kalyan <shreyas.kalyan@mongodb.com>2022-09-02 11:16:13 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-09-09 20:34:58 +0000
commitfb1b3f7abaf6a18b803afd90b6fdab4f42cfebed (patch)
treecf362b4e3daa1febd62e924450faa740f363a7d8 /src
parent08b939c1c9081f20912111408b7d8201da551530 (diff)
downloadmongo-fb1b3f7abaf6a18b803afd90b6fdab4f42cfebed.tar.gz
SERVER-67757 FLE2 Range Query: implement delete
Diffstat (limited to 'src')
-rw-r--r--src/mongo/crypto/fle_crypto.cpp3
-rw-r--r--src/mongo/db/fle_crud.cpp163
-rw-r--r--src/mongo/db/fle_crud_test.cpp99
3 files changed, 190 insertions, 75 deletions
diff --git a/src/mongo/crypto/fle_crypto.cpp b/src/mongo/crypto/fle_crypto.cpp
index 814d99bf95e..372cb4830d4 100644
--- a/src/mongo/crypto/fle_crypto.cpp
+++ b/src/mongo/crypto/fle_crypto.cpp
@@ -1353,7 +1353,8 @@ void collectIndexedFields(std::vector<EDCIndexedFields>* pFields,
ConstDataRange cdr,
StringData fieldPath) {
auto [encryptedTypeBinding, subCdr] = fromEncryptedConstDataRange(cdr);
- if (encryptedTypeBinding != EncryptedBinDataType::kFLE2EqualityIndexedValue) {
+ if (encryptedTypeBinding != EncryptedBinDataType::kFLE2EqualityIndexedValue &&
+ encryptedTypeBinding != EncryptedBinDataType::kFLE2RangeIndexedValue) {
return;
}
diff --git a/src/mongo/db/fle_crud.cpp b/src/mongo/db/fle_crud.cpp
index 80a3e07aa24..0837600a1c6 100644
--- a/src/mongo/db/fle_crud.cpp
+++ b/src/mongo/db/fle_crud.cpp
@@ -574,6 +574,69 @@ void processFieldsForInsert(FLEQueryInterface* queryImpl,
}
}
+void processRemovedFieldsHelper(FLEQueryInterface* queryImpl,
+ const EncryptedFieldConfig& efc,
+ const ESCDerivedFromDataTokenAndContentionFactorToken& esc,
+ const ECCDerivedFromDataTokenAndContentionFactorToken& ecc,
+ uint64_t count,
+ const EDCIndexedFields& deletedField,
+ const FLEDeleteToken& deleteToken,
+ const TxnCollectionReader& reader,
+ const NamespaceString& eccNss,
+ const NamespaceString& edcNss,
+ int32_t* pStmtId) {
+ auto tagToken = FLETwiceDerivedTokenGenerator::generateECCTwiceDerivedTagToken(ecc);
+ auto valueToken = FLETwiceDerivedTokenGenerator::generateECCTwiceDerivedValueToken(ecc);
+
+ auto alpha = ECCCollection::emuBinary(reader, tagToken, valueToken);
+
+ uint64_t index = 0;
+ if (alpha.has_value() && alpha.value() == 0) {
+ index = 1;
+ } else if (!alpha.has_value()) {
+ auto block = ECCCollection::generateId(tagToken, boost::none);
+
+ auto r_ecc = reader.getById(block);
+ uassert(6371306, "ECC null document not found", !r_ecc.isEmpty());
+
+ auto eccNullDoc = uassertStatusOK(ECCCollection::decryptNullDocument(valueToken, r_ecc));
+ index = eccNullDoc.position + 2;
+ } else {
+ auto block = ECCCollection::generateId(tagToken, alpha);
+
+ auto r_ecc = reader.getById(block);
+ uassert(6371307, "ECC document not found", !r_ecc.isEmpty());
+
+ auto eccDoc = uassertStatusOK(ECCCollection::decryptDocument(valueToken, r_ecc));
+
+ if (eccDoc.valueType == ECCValueType::kCompactionPlaceholder) {
+ uassertStatusOK(
+ Status(ErrorCodes::FLECompactionPlaceholder, "Found contention placeholder"));
+ }
+
+ index = alpha.value() + 1;
+ }
+
+ auto eccInsertReply = uassertStatusOK(queryImpl->insertDocument(
+ eccNss,
+ ECCCollection::generateDocument(tagToken, valueToken, index, count),
+ pStmtId,
+ true));
+ checkWriteErrors(eccInsertReply);
+
+ NamespaceString nssEcoc(edcNss.db(), efc.getEcocCollection().value());
+
+ // TODO - make this a batch of ECOC updates?
+ EncryptedStateCollectionTokens tokens(esc, ecc);
+ auto encryptedTokens = uassertStatusOK(tokens.serialize(deleteToken.ecocToken));
+ auto ecocInsertReply = uassertStatusOK(queryImpl->insertDocument(
+ nssEcoc,
+ ECOCCollection::generateDocument(deletedField.fieldPathName, encryptedTokens),
+ pStmtId,
+ false));
+ checkWriteErrors(ecocInsertReply);
+};
+
void processRemovedFields(FLEQueryInterface* queryImpl,
const NamespaceString& edcNss,
const EncryptedFieldConfig& efc,
@@ -581,13 +644,11 @@ void processRemovedFields(FLEQueryInterface* queryImpl,
const std::vector<EDCIndexedFields>& deletedFields,
int32_t* pStmtId) {
- NamespaceString nssEcc(edcNss.db(), efc.getEccCollection().value());
-
-
- auto docCount = queryImpl->countDocuments(nssEcc);
+ NamespaceString eccNss(edcNss.db(), efc.getEccCollection().value());
- TxnCollectionReader reader(docCount, queryImpl, nssEcc);
+ auto docCount = queryImpl->countDocuments(eccNss);
+ TxnCollectionReader reader(docCount, queryImpl, eccNss);
for (const auto& deletedField : deletedFields) {
// TODO - verify each indexed fields is listed in EncryptionInformation for the
@@ -605,65 +666,45 @@ void processRemovedFields(FLEQueryInterface* queryImpl,
// TODO - add support other types
uassert(6371305,
- "Ony support deleting equality indexed fields",
- encryptedTypeBinding == EncryptedBinDataType::kFLE2EqualityIndexedValue);
-
- auto plainTextField = uassertStatusOK(FLE2IndexedEqualityEncryptedValue::decryptAndParse(
- deleteToken.serverEncryptionToken, subCdr));
-
- auto tagToken =
- FLETwiceDerivedTokenGenerator::generateECCTwiceDerivedTagToken(plainTextField.ecc);
- auto valueToken =
- FLETwiceDerivedTokenGenerator::generateECCTwiceDerivedValueToken(plainTextField.ecc);
-
- auto alpha = ECCCollection::emuBinary(reader, tagToken, valueToken);
-
- uint64_t index = 0;
- if (alpha.has_value() && alpha.value() == 0) {
- index = 1;
- } else if (!alpha.has_value()) {
- auto block = ECCCollection::generateId(tagToken, boost::none);
-
- auto r_ecc = reader.getById(block);
- uassert(6371306, "ECC null document not found", !r_ecc.isEmpty());
-
- auto eccNullDoc =
- uassertStatusOK(ECCCollection::decryptNullDocument(valueToken, r_ecc));
- index = eccNullDoc.position + 2;
+ "Ony support deleting equality indexed fields or range indexed fields",
+ encryptedTypeBinding == EncryptedBinDataType::kFLE2EqualityIndexedValue ||
+ encryptedTypeBinding == EncryptedBinDataType::kFLE2RangeIndexedValue);
+
+ if (encryptedTypeBinding == EncryptedBinDataType::kFLE2EqualityIndexedValue) {
+ auto plainTextField =
+ uassertStatusOK(FLE2IndexedEqualityEncryptedValue::decryptAndParse(
+ deleteToken.serverEncryptionToken, subCdr));
+ processRemovedFieldsHelper(queryImpl,
+ efc,
+ plainTextField.esc,
+ plainTextField.ecc,
+ plainTextField.count,
+ deletedField,
+ deleteToken,
+ reader,
+ eccNss,
+ edcNss,
+ pStmtId);
} else {
- auto block = ECCCollection::generateId(tagToken, alpha);
-
- auto r_ecc = reader.getById(block);
- uassert(6371307, "ECC document not found", !r_ecc.isEmpty());
-
- auto eccDoc = uassertStatusOK(ECCCollection::decryptDocument(valueToken, r_ecc));
-
- if (eccDoc.valueType == ECCValueType::kCompactionPlaceholder) {
- uassertStatusOK(
- Status(ErrorCodes::FLECompactionPlaceholder, "Found contention placeholder"));
+ auto plainTextField = uassertStatusOK(FLE2IndexedRangeEncryptedValue::decryptAndParse(
+ deleteToken.serverEncryptionToken, subCdr));
+
+ for (size_t i = 0; i < plainTextField.counters.size(); i++) {
+ const auto& edgeTokenSet = plainTextField.tokens[i];
+ const auto& count = plainTextField.counters[i];
+ processRemovedFieldsHelper(queryImpl,
+ efc,
+ edgeTokenSet.esc,
+ edgeTokenSet.ecc,
+ count,
+ deletedField,
+ deleteToken,
+ reader,
+ eccNss,
+ edcNss,
+ pStmtId);
}
-
- index = alpha.value() + 1;
}
-
- auto eccInsertReply = uassertStatusOK(queryImpl->insertDocument(
- nssEcc,
- ECCCollection::generateDocument(tagToken, valueToken, index, plainTextField.count),
- pStmtId,
- true));
- checkWriteErrors(eccInsertReply);
-
- NamespaceString nssEcoc(edcNss.db(), efc.getEcocCollection().value());
-
- // TODO - make this a batch of ECOC updates?
- EncryptedStateCollectionTokens tokens(plainTextField.esc, plainTextField.ecc);
- auto encryptedTokens = uassertStatusOK(tokens.serialize(deleteToken.ecocToken));
- auto ecocInsertReply = uassertStatusOK(queryImpl->insertDocument(
- nssEcoc,
- ECOCCollection::generateDocument(deletedField.fieldPathName, encryptedTokens),
- pStmtId,
- false));
- checkWriteErrors(ecocInsertReply);
}
}
diff --git a/src/mongo/db/fle_crud_test.cpp b/src/mongo/db/fle_crud_test.cpp
index 764d8846ea2..f7fab888477 100644
--- a/src/mongo/db/fle_crud_test.cpp
+++ b/src/mongo/db/fle_crud_test.cpp
@@ -158,15 +158,20 @@ protected:
void testValidateTags(BSONObj obj);
- void doSingleInsert(int id, BSONElement element, bool bypassDocumentValidation = false);
+ void doSingleInsert(int id,
+ BSONElement element,
+ Fle2AlgorithmInt alg = Fle2AlgorithmInt::kEquality,
+ bool bypassDocumentValidation = false);
void doSingleInsert(int id, BSONObj obj, bool bypassDocumentValidation = false);
+ void doSingleRangeInsert(int id, BSONElement element);
+
void doSingleInsertWithContention(
int id, BSONElement element, int64_t cm, uint64_t cf, EncryptedFieldConfig efc);
void doSingleInsertWithContention(
int id, BSONObj obj, int64_t cm, uint64_t cf, EncryptedFieldConfig efc);
- void doSingleDelete(int id);
+ void doSingleDelete(int id, Fle2AlgorithmInt alg = Fle2AlgorithmInt::kEquality);
void doSingleUpdate(int id, BSONElement element);
void doSingleUpdate(int id, BSONObj obj);
@@ -364,7 +369,8 @@ std::vector<char> FleCrudTest::generatePlaceholder(UUID keyId, BSONElement value
return v;
}
-EncryptedFieldConfig getTestEncryptedFieldConfig() {
+EncryptedFieldConfig getTestEncryptedFieldConfig(
+ Fle2AlgorithmInt alg = Fle2AlgorithmInt::kEquality) {
constexpr auto schema = R"({
"escCollection": "esc",
@@ -385,7 +391,29 @@ EncryptedFieldConfig getTestEncryptedFieldConfig() {
]
})";
- return EncryptedFieldConfig::parse(IDLParserContext("root"), fromjson(schema));
+ constexpr auto rangeSchema = R"({
+ "escCollection": "esc",
+ "eccCollection": "ecc",
+ "ecocCollection": "ecoc",
+ "fields": [
+ {
+ "keyId":
+ {
+ "$uuid": "12345678-1234-9876-1234-123456789012"
+ }
+ ,
+ "path": "encrypted",
+ "bsonType": "int",
+ "queries": {"queryType": "range", "min": 0, "max": 15, "sparsity": 1}
+
+ }
+ ]
+})";
+
+ if (alg == Fle2AlgorithmInt::kEquality) {
+ return EncryptedFieldConfig::parse(IDLParserContext("root"), fromjson(schema));
+ }
+ return EncryptedFieldConfig::parse(IDLParserContext("root"), fromjson(rangeSchema));
}
void FleCrudTest::assertDocumentCounts(uint64_t edc, uint64_t esc, uint64_t ecc, uint64_t ecoc) {
@@ -443,15 +471,42 @@ void FleCrudTest::validateDocument(int id, boost::optional<BSONObj> doc) {
}
}
+BSONObj generateFLE2RangeInsertSpec(BSONElement value) {
+ FLE2RangeInsertSpec spec;
+ spec.setValue(value);
+
+ auto lowerDoc = BSON("lb" << 0);
+ spec.setLowerBound(lowerDoc.firstElement());
+ auto upperDoc = BSON("ub" << 15);
+
+ spec.setUpperBound(upperDoc.firstElement());
+ auto specDoc = BSON("s" << spec.toBSON());
+
+ return specDoc;
+}
+
// Use different keys for index and user
-std::vector<char> generateSinglePlaceholder(BSONElement value, int64_t cm = 0) {
+std::vector<char> generateSinglePlaceholder(BSONElement value,
+ Fle2AlgorithmInt alg = Fle2AlgorithmInt::kEquality,
+ int64_t cm = 0) {
FLE2EncryptionPlaceholder ep;
- ep.setAlgorithm(mongo::Fle2AlgorithmInt::kEquality);
+ // Has to be generated outside of if statements to root the
+ // value until ep is finalized as an object.
+ BSONObj temp = generateFLE2RangeInsertSpec(value);
+
+ ep.setAlgorithm(alg);
ep.setUserKeyId(userKeyId);
ep.setIndexKeyId(indexKeyId);
- ep.setValue(value);
ep.setType(mongo::Fle2PlaceholderType::kInsert);
+
+ if (alg == Fle2AlgorithmInt::kRange) {
+ ep.setValue(temp.firstElement());
+ ep.setSparsity(1);
+ } else {
+ ep.setValue(value);
+ }
+
ep.setMaxContentionCounter(cm);
BSONObj obj = ep.toBSON();
@@ -472,8 +527,11 @@ void FleCrudTest::testValidateTags(BSONObj obj) {
FLEClientCrypto::validateTagsArray(obj);
}
-void FleCrudTest::doSingleInsert(int id, BSONElement element, bool bypassDocumentValidation) {
- auto buf = generateSinglePlaceholder(element);
+void FleCrudTest::doSingleInsert(int id,
+ BSONElement element,
+ Fle2AlgorithmInt alg,
+ bool bypassDocumentValidation) {
+ auto buf = generateSinglePlaceholder(element, alg);
BSONObjBuilder builder;
builder.append("_id", id);
builder.append("counter", 1);
@@ -486,7 +544,7 @@ void FleCrudTest::doSingleInsert(int id, BSONElement element, bool bypassDocumen
auto serverPayload = EDCServerCollection::getEncryptedFieldInfo(result);
- auto efc = getTestEncryptedFieldConfig();
+ auto efc = getTestEncryptedFieldConfig(alg);
uassertStatusOK(processInsert(_queryImpl.get(), _edcNs, serverPayload, efc, 0, result, false));
}
@@ -497,7 +555,7 @@ void FleCrudTest::doSingleInsert(int id, BSONObj obj, bool bypassDocumentValidat
void FleCrudTest::doSingleInsertWithContention(
int id, BSONElement element, int64_t cm, uint64_t cf, EncryptedFieldConfig efc) {
- auto buf = generateSinglePlaceholder(element, cm);
+ auto buf = generateSinglePlaceholder(element, Fle2AlgorithmInt::kEquality, cm);
BSONObjBuilder builder;
builder.append("_id", id);
builder.append("counter", 1);
@@ -567,9 +625,9 @@ void FleCrudTest::doSingleUpdateWithUpdateDoc(int id,
processUpdate(_queryImpl.get(), expCtx, updateRequest);
}
-void FleCrudTest::doSingleDelete(int id) {
+void FleCrudTest::doSingleDelete(int id, Fle2AlgorithmInt alg) {
- auto efc = getTestEncryptedFieldConfig();
+ auto efc = getTestEncryptedFieldConfig(alg);
auto doc = EncryptionInformationHelpers::encryptionInformationSerializeForDelete(
_edcNs, efc, &_keyVault);
@@ -807,6 +865,21 @@ TEST_F(FleCrudTest, InsertAndDeleteOne) {
getECCDocument(getTestECCToken(element), 1);
}
+// Insert and delete one document
+TEST_F(FleCrudTest, InsertAndDeleteOneRange) {
+ auto doc = BSON("encrypted" << 5);
+ auto element = doc.firstElement();
+
+ doSingleInsert(1, element, Fle2AlgorithmInt::kRange);
+
+ assertDocumentCounts(1, 5, 0, 5);
+
+ doSingleDelete(1, Fle2AlgorithmInt::kRange);
+
+ assertDocumentCounts(0, 5, 5, 10);
+ assertECOCDocumentCountByField("encrypted", 10);
+}
+
// Insert two documents, and delete both
TEST_F(FleCrudTest, InsertTwoSamAndDeleteTwo) {
auto doc = BSON("encrypted"