forked from aws/aws-encryption-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKmsMasterKeyProvider.java
454 lines (396 loc) · 16.5 KB
/
KmsMasterKeyProvider.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazonaws.encryptionsdk.kmssdkv2;
import static com.amazonaws.encryptionsdk.internal.AwsKmsCmkArnInfo.parseInfoFromKeyArn;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import com.amazonaws.encryptionsdk.*;
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
import com.amazonaws.encryptionsdk.exception.NoSuchMasterKeyException;
import com.amazonaws.encryptionsdk.exception.UnsupportedProviderException;
import com.amazonaws.encryptionsdk.internal.AwsKmsCmkArnInfo;
import com.amazonaws.encryptionsdk.kms.DiscoveryFilter;
import com.amazonaws.encryptionsdk.kms.KmsMethods;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.kms.KmsClient;
import software.amazon.awssdk.services.kms.KmsClientBuilder;
/**
* Provides {@link MasterKey}s backed by the AWS Key Management Service. This object is regional and
* if you want to use keys from multiple regions, you'll need multiple copies of this object.
*
* <p>This component is not multi-Region key aware, and will treat every AWS KMS identifier as
* regionally isolated.
*/
public class KmsMasterKeyProvider extends MasterKeyProvider<KmsMasterKey> implements KmsMethods {
private static final String PROVIDER_NAME = "aws-kms";
private final List<String> keyIds_;
private final List<String> grantTokens_;
private final boolean isDiscovery_;
private final DiscoveryFilter discoveryFilter_;
private final RegionalClientSupplier regionalClientSupplier_;
private final Region defaultRegion_;
public static class Builder implements Cloneable {
private Region defaultRegion_ = null;
private Supplier<KmsClientBuilder> builderSupplier_ = null;
private RegionalClientSupplier regionalClientSupplier_ = null;
private DiscoveryFilter discoveryFilter_ = null;
Builder() {
// Default access: Don't allow outside classes to extend this class
}
public Builder clone() {
try {
Builder cloned = (Builder) super.clone();
cloned.builderSupplier_ = builderSupplier_;
return cloned;
} catch (CloneNotSupportedException e) {
throw new Error("Impossible: CloneNotSupportedException", e);
}
}
/**
* Sets the default region. This region will be used when specifying key IDs for encryption or
* in {@link KmsMasterKeyProvider#getMasterKey(String)} that are not full ARNs, but are instead
* bare key IDs or aliases.
*
* <p>If the default region is not specified, only full key ARNs will be usable.
*
* @param defaultRegion The default region to use.
* @return
*/
public Builder defaultRegion(Region defaultRegion) {
this.defaultRegion_ = defaultRegion;
return this;
}
/**
* Provides a custom factory function that will vend KMS clients. This is provided for advanced
* use cases which require complete control over the client construction process.
*
* <p>Because the regional client supplier fully controls the client construction process, it is
* not possible to configure the client through methods such as {@link
* #builderSupplier(Supplier)}; if you try to use these in combination, an {@link
* IllegalStateException} will be thrown.
*
* <p>Note: The AWS Encryption SDK for Java does not support the {@code KmsAsyncClient}
* interface.
*
* @param regionalClientSupplier
* @return
*/
public Builder customRegionalClientSupplier(RegionalClientSupplier regionalClientSupplier) {
if (builderSupplier_ != null) {
throw clientSupplierComboException();
}
regionalClientSupplier_ = regionalClientSupplier;
return this;
}
/**
* Configures the {@link KmsMasterKeyProvider} to use settings from this {@link
* KmsClientBuilder} to configure KMS clients. Note that the region set on this builder will be
* ignored, but all other settings will be propagated into the regional clients.
*
* <p>Trying to use this method in combination with {@link
* #customRegionalClientSupplier(RegionalClientSupplier)} will cause an {@link
* IllegalStateException} to be thrown.
*
* <p>Note: The AWS Encryption SDK for Java does not support the {@code KmsAsyncClient}
* interface.
*
* @param supplier Should return a new {@link KmsClientBuilder} on each invocation.
* @return
*/
public Builder builderSupplier(Supplier<KmsClientBuilder> supplier) {
if (regionalClientSupplier_ != null) {
throw clientSupplierComboException();
}
this.builderSupplier_ = supplier;
return this;
}
private RuntimeException clientSupplierComboException() {
return new IllegalStateException(
"only one of builderSupplier and customRegionalClientSupplier may be used");
}
/**
* Builds the master key provider in Discovery Mode. In Discovery Mode the KMS Master Key
* Provider will attempt to decrypt using any key identifier it discovers in the encrypted
* message. KMS Master Key Providers in Discovery Mode will not encrypt data keys.
*
* @return
*/
public KmsMasterKeyProvider buildDiscovery() {
final boolean isDiscovery = true;
RegionalClientSupplier supplier = clientFactory();
return new KmsMasterKeyProvider(
supplier, defaultRegion_, emptyList(), emptyList(), isDiscovery, discoveryFilter_);
}
/**
* Builds the master key provider in Discovery Mode with a {@link DiscoveryFilter}. In Discovery
* Mode the KMS Master Key Provider will attempt to decrypt using any key identifier it
* discovers in the encrypted message that is accepted by the {@code filter}. KMS Master Key
* Providers in Discovery Mode will not encrypt data keys.
*
* @param filter
* @return
*/
public KmsMasterKeyProvider buildDiscovery(DiscoveryFilter filter) {
if (filter == null) {
throw new IllegalArgumentException(
"Discovery filter must not be null if specifying " + "a discovery filter.");
}
discoveryFilter_ = filter;
return buildDiscovery();
}
/**
* Builds the master key provider in Strict Mode. KMS Master Key Providers in Strict Mode will
* only attempt to decrypt using key ARNs listed in {@code keyIds}. KMS Master Key Providers in
* Strict Mode will encrypt data keys using the keys listed in {@code keyIds}
*
* <p>In Strict Mode, one or more CMKs must be provided. For providers that will only be used
* for encryption, you can use any valid KMS key identifier. For providers that will be used for
* decryption, you must use the key ARN; key ids, alias names, and alias ARNs are not supported.
*
* @param keyIds
* @return
*/
public KmsMasterKeyProvider buildStrict(List<String> keyIds) {
if (keyIds == null) {
throw new IllegalArgumentException(
"Strict mode must be configured with a non-empty " + "list of keyIds.");
}
final boolean isDiscovery = false;
RegionalClientSupplier supplier = clientFactory();
return new KmsMasterKeyProvider(
supplier, defaultRegion_, new ArrayList<>(keyIds), emptyList(), isDiscovery, null);
}
/**
* Builds the master key provider in strict mode. KMS Master Key Providers in Strict Mode will
* only attempt to decrypt using key ARNs listed in {@code keyIds}. KMS Master Key Providers in
* Strict Mode will encrypt data keys using the keys listed in {@code keyIds}
*
* <p>In Strict Mode, one or more CMKs must be provided. For providers that will only be used
* for encryption, you can use any valid KMS key identifier. For providers that will be used for
* decryption, you must use the key ARN; key ids, alias names, and alias ARNs are not supported.
*
* @param keyIds
* @return
*/
public KmsMasterKeyProvider buildStrict(String... keyIds) {
return buildStrict(asList(keyIds));
}
RegionalClientSupplier clientFactory() {
if (regionalClientSupplier_ != null) {
return regionalClientSupplier_;
}
ConcurrentHashMap<Region, KmsClient> clientCache = new ConcurrentHashMap<>();
snoopClientCache(clientCache);
return region -> {
KmsClient client = clientCache.get(region);
if (client != null) return client;
KmsClientBuilder builder =
builderSupplier_ != null ? builderSupplier_.get() : KmsClient.builder();
// We can't just use computeIfAbsent as we need to avoid leaking KMS clients if we're asked
// to decrypt
// an EDK with a bogus region in its ARN. So we'll install a request handler to identify the
// first
// successful call, and cache it when we see that.
RequestClientCacher cacher = new RequestClientCacher(clientCache, region);
ClientOverrideConfiguration overrideConfig =
builder.overrideConfiguration().toBuilder().addExecutionInterceptor(cacher).build();
client = builder.region(region).overrideConfiguration(overrideConfig).build();
return cacher.setClient(client);
};
}
protected void snoopClientCache(ConcurrentHashMap<Region, KmsClient> map) {
// no-op - this is a test hook
}
}
public static Builder builder() {
return new Builder();
}
KmsMasterKeyProvider(
RegionalClientSupplier supplier,
Region defaultRegion,
List<String> keyIds,
List<String> grantTokens,
boolean isDiscovery,
DiscoveryFilter discoveryFilter) {
if (!isDiscovery && (keyIds == null || keyIds.isEmpty())) {
throw new IllegalArgumentException(
"Strict mode must be configured with a non-empty " + "list of keyIds.");
}
if (!isDiscovery && keyIds.contains(null)) {
throw new IllegalArgumentException(
"Strict mode cannot be configured with a " + "null key identifier.");
}
if (!isDiscovery && discoveryFilter != null) {
throw new IllegalArgumentException(
"Strict mode cannot be configured with a " + "discovery filter.");
}
// If we don't have a default region, we need to check that all key IDs will be usable
if (!isDiscovery && defaultRegion == null) {
for (String keyId : keyIds) {
final AwsKmsCmkArnInfo arnInfo = parseInfoFromKeyArn(keyId);
if (arnInfo == null) {
throw new AwsCryptoException(
"Can't use non-ARN key identifiers or aliases when " + "no default region is set");
}
}
}
this.regionalClientSupplier_ = supplier;
this.defaultRegion_ = defaultRegion;
this.keyIds_ = Collections.unmodifiableList(new ArrayList<>(keyIds));
this.isDiscovery_ = isDiscovery;
this.discoveryFilter_ = discoveryFilter;
this.grantTokens_ = grantTokens;
}
/** Returns "aws-kms" */
@Override
public String getDefaultProviderId() {
return PROVIDER_NAME;
}
@Override
public KmsMasterKey getMasterKey(final String provider, final String keyId)
throws UnsupportedProviderException, NoSuchMasterKeyException {
if (!canProvide(provider)) {
throw new UnsupportedProviderException();
}
if (!isDiscovery_ && !keyIds_.contains(keyId)) {
throw new NoSuchMasterKeyException("Key must be in supplied list of keyIds.");
}
final AwsKmsCmkArnInfo arnInfo = parseInfoFromKeyArn(keyId);
if (isDiscovery_ && discoveryFilter_ != null && (arnInfo == null)) {
throw new NoSuchMasterKeyException(
"Cannot use non-ARN key identifiers or aliases if " + "discovery filter is configured.");
} else if (isDiscovery_
&& discoveryFilter_ != null
&& !discoveryFilter_.allowsPartitionAndAccount(
arnInfo.getPartition(), arnInfo.getAccountId())) {
throw new NoSuchMasterKeyException(
"Cannot use key in partition "
+ arnInfo.getPartition()
+ " with account id "
+ arnInfo.getAccountId()
+ " with configured discovery filter.");
}
Region region = defaultRegion_;
if (arnInfo != null) {
region = Region.of(arnInfo.getRegion());
}
final Region region_ = region;
Supplier<KmsClient> kmsSupplier =
() -> {
KmsClient client = regionalClientSupplier_.getClient(region_);
if (client == null) {
throw new AwsCryptoException("Can't use keys from region " + region_.id());
}
return client;
};
final KmsMasterKey result = KmsMasterKey.getInstance(kmsSupplier, keyId, this);
result.setGrantTokens(grantTokens_);
return result;
}
/** Returns all CMKs provided to the constructor of this object. */
@Override
public List<KmsMasterKey> getMasterKeysForEncryption(final MasterKeyRequest request) {
if (keyIds_ == null) {
return emptyList();
}
List<KmsMasterKey> result = new ArrayList<>(keyIds_.size());
for (String id : keyIds_) {
result.add(getMasterKey(id));
}
return result;
}
@Override
public DataKey<KmsMasterKey> decryptDataKey(
final CryptoAlgorithm algorithm,
final Collection<? extends EncryptedDataKey> encryptedDataKeys,
final Map<String, String> encryptionContext)
throws AwsCryptoException {
final List<Exception> exceptions = new ArrayList<>();
for (final EncryptedDataKey edk : encryptedDataKeys) {
if (canProvide(edk.getProviderId())) {
try {
final String keyArn = new String(edk.getProviderInformation(), StandardCharsets.UTF_8);
// This will throw if we can't use this key for whatever reason
return getMasterKey(keyArn)
.decryptDataKey(algorithm, singletonList(edk), encryptionContext);
} catch (final Exception ex) {
exceptions.add(ex);
}
}
}
throw buildCannotDecryptDksException(exceptions);
}
/**
* @deprecated This method is inherently not thread safe. Use {@link
* KmsMasterKey#setGrantTokens(List)} instead. {@link KmsMasterKeyProvider}s constructed using
* the builder will throw an exception on attempts to modify the list of grant tokens.
*/
@Deprecated
@Override
public void setGrantTokens(final List<String> grantTokens) {
try {
this.grantTokens_.clear();
this.grantTokens_.addAll(grantTokens);
} catch (UnsupportedOperationException e) {
throw grantTokenError();
}
}
@Override
public List<String> getGrantTokens() {
return new ArrayList<>(grantTokens_);
}
/**
* @deprecated This method is inherently not thread safe. Use {@link #withGrantTokens(List)} or
* {@link KmsMasterKey#setGrantTokens(List)} instead. {@link KmsMasterKeyProvider}s
* constructed using the builder will throw an exception on attempts to modify the list of
* grant tokens.
*/
@Deprecated
@Override
public void addGrantToken(final String grantToken) {
try {
grantTokens_.add(grantToken);
} catch (UnsupportedOperationException e) {
throw grantTokenError();
}
}
private RuntimeException grantTokenError() {
return new IllegalStateException(
"This master key provider is immutable. Use withGrantTokens instead.");
}
/**
* Returns a new {@link KmsMasterKeyProvider} that is configured identically to this one, except
* with the given list of grant tokens. The grant token list in the returned provider is immutable
* (but can be further overridden by invoking withGrantTokens again).
*
* @param grantTokens
* @return
*/
public KmsMasterKeyProvider withGrantTokens(List<String> grantTokens) {
grantTokens = Collections.unmodifiableList(new ArrayList<>(grantTokens));
return new KmsMasterKeyProvider(
regionalClientSupplier_,
defaultRegion_,
keyIds_,
grantTokens,
isDiscovery_,
discoveryFilter_);
}
/**
* Returns a new {@link KmsMasterKeyProvider} that is configured identically to this one, except
* with the given list of grant tokens. The grant token list in the returned provider is immutable
* (but can be further overridden by invoking withGrantTokens again).
*
* @param grantTokens
* @return
*/
public KmsMasterKeyProvider withGrantTokens(String... grantTokens) {
return withGrantTokens(asList(grantTokens));
}
}