Skip to content

Commit bd88e8b

Browse files
committed
Moved builder to encryption project and removed CsfleEncryptionEnum (using the one that's already there)
1 parent 0069760 commit bd88e8b

File tree

2 files changed

+45
-61
lines changed

2 files changed

+45
-61
lines changed

src/MongoDB.Driver/Encryption/CsfleSchemaBuilder.cs renamed to src/MongoDB.Driver.Encryption/CsfleSchemaBuilder.cs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ internal EncryptedCollectionBuilder()
7878
/// <summary>
7979
/// //TODO
8080
/// </summary>
81-
public EncryptedCollectionBuilder<TDocument> EncryptMetadata(Guid? keyId = null, CsfleEncryptionAlgorithm? algorithm = null)
81+
public EncryptedCollectionBuilder<TDocument> EncryptMetadata(Guid? keyId = null, EncryptionAlgorithm? algorithm = null)
8282
{
8383
if (keyId is null && algorithm is null)
8484
{
@@ -99,7 +99,7 @@ public EncryptedCollectionBuilder<TDocument> EncryptMetadata(Guid? keyId = null,
9999
public EncryptedCollectionBuilder<TDocument> PatternProperty(
100100
string pattern,
101101
BsonType bsonType,
102-
CsfleEncryptionAlgorithm? algorithm = null,
102+
EncryptionAlgorithm? algorithm = null,
103103
Guid? keyId = null)
104104
=> PatternProperty(pattern, [bsonType], algorithm, keyId);
105105

@@ -109,7 +109,7 @@ public EncryptedCollectionBuilder<TDocument> PatternProperty(
109109
public EncryptedCollectionBuilder<TDocument> PatternProperty(
110110
string pattern,
111111
IEnumerable<BsonType> bsonTypes,
112-
CsfleEncryptionAlgorithm? algorithm = null,
112+
EncryptionAlgorithm? algorithm = null,
113113
Guid? keyId = null)
114114
{
115115
AddToPatternProperties(pattern, CreateEncryptDocument(bsonTypes, algorithm, keyId));
@@ -146,7 +146,7 @@ public EncryptedCollectionBuilder<TDocument> PatternProperty<TField>(
146146
public EncryptedCollectionBuilder<TDocument> Property<TField>(
147147
Expression<Func<TDocument, TField>> path,
148148
BsonType bsonType,
149-
CsfleEncryptionAlgorithm? algorithm = null,
149+
EncryptionAlgorithm? algorithm = null,
150150
Guid? keyId = null)
151151
=> Property(path, [bsonType], algorithm, keyId);
152152

@@ -156,7 +156,7 @@ public EncryptedCollectionBuilder<TDocument> Property<TField>(
156156
public EncryptedCollectionBuilder<TDocument> Property<TField>(
157157
Expression<Func<TDocument, TField>> path,
158158
IEnumerable<BsonType> bsonTypes,
159-
CsfleEncryptionAlgorithm? algorithm = null,
159+
EncryptionAlgorithm? algorithm = null,
160160
Guid? keyId = null)
161161
=> Property(new ExpressionFieldDefinition<TDocument, TField>(path), bsonTypes, algorithm, keyId);
162162

@@ -166,7 +166,7 @@ public EncryptedCollectionBuilder<TDocument> Property<TField>(
166166
public EncryptedCollectionBuilder<TDocument> Property(
167167
FieldDefinition<TDocument> path,
168168
BsonType bsonType,
169-
CsfleEncryptionAlgorithm? algorithm = null,
169+
EncryptionAlgorithm? algorithm = null,
170170
Guid? keyId = null)
171171
=> Property(path, [bsonType], algorithm, keyId);
172172

@@ -176,7 +176,7 @@ public EncryptedCollectionBuilder<TDocument> Property(
176176
public EncryptedCollectionBuilder<TDocument> Property(
177177
FieldDefinition<TDocument> path,
178178
IEnumerable<BsonType> bsonTypes,
179-
CsfleEncryptionAlgorithm? algorithm = null,
179+
EncryptionAlgorithm? algorithm = null,
180180
Guid? keyId = null)
181181
{
182182
var fieldName = path.Render(_args).FieldName;
@@ -211,7 +211,7 @@ public EncryptedCollectionBuilder<TDocument> Property<TField>(
211211

212212
private static BsonDocument CreateEncryptDocument(
213213
IEnumerable<BsonType> bsonTypes,
214-
CsfleEncryptionAlgorithm? algorithm = null,
214+
EncryptionAlgorithm? algorithm = null,
215215
Guid? keyId = null)
216216
{
217217
if (bsonTypes == null)
@@ -296,30 +296,14 @@ private void AddToProperties(string field, BsonDocument document)
296296
};
297297
}
298298

299-
private static string MapCsfleEncyptionAlgorithmToString(CsfleEncryptionAlgorithm algorithm)
299+
private static string MapCsfleEncyptionAlgorithmToString(EncryptionAlgorithm algorithm)
300300
{
301301
return algorithm switch
302302
{
303-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random => "AEAD_AES_256_CBC_HMAC_SHA_512-Random",
304-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic => "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
303+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random => "AEAD_AES_256_CBC_HMAC_SHA_512-Random",
304+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic => "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
305305
_ => throw new ArgumentException($"Unexpected algorithm type: {algorithm}.", nameof(algorithm))
306306
};
307307
}
308308
}
309-
310-
/// <summary>
311-
/// The type of possible encryption algorithms. //TODO Maybe we need a more generic name but EncryptionAlgorithm is already taken (it's a superset of these values)
312-
/// </summary>
313-
public enum CsfleEncryptionAlgorithm
314-
{
315-
/// <summary>
316-
/// Randomized encryption algorithm.
317-
/// </summary>
318-
AEAD_AES_256_CBC_HMAC_SHA_512_Random,
319-
320-
/// <summary>
321-
/// Deterministic encryption algorithm.
322-
/// </summary>
323-
AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic
324-
}
325309
}

tests/MongoDB.Driver.Tests/Encryption/CsfleSchemaBuilderTests.cs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,26 @@ public void CsfleSchemaBuilder_works_as_expected()
4040
builder
4141
.EncryptMetadata(keyId: _keyId)
4242
.Property(p => p.MedicalRecords, BsonType.Array,
43-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
43+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
4444
.Property("bloodType", BsonType.String,
45-
algorithm: CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
45+
algorithm: EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
4646
.Property(p => p.Ssn, BsonType.Int32,
47-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
47+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
4848
.Property(p => p.Insurance, innerBuilder =>
4949
{
5050
innerBuilder
5151
.Property(i => i.PolicyNumber, BsonType.Int32,
52-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic);
52+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic);
5353
})
54-
.PatternProperty("_PIIString$", BsonType.String, CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
55-
.PatternProperty("_PIIArray$", BsonType.Array, CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
54+
.PatternProperty("_PIIString$", BsonType.String, EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
55+
.PatternProperty("_PIIArray$", BsonType.Array, EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
5656
.PatternProperty(p => p.Insurance, innerBuilder =>
5757
{
5858
innerBuilder
5959
.PatternProperty("_PIIString$", BsonType.String,
60-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
60+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
6161
.PatternProperty("_PIINumber$", BsonType.Int32,
62-
algorithm: CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic);
62+
algorithm: EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic);
6363
});
6464

6565
} );
@@ -155,7 +155,7 @@ public void CsfleSchemaBuilder_WithMultipleTypes_works_as_expected()
155155
builder
156156
.EncryptMetadata(keyId: _keyId)
157157
.Property(p => p.MedicalRecords, BsonType.Array,
158-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
158+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
159159
});
160160

161161
schemaBuilder.Encrypt<TestClass>(testClassCollectionName, builder =>
@@ -201,14 +201,14 @@ public void CsfleSchemaBuilder_WithMultipleTypes_works_as_expected()
201201

202202
[Theory]
203203
[InlineData(
204-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
204+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
205205
null,
206206
""" "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
207207
[InlineData(
208208
null,
209209
_keyIdString,
210210
""" "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
211-
public void EncryptedCollection_Metadata_works_as_expected(CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
211+
public void EncryptedCollection_Metadata_works_as_expected(EncryptionAlgorithm? algorithm, string keyString, string expectedContent)
212212
{
213213
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
214214
var builder = new EncryptedCollectionBuilder<Patient>();
@@ -229,18 +229,18 @@ public void EncryptedCollection_Metadata_works_as_expected(CsfleEncryptionAlgori
229229

230230
[Theory]
231231
[InlineData(BsonType.Array,
232-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
232+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
233233
null,
234234
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
235235
[InlineData(BsonType.Array,
236236
null,
237237
_keyIdString,
238238
""" "bsonType": "array", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
239239
[InlineData(BsonType.Array,
240-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
240+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
241241
_keyIdString,
242242
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
243-
public void EncryptedCollection_PatternProperty_works_as_expected(BsonType bsonType, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
243+
public void EncryptedCollection_PatternProperty_works_as_expected(BsonType bsonType, EncryptionAlgorithm? algorithm, string keyString, string expectedContent)
244244
{
245245
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
246246
var builder = new EncryptedCollectionBuilder<Patient>();
@@ -265,18 +265,18 @@ public void EncryptedCollection_PatternProperty_works_as_expected(BsonType bsonT
265265

266266
[Theory]
267267
[InlineData(new[] {BsonType.Array, BsonType.String},
268-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
268+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
269269
null,
270270
""" "bsonType": ["array", "string"], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
271271
[InlineData(new[] {BsonType.Array, BsonType.String},
272272
null,
273273
_keyIdString,
274274
""" "bsonType": ["array", "string"], "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
275275
[InlineData(new[] {BsonType.Array, BsonType.String},
276-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
276+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
277277
_keyIdString,
278278
""" "bsonType": ["array", "string"], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
279-
public void EncryptedCollection_PatternPropertyWithMultipleBsonTypes_works_as_expected(IEnumerable<BsonType> bsonTypes, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
279+
public void EncryptedCollection_PatternPropertyWithMultipleBsonTypes_works_as_expected(IEnumerable<BsonType> bsonTypes, EncryptionAlgorithm? algorithm, string keyString, string expectedContent)
280280
{
281281
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
282282
var builder = new EncryptedCollectionBuilder<Patient>();
@@ -310,9 +310,9 @@ public void EncryptedCollection_PatternPropertyNested_works_as_expected()
310310
innerBuilder
311311
.EncryptMetadata(keyId)
312312
.Property("policyNumber", BsonType.Int32,
313-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
313+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
314314
.PatternProperty("randomRegex*", BsonType.String,
315-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
315+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
316316
});
317317

318318
var expected = """
@@ -359,9 +359,9 @@ public void EncryptedCollection_PatternPropertyNestedWithString_works_as_expecte
359359
innerBuilder
360360
.EncryptMetadata(keyId)
361361
.Property("policyNumber", BsonType.Int32,
362-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
362+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
363363
.PatternProperty("randomRegex*", BsonType.String,
364-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
364+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
365365
});
366366

367367
var expected = """
@@ -399,18 +399,18 @@ public void EncryptedCollection_PatternPropertyNestedWithString_works_as_expecte
399399

400400
[Theory]
401401
[InlineData(BsonType.Array,
402-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
402+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
403403
null,
404404
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
405405
[InlineData(BsonType.Array,
406406
null,
407407
_keyIdString,
408408
""" "bsonType": "array", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
409409
[InlineData(BsonType.Array,
410-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
410+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
411411
_keyIdString,
412412
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
413-
public void EncryptedCollection_PropertyWithExpression_works_as_expected(BsonType bsonType, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
413+
public void EncryptedCollection_PropertyWithExpression_works_as_expected(BsonType bsonType, EncryptionAlgorithm? algorithm, string keyString, string expectedContent)
414414
{
415415
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
416416
var builder = new EncryptedCollectionBuilder<Patient>();
@@ -435,18 +435,18 @@ public void EncryptedCollection_PropertyWithExpression_works_as_expected(BsonTyp
435435

436436
[Theory]
437437
[InlineData(new[] {BsonType.Array, BsonType.String},
438-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
438+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
439439
null,
440440
""" "bsonType": ["array", "string"], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
441441
[InlineData(new[] {BsonType.Array, BsonType.String},
442442
null,
443443
_keyIdString,
444444
""" "bsonType": ["array", "string"], "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
445445
[InlineData(new[] {BsonType.Array, BsonType.String},
446-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
446+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
447447
_keyIdString,
448448
""" "bsonType": ["array", "string"], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
449-
public void EncryptedCollection_PropertyWithMultipleBsonTypes_works_as_expected(IEnumerable<BsonType> bsonTypes, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
449+
public void EncryptedCollection_PropertyWithMultipleBsonTypes_works_as_expected(IEnumerable<BsonType> bsonTypes, EncryptionAlgorithm? algorithm, string keyString, string expectedContent)
450450
{
451451
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
452452
var builder = new EncryptedCollectionBuilder<Patient>();
@@ -471,18 +471,18 @@ public void EncryptedCollection_PropertyWithMultipleBsonTypes_works_as_expected(
471471

472472
[Theory]
473473
[InlineData(BsonType.Array,
474-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
474+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
475475
null,
476476
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
477477
[InlineData(BsonType.Array,
478478
null,
479479
_keyIdString,
480480
""" "bsonType": "array", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
481481
[InlineData(BsonType.Array,
482-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
482+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
483483
_keyIdString,
484484
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
485-
public void EncryptedCollection_PropertyWithString_works_as_expected(BsonType bsonType, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
485+
public void EncryptedCollection_PropertyWithString_works_as_expected(BsonType bsonType, EncryptionAlgorithm? algorithm, string keyString, string expectedContent)
486486
{
487487
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
488488
var builder = new EncryptedCollectionBuilder<Patient>();
@@ -516,9 +516,9 @@ public void EncryptedCollection_PropertyNested_works_as_expected()
516516
innerBuilder
517517
.EncryptMetadata(keyId)
518518
.Property("policyNumber", BsonType.Int32,
519-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
519+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
520520
.PatternProperty("randomRegex*", BsonType.String,
521-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
521+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
522522
});
523523

524524
var expected = """
@@ -565,9 +565,9 @@ public void EncryptedCollection_PropertyNestedWithString_works_as_expected()
565565
innerBuilder
566566
.EncryptMetadata(keyId)
567567
.Property("policyNumber", BsonType.Int32,
568-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
568+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
569569
.PatternProperty("randomRegex*", BsonType.String,
570-
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
570+
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
571571
});
572572

573573
var expected = """

0 commit comments

Comments
 (0)