Skip to content

Commit 2fd0a88

Browse files
committed
Consider absent secretId in AppRole authentication steps.
We now skip secretId retrieval when using AppRole authentication steps to avoid Unknown SecretId configuration errors. Also, renamed AbsentSecretId.INSTANCE to ABSENT_SECRET_ID to cause more meaningful messages when used in toString. Closes gh-656
1 parent 0a935e1 commit 2fd0a88

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@
4343
import org.springframework.web.client.RestClientException;
4444
import org.springframework.web.client.RestOperations;
4545

46-
import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.get;
47-
import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.method;
48-
import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.post;
49-
import static org.springframework.vault.authentication.AuthenticationUtil.getLoginPath;
46+
import static org.springframework.vault.authentication.AuthenticationSteps.HttpRequestBuilder.*;
47+
import static org.springframework.vault.authentication.AuthenticationUtil.*;
5048

5149
/**
5250
* AppRole implementation of {@link ClientAuthentication}. RoleId and SecretId (optional)
@@ -107,6 +105,11 @@ private static Node<Map<String, String>> getAuthenticationSteps(AppRoleAuthentic
107105
SecretId secretId) {
108106

109107
Node<String> roleIdSteps = getRoleIdSteps(options, roleId);
108+
109+
if (!hasSecretId(options.getSecretId())) {
110+
return roleIdSteps.map(it -> getAppRoleLoginBody(it, null));
111+
}
112+
110113
Node<String> secretIdSteps = getSecretIdSteps(options, secretId);
111114

112115
return roleIdSteps.zipWith(secretIdSteps).map(it -> getAppRoleLoginBody(it.getLeft(), it.getRight()));
@@ -293,7 +296,7 @@ private static HttpHeaders createHttpHeaders(VaultToken token) {
293296
}
294297

295298
private static HttpEntity<String> createHttpEntity(VaultToken token) {
296-
return new HttpEntity<String>(null, createHttpHeaders(token));
299+
return new HttpEntity<>(null, createHttpHeaders(token));
297300
}
298301

299302
private Map<String, String> getAppRoleLoginBody(RoleId roleId, SecretId secretId) {
@@ -302,13 +305,17 @@ private Map<String, String> getAppRoleLoginBody(RoleId roleId, SecretId secretId
302305

303306
login.put("role_id", getRoleId(roleId));
304307

305-
if (!ClassUtils.isAssignableValue(AbsentSecretId.class, secretId)) {
308+
if (hasSecretId(secretId)) {
306309
login.put("secret_id", getSecretId(secretId));
307310
}
308311

309312
return login;
310313
}
311314

315+
private static boolean hasSecretId(SecretId secretId) {
316+
return !ClassUtils.isAssignableValue(AbsentSecretId.class, secretId);
317+
}
318+
312319
private static Map<String, String> getAppRoleLoginBody(String roleId, @Nullable String secretId) {
313320

314321
Map<String, String> login = new HashMap<>();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ static SecretId provided(String secretId) {
448448
* @return a {@link SecretId} that represents an absent secretId
449449
*/
450450
static SecretId absent() {
451-
return AbsentSecretId.INSTANCE;
451+
return AbsentSecretId.ABSENT_SECRET_ID;
452452
}
453453

454454
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AppRoleTokens {
3232
*/
3333
enum AbsentSecretId implements SecretId {
3434

35-
INSTANCE;
35+
ABSENT_SECRET_ID;
3636

3737
}
3838

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@
3838
*/
3939
class AppRoleAuthenticationStepsIntegrationTests extends AppRoleAuthenticationIntegrationTestBase {
4040

41+
@Test
42+
void shouldAuthenticateWithRoleIdOnly() {
43+
44+
String roleId = getRoleId("no-secret-id");
45+
AppRoleAuthenticationOptions options = AppRoleAuthenticationOptions.builder().roleId(RoleId.provided(roleId))
46+
.build();
47+
48+
AuthenticationStepsExecutor executor = new AuthenticationStepsExecutor(
49+
AppRoleAuthentication.createAuthenticationSteps(options), prepare().getRestTemplate());
50+
51+
assertThat(executor.login()).isNotNull();
52+
}
53+
4154
@Test
4255
void authenticationStepsShouldAuthenticateWithWrappedSecretId() {
4356

0 commit comments

Comments
 (0)