Skip to content

Commit 498d4a9

Browse files
committed
Add bedrock profile autoconfiguration support and replace deprecated
Signed-off-by: Baojun Jiang <[email protected]>
1 parent 673f483 commit 498d4a9

File tree

3 files changed

+120
-6
lines changed

3 files changed

+120
-6
lines changed

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

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616

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

19+
import java.nio.file.Paths;
20+
1921
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
2022
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
2123
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
2224
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
25+
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
2326
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
27+
import software.amazon.awssdk.profiles.ProfileFile;
2428
import software.amazon.awssdk.regions.Region;
2529
import software.amazon.awssdk.regions.providers.AwsRegionProvider;
2630
import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain;
@@ -36,6 +40,7 @@
3640
*
3741
* @author Christian Tzolov
3842
* @author Wei Jiang
43+
* @author Baojun Jiang
3944
*/
4045
@Configuration
4146
@EnableConfigurationProperties(BedrockAwsConnectionProperties.class)
@@ -44,19 +49,46 @@ public class BedrockAwsConnectionConfiguration {
4449
@Bean
4550
@ConditionalOnMissingBean
4651
public AwsCredentialsProvider credentialsProvider(BedrockAwsConnectionProperties properties) {
47-
4852
if (StringUtils.hasText(properties.getAccessKey()) && StringUtils.hasText(properties.getSecretKey())) {
49-
53+
// Security key
5054
if (StringUtils.hasText(properties.getSessionToken())) {
5155
return StaticCredentialsProvider.create(AwsSessionCredentials.create(properties.getAccessKey(),
5256
properties.getSecretKey(), properties.getSessionToken()));
5357
}
54-
5558
return StaticCredentialsProvider
5659
.create(AwsBasicCredentials.create(properties.getAccessKey(), properties.getSecretKey()));
5760
}
58-
59-
return DefaultCredentialsProvider.create();
61+
else if (properties.getProfile() != null && StringUtils.hasText(properties.getProfile().getName())) {
62+
// Profile
63+
ProfileProperties profile = properties.getProfile();
64+
String configurationPath = profile.getConfigurationPath();
65+
String credentialsPath = profile.getCredentialsPath();
66+
boolean hasCredentials = StringUtils.hasText(credentialsPath);
67+
boolean hasConfig = StringUtils.hasText(configurationPath);
68+
ProfileCredentialsProvider.Builder providerBuilder = ProfileCredentialsProvider.builder();
69+
if (hasCredentials || hasConfig) {
70+
ProfileFile.Aggregator aggregator = ProfileFile.aggregator();
71+
if (hasCredentials) {
72+
ProfileFile profileFile = ProfileFile.builder()
73+
.content(Paths.get(credentialsPath))
74+
.type(ProfileFile.Type.CREDENTIALS)
75+
.build();
76+
aggregator.addFile(profileFile);
77+
}
78+
if (hasConfig) {
79+
ProfileFile configFile = ProfileFile.builder()
80+
.content(Paths.get(configurationPath))
81+
.type(ProfileFile.Type.CONFIGURATION)
82+
.build();
83+
aggregator.addFile(configFile);
84+
}
85+
ProfileFile aggregatedProfileFile = aggregator.build();
86+
providerBuilder.profileFile(aggregatedProfileFile);
87+
}
88+
return providerBuilder.profileName(profile.getName()).build();
89+
}
90+
// IAM Role
91+
return DefaultCredentialsProvider.builder().build();
6092
}
6193

6294
@Bean

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
import java.time.Duration;
2020

2121
import org.springframework.boot.context.properties.ConfigurationProperties;
22+
import org.springframework.boot.context.properties.NestedConfigurationProperty;
2223

2324
/**
2425
* Configuration properties for Bedrock AWS connection.
2526
*
2627
* @author Christian Tzolov
28+
* @author Baojun Jiang
2729
* @since 0.8.0
2830
*/
2931
@ConfigurationProperties(BedrockAwsConnectionProperties.CONFIG_PREFIX)
@@ -48,10 +50,17 @@ public class BedrockAwsConnectionProperties {
4850

4951
/**
5052
* AWS session token. (optional) When provided the AwsSessionCredentials are used.
51-
* Otherwise the AwsBasicCredentials are used.
53+
* Otherwise, the AwsBasicCredentials are used.
5254
*/
5355
private String sessionToken;
5456

57+
/**
58+
* Aws profile. (optional) When the {@link #accessKey} and {@link #secretKey} are not
59+
* declared. Otherwise, the AwsBasicCredentials are used.
60+
*/
61+
@NestedConfigurationProperty
62+
private ProfileProperties profile;
63+
5564
/**
5665
* Maximum duration of the entire API call operation.
5766
*/
@@ -149,4 +158,12 @@ public void setSessionToken(String sessionToken) {
149158
this.sessionToken = sessionToken;
150159
}
151160

161+
public ProfileProperties getProfile() {
162+
return this.profile;
163+
}
164+
165+
public void setProfile(ProfileProperties profile) {
166+
this.profile = profile;
167+
}
168+
152169
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2023-2024 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+
17+
package org.springframework.ai.model.bedrock.autoconfigure;
18+
19+
/**
20+
* Configuration properties for Bedrock AWS connection using profile.
21+
*
22+
* @author Baojun Jiang
23+
*/
24+
public class ProfileProperties {
25+
26+
/**
27+
* Name of the profile to use.
28+
*/
29+
private String name;
30+
31+
/**
32+
* (optional) Path to the credentials file. default: ~/.aws/credentials
33+
*/
34+
private String credentialsPath;
35+
36+
/**
37+
* (optional) Path to the configuration file. default: ~/.aws/config
38+
*/
39+
private String configurationPath;
40+
41+
public String getName() {
42+
return this.name;
43+
}
44+
45+
public void setName(String name) {
46+
this.name = name;
47+
}
48+
49+
public String getCredentialsPath() {
50+
return this.credentialsPath;
51+
}
52+
53+
public void setCredentialsPath(String credentialsPath) {
54+
this.credentialsPath = credentialsPath;
55+
}
56+
57+
public String getConfigurationPath() {
58+
return this.configurationPath;
59+
}
60+
61+
public void setConfigurationPath(String configurationPath) {
62+
this.configurationPath = configurationPath;
63+
}
64+
65+
}

0 commit comments

Comments
 (0)