Skip to content

Commit f0ba84a

Browse files
committed
Update bedrock configuration test case and document
Signed-off-by: Baojun Jiang <[email protected]>
1 parent 3b49357 commit f0ba84a

File tree

6 files changed

+97
-10
lines changed

6 files changed

+97
-10
lines changed

auto-configurations/models/spring-ai-autoconfigure-model-bedrock-ai/src/main/java/org/springframework/ai/model/bedrock/autoconfigure/BedrockAwsConnectionConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ else if (properties.getProfile() != null && StringUtils.hasText(properties.getPr
8787
}
8888
return providerBuilder.profileName(profile.getName()).build();
8989
}
90-
// IAM Role
91-
return DefaultCredentialsProvider.builder().build();
90+
else {
91+
// Default: IAM Role, System Environment, etc.
92+
return DefaultCredentialsProvider.builder().build();
93+
}
9294
}
9395

9496
@Bean

auto-configurations/models/spring-ai-autoconfigure-model-bedrock-ai/src/test/java/org/springframework/ai/model/bedrock/autoconfigure/BedrockAwsConnectionConfigurationIT.java

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@
1616

1717
package org.springframework.ai.model.bedrock.autoconfigure;
1818

19+
import java.lang.reflect.Field;
20+
import java.nio.file.Files;
21+
import java.nio.file.Paths;
22+
1923
import org.junit.jupiter.api.Test;
2024
import software.amazon.awssdk.auth.credentials.AwsCredentials;
2125
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
26+
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
27+
import software.amazon.awssdk.profiles.ProfileFile;
2228
import software.amazon.awssdk.regions.Region;
2329
import software.amazon.awssdk.regions.providers.AwsRegionProvider;
2430

@@ -63,7 +69,8 @@ public void autoConfigureAWSCredentialAndRegionProvider() {
6369
public void autoConfigureWithCustomAWSCredentialAndRegionProvider() {
6470
BedrockTestUtils.getContextRunner()
6571
.withConfiguration(AutoConfigurations.of(TestAutoConfiguration.class,
66-
CustomAwsCredentialsProviderAndAwsRegionProviderAutoConfiguration.class))
72+
CustomAwsCredentialsProviderAutoConfiguration.class,
73+
CustomAwsRegionProviderAutoConfiguration.class))
6774
.run(context -> {
6875
var awsCredentialsProvider = context.getBean(AwsCredentialsProvider.class);
6976
var awsRegionProvider = context.getBean(AwsRegionProvider.class);
@@ -80,14 +87,73 @@ public void autoConfigureWithCustomAWSCredentialAndRegionProvider() {
8087
});
8188
}
8289

90+
@Test
91+
public void autoConfigureWithCustomAWSProfileCredentialAndRegionProvider() {
92+
BedrockTestUtils.getContextRunner()
93+
.withConfiguration(AutoConfigurations.of(TestAutoConfiguration.class,
94+
CustomAwsProfileCredentialsProviderAutoConfiguration.class,
95+
CustomAwsRegionProviderAutoConfiguration.class))
96+
.run(context -> {
97+
var awsCredentialsProvider = context.getBean(AwsCredentialsProvider.class);
98+
var awsRegionProvider = context.getBean(AwsRegionProvider.class);
99+
100+
assertThat(awsCredentialsProvider).isNotNull();
101+
assertThat(awsRegionProvider).isNotNull();
102+
103+
assertThat(awsCredentialsProvider).isInstanceOf(ProfileCredentialsProvider.class);
104+
// aws sdk2.x does not provide method to get profileName, use reflection
105+
// to get
106+
Field field = ProfileCredentialsProvider.class.getDeclaredField("profileName");
107+
field.setAccessible(true);
108+
assertThat(field.get(awsCredentialsProvider)).isEqualTo("CUSTOM_PROFILE_NAME");
109+
110+
assertThat(awsRegionProvider.getRegion()).isEqualTo(Region.AWS_GLOBAL);
111+
});
112+
}
113+
83114
@EnableConfigurationProperties(BedrockAwsConnectionProperties.class)
84115
@Import(BedrockAwsConnectionConfiguration.class)
85116
static class TestAutoConfiguration {
86117

87118
}
88119

