Skip to content

Commit 1cdaa49

Browse files
committed
Update code to use recent language features.
Closes #881
1 parent a140b5c commit 1cdaa49

File tree

114 files changed

+533
-614
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+533
-614
lines changed

spring-vault-core/src/main/java/org/springframework/vault/authentication/AppRoleAuthentication.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.vault.authentication;
1717

18+
import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.*;
19+
import static org.springframework.vault.authentication.AuthenticationUtil.*;
20+
1821
import java.util.HashMap;
1922
import java.util.Map;
2023

@@ -43,9 +46,6 @@
4346
import org.springframework.web.client.RestClientException;
4447
import org.springframework.web.client.RestOperations;
4548

46-
import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.*;
47-
import static org.springframework.vault.authentication.AuthenticationUtil.*;
48-
4949
/**
5050
* AppRole implementation of {@link ClientAuthentication}. RoleId and SecretId (optional)
5151
* are sent in the login request to Vault to obtain a {@link VaultToken}.
@@ -217,8 +217,8 @@ private String getRoleId(RoleId roleId) throws VaultLoginException {
217217
return (String) entity.getBody().getRequiredData().get("role_id");
218218
}
219219
catch (HttpStatusCodeException e) {
220-
throw new VaultLoginException(String.format("Cannot get Role id using AppRole: %s",
221-
VaultResponses.getError(e.getResponseBodyAsString())), e);
220+
throw new VaultLoginException("Cannot get Role id using AppRole: %s"
221+
.formatted(VaultResponses.getError(e.getResponseBodyAsString())), e);
222222
}
223223
}
224224

@@ -236,8 +236,8 @@ private String getRoleId(RoleId roleId) throws VaultLoginException {
236236
return (String) response.getRequiredData().get("role_id");
237237
}
238238
catch (HttpStatusCodeException e) {
239-
throw new VaultLoginException(String.format("Cannot unwrap Role id using AppRole: %s",
240-
VaultResponses.getError(e.getResponseBodyAsString())), e);
239+
throw new VaultLoginException("Cannot unwrap Role id using AppRole: %s"
240+
.formatted(VaultResponses.getError(e.getResponseBodyAsString())), e);
241241
}
242242
}
243243

@@ -260,8 +260,8 @@ private String getSecretId(SecretId secretId) throws VaultLoginException {
260260
return (String) response.getRequiredData().get("secret_id");
261261
}
262262
catch (HttpStatusCodeException e) {
263-
throw new VaultLoginException(String.format("Cannot get Secret id using AppRole: %s",
264-
VaultResponses.getError(e.getResponseBodyAsString())), e);
263+
throw new VaultLoginException("Cannot get Secret id using AppRole: %s"
264+
.formatted(VaultResponses.getError(e.getResponseBodyAsString())), e);
265265
}
266266
}
267267

@@ -280,8 +280,8 @@ private String getSecretId(SecretId secretId) throws VaultLoginException {
280280
return (String) response.getRequiredData().get("secret_id");
281281
}
282282
catch (HttpStatusCodeException e) {
283-
throw new VaultLoginException(String.format("Cannot unwrap Role id using AppRole: %s",
284-
VaultResponses.getError(e.getResponseBodyAsString())), e);
283+
throw new VaultLoginException("Cannot unwrap Role id using AppRole: %s"
284+
.formatted(VaultResponses.getError(e.getResponseBodyAsString())), e);
285285
}
286286
}
287287

@@ -331,11 +331,11 @@ private static Map<String, String> getAppRoleLoginBody(String roleId, @Nullable
331331
}
332332

333333
private static String getSecretIdPath(AppRoleAuthenticationOptions options) {
334-
return String.format("auth/%s/role/%s/secret-id", options.getPath(), options.getAppRole());
334+
return "auth/%s/role/%s/secret-id".formatted(options.getPath(), options.getAppRole());
335335
}
336336

337337
private static String getRoleIdIdPath(AppRoleAuthenticationOptions options) {
338-
return String.format("auth/%s/role/%s/role-id", options.getPath(), options.getAppRole());
338+
return "auth/%s/role/%s/role-id".formatted(options.getPath(), options.getAppRole());
339339
}
340340

341341
}

spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationSteps.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ public static class HttpRequest<T> {
467467

468468
@Override
469469
public String toString() {
470-
return String.format("%s %s AS %s", getMethod(), getUri() != null ? getUri() : getUriTemplate(),
470+
return "%s %s AS %s".formatted(getMethod(), getUri() != null ? getUri() : getUriTemplate(),
471471
getResponseType());
472472
}
473473

@@ -529,9 +529,8 @@ public Node<?> getPrevious() {
529529
public boolean equals(Object o) {
530530
if (this == o)
531531
return true;
532-
if (!(o instanceof HttpRequestNode))
532+
if (!(o instanceof HttpRequestNode<?> that))
533533
return false;
534-
HttpRequestNode<?> that = (HttpRequestNode<?>) o;
535534
return this.definition.equals(that.definition) && this.previous.equals(that.previous);
536535
}
537536

@@ -574,9 +573,8 @@ public Node<?> getPrevious() {
574573
public boolean equals(Object o) {
575574
if (this == o)
576575
return true;
577-
if (!(o instanceof MapStep))
576+
if (!(o instanceof MapStep<?, ?> mapStep))
578577
return false;
579-
MapStep<?, ?> mapStep = (MapStep<?, ?>) o;
580578
return this.mapper.equals(mapStep.mapper) && this.previous.equals(mapStep.previous);
581579
}
582580

@@ -620,9 +618,8 @@ public List<Node<?>> getRight() {
620618
public boolean equals(Object o) {
621619
if (this == o)
622620
return true;
623-
if (!(o instanceof ZipStep))
621+
if (!(o instanceof ZipStep<?, ?> zipStep))
624622
return false;
625-
ZipStep<?, ?> zipStep = (ZipStep<?, ?>) o;
626623
return this.left.equals(zipStep.left) && this.right.equals(zipStep.right);
627624
}
628625

@@ -666,9 +663,8 @@ public AuthenticationSteps.Node<?> getPrevious() {
666663
public boolean equals(Object o) {
667664
if (this == o)
668665
return true;
669-
if (!(o instanceof OnNextStep))
666+
if (!(o instanceof OnNextStep<?> that))
670667
return false;
671-
OnNextStep<?> that = (OnNextStep<?>) o;
672668
return this.consumer.equals(that.consumer) && this.previous.equals(that.previous);
673669
}
674670

@@ -707,9 +703,8 @@ public Node<?> getPrevious() {
707703
public boolean equals(Object o) {
708704
if (this == o)
709705
return true;
710-
if (!(o instanceof ScalarValueStep))
706+
if (!(o instanceof ScalarValueStep<?> that))
711707
return false;
712-
ScalarValueStep<?> that = (ScalarValueStep<?>) o;
713708
return this.value.equals(that.value) && this.previous.equals(that.previous);
714709
}
715710

@@ -752,9 +747,8 @@ public Node<?> getPrevious() {
752747
public boolean equals(Object o) {
753748
if (this == o)
754749
return true;
755-
if (!(o instanceof SupplierStep))
750+
if (!(o instanceof SupplierStep<?> that))
756751
return false;
757-
SupplierStep<?> that = (SupplierStep<?>) o;
758752
return this.supplier.equals(that.supplier) && this.previous.equals(that.previous);
759753
}
760754

@@ -819,9 +813,8 @@ public R getRight() {
819813
public boolean equals(Object o) {
820814
if (this == o)
821815
return true;
822-
if (!(o instanceof Pair))
816+
if (!(o instanceof Pair<?, ?> pair))
823817
return false;
824-
Pair<?, ?> pair = (Pair<?, ?>) o;
825818
return this.left.equals(pair.left) && this.right.equals(pair.right);
826819
}
827820

spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@ public VaultToken login() throws VaultException {
8282
return (VaultToken) state;
8383
}
8484

85-
if (state instanceof VaultResponse) {
85+
if (state instanceof VaultResponse response) {
8686

87-
VaultResponse response = (VaultResponse) state;
8887
Assert.state(response.getAuth() != null, "Auth field must not be null");
8988
return LoginTokenUtil.from(response.getAuth());
9089
}
9190

9291
throw new IllegalStateException(
93-
String.format("Cannot retrieve VaultToken from authentication chain. Got instead %s", state));
92+
"Cannot retrieve VaultToken from authentication chain. Got instead %s".formatted(state));
9493
}
9594

9695
@SuppressWarnings({ "unchecked", "ConstantConditions" })
@@ -101,7 +100,7 @@ private Object evaluate(Iterable<Node<?>> steps) {
101100
for (Node<?> o : steps) {
102101

103102
if (logger.isDebugEnabled()) {
104-
logger.debug(String.format("Executing %s with current state %s", o, state));
103+
logger.debug("Executing %s with current state %s".formatted(o, state));
105104
}
106105

107106
try {
@@ -130,17 +129,15 @@ private Object evaluate(Iterable<Node<?>> steps) {
130129
}
131130

132131
if (logger.isDebugEnabled()) {
133-
logger.debug(String.format("Executed %s with current state %s", o, state));
132+
logger.debug("Executed %s with current state %s".formatted(o, state));
134133
}
135134
}
136135
catch (HttpStatusCodeException e) {
137-
throw new VaultLoginException(
138-
String.format("HTTP request %s in state %s failed with Status %s and body %s", o, state,
139-
e.getStatusCode().value(), VaultResponses.getError(e.getResponseBodyAsString())),
140-
e);
136+
throw new VaultLoginException("HTTP request %s in state %s failed with Status %s and body %s".formatted(
137+
o, state, e.getStatusCode().value(), VaultResponses.getError(e.getResponseBodyAsString())), e);
141138
}
142139
catch (RuntimeException e) {
143-
throw new VaultLoginException(String.format("Authentication execution failed in %s", o), e);
140+
throw new VaultLoginException("Authentication execution failed in %s".formatted(o), e);
144141
}
145142
}
146143
return state;

spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsOperator.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import org.springframework.core.io.buffer.DataBufferUtils;
3030
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
3131
import org.springframework.http.HttpEntity;
32-
import org.springframework.http.HttpHeaders;
33-
import org.springframework.lang.Nullable;
3432
import org.springframework.util.Assert;
3533
import org.springframework.vault.VaultException;
3634
import org.springframework.vault.authentication.AuthenticationSteps.HttpRequest;
@@ -102,17 +100,15 @@ public Mono<VaultToken> getVaultToken() throws VaultException {
102100
return (VaultToken) stateObject;
103101
}
104102

105-
if (stateObject instanceof VaultResponse) {
106-
107-
VaultResponse response = (VaultResponse) stateObject;
103+
if (stateObject instanceof VaultResponse response) {
108104

109105
Assert.state(response.getAuth() != null, "Auth field must not be null");
110106

111107
return LoginTokenUtil.from(response.getAuth());
112108
}
113109

114110
throw new IllegalStateException(
115-
String.format("Cannot retrieve VaultToken from authentication chain. Got instead %s", stateObject));
111+
"Cannot retrieve VaultToken from authentication chain. Got instead %s".formatted(stateObject));
116112
}).onErrorMap(t -> new VaultLoginException("Cannot retrieve VaultToken from authentication chain", t));
117113
}
118114

@@ -124,7 +120,7 @@ private Mono<Object> createMono(Iterable<Node<?>> steps) {
124120
for (Node<?> o : steps) {
125121

126122
if (logger.isDebugEnabled()) {
127-
logger.debug(String.format("Executing %s with current state %s", o, state));
123+
logger.debug("Executing %s with current state %s".formatted(o, state));
128124
}
129125

130126
if (o instanceof HttpRequestNode) {
@@ -153,7 +149,7 @@ private Mono<Object> createMono(Iterable<Node<?>> steps) {
153149
}
154150

155151
if (logger.isDebugEnabled()) {
156-
logger.debug(String.format("Executed %s with current state %s", o, state));
152+
logger.debug("Executed %s with current state %s".formatted(o, state));
157153
}
158154
}
159155
return state;
@@ -205,20 +201,18 @@ private Mono<Object> doSupplierStepLater(SupplierStep<Object> supplierStep) {
205201

206202
Supplier<?> supplier = supplierStep.getSupplier();
207203

208-
if (!(supplier instanceof ResourceCredentialSupplier)) {
204+
if (!(supplier instanceof ResourceCredentialSupplier resourceSupplier)) {
209205
return Mono.fromSupplier(supplierStep.getSupplier()).subscribeOn(Schedulers.boundedElastic());
210206
}
211207

212-
ResourceCredentialSupplier resourceSupplier = (ResourceCredentialSupplier) supplier;
213-
214208
return DataBufferUtils.join(DataBufferUtils.read(resourceSupplier.getResource(), this.factory, 4096))
215209
.map(dataBuffer -> {
216210
String result = dataBuffer.toString(ResourceCredentialSupplier.CHARSET);
217211
DataBufferUtils.release(dataBuffer);
218212
return (Object) result;
219213
})
220214
.onErrorMap(IOException.class, e -> new VaultException(
221-
String.format("Credential retrieval from %s failed", resourceSupplier.getResource()), e));
215+
"Credential retrieval from %s failed".formatted(resourceSupplier.getResource()), e));
222216
}
223217

224218
enum Undefinded {

spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ abstract class AuthenticationUtil {
1313
* @return
1414
*/
1515
static String getLoginPath(String authMount) {
16-
return String.format("auth/%s/login", authMount);
16+
return "auth/%s/login".formatted(authMount);
1717
}
1818

