Skip to content

Commit 2f8251e

Browse files
authored
V2 (#98)
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.
1 parent f274d55 commit 2f8251e

File tree

12 files changed

+312
-286
lines changed

12 files changed

+312
-286
lines changed

.github/workflows/maven.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ jobs:
2525
- name: Build with Maven
2626
run: mvn -B package --file pom.xml
2727
- name: Codecov
28-
uses: codecov/[email protected].0
28+
uses: codecov/[email protected].1

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ You can get the latest release from Maven:
3030
<dependency>
3131
<groupId>com.amazonaws.secretsmanager</groupId>
3232
<artifactId>aws-secretsmanager-caching-java</artifactId>
33-
<version>1.0.2</version>
33+
<version>2.0.0</version>
3434
</dependency>
3535
```
3636

pom.xml

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>com.amazonaws.secretsmanager</groupId>
55
<artifactId>aws-secretsmanager-caching-java</artifactId>
6-
<version>1.0.2</version>
6+
<version>2.0.0</version>
77
<packaging>jar</packaging>
88

99

@@ -38,26 +38,24 @@
3838
<maven.compiler.source>1.8</maven.compiler.source>
3939
<maven.compiler.target>1.8</maven.compiler.target>
4040
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
41-
<checkstyle.plugin.version>3.1.2</checkstyle.plugin.version>
42-
<findbugs.plugin.version>3.0.5</findbugs.plugin.version>
4341
</properties>
4442

4543
<dependencies>
4644
<dependency>
47-
<groupId>com.amazonaws</groupId>
48-
<artifactId>aws-java-sdk-secretsmanager</artifactId>
49-
<version>1.12.264</version>
45+
<groupId>software.amazon.awssdk</groupId>
46+
<artifactId>secretsmanager</artifactId>
47+
<version>2.20.93</version>
5048
</dependency>
5149
<dependency>
5250
<groupId>org.testng</groupId>
5351
<artifactId>testng</artifactId>
54-
<version>7.7.0</version>
52+
<version>7.8.0</version>
5553
<scope>test</scope>
5654
</dependency>
5755
<dependency>
5856
<groupId>org.mockito</groupId>
59-
<artifactId>mockito-all</artifactId>
60-
<version>1.10.19</version>
57+
<artifactId>mockito-core</artifactId>
58+
<version>5.4.0</version>
6159
<scope>test</scope>
6260
</dependency>
6361
</dependencies>
@@ -66,7 +64,7 @@
6664
<plugin>
6765
<groupId>org.apache.maven.plugins</groupId>
6866
<artifactId>maven-compiler-plugin</artifactId>
69-
<version>3.10.1</version>
67+
<version>3.11.0</version>
7068
<configuration>
7169
<source>1.8</source>
7270
<target>1.8</target>
@@ -78,7 +76,7 @@
7876
<plugin>
7977
<groupId>org.apache.maven.plugins</groupId>
8078
<artifactId>maven-source-plugin</artifactId>
81-
<version>3.2.1</version>
79+
<version>3.3.0</version>
8280
<executions>
8381
<execution>
8482
<id>attach-sources</id>
@@ -91,7 +89,7 @@
9189
<plugin>
9290
<groupId>org.apache.maven.plugins</groupId>
9391
<artifactId>maven-javadoc-plugin</artifactId>
94-
<version>3.4.0</version>
92+
<version>3.5.0</version>
9593
<executions>
9694
<execution>
9795
<id>attach-javadocs</id>
@@ -103,10 +101,9 @@
103101
</plugin>
104102
<plugin>
105103
<artifactId>maven-checkstyle-plugin</artifactId>
106-
<version>${checkstyle.plugin.version}</version>
104+
<version>3.3.0</version>
107105
<configuration>
108106
<configLocation>${basedir}/config/checkstyle/checkstyle.xml</configLocation>
109-
<encoding>${project.build.sourceEncoding}</encoding>
110107
<consoleOutput>true</consoleOutput>
111108
<failsOnError>true</failsOnError>
112109
<linkXRef>false</linkXRef>
@@ -126,7 +123,7 @@
126123
<plugin>
127124
<groupId>org.codehaus.mojo</groupId>
128125
<artifactId>findbugs-maven-plugin</artifactId>
129-
<version>${findbugs.plugin.version}</version>
126+
<version>3.0.5</version>
130127
<configuration>
131128
<effort>Max</effort>
132129
<threshold>Low</threshold>
@@ -146,7 +143,7 @@
146143
<plugin>
147144
<groupId>org.jacoco</groupId>
148145
<artifactId>jacoco-maven-plugin</artifactId>
149-
<version>0.8.8</version>
146+
<version>0.8.10</version>
150147
<executions>
151148
<execution>
152149
<id>prepare-agent</id>
@@ -174,7 +171,7 @@
174171
<plugin>
175172
<groupId>org.apache.maven.plugins</groupId>
176173
<artifactId>maven-gpg-plugin</artifactId>
177-
<version>3.0.1</version>
174+
<version>3.1.0</version>
178175
<executions>
179176
<execution>
180177
<id>sign-artifacts</id>

src/main/java/com/amazonaws/secretsmanager/caching/SecretCache.java

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
*/
1313
package com.amazonaws.secretsmanager.caching;
1414

15+
import java.nio.ByteBuffer;
16+
1517
import com.amazonaws.secretsmanager.caching.cache.LRUCache;
1618
import com.amazonaws.secretsmanager.caching.cache.SecretCacheItem;
17-
import com.amazonaws.services.secretsmanager.AWSSecretsManager;
18-
import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder;
19-
import com.amazonaws.services.secretsmanager.model.GetSecretValueResult;
19+
import com.amazonaws.secretsmanager.caching.cache.internal.VersionInfo;
2020

21-
import java.nio.ByteBuffer;
21+
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
22+
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
23+
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
24+
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder;
25+
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse;
2226

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

4953
/** The AWS Secrets Manager client to use when requesting secrets. */
50-
private final AWSSecretsManager client;
54+
private final SecretsManagerClient client;
5155

5256
/**
53-
* Constructs a new secret cache using the standard AWS Secrets Manager client with default options.
57+
* Constructs a new secret cache using the standard AWS Secrets Manager client
58+
* with default options.
5459
*/
5560
public SecretCache() {
56-
this(AWSSecretsManagerClientBuilder.standard());
61+
this(SecretsManagerClient.builder());
5762
}
5863

59-
6064
/**
61-
* Constructs a new secret cache using an AWS Secrets Manager client created using the
65+
* Constructs a new secret cache using an AWS Secrets Manager client created
66+
* using the
6267
* provided builder.
6368
*
64-
* @param builder
65-
* The builder to use for creating the AWS Secrets Manager client.
69+
* @param builder The builder to use for creating the AWS Secrets Manager
70+
* client.
6671
*/
67-
public SecretCache(AWSSecretsManagerClientBuilder builder) {
68-
this(null == builder ?
69-
AWSSecretsManagerClientBuilder.standard().build() :
70-
builder.build());
72+
public SecretCache(SecretsManagerClientBuilder builder) {
73+
this(new SecretCacheConfiguration().withClient(builder
74+
.overrideConfiguration(
75+
builder.overrideConfiguration().toBuilder()
76+
.putAdvancedOption(SdkAdvancedClientOption.USER_AGENT_SUFFIX, VersionInfo.USER_AGENT)
77+
.build())
78+
.build()));
7179
}
7280

7381
/**
7482
* Constructs a new secret cache using the provided AWS Secrets Manager client.
7583
*
76-
* @param client
77-
* The AWS Secrets Manager client to use for requesting secret values.
84+
* @param client The AWS Secrets Manager client to use for requesting secret
85+
* values.
7886
*/
79-
public SecretCache(AWSSecretsManager client) {
87+
public SecretCache(SecretsManagerClient client) {
8088
this(new SecretCacheConfiguration().withClient(client));
8189
}
8290

8391
/**
8492
* Constructs a new secret cache using the provided cache configuration.
8593
*
86-
* @param config
87-
* The secret cache configuration.
94+
* @param config The secret cache configuration.
8895
*/
8996
public SecretCache(SecretCacheConfiguration config) {
90-
if (null == config) { config = new SecretCacheConfiguration(); }
97+
if (null == config) {
98+
config = new SecretCacheConfiguration();
99+
}
91100
this.cache = new LRUCache<String, SecretCacheItem>(config.getMaxCacheSize());
92101
this.config = config;
93-
this.client = config.getClient() != null ? config.getClient() :
94-
AWSSecretsManagerClientBuilder.standard().build();
102+
ClientOverrideConfiguration defaultOverride = ClientOverrideConfiguration.builder()
103+
.putAdvancedOption(SdkAdvancedClientOption.USER_AGENT_SUFFIX, VersionInfo.USER_AGENT).build();
104+
this.client = config.getClient() != null ? config.getClient()
105+
: SecretsManagerClient.builder().overrideConfiguration(defaultOverride).build();
95106
}
96107

97108
/**
98109
* Method to retrieve the cached secret item.
99110
*
100-
* @param secretId
101-
* The identifier for the secret being requested.
111+
* @param secretId The identifier for the secret being requested.
102112
* @return The cached secret item
103113
*/
104114
private SecretCacheItem getCachedSecret(final String secretId) {
@@ -114,39 +124,40 @@ private SecretCacheItem getCachedSecret(final String secretId) {
114124
/**
115125
* Method to retrieve a string secret from AWS Secrets Manager.
116126
*
117-
* @param secretId
118-
* The identifier for the secret being requested.
127+
* @param secretId The identifier for the secret being requested.
119128
* @return The string secret
120129
*/
121130
public String getSecretString(final String secretId) {
122131
SecretCacheItem secret = this.getCachedSecret(secretId);
123-
GetSecretValueResult gsv = secret.getSecretValue();
124-
if (null == gsv) { return null; }
125-
return gsv.getSecretString();
132+
GetSecretValueResponse gsv = secret.getSecretValue();
133+
if (null == gsv) {
134+
return null;
135+
}
136+
return gsv.secretString();
126137
}
127138

128139
/**
129140
* Method to retrieve a binary secret from AWS Secrets Manager.
130141
*
131-
* @param secretId
132-
* The identifier for the secret being requested.
142+
* @param secretId The identifier for the secret being requested.
133143
* @return The binary secret
134144
*/
135145
public ByteBuffer getSecretBinary(final String secretId) {
136146
SecretCacheItem secret = this.getCachedSecret(secretId);
137-
GetSecretValueResult gsv = secret.getSecretValue();
138-
if (null == gsv) { return null; }
139-
return gsv.getSecretBinary();
147+
GetSecretValueResponse gsv = secret.getSecretValue();
148+
if (null == gsv) {
149+
return null;
150+
}
151+
return gsv.secretBinary().asByteBuffer();
140152
}
141153

142154
/**
143155
* Method to force the refresh of a cached secret state.
144156
*
145-
* @param secretId
146-
* The identifier for the secret being refreshed.
157+
* @param secretId The identifier for the secret being refreshed.
147158
* @return True if the refresh completed without error.
148-
* @throws InterruptedException
149-
* If the thread is interrupted while waiting for the refresh.
159+
* @throws InterruptedException If the thread is interrupted while waiting for
160+
* the refresh.
150161
*/
151162
public boolean refreshNow(final String secretId) throws InterruptedException {
152163
SecretCacheItem secret = this.getCachedSecret(secretId);

src/main/java/com/amazonaws/secretsmanager/caching/SecretCacheConfiguration.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
package com.amazonaws.secretsmanager.caching;
1515

16-
import com.amazonaws.services.secretsmanager.AWSSecretsManager;
17-
1816
import java.util.concurrent.TimeUnit;
1917

18+
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
19+
2020

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

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

3939
/** Used to hook in-memory cache updates. */
4040
private SecretCacheHook cacheHook = null;
@@ -71,7 +71,7 @@ public SecretCacheConfiguration() {
7171
*
7272
* @return The AWS Secrets Manager client.
7373
*/
74-
public AWSSecretsManager getClient() {
74+
public SecretsManagerClient getClient() {
7575
return client;
7676
}
7777

@@ -83,7 +83,7 @@ public AWSSecretsManager getClient() {
8383
* @param client
8484
* The AWS Secrets Manager client.
8585
*/
86-
public void setClient(AWSSecretsManager client) {
86+
public void setClient(SecretsManagerClient client) {
8787
this.client = client;
8888
}
8989

@@ -95,7 +95,7 @@ public void setClient(AWSSecretsManager client) {
9595
* The AWS Secrets Manager client.
9696
* @return The updated ClientConfiguration object with the new client setting.
9797
*/
98-
public SecretCacheConfiguration withClient(AWSSecretsManager client) {
98+
public SecretCacheConfiguration withClient(SecretsManagerClient client) {
9999
this.setClient(client);
100100
return this;
101101
}

0 commit comments

Comments
 (0)