21
21
import java .util .Collection ;
22
22
import java .util .List ;
23
23
import java .util .Map ;
24
+ import java .util .function .Supplier ;
24
25
25
26
import com .amazonaws .AmazonServiceException ;
26
27
import com .amazonaws .AmazonWebServiceRequest ;
48
49
* {@link AwsCrypto}.
49
50
*/
50
51
public final class KmsMasterKey extends MasterKey <KmsMasterKey > implements KmsMethods {
51
- private final AWSKMS kms_ ;
52
+ private final Supplier < AWSKMS > kms_ ;
52
53
private final MasterKeyProvider <KmsMasterKey > sourceProvider_ ;
53
54
private final String id_ ;
54
55
private final List <String > grantTokens_ = new ArrayList <>();
@@ -77,12 +78,12 @@ public static KmsMasterKey getInstance(final AWSCredentialsProvider creds, final
77
78
return new KmsMasterKeyProvider (creds , keyId ).getMasterKey (keyId );
78
79
}
79
80
80
- static KmsMasterKey getInstance (final AWSKMS kms , final String id ,
81
+ static KmsMasterKey getInstance (final Supplier < AWSKMS > kms , final String id ,
81
82
final MasterKeyProvider <KmsMasterKey > provider ) {
82
83
return new KmsMasterKey (kms , id , provider );
83
84
}
84
85
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 ) {
86
87
kms_ = kms ;
87
88
id_ = id ;
88
89
sourceProvider_ = provider ;
@@ -101,7 +102,7 @@ public String getKeyId() {
101
102
@ Override
102
103
public DataKey <KmsMasterKey > generateDataKey (final CryptoAlgorithm algorithm ,
103
104
final Map <String , String > encryptionContext ) {
104
- final GenerateDataKeyResult gdkResult = kms_ .generateDataKey (updateUserAgent (
105
+ final GenerateDataKeyResult gdkResult = kms_ .get (). generateDataKey (updateUserAgent (
105
106
new GenerateDataKeyRequest ()
106
107
.withKeyId (getKeyId ())
107
108
.withNumberOfBytes (algorithm .getDataKeyLength ())
@@ -145,7 +146,7 @@ public DataKey<KmsMasterKey> encryptDataKey(final CryptoAlgorithm algorithm,
145
146
throw new IllegalArgumentException ("Only RAW encoded keys are supported" );
146
147
}
147
148
try {
148
- final EncryptResult encryptResult = kms_ .encrypt (updateUserAgent (
149
+ final EncryptResult encryptResult = kms_ .get (). encrypt (updateUserAgent (
149
150
new EncryptRequest ()
150
151
.withKeyId (id_ )
151
152
.withPlaintext (ByteBuffer .wrap (key .getEncoded ()))
@@ -167,7 +168,7 @@ public DataKey<KmsMasterKey> decryptDataKey(final CryptoAlgorithm algorithm,
167
168
final List <Exception > exceptions = new ArrayList <>();
168
169
for (final EncryptedDataKey edk : encryptedDataKeys ) {
169
170
try {
170
- final DecryptResult decryptResult = kms_ .decrypt (updateUserAgent (
171
+ final DecryptResult decryptResult = kms_ .get (). decrypt (updateUserAgent (
171
172
new DecryptRequest ()
172
173
.withCiphertextBlob (ByteBuffer .wrap (edk .getEncryptedDataKey ()))
173
174
.withEncryptionContext (encryptionContext )
0 commit comments