Skip to content

Commit 3573f34

Browse files
authored
change the time offset precision from int to long (#2448)
**Notes:** The clockskew auto-correct logic in the SDK relies on the `int` primitive type when calculating the offset. When the offset is converted from milliseconds to days, the ms represented as an `int` have the boundaries as -24 and +24 days. Changing it to long (64-bit precision) fixes the limit.
1 parent 8914ae4 commit 3573f34

File tree

14 files changed

+83
-35
lines changed

14 files changed

+83
-35
lines changed

aws-android-sdk-core/src/main/java/com/amazonaws/AmazonWebServiceClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public abstract class AmazonWebServiceClient {
9595

9696
/** Optional offset (in seconds) to use when signing requests. */
9797
@SuppressWarnings("checkstyle:visibilitymodifier")
98-
protected int timeOffset;
98+
protected long timeOffset;
9999

100100
/** AWS signer for authenticating requests. */
101101
private volatile Signer signer;
@@ -660,7 +660,7 @@ public AmazonWebServiceClient withTimeOffset(final int timeOffset) {
660660
*
661661
* @return The optional value for time offset (in seconds) for this client.
662662
*/
663-
public int getTimeOffset() {
663+
public long getTimeOffset() {
664664
return timeOffset;
665665
}
666666

aws-android-sdk-core/src/main/java/com/amazonaws/DefaultRequest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class DefaultRequest<T> implements Request<T> {
6868
private InputStream content;
6969

7070
/** An optional time offset to account for clock skew */
71-
private int timeOffset;
71+
private long timeOffset;
7272

7373
/** All AWS Request metrics are collected into this object. */
7474
private AWSRequestMetrics metrics;
@@ -264,24 +264,43 @@ public void setHostPrefix(String hostPrefix) {
264264
* {@inheritDoc}
265265
*/
266266
@Override
267-
public int getTimeOffset() {
267+
public long getTimeOffset() {
268268
return timeOffset;
269269
}
270270

271271
/**
272272
* {@inheritDoc}
273273
*/
274+
@Deprecated
274275
@Override
275276
public void setTimeOffset(int timeOffset) {
277+
this.setTimeOffset((long) timeOffset);
278+
}
279+
280+
/**
281+
* {@inheritDoc}
282+
*/
283+
@Override
284+
public void setTimeOffset(long timeOffset) {
276285
this.timeOffset = timeOffset;
277286
}
278287

279288
/**
280289
* {@inheritDoc}
281290
*/
282291
@Override
292+
@Deprecated
283293
@SuppressWarnings("checkstyle:hiddenfield")
284294
public Request<T> withTimeOffset(int timeOffset) {
295+
return withTimeOffset((long) timeOffset);
296+
}
297+
298+
/**
299+
* {@inheritDoc}
300+
*/
301+
@Override
302+
@SuppressWarnings("checkstyle:hiddenfield")
303+
public Request<T> withTimeOffset(long timeOffset) {
285304
setTimeOffset(timeOffset);
286305
return this;
287306
}

aws-android-sdk-core/src/main/java/com/amazonaws/Request.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,16 @@ public interface Request<T> {
193193
*
194194
* @return The optional value for time offset (in seconds) for this request.
195195
*/
196-
public int getTimeOffset();
196+
public long getTimeOffset();
197+
198+
/**
199+
* This is deprecated, use {@link #setTimeOffset(long)} instead.
200+
*
201+
* @param timeOffset The optional value for time offset (in seconds) for
202+
* this request.
203+
*/
204+
@Deprecated
205+
public void setTimeOffset(int timeOffset);
197206

198207
/**
199208
* Sets the optional value for time offset for this request. This will be
@@ -204,7 +213,16 @@ public interface Request<T> {
204213
* @param timeOffset The optional value for time offset (in seconds) for
205214
* this request.
206215
*/
207-
public void setTimeOffset(int timeOffset);
216+
public void setTimeOffset(long timeOffset);
217+
218+
/**
219+
* This is deprecated, use {@link #withTimeOffset(long)} instead.
220+
* @param timeOffset the time offset for the request.
221+
*
222+
* @return The updated request object.
223+
*/
224+
@Deprecated
225+
public Request<T> withTimeOffset(int timeOffset);
208226

209227
/**
210228
* Sets the optional value for time offset for this request. This will be
@@ -215,7 +233,7 @@ public interface Request<T> {
215233
*
216234
* @return The updated request object.
217235
*/
218-
public Request<T> withTimeOffset(int timeOffset);
236+
public Request<T> withTimeOffset(long timeOffset);
219237

220238
/**
221239
* @return the request metrics.

aws-android-sdk-core/src/main/java/com/amazonaws/SDKGlobalConfiguration.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
package com.amazonaws;
1717

18-
import java.util.concurrent.atomic.AtomicInteger;
18+
import java.util.concurrent.atomic.AtomicLong;
1919

2020
/**
2121
* SDKGlobalConfiguration is to configure any global settings
@@ -131,27 +131,38 @@ public class SDKGlobalConfiguration {
131131
* this variable will adjust clock skew globally. Java SDK detects clock
132132
* skew errors and adjusts global clock skew automatically.
133133
*/
134-
private static final AtomicInteger GLOBAL_TIME_OFFSET = new AtomicInteger(0);
134+
private static final AtomicLong GLOBAL_TIME_OFFSET = new AtomicLong(0);
135135

136136
/**
137137
* Sets the global time offset. If this value is set then all the subsequent
138138
* requests will use this value to generate timestamps. To adjust clock skew
139-
* per request use {@link Request#setTimeOffset(int)}
139+
* per request use {@link Request#setTimeOffset(long)}
140140
*
141141
* @param timeOffset the time difference between local client and server
142142
*/
143-
public static void setGlobalTimeOffset(int timeOffset) {
143+
public static void setGlobalTimeOffset(long timeOffset) {
144144
GLOBAL_TIME_OFFSET.set(timeOffset);
145145
}
146146

147+
/**
148+
* This method is deprecated, use {@link #setGlobalTimeOffset(long)} instead.
149+
*
150+
* @param timeOffset the time difference between local client and server
151+
*/
152+
@Deprecated
153+
public static void setGlobalTimeOffset(int timeOffset) {
154+
setGlobalTimeOffset((long) timeOffset);
155+
}
156+
147157
/**
148158
* Gets the global time offset. See {@link Request#getTimeOffset()} if
149159
* global time offset is not set.
150160
*
151-
* @return GLOBAL_TIME_OFFSET an AtomicInteger that holds the value of time
161+
* @return GLOBAL_TIME_OFFSET an AtomicLong that holds the value of time
152162
* offset
153163
*/
154-
public static int getGlobalTimeOffset() {
164+
public static long getGlobalTimeOffset() {
155165
return GLOBAL_TIME_OFFSET.get();
156166
}
167+
157168
}

aws-android-sdk-core/src/main/java/com/amazonaws/auth/AWS3Signer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void sign(Request<?> request, AWSCredentials credentials) {
7474
SigningAlgorithm algorithm = SigningAlgorithm.HmacSHA256;
7575
String nonce = UUID.randomUUID().toString();
7676

77-
int timeOffset = getTimeOffset(request);
77+
long timeOffset = getTimeOffset(request);
7878
Date dateValue = getSignatureDate(timeOffset);
7979
String date = DateUtils.formatRFC822Date(dateValue);
8080
boolean isHttps = false;

aws-android-sdk-core/src/main/java/com/amazonaws/auth/AWS4Signer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ protected final String getDateStamp(long dateMilli) {
317317
}
318318

319319
protected final long getDateFromRequest(Request<?> request) {
320-
final int timeOffset = getTimeOffset(request);
320+
final long timeOffset = getTimeOffset(request);
321321
Date date = getSignatureDate(timeOffset);
322322
if (overriddenDate != null) {
323323
date = overriddenDate;

aws-android-sdk-core/src/main/java/com/amazonaws/auth/AbstractAWSSigner.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ protected String newString(byte[] bytes) {
432432
return new String(bytes, UTF8);
433433
}
434434

435-
protected Date getSignatureDate(int timeOffset) {
435+
protected Date getSignatureDate(long timeOffset) {
436436
Date dateValue = new Date();
437437
if (timeOffset != 0) {
438438
long epochMillis = dateValue.getTime();
@@ -442,8 +442,8 @@ protected Date getSignatureDate(int timeOffset) {
442442
return dateValue;
443443
}
444444

445-
protected int getTimeOffset(Request<?> request) {
446-
int timeOffset = request.getTimeOffset();
445+
protected long getTimeOffset(Request<?> request) {
446+
long timeOffset = request.getTimeOffset();
447447
if (SDKGlobalConfiguration.getGlobalTimeOffset() != 0) {
448448
// if global time offset is set then use that (For clock skew
449449
// issues)

aws-android-sdk-core/src/main/java/com/amazonaws/auth/QueryStringSigner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void sign(Request<?> request, SignatureVersion version, SigningAlgorithm
6767
request.addParameter("AWSAccessKeyId", sanitizedCredentials.getAWSAccessKeyId());
6868
request.addParameter("SignatureVersion", version.toString());
6969

70-
int timeOffset = getTimeOffset(request);
70+
long timeOffset = getTimeOffset(request);
7171
request.addParameter("Timestamp", getFormattedTimestamp(timeOffset));
7272

7373
if (sanitizedCredentials instanceof AWSSessionCredentials) {
@@ -161,7 +161,7 @@ private String getCanonicalizedResourcePath(Request<?> request) {
161161
/**
162162
* Formats date as ISO 8601 timestamp
163163
*/
164-
private String getFormattedTimestamp(int offset) {
164+
private String getFormattedTimestamp(long offset) {
165165
SimpleDateFormat df = new SimpleDateFormat(
166166
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
167167
df.setTimeZone(TimeZone.getTimeZone("UTC"));

aws-android-sdk-core/src/main/java/com/amazonaws/http/AmazonHttpClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class AmazonHttpClient {
7272
private static final int HTTP_STATUS_REQ_TOO_LONG = 413;
7373
private static final int HTTP_STATUS_SERVICE_UNAVAILABLE = 503;
7474

75-
private static final int TIME_MILLISEC = 1000;
75+
private static final long TIME_MILLISEC = 1000L;
7676

7777
/**
7878
* Logger providing detailed information on requests/responses. Users can
@@ -439,7 +439,7 @@ <T> Response<T> executeHelper(Request<?> request,
439439
* exception.
440440
*/
441441
if (RetryUtils.isClockSkewError(ase)) {
442-
final int timeOffset = parseClockSkewOffset(httpResponse, ase);
442+
final long timeOffset = parseClockSkewOffset(httpResponse, ase);
443443
SDKGlobalConfiguration.setGlobalTimeOffset(timeOffset);
444444
}
445445
resetRequestAfterError(request, ase);
@@ -803,7 +803,7 @@ private String getServerDateFromException(String body) {
803803
return msg;
804804
}
805805

806-
int parseClockSkewOffset(HttpResponse response, AmazonServiceException exception) {
806+
long parseClockSkewOffset(HttpResponse response, AmazonServiceException exception) {
807807
final Date deviceDate = new Date();
808808
Date serverDate = null;
809809
String serverDateStr = null;
@@ -827,7 +827,7 @@ int parseClockSkewOffset(HttpResponse response, AmazonServiceException exception
827827
}
828828

829829
final long diff = deviceDate.getTime() - serverDate.getTime();
830-
return (int) (diff / TIME_MILLISEC);
830+
return diff / TIME_MILLISEC;
831831
}
832832

833833
@Override

aws-android-sdk-core/src/main/java/com/amazonaws/internal/keyvaluestore/AWSKeyValueStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Copyright 2019-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");

0 commit comments

Comments
 (0)