Polishing.

Rename consistently role option. Add since and author tags.

Closes gh-780
This commit is contained in:
Mark Paluch
2023-04-19 08:27:18 +02:00
parent 4a60c1c79c
commit 5936bfd833
7 changed files with 47 additions and 29 deletions

View File

@@ -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<String, String> body = name != null ? Collections.singletonMap("name", name) : Collections.emptyMap();
Map<String, Object> 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<String, Object> 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<String, Object> getRequestBody(ClientCertificateAuthenticationOptions options) {
String name = options.getRole();
return name != null ? Collections.singletonMap("name", name) : Collections.emptyMap();
}
}

View File

@@ -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);
}
}

View File

@@ -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<String> assertThatPolicies(final VaultToken token) {
ListAssert<String> 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<Map<String, Object>> lookupSelf(final VaultToken token) {
ResponseEntity<Map<String, Object>> lookupSelf(VaultToken token) {
return vaultOperations.doWithVault(restOperations -> {
HttpHeaders headers = new HttpHeaders();

View File

@@ -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();

View File

@@ -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() //

View File

@@ -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 {

View File

@@ -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);