Browse Source

Small fix

pull/1631/head
Ferdinando Papale 5 months ago
parent
commit
5dfcdb04fb
  1. 25
      src/MongoDB.Driver/Encryption/CsfleSchemaBuilder.cs
  2. 3
      tests/MongoDB.Driver.Tests/Encryption/CsfleSchemaBuilderTests.cs

25
src/MongoDB.Driver/Encryption/CsfleSchemaBuilder.cs

@ -198,10 +198,15 @@ namespace MongoDB.Driver.Encryption
schema.Add("properties", properties);
}
if (_metadata is not null)
{
schema.Merge(_metadata.Build(args));
}
return schema;
}
private static string MapCsfleEncyptionAlgorithmToString(CsfleEncyptionAlgorithm? algorithm)
private static string MapCsfleEncyptionAlgorithmToString(CsfleEncyptionAlgorithm algorithm)
{
return algorithm switch
{
@ -264,9 +269,9 @@ namespace MongoDB.Driver.Encryption
{
"encrypt", new BsonDocument
{
{ "algorithm", () => MapCsfleEncyptionAlgorithmToString(Algorithm), Algorithm is not null },
{ "algorithm", () => MapCsfleEncyptionAlgorithmToString(Algorithm!.Value), Algorithm is not null },
{ "bsonType", () => MapBsonTypeToString(BsonType!.Value), BsonType is not null },
{ "keyId", () => new BsonBinaryData(KeyId!.Value, GuidRepresentation.Standard), KeyId is not null },
{ "keyId", () => new BsonArray( new [] {new BsonBinaryData(KeyId!.Value, GuidRepresentation.Standard) }), KeyId is not null },
}
}
}
@ -317,6 +322,20 @@ namespace MongoDB.Driver.Encryption
KeyId = keyId;
Algorithm = algorithm;
}
public BsonDocument Build(RenderArgs<TDocument> args)
{
return new BsonDocument
{
{
"encryptMetadata", new BsonDocument
{
{ "algorithm", () => MapCsfleEncyptionAlgorithmToString(Algorithm!.Value), Algorithm is not null },
{ "keyId", () => new BsonArray( new [] {new BsonBinaryData(KeyId!.Value, GuidRepresentation.Standard) }), KeyId is not null },
}
}
};
}
}
}

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

@ -27,7 +27,10 @@ namespace MongoDB.Driver.Tests.Encryption
[Fact]
public void Test1()
{
var myKeyId = Guid.Parse("6f4af470-00d1-401f-ac39-f45902a0c0c8");
var typedBuilder = CsfleSchemaBuilder.GetTypeBuilder<Patient>()
.EncryptMetadata(keyId: myKeyId)
.Encrypt("bloodType", bsonType: BsonType.String,
algorithm: CsfleEncyptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
.Encrypt(p => p.Ssn, bsonType: BsonType.Int32,

Loading…
Cancel
Save