-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C#] Dynamodb updates and feature requests (#220)
* Add support for global dynamodb tables * Add support for EndpointConfiguration, TableName and RegionSuffix * Add more test cases * Added integration test to verify backwards compatibility * Remove BOM characters * Minor change in serviceURL * Update Directory.Build.props
- Loading branch information
Showing
21 changed files
with
518 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
csharp/AppEncryption/AppEncryption.IntegrationTests/Regression/DynamoDbCompatibilityTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Amazon.DynamoDBv2; | ||
using Amazon.DynamoDBv2.Model; | ||
using GoDaddy.Asherah.AppEncryption.Kms; | ||
using GoDaddy.Asherah.AppEncryption.Persistence; | ||
using GoDaddy.Asherah.AppEncryption.Tests; | ||
using GoDaddy.Asherah.AppEncryption.Tests.AppEncryption.Persistence; | ||
using GoDaddy.Asherah.Crypto; | ||
using Xunit; | ||
|
||
namespace GoDaddy.Asherah.AppEncryption.IntegrationTests.Regression | ||
{ | ||
public class DynamoDbCompatibilityTest : IClassFixture<DynamoDBContainerFixture>, IClassFixture<MetricsFixture> | ||
{ | ||
private const string StaticMasterKey = "thisIsAStaticMasterKeyForTesting"; | ||
private const string PartitionKey = "Id"; | ||
private const string SortKey = "Created"; | ||
private const string DefaultTableName = "EncryptionKey"; | ||
|
||
private readonly DynamoDbMetastoreImpl dynamoDbMetastoreImpl; | ||
private readonly DynamoDbMetastoreImpl dynamoDbMetastoreImplWithKeySuffix; | ||
|
||
public DynamoDbCompatibilityTest(DynamoDBContainerFixture dynamoDbContainerFixture) | ||
{ | ||
// Use AWS SDK to create client and initialize table | ||
AmazonDynamoDBConfig amazonDynamoDbConfig = new AmazonDynamoDBConfig | ||
{ | ||
ServiceURL = dynamoDbContainerFixture.ServiceUrl, | ||
AuthenticationRegion = "us-west-2", | ||
}; | ||
IAmazonDynamoDB tempDynamoDbClient = new AmazonDynamoDBClient(amazonDynamoDbConfig); | ||
CreateTableRequest request = new CreateTableRequest | ||
{ | ||
TableName = DefaultTableName, | ||
AttributeDefinitions = new List<AttributeDefinition> | ||
{ | ||
new AttributeDefinition(PartitionKey, ScalarAttributeType.S), | ||
new AttributeDefinition(SortKey, ScalarAttributeType.N), | ||
}, | ||
KeySchema = new List<KeySchemaElement> | ||
{ | ||
new KeySchemaElement(PartitionKey, KeyType.HASH), | ||
new KeySchemaElement(SortKey, KeyType.RANGE), | ||
}, | ||
ProvisionedThroughput = new ProvisionedThroughput(1L, 1L), | ||
}; | ||
tempDynamoDbClient.CreateTableAsync(request).Wait(); | ||
|
||
// Use a builder without the suffix | ||
dynamoDbMetastoreImpl = DynamoDbMetastoreImpl.NewBuilder() | ||
.WithEndPointConfiguration(dynamoDbContainerFixture.ServiceUrl, "us-west-2") | ||
.Build(); | ||
|
||
// Connect to the same metastore but initialize it with a key suffix | ||
dynamoDbMetastoreImplWithKeySuffix = DynamoDbMetastoreImpl.NewBuilder() | ||
.WithEndPointConfiguration(dynamoDbContainerFixture.ServiceUrl, "us-west-2") | ||
.WithKeySuffix("us-west-2") | ||
.Build(); | ||
} | ||
|
||
[Fact] | ||
private void TestRegionSuffixBackwardCompatibility() | ||
{ | ||
string dataRowString; | ||
string originalPayloadString; | ||
string decryptedPayloadString; | ||
|
||
// Encrypt originalPayloadString with metastore without key suffix | ||
using (SessionFactory sessionFactory = SessionFactory.NewBuilder("productId", "reference_app") | ||
.WithMetastore(dynamoDbMetastoreImpl) | ||
.WithCryptoPolicy(new NeverExpiredCryptoPolicy()) | ||
.WithKeyManagementService(new StaticKeyManagementServiceImpl(StaticMasterKey)) | ||
.Build()) | ||
{ | ||
using (Session<byte[], byte[]> sessionBytes = sessionFactory.GetSessionBytes("shopper123")) | ||
{ | ||
originalPayloadString = "mysupersecretpayload"; | ||
byte[] dataRowRecordBytes = | ||
sessionBytes.Encrypt(Encoding.UTF8.GetBytes(originalPayloadString)); | ||
|
||
// Consider this us "persisting" the DRR | ||
dataRowString = Convert.ToBase64String(dataRowRecordBytes); | ||
} | ||
} | ||
|
||
// Decrypt dataRowString with metastore with key suffix | ||
using (SessionFactory sessionFactory = SessionFactory.NewBuilder("productId", "reference_app") | ||
.WithMetastore(dynamoDbMetastoreImplWithKeySuffix) | ||
.WithCryptoPolicy(new NeverExpiredCryptoPolicy()) | ||
.WithKeyManagementService(new StaticKeyManagementServiceImpl(StaticMasterKey)) | ||
.Build()) | ||
{ | ||
using (Session<byte[], byte[]> sessionBytes = sessionFactory.GetSessionBytes("shopper123")) | ||
{ | ||
byte[] newDataRowRecordBytes = Convert.FromBase64String(dataRowString); | ||
|
||
// Decrypt the payload | ||
decryptedPayloadString = Encoding.UTF8.GetString(sessionBytes.Decrypt(newDataRowRecordBytes)); | ||
} | ||
} | ||
|
||
// Verify that we were able to decrypt with a suffixed builder | ||
Assert.Equal(decryptedPayloadString, originalPayloadString); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.