Skip to content

Commit f313599

Browse files
Assert all values
1 parent e2e45eb commit f313599

File tree

5 files changed

+87
-37
lines changed

5 files changed

+87
-37
lines changed

Examples/runtimes/go/migration/PlaintextToAWSDBE/awsdbe/step1.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
1111

1212
"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"
1314
"github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/examples/utils"
1415

1516
dbesdkdynamodbencryptiontypes "github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/awscryptographydbencryptionsdkdynamodbsmithygeneratedtypes"
@@ -66,12 +67,14 @@ func MigrationStep1(kmsKeyID, ddbTableName, partitionKeyValue, sortKeyWriteValue
6667
// 3. Put an item into your table.
6768
// This item will be stored in plaintext.
6869
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"
6972
item := map[string]types.AttributeValue{
7073
"partition_key": &types.AttributeValueMemberS{Value: partitionKeyValue},
7174
"sort_key": &types.AttributeValueMemberN{Value: sortKeyWriteValue},
7275
"attribute1": &types.AttributeValueMemberS{Value: encryptedAndSignedValue},
73-
"attribute2": &types.AttributeValueMemberS{Value: "this will never be encrypted, but it will be signed"},
74-
"attribute3": &types.AttributeValueMemberS{Value: "this will never be encrypted nor signed"},
76+
"attribute2": &types.AttributeValueMemberS{Value: signOnlyValue},
77+
"attribute3": &types.AttributeValueMemberS{Value: doNothingValue},
7578
}
7679

7780
putInput := dynamodb.PutItemInput{
@@ -112,11 +115,9 @@ func MigrationStep1(kmsKeyID, ddbTableName, partitionKeyValue, sortKeyWriteValue
112115
}
113116

114117
// Verify we got the expected item back
115-
if partitionKeyValue != result.Item["partition_key"].(*types.AttributeValueMemberS).Value {
116-
panic("Decrypted item does not match original item")
117-
}
118-
if encryptedAndSignedValue != result.Item["attribute1"].(*types.AttributeValueMemberS).Value {
119-
panic("Decrypted item does not match original item")
118+
err = plaintexttoawsdbe.VerifyReturnedItem(result, partitionKeyValue, sortKeyReadValue, encryptedAndSignedValue, signOnlyValue, doNothingValue)
119+
if err != nil {
120+
return err
120121
}
121122
fmt.Println("MigrationStep1 completed successfully")
122123
return nil

Examples/runtimes/go/migration/PlaintextToAWSDBE/awsdbe/step2.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
dbesdkdynamodbencryptiontypes "github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/awscryptographydbencryptionsdkdynamodbsmithygeneratedtypes"
1616
"github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/dbesdkmiddleware"
17+
plaintexttoawsdbe "github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/examples/migration/PlaintextToAWSDBE"
1718
"github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/examples/utils"
1819
)
1920

@@ -64,12 +65,14 @@ func MigrationStep2(kmsKeyID, ddbTableName, partitionKeyValue, sortKeyWriteValue
6465
// 3. Put an item into your table.
6566
// This item will be encrypted.
6667
encryptedAndSignedValue := "this will be encrypted and signed"
68+
signOnlyValue := "this will never be encrypted, but it will be signed"
69+
doNothingValue := "this will never be encrypted nor signed"
6770
item := map[string]types.AttributeValue{
6871
"partition_key": &types.AttributeValueMemberS{Value: partitionKeyValue},
6972
"sort_key": &types.AttributeValueMemberN{Value: sortKeyWriteValue},
7073
"attribute1": &types.AttributeValueMemberS{Value: encryptedAndSignedValue},
71-
"attribute2": &types.AttributeValueMemberS{Value: "this will never be encrypted, but it will be signed"},
72-
"attribute3": &types.AttributeValueMemberS{Value: "this will never be encrypted nor signed"},
74+
"attribute2": &types.AttributeValueMemberS{Value: signOnlyValue},
75+
"attribute3": &types.AttributeValueMemberS{Value: doNothingValue},
7376
}
7477

7578
putInput := dynamodb.PutItemInput{
@@ -110,11 +113,9 @@ func MigrationStep2(kmsKeyID, ddbTableName, partitionKeyValue, sortKeyWriteValue
110113
}
111114

112115
// Verify we got the expected item back
113-
if partitionKeyValue != result.Item["partition_key"].(*types.AttributeValueMemberS).Value {
114-
panic("Decrypted item does not match original item")
115-
}
116-
if encryptedAndSignedValue != result.Item["attribute1"].(*types.AttributeValueMemberS).Value {
117-
panic("Decrypted item does not match original item")
116+
err = plaintexttoawsdbe.VerifyReturnedItem(result, partitionKeyValue, sortKeyReadValue, encryptedAndSignedValue, signOnlyValue, doNothingValue)
117+
if err != nil {
118+
return err
118119
}
119120
fmt.Println("MigrationStep2 completed successfully")
120121
return nil
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package plaintexttoawsdbe
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
7+
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
8+
)
9+
10+
func VerifyReturnedItem(result *dynamodb.GetItemOutput, partitionKeyValue, sortKeyValue, encryptedAndSignedValue, signOnlyValue, doNothingValue string) error {
11+
returnedPartitionKey, ok := result.Item["partition_key"].(*types.AttributeValueMemberS)
12+
fmt.Println(returnedPartitionKey.Value)
13+
if !ok {
14+
// We return this error because we run test against the error.
15+
// When used in production code, you can decide how you want to handle errors.
16+
return fmt.Errorf("partition_key is not a string attribute")
17+
}
18+
returnedsortKey, ok := result.Item["sort_key"].(*types.AttributeValueMemberN)
19+
if !ok {
20+
// We return this error because we run test against the error.
21+
// When used in production code, you can decide how you want to handle errors.
22+
return fmt.Errorf("sort_key is not a number attribute")
23+
}
24+
returnedAttribute1, ok := result.Item["attribute1"].(*types.AttributeValueMemberS)
25+
if !ok {
26+
// We return this error because we run test against the error.
27+
// When used in production code, you can decide how you want to handle errors.
28+
return fmt.Errorf("attribute1 is not a string attribute")
29+
}
30+
returnedAttribute2, ok := result.Item["attribute2"].(*types.AttributeValueMemberS)
31+
if !ok {
32+
// We return this error because we run test against the error.
33+
// When used in production code, you can decide how you want to handle errors.
34+
return fmt.Errorf("attribute2 is not a string attribute")
35+
}
36+
returnedAttribute3, ok := result.Item["attribute3"].(*types.AttributeValueMemberS)
37+
if !ok {
38+
// We return this error because we run test against the error.
39+
// When used in production code, you can decide how you want to handle errors.
40+
return fmt.Errorf("attribute3 is not a string attribute")
41+
}
42+
43+
if returnedPartitionKey.Value != partitionKeyValue {
44+
panic(fmt.Sprintf("Expected partition key %s, got %s", partitionKeyValue, returnedPartitionKey))
45+
}
46+
if returnedsortKey.Value != sortKeyValue {
47+
panic(fmt.Sprintf("Expected partition key %s, got %s", sortKeyValue, returnedsortKey))
48+
}
49+
if returnedAttribute1.Value != encryptedAndSignedValue {
50+
panic(fmt.Sprintf("Expected attribute1 value %s, got %s", encryptedAndSignedValue, returnedAttribute1.Value))
51+
}
52+
if returnedAttribute2.Value != signOnlyValue {
53+
panic(fmt.Sprintf("Expected attribute2 value %s, got %s", signOnlyValue, returnedAttribute2.Value))
54+
}
55+
if returnedAttribute3.Value != doNothingValue {
56+
panic(fmt.Sprintf("Expected attribute3 value %s, got %s", doNothingValue, returnedAttribute3.Value))
57+
}
58+
return nil
59+
}

Examples/runtimes/go/migration/PlaintextToAWSDBE/plaintext/step0.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
plaintexttoawsdbe "github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/examples/migration/PlaintextToAWSDBE"
78
"github.com/aws/aws-database-encryption-sdk-dynamodb/releases/go/dynamodb-esdk/examples/utils"
89
"github.com/aws/aws-sdk-go-v2/config"
910
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
@@ -38,12 +39,15 @@ func MigrationStep0(ddbTableName, partitionKeyValue, sortKeyWriteValue, sortKeyR
3839

3940
// 2. Put an example item into DynamoDB table
4041
// This item will be stored in plaintext.
42+
encryptedAndSignedValue := "this will be encrypted and signed"
43+
signOnlyValue := "this will never be encrypted, but it will be signed"
44+
doNothingValue := "this will never be encrypted nor signed"
4145
item := map[string]types.AttributeValue{
4246
"partition_key": &types.AttributeValueMemberS{Value: partitionKeyValue},
4347
"sort_key": &types.AttributeValueMemberN{Value: sortKeyWriteValue},
44-
"attribute1": &types.AttributeValueMemberS{Value: "this will be encrypted and signed"},
45-
"attribute2": &types.AttributeValueMemberS{Value: "this will never be encrypted, but it will be signed"},
46-
"attribute3": &types.AttributeValueMemberS{Value: "this will never be encrypted nor signed"},
48+
"attribute1": &types.AttributeValueMemberS{Value: encryptedAndSignedValue},
49+
"attribute2": &types.AttributeValueMemberS{Value: signOnlyValue},
50+
"attribute3": &types.AttributeValueMemberS{Value: doNothingValue},
4751
}
4852

4953
putInput := &dynamodb.PutItemInput{
@@ -79,24 +83,9 @@ func MigrationStep0(ddbTableName, partitionKeyValue, sortKeyWriteValue, sortKeyR
7983
panic("No item found")
8084
}
8185

82-
returnedPartitionKey, ok := result.Item["partition_key"].(*types.AttributeValueMemberS)
83-
if !ok {
84-
// We return this error because we run test against the error.
85-
// When used in production code, you can decide how you want to handle errors.
86-
return fmt.Errorf("partition_key is not a string attribute")
87-
}
88-
returnedAttribute1, ok := result.Item["attribute1"].(*types.AttributeValueMemberS)
89-
if !ok {
90-
// We return this error because we run test against the error.
91-
// When used in production code, you can decide how you want to handle errors.
92-
return fmt.Errorf("partition_key is not a string attribute")
93-
}
94-
95-
if returnedPartitionKey.Value != partitionKeyValue {
96-
panic(fmt.Sprintf("Expected partition key %s, got %s", partitionKeyValue, returnedPartitionKey))
97-
}
98-
if returnedAttribute1.Value != "this will be encrypted and signed" {
99-
panic(fmt.Sprintf("Expected attribute1 value, got %s", returnedAttribute1))
86+
err = plaintexttoawsdbe.VerifyReturnedItem(result, partitionKeyValue, sortKeyReadValue, encryptedAndSignedValue, signOnlyValue, doNothingValue)
87+
if err != nil {
88+
return err
10089
}
10190

10291
fmt.Println("MigrationStep0 completed successfully")

Examples/runtimes/go/migration/PlaintextToAWSDBE/plaintext/step0_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ func TestMigrationStep0(t *testing.T) {
3232

3333
// When: Execute Step 0 with sortReadValue=2, Then: should error out when reading encrypted items.
3434
err = MigrationStep0(tableName, partitionKey, sortKeys[0], sortKeys[2])
35-
utils.AssertErrorMessage(err, "partition_key is not a string attribute")
35+
utils.AssertErrorMessage(err, "attribute1 is not a string attribute")
3636

3737
// Given: Step 3 has succeeded (if it exists)
3838
awsdbe.MigrationStep3(kmsKeyID, tableName, partitionKey, sortKeys[3], sortKeys[3])
3939
// When: Execute Step 0 with sortReadValue=3, Then: should error out
4040
err = MigrationStep0(tableName, partitionKey, sortKeys[0], sortKeys[3])
41-
utils.AssertErrorMessage(err, "partition_key is not a string attribute")
41+
utils.AssertErrorMessage(err, "attribute1 is not a string attribute")
4242

4343
// Cleanup
4444
for _, sortKey := range sortKeys { // Only clean up items we created

0 commit comments

Comments
 (0)