1212import java .util .Map ;
1313
1414public class ProfileCredentialsProvider implements AlibabaCloudCredentialsProvider {
15- private static volatile Wini ini ;
15+ private final String filePath ;
16+ private volatile Wini ini ;
17+ private AlibabaCloudCredentialsProvider innerProvider ;
1618
17- private static Wini getIni (String filePath ) throws IOException {
19+ private Wini getIni (String filePath ) throws IOException {
1820 if (null == ini ) {
19- synchronized (ProfileCredentialsProvider . class ) {
21+ synchronized (this ) {
2022 if (null == ini ) {
2123 ini = new Wini (new File (filePath ));
2224 }
@@ -25,31 +27,61 @@ private static Wini getIni(String filePath) throws IOException {
2527 return ini ;
2628 }
2729
28- @ Override
29- public AlibabaCloudCredentials getCredentials () throws ClientException {
30- String filePath = AuthUtils .getEnvironmentCredentialsFile ();
31- if (filePath == null ) {
30+ // 本包可见
31+ ProfileCredentialsProvider (String filePath ) {
32+ if (StringUtils .isEmpty (filePath )) {
3233 filePath = AuthConstant .DEFAULT_CREDENTIALS_FILE_PATH ;
3334 }
34- if (filePath .isEmpty ()) {
35- throw new ClientException ("The specified credentials file is empty" );
36- }
37- Wini ini ;
38- try {
39- ini = getIni (filePath );
40- } catch (IOException e ) {
41- return null ;
42- }
43- Map <String , Map <String , String >> client = loadIni (ini );
44- Map <String , String > clientConfig = client .get (AuthUtils .getClientType ());
35+ this .filePath = filePath ;
36+ }
37+
38+ public ProfileCredentialsProvider () {
39+ this (AuthUtils .getEnvironmentCredentialsFile ());
40+ }
41+
42+ private AlibabaCloudCredentialsProvider getCredentialsProvider (Map <String , String > clientConfig ) throws ClientException {
4543 if (clientConfig == null ) {
4644 throw new ClientException ("Client is not open in the specified credentials file" );
4745 }
48- CredentialsProviderFactory credentialsProviderFactory = new CredentialsProviderFactory ();
49- return createCredential (clientConfig , credentialsProviderFactory );
46+
47+ String configType = clientConfig .get (AuthConstant .INI_TYPE );
48+ if (StringUtils .isEmpty (configType )) {
49+ throw new ClientException ("The configured client type is empty" );
50+ }
51+ if (AuthConstant .INI_TYPE_ARN .equals (configType )) {
52+ return getSTSAssumeRoleSessionCredentialsProvider (clientConfig );
53+ }
54+ if (AuthConstant .INI_TYPE_KEY_PAIR .equals (configType )) {
55+ return getSTSGetSessionAccessKeyCredentialsProvider (clientConfig );
56+ }
57+ if (AuthConstant .INI_TYPE_RAM .equals (configType )) {
58+ return getInstanceProfileCredentialsProvider (clientConfig );
59+ }
60+ if (AuthConstant .INI_TYPE_ACESS_KEY .equals (configType )) {
61+ return getStaticCredentialsProvider (clientConfig );
62+ }
63+
64+ throw new ClientException (String .format ("The configured client type %s is not supported" , configType ));
65+ }
66+
67+ @ Override
68+ public AlibabaCloudCredentials getCredentials () throws ClientException {
69+ // lazy load it
70+ if (this .innerProvider == null ) {
71+ Wini ini ;
72+ try {
73+ ini = getIni (filePath );
74+ } catch (IOException e ) {
75+ throw new ClientException ("Client is not open in the specified credentials file" );
76+ }
77+ Map <String , Map <String , String >> client = loadIni (ini );
78+ Map <String , String > clientConfig = client .get (AuthUtils .getClientType ());
79+ this .innerProvider = getCredentialsProvider (clientConfig );
80+ }
81+ return this .innerProvider .getCredentials ();
5082 }
5183
52- private Map <String , Map <String , String >> loadIni (Wini ini ) {
84+ private static Map <String , Map <String , String >> loadIni (Wini ini ) {
5385 Map <String , Map <String , String >> client = new HashMap <String , Map <String , String >>();
5486 boolean enable ;
5587 for (Map .Entry <String , Profile .Section > clientType : ini .entrySet ()) {
@@ -65,78 +97,67 @@ private Map<String, Map<String, String>> loadIni(Wini ini) {
6597 return client ;
6698 }
6799
68- private AlibabaCloudCredentials createCredential (Map <String , String > clientConfig ,
69- CredentialsProviderFactory factory ) throws ClientException {
70- String configType = clientConfig .get (AuthConstant .INI_TYPE );
71- if (StringUtils .isEmpty (configType )) {
72- throw new ClientException ("The configured client type is empty" );
73- }
74- if (AuthConstant .INI_TYPE_ARN .equals (configType )) {
75- return getSTSAssumeRoleSessionCredentials (clientConfig , factory );
76- }
77- if (AuthConstant .INI_TYPE_KEY_PAIR .equals (configType )) {
78- return getSTSGetSessionAccessKeyCredentials (clientConfig , factory );
79- }
80- if (AuthConstant .INI_TYPE_RAM .equals (configType )) {
81- return getInstanceProfileCredentials (clientConfig , factory );
82- }
83- String accessKeyId = clientConfig .get (AuthConstant .INI_ACCESS_KEY_ID );
84- String accessKeySecret = clientConfig .get (AuthConstant .INI_ACCESS_KEY_IDSECRET );
85- if (StringUtils .isEmpty (accessKeyId ) || StringUtils .isEmpty (accessKeySecret )) {
86- return null ;
87- }
88- return new BasicCredentials (accessKeyId , accessKeySecret );
89- }
90-
91- private AlibabaCloudCredentials getSTSAssumeRoleSessionCredentials (Map <String , String > clientConfig ,
92- CredentialsProviderFactory factory )
100+ private static AlibabaCloudCredentialsProvider getSTSAssumeRoleSessionCredentialsProvider (Map <String , String > clientConfig )
93101 throws ClientException {
94102 String accessKeyId = clientConfig .get (AuthConstant .INI_ACCESS_KEY_ID );
103+ if (StringUtils .isEmpty (accessKeyId )) {
104+ throw new ClientException ("The configured access_key_id is empty" );
105+ }
95106 String accessKeySecret = clientConfig .get (AuthConstant .INI_ACCESS_KEY_IDSECRET );
107+ if (StringUtils .isEmpty (accessKeySecret )) {
108+ throw new ClientException ("The configured access_key_secret is empty" );
109+ }
96110 String roleSessionName = clientConfig .get (AuthConstant .INI_ROLE_SESSION_NAME );
111+ if (StringUtils .isEmpty (roleSessionName )) {
112+ throw new ClientException ("The configured role_session_name is empty" );
113+ }
97114 String roleArn = clientConfig .get (AuthConstant .INI_ROLE_ARN );
115+ if (StringUtils .isEmpty (roleArn )) {
116+ throw new ClientException ("The configured role_arn is empty" );
117+ }
98118 String regionId = clientConfig .get (AuthConstant .DEFAULT_REGION );
99119 String policy = clientConfig .get (AuthConstant .INI_POLICY );
100- if (StringUtils .isEmpty (accessKeyId ) || StringUtils .isEmpty (accessKeySecret )) {
101- throw new ClientException ("The configured access_key_id or access_key_secret is empty" );
102- }
103- if (StringUtils .isEmpty (roleSessionName ) || StringUtils .isEmpty (roleArn )) {
104- throw new ClientException ("The configured role_session_name or role_arn is empty" );
105- }
106- STSAssumeRoleSessionCredentialsProvider provider =
107- factory .createCredentialsProvider (new STSAssumeRoleSessionCredentialsProvider (accessKeyId ,
108- accessKeySecret , roleSessionName , roleArn , regionId , policy ));
109- return provider .getCredentials ();
120+
121+ return new STSAssumeRoleSessionCredentialsProvider (accessKeyId , accessKeySecret , roleSessionName , roleArn , regionId , policy );
110122 }
111123
112- private AlibabaCloudCredentials getSTSGetSessionAccessKeyCredentials (Map <String , String > clientConfig ,
113- CredentialsProviderFactory factory )
124+ private static AlibabaCloudCredentialsProvider getSTSGetSessionAccessKeyCredentialsProvider (Map <String , String > clientConfig )
114125 throws ClientException {
115126 String publicKeyId = clientConfig .get (AuthConstant .INI_PUBLIC_KEY_ID );
127+ if (StringUtils .isEmpty (publicKeyId )) {
128+ throw new ClientException ("The configured public_key_id is empty" );
129+ }
116130 String privateKeyFile = clientConfig .get (AuthConstant .INI_PRIVATE_KEY_FILE );
117131 if (StringUtils .isEmpty (privateKeyFile )) {
118132 throw new ClientException ("The configured private_key_file is empty" );
119133 }
120134 String privateKey = AuthUtils .readFile (privateKeyFile );
121- if (StringUtils .isEmpty (publicKeyId ) || StringUtils . isEmpty ( privateKey )) {
122- throw new ClientException ("The configured public_key_id or private_key_file content is empty" );
135+ if (StringUtils .isEmpty (privateKey )) {
136+ throw new ClientException ("The configured private_key_file content is empty" );
123137 }
124- STSGetSessionAccessKeyCredentialsProvider provider =
125- factory .createCredentialsProvider (new STSGetSessionAccessKeyCredentialsProvider (publicKeyId , privateKey ));
126- return provider .getCredentials ();
138+
139+ return new STSGetSessionAccessKeyCredentialsProvider (publicKeyId , privateKey );
127140 }
128141
129- private AlibabaCloudCredentials getInstanceProfileCredentials (Map <String , String > clientConfig ,
130- CredentialsProviderFactory factory )
131- throws ClientException {
142+ private static AlibabaCloudCredentialsProvider getInstanceProfileCredentialsProvider (Map <String , String > clientConfig ) throws ClientException {
132143 String roleName = clientConfig .get (AuthConstant .INI_ROLE_NAME );
133144 if (StringUtils .isEmpty (roleName )) {
134145 throw new ClientException ("The configured role_name is empty" );
135146 }
136- InstanceProfileCredentialsProvider provider =
137- factory .createCredentialsProvider (new InstanceProfileCredentialsProvider (roleName ));
138- return provider .getCredentials ();
147+
148+ return new InstanceProfileCredentialsProvider (roleName );
139149 }
140150
151+ private static AlibabaCloudCredentialsProvider getStaticCredentialsProvider (Map <String , String > clientConfig ) throws ClientException {
152+ String accessKeyId = clientConfig .get (AuthConstant .INI_ACCESS_KEY_ID );
153+ if (StringUtils .isEmpty (accessKeyId )) {
154+ throw new ClientException ("The configured access_key_id is empty" );
155+ }
156+ String accessKeySecret = clientConfig .get (AuthConstant .INI_ACCESS_KEY_IDSECRET );
157+ if (StringUtils .isEmpty (accessKeySecret )) {
158+ throw new ClientException ("The configured access_key_secret is empty" );
159+ }
141160
161+ return new StaticCredentialsProvider (new BasicCredentials (accessKeyId , accessKeySecret ));
162+ }
142163}
0 commit comments