Skip to content

Commit

Permalink
V2 (#98)
Browse files Browse the repository at this point in the history
Closes #5 

*Description of changes:*

Provide a major version 2 version of the Java Caching Library.

Version 2 uses the Java SDK v2 under the hood. The library should be a
drop-in replacement, unless you currently provide a v1 client to the
caching library at creation. In that case, you will have to start
passing a v2 client instead.

Version 1 and version 2 will be maintained side by side in separate
branches.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.
  • Loading branch information
simonmarty authored Aug 17, 2023
1 parent f274d55 commit 2f8251e
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 286 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Codecov
uses: codecov/[email protected].0
uses: codecov/[email protected].1
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ You can get the latest release from Maven:
<dependency>
<groupId>com.amazonaws.secretsmanager</groupId>
<artifactId>aws-secretsmanager-caching-java</artifactId>
<version>1.0.2</version>
<version>2.0.0</version>
</dependency>
```

Expand Down
31 changes: 14 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>com.amazonaws.secretsmanager</groupId>
<artifactId>aws-secretsmanager-caching-java</artifactId>
<version>1.0.2</version>
<version>2.0.0</version>
<packaging>jar</packaging>


Expand Down Expand Up @@ -38,26 +38,24 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<checkstyle.plugin.version>3.1.2</checkstyle.plugin.version>
<findbugs.plugin.version>3.0.5</findbugs.plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-secretsmanager</artifactId>
<version>1.12.264</version>
<groupId>software.amazon.awssdk</groupId>
<artifactId>secretsmanager</artifactId>
<version>2.20.93</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.0</version>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<artifactId>mockito-core</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -66,7 +64,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<version>3.11.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand All @@ -78,7 +76,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<version>3.3.0</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -91,7 +89,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.0</version>
<version>3.5.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -103,10 +101,9 @@
</plugin>
<plugin>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstyle.plugin.version}</version>
<version>3.3.0</version>
<configuration>
<configLocation>${basedir}/config/checkstyle/checkstyle.xml</configLocation>
<encoding>${project.build.sourceEncoding}</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
Expand All @@ -126,7 +123,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${findbugs.plugin.version}</version>
<version>3.0.5</version>
<configuration>
<effort>Max</effort>
<threshold>Low</threshold>
Expand All @@ -146,7 +143,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<version>0.8.10</version>
<executions>
<execution>
<id>prepare-agent</id>
Expand Down Expand Up @@ -174,7 +171,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.0.1</version>
<version>3.1.0</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down
89 changes: 50 additions & 39 deletions src/main/java/com/amazonaws/secretsmanager/caching/SecretCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
*/
package com.amazonaws.secretsmanager.caching;

import java.nio.ByteBuffer;

import com.amazonaws.secretsmanager.caching.cache.LRUCache;
import com.amazonaws.secretsmanager.caching.cache.SecretCacheItem;
import com.amazonaws.services.secretsmanager.AWSSecretsManager;
import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder;
import com.amazonaws.services.secretsmanager.model.GetSecretValueResult;
import com.amazonaws.secretsmanager.caching.cache.internal.VersionInfo;

import java.nio.ByteBuffer;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse;

/**
* Provides the primary entry-point to the AWS Secrets Manager client cache SDK.
Expand Down Expand Up @@ -47,58 +51,64 @@ public class SecretCache implements AutoCloseable {
private final SecretCacheConfiguration config;

/** The AWS Secrets Manager client to use when requesting secrets. */
private final AWSSecretsManager client;
private final SecretsManagerClient client;

/**
* Constructs a new secret cache using the standard AWS Secrets Manager client with default options.
* Constructs a new secret cache using the standard AWS Secrets Manager client
* with default options.
*/
public SecretCache() {
this(AWSSecretsManagerClientBuilder.standard());
this(SecretsManagerClient.builder());
}


/**
* Constructs a new secret cache using an AWS Secrets Manager client created using the
* Constructs a new secret cache using an AWS Secrets Manager client created
* using the
* provided builder.
*
* @param builder
* The builder to use for creating the AWS Secrets Manager client.
* @param builder The builder to use for creating the AWS Secrets Manager
* client.
*/
public SecretCache(AWSSecretsManagerClientBuilder builder) {
this(null == builder ?
AWSSecretsManagerClientBuilder.standard().build() :
builder.build());
public SecretCache(SecretsManagerClientBuilder builder) {
this(new SecretCacheConfiguration().withClient(builder
.overrideConfiguration(
builder.overrideConfiguration().toBuilder()
.putAdvancedOption(SdkAdvancedClientOption.USER_AGENT_SUFFIX, VersionInfo.USER_AGENT)
.build())
.build()));
}

/**
* Constructs a new secret cache using the provided AWS Secrets Manager client.
*
* @param client
* The AWS Secrets Manager client to use for requesting secret values.
* @param client The AWS Secrets Manager client to use for requesting secret
* values.
*/
public SecretCache(AWSSecretsManager client) {
public SecretCache(SecretsManagerClient client) {
this(new SecretCacheConfiguration().withClient(client));
}

/**
* Constructs a new secret cache using the provided cache configuration.
*
* @param config
* The secret cache configuration.
* @param config The secret cache configuration.
*/
public SecretCache(SecretCacheConfiguration config) {
if (null == config) { config = new SecretCacheConfiguration(); }
if (null == config) {
config = new SecretCacheConfiguration();
}
this.cache = new LRUCache<String, SecretCacheItem>(config.getMaxCacheSize());
this.config = config;
this.client = config.getClient() != null ? config.getClient() :
AWSSecretsManagerClientBuilder.standard().build();
ClientOverrideConfiguration defaultOverride = ClientOverrideConfiguration.builder()
.putAdvancedOption(SdkAdvancedClientOption.USER_AGENT_SUFFIX, VersionInfo.USER_AGENT).build();
this.client = config.getClient() != null ? config.getClient()
: SecretsManagerClient.builder().overrideConfiguration(defaultOverride).build();
}

/**
* Method to retrieve the cached secret item.
*
* @param secretId
* The identifier for the secret being requested.
* @param secretId The identifier for the secret being requested.
* @return The cached secret item
*/
private SecretCacheItem getCachedSecret(final String secretId) {
Expand All @@ -114,39 +124,40 @@ private SecretCacheItem getCachedSecret(final String secretId) {
/**
* Method to retrieve a string secret from AWS Secrets Manager.
*
* @param secretId
* The identifier for the secret being requested.
* @param secretId The identifier for the secret being requested.
* @return The string secret
*/
public String getSecretString(final String secretId) {
SecretCacheItem secret = this.getCachedSecret(secretId);
GetSecretValueResult gsv = secret.getSecretValue();
if (null == gsv) { return null; }
return gsv.getSecretString();
GetSecretValueResponse gsv = secret.getSecretValue();
if (null == gsv) {
return null;
}
return gsv.secretString();
}

/**
* Method to retrieve a binary secret from AWS Secrets Manager.
*
* @param secretId
* The identifier for the secret being requested.
* @param secretId The identifier for the secret being requested.
* @return The binary secret
*/
public ByteBuffer getSecretBinary(final String secretId) {
SecretCacheItem secret = this.getCachedSecret(secretId);
GetSecretValueResult gsv = secret.getSecretValue();
if (null == gsv) { return null; }
return gsv.getSecretBinary();
GetSecretValueResponse gsv = secret.getSecretValue();
if (null == gsv) {
return null;
}
return gsv.secretBinary().asByteBuffer();
}

/**
* Method to force the refresh of a cached secret state.
*
* @param secretId
* The identifier for the secret being refreshed.
* @param secretId The identifier for the secret being refreshed.
* @return True if the refresh completed without error.
* @throws InterruptedException
* If the thread is interrupted while waiting for the refresh.
* @throws InterruptedException If the thread is interrupted while waiting for
* the refresh.
*/
public boolean refreshNow(final String secretId) throws InterruptedException {
SecretCacheItem secret = this.getCachedSecret(secretId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

package com.amazonaws.secretsmanager.caching;

import com.amazonaws.services.secretsmanager.AWSSecretsManager;

import java.util.concurrent.TimeUnit;

import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;


/**
* Cache configuration options such as max cache size, ttl for cached items, etc.
Expand All @@ -34,7 +34,7 @@ public class SecretCacheConfiguration {
public static final String DEFAULT_VERSION_STAGE = "AWSCURRENT";

/** The client this cache instance will use for accessing AWS Secrets Manager. */
private AWSSecretsManager client = null;
private SecretsManagerClient client = null;

/** Used to hook in-memory cache updates. */
private SecretCacheHook cacheHook = null;
Expand Down Expand Up @@ -71,7 +71,7 @@ public SecretCacheConfiguration() {
*
* @return The AWS Secrets Manager client.
*/
public AWSSecretsManager getClient() {
public SecretsManagerClient getClient() {
return client;
}

Expand All @@ -83,7 +83,7 @@ public AWSSecretsManager getClient() {
* @param client
* The AWS Secrets Manager client.
*/
public void setClient(AWSSecretsManager client) {
public void setClient(SecretsManagerClient client) {
this.client = client;
}

Expand All @@ -95,7 +95,7 @@ public void setClient(AWSSecretsManager client) {
* The AWS Secrets Manager client.
* @return The updated ClientConfiguration object with the new client setting.
*/
public SecretCacheConfiguration withClient(AWSSecretsManager client) {
public SecretCacheConfiguration withClient(SecretsManagerClient client) {
this.setClient(client);
return this;
}
Expand Down
Loading

0 comments on commit 2f8251e

Please sign in to comment.