Skip to content

Commit 5936bfd

Browse files
committed
Polishing.
Rename consistently role option. Add since and author tags. Closes gh-780
1 parent 4a60c1c commit 5936bfd

File tree

7 files changed

+47
-29
lines changed

7 files changed

+47
-29
lines changed

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

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

18-
import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.post;
19-
2018
import java.util.Collections;
2119
import java.util.Map;
2220

2321
import org.apache.commons.logging.Log;
2422
import org.apache.commons.logging.LogFactory;
23+
24+
import org.springframework.util.Assert;
25+
import org.springframework.vault.support.VaultResponse;
26+
import org.springframework.vault.support.VaultToken;
27+
import org.springframework.web.client.RestClientException;
28+
import org.springframework.web.client.RestOperations;
29+
30+
import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.*;
31+
32+
import org.apache.commons.logging.Log;
33+
import org.apache.commons.logging.LogFactory;
34+
2535
import org.springframework.util.Assert;
2636
import org.springframework.vault.support.VaultResponse;
2737
import org.springframework.vault.support.VaultToken;
@@ -32,6 +42,7 @@
3242
* TLS Client Certificate {@link ClientAuthentication}.
3343
*
3444
* @author Mark Paluch
45+
* @author Andy Lintner
3546
*/
3647
public class ClientCertificateAuthentication implements ClientAuthentication, AuthenticationStepsFactory {
3748

@@ -83,8 +94,7 @@ public static AuthenticationSteps createAuthenticationSteps() {
8394
public static AuthenticationSteps createAuthenticationSteps(ClientCertificateAuthenticationOptions options) {
8495
Assert.notNull(options, "ClientCertificateAuthenticationOptions must not be null");
8596

86-
String name = options.getName();
87-
Map<String, String> body = name != null ? Collections.singletonMap("name", name) : Collections.emptyMap();
97+
Map<String, Object> body = getRequestBody(options);
8898

8999
return AuthenticationSteps.fromSupplier(() -> body)
90100
.login(post(AuthenticationUtil.getLoginPath(options.getPath())).as(VaultResponse.class));
@@ -103,12 +113,9 @@ public AuthenticationSteps getAuthenticationSteps() {
103113
private VaultToken createTokenUsingTlsCertAuthentication() {
104114

105115
try {
106-
String name = this.options.getName();
107-
116+
Map<String, Object> request = getRequestBody(this.options);
108117
VaultResponse response = this.restOperations.postForObject(
109-
AuthenticationUtil.getLoginPath(this.options.getPath()),
110-
name != null ? Collections.singletonMap("name", name) : Collections.emptyMap(),
111-
VaultResponse.class);
118+
AuthenticationUtil.getLoginPath(this.options.getPath()), request, VaultResponse.class);
112119

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

@@ -121,4 +128,10 @@ private VaultToken createTokenUsingTlsCertAuthentication() {
121128
}
122129
}
123130

131+
private static Map<String, Object> getRequestBody(ClientCertificateAuthenticationOptions options) {
132+
String name = options.getRole();
133+
134+
return name != null ? Collections.singletonMap("name", name) : Collections.emptyMap();
135+
}
136+
124137
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* constructed.
2727
*
2828
* @author Mark Paluch
29+
* @author Andy Lintner
2930
* @since 2.3
3031
* @see ClientCertificateAuthenticationOptions
3132
* @see #builder()
@@ -40,14 +41,14 @@ public class ClientCertificateAuthenticationOptions {
4041
private final String path;
4142

4243
/**
43-
* Optional named certificate role to authenticate against.
44+
* Named certificate role to authenticate against. Can be {@literal null}.
4445
*/
4546
@Nullable
46-
private final String name;
47+
private final String role;
4748

48-
private ClientCertificateAuthenticationOptions(String path, String name) {
49+
private ClientCertificateAuthenticationOptions(String path, @Nullable String role) {
4950
this.path = path;
50-
this.name = name;
51+
this.role = role;
5152
}
5253

5354
/**
@@ -66,10 +67,11 @@ public String getPath() {
6667

6768
/**
6869
* @return the optional named certificate role to authenticate against.
70+
* @since 2.3.4
6971
*/
7072
@Nullable
71-
public String getName() {
72-
return this.name;
73+
public String getRole() {
74+
return this.role;
7375
}
7476

7577
/**
@@ -80,7 +82,7 @@ public static class ClientCertificateAuthenticationOptionsBuilder {
8082
private String path = DEFAULT_CERT_PATH;
8183

8284
@Nullable
83-
private String name;
85+
private String role;
8486

8587
ClientCertificateAuthenticationOptionsBuilder() {
8688
}
@@ -102,12 +104,13 @@ public ClientCertificateAuthenticationOptionsBuilder path(String path) {
102104
* Configure the named certificate role to authenticate against.
103105
* @param name must not be empty or {@literal null}.
104106
* @return {@code this} {@link ClientCertificateAuthenticationOptionsBuilder}.
107+
* @since 2.3.4
105108
*/
106-
public ClientCertificateAuthenticationOptionsBuilder name(String name) {
109+
public ClientCertificateAuthenticationOptionsBuilder role(String name) {
107110

108-
Assert.hasText(name, "Name must not be empty");
111+
Assert.hasText(name, "Role must not be empty");
109112

110-
this.name = name;
113+
this.role = name;
111114
return this;
112115
}
113116

@@ -116,7 +119,7 @@ public ClientCertificateAuthenticationOptionsBuilder name(String name) {
116119
* @return a new {@link ClientCertificateAuthenticationOptions}.
117120
*/
118121
public ClientCertificateAuthenticationOptions build() {
119-
return new ClientCertificateAuthenticationOptions(this.path, this.name);
122+
return new ClientCertificateAuthenticationOptions(this.path, this.role);
120123
}
121124

122125
}

spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationIntegrationTestBase.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* Integration test base class for {@link ClientCertificateAuthentication} tests.
4444
*
4545
* @author Mark Paluch
46+
* @author Andy Lintner
4647
*/
4748
public abstract class ClientCertificateAuthenticationIntegrationTestBase extends IntegrationTestSupport {
4849

@@ -86,13 +87,13 @@ public void before() {
8687
});
8788
}
8889

89-
ListAssert<String> assertThatPolicies(final VaultToken token) {
90+
ListAssert<String> assertThatPolicies(VaultToken token) {
9091
return assertThat(lookupSelf(token).getBody()).isNotNull()
9192
.extracting("data", as(InstanceOfAssertFactories.map(String.class, Object.class))).isNotNull()
9293
.extracting("policies", as(InstanceOfAssertFactories.list(String.class))).isNotNull();
9394
}
9495

95-
ResponseEntity<Map<String, Object>> lookupSelf(final VaultToken token) {
96+
ResponseEntity<Map<String, Object>> lookupSelf(VaultToken token) {
9697

9798
return vaultOperations.doWithVault(restOperations -> {
9899
HttpHeaders headers = new HttpHeaders();

spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationIntegrationTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Integration tests for {@link ClientCertificateAuthentication}.
3232
*
3333
* @author Mark Paluch
34+
* @author Andy Lintner
3435
*/
3536
class ClientCertificateAuthenticationIntegrationTests extends ClientCertificateAuthenticationIntegrationTestBase {
3637

@@ -90,7 +91,7 @@ void shouldSelectRoleOne() {
9091
RestTemplate restTemplate = VaultClients.createRestTemplate(TestRestTemplateFactory.TEST_VAULT_ENDPOINT,
9192
clientHttpRequestFactory);
9293
ClientCertificateAuthentication authentication = new ClientCertificateAuthentication(
93-
ClientCertificateAuthenticationOptions.builder().name("my-default-role").build(), restTemplate);
94+
ClientCertificateAuthenticationOptions.builder().role("my-default-role").build(), restTemplate);
9495
VaultToken login = authentication.login();
9596

9697
assertThat(login.getToken()).isNotEmpty();
@@ -106,7 +107,7 @@ void shouldSelectRoleTwo() {
106107
RestTemplate restTemplate = VaultClients.createRestTemplate(TestRestTemplateFactory.TEST_VAULT_ENDPOINT,
107108
clientHttpRequestFactory);
108109
ClientCertificateAuthentication authentication = new ClientCertificateAuthentication(
109-
ClientCertificateAuthenticationOptions.builder().name("my-alternate-role").build(), restTemplate);
110+
ClientCertificateAuthenticationOptions.builder().role("my-alternate-role").build(), restTemplate);
110111
VaultToken login = authentication.login();
111112

112113
assertThat(login.getToken()).isNotEmpty();

spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationOperatorIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void shouldSelectRoleOne() {
8383

8484
AuthenticationStepsOperator operator = new AuthenticationStepsOperator(
8585
ClientCertificateAuthentication.createAuthenticationSteps(
86-
ClientCertificateAuthenticationOptions.builder().name("my-default-role").build()),
86+
ClientCertificateAuthenticationOptions.builder().role("my-default-role").build()),
8787
webClient);
8888

8989
operator.getVaultToken() //
@@ -100,7 +100,7 @@ void shouldSelectRoleTwo() {
100100

101101
AuthenticationStepsOperator operator = new AuthenticationStepsOperator(
102102
ClientCertificateAuthentication.createAuthenticationSteps(
103-
ClientCertificateAuthenticationOptions.builder().name("my-alternate-role").build()),
103+
ClientCertificateAuthenticationOptions.builder().role("my-alternate-role").build()),
104104
webClient);
105105

106106
operator.getVaultToken() //

spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationStepsIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
import org.springframework.vault.util.TestRestTemplateFactory;
2828
import org.springframework.web.client.RestTemplate;
2929

30-
import static org.assertj.core.api.Assertions.assertThat;
31-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
30+
import static org.assertj.core.api.Assertions.*;
3231

3332
/**
3433
* Integration tests for {@link ClientCertificateAuthentication} using
3534
* {@link AuthenticationStepsExecutor}.
3635
*
3736
* @author Mark Paluch
37+
* @author Andy Lintner
3838
*/
3939
class ClientCertificateAuthenticationStepsIntegrationTests extends ClientCertificateAuthenticationIntegrationTestBase {
4040

spring-vault-core/src/test/java/org/springframework/vault/authentication/ClientCertificateAuthenticationUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void loginShouldObtainToken() {
6565
+ "}"));
6666

6767
ClientCertificateAuthenticationOptions options = ClientCertificateAuthenticationOptions.builder()
68-
.name("my-default-role") //
68+
.role("my-default-role") //
6969
.path("my/path").build();
7070

7171
ClientCertificateAuthentication sut = new ClientCertificateAuthentication(options, this.restTemplate);

0 commit comments

Comments
 (0)