Polishing.

See gh-158.
This commit is contained in:
Mark Paluch
2017-11-14 15:25:03 +01:00
parent a1cc3a7cb2
commit bba0a7f096
8 changed files with 43 additions and 27 deletions

View File

@@ -107,21 +107,7 @@ public class AwsIamAuthentication implements ClientAuthentication {
@SuppressWarnings("unchecked")
private VaultToken createTokenUsingAwsIam() {
Map<String, String> 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<String, String> 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<String, String> createRequestBody(
AwsIamAuthenticationOptions options) {
Map<String, String> 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<String, String> headers = createIamRequestHeaders(options);

View File

@@ -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.
*

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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