12
12
import java .util .Map ;
13
13
14
14
public class ProfileCredentialsProvider implements AlibabaCloudCredentialsProvider {
15
- private static volatile Wini ini ;
15
+ private final String filePath ;
16
+ private volatile Wini ini ;
17
+ private AlibabaCloudCredentialsProvider innerProvider ;
16
18
17
- private static Wini getIni (String filePath ) throws IOException {
19
+ private Wini getIni (String filePath ) throws IOException {
18
20
if (null == ini ) {
19
- synchronized (ProfileCredentialsProvider . class ) {
21
+ synchronized (this ) {
20
22
if (null == ini ) {
21
23
ini = new Wini (new File (filePath ));
22
24
}
@@ -25,31 +27,61 @@ private static Wini getIni(String filePath) throws IOException {
25
27
return ini ;
26
28
}
27
29
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 )) {
32
33
filePath = AuthConstant .DEFAULT_CREDENTIALS_FILE_PATH ;
33
34
}
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 {
45
43
if (clientConfig == null ) {
46
44
throw new ClientException ("Client is not open in the specified credentials file" );
47
45
}
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 ();
50
82
}
51
83
52
- private Map <String , Map <String , String >> loadIni (Wini ini ) {
84
+ private static Map <String , Map <String , String >> loadIni (Wini ini ) {
53
85
Map <String , Map <String , String >> client = new HashMap <String , Map <String , String >>();
54
86
boolean enable ;
55
87
for (Map .Entry <String , Profile .Section > clientType : ini .entrySet ()) {
@@ -65,78 +97,67 @@ private Map<String, Map<String, String>> loadIni(Wini ini) {
65
97
return client ;
66
98
}
67
99
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 )
93
101
throws ClientException {
94
102
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
+ }
95
106
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
+ }
96
110
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
+ }
97
114
String roleArn = clientConfig .get (AuthConstant .INI_ROLE_ARN );
115
+ if (StringUtils .isEmpty (roleArn )) {
116
+ throw new ClientException ("The configured role_arn is empty" );
117
+ }
98
118
String regionId = clientConfig .get (AuthConstant .DEFAULT_REGION );
99
119
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 );
110
122
}
111
123
112
- private AlibabaCloudCredentials getSTSGetSessionAccessKeyCredentials (Map <String , String > clientConfig ,
113
- CredentialsProviderFactory factory )
124
+ private static AlibabaCloudCredentialsProvider getSTSGetSessionAccessKeyCredentialsProvider (Map <String , String > clientConfig )
114
125
throws ClientException {
115
126
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
+ }
116
130
String privateKeyFile = clientConfig .get (AuthConstant .INI_PRIVATE_KEY_FILE );
117
131
if (StringUtils .isEmpty (privateKeyFile )) {
118
132
throw new ClientException ("The configured private_key_file is empty" );
119
133
}
120
134
String privateKey = AuthUtils .getPrivateKey (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" );
123
137
}
124
- STSGetSessionAccessKeyCredentialsProvider provider =
125
- factory .createCredentialsProvider (new STSGetSessionAccessKeyCredentialsProvider (publicKeyId , privateKey ));
126
- return provider .getCredentials ();
138
+
139
+ return new STSGetSessionAccessKeyCredentialsProvider (publicKeyId , privateKey );
127
140
}
128
141
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 {
132
143
String roleName = clientConfig .get (AuthConstant .INI_ROLE_NAME );
133
144
if (StringUtils .isEmpty (roleName )) {
134
145
throw new ClientException ("The configured role_name is empty" );
135
146
}
136
- InstanceProfileCredentialsProvider provider =
137
- factory .createCredentialsProvider (new InstanceProfileCredentialsProvider (roleName ));
138
- return provider .getCredentials ();
147
+
148
+ return new InstanceProfileCredentialsProvider (roleName );
139
149
}
140
150
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
+ }
141
160
161
+ return new StaticCredentialsProvider (new BasicCredentials (accessKeyId , accessKeySecret ));
162
+ }
142
163
}
0 commit comments