Skip to content

Commit 1e431c3

Browse files
missedonemp911de
authored andcommitted
Add AWS IAM Authentication to EnvironmentVaultConfiguration.
Closes gh-761 Original pull request gh-765
1 parent 1e943b8 commit 1e431c3

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@
234234
<version>${aws-java-sdk.version}</version>
235235
<optional>true</optional>
236236
</dependency>
237+
<dependency>
238+
<groupId>software.amazon.awssdk</groupId>
239+
<artifactId>sts</artifactId>
240+
<version>${aws-java-sdk.version}</version>
241+
<optional>true</optional>
242+
</dependency>
237243

238244
<!-- GCP SDK -->
239245
<dependency>

spring-vault-core/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,21 @@
189189
</exclusion>
190190
</exclusions>
191191
</dependency>
192+
<dependency>
193+
<groupId>software.amazon.awssdk</groupId>
194+
<artifactId>sts</artifactId>
195+
<optional>true</optional>
196+
<exclusions>
197+
<exclusion>
198+
<groupId>software.amazon.ion</groupId>
199+
<artifactId>ion-java</artifactId>
200+
</exclusion>
201+
<exclusion>
202+
<groupId>com.fasterxml.jackson.dataformat</groupId>
203+
<artifactId>jackson-dataformat-cbor</artifactId>
204+
</exclusion>
205+
</exclusions>
206+
</dependency>
192207

193208
<dependency>
194209
<groupId>com.google.apis</groupId>

spring-vault-core/src/main/java/org/springframework/vault/config/EnvironmentVaultConfiguration.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.vault.authentication.AppRoleAuthenticationOptions.RoleId;
3939
import org.springframework.vault.authentication.AppRoleAuthenticationOptions.SecretId;
4040
import org.springframework.vault.authentication.AwsEc2AuthenticationOptions.AwsEc2AuthenticationOptionsBuilder;
41+
import org.springframework.vault.authentication.AwsIamAuthenticationOptions.AwsIamAuthenticationOptionsBuilder;
4142
import org.springframework.vault.authentication.AzureMsiAuthenticationOptions.AzureMsiAuthenticationOptionsBuilder;
4243
import org.springframework.vault.authentication.CubbyholeAuthenticationOptions.CubbyholeAuthenticationOptionsBuilder;
4344
import org.springframework.vault.authentication.KubernetesAuthenticationOptions.KubernetesAuthenticationOptionsBuilder;
@@ -46,6 +47,7 @@
4647
import org.springframework.vault.support.SslConfiguration.KeyStoreConfiguration;
4748
import org.springframework.vault.support.VaultToken;
4849
import org.springframework.web.client.RestOperations;
50+
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
4951

5052
/**
5153
* Configuration using Spring's {@link org.springframework.core.env.Environment} to
@@ -162,12 +164,14 @@
162164
* @author Raoof Mohammed
163165
* @author Justin Bertrand
164166
* @author Ryan Gow
167+
* @author Nick Tan
165168
* @see org.springframework.core.env.Environment
166169
* @see org.springframework.core.env.PropertySource
167170
* @see VaultEndpoint
168171
* @see AppIdAuthentication
169172
* @see AppRoleAuthentication
170173
* @see AwsEc2Authentication
174+
* @see AwsIamAuthentication
171175
* @see AzureMsiAuthentication
172176
* @see ClientCertificateAuthentication
173177
* @see CubbyholeAuthentication
@@ -264,6 +268,8 @@ public ClientAuthentication clientAuthentication() {
264268
return appRoleAuthentication();
265269
case AWS_EC2:
266270
return awsEc2Authentication();
271+
case AWS_IAM:
272+
return awsIamAuthentication();
267273
case AZURE:
268274
return azureMsiAuthentication();
269275
case CERT:
@@ -369,6 +375,17 @@ protected ClientAuthentication awsEc2Authentication() {
369375
return new AwsEc2Authentication(builder.build(), restOperations(), restOperations());
370376
}
371377

378+
protected ClientAuthentication awsIamAuthentication() {
379+
String role = getProperty("vault.aws-iam.role");
380+
Assert.isTrue(StringUtils.hasText(role),
381+
"Vault AWS-IAM authentication: Role (vault.aws-iam.role) must not be empty");
382+
383+
AwsIamAuthenticationOptionsBuilder builder = AwsIamAuthenticationOptions.builder().role(role)
384+
.credentialsProvider(DefaultCredentialsProvider.create());
385+
386+
return new AwsIamAuthentication(builder.build(), restOperations());
387+
}
388+
372389
protected ClientAuthentication azureMsiAuthentication() {
373390

374391
String role = getProperty("vault.azure-msi.role");
@@ -454,7 +471,7 @@ enum AppIdUserId {
454471

455472
enum AuthenticationMethod {
456473

457-
TOKEN, APPID, APPROLE, AWS_EC2, AZURE, CERT, CUBBYHOLE, KUBERNETES;
474+
TOKEN, APPID, APPROLE, AWS_EC2, AWS_IAM, AZURE, CERT, CUBBYHOLE, KUBERNETES;
458475

459476
}
460477

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2017-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.vault.config;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.ExtendWith;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.context.annotation.Import;
25+
import org.springframework.test.context.TestPropertySource;
26+
import org.springframework.test.context.junit.jupiter.SpringExtension;
27+
import org.springframework.vault.authentication.AwsEc2Authentication;
28+
import org.springframework.vault.authentication.AwsIamAuthentication;
29+
import org.springframework.vault.authentication.ClientAuthentication;
30+
31+
/**
32+
* Unit tests for {@link EnvironmentVaultConfiguration} with AppRole authentication.
33+
*
34+
* @author Nick Tan
35+
*/
36+
@ExtendWith(SpringExtension.class)
37+
@TestPropertySource(
38+
properties = { "vault.uri=https://localhost:8123", "vault.authentication=aws-iam", "vault.aws-iam.role=role" })
39+
class EnvironmentVaultConfigurationAwsIamAuthenticationUnitTests {
40+
41+
@Configuration
42+
@Import(EnvironmentVaultConfiguration.class)
43+
static class ApplicationConfiguration {
44+
45+
}
46+
47+
@Autowired
48+
EnvironmentVaultConfiguration configuration;
49+
50+
@Test
51+
void shouldConfigureAuthentication() {
52+
53+
ClientAuthentication clientAuthentication = this.configuration.clientAuthentication();
54+
55+
assertThat(clientAuthentication).isInstanceOf(AwsIamAuthentication.class);
56+
}
57+
58+
}

0 commit comments

Comments
 (0)