12
12
*/
13
13
package com .amazonaws .secretsmanager .caching ;
14
14
15
+ import java .nio .ByteBuffer ;
16
+
15
17
import com .amazonaws .secretsmanager .caching .cache .LRUCache ;
16
18
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 ;
20
20
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 ;
22
26
23
27
/**
24
28
* Provides the primary entry-point to the AWS Secrets Manager client cache SDK.
@@ -47,58 +51,64 @@ public class SecretCache implements AutoCloseable {
47
51
private final SecretCacheConfiguration config ;
48
52
49
53
/** The AWS Secrets Manager client to use when requesting secrets. */
50
- private final AWSSecretsManager client ;
54
+ private final SecretsManagerClient client ;
51
55
52
56
/**
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.
54
59
*/
55
60
public SecretCache () {
56
- this (AWSSecretsManagerClientBuilder . standard ());
61
+ this (SecretsManagerClient . builder ());
57
62
}
58
63
59
-
60
64
/**
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
62
67
* provided builder.
63
68
*
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.
66
71
*/
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 ()));
71
79
}
72
80
73
81
/**
74
82
* Constructs a new secret cache using the provided AWS Secrets Manager client.
75
83
*
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.
78
86
*/
79
- public SecretCache (AWSSecretsManager client ) {
87
+ public SecretCache (SecretsManagerClient client ) {
80
88
this (new SecretCacheConfiguration ().withClient (client ));
81
89
}
82
90
83
91
/**
84
92
* Constructs a new secret cache using the provided cache configuration.
85
93
*
86
- * @param config
87
- * The secret cache configuration.
94
+ * @param config The secret cache configuration.
88
95
*/
89
96
public SecretCache (SecretCacheConfiguration config ) {
90
- if (null == config ) { config = new SecretCacheConfiguration (); }
97
+ if (null == config ) {
98
+ config = new SecretCacheConfiguration ();
99
+ }
91
100
this .cache = new LRUCache <String , SecretCacheItem >(config .getMaxCacheSize ());
92
101
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 ();
95
106
}
96
107
97
108
/**
98
109
* Method to retrieve the cached secret item.
99
110
*
100
- * @param secretId
101
- * The identifier for the secret being requested.
111
+ * @param secretId The identifier for the secret being requested.
102
112
* @return The cached secret item
103
113
*/
104
114
private SecretCacheItem getCachedSecret (final String secretId ) {
@@ -114,39 +124,40 @@ private SecretCacheItem getCachedSecret(final String secretId) {
114
124
/**
115
125
* Method to retrieve a string secret from AWS Secrets Manager.
116
126
*
117
- * @param secretId
118
- * The identifier for the secret being requested.
127
+ * @param secretId The identifier for the secret being requested.
119
128
* @return The string secret
120
129
*/
121
130
public String getSecretString (final String secretId ) {
122
131
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 ();
126
137
}
127
138
128
139
/**
129
140
* Method to retrieve a binary secret from AWS Secrets Manager.
130
141
*
131
- * @param secretId
132
- * The identifier for the secret being requested.
142
+ * @param secretId The identifier for the secret being requested.
133
143
* @return The binary secret
134
144
*/
135
145
public ByteBuffer getSecretBinary (final String secretId ) {
136
146
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 ();
140
152
}
141
153
142
154
/**
143
155
* Method to force the refresh of a cached secret state.
144
156
*
145
- * @param secretId
146
- * The identifier for the secret being refreshed.
157
+ * @param secretId The identifier for the secret being refreshed.
147
158
* @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.
150
161
*/
151
162
public boolean refreshNow (final String secretId ) throws InterruptedException {
152
163
SecretCacheItem secret = this .getCachedSecret (secretId );
0 commit comments