Skip to content

Commit 4ef0362

Browse files
committed
Update code to use recent language features.
Closes #881
1 parent 899c9d1 commit 4ef0362

File tree

114 files changed

+532
-611
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

+532
-611
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
@@ -446,7 +446,7 @@ public static class HttpRequest<T> {
446446

447447
@Override
448448
public String toString() {
449-
return String.format("%s %s AS %s", getMethod(), getUri() != null ? getUri() : getUriTemplate(),
449+
return "%s %s AS %s".formatted(getMethod(), getUri() != null ? getUri() : getUriTemplate(),
450450
getResponseType());
451451
}
452452

@@ -508,9 +508,8 @@ public Node<?> getPrevious() {
508508
public boolean equals(Object o) {
509509
if (this == o)
510510
return true;
511-
if (!(o instanceof HttpRequestNode))
511+
if (!(o instanceof HttpRequestNode<?> that))
512512
return false;
513-
HttpRequestNode<?> that = (HttpRequestNode<?>) o;
514513
return this.definition.equals(that.definition) && this.previous.equals(that.previous);
515514
}
516515

@@ -553,9 +552,8 @@ public Node<?> getPrevious() {
553552
public boolean equals(Object o) {
554553
if (this == o)
555554
return true;
556-
if (!(o instanceof MapStep))
555+
if (!(o instanceof MapStep<?, ?> mapStep))
557556
return false;
558-
MapStep<?, ?> mapStep = (MapStep<?, ?>) o;
559557
return this.mapper.equals(mapStep.mapper) && this.previous.equals(mapStep.previous);
560558
}
561559

@@ -599,9 +597,8 @@ public List<Node<?>> getRight() {
599597
public boolean equals(Object o) {
600598
if (this == o)
601599
return true;
602-
if (!(o instanceof ZipStep))
600+
if (!(o instanceof ZipStep<?, ?> zipStep))
603601
return false;
604-
ZipStep<?, ?> zipStep = (ZipStep<?, ?>) o;
605602
return this.left.equals(zipStep.left) && this.right.equals(zipStep.right);
606603
}
607604

@@ -645,9 +642,8 @@ public AuthenticationSteps.Node<?> getPrevious() {
645642
public boolean equals(Object o) {
646643
if (this == o)
647644
return true;
648-
if (!(o instanceof OnNextStep))
645+
if (!(o instanceof OnNextStep<?> that))
649646
return false;
650-
OnNextStep<?> that = (OnNextStep<?>) o;
651647
return this.consumer.equals(that.consumer) && this.previous.equals(that.previous);
652648
}
653649

@@ -686,9 +682,8 @@ public Node<?> getPrevious() {
686682
public boolean equals(Object o) {
687683
if (this == o)
688684
return true;
689-
if (!(o instanceof ScalarValueStep))
685+
if (!(o instanceof ScalarValueStep<?> that))
690686
return false;
691-
ScalarValueStep<?> that = (ScalarValueStep<?>) o;
692687
return this.value.equals(that.value) && this.previous.equals(that.previous);
693688
}
694689

@@ -731,9 +726,8 @@ public Node<?> getPrevious() {
731726
public boolean equals(Object o) {
732727
if (this == o)
733728
return true;
734-
if (!(o instanceof SupplierStep))
729+
if (!(o instanceof SupplierStep<?> that))
735730
return false;
736-
SupplierStep<?> that = (SupplierStep<?>) o;
737731
return this.supplier.equals(that.supplier) && this.previous.equals(that.previous);
738732
}
739733

@@ -798,9 +792,8 @@ public R getRight() {
798792
public boolean equals(Object o) {
799793
if (this == o)
800794
return true;
801-
if (!(o instanceof Pair))
795+
if (!(o instanceof Pair<?, ?> pair))
802796
return false;
803-
Pair<?, ?> pair = (Pair<?, ?>) o;
804797
return this.left.equals(pair.left) && this.right.equals(pair.right);
805798
}
806799

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
@@ -81,15 +81,14 @@ public VaultToken login() throws VaultException {
8181
return (VaultToken) state;
8282
}
8383

84-
if (state instanceof VaultResponse) {
84+
if (state instanceof VaultResponse response) {
8585

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

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

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

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

106105
try {
@@ -129,17 +128,15 @@ private Object evaluate(Iterable<Node<?>> steps) {
129128
}
130129

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

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,15 @@ public Mono<VaultToken> getVaultToken() throws VaultException {
101101
return (VaultToken) stateObject;
102102
}
103103

104-
if (stateObject instanceof VaultResponse) {
105-
106-
VaultResponse response = (VaultResponse) stateObject;
104+
if (stateObject instanceof VaultResponse response) {
107105

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

110108
return LoginTokenUtil.from(response.getAuth());
111109
}
112110

113111
throw new IllegalStateException(
114-
String.format("Cannot retrieve VaultToken from authentication chain. Got instead %s", stateObject));
112+
"Cannot retrieve VaultToken from authentication chain. Got instead %s".formatted(stateObject));
115113
}).onErrorMap(t -> new VaultLoginException("Cannot retrieve VaultToken from authentication chain", t));
116114
}
117115

@@ -123,7 +121,7 @@ private Mono<Object> createMono(Iterable<Node<?>> steps) {
123121
for (Node<?> o : steps) {
124122

125123
if (logger.isDebugEnabled()) {
126-
logger.debug(String.format("Executing %s with current state %s", o, state));
124+
logger.debug("Executing %s with current state %s".formatted(o, state));
127125
}
128126

129127
if (o instanceof HttpRequestNode) {
@@ -152,7 +150,7 @@ private Mono<Object> createMono(Iterable<Node<?>> steps) {
152150
}
153151

154152
if (logger.isDebugEnabled()) {
155-
logger.debug(String.format("Executed %s with current state %s", o, state));
153+
logger.debug("Executed %s with current state %s".formatted(o, state));
156154
}
157155
}
158156
return state;
@@ -217,20 +215,18 @@ private Mono<Object> doSupplierStepLater(SupplierStep<Object> supplierStep) {
217215

218216
Supplier<?> supplier = supplierStep.getSupplier();
219217

220-
if (!(supplier instanceof ResourceCredentialSupplier)) {
218+
if (!(supplier instanceof ResourceCredentialSupplier resourceSupplier)) {
221219
return Mono.fromSupplier(supplierStep.getSupplier()).subscribeOn(Schedulers.boundedElastic());
222220
}
223221

224-
ResourceCredentialSupplier resourceSupplier = (ResourceCredentialSupplier) supplier;
225-
226222
return DataBufferUtils.join(DataBufferUtils.read(resourceSupplier.getResource(), this.factory, 4096))
227223
.map(dataBuffer -> {
228224
String result = dataBuffer.toString(ResourceCredentialSupplier.CHARSET);
229225
DataBufferUtils.release(dataBuffer);
230226
return (Object) result;
231227
})
232228
.onErrorMap(IOException.class, e -> new VaultException(
233-
String.format("Credential retrieval from %s failed", resourceSupplier.getResource()), e));
229+
"Credential retrieval from %s failed".formatted(resourceSupplier.getResource()), e));
234230
}
235231

236232
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ private VaultToken createTokenUsingAwsEc2() {
157157

158158
if (response.getAuth().get("metadata") instanceof Map) {
159159
Map<Object, Object> metadata = (Map<Object, Object>) response.getAuth().get("metadata");
160-
logger.debug(String.format("Login successful using AWS-EC2 authentication for instance %s, AMI %s",
161-
metadata.get("instance_id"), metadata.get("instance_id")));
160+
logger.debug("Login successful using AWS-EC2 authentication for instance %s, AMI %s"
161+
.formatted(metadata.get("instance_id"), metadata.get("instance_id")));
162162
}
163163
else {
164164
logger.debug("Login successful using AWS-EC2 authentication");
@@ -197,7 +197,7 @@ protected Map<String, String> getEc2Login() {
197197
}
198198
catch (RestClientException e) {
199199
throw new VaultLoginException(
200-
String.format("Cannot obtain Identity Document from %s", this.options.getIdentityDocumentUri()), e);
200+
"Cannot obtain Identity Document from %s".formatted(this.options.getIdentityDocumentUri()), e);
201201
}
202202
}
203203

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
}

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

Lines changed: 3 additions & 3 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.LinkedHashMap;
1921
import java.util.Map;
2022

@@ -29,8 +31,6 @@
2931
import org.springframework.web.client.HttpStatusCodeException;
3032
import org.springframework.web.client.RestOperations;
3133

32-
import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.get;
33-
3434
/**
3535
* GCP GCE (Google Compute Engine)-based login implementation using GCE's metadata service
3636
* to create signed JSON Web Token.
@@ -157,7 +157,7 @@ private static HttpHeaders getMetadataHttpHeaders() {
157157
}
158158

159159
private static String getAudience(String role) {
160-
return String.format("https://localhost:8200/vault/%s", role);
160+
return "https://localhost:8200/vault/%s".formatted(role);
161161
}
162162

163163
}

0 commit comments

Comments
 (0)