diff --git a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AwsIamAuthentication.java b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AwsIamAuthentication.java index 92271804..f43846a9 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AwsIamAuthentication.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AwsIamAuthentication.java @@ -107,21 +107,7 @@ public class AwsIamAuthentication implements ClientAuthentication { @SuppressWarnings("unchecked") private VaultToken createTokenUsingAwsIam() { - Map login = new HashMap<>(); - - login.put("iam_http_request_method", "POST"); - login.put("iam_request_url", Base64Utils.encodeToString(options.getEndpointUri() - .toString().getBytes())); - login.put("iam_request_body", REQUEST_BODY_BASE64_ENCODED); - - String headerJson = getSignedHeaders(options); - - login.put("iam_request_headers", - Base64Utils.encodeToString(headerJson.getBytes())); - - if (!StringUtils.isEmpty(options.getRole())) { - login.put("role", options.getRole()); - } + Map login = createRequestBody(this.options); try { @@ -154,6 +140,34 @@ public class AwsIamAuthentication implements ClientAuthentication { } } + /** + * Create the request body to perform a Vault login using the AWS-IAM authentication + * method. + * + * @param options must not be {@literal null}. + * @return the map containing body key-value pairs. + */ + protected static Map createRequestBody( + AwsIamAuthenticationOptions options) { + + Map login = new HashMap<>(); + + login.put("iam_http_request_method", "POST"); + login.put("iam_request_url", Base64Utils.encodeToString(options.getEndpointUri() + .toString().getBytes())); + login.put("iam_request_body", REQUEST_BODY_BASE64_ENCODED); + + String headerJson = getSignedHeaders(options); + + login.put("iam_request_headers", + Base64Utils.encodeToString(headerJson.getBytes())); + + if (!StringUtils.isEmpty(options.getRole())) { + login.put("role", options.getRole()); + } + return login; + } + private static String getSignedHeaders(AwsIamAuthenticationOptions options) { Map headers = createIamRequestHeaders(options); diff --git a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AwsIamAuthenticationOptions.java b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AwsIamAuthenticationOptions.java index 91d1217c..d5336194 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/authentication/AwsIamAuthenticationOptions.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/authentication/AwsIamAuthenticationOptions.java @@ -51,7 +51,9 @@ public class AwsIamAuthenticationOptions { private final AWSCredentialsProvider credentialsProvider; /** - * EC2 instance role name. May be {@literal null} if none. + * Name of the role against which the login is being attempted. If role is not + * specified, the friendly name (i.e., role name or username) of the IAM principal + * authenticated. If a matching role is not found, login fails. */ @Nullable private final String role; @@ -194,7 +196,7 @@ public class AwsIamAuthenticationOptions { } /** - * Configure the name of the role against which the login is being attempted.If + * Configure the name of the role against which the login is being attempted. If * role is not specified, the friendly name (i.e., role name or username) of the * IAM principal authenticated. If a matching role is not found, login fails. * diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppIdAuthenticationUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppIdAuthenticationUnitTests.java index a602a471..bd389845 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppIdAuthenticationUnitTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AppIdAuthenticationUnitTests.java @@ -45,7 +45,7 @@ public class AppIdAuthenticationUnitTests { private MockRestServiceServer mockRest; @Before - public void before() throws Exception { + public void before() { RestTemplate restTemplate = VaultClients.createRestTemplate(); restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler()); @@ -54,7 +54,7 @@ public class AppIdAuthenticationUnitTests { } @Test - public void loginShouldObtainTokenWithStaticUserId() throws Exception { + public void loginShouldObtainTokenWithStaticUserId() { AppIdAuthenticationOptions options = AppIdAuthenticationOptions.builder() .appId("hello") // @@ -77,7 +77,7 @@ public class AppIdAuthenticationUnitTests { } @Test(expected = VaultException.class) - public void loginShouldFail() throws Exception { + public void loginShouldFail() { AppIdAuthenticationOptions options = AppIdAuthenticationOptions.builder() .appId("hello") // diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AuthenticationStepsExecutorUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AuthenticationStepsExecutorUnitTests.java index 28b27877..f941e64e 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AuthenticationStepsExecutorUnitTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AuthenticationStepsExecutorUnitTests.java @@ -53,7 +53,7 @@ public class AuthenticationStepsExecutorUnitTests { private MockRestServiceServer mockRest; @Before - public void before() throws Exception { + public void before() { RestTemplate restTemplate = VaultClients.createRestTemplate(); restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler()); diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AuthenticationStepsOperatorUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AuthenticationStepsOperatorUnitTests.java index 0df2ae03..e6dff77f 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/AuthenticationStepsOperatorUnitTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/AuthenticationStepsOperatorUnitTests.java @@ -41,7 +41,7 @@ import static org.springframework.vault.authentication.AuthenticationSteps.HttpR public class AuthenticationStepsOperatorUnitTests { @Before - public void before() throws Exception { + public void before() { } @Test 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 85ffd9e6..e9a53609 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 @@ -46,7 +46,7 @@ public class ClientCertificateAuthenticationUnitTests { private MockRestServiceServer mockRest; @Before - public void before() throws Exception { + public void before() { RestTemplate restTemplate = VaultClients.createRestTemplate(); restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler()); @@ -56,7 +56,7 @@ public class ClientCertificateAuthenticationUnitTests { } @Test - public void loginShouldObtainToken() throws Exception { + public void loginShouldObtainToken() { mockRest.expect(requestTo("/auth/cert/login")) .andExpect(method(HttpMethod.POST)) @@ -80,7 +80,7 @@ public class ClientCertificateAuthenticationUnitTests { } @Test(expected = VaultException.class) - public void loginShouldFail() throws Exception { + public void loginShouldFail() { mockRest.expect(requestTo("/auth/cert/login")) // .andRespond(withServerError()); diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/IpAddressUserIdTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/IpAddressUserIdTests.java index 8ebb2049..ea4319fb 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/IpAddressUserIdTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/IpAddressUserIdTests.java @@ -29,7 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class IpAddressUserIdTests { @Test - public void shouldGenerateUppercaseSha256HexString() throws Exception { + public void shouldGenerateUppercaseSha256HexString() { String userId = new IpAddressUserId().createUserId(); diff --git a/spring-vault-core/src/test/java/org/springframework/vault/authentication/MacAddressUserIdUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/authentication/MacAddressUserIdUnitTests.java index 8c961786..9dc8c211 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/authentication/MacAddressUserIdUnitTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/authentication/MacAddressUserIdUnitTests.java @@ -34,7 +34,7 @@ import static org.junit.Assume.assumeTrue; public class MacAddressUserIdUnitTests { @Test - public void shouldGenerateUppercaseSha256HexString() throws Exception { + public void shouldGenerateUppercaseSha256HexString() { String userId = new MacAddressUserId().createUserId();