Browse Source

Moved builder to encryption project and removed CsfleEncryptionEnum (using the one that's already there)

pull/1631/head
Ferdinando Papale 3 months ago
parent
commit
678dcb29a6
  1. 38
      src/MongoDB.Driver.Encryption/CsfleSchemaBuilder.cs
  2. 68
      tests/MongoDB.Driver.Tests/Encryption/CsfleSchemaBuilderTests.cs

38
src/MongoDB.Driver/Encryption/CsfleSchemaBuilder.cs → src/MongoDB.Driver.Encryption/CsfleSchemaBuilder.cs

@ -78,7 +78,7 @@ namespace MongoDB.Driver.Encryption
/// <summary>
/// //TODO
/// </summary>
public EncryptedCollectionBuilder<TDocument> EncryptMetadata(Guid? keyId = null, CsfleEncryptionAlgorithm? algorithm = null)
public EncryptedCollectionBuilder<TDocument> EncryptMetadata(Guid? keyId = null, EncryptionAlgorithm? algorithm = null)
{
if (keyId is null && algorithm is null)
{
@ -99,7 +99,7 @@ namespace MongoDB.Driver.Encryption
public EncryptedCollectionBuilder<TDocument> PatternProperty(
string pattern,
BsonType bsonType,
CsfleEncryptionAlgorithm? algorithm = null,
EncryptionAlgorithm? algorithm = null,
Guid? keyId = null)
=> PatternProperty(pattern, [bsonType], algorithm, keyId);
@ -109,7 +109,7 @@ namespace MongoDB.Driver.Encryption
public EncryptedCollectionBuilder<TDocument> PatternProperty(
string pattern,
IEnumerable<BsonType> bsonTypes,
CsfleEncryptionAlgorithm? algorithm = null,
EncryptionAlgorithm? algorithm = null,
Guid? keyId = null)
{
AddToPatternProperties(pattern, CreateEncryptDocument(bsonTypes, algorithm, keyId));
@ -146,7 +146,7 @@ namespace MongoDB.Driver.Encryption
public EncryptedCollectionBuilder<TDocument> Property<TField>(
Expression<Func<TDocument, TField>> path,
BsonType bsonType,
CsfleEncryptionAlgorithm? algorithm = null,
EncryptionAlgorithm? algorithm = null,
Guid? keyId = null)
=> Property(path, [bsonType], algorithm, keyId);
@ -156,7 +156,7 @@ namespace MongoDB.Driver.Encryption
public EncryptedCollectionBuilder<TDocument> Property<TField>(
Expression<Func<TDocument, TField>> path,
IEnumerable<BsonType> bsonTypes,
CsfleEncryptionAlgorithm? algorithm = null,
EncryptionAlgorithm? algorithm = null,
Guid? keyId = null)
=> Property(new ExpressionFieldDefinition<TDocument, TField>(path), bsonTypes, algorithm, keyId);
@ -166,7 +166,7 @@ namespace MongoDB.Driver.Encryption
public EncryptedCollectionBuilder<TDocument> Property(
FieldDefinition<TDocument> path,
BsonType bsonType,
CsfleEncryptionAlgorithm? algorithm = null,
EncryptionAlgorithm? algorithm = null,
Guid? keyId = null)
=> Property(path, [bsonType], algorithm, keyId);
@ -176,7 +176,7 @@ namespace MongoDB.Driver.Encryption
public EncryptedCollectionBuilder<TDocument> Property(
FieldDefinition<TDocument> path,
IEnumerable<BsonType> bsonTypes,
CsfleEncryptionAlgorithm? algorithm = null,
EncryptionAlgorithm? algorithm = null,
Guid? keyId = null)
{
var fieldName = path.Render(_args).FieldName;
@ -211,7 +211,7 @@ namespace MongoDB.Driver.Encryption
private static BsonDocument CreateEncryptDocument(
IEnumerable<BsonType> bsonTypes,
CsfleEncryptionAlgorithm? algorithm = null,
EncryptionAlgorithm? algorithm = null,
Guid? keyId = null)
{
if (bsonTypes == null)
@ -296,30 +296,14 @@ namespace MongoDB.Driver.Encryption
};
}
private static string MapCsfleEncyptionAlgorithmToString(CsfleEncryptionAlgorithm algorithm)
private static string MapCsfleEncyptionAlgorithmToString(EncryptionAlgorithm algorithm)
{
return algorithm switch
{
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random => "AEAD_AES_256_CBC_HMAC_SHA_512-Random",
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic => "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random => "AEAD_AES_256_CBC_HMAC_SHA_512-Random",
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic => "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
_ => throw new ArgumentException($"Unexpected algorithm type: {algorithm}.", nameof(algorithm))
};
}
}
/// <summary>
/// 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)
/// </summary>
public enum CsfleEncryptionAlgorithm
{
/// <summary>
/// Randomized encryption algorithm.
/// </summary>
AEAD_AES_256_CBC_HMAC_SHA_512_Random,
/// <summary>
/// Deterministic encryption algorithm.
/// </summary>
AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic
}
}

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

@ -40,26 +40,26 @@ namespace MongoDB.Driver.Tests.Encryption
builder
.EncryptMetadata(keyId: _keyId)
.Property(p => p.MedicalRecords, BsonType.Array,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
.Property("bloodType", BsonType.String,
algorithm: CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
algorithm: EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
.Property(p => p.Ssn, BsonType.Int32,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
.Property(p => p.Insurance, innerBuilder =>
{
innerBuilder
.Property(i => i.PolicyNumber, BsonType.Int32,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic);
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic);
})
.PatternProperty("_PIIString$", BsonType.String, CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
.PatternProperty("_PIIArray$", BsonType.Array, CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
.PatternProperty("_PIIString$", BsonType.String, EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
.PatternProperty("_PIIArray$", BsonType.Array, EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
.PatternProperty(p => p.Insurance, innerBuilder =>
{
innerBuilder
.PatternProperty("_PIIString$", BsonType.String,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
.PatternProperty("_PIINumber$", BsonType.Int32,
algorithm: CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic);
algorithm: EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic);
});
} );
@ -155,7 +155,7 @@ namespace MongoDB.Driver.Tests.Encryption
builder
.EncryptMetadata(keyId: _keyId)
.Property(p => p.MedicalRecords, BsonType.Array,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
});
schemaBuilder.Encrypt<TestClass>(testClassCollectionName, builder =>
@ -201,14 +201,14 @@ namespace MongoDB.Driver.Tests.Encryption
[Theory]
[InlineData(
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
null,
""" "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
[InlineData(
null,
_keyIdString,
""" "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
public void EncryptedCollection_Metadata_works_as_expected(CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
public void EncryptedCollection_Metadata_works_as_expected(EncryptionAlgorithm? algorithm, string keyString, string expectedContent)
{
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
var builder = new EncryptedCollectionBuilder<Patient>();
@ -229,7 +229,7 @@ namespace MongoDB.Driver.Tests.Encryption
[Theory]
[InlineData(BsonType.Array,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
null,
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
[InlineData(BsonType.Array,
@ -237,10 +237,10 @@ namespace MongoDB.Driver.Tests.Encryption
_keyIdString,
""" "bsonType": "array", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
[InlineData(BsonType.Array,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
_keyIdString,
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
public void EncryptedCollection_PatternProperty_works_as_expected(BsonType bsonType, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
public void EncryptedCollection_PatternProperty_works_as_expected(BsonType bsonType, EncryptionAlgorithm? algorithm, string keyString, string expectedContent)
{
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
var builder = new EncryptedCollectionBuilder<Patient>();
@ -265,7 +265,7 @@ namespace MongoDB.Driver.Tests.Encryption
[Theory]
[InlineData(new[] {BsonType.Array, BsonType.String},
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
null,
""" "bsonType": ["array", "string"], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
[InlineData(new[] {BsonType.Array, BsonType.String},
@ -273,10 +273,10 @@ namespace MongoDB.Driver.Tests.Encryption
_keyIdString,
""" "bsonType": ["array", "string"], "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
[InlineData(new[] {BsonType.Array, BsonType.String},
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
_keyIdString,
""" "bsonType": ["array", "string"], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
public void EncryptedCollection_PatternPropertyWithMultipleBsonTypes_works_as_expected(IEnumerable<BsonType> bsonTypes, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
public void EncryptedCollection_PatternPropertyWithMultipleBsonTypes_works_as_expected(IEnumerable<BsonType> bsonTypes, EncryptionAlgorithm? algorithm, string keyString, string expectedContent)
{
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
var builder = new EncryptedCollectionBuilder<Patient>();
@ -310,9 +310,9 @@ namespace MongoDB.Driver.Tests.Encryption
innerBuilder
.EncryptMetadata(keyId)
.Property("policyNumber", BsonType.Int32,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
.PatternProperty("randomRegex*", BsonType.String,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
});
var expected = """
@ -359,9 +359,9 @@ namespace MongoDB.Driver.Tests.Encryption
innerBuilder
.EncryptMetadata(keyId)
.Property("policyNumber", BsonType.Int32,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
.PatternProperty("randomRegex*", BsonType.String,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
});
var expected = """
@ -399,7 +399,7 @@ namespace MongoDB.Driver.Tests.Encryption
[Theory]
[InlineData(BsonType.Array,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
null,
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
[InlineData(BsonType.Array,
@ -407,10 +407,10 @@ namespace MongoDB.Driver.Tests.Encryption
_keyIdString,
""" "bsonType": "array", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
[InlineData(BsonType.Array,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
_keyIdString,
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
public void EncryptedCollection_PropertyWithExpression_works_as_expected(BsonType bsonType, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
public void EncryptedCollection_PropertyWithExpression_works_as_expected(BsonType bsonType, EncryptionAlgorithm? algorithm, string keyString, string expectedContent)
{
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
var builder = new EncryptedCollectionBuilder<Patient>();
@ -435,7 +435,7 @@ namespace MongoDB.Driver.Tests.Encryption
[Theory]
[InlineData(new[] {BsonType.Array, BsonType.String},
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
null,
""" "bsonType": ["array", "string"], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
[InlineData(new[] {BsonType.Array, BsonType.String},
@ -443,10 +443,10 @@ namespace MongoDB.Driver.Tests.Encryption
_keyIdString,
""" "bsonType": ["array", "string"], "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
[InlineData(new[] {BsonType.Array, BsonType.String},
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
_keyIdString,
""" "bsonType": ["array", "string"], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
public void EncryptedCollection_PropertyWithMultipleBsonTypes_works_as_expected(IEnumerable<BsonType> bsonTypes, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
public void EncryptedCollection_PropertyWithMultipleBsonTypes_works_as_expected(IEnumerable<BsonType> bsonTypes, EncryptionAlgorithm? algorithm, string keyString, string expectedContent)
{
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
var builder = new EncryptedCollectionBuilder<Patient>();
@ -471,7 +471,7 @@ namespace MongoDB.Driver.Tests.Encryption
[Theory]
[InlineData(BsonType.Array,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
null,
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random" """)]
[InlineData(BsonType.Array,
@ -479,10 +479,10 @@ namespace MongoDB.Driver.Tests.Encryption
_keyIdString,
""" "bsonType": "array", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
[InlineData(BsonType.Array,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
_keyIdString,
""" "bsonType": "array", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random", "keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }] """)]
public void EncryptedCollection_PropertyWithString_works_as_expected(BsonType bsonType, CsfleEncryptionAlgorithm? algorithm, string keyString, string expectedContent)
public void EncryptedCollection_PropertyWithString_works_as_expected(BsonType bsonType, EncryptionAlgorithm? algorithm, string keyString, string expectedContent)
{
Guid? keyId = keyString is null ? null : Guid.Parse(keyString);
var builder = new EncryptedCollectionBuilder<Patient>();
@ -516,9 +516,9 @@ namespace MongoDB.Driver.Tests.Encryption
innerBuilder
.EncryptMetadata(keyId)
.Property("policyNumber", BsonType.Int32,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
.PatternProperty("randomRegex*", BsonType.String,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
});
var expected = """
@ -565,9 +565,9 @@ namespace MongoDB.Driver.Tests.Encryption
innerBuilder
.EncryptMetadata(keyId)
.Property("policyNumber", BsonType.Int32,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
.PatternProperty("randomRegex*", BsonType.String,
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
});
var expected = """

Loading…
Cancel
Save