1919
private AuthenticationUtil() {

spring-vault-core/src/main/java/org/springframework/vault/authentication/AwsEc2Authentication.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ private VaultToken createTokenUsingAwsEc2() {
188188

189189
if (response.getAuth().get("metadata") instanceof Map) {
190190
Map<Object, Object> metadata = (Map<Object, Object>) response.getAuth().get("metadata");
191-
logger.debug(String.format("Login successful using AWS-EC2 authentication for instance %s, AMI %s",
192-
metadata.get("instance_id"), metadata.get("instance_id")));
191+
logger.debug("Login successful using AWS-EC2 authentication for instance %s, AMI %s"
192+
.formatted(metadata.get("instance_id"), metadata.get("instance_id")));
193193
}
194194
else {
195195
logger.debug("Login successful using AWS-EC2 authentication");
@@ -240,7 +240,7 @@ protected Map<String, String> getEc2Login() {
240240
}
241241
catch (RestClientException e) {
242242
throw new VaultLoginException(
243-
String.format("Cannot obtain Identity Document from %s", this.options.getIdentityDocumentUri()), e);
243+
"Cannot obtain Identity Document from %s".formatted(this.options.getIdentityDocumentUri()), e);
244244
}
245245
}
246246

@@ -260,7 +260,7 @@ private String createIMDSv2Token() {
260260
}
261261
catch (RestClientException e) {
262262
throw new VaultLoginException(
263-
String.format("Cannot obtain IMDSv2 Token from %s", this.options.getMetadataTokenRequestUri()), e);
263+
"Cannot obtain IMDSv2 Token from %s".formatted(this.options.getMetadataTokenRequestUri()), e);
264264
}
265265
}
266266

