Skip to content

Commit 855ceb3

Browse files
committed
Add CredentialsProvider
1 parent d0126d5 commit 855ceb3

File tree

63 files changed

+6931
-63
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+6931
-63
lines changed

pom.xml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
<description>The Aliyun OSS SDK for Java used for accessing Aliyun Object Storage Service</description>
1818
<url>http://www.aliyun.com/product/oss</url>
1919

20-
2120
<dependencies>
2221
<dependency>
2322
<groupId>org.apache.httpcomponents</groupId>
@@ -33,13 +32,26 @@
3332
<groupId>com.sun.jersey</groupId>
3433
<artifactId>jersey-json</artifactId>
3534
<version>1.9</version>
36-
<scope>test</scope>
37-
<exclusions>
38-
<exclusion>
39-
<groupId>stax</groupId>
40-
<artifactId>stax-api</artifactId>
41-
</exclusion>
42-
</exclusions>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.aliyun</groupId>
38+
<artifactId>aliyun-java-sdk-core</artifactId>
39+
<version>3.4.0</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.aliyun</groupId>
43+
<artifactId>aliyun-java-sdk-ram</artifactId>
44+
<version>3.0.0</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.aliyun</groupId>
48+
<artifactId>aliyun-java-sdk-sts</artifactId>
49+
<version>3.0.0</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>com.aliyun</groupId>
53+
<artifactId>aliyun-java-sdk-ecs</artifactId>
54+
<version>4.2.0</version>
4355
</dependency>
4456
<dependency>
4557
<groupId>junit</groupId>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.aliyun.oss;
21+
22+
import com.aliyun.oss.common.auth.CredentialsProvider;
23+
24+
/**
25+
* Fluent builder for OSS Client. Use of the builder is preferred over using
26+
* constructors of the client class.
27+
*/
28+
public interface OSSBuilder {
29+
30+
/**
31+
* Uses the specified OSS Endpoint and Access Id/Access Key to create a new
32+
* {@link OSSClient} instance.
33+
*
34+
* @param endpoint
35+
* OSS endpoint.
36+
* @param accessKeyId
37+
* Access Key ID.
38+
* @param secretAccessKey
39+
* Secret Access Key.
40+
*/
41+
public OSS build(String endpoint, String accessKeyId, String secretAccessKey);
42+
43+
/**
44+
* Uses the specified OSS Endpoint, a security token from AliCloud STS and
45+
* Access Id/Access Key to create a new {@link OSSClient} instance.
46+
*
47+
* @param endpoint
48+
* OSS Endpoint.
49+
* @param accessKeyId
50+
* Access Id from STS.
51+
* @param secretAccessKey
52+
* Access Key from STS
53+
* @param securityToken
54+
* Security Token from STS.
55+
*/
56+
public OSS build(String endpoint, String accessKeyId, String secretAccessKey, String securityToken);
57+
58+
/**
59+
* Uses a specified OSS Endpoint, Access Id, Access Key, Client side
60+
* configuration to create a {@link OSSClient} instance.
61+
*
62+
* @param endpoint
63+
* OSS Endpoint.
64+
* @param accessKeyId
65+
* Access Key ID.
66+
* @param secretAccessKey
67+
* Secret Access Key.
68+
* @param config
69+
* A {@link ClientConfiguration} instance. The method would use
70+
* default configuration if it's null.
71+
*/
72+
public OSS build(String endpoint, String accessKeyId, String secretAccessKey, ClientConfiguration config);
73+
74+
/**
75+
* Uses specified OSS Endpoint, the temporary (Access Id/Access Key/Security
76+
* Token) from STS and the client configuration to create a new
77+
* {@link OSSClient} instance.
78+
*
79+
* @param endpoint
80+
* OSS Endpoint.
81+
* @param accessKeyId
82+
* Access Key Id provided by STS.
83+
* @param secretAccessKey
84+
* Secret Access Key provided by STS.
85+
* @param securityToken
86+
* Security token provided by STS.
87+
* @param config
88+
* A {@link ClientConfiguration} instance. The method would use
89+
* default configuration if it's null.
90+
*/
91+
public OSS build(String endpoint, String accessKeyId, String secretAccessKey, String securityToken,
92+
ClientConfiguration config);
93+
94+
/**
95+
* Uses the specified {@link CredentialsProvider} and OSS Endpoint to create
96+
* a new {@link OSSClient} instance.
97+
*
98+
* @param endpoint
99+
* OSS services Endpoint.
100+
* @param credsProvider
101+
* Credentials provider which has access key Id and access Key
102+
* secret.
103+
*/
104+
public OSS build(String endpoint, CredentialsProvider credsProvider);
105+
106+
/**
107+
* Uses the specified {@link CredentialsProvider}, client configuration and
108+
* OSS endpoint to create a new {@link OSSClient} instance.
109+
*
110+
* @param endpoint
111+
* OSS services Endpoint.
112+
* @param credsProvider
113+
* Credentials provider.
114+
* @param config
115+
* client configuration.
116+
*/
117+
public OSS build(String endpoint, CredentialsProvider credsProvider, ClientConfiguration config);
118+
119+
}

