Skip to content

Commit 2e679b2

Browse files
Switch to supported ClientConfiguration.withUserAgentSuffix
1 parent 673c673 commit 2e679b2

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ target/
33
.settings/
44
.project
55
.classpath
6+
/bin/

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import javax.crypto.spec.SecretKeySpec;
2525

2626
import com.amazonaws.AmazonServiceException;
27-
import com.amazonaws.AmazonWebServiceRequest;
2827
import com.amazonaws.auth.AWSCredentials;
2928
import com.amazonaws.auth.AWSCredentialsProvider;
3029
import com.amazonaws.encryptionsdk.AwsCrypto;
@@ -35,7 +34,6 @@
3534
import com.amazonaws.encryptionsdk.MasterKeyProvider;
3635
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
3736
import com.amazonaws.encryptionsdk.exception.UnsupportedProviderException;
38-
import com.amazonaws.encryptionsdk.internal.VersionInfo;
3937
import com.amazonaws.services.kms.AWSKMS;
4038
import com.amazonaws.services.kms.model.DecryptRequest;
4139
import com.amazonaws.services.kms.model.DecryptResult;
@@ -92,12 +90,12 @@ public String getKeyId() {
9290
@Override
9391
public DataKey<KmsMasterKey> generateDataKey(final CryptoAlgorithm algorithm,
9492
final Map<String, String> encryptionContext) {
95-
final GenerateDataKeyResult gdkResult = kms_.generateDataKey(appendUserAgent(
93+
final GenerateDataKeyResult gdkResult = kms_.generateDataKey(
9694
new GenerateDataKeyRequest()
9795
.withKeyId(getKeyId())
9896
.withNumberOfBytes(algorithm.getDataKeyLength())
9997
.withEncryptionContext(encryptionContext)
100-
.withGrantTokens(grantTokens_))
98+
.withGrantTokens(grantTokens_)
10199
);
102100
final byte[] rawKey = new byte[algorithm.getDataKeyLength()];
103101
gdkResult.getPlaintext().get(rawKey);
@@ -136,12 +134,12 @@ public DataKey<KmsMasterKey> encryptDataKey(final CryptoAlgorithm algorithm,
136134
throw new IllegalArgumentException("Only RAW encoded keys are supported");
137135
}
138136
try {
139-
final EncryptResult encryptResult = kms_.encrypt(appendUserAgent(
137+
final EncryptResult encryptResult = kms_.encrypt(
140138
new EncryptRequest()
141139
.withKeyId(id_)
142140
.withPlaintext(ByteBuffer.wrap(key.getEncoded()))
143141
.withEncryptionContext(encryptionContext)
144-
.withGrantTokens(grantTokens_)));
142+
.withGrantTokens(grantTokens_));
145143
final byte[] edk = new byte[encryptResult.getCiphertextBlob().remaining()];
146144
encryptResult.getCiphertextBlob().get(edk);
147145
return new DataKey<>(dataKey.getKey(), edk, encryptResult.getKeyId().getBytes(StandardCharsets.UTF_8), this);
@@ -158,11 +156,11 @@ public DataKey<KmsMasterKey> decryptDataKey(final CryptoAlgorithm algorithm,
158156
final List<Exception> exceptions = new ArrayList<>();
159157
for (final EncryptedDataKey edk : encryptedDataKeys) {
160158
try {
161-
final DecryptResult decryptResult = kms_.decrypt(appendUserAgent(
159+
final DecryptResult decryptResult = kms_.decrypt(
162160
new DecryptRequest()
163161
.withCiphertextBlob(ByteBuffer.wrap(edk.getEncryptedDataKey()))
164162
.withEncryptionContext(encryptionContext)
165-
.withGrantTokens(grantTokens_)));
163+
.withGrantTokens(grantTokens_));
166164
if (decryptResult.getKeyId().equals(id_)) {
167165
final byte[] rawKey = new byte[algorithm.getDataKeyLength()];
168166
decryptResult.getPlaintext().get(rawKey);
@@ -181,9 +179,4 @@ public DataKey<KmsMasterKey> decryptDataKey(final CryptoAlgorithm algorithm,
181179

182180
throw buildCannotDecryptDksException(exceptions);
183181
}
184-
185-
private static <R extends AmazonWebServiceRequest> R appendUserAgent(final R request) {
186-
request.getRequestClientOptions().appendUserAgent(VersionInfo.USER_AGENT);
187-
return request;
188-
}
189182
}

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
3333
import com.amazonaws.encryptionsdk.exception.NoSuchMasterKeyException;
3434
import com.amazonaws.encryptionsdk.exception.UnsupportedProviderException;
35+
import com.amazonaws.encryptionsdk.internal.VersionInfo;
3536
import com.amazonaws.internal.StaticCredentialsProvider;
3637
import com.amazonaws.regions.Region;
3738
import com.amazonaws.regions.Regions;
@@ -43,6 +44,8 @@
4344
* if you want to use keys from multiple regions, you'll need multiple copies of this object.
4445
*/
4546
public class KmsMasterKeyProvider extends MasterKeyProvider<KmsMasterKey> implements KmsMethods {
47+
private static final ClientConfiguration DEFAULT_CLIENT_CONFIG =
48+
new ClientConfiguration().withUserAgentSuffix(VersionInfo.USER_AGENT);
4649
private static final String PROVIDER_NAME = "aws-kms";
4750
private final AWSKMS kms_;
4851
private final List<String> keyIds_;
@@ -55,7 +58,7 @@ public class KmsMasterKeyProvider extends MasterKeyProvider<KmsMasterKey> implem
5558
* to talk to the {@link Regions#DEFAULT_REGION}.
5659
*/
5760
public KmsMasterKeyProvider() {
58-
this(new AWSKMSClient(), Region.getRegion(Regions.DEFAULT_REGION), Collections.<String> emptyList());
61+
this(new AWSKMSClient(DEFAULT_CLIENT_CONFIG), Region.getRegion(Regions.DEFAULT_REGION), Collections.<String> emptyList());
5962
}
6063

6164
/**
@@ -64,7 +67,7 @@ public KmsMasterKeyProvider() {
6467
* {@code keyId} as appropriate.
6568
*/
6669
public KmsMasterKeyProvider(final String keyId) {
67-
this(new AWSKMSClient(), getStartingRegion(keyId), Collections.singletonList(keyId));
70+
this(new AWSKMSClient(DEFAULT_CLIENT_CONFIG), getStartingRegion(keyId), Collections.singletonList(keyId));
6871
}
6972

7073
/**
@@ -110,7 +113,10 @@ public KmsMasterKeyProvider(final AWSCredentialsProvider creds) {
110113
*/
111114
public KmsMasterKeyProvider(final AWSCredentialsProvider creds, final Region region,
112115
final ClientConfiguration clientConfiguration, final String keyId) {
113-
this(new AWSKMSClient(creds, clientConfiguration), region, Collections.singletonList(keyId));
116+
this(
117+
new AWSKMSClient(creds, clientConfiguration.withUserAgentSuffix(VersionInfo.USER_AGENT)),
118+
region,
119+
Collections.singletonList(keyId));
114120
}
115121

116122
/**
@@ -119,7 +125,10 @@ public KmsMasterKeyProvider(final AWSCredentialsProvider creds, final Region reg
119125
*/
120126
public KmsMasterKeyProvider(final AWSCredentialsProvider creds, final Region region,
121127
final ClientConfiguration clientConfiguration, final List<String> keyIds) {
122-
this(new AWSKMSClient(creds, clientConfiguration), region, keyIds);
128+
this(
129+
new AWSKMSClient(creds, clientConfiguration.withUserAgentSuffix(VersionInfo.USER_AGENT)),
130+
region,
131+
keyIds);
123132
}
124133

125134
/**

0 commit comments

Comments
 (0)