23
23
import java .util .Map ;
24
24
25
25
import com .amazonaws .AmazonServiceException ;
26
+ import com .amazonaws .AmazonWebServiceRequest ;
26
27
import com .amazonaws .auth .AWSCredentials ;
27
28
import com .amazonaws .auth .AWSCredentialsProvider ;
28
29
import com .amazonaws .encryptionsdk .AwsCrypto ;
33
34
import com .amazonaws .encryptionsdk .MasterKeyProvider ;
34
35
import com .amazonaws .encryptionsdk .exception .AwsCryptoException ;
35
36
import com .amazonaws .encryptionsdk .exception .UnsupportedProviderException ;
37
+ import com .amazonaws .encryptionsdk .internal .VersionInfo ;
36
38
import com .amazonaws .services .kms .AWSKMS ;
37
39
import com .amazonaws .services .kms .model .DecryptRequest ;
38
40
import com .amazonaws .services .kms .model .DecryptResult ;
@@ -51,6 +53,12 @@ public final class KmsMasterKey extends MasterKey<KmsMasterKey> implements KmsMe
51
53
private final String id_ ;
52
54
private final List <String > grantTokens_ = new ArrayList <>();
53
55
56
+ private <T extends AmazonWebServiceRequest > T updateUserAgent (T request ) {
57
+ request .getRequestClientOptions ().appendUserAgent (VersionInfo .USER_AGENT );
58
+
59
+ return request ;
60
+ }
61
+
54
62
/**
55
63
*
56
64
* @deprecated Use a {@link KmsMasterKeyProvider} to obtain {@link KmsMasterKey}s.
@@ -93,13 +101,13 @@ public String getKeyId() {
93
101
@ Override
94
102
public DataKey <KmsMasterKey > generateDataKey (final CryptoAlgorithm algorithm ,
95
103
final Map <String , String > encryptionContext ) {
96
- final GenerateDataKeyResult gdkResult = kms_ .generateDataKey (
104
+ final GenerateDataKeyResult gdkResult = kms_ .generateDataKey (updateUserAgent (
97
105
new GenerateDataKeyRequest ()
98
106
.withKeyId (getKeyId ())
99
107
.withNumberOfBytes (algorithm .getDataKeyLength ())
100
108
.withEncryptionContext (encryptionContext )
101
109
.withGrantTokens (grantTokens_ )
102
- );
110
+ ) );
103
111
final byte [] rawKey = new byte [algorithm .getDataKeyLength ()];
104
112
gdkResult .getPlaintext ().get (rawKey );
105
113
if (gdkResult .getPlaintext ().remaining () > 0 ) {
@@ -137,12 +145,12 @@ public DataKey<KmsMasterKey> encryptDataKey(final CryptoAlgorithm algorithm,
137
145
throw new IllegalArgumentException ("Only RAW encoded keys are supported" );
138
146
}
139
147
try {
140
- final EncryptResult encryptResult = kms_ .encrypt (
148
+ final EncryptResult encryptResult = kms_ .encrypt (updateUserAgent (
141
149
new EncryptRequest ()
142
150
.withKeyId (id_ )
143
151
.withPlaintext (ByteBuffer .wrap (key .getEncoded ()))
144
152
.withEncryptionContext (encryptionContext )
145
- .withGrantTokens (grantTokens_ ));
153
+ .withGrantTokens (grantTokens_ ))) ;
146
154
final byte [] edk = new byte [encryptResult .getCiphertextBlob ().remaining ()];
147
155
encryptResult .getCiphertextBlob ().get (edk );
148
156
return new DataKey <>(dataKey .getKey (), edk , encryptResult .getKeyId ().getBytes (StandardCharsets .UTF_8 ), this );
@@ -159,11 +167,11 @@ public DataKey<KmsMasterKey> decryptDataKey(final CryptoAlgorithm algorithm,
159
167
final List <Exception > exceptions = new ArrayList <>();
160
168
for (final EncryptedDataKey edk : encryptedDataKeys ) {
161
169
try {
162
- final DecryptResult decryptResult = kms_ .decrypt (
170
+ final DecryptResult decryptResult = kms_ .decrypt (updateUserAgent (
163
171
new DecryptRequest ()
164
172
.withCiphertextBlob (ByteBuffer .wrap (edk .getEncryptedDataKey ()))
165
173
.withEncryptionContext (encryptionContext )
166
- .withGrantTokens (grantTokens_ ));
174
+ .withGrantTokens (grantTokens_ ))) ;
167
175
if (decryptResult .getKeyId ().equals (id_ )) {
168
176
final byte [] rawKey = new byte [algorithm .getDataKeyLength ()];
169
177
decryptResult .getPlaintext ().get (rawKey );
0 commit comments