Skip to content

Commit

Permalink
chore(Java): polish Java examples
Browse files Browse the repository at this point in the history
  • Loading branch information
texastony committed Dec 17, 2024
1 parent 4b2b629 commit b98cd0c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,13 @@ public static KeyManagementStrategy decryptEncryptStrategy(
@Nullable KmsClient decryptKmsClient,
@Nullable KmsClient encryptKmsClient
) {
decryptKmsClient = kms(decryptKmsClient);
encryptKmsClient = kms(encryptKmsClient);

return KeyManagementStrategy
.builder()
.AwsKmsDecryptEncrypt(
AwsKmsDecryptEncrypt
.builder()
.decrypt(AwsKms.builder().kmsClient(decryptKmsClient).build())
.encrypt(AwsKms.builder().kmsClient(encryptKmsClient).build())
.decrypt(AwsKms.builder().kmsClient(kms(decryptKmsClient)).build())
.encrypt(AwsKms.builder().kmsClient(kms(encryptKmsClient)).build())
.build()
)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
package software.amazon.cryptography.example.hierarchy.mutations;

import static software.amazon.cryptography.example.hierarchy.mutations.MutationsProvider.MutableBranchKeyPropertiesToString;
import static software.amazon.cryptography.example.hierarchy.mutations.MutationsProvider.MutationsToString;
import static software.amazon.cryptography.example.hierarchy.mutations.MutationsProvider.executeInitialize;

import java.util.Objects;
Expand Down Expand Up @@ -61,7 +63,15 @@ public static DescribeMutationOutput Example(
"\n It was started on: " +
description.MutationDetails().CreateTime() +
"\n The Input was: " +
description.MutationDetails().Input()
MutationsToString(description.MutationDetails().Input()) +
"\n The Original Properties are: " +
MutableBranchKeyPropertiesToString(
description.MutationDetails().Original()
) +
"\n The Terminal Properties are: " +
MutableBranchKeyPropertiesToString(
description.MutationDetails().Terminal()
)
);
// The Description object holds Details and the Token.
System.out.println(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static String End2End(
final KeyManagementStrategy strategy = AdminProvider.strategy(null);

System.out.println("BranchKey ID to mutate: " + branchKeyId);
HashMap<String, String> terminalEC = new HashMap<>();
HashMap<String, String> terminalEC = new HashMap<>(2, 1);
terminalEC.put("Robbie", "is a dog.");
Mutations mutations = Mutations
.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -20,6 +21,7 @@
import software.amazon.cryptography.keystoreadmin.model.InitializeMutationOutput;
import software.amazon.cryptography.keystoreadmin.model.KeyManagementStrategy;
import software.amazon.cryptography.keystoreadmin.model.KmsSymmetricEncryption;
import software.amazon.cryptography.keystoreadmin.model.MutableBranchKeyProperties;
import software.amazon.cryptography.keystoreadmin.model.MutatedBranchKeyItem;
import software.amazon.cryptography.keystoreadmin.model.MutationToken;
import software.amazon.cryptography.keystoreadmin.model.Mutations;
Expand All @@ -28,16 +30,6 @@

public class MutationsProvider {

public static String mutatedItemsToString(
List<MutatedBranchKeyItem> mutatedItems
) {
return mutatedItems
.stream()
.map(item -> String.format("%s : %s", item.ItemType(), item.Description())
)
.collect(Collectors.joining(",\n"));
}

public static Mutations defaultMutation(
@Nonnull final String terminalKmsArn
) {
Expand Down Expand Up @@ -153,6 +145,16 @@ static ApplyMutationResult workPage(
return result;
}

public static String mutatedItemsToString(
List<MutatedBranchKeyItem> mutatedItems
) {
return mutatedItems
.stream()
.map(item -> String.format("%s : %s", item.ItemType(), item.Description())
)
.collect(Collectors.joining(",\n"));
}

static MutationToken executeInitialize(
String branchKeyId,
KeyStoreAdmin admin,
Expand Down Expand Up @@ -219,4 +221,33 @@ static void resetMutationIndex(
"\n"
);
}

static String MutationsToString(Mutations mutations) {
final String kmsArn = mutations.TerminalKmsArn().isEmpty()
? ""
: "KMS ARN: " + mutations.TerminalKmsArn();
final String ec = mutations.TerminalEncryptionContext().isEmpty()
? ""
: "Encryption Context: " +
mutations.TerminalEncryptionContext().toString();
if (!kmsArn.isEmpty() && !ec.isEmpty()) {
return kmsArn + "\n" + ec;
}
return kmsArn + ec;
}

static String MutableBranchKeyPropertiesToString(
MutableBranchKeyProperties properties
) {
final String kmsArn = "KMS ARN: " + properties.KmsArn();
if (properties.CustomEncryptionContext().isEmpty()) {
return kmsArn;
}
return (
kmsArn +
"\n" +
"Encryption Context: " +
properties.CustomEncryptionContext().toString()
);
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ If you are using the AWS Cryptographic Material Providers Library for Python you

## Supported Languages

- Java
- [Java (link to examples)](https://github.com/aws/aws-cryptographic-material-providers-library/tree/main/AwsCryptographicMaterialProviders/runtimes/java/src/examples)
- .NET
- Python
- Dafny
Expand Down

0 comments on commit b98cd0c

Please sign in to comment.