1616
1717package org .springframework .ai .model .bedrock .autoconfigure ;
1818
19+ import java .nio .file .Paths ;
20+
1921import software .amazon .awssdk .auth .credentials .AwsBasicCredentials ;
2022import software .amazon .awssdk .auth .credentials .AwsCredentialsProvider ;
2123import software .amazon .awssdk .auth .credentials .AwsSessionCredentials ;
2224import software .amazon .awssdk .auth .credentials .DefaultCredentialsProvider ;
25+ import software .amazon .awssdk .auth .credentials .ProfileCredentialsProvider ;
2326import software .amazon .awssdk .auth .credentials .StaticCredentialsProvider ;
27+ import software .amazon .awssdk .profiles .ProfileFile ;
2428import software .amazon .awssdk .regions .Region ;
2529import software .amazon .awssdk .regions .providers .AwsRegionProvider ;
2630import software .amazon .awssdk .regions .providers .DefaultAwsRegionProviderChain ;
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
0 commit comments