diff --git a/spring-vault-core/src/main/java/org/springframework/vault/authentication/ClientCertificateAuthentication.java b/spring-vault-core/src/main/java/org/springframework/vault/authentication/ClientCertificateAuthentication.java index 99121855..d044ed7a 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/authentication/ClientCertificateAuthentication.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/authentication/ClientCertificateAuthentication.java @@ -15,13 +15,23 @@ */ package org.springframework.vault.authentication; -import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.post; - import java.util.Collections; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + +import org.springframework.util.Assert; +import org.springframework.vault.support.VaultResponse; +import org.springframework.vault.support.VaultToken; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestOperations; + +import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.*; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.springframework.util.Assert; import org.springframework.vault.support.VaultResponse; import org.springframework.vault.support.VaultToken; @@ -32,6 +42,7 @@ import org.springframework.web.client.RestOperations; * TLS Client Certificate {@link ClientAuthentication}. * * @author Mark Paluch + * @author Andy Lintner */ public class ClientCertificateAuthentication implements ClientAuthentication, AuthenticationStepsFactory { @@ -83,8 +94,7 @@ public class ClientCertificateAuthentication implements ClientAuthentication, Au public static AuthenticationSteps createAuthenticationSteps(ClientCertificateAuthenticationOptions options) { Assert.notNull(options, "ClientCertificateAuthenticationOptions must not be null"); - String name = options.getName(); - Map body = name != null ? Collections.singletonMap("name", name) : Collections.emptyMap(); + Map body = getRequestBody(options); return AuthenticationSteps.fromSupplier(() -> body) .login(post(AuthenticationUtil.getLoginPath(options.getPath())).as(VaultResponse.class)); @@ -103,12 +113,9 @@ public class ClientCertificateAuthentication implements ClientAuthentication, Au private VaultToken createTokenUsingTlsCertAuthentication() { try { - String name = this.options.getName(); - + Map request = getRequestBody(this.options); VaultResponse response = this.restOperations.postForObject( - AuthenticationUtil.getLoginPath(this.options.getPath()), - name != null ? Collections.singletonMap("name", name) : Collections.emptyMap(), - VaultResponse.class); + AuthenticationUtil.getLoginPath(this.options.getPath()), request, VaultResponse.class); Assert.state(response.getAuth() != null, "Auth field must not be null"); @@ -121,4 +128,10 @@ public class ClientCertificateAuthentication implements ClientAuthentication, Au } } + private static Map getRequestBody(ClientCertificateAuthenticationOptions options) { + String name = options.getRole(); + + return name != null ? Collections.singletonMap("name", name) : Collections.emptyMap(); + } + } diff --git a/spring-vault-core/src/main/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOptions.java b/spring-vault-core/src/main/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOptions.java index 630f3b9f..4d5b2c3a 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOptions.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOptions.java @@ -26,6 +26,7 @@ import org.springframework.util.Assert; * constructed. * * @author Mark Paluch + * @author Andy Lintner * @since 2.3 * @see ClientCertificateAuthenticationOptions * @see #builder() @@ -40,14 +41,14 @@ public class ClientCertificateAuthenticationOptions { private final String path; /** - * Optional named certificate role to authenticate against. + * Named certificate role to authenticate against. Can be {@literal null}. */ @Nullable - private final String name; + private final String role; - private ClientCertificateAuthenticationOptions(String path, String name) { + private ClientCertificateAuthenticationOptions(String path, @Nullable String role) { this.path = path; - this.name = name; + this.role = role; } /** @@ -66,10 +67,11 @@ public class ClientCertificateAuthenticationOptions { /** * @return the optional named certificate role to authenticate against. + * @since 2.3.4 */ @Nullable - public String getName() { - return this.name; + public String getRole() { + return this.role; } /** @@ -80,7 +82,7 @@ public class ClientCertificateAuthenticationOptions { private String path = DEFAULT_CERT_PATH; @Nullable - private String name; + private String role; ClientCertificateAuthenticationOptionsBuilder() { } @@ -102,12 +104,13 @@ public class ClientCertificateAuthenticationOptions { * Configure the named certificate role to authenticate against. * @param name must not be empty or {@literal null}. * @return {@code this} {@link ClientCertificateAuthenticationOptionsBuilder}. + * @since 2.3.4 */ - public ClientCertificateAuthenticationOptionsBuilder name(String name) { + public ClientCertificateAuthenticationOptionsBuilder role(String name) { - Assert.hasText(name, "Name must not be empty"); + Assert.hasText(name, "Role must not be empty"); - this.name = name; + this.role = name; return this; } @@ -116,7 +119,7 @@ public class ClientCertificateAuthenticationOptions { * @return a new {@link ClientCertificateAuthenticationOptions}. */ public ClientCertificateAuthenticationOptions build() { - return new ClientCertificateAuthenticationOptions(this.path, this.name); + return new ClientCertificateAuthenticationOptions(this.path, this.role); } } diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationIntegrationTestBase.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationIntegrationTestBase.java index c9c83773..668ed970 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationIntegrationTestBase.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationIntegrationTestBase.java @@ -43,6 +43,7 @@ import org.springframework.vault.util.IntegrationTestSupport; * Integration test base class for {@link ClientCertificateAuthentication} tests. * * @author Mark Paluch + * @author Andy Lintner */ public abstract class ClientCertificateAuthenticationIntegrationTestBase extends IntegrationTestSupport { @@ -86,13 +87,13 @@ public abstract class ClientCertificateAuthenticationIntegrationTestBase extends }); } - ListAssert assertThatPolicies(final VaultToken token) { + ListAssert assertThatPolicies(VaultToken token) { return assertThat(lookupSelf(token).getBody()).isNotNull() .extracting("data", as(InstanceOfAssertFactories.map(String.class, Object.class))).isNotNull() .extracting("policies", as(InstanceOfAssertFactories.list(String.class))).isNotNull(); } - ResponseEntity> lookupSelf(final VaultToken token) { + ResponseEntity> lookupSelf(VaultToken token) { return vaultOperations.doWithVault(restOperations -> { HttpHeaders headers = new HttpHeaders(); diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationIntegrationTests.java index 9a030ee7..525a43c3 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationIntegrationTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationIntegrationTests.java @@ -31,6 +31,7 @@ import org.springframework.web.client.RestTemplate; * Integration tests for {@link ClientCertificateAuthentication}. * * @author Mark Paluch + * @author Andy Lintner */ class ClientCertificateAuthenticationIntegrationTests extends ClientCertificateAuthenticationIntegrationTestBase { @@ -90,7 +91,7 @@ class ClientCertificateAuthenticationIntegrationTests extends ClientCertificateA RestTemplate restTemplate = VaultClients.createRestTemplate(TestRestTemplateFactory.TEST_VAULT_ENDPOINT, clientHttpRequestFactory); ClientCertificateAuthentication authentication = new ClientCertificateAuthentication( - ClientCertificateAuthenticationOptions.builder().name("my-default-role").build(), restTemplate); + ClientCertificateAuthenticationOptions.builder().role("my-default-role").build(), restTemplate); VaultToken login = authentication.login(); assertThat(login.getToken()).isNotEmpty(); @@ -106,7 +107,7 @@ class ClientCertificateAuthenticationIntegrationTests extends ClientCertificateA RestTemplate restTemplate = VaultClients.createRestTemplate(TestRestTemplateFactory.TEST_VAULT_ENDPOINT, clientHttpRequestFactory); ClientCertificateAuthentication authentication = new ClientCertificateAuthentication( - ClientCertificateAuthenticationOptions.builder().name("my-alternate-role").build(), restTemplate); + ClientCertificateAuthenticationOptions.builder().role("my-alternate-role").build(), restTemplate); VaultToken login = authentication.login(); assertThat(login.getToken()).isNotEmpty(); diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOperatorIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOperatorIntegrationTests.java index bb30489b..cd8e78d3 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOperatorIntegrationTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOperatorIntegrationTests.java @@ -83,7 +83,7 @@ class ClientCertificateAuthenticationOperatorIntegrationTests AuthenticationStepsOperator operator = new AuthenticationStepsOperator( ClientCertificateAuthentication.createAuthenticationSteps( - ClientCertificateAuthenticationOptions.builder().name("my-default-role").build()), + ClientCertificateAuthenticationOptions.builder().role("my-default-role").build()), webClient); operator.getVaultToken() // @@ -100,7 +100,7 @@ class ClientCertificateAuthenticationOperatorIntegrationTests AuthenticationStepsOperator operator = new AuthenticationStepsOperator( ClientCertificateAuthentication.createAuthenticationSteps( - ClientCertificateAuthenticationOptions.builder().name("my-alternate-role").build()), + ClientCertificateAuthenticationOptions.builder().role("my-alternate-role").build()), webClient); operator.getVaultToken() // diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationStepsIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationStepsIntegrationTests.java index 0a5c451f..c1b0a474 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationStepsIntegrationTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationStepsIntegrationTests.java @@ -27,14 +27,14 @@ import org.springframework.vault.util.Settings; import org.springframework.vault.util.TestRestTemplateFactory; import org.springframework.web.client.RestTemplate; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.*; /** * Integration tests for {@link ClientCertificateAuthentication} using * {@link AuthenticationStepsExecutor}. * * @author Mark Paluch + * @author Andy Lintner */ class ClientCertificateAuthenticationStepsIntegrationTests extends ClientCertificateAuthenticationIntegrationTestBase { diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationUnitTests.java index 24584ebf..1fdd4863 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationUnitTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationUnitTests.java @@ -65,7 +65,7 @@ class ClientCertificateAuthenticationUnitTests { + "}")); ClientCertificateAuthenticationOptions options = ClientCertificateAuthenticationOptions.builder() - .name("my-default-role") // + .role("my-default-role") // .path("my/path").build(); ClientCertificateAuthentication sut = new ClientCertificateAuthentication(options, this.restTemplate);