Skip to content

Commit

Permalink
fix: Update user agent string to correctly reflect version (#141)
Browse files Browse the repository at this point in the history
* fix: Update user agent string to correctly reflect version
  • Loading branch information
farleyb-amazon authored Mar 18, 2021
1 parent fd6b20e commit bf72702
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
7 changes: 7 additions & 0 deletions sdk1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@
</repositories>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.amazonaws.services.kms.model.GenerateDataKeyRequest;
import com.amazonaws.services.kms.model.GenerateDataKeyResult;
import com.amazonaws.util.StringUtils;
import com.amazonaws.util.VersionInfoUtils;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
Expand All @@ -43,6 +42,7 @@
import static com.amazonaws.services.dynamodbv2.datamodeling.encryption.materials.WrappedRawMaterials.CONTENT_KEY_ALGORITHM;
import static com.amazonaws.services.dynamodbv2.datamodeling.encryption.materials.WrappedRawMaterials.ENVELOPE_KEY;
import static com.amazonaws.services.dynamodbv2.datamodeling.encryption.materials.WrappedRawMaterials.KEY_WRAPPING_ALGORITHM;
import static com.amazonaws.services.dynamodbv2.datamodeling.internal.Utils.loadVersion;

/**
* Generates a unique data key for each record in DynamoDB and protects that key
Expand All @@ -53,9 +53,8 @@
* @see <a href="http://docs.aws.amazon.com/kms/latest/developerguide/encrypt-context.html">KMS Encryption Context</a>
*/
public class DirectKmsMaterialProvider implements EncryptionMaterialsProvider {
private static final String VERSION_STRING = "1.0";
private static final String USER_AGENT = DirectKmsMaterialProvider.class.getName()
+ "/" + VERSION_STRING + "/" + VersionInfoUtils.getVersion();
static final String USER_AGENT_PREFIX = "DynamodbEncryptionSdkJava/";
private static final String USER_AGENT = USER_AGENT_PREFIX + loadVersion();
private static final String COVERED_ATTR_CTX_KEY = "aws-kms-ec-attr";
private static final String SIGNING_KEY_ALGORITHM = "amzn-ddb-sig-alg";
private static final String TABLE_NAME_EC_KEY = "*aws-kms-table*";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
*/
package com.amazonaws.services.dynamodbv2.datamodeling.internal;

import java.io.IOException;
import java.security.SecureRandom;
import java.util.Properties;

public class Utils {
private static final ThreadLocal<SecureRandom> RND = new ThreadLocal<SecureRandom>() {
Expand Down Expand Up @@ -47,4 +49,18 @@ public static <V> V checkNotNull(final V ref, final String errMsg) {
return ref;
}
}

/*
* Loads the version of the library
*/
public static String loadVersion() {
try {
final Properties properties = new Properties();
properties.load(ClassLoader.getSystemResourceAsStream("project.properties"));
return properties.getProperty("version");
} catch (final IOException ex) {
return "unknown";
}
}

}
1 change: 1 addition & 0 deletions sdk1/src/main/resources/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=${project.version}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package com.amazonaws.services.dynamodbv2.datamodeling.encryption.providers;

import com.amazonaws.RequestClientOptions;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException;
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.EncryptionContext;
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.materials.DecryptionMaterials;
Expand Down Expand Up @@ -337,6 +338,19 @@ public GenerateDataKeyResult generateDataKey(GenerateDataKeyRequest r) {
assertTrue(gdkCalled.get());
}

@Test
public void userAgentIsAdded() {
AWSKMS kmsSpy = new FakeKMS() {
@Override
public GenerateDataKeyResult generateDataKey(GenerateDataKeyRequest r) {
assertTrue(r.getRequestClientOptions().getClientMarker(RequestClientOptions.Marker.USER_AGENT)
.contains(DirectKmsMaterialProvider.USER_AGENT_PREFIX));
return super.generateDataKey(r);
}
};
new DirectKmsMaterialProvider(kmsSpy, keyId).getEncryptionMaterials(ctx);
}

private static class ExtendedKmsMaterialProvider extends DirectKmsMaterialProvider {
private final String encryptionKeyIdAttributeName;

Expand Down

0 comments on commit bf72702

Please sign in to comment.