Skip to content

Commit b7e1d17

Browse files
committed
HADOOP-17379. AbstractS3ATokenIdentifier to set issue date == now.
1 parent 0b2510e commit b7e1d17

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/auth/delegation/AbstractS3ATokenIdentifier.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.io.DataOutput;
2525
import java.io.IOException;
2626
import java.net.URI;
27+
import java.time.Clock;
28+
import java.time.Instant;
2729
import java.util.Objects;
2830
import java.util.UUID;
2931

@@ -140,6 +142,7 @@ protected AbstractS3ATokenIdentifier(
140142
final URI uri) {
141143
super(kind, owner, renewer, realUser);
142144
this.uri = requireNonNull(uri);
145+
initializeIssueDate();
143146
}
144147

145148
/**
@@ -164,6 +167,13 @@ protected AbstractS3ATokenIdentifier(
164167
*/
165168
protected AbstractS3ATokenIdentifier(final Text kind) {
166169
super(kind);
170+
initializeIssueDate();
171+
}
172+
173+
private void initializeIssueDate() {
174+
Clock clock = Clock.systemDefaultZone();
175+
long now = clock.millis();
176+
setIssueDate(now);
167177
}
168178

169179
public String getBucket() {

hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/auth/delegation/SessionTokenIdentifier.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public SessionTokenIdentifier(
9090
final String origin) {
9191
super(kind, uri, owner, renewer, origin, encryptionSecrets);
9292
this.marshalledCredentials = marshalledCredentials;
93+
this.setMaxDate(this.marshalledCredentials.getExpiration());
9394
}
9495

9596
/**

hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/auth/delegation/TestS3ADelegationTokenSupport.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.hadoop.fs.s3a.auth.delegation;
2020

2121
import java.net.URI;
22+
import java.util.Random;
2223

2324
import org.junit.BeforeClass;
2425
import org.junit.Test;
@@ -38,6 +39,7 @@
3839
import static org.junit.Assert.assertEquals;
3940
import static org.junit.Assert.assertNotNull;
4041
import static org.junit.Assert.assertNull;
42+
import static org.junit.Assert.assertTrue;
4143

4244
/**
4345
* Unit tests related to S3A DT support.
@@ -58,6 +60,34 @@ public void testSessionTokenKind() throws Throwable {
5860
assertEquals(SESSION_TOKEN_KIND, identifier.getKind());
5961
}
6062

63+
@Test
64+
public void testSessionTokenIssueDateAndMaxDate() throws Throwable {
65+
AbstractS3ATokenIdentifier identifier
66+
= new SessionTokenIdentifier();
67+
assertEquals(SESSION_TOKEN_KIND, identifier.getKind());
68+
assertTrue("issue date is not set", identifier.getIssueDate() > 0L);
69+
// there's no information on the max date, hence the default
70+
assertEquals("max date", 0L, identifier.getMaxDate());
71+
72+
Text alice = new Text("alice");
73+
Text renewer = new Text("yarn");
74+
long expireTs = new Random().nextInt(Integer.MAX_VALUE);
75+
MarshalledCredentials cred = new MarshalledCredentials("a", "b", "");
76+
cred.setExpiration(expireTs);
77+
AbstractS3ATokenIdentifier identifier2
78+
= new SessionTokenIdentifier(SESSION_TOKEN_KIND,
79+
alice,
80+
renewer,
81+
new URI("s3a://landsat-pds/"),
82+
cred,
83+
new EncryptionSecrets(S3AEncryptionMethods.SSE_S3, ""),
84+
"origin");
85+
86+
assertTrue("issue date is not set", identifier2.getIssueDate() > 0L);
87+
assertEquals("expiry time is not same as the credential's one", expireTs, identifier2.getExpiryTime());
88+
assertEquals("max date is not same as expiry time", identifier2.getMaxDate(), identifier2.getExpiryTime());
89+
}
90+
6191
@Test
6292
public void testSessionTokenDecode() throws Throwable {
6393
Text alice = new Text("alice");
@@ -90,6 +120,8 @@ public void testSessionTokenDecode() throws Throwable {
90120
UserGroupInformation.AuthenticationMethod.TOKEN,
91121
decodedUser.getAuthenticationMethod());
92122
assertEquals("origin", decoded.getOrigin());
123+
assertEquals("issue date", identifier.getIssueDate(), decoded.getIssueDate());
124+
assertEquals("max date", identifier.getMaxDate(), decoded.getMaxDate());
93125
}
94126

95127
@Test

0 commit comments

Comments
 (0)