89120
@AutoConfiguration
90-
static class CustomAwsCredentialsProviderAndAwsRegionProviderAutoConfiguration {
121+
static class CustomAwsProfileCredentialsProviderAutoConfiguration {
122+
123+
@Bean
124+
@ConditionalOnMissingBean
125+
public AwsCredentialsProvider credentialsProvider() {
126+
String credentialsPath = "CUSTOM_CREDENTIALS_PATH";
127+
String configurationPath = "CUSTOM_CONFIGURATION_PATH";
128+
boolean hasCredentials = Files.exists(Paths.get(credentialsPath));
129+
boolean hasConfig = Files.exists(Paths.get(configurationPath));
130+
ProfileCredentialsProvider.Builder providerBuilder = ProfileCredentialsProvider.builder();
131+
if (hasCredentials || hasConfig) {
132+
ProfileFile.Aggregator aggregator = ProfileFile.aggregator();
133+
if (hasCredentials) {
134+
ProfileFile profileFile = ProfileFile.builder()
135+
.content(Paths.get(credentialsPath))
136+
.type(ProfileFile.Type.CREDENTIALS)
137+
.build();
138+
aggregator.addFile(profileFile);
139+
}
140+
if (hasConfig) {
141+
ProfileFile configFile = ProfileFile.builder()
142+
.content(Paths.get(configurationPath))
143+
.type(ProfileFile.Type.CONFIGURATION)
144+
.build();
145+
aggregator.addFile(configFile);
146+
}
147+
ProfileFile aggregatedProfileFile = aggregator.build();
148+
providerBuilder.profileFile(aggregatedProfileFile);
149+
}
150+
return providerBuilder.profileName("CUSTOM_PROFILE_NAME").build();
151+
}
152+
153+
}
154+
155+
@AutoConfiguration
156+
static class CustomAwsCredentialsProviderAutoConfiguration {
91157

92158
@Bean
93159
@ConditionalOnMissingBean
@@ -114,6 +180,11 @@ public String secretAccessKey() {
114180
};
115181
}
116182

183+
}
184+
185+
@AutoConfiguration
186+
static class CustomAwsRegionProviderAutoConfiguration {
187+
117188
@Bean
118189
@ConditionalOnMissingBean
119190
public AwsRegionProvider regionProvider() {

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/bedrock.adoc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ spring.ai.bedrock.aws.region=us-east-1
6464
spring.ai.bedrock.aws.access-key=YOUR_ACCESS_KEY
6565
spring.ai.bedrock.aws.secret-key=YOUR_SECRET_KEY
6666
67+
spring.ai.bedrock.aws.profile.name=YOUR_PROFILE_NAME
68+
spring.ai.bedrock.aws.profile.credentials-path=YOUR_CREDENTIALS_PATH
69+
spring.ai.bedrock.aws.profile.configuration-path=YOUR_CONFIGURATION_PATH
70+
6771
spring.ai.bedrock.aws.timeout=10m
6872
----
6973

@@ -72,12 +76,13 @@ The `region` property is compulsory.
7276
AWS credentials are resolved in the following order:
7377

7478
1. Spring-AI Bedrock `spring.ai.bedrock.aws.access-key` and `spring.ai.bedrock.aws.secret-key` properties.
75-
2. Java System Properties - `aws.accessKeyId` and `aws.secretAccessKey`.
76-
3. Environment Variables - `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.
77-
4. Web Identity Token credentials from system properties or environment variables.
78-
5. Credential profiles file at the default location (`~/.aws/credentials`) shared by all AWS SDKs and the AWS CLI.
79-
6. Credentials delivered through the Amazon EC2 container service if the `AWS_CONTAINER_CREDENTIALS_RELATIVE_URI` environment variable is set and the security manager has permission to access the variable.
80-
7. Instance profile credentials delivered through the Amazon EC2 metadata service or set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables.
79+
2. Spring-AI Bedrock `spring.ai.bedrock.aws.profile.name`
80+
3. Java System Properties - `aws.accessKeyId` and `aws.secretAccessKey`.
81+
4. Environment Variables - `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.
82+
5. Web Identity Token credentials from system properties or environment variables.
83+
6. If `spring.ai.bedrock.aws.profile.credentials-path` and `spring.ai.bedrock.aws.profile.configuration-path` are not specified, Spring AI use the standard AWS shared files: `~/.aws/credentials` for credentials and `~/.aws/config` for configuration, which are used by all AWS SDKs and the AWS CLI.
84+
7. Credentials delivered through the Amazon EC2 container service if the `AWS_CONTAINER_CREDENTIALS_RELATIVE_URI` environment variable is set and the security manager has permission to access the variable.
85+
8. Instance profile credentials delivered through the Amazon EC2 metadata service or set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables.
8186

8287
AWS region is resolved in the following order:
8388

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/bedrock-converse.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ The prefix `spring.ai.bedrock.aws` is the property prefix to configure the conne
8080
| spring.ai.bedrock.aws.asyncReadTimeout | Max duration spent reading asynchronous responses | 30s
8181
| spring.ai.bedrock.aws.access-key | AWS access key | -
8282
| spring.ai.bedrock.aws.secret-key | AWS secret key | -
83+
| spring.ai.bedrock.aws.profile.name | AWS profile name. | -
84+
| spring.ai.bedrock.aws.profile.credentials-path | AWS credentials file path. | -
85+
| spring.ai.bedrock.aws.profile.configuration-path | AWS config file path. | -
8386
| spring.ai.bedrock.aws.session-token | AWS session token for temporary credentials | -
8487
|====
8588

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/bedrock-cohere-embedding.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ The prefix `spring.ai.bedrock.aws` is the property prefix to configure the conne
9090
| spring.ai.bedrock.aws.region | AWS region to use. | us-east-1
9191
| spring.ai.bedrock.aws.access-key | AWS access key. | -
9292
| spring.ai.bedrock.aws.secret-key | AWS secret key. | -
93+
| spring.ai.bedrock.aws.profile.name | AWS profile name. | -
94+
| spring.ai.bedrock.aws.profile.credentials-path | AWS credentials file path. | -
95+
| spring.ai.bedrock.aws.profile.configuration-path | AWS config file path. | -
9396
|====
9497

9598
[NOTE]

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/bedrock-titan-embedding.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ The prefix `spring.ai.bedrock.aws` is the property prefix to configure the conne
9797
| spring.ai.bedrock.aws.region | AWS region to use. | us-east-1
9898
| spring.ai.bedrock.aws.access-key | AWS access key. | -
9999
| spring.ai.bedrock.aws.secret-key | AWS secret key. | -
100+
| spring.ai.bedrock.aws.profile.name | AWS profile name. | -
101+
| spring.ai.bedrock.aws.profile.credentials-path | AWS credentials file path. | -
102+
| spring.ai.bedrock.aws.profile.configuration-path | AWS config file path. | -
100103
|====
101104

102105
[NOTE]

0 commit comments

Comments
 (0)