diff --git a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java index 06ea54e62..3e6c0ca1c 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsExecutor.java @@ -92,6 +92,7 @@ public VaultToken login() throws VaultException { String.format("Cannot retrieve VaultToken from authentication chain. Got instead %s", state)); } + @SuppressWarnings({ "unchecked", "ConstantConditions" }) private Object evaluate(Iterable> steps) { Object state = null; @@ -144,28 +145,6 @@ private Object evaluate(Iterable> steps) { return state; } - private static Object doScalarValueStep(ScalarValueStep scalarValueStep) { - return scalarValueStep.get(); - } - - private static Object doSupplierStep(SupplierStep supplierStep) { - return supplierStep.get(); - } - - private static Object doMapStep(MapStep o, Object state) { - return o.apply(state); - } - - private Object doZipStep(ZipStep o, Object state) { - - Object result = evaluate(o.getRight()); - return Pair.of(state, result); - } - - private static Object doOnNext(OnNextStep o, Object state) { - return o.apply(state); - } - @SuppressWarnings("ConstantConditions") @Nullable private Object doHttpRequest(HttpRequestNode step, @Nullable Object state) { @@ -187,7 +166,7 @@ private Object doHttpRequest(HttpRequestNode step, @Nullable Object stat } - private static HttpEntity getEntity(HttpEntity entity, @Nullable Object state) { + private static HttpEntity getEntity(@Nullable HttpEntity entity, @Nullable Object state) { if (entity == null) { return state == null ? HttpEntity.EMPTY : new HttpEntity<>(state); @@ -200,4 +179,26 @@ private static HttpEntity getEntity(HttpEntity entity, @Nullable Object st return entity; } + private static Object doMapStep(MapStep o, Object state) { + return o.apply(state); + } + + private Object doZipStep(ZipStep o, Object state) { + + Object result = evaluate(o.getRight()); + return Pair.of(state, result); + } + + private static Object doOnNext(OnNextStep o, Object state) { + return o.apply(state); + } + + private static Object doScalarValueStep(ScalarValueStep scalarValueStep) { + return scalarValueStep.get(); + } + + private static Object doSupplierStep(SupplierStep supplierStep) { + return supplierStep.get(); + } + } diff --git a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsOperator.java b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsOperator.java index 64f2737c8..8416e3262 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsOperator.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AuthenticationStepsOperator.java @@ -29,6 +29,7 @@ import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.http.HttpEntity; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.vault.VaultException; import org.springframework.vault.authentication.AuthenticationSteps.HttpRequest; @@ -117,7 +118,7 @@ public Mono getVaultToken() throws VaultException { @SuppressWarnings("unchecked") private Mono createMono(Iterable> steps) { - Mono state = Mono.just(Undefinded.INSTANCE); + Mono state = Mono.just(Undefinded.UNDEFINDED); for (Node o : steps) { @@ -157,43 +158,6 @@ private Mono createMono(Iterable> steps) { return state; } - private static Object doScalarValueStep(ScalarValueStep scalarValueStep) { - return scalarValueStep.get(); - } - - private Mono doSupplierStepLater(SupplierStep supplierStep) { - - Supplier supplier = supplierStep.getSupplier(); - - if (!(supplier instanceof ResourceCredentialSupplier)) { - return Mono.fromSupplier(supplierStep.getSupplier()).subscribeOn(Schedulers.boundedElastic()); - } - - ResourceCredentialSupplier resourceSupplier = (ResourceCredentialSupplier) supplier; - - return DataBufferUtils.join(DataBufferUtils.read(resourceSupplier.getResource(), this.factory, 4096)) - .map(dataBuffer -> { - String result = dataBuffer.toString(ResourceCredentialSupplier.CHARSET); - DataBufferUtils.release(dataBuffer); - return (Object) result; - }).onErrorMap(IOException.class, - e -> new VaultException( - String.format("Credential retrieval from %s failed", resourceSupplier.getResource()), - e)); - } - - private static Object doMapStep(MapStep o, Object state) { - return o.apply(state); - } - - private Mono doZipStep(ZipStep o) { - return createMono(o.getRight()); - } - - private static Object doOnNext(OnNextStep o, Object state) { - return o.apply(state); - } - private Mono doHttpRequest(HttpRequestNode step, Object state) { HttpRequest definition = step.getDefinition(); @@ -213,14 +177,14 @@ private Mono doHttpRequest(HttpRequestNode step, Object state) { spec = spec.header(header.getKey(), header.getValue().get(0)); } - if (entity.getBody() != null && !entity.getBody().equals(Undefinded.INSTANCE)) { + if (entity.getBody() != null && !entity.getBody().equals(Undefinded.UNDEFINDED)) { return spec.bodyValue(entity.getBody()).retrieve().bodyToMono(definition.getResponseType()); } return spec.retrieve().bodyToMono(definition.getResponseType()); } - private static HttpEntity getEntity(HttpEntity entity, Object state) { + private static HttpEntity getEntity(@Nullable HttpEntity entity, @Nullable Object state) { if (entity == null) { return state == null ? HttpEntity.EMPTY : new HttpEntity<>(state); @@ -233,13 +197,47 @@ private static HttpEntity getEntity(HttpEntity entity, Object state) { return entity; } - static class Undefinded { + private static Object doMapStep(MapStep o, Object state) { + return o.apply(state); + } - static final Undefinded INSTANCE = new Undefinded(); + private Mono doZipStep(ZipStep o) { + return createMono(o.getRight()); + } + + private static void doOnNext(OnNextStep o, Object state) { + o.apply(state); + } + + private static Object doScalarValueStep(ScalarValueStep scalarValueStep) { + return scalarValueStep.get(); + } + + private Mono doSupplierStepLater(SupplierStep supplierStep) { + + Supplier supplier = supplierStep.getSupplier(); - private Undefinded() { + if (!(supplier instanceof ResourceCredentialSupplier)) { + return Mono.fromSupplier(supplierStep.getSupplier()).subscribeOn(Schedulers.boundedElastic()); } + ResourceCredentialSupplier resourceSupplier = (ResourceCredentialSupplier) supplier; + + return DataBufferUtils.join(DataBufferUtils.read(resourceSupplier.getResource(), this.factory, 4096)) + .map(dataBuffer -> { + String result = dataBuffer.toString(ResourceCredentialSupplier.CHARSET); + DataBufferUtils.release(dataBuffer); + return (Object) result; + }).onErrorMap(IOException.class, + e -> new VaultException( + String.format("Credential retrieval from %s failed", resourceSupplier.getResource()), + e)); + } + + enum Undefinded { + + UNDEFINDED; + } }