-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathAttributeEncryptor.java
282 lines (240 loc) · 11.8 KB
/
AttributeEncryptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.services.dynamodbv2.datamodeling;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig.SaveBehavior;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingsRegistry.Mapping;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingsRegistry.Mappings;
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.DoNotEncrypt;
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.DoNotTouch;
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.DynamoDBEncryptor;
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.EncryptionContext;
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.EncryptionFlags;
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.HandleUnknownAttributes;
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.TableAadOverride;
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.providers.EncryptionMaterialsProvider;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Encrypts all non-key fields prior to storing them in DynamoDB.
* <em>This must be used with @{link SaveBehavior#PUT} or @{link SaveBehavior#CLOBBER}.</em>
*
* @author Greg Rubin
*/
public class AttributeEncryptor implements AttributeTransformer {
private static final Log LOG = LogFactory.getLog(AttributeEncryptor.class);
private final DynamoDBEncryptor encryptor;
private final Map<Class<?>, ModelClassMetadata> metadataCache = new ConcurrentHashMap<>();
public AttributeEncryptor(final DynamoDBEncryptor encryptor) {
this.encryptor = encryptor;
}
public AttributeEncryptor(final EncryptionMaterialsProvider encryptionMaterialsProvider) {
encryptor = DynamoDBEncryptor.getInstance(encryptionMaterialsProvider);
}
public DynamoDBEncryptor getEncryptor() {
return encryptor;
}
@Override
public Map<String, AttributeValue> transform(final Parameters<?> parameters) {
// one map of attributeFlags per model class
final ModelClassMetadata metadata = getModelClassMetadata(parameters);
final Map<String, AttributeValue> attributeValues = parameters.getAttributeValues();
// If this class is marked as "DoNotTouch" then we know our encryptor will not change it at all
// so we may as well fast-return and do nothing. This also avoids emitting errors when they would not apply.
if (metadata.doNotTouch) {
return attributeValues;
}
// When AttributeEncryptor is used without SaveBehavior.PUT or CLOBBER, it is trying to transform only a subset
// of the actual fields stored in DynamoDB. This means that the generated signature will not cover any
// unmodified fields. Thus, upon untransform, the signature verification will fail as it won't cover all
// expected fields.
if (parameters.isPartialUpdate()) {
throw new DynamoDBMappingException(
"Use of AttributeEncryptor without SaveBehavior.PUT or SaveBehavior.CLOBBER is an error " +
"and can result in data-corruption. This occured while trying to save " +
parameters.getModelClass());
}
try {
return encryptor.encryptRecord(
attributeValues,
metadata.getEncryptionFlags(),
paramsToContext(parameters));
} catch (Exception ex) {
throw new DynamoDBMappingException(ex);
}
}
@Override
public Map<String, AttributeValue> untransform(final Parameters<?> parameters) {
final Map<String, Set<EncryptionFlags>> attributeFlags = getEncryptionFlags(parameters);
try {
return encryptor.decryptRecord(
parameters.getAttributeValues(),
attributeFlags,
paramsToContext(parameters));
} catch (Exception ex) {
throw new DynamoDBMappingException(ex);
}
}
/*
* For any attributes we see from DynamoDB that aren't modeled in the mapper class,
* we either ignore them (the default behavior), or include them for encryption/signing
* based on the presence of the @HandleUnknownAttributes annotation (unless the class
* has @DoNotTouch, then we don't include them).
*/
private Map<String, Set<EncryptionFlags>> getEncryptionFlags(final Parameters<?> parameters) {
final ModelClassMetadata metadata = getModelClassMetadata(parameters);
// If the class is annotated with @DoNotTouch, then none of the attributes are
// encrypted or signed, so we don't need to bother looking for unknown attributes.
if (metadata.getDoNotTouch()) {
return metadata.getEncryptionFlags();
}
final Set<EncryptionFlags> unknownAttributeBehavior = metadata.getUnknownAttributeBehavior();
final Map<String, Set<EncryptionFlags>> attributeFlags = new HashMap<>();
attributeFlags.putAll(metadata.getEncryptionFlags());
for (final String attributeName : parameters.getAttributeValues().keySet()) {
if (!attributeFlags.containsKey(attributeName) &&
!encryptor.getSignatureFieldName().equals(attributeName) &&
!encryptor.getMaterialDescriptionFieldName().equals(attributeName)) {
attributeFlags.put(attributeName, unknownAttributeBehavior);
}
}
return attributeFlags;
}
private <T> ModelClassMetadata getModelClassMetadata(Parameters<T> parameters) {
// Due to the lack of explicit synchronization, it is possible that
// elements in the cache will be added multiple times. Since they will
// all be identical, this is okay. Avoiding explicit synchronization
// means that in the general (retrieval) case, should never block and
// should be extremely fast.
final Class<T> clazz = parameters.getModelClass();
ModelClassMetadata metadata = metadataCache.get(clazz);
if (metadata == null) {
Map<String, Set<EncryptionFlags>> attributeFlags = new HashMap<>();
final boolean handleUnknownAttributes = handleUnknownAttributes(clazz);
final EnumSet<EncryptionFlags> unknownAttributeBehavior = EnumSet.noneOf(EncryptionFlags.class);
if (shouldTouch(clazz)) {
Mappings mappings = DynamoDBMappingsRegistry.instance().mappingsOf(clazz);
for (Mapping mapping : mappings.getMappings()) {
final EnumSet<EncryptionFlags> flags = EnumSet.noneOf(EncryptionFlags.class);
if (shouldTouch(mapping)) {
if (shouldEncryptAttribute(clazz, mapping)) {
flags.add(EncryptionFlags.ENCRYPT);
}
flags.add(EncryptionFlags.SIGN);
}
attributeFlags.put(mapping.getAttributeName(), Collections.unmodifiableSet(flags));
}
if (handleUnknownAttributes) {
unknownAttributeBehavior.add(EncryptionFlags.SIGN);
if (shouldEncrypt(clazz)) {
unknownAttributeBehavior.add(EncryptionFlags.ENCRYPT);
}
}
}
metadata = new ModelClassMetadata(Collections.unmodifiableMap(attributeFlags), doNotTouch(clazz),
Collections.unmodifiableSet(unknownAttributeBehavior));
metadataCache.put(clazz, metadata);
}
return metadata;
}
/**
* @return True if {@link DoNotTouch} is not present on the class level. False otherwise
*/
private boolean shouldTouch(Class<?> clazz) {
return !doNotTouch(clazz);
}
/**
* @return True if {@link DoNotTouch} is not present on the getter level. False otherwise.
*/
private boolean shouldTouch(Mapping mapping) {
return !doNotTouch(mapping);
}
/**
* @return True if {@link DoNotTouch} IS present on the class level. False otherwise.
*/
private boolean doNotTouch(Class<?> clazz) {
return clazz.isAnnotationPresent(DoNotTouch.class);
}
/**
* @return True if {@link DoNotTouch} IS present on the getter level. False otherwise.
*/
private boolean doNotTouch(Mapping mapping) {
return mapping.getter().isAnnotationPresent(DoNotTouch.class);
}
/**
* @return True if {@link DoNotEncrypt} is NOT present on the class level. False otherwise.
*/
private boolean shouldEncrypt(Class<?> clazz) {
return !doNotEncrypt(clazz);
}
/**
* @return True if {@link DoNotEncrypt} IS present on the class level. False otherwise.
*/
private boolean doNotEncrypt(Class<?> clazz) {
return clazz.isAnnotationPresent(DoNotEncrypt.class);
}
/**
* @return True if {@link DoNotEncrypt} IS present on the getter level. False otherwise.
*/
private boolean doNotEncrypt(Mapping mapping) {
return mapping.getter().isAnnotationPresent(DoNotEncrypt.class);
}
/**
* @return True if the attribute should be encrypted, false otherwise.
*/
private boolean shouldEncryptAttribute(final Class<?> clazz, final Mapping mapping) {
return !(doNotEncrypt(clazz) || doNotEncrypt(mapping) || mapping.isPrimaryKey() || mapping.isVersion());
}
private static EncryptionContext paramsToContext(Parameters<?> params) {
final Class<?> clazz = params.getModelClass();
final TableAadOverride override = clazz.getAnnotation(TableAadOverride.class);
final String tableName = ((override == null) ? params.getTableName() : override.tableName());
return new EncryptionContext.Builder()
.withHashKeyName(params.getHashKeyName())
.withRangeKeyName(params.getRangeKeyName())
.withTableName(tableName)
.withModeledClass(params.getModelClass())
.withAttributeValues(params.getAttributeValues()).build();
}
private boolean handleUnknownAttributes(Class<?> clazz) {
return clazz.getAnnotation(HandleUnknownAttributes.class) != null;
}
private static class ModelClassMetadata {
private final Map<String, Set<EncryptionFlags>> encryptionFlags;
private final boolean doNotTouch;
private final Set<EncryptionFlags> unknownAttributeBehavior;
public ModelClassMetadata(Map<String, Set<EncryptionFlags>> encryptionFlags,
boolean doNotTouch, Set<EncryptionFlags> unknownAttributeBehavior) {
this.encryptionFlags = encryptionFlags;
this.doNotTouch = doNotTouch;
this.unknownAttributeBehavior = unknownAttributeBehavior;
}
public Map<String, Set<EncryptionFlags>> getEncryptionFlags() {
return encryptionFlags;
}
public boolean getDoNotTouch() {
return doNotTouch;
}
public Set<EncryptionFlags> getUnknownAttributeBehavior() {
return unknownAttributeBehavior;
}
}
}