|
| 1 | +package awsdbe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 8 | + "github.com/aws/aws-sdk-go-v2/config" |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/dynamodb" |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" |
| 11 | + |
| 12 | + "github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/dbesdkmiddleware" |
| 13 | + plaintexttoawsdbe "github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/examples/migration/PlaintextToAWSDBE" |
| 14 | + "github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/examples/utils" |
| 15 | + |
| 16 | + dbesdkdynamodbencryptiontypes "github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/awscryptographydbencryptionsdkdynamodbsmithygeneratedtypes" |
| 17 | +) |
| 18 | + |
| 19 | +/* |
| 20 | +Migration Step 1: This is an example demonstrating how to start using the |
| 21 | +AWS Database Encryption SDK with a pre-existing table with plaintext items. |
| 22 | +In this example, we configure a DynamoDb Encryption Interceptor to do the following: |
| 23 | + - Write items only in plaintext |
| 24 | + - Read items in plaintext or, if the item is encrypted, decrypt with our encryption configuration |
| 25 | +
|
| 26 | +While this step configures your client to be ready to start reading encrypted items, |
| 27 | +we do not yet expect to be reading any encrypted items, |
| 28 | +as our client still writes plaintext items. |
| 29 | +Before you move on to step 2, ensure that these changes have successfully been deployed |
| 30 | +to all of your readers. |
| 31 | +
|
| 32 | +Running this example requires access to the DDB Table whose name |
| 33 | +is provided in the function parameter. |
| 34 | +This table must be configured with the following |
| 35 | +primary key configuration: |
| 36 | + - Partition key is named "partition_key" with type (S) |
| 37 | + - Sort key is named "sort_key" with type (S) |
| 38 | +*/ |
| 39 | +func MigrationStep1(kmsKeyID, ddbTableName, partitionKeyValue, sortKeyWriteValue, sortKeyReadValue string) error { |
| 40 | + cfg, err := config.LoadDefaultConfig(context.TODO()) |
| 41 | + utils.HandleError(err) |
| 42 | + |
| 43 | + // 1. Configure your Keyring, attribute actions, |
| 44 | + // allowedUnsignedAttributes, and encryption configuration for table. |
| 45 | + // This is common across all the steps. |
| 46 | + |
| 47 | + // Note that while we still are not writing encrypted items, |
| 48 | + // and our key will not be used to encrypt items in this example, |
| 49 | + // our configuration specifies that we may read encrypted items, |
| 50 | + // and we should expect to be able to decrypt and process any encrypted items. |
| 51 | + // To that end, we must fully define our encryption configuration in |
| 52 | + // this step. |
| 53 | + |
| 54 | + // This `PlaintextOverrideForcePlaintextWriteAllowPlaintextRead` means: |
| 55 | + // - Write: Items are forced to be written as plaintext. |
| 56 | + // Items may not be written as encrypted items. |
| 57 | + // - Read: Items are allowed to be read as plaintext. |
| 58 | + // Items are allowed to be read as encrypted items. |
| 59 | + listOfTableConfigs := configureTable(kmsKeyID, ddbTableName, dbesdkdynamodbencryptiontypes.PlaintextOverrideForcePlaintextWriteAllowPlaintextRead) |
| 60 | + |
| 61 | + // 2. Create DynamoDB client with dbEsdkMiddleware |
| 62 | + dbEsdkMiddleware, err := dbesdkmiddleware.NewDBEsdkMiddleware(listOfTableConfigs) |
| 63 | + utils.HandleError(err) |
| 64 | + |
| 65 | + ddb := dynamodb.NewFromConfig(cfg, dbEsdkMiddleware.CreateMiddleware()) |
| 66 | + |
| 67 | + // 3. Put an item into your table. |
| 68 | + // This item will be stored in plaintext. |
| 69 | + encryptedAndSignedValue := "this will be encrypted and signed" |
| 70 | + signOnlyValue := "this will never be encrypted, but it will be signed" |
| 71 | + doNothingValue := "this will never be encrypted nor signed" |
| 72 | + item := map[string]types.AttributeValue{ |
| 73 | + "partition_key": &types.AttributeValueMemberS{Value: partitionKeyValue}, |
| 74 | + "sort_key": &types.AttributeValueMemberN{Value: sortKeyWriteValue}, |
| 75 | + "attribute1": &types.AttributeValueMemberS{Value: encryptedAndSignedValue}, |
| 76 | + "attribute2": &types.AttributeValueMemberS{Value: signOnlyValue}, |
| 77 | + "attribute3": &types.AttributeValueMemberS{Value: doNothingValue}, |
| 78 | + } |
| 79 | + |
| 80 | + putInput := dynamodb.PutItemInput{ |
| 81 | + TableName: &ddbTableName, |
| 82 | + Item: item, |
| 83 | + } |
| 84 | + |
| 85 | + _, err = ddb.PutItem(context.TODO(), &putInput) |
| 86 | + |
| 87 | + // We return this error because we run test against the error. |
| 88 | + // When used in production code, you can decide how you want to handle errors. |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + // 4. Get an item back from the table using the DynamoDb Client. |
| 94 | + // If this is an item written in plaintext (i.e. any item written |
| 95 | + // during Step 0 or 1), then the item will still be in plaintext. |
| 96 | + // If this is an item that was encrypted client-side (i.e. any item written |
| 97 | + // during Step 2 or after), then the item will be decrypted client-side |
| 98 | + // and surfaced as a plaintext item. |
| 99 | + key := map[string]types.AttributeValue{ |
| 100 | + "partition_key": &types.AttributeValueMemberS{Value: partitionKeyValue}, |
| 101 | + "sort_key": &types.AttributeValueMemberN{Value: sortKeyReadValue}, |
| 102 | + } |
| 103 | + |
| 104 | + getInput := &dynamodb.GetItemInput{ |
| 105 | + TableName: &ddbTableName, |
| 106 | + Key: key, |
| 107 | + ConsistentRead: aws.Bool(true), |
| 108 | + } |
| 109 | + |
| 110 | + result, err := ddb.GetItem(context.TODO(), getInput) |
| 111 | + // We return this error because we run test against the error. |
| 112 | + // When used in production code, you can decide how you want to handle errors. |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + // Verify we got the expected item back |
| 118 | + err = plaintexttoawsdbe.VerifyReturnedItem(result, partitionKeyValue, sortKeyReadValue, encryptedAndSignedValue, signOnlyValue, doNothingValue) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + fmt.Println("MigrationStep1 completed successfully") |
| 123 | + return nil |
| 124 | +} |
0 commit comments