Skip to content

Commit c16c0a7

Browse files
author
Bryan Donlan
committed
Avoid creating multiple clients if KmsMasterKeys are created before client initialization
Note that, while previously we would throw an exception when a MK was requested for a region that the MKP cannot service immediately on MK creation, we now throw on first use of the MK.
1 parent 4b85273 commit c16c0a7

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
### Minor Changes
66

77
* Restored the KMS client cache with a fix for the memory leak.
8+
* When using a master key provider that can only service a subset of regions
9+
(e.g. using the deprecated constructors), and requesting a master key from a
10+
region not servicable by that MKP, the exception will now be thrown on first
11+
use of the MK, rather than at getMasterKey time.
812

913
## 1.3.4
1014

src/main/java/com/amazonaws/encryptionsdk/kms/KmsMasterKey.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collection;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.function.Supplier;
2425

2526
import com.amazonaws.AmazonServiceException;
2627
import com.amazonaws.AmazonWebServiceRequest;
@@ -48,7 +49,7 @@
4849
* {@link AwsCrypto}.
4950
*/
5051
public final class KmsMasterKey extends MasterKey<KmsMasterKey> implements KmsMethods {
51-
private final AWSKMS kms_;
52+
private final Supplier<AWSKMS> kms_;
5253
private final MasterKeyProvider<KmsMasterKey> sourceProvider_;
5354
private final String id_;
5455
private final List<String> grantTokens_ = new ArrayList<>();
@@ -77,12 +78,12 @@ public static KmsMasterKey getInstance(final AWSCredentialsProvider creds, final
7778
return new KmsMasterKeyProvider(creds, keyId).getMasterKey(keyId);
7879
}
7980

80-
static KmsMasterKey getInstance(final AWSKMS kms, final String id,
81+
static KmsMasterKey getInstance(final Supplier<AWSKMS> kms, final String id,
8182
final MasterKeyProvider<KmsMasterKey> provider) {
8283
return new KmsMasterKey(kms, id, provider);
8384
}
8485

85-
private KmsMasterKey(final AWSKMS kms, final String id, final MasterKeyProvider<KmsMasterKey> provider) {
86+
private KmsMasterKey(final Supplier<AWSKMS> kms, final String id, final MasterKeyProvider<KmsMasterKey> provider) {
8687
kms_ = kms;
8788
id_ = id;
8889
sourceProvider_ = provider;
@@ -101,7 +102,7 @@ public String getKeyId() {
101102
@Override
102103
public DataKey<KmsMasterKey> generateDataKey(final CryptoAlgorithm algorithm,
103104
final Map<String, String> encryptionContext) {
104-
final GenerateDataKeyResult gdkResult = kms_.generateDataKey(updateUserAgent(
105+
final GenerateDataKeyResult gdkResult = kms_.get().generateDataKey(updateUserAgent(
105106
new GenerateDataKeyRequest()
106107
.withKeyId(getKeyId())
107108
.withNumberOfBytes(algorithm.getDataKeyLength())
@@ -145,7 +146,7 @@ public DataKey<KmsMasterKey> encryptDataKey(final CryptoAlgorithm algorithm,
145146
throw new IllegalArgumentException("Only RAW encoded keys are supported");
146147
}
147148
try {
148-
final EncryptResult encryptResult = kms_.encrypt(updateUserAgent(
149+
final EncryptResult encryptResult = kms_.get().encrypt(updateUserAgent(
149150
new EncryptRequest()
150151
.withKeyId(id_)
151152
.withPlaintext(ByteBuffer.wrap(key.getEncoded()))
@@ -167,7 +168,7 @@ public DataKey<KmsMasterKey> decryptDataKey(final CryptoAlgorithm algorithm,
167168
final List<Exception> exceptions = new ArrayList<>();
168169
for (final EncryptedDataKey edk : encryptedDataKeys) {
169170
try {
170-
final DecryptResult decryptResult = kms_.decrypt(updateUserAgent(
171+
final DecryptResult decryptResult = kms_.get().decrypt(updateUserAgent(
171172
new DecryptRequest()
172173
.withCiphertextBlob(ByteBuffer.wrap(edk.getEncryptedDataKey()))
173174
.withEncryptionContext(encryptionContext)

src/main/java/com/amazonaws/encryptionsdk/kms/KmsMasterKeyProvider.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Map;
2626
import java.util.Objects;
2727
import java.util.concurrent.ConcurrentHashMap;
28+
import java.util.function.Supplier;
2829

2930
import com.amazonaws.AmazonServiceException;
3031
import com.amazonaws.ClientConfiguration;
@@ -517,12 +518,17 @@ public KmsMasterKey getMasterKey(final String provider, final String keyId) thro
517518
regionName = defaultRegion_;
518519
}
519520

520-
AWSKMS kms = regionalClientSupplier_.getClient(regionName);
521-
if (kms == null) {
522-
throw new AwsCryptoException("Can't use keys from region " + regionName);
523-
}
521+
String regionName_ = regionName;
522+
523+
Supplier<AWSKMS> kmsSupplier = () -> {
524+
AWSKMS kms = regionalClientSupplier_.getClient(regionName_);
525+
if (kms == null) {
526+
throw new AwsCryptoException("Can't use keys from region " + regionName_);
527+
}
528+
return kms;
529+
};
524530

525-
final KmsMasterKey result = KmsMasterKey.getInstance(kms, keyId, this);
531+
final KmsMasterKey result = KmsMasterKey.getInstance(kmsSupplier, keyId, this);
526532
result.setGrantTokens(grantTokens_);
527533
return result;
528534
}

0 commit comments

Comments
 (0)