-
Notifications
You must be signed in to change notification settings - Fork 25
chore(.NET): update hierarchy examples #618
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
base: mainline
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,6 @@ | ||||||||||||||||||||||||||||||||||||||
| // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||||||||||||||||||||||||||||||||||||||
| // SPDX-License-Identifier: Apache-2.0 | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| using Amazon.DynamoDBv2; | ||||||||||||||||||||||||||||||||||||||
| using Amazon.KeyManagementService; | ||||||||||||||||||||||||||||||||||||||
| using AWS.Cryptography.EncryptionSDK; | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -34,11 +37,15 @@ | |||||||||||||||||||||||||||||||||||||
| /// or you can implement an interface that selects | ||||||||||||||||||||||||||||||||||||||
| /// the Branch Key based on the Encryption Context. | ||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||
| /// This example demonstrates configuring a Hierarchical Keyring | ||||||||||||||||||||||||||||||||||||||
| /// with a Branch Key ID Supplier to encrypt and decrypt data for | ||||||||||||||||||||||||||||||||||||||
| /// two separate tenants. | ||||||||||||||||||||||||||||||||||||||
| /// This example demonstrates how to: | ||||||||||||||||||||||||||||||||||||||
| /// - Create a key store | ||||||||||||||||||||||||||||||||||||||
| /// - Create a branch key | ||||||||||||||||||||||||||||||||||||||
| /// - Version a branch key | ||||||||||||||||||||||||||||||||||||||
| /// - Configure a Hierarchical Keyring | ||||||||||||||||||||||||||||||||||||||
| /// with a static branch key configuration to ensure we are restricting | ||||||||||||||||||||||||||||||||||||||
| /// access to a single tenant. | ||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||
| /// This example requires access to the DDB Table where you are storing the Branch Keys. | ||||||||||||||||||||||||||||||||||||||
| /// This example requires read, write, and DescribeTable access to the DDB Table where you are storing the Branch Keys. | ||||||||||||||||||||||||||||||||||||||
| /// This table must be configured with the following | ||||||||||||||||||||||||||||||||||||||
| /// primary key configuration: | ||||||||||||||||||||||||||||||||||||||
| /// - Partition key is named "partition_key" with type (S) | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -47,162 +54,147 @@ | |||||||||||||||||||||||||||||||||||||
| /// This example also requires using a KMS Key. | ||||||||||||||||||||||||||||||||||||||
| /// You need the following access on this key: | ||||||||||||||||||||||||||||||||||||||
| /// - GenerateDataKeyWithoutPlaintext | ||||||||||||||||||||||||||||||||||||||
| /// - ReEncrypt | ||||||||||||||||||||||||||||||||||||||
| /// - Decrypt | ||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||
| public class AwsKmsHierarchicalKeyring | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| // THESE ARE PUBLIC RESOURCES DO NOT USE IN A PRODUCTION ENVIRONMENT | ||||||||||||||||||||||||||||||||||||||
| private static string branchKeyIdA = "43574aa0-de30-424e-bad4-0b06f6e89478"; | ||||||||||||||||||||||||||||||||||||||
| private static string branchKeyIdB = "a2f4be37-15ec-489a-bcb5-dcce1f6bfe84"; | ||||||||||||||||||||||||||||||||||||||
| private static void Run(MemoryStream plaintext) | ||||||||||||||||||||||||||||||||||||||
| private void Run(MemoryStream plaintext) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| var kmsConfig = new KMSConfiguration { KmsKeyArn = ExampleUtils.ExampleUtils.GetBranchKeyArn() }; | ||||||||||||||||||||||||||||||||||||||
| // Create an AWS KMS Configuration to use with your KeyStore. | ||||||||||||||||||||||||||||||||||||||
| // The KMS Configuration MUST have the right access to the resources in the KeyStore. | ||||||||||||||||||||||||||||||||||||||
| var keystoreConfig = new KeyStoreConfig | ||||||||||||||||||||||||||||||||||||||
| // 1. Configure your KeyStore resource. | ||||||||||||||||||||||||||||||||||||||
| // `ddbTableName` is the name you want for the DDB table that | ||||||||||||||||||||||||||||||||||||||
| // will back your keystore. | ||||||||||||||||||||||||||||||||||||||
| // `kmsKeyArn` is the KMS Key that will protect your branch keys and beacon keys | ||||||||||||||||||||||||||||||||||||||
| // when they are stored in your DDB table. | ||||||||||||||||||||||||||||||||||||||
| var keyStoreConfig = new KeyStoreConfig | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| // Client MUST have permissions to decrypt kmsConfig.KmsKeyArn | ||||||||||||||||||||||||||||||||||||||
| KmsClient = new AmazonKeyManagementServiceClient(), | ||||||||||||||||||||||||||||||||||||||
| KmsConfiguration = kmsConfig, | ||||||||||||||||||||||||||||||||||||||
| KmsConfiguration = new KMSConfiguration{ KmsKeyArn = ExampleUtils.ExampleUtils.GetBranchKeyArn() }, | ||||||||||||||||||||||||||||||||||||||
| DdbTableName = ExampleUtils.ExampleUtils.GetKeyStoreName(), | ||||||||||||||||||||||||||||||||||||||
| DdbClient = new AmazonDynamoDBClient(), | ||||||||||||||||||||||||||||||||||||||
| LogicalKeyStoreName = ExampleUtils.ExampleUtils.GetLogicalKeyStoreName() | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
| var materialProviders = new MaterialProviders(new MaterialProvidersConfig()); | ||||||||||||||||||||||||||||||||||||||
| var keystore = new KeyStore(keystoreConfig); | ||||||||||||||||||||||||||||||||||||||
| var keyStore = new KeyStore(keyStoreConfig); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // 2. Create the DynamoDb table that will store the branch keys and beacon keys. | ||||||||||||||||||||||||||||||||||||||
| // This checks if the correct table already exists at `ddbTableName` | ||||||||||||||||||||||||||||||||||||||
| // by using the DescribeTable API. If no table exists, | ||||||||||||||||||||||||||||||||||||||
| // it will create one. If a table exists, it will verify | ||||||||||||||||||||||||||||||||||||||
| // the table's configuration and will error if the configuration is incorrect. | ||||||||||||||||||||||||||||||||||||||
| // It may take a couple minutes for the table to become ACTIVE, | ||||||||||||||||||||||||||||||||||||||
| // at which point it is ready to store branch and beacon keys. | ||||||||||||||||||||||||||||||||||||||
| // To create a KeyStore table, you need: | ||||||||||||||||||||||||||||||||||||||
| // - "dynamodb:DescribeTable", | ||||||||||||||||||||||||||||||||||||||
| // - "dynamodb:CreateTable", | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // This will NOT create a Key Store but check that the configured table name exists and it validates | ||||||||||||||||||||||||||||||||||||||
| // its construction. In order to check the construction is correct the configured IAM role | ||||||||||||||||||||||||||||||||||||||
| // MUST have `DescribeTable` permission on the Key Store resource. | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+93
to
+95
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion:
Suggested change
|
||||||||||||||||||||||||||||||||||||||
| keyStore.CreateKeyStore(new CreateKeyStoreInput()); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // We have already created a Table with the specified configuration. | ||||||||||||||||||||||||||||||||||||||
| // For testing purposes we will not create a table when we run this example. | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+98
to
+99
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // 3. Create a new branch key and beacon key in our KeyStore. | ||||||||||||||||||||||||||||||||||||||
| // Both the branch key and the beacon key will share an Id. | ||||||||||||||||||||||||||||||||||||||
| // This creation is eventually consistent. See the CreateBranchKey | ||||||||||||||||||||||||||||||||||||||
| // Example for how to populate this table. | ||||||||||||||||||||||||||||||||||||||
| // To create a Branch Key and a Beacon Key you need: | ||||||||||||||||||||||||||||||||||||||
| // - "dynamodb:PutItem", | ||||||||||||||||||||||||||||||||||||||
| // - "dynamodb:ConditionCheckItem", | ||||||||||||||||||||||||||||||||||||||
| // - "dynamodb:UpdateItem" | ||||||||||||||||||||||||||||||||||||||
texastony marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Create a branch key supplier that maps the branch key id to a more readable format | ||||||||||||||||||||||||||||||||||||||
| var branchKeySupplier = new ExampleBranchKeySupplier(branchKeyIdA, branchKeyIdB); | ||||||||||||||||||||||||||||||||||||||
| // var branchKeyId = CreateBranchKey.createBranchKey(); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Create an AWS Hierarchical Keyring with the branch key id supplier | ||||||||||||||||||||||||||||||||||||||
| // For testing purposes we will not create another Branch Key when we run this example. | ||||||||||||||||||||||||||||||||||||||
| // We will use an existing branch key created using the above code to run this | ||||||||||||||||||||||||||||||||||||||
| // example. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // THIS IS A PUBLIC RESOURCE DO NOT USE IN A PRODUCTION ENVIRONMENT | ||||||||||||||||||||||||||||||||||||||
| var branchKeyId = "43574aa0-de30-424e-bad4-0b06f6e89478"; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // 4. Create the Hierarchical Keyring, using the Branch Key ID Supplier above. | ||||||||||||||||||||||||||||||||||||||
| // With this configuration, the AWS SDK Client ultimately configured will be capable | ||||||||||||||||||||||||||||||||||||||
| // of encrypting or decrypting items for either tenant (assuming correct KMS access). | ||||||||||||||||||||||||||||||||||||||
| // We are restricting the keyring to only branch key by statically configuring | ||||||||||||||||||||||||||||||||||||||
| // it with a branch key id. For an examples on how to decide on which branch key to | ||||||||||||||||||||||||||||||||||||||
| // use see the `AwsKmsHierarchicalKeyringBranhcKeySupplier` example in this directory. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
| // 4. Create the Hierarchical Keyring, using the Branch Key ID Supplier above. | |
| // With this configuration, the AWS SDK Client ultimately configured will be capable | |
| // of encrypting or decrypting items for either tenant (assuming correct KMS access). | |
| // We are restricting the keyring to only branch key by statically configuring | |
| // it with a branch key id. For an examples on how to decide on which branch key to | |
| // use see the `AwsKmsHierarchicalKeyringBranhcKeySupplier` example in this directory. | |
| // 4. Create the Hierarchical Keyring, with a static Branch Key ID. | |
| // With this configuration, the AWS SDK Client ultimately configured will be capable | |
| // of encrypting or decrypting items for either tenant (assuming correct KMS access). | |
| // We are restricting the keyring to only branch key by statically configuring | |
| // it with a branch key id. For an examples on how to decide on which branch key to | |
| // use see the `AwsKmsHierarchicalKeyringBranhcKeySupplier` example in this directory. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue: Is this true?
I think that multiple of versions of the same Branch Key
count towards the Entry Capacity.
| // The value provided to `EntryCapacity` dictates how many branch keys will be held locally | |
| // The value provided to `EntryCapacity` dictates how many branch key versions will be held locally |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // Example for how to rotate a branch key. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Link to our docs.
The VersionBranchKey file and these comments do not explain
what rotation or an active branch key is.
We could add all that content here or in the other file.
Or we could link to our docs.
| // 8. Rotate the Branch Key in our KeyStore. | |
| // Only the branch key will be rotated. | |
| // This rotation is eventually consistent. See the VersionBranchKey | |
| // Example for how to rotate a branch key. | |
| // To rotate a branch key you need: | |
| // - "dynamodb:GetItem" | |
| // - "kms:ReEncrypt*" | |
| // - "kms:GenerateDataKeyWithoutPlaintext" | |
| // 8. Rotate the Branch Key in our KeyStore. | |
| // Only the branch key will be rotated. | |
| // This rotation is eventually consistent. | |
| // See the VersionBranchKey for a narrower example. | |
| // See the Developer Guide for details: https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-hierarchical-keyring.html#rotate-branch-key | |
| // Example for how to rotate a branch key. | |
| // To rotate a branch key you need: | |
| // - "dynamodb:GetItem" | |
| // - "kms:ReEncrypt*" | |
| // - "kms:GenerateDataKeyWithoutPlaintext" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Link to our docs.
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-hierarchical-keyring.html