Skip to content

Commit 2b23c2e

Browse files
committed
Remove all newlines in PKCS7 from EC2MetaData.
Closes gh-571.
1 parent bdc2e79 commit 2b23c2e

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected static AuthenticationSteps createAuthenticationSteps(AwsEc2Authenticat
110110
return AuthenticationSteps
111111
.fromHttpRequest(HttpRequestBuilder.get(options.getIdentityDocumentUri().toString()).as(String.class)) //
112112
.map(pkcs7 -> pkcs7.replaceAll("\\r", "")) //
113-
.map(pkcs7 -> pkcs7.replace("\\n", "")) //
113+
.map(pkcs7 -> pkcs7.replaceAll("\\n", "")) //
114114
.map(pkcs7 -> {
115115

116116
Map<String, String> login = new HashMap<>();
@@ -189,7 +189,7 @@ protected Map<String, String> getEc2Login() {
189189
String pkcs7 = this.awsMetadataRestOperations.getForObject(this.options.getIdentityDocumentUri(),
190190
String.class);
191191
if (StringUtils.hasText(pkcs7)) {
192-
login.put("pkcs7", pkcs7.replaceAll("\\r", "").replace("\\n", ""));
192+
login.put("pkcs7", pkcs7.replaceAll("\\r", "").replaceAll("\\n", ""));
193193
}
194194

195195
return login;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ void shouldObtainIdentityDocument() {
7373
assertThat(authentication.getEc2Login()).containsEntry("pkcs7", "Hello, world").containsKey("nonce").hasSize(2);
7474
}
7575

76+
@Test
77+
void shouldCleanUpIdentityResponse() {
78+
79+
this.mockRest.expect(requestTo("http://169.254.169.254/latest/dynamic/instance-identity/pkcs7")) //
80+
.andExpect(method(HttpMethod.GET)) //
81+
.andRespond(withSuccess().body("Hello, \r\r\n\nworld"));
82+
83+
AwsEc2Authentication authentication = new AwsEc2Authentication(this.restTemplate);
84+
85+
assertThat(authentication.getEc2Login()).containsEntry("pkcs7", "Hello, world");
86+
}
87+
7688
@Test
7789
void shouldContainRole() {
7890

0 commit comments

Comments
 (0)