Skip to content

Commit 0f05bb3

Browse files
committed
support time skew manual adjustment.
1 parent 8cd4f8c commit 0f05bb3

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.LinkedList;
2626
import java.util.List;
2727
import java.util.Map;
28+
import java.util.Date;
2829
import java.util.concurrent.locks.Lock;
2930
import java.util.concurrent.locks.ReentrantLock;
3031

@@ -99,6 +100,8 @@ public class ClientConfiguration {
99100

100101
protected SignVersion signatureVersion = DEFAULT_SIGNATURE_VERSION;
101102

103+
protected long tickOffset = 0;
104+
102105
public ClientConfiguration() {
103106
super();
104107
AppendDefaultExcludeList(this.cnameExcludeList);
@@ -672,4 +675,28 @@ public SignVersion getSignatureVersion() {
672675
public void setSignatureVersion(SignVersion signatureVersion) {
673676
this.signatureVersion = signatureVersion;
674677
}
678+
679+
/**
680+
* Gets the difference between customized epoch time and local time, in millisecond.
681+
*
682+
* @return tick Offset
683+
*/
684+
public long getTickOffset() {
685+
return tickOffset;
686+
}
687+
688+
/**
689+
* Sets the custom base time.
690+
* OSS's token validation logic depends on the time.
691+
* It requires that there's no more than 15 min time difference between client and OSS server.
692+
* This API calculates the difference between local time to epoch time.
693+
* Later one other APIs use this difference to offset the local time before sending request to OSS.
694+
*
695+
* @param epochTicks
696+
* Custom Epoch ticks (in millisecond).
697+
*/
698+
public void setTickOffset(long epochTicks) {
699+
long localTime = new Date().getTime();
700+
this.tickOffset = epochTicks - localTime;
701+
}
675702
}

src/main/java/com/aliyun/oss/internal/OSSRequestMessageBuilder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,16 @@ public OSSRequestMessageBuilder setOriginalRequest(WebServiceRequest originalReq
167167
}
168168

169169
public RequestMessage build() {
170+
ClientConfiguration clientCofig = this.innerClient.getClientConfiguration();
170171
Map<String, String> sentHeaders = new HashMap<String, String>(this.headers);
171-
sentHeaders.put(OSSHeaders.DATE, DateUtil.formatRfc822Date(new Date()));
172172
Map<String, String> sentParameters = new LinkedHashMap<String, String>(this.parameters);
173+
Date now = new Date();
174+
if (clientCofig.getTickOffset() != 0) {
175+
now.setTime(now.getTime() + clientCofig.getTickOffset());
176+
}
177+
sentHeaders.put(OSSHeaders.DATE, DateUtil.formatRfc822Date(now));
173178

174179
RequestMessage request = new RequestMessage(this.originalRequest, this.bucket, this.key);
175-
ClientConfiguration clientCofig = this.innerClient.getClientConfiguration();
176180
request.setBucket(bucket);
177181
request.setKey(key);
178182
request.setEndpoint(determineFinalEndpoint(this.endpoint, this.bucket, clientCofig));

src/test/java/com/aliyun/oss/integrationtests/ClientBuilderTest.java

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121

2222
import java.io.ByteArrayInputStream;
2323
import java.io.InputStream;
24+
import java.util.Date;
2425

25-
import com.aliyun.oss.ClientBuilderConfiguration;
26-
import com.aliyun.oss.OSSClient;
27-
import com.aliyun.oss.OSSClientBuilder;
26+
import com.aliyun.oss.*;
2827
import com.aliyun.oss.common.auth.Credentials;
2928
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
3029
import com.aliyun.oss.model.BucketInfo;
@@ -33,6 +32,8 @@
3332
import junit.framework.Assert;
3433
import org.junit.Test;
3534

35+
import static com.aliyun.oss.OSSErrorCode.REQUEST_TIME_TOO_SKEWED;
36+
3637
public class ClientBuilderTest extends TestBase {
3738

3839
private final static String TEST_KEY = "test/test.txt";
@@ -170,4 +171,56 @@ public void testClientBuilderWithAll() {
170171
}
171172
}
172173

174+
@Test
175+
public void testClientBuilderSpecialEpochTicks() {
176+
OSSClient client = null;
177+
ClientBuilderConfiguration config = new ClientBuilderConfiguration();
178+
Assert.assertEquals(config.getTickOffset(), 0);
179+
config.setSupportCname(true);
180+
config.setConnectionTimeout(10000);
181+
try {
182+
long epochTicks = new Date().getTime();
183+
epochTicks -= 16*600*1000;
184+
config.setTickOffset(epochTicks);
185+
client = (OSSClient) new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT,
186+
new DefaultCredentialProvider(TestConfig.OSS_TEST_ACCESS_KEY_ID,
187+
TestConfig.OSS_TEST_ACCESS_KEY_SECRET),
188+
config);
189+
Assert.assertTrue(client.getClientConfiguration().isSupportCname());
190+
Assert.assertEquals(client.getClientConfiguration().getConnectionTimeout(), 10000);
191+
Assert.assertEquals(client.getClientConfiguration().getTickOffset(), -16*600*1000);
192+
193+
BucketInfo info = client.getBucketInfo(bucketName);
194+
Assert.assertTrue(false);
195+
} catch (Exception e) {
196+
if (e instanceof ServiceException) {
197+
String errorCode = ((ServiceException) e).getErrorCode();
198+
Assert.assertEquals(errorCode, REQUEST_TIME_TOO_SKEWED);
199+
} else {
200+
Assert.assertTrue(false);
201+
}
202+
} finally {
203+
client.shutdown();
204+
}
205+
206+
try {
207+
long epochTicks = new Date().getTime();
208+
config.setTickOffset(epochTicks);
209+
client = (OSSClient) new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT,
210+
new DefaultCredentialProvider(TestConfig.OSS_TEST_ACCESS_KEY_ID,
211+
TestConfig.OSS_TEST_ACCESS_KEY_SECRET),
212+
config);
213+
Assert.assertTrue(client.getClientConfiguration().isSupportCname());
214+
Assert.assertEquals(client.getClientConfiguration().getConnectionTimeout(), 10000);
215+
216+
BucketInfo info = client.getBucketInfo(bucketName);
217+
Assert.assertEquals(info.getBucket().getName(), bucketName);
218+
} catch (Exception e) {
219+
Assert.assertTrue(false);
220+
} finally {
221+
client.shutdown();
222+
}
223+
224+
225+
}
173226
}

0 commit comments

Comments
 (0)