99
1010import java .nio .charset .StandardCharsets ;
1111
12- import static com .bettercloud .vault .api .LogicalUtilities .retry ;
1312
1413/**
1514 * <p>The implementing class for operations on REST endpoints, under the "Leases" section of the Vault HTTP API
@@ -23,10 +22,17 @@ public class Leases {
2322
2423 private final VaultConfig config ;
2524
25+ private String nameSpace ;
26+
2627 public Leases (final VaultConfig config ) {
2728 this .config = config ;
2829 }
2930
31+ public Leases withNameSpace (final String nameSpace ) {
32+ this .nameSpace = nameSpace ;
33+ return this ;
34+ }
35+
3036 /**
3137 * <p>Immediately revokes a secret associated with a given lease. E.g.:</p>
3238 *
@@ -45,14 +51,22 @@ public VaultResponse revoke(final String leaseId) throws VaultException {
4551 int retryCount = 0 ;
4652 while (true ) {
4753 try {
48- final RestResponse restResponse = new Rest ()//NOPMD
54+ final RestResponse restResponse ;
55+ final Rest rest = new Rest ()//NOPMD
4956 .url (config .getAddress () + "/v1/sys/revoke/" + leaseId )
5057 .header ("X-Vault-Token" , config .getToken ())
5158 .connectTimeoutSeconds (config .getOpenTimeout ())
5259 .readTimeoutSeconds (config .getReadTimeout ())
5360 .sslVerification (config .getSslConfig ().isVerify ())
54- .sslContext (config .getSslConfig ().getSslContext ())
55- .put ();
61+ .sslContext (config .getSslConfig ().getSslContext ());
62+
63+ if (this .nameSpace != null && !this .nameSpace .isEmpty ()) {
64+ restResponse = rest
65+ .header ("X-Vault-Namespace" , this .nameSpace )
66+ .put ();
67+ } else {
68+ restResponse = rest .put ();
69+ }
5670
5771 // Validate response
5872 if (restResponse .getStatus () != 204 ) {
@@ -61,7 +75,20 @@ public VaultResponse revoke(final String leaseId) throws VaultException {
6175 return new VaultResponse (restResponse , retryCount );
6276 } catch (Exception e ) {
6377 // If there are retries to perform, then pause for the configured interval and then execute the loop again...
64- retry (retryCount , e , this .config );
78+ if (retryCount < config .getMaxRetries ()) {
79+ retryCount ++;
80+ try {
81+ final int retryIntervalMilliseconds = config .getRetryIntervalMilliseconds ();
82+ Thread .sleep (retryIntervalMilliseconds );
83+ } catch (InterruptedException e1 ) {
84+ e1 .printStackTrace ();
85+ }
86+ } else if (e instanceof VaultException ) {
87+ // ... otherwise, give up.
88+ throw (VaultException ) e ;
89+ } else {
90+ throw new VaultException (e );
91+ }
6592 }
6693 }
6794 }
@@ -86,14 +113,22 @@ public VaultResponse revokePrefix(final String prefix) throws VaultException {
86113 int retryCount = 0 ;
87114 while (true ) {
88115 try {
89- final RestResponse restResponse = new Rest ()//NOPMD
116+ final RestResponse restResponse ;
117+ final Rest rest = new Rest ()//NOPMD
90118 .url (config .getAddress () + "/v1/sys/revoke-prefix/" + prefix )
91119 .header ("X-Vault-Token" , config .getToken ())
92120 .connectTimeoutSeconds (config .getOpenTimeout ())
93121 .readTimeoutSeconds (config .getReadTimeout ())
94122 .sslVerification (config .getSslConfig ().isVerify ())
95- .sslContext (config .getSslConfig ().getSslContext ())
96- .put ();
123+ .sslContext (config .getSslConfig ().getSslContext ());
124+
125+ if (this .nameSpace != null && !this .nameSpace .isEmpty ()) {
126+ restResponse = rest
127+ .header ("X-Vault-Namespace" , this .nameSpace )
128+ .put ();
129+ } else {
130+ restResponse = rest .put ();
131+ }
97132
98133 // Validate response
99134 if (restResponse .getStatus () != 204 ) {
@@ -102,7 +137,20 @@ public VaultResponse revokePrefix(final String prefix) throws VaultException {
102137 return new VaultResponse (restResponse , retryCount );
103138 } catch (Exception e ) {
104139 // If there are retries to perform, then pause for the configured interval and then execute the loop again...
105- retry (retryCount , e , this .config );
140+ if (retryCount < config .getMaxRetries ()) {
141+ retryCount ++;
142+ try {
143+ final int retryIntervalMilliseconds = config .getRetryIntervalMilliseconds ();
144+ Thread .sleep (retryIntervalMilliseconds );
145+ } catch (InterruptedException e1 ) {
146+ e1 .printStackTrace ();
147+ }
148+ } else if (e instanceof VaultException ) {
149+ // ... otherwise, give up.
150+ throw (VaultException ) e ;
151+ } else {
152+ throw new VaultException (e );
153+ }
106154 }
107155 }
108156 }
@@ -130,14 +178,22 @@ public VaultResponse revokeForce(final String prefix) throws VaultException {
130178 int retryCount = 0 ;
131179 while (true ) {
132180 try {
133- final RestResponse restResponse = new Rest ()//NOPMD
181+ final RestResponse restResponse ;
182+ final Rest rest = new Rest ()//NOPMD
134183 .url (config .getAddress () + "/v1/sys/revoke-force/" + prefix )
135184 .header ("X-Vault-Token" , config .getToken ())
136185 .connectTimeoutSeconds (config .getOpenTimeout ())
137186 .readTimeoutSeconds (config .getReadTimeout ())
138187 .sslVerification (config .getSslConfig ().isVerify ())
139- .sslContext (config .getSslConfig ().getSslContext ())
140- .put ();
188+ .sslContext (config .getSslConfig ().getSslContext ());
189+
190+ if (this .nameSpace != null && !this .nameSpace .isEmpty ()) {
191+ restResponse = rest
192+ .header ("X-Vault-Namespace" , this .nameSpace )
193+ .put ();
194+ } else {
195+ restResponse = rest .put ();
196+ }
141197
142198 // Validate response
143199 if (restResponse .getStatus () != 204 ) {
@@ -146,7 +202,20 @@ public VaultResponse revokeForce(final String prefix) throws VaultException {
146202 return new VaultResponse (restResponse , retryCount );
147203 } catch (Exception e ) {
148204 // If there are retries to perform, then pause for the configured interval and then execute the loop again...
149- retry (retryCount , e , this .config );
205+ if (retryCount < config .getMaxRetries ()) {
206+ retryCount ++;
207+ try {
208+ final int retryIntervalMilliseconds = config .getRetryIntervalMilliseconds ();
209+ Thread .sleep (retryIntervalMilliseconds );
210+ } catch (InterruptedException e1 ) {
211+ e1 .printStackTrace ();
212+ }
213+ } else if (e instanceof VaultException ) {
214+ // ... otherwise, give up.
215+ throw (VaultException ) e ;
216+ } else {
217+ throw new VaultException (e );
218+ }
150219 }
151220 }
152221 }
@@ -179,22 +248,44 @@ public VaultResponse renew(final String leaseId, final long increment) throws Va
179248 while (true ) {
180249 try {
181250 final String requestJson = Json .object ().add ("increment" , increment ).toString ();
182- final RestResponse restResponse = new Rest ()//NOPMD
251+ final RestResponse restResponse ;
252+ final Rest rest = new Rest ()//NOPMD
183253 .url (config .getAddress () + "/v1/sys/renew/" + leaseId )
184254 .header ("X-Vault-Token" , config .getToken ())
185255 .body (increment < 0 ? null : requestJson .getBytes (StandardCharsets .UTF_8 ))
186256 .connectTimeoutSeconds (config .getOpenTimeout ())
187257 .readTimeoutSeconds (config .getReadTimeout ())
188258 .sslVerification (config .getSslConfig ().isVerify ())
189- .sslContext (config .getSslConfig ().getSslContext ())
190- .put ();
259+ .sslContext (config .getSslConfig ().getSslContext ());
260+
261+ if (this .nameSpace != null && !this .nameSpace .isEmpty ()) {
262+ restResponse = rest
263+ .header ("X-Vault-Namespace" , this .nameSpace )
264+ .post ();
265+ } else {
266+ restResponse = rest .post ();
267+ }
268+
191269 // Validate response
192270 if (restResponse .getStatus () != 200 ) {
193271 throw new VaultException ("Expecting HTTP status 200, but instead receiving " + restResponse .getStatus (), restResponse .getStatus ());
194272 }
195273 return new VaultResponse (restResponse , retryCount );
196274 } catch (Exception e ) {
197- retry (retryCount , e , this .config );
275+ if (retryCount < config .getMaxRetries ()) {
276+ retryCount ++;
277+ try {
278+ final int retryIntervalMilliseconds = config .getRetryIntervalMilliseconds ();
279+ Thread .sleep (retryIntervalMilliseconds );
280+ } catch (InterruptedException e1 ) {
281+ e1 .printStackTrace ();
282+ }
283+ } else if (e instanceof VaultException ) {
284+ // ... otherwise, give up.
285+ throw (VaultException ) e ;
286+ } else {
287+ throw new VaultException (e );
288+ }
198289 }
199290 }
200291 }
0 commit comments