spring-vault-core/src/main/java/org/springframework/vault/authentication/AwsIamAuthentication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ private VaultToken createTokenUsingAwsIam() {
156156

157157
if (response.getAuth().get("metadata") instanceof Map) {
158158
Map<Object, Object> metadata = (Map<Object, Object>) response.getAuth().get("metadata");
159-
logger.debug(String.format("Login successful using AWS-IAM authentication for user id %s, ARN %s",
160-
metadata.get("client_user_id"), metadata.get("canonical_arn")));
159+
logger.debug("Login successful using AWS-IAM authentication for user id %s, ARN %s"
160+
.formatted(metadata.get("client_user_id"), metadata.get("canonical_arn")));
161161
}
162162
else {
163163
logger.debug("Login successful using AWS-IAM authentication");

spring-vault-core/src/main/java/org/springframework/vault/authentication/CubbyholeAuthentication.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.vault.authentication;
1717

18+
import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.*;
19+
1820
import java.util.Map;
1921

2022
import org.apache.commons.logging.Log;
@@ -33,8 +35,6 @@
3335
import org.springframework.web.client.RestClientException;
3436
import org.springframework.web.client.RestOperations;
3537

36-
import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.method;
37-
3838
/**
3939
* Cubbyhole {@link ClientAuthentication} implementation.
4040
* <p>
@@ -225,9 +225,7 @@ private boolean shouldEnhanceTokenWithSelfLookup(VaultToken token) {
225225
return false;
226226
}
227227

228-
if (token instanceof LoginToken) {
229-
230-
LoginToken loginToken = (LoginToken) token;
228+
if (token instanceof LoginToken loginToken) {
231229

232230
if (loginToken.getLeaseDuration().isZero()) {
233231
return false;
@@ -273,17 +271,17 @@ private static VaultToken getToken(CubbyholeAuthenticationOptions options, Vault
273271
Map<String, Object> data = response.getData();
274272
if (data == null || data.isEmpty()) {
275273
throw new VaultLoginException(
276-
String.format("Cannot retrieve Token from Cubbyhole: Response at %s does not contain a token",
277-
options.getPath()));
274+
"Cannot retrieve Token from Cubbyhole: Response at %s does not contain a token"
275+
.formatted(options.getPath()));
278276
}
279277

280278
if (data.size() == 1) {
281279
String token = (String) data.get(data.keySet().iterator().next());
282280
return VaultToken.of(token);
283281
}
284282

285-
throw new VaultLoginException(String
286-
.format("Cannot retrieve Token from Cubbyhole: Response at %s does not contain an unique token", url));
283+
throw new VaultLoginException(
284+
"Cannot retrieve Token from Cubbyhole: Response at %s does not contain an unique token".formatted(url));
287285
}
288286

289287
}

0 commit comments

Comments
 (0)