Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Empty string defers to SDK default region #127

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ public static Result<IKMSClient, Error> KMSClientForRegion(
final DafnySequence<? extends Character> region
) {
try {
final String regionString = new String(
(char[]) region.toArray().unwrap()
);
final char[] inputRegion = (char[]) region.toArray().unwrap();
// The ESDK uses empty string as a kind of none.
// In the case of an AWS KMS raw key identifier there is no region element
// an so "" is used in this case.
if (inputRegion.length == 0) {
return KMSClient();
}

final String regionString = new String(inputRegion);
final KmsClientBuilder builder = KmsClient.builder();
final KmsClient client = builder
.region(Region.of(regionString))
Expand Down
4 changes: 4 additions & 0 deletions ComAmazonawsKms/runtimes/net/Extern/KMSClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Dafny.ISequence<char> regionDafnyString
)
{
string regionStr = TypeConversion.FromDafny_N6_smithy__N3_api__S6_String(regionDafnyString);
// The ESDK uses empty string as a kind of none.
// In the case of an AWS KMS raw key identifier there is no region element
// an so "" is used in this case.
if (regionStr == "") return KMSClient();
var region = RegionEndpoint.GetBySystemName(regionStr);
var client = new DefaultKmsClient(region);

Expand Down
13 changes: 9 additions & 4 deletions ComAmazonawsKms/test/TestComAmazonawsKms.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module TestComAmazonawsKms {
nameonly expectedKeyId: Kms.Types.KeyIdType
)
{
var client :- expect Kms.KMSClient();
var client :- expect Kms.KMSClientForRegion(TEST_REGION);

var ret := client.Decrypt(input);

Expand All @@ -106,7 +106,7 @@ module TestComAmazonawsKms {
)
requires input.NumberOfBytes.Some?
{
var client :- expect Kms.KMSClient();
var client :- expect Kms.KMSClientForRegion(TEST_REGION);

var ret := client.GenerateDataKey(input);

Expand Down Expand Up @@ -138,7 +138,7 @@ module TestComAmazonawsKms {
nameonly input: Kms.Types.EncryptRequest
)
{
var client :- expect Kms.KMSClient();
var client :- expect Kms.KMSClientForRegion(TEST_REGION);

var ret := client.Encrypt(input);

Expand Down Expand Up @@ -167,9 +167,14 @@ module TestComAmazonawsKms {
// While we cannot easily test that the expected implementations
// return Some(), we can at least ensure that the ones that do are correct.
method {:test} RegionMatchTest() {
var client :- expect Kms.KMSClient();
var client :- expect Kms.KMSClientForRegion(TEST_REGION);
var region := Kms.RegionMatch(client, TEST_REGION);
expect region.None? || region.value;
}

// This should default to whatever default SDK uses.
method {:test} EmptyStringIsDefaultRegion() {
var client :- expect Kms.KMSClientForRegion("");
}

}
Loading