src/main/java/com/aliyun/oss/OSSClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public OSSClient(String accessKeyId, String secretAccessKey) {
126126
* @param secretAccessKey
127127
* Secret Access Key.
128128
*/
129+
@Deprecated
129130
public OSSClient(String endpoint, String accessKeyId, String secretAccessKey) {
130131
this(endpoint, new DefaultCredentialProvider(accessKeyId, secretAccessKey), null);
131132
}
@@ -143,6 +144,7 @@ public OSSClient(String endpoint, String accessKeyId, String secretAccessKey) {
143144
* @param securityToken
144145
* Security Token from STS.
145146
*/
147+
@Deprecated
146148
public OSSClient(String endpoint, String accessKeyId, String secretAccessKey, String securityToken) {
147149
this(endpoint, new DefaultCredentialProvider(accessKeyId, secretAccessKey, securityToken), null);
148150
}
@@ -161,6 +163,7 @@ public OSSClient(String endpoint, String accessKeyId, String secretAccessKey, St
161163
* A {@link ClientConfiguration} instance. The method would use
162164
* default configuration if it's null.
163165
*/
166+
@Deprecated
164167
public OSSClient(String endpoint, String accessKeyId, String secretAccessKey, ClientConfiguration config) {
165168
this(endpoint, new DefaultCredentialProvider(accessKeyId, secretAccessKey), config);
166169
}
@@ -182,6 +185,7 @@ public OSSClient(String endpoint, String accessKeyId, String secretAccessKey, Cl
182185
* A {@link ClientConfiguration} instance. The method would use
183186
* default configuration if it's null.
184187
*/
188+
@Deprecated
185189
public OSSClient(String endpoint, String accessKeyId, String secretAccessKey, String securityToken,
186190
ClientConfiguration config) {
187191
this(endpoint, new DefaultCredentialProvider(accessKeyId, secretAccessKey, securityToken), config);
@@ -197,6 +201,7 @@ public OSSClient(String endpoint, String accessKeyId, String secretAccessKey, St
197201
* Credentials provider which has access key Id and access Key
198202
* secret.
199203
*/
204+
@Deprecated
200205
public OSSClient(String endpoint, CredentialsProvider credsProvider) {
201206
this(endpoint, credsProvider, null);
202207
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.aliyun.oss;
21+
22+
import com.aliyun.oss.common.auth.CredentialsProvider;
23+
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
24+
25+
/**
26+
* Fluent builder for OSS Client. Use of the builder is preferred over using
27+
* constructors of the client class.
28+
*/
29+
public class OSSClientBuilder implements OSSBuilder {
30+
31+
@Override
32+
public OSS build(String endpoint, String accessKeyId, String secretAccessKey) {
33+
return new OSSClient(endpoint, getDefaultCredentialProvider(accessKeyId, secretAccessKey),
34+
getDefaultClientConfiguration());
35+
}
36+
37+
@Override
38+
public OSS build(String endpoint, String accessKeyId, String secretAccessKey, String securityToken) {
39+
return new OSSClient(endpoint, getDefaultCredentialProvider(accessKeyId, secretAccessKey, securityToken),
40+
getDefaultClientConfiguration());
41+
}
42+
43+
@Override
44+
public OSS build(String endpoint, String accessKeyId, String secretAccessKey, ClientConfiguration config) {
45+
if (config == null) {
46+
config = getDefaultClientConfiguration();
47+
}
48+
return new OSSClient(endpoint, getDefaultCredentialProvider(accessKeyId, secretAccessKey), config);
49+
}
50+
51+
@Override
52+
public OSS build(String endpoint, String accessKeyId, String secretAccessKey, String securityToken,
53+
ClientConfiguration config) {
54+
if (config == null) {
55+
config = getDefaultClientConfiguration();
56+
}
57+
return new OSSClient(endpoint, getDefaultCredentialProvider(accessKeyId, secretAccessKey, securityToken),
58+
config);
59+
}
60+
61+
@Override
62+
public OSS build(String endpoint, CredentialsProvider credsProvider) {
63+
return new OSSClient(endpoint, credsProvider, getDefaultClientConfiguration());
64+
}
65+
66+
@Override
67+
public OSS build(String endpoint, CredentialsProvider credsProvider, ClientConfiguration config) {
68+
if (config == null) {
69+
config = getDefaultClientConfiguration();
70+
}
71+
return new OSSClient(endpoint, credsProvider, config);
72+
}
73+
74+
private static ClientConfiguration getDefaultClientConfiguration() {
75+
ClientConfiguration config = new ClientConfiguration();
76+
config.setSupportCname(false);
77+
return config;
78+
}
79+
80+
private static DefaultCredentialProvider getDefaultCredentialProvider(String accessKeyId, String secretAccessKey) {
81+
return new DefaultCredentialProvider(accessKeyId, secretAccessKey);
82+
}
83+
84+
private static DefaultCredentialProvider getDefaultCredentialProvider(String accessKeyId, String secretAccessKey,
85+
String securityToken) {
86+
return new DefaultCredentialProvider(accessKeyId, secretAccessKey, securityToken);
87+
}
88+
89+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.aliyun.oss.common.auth;
21+
22+
import com.aliyun.oss.common.auth.Credentials;
23+
import com.aliyun.oss.common.utils.AuthUtils;
24+
25+
public class BasicCredentials implements Credentials {
26+
27+
public BasicCredentials(String accessKeyId, String accessKeySecret, String securityToken) {
28+
this(accessKeyId, accessKeySecret, securityToken, 0);
29+
}
30+
31+
public BasicCredentials(String accessKeyId, String accessKeySecret, String securityToken,
32+
long expiredDurationSeconds) {
33+
this.accessKeyId = accessKeyId;
34+
this.accessKeySecret = accessKeySecret;
35+
this.securityToken = securityToken;
36+
this.expiredDurationSeconds = expiredDurationSeconds;
37+
this.startedTimeInMilliSeconds = System.currentTimeMillis();
38+
}
39+
40+
public BasicCredentials withExpiredFactor(double expiredFactor) {
41+
this.expiredFactor = expiredFactor;
42+
return this;
43+
}
44+
45+
public BasicCredentials withExpiredDuration(long expiredDurationSeconds) {
46+
this.expiredDurationSeconds = expiredDurationSeconds;
47+
return this;
48+
}
49+
50+
@Override
51+
public String getAccessKeyId() {
52+
return accessKeyId;
53+
}
54+
55+
@Override
56+
public String getSecretAccessKey() {
57+
return accessKeySecret;
58+
}
59+
60+
@Override
61+
public String getSecurityToken() {
62+
return securityToken;
63+
}
64+
65+
@Override
66+
public boolean useSecurityToken() {
67+
return this.securityToken != null;
68+
}
69+
70+
public boolean willSoonExpire() {
71+
if (expiredDurationSeconds == 0) {
72+
return false;
73+
}
74+
long now = System.currentTimeMillis();
75+
return expiredDurationSeconds * expiredFactor < (now - startedTimeInMilliSeconds) / 1000.0;
76+
}
77+
78+
protected String accessKeyId;
79+
protected String accessKeySecret;
80+
protected String securityToken;
81+
82+
protected long expiredDurationSeconds;
83+
protected long startedTimeInMilliSeconds = 0;
84+
protected double expiredFactor = AuthUtils.DEFAULT_EXPIRED_FACTOR;
85+
86+
}

0 commit comments

Comments
 (0)