Skip to content

Commit 8c9533a

Browse files
authored
HADOOP-18397. Shutdown AWSSecurityTokenService when its resources are no longer in use (#4722)
Contributed by Viraj Jasani.
1 parent 59619ad commit 8c9533a

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,11 @@ public static MarshalledCredentials requestSessionCredentials(
207207
stsEndpoint.isEmpty() ? null : stsEndpoint,
208208
stsRegion)
209209
.build();
210-
return fromSTSCredentials(
211-
STSClientFactory.createClientConnection(tokenService, invoker)
212-
.requestSessionCredentials(duration, TimeUnit.SECONDS));
210+
try (STSClientFactory.STSClient stsClient = STSClientFactory.createClientConnection(
211+
tokenService, invoker)) {
212+
return fromSTSCredentials(stsClient.requestSessionCredentials(duration,
213+
TimeUnit.SECONDS));
214+
}
213215
} catch (SdkClientException e) {
214216
if (stsRegion.isEmpty()) {
215217
LOG.error("Region must be provided when requesting session credentials.",

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,10 @@ public static AWSSecurityTokenServiceClientBuilder builder(
149149
* @param tokenService STS instance
150150
* @param invoker invoker to use
151151
* @return an STS client bonded to that interface.
152-
* @throws IOException on any failure
153152
*/
154153
public static STSClient createClientConnection(
155154
final AWSSecurityTokenService tokenService,
156-
final Invoker invoker)
157-
throws IOException {
155+
final Invoker invoker) {
158156
return new STSClient(tokenService, invoker);
159157
}
160158

@@ -175,12 +173,9 @@ private STSClient(final AWSSecurityTokenService tokenService,
175173

176174
@Override
177175
public void close() throws IOException {
178-
try {
179-
tokenService.shutdown();
180-
} catch (UnsupportedOperationException ignored) {
181-
// ignore this, as it is what the STS client currently
182-
// does.
183-
}
176+
// Since we are not using AbstractAWSSecurityTokenService, we
177+
// don't need to worry about catching UnsupportedOperationException.
178+
tokenService.shutdown();
184179
}
185180

186181
/**

hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/ITestS3ATemporaryCredentials.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,14 @@ public void testSTS() throws IOException {
125125
credentials,
126126
getStsEndpoint(conf),
127127
getStsRegion(conf));
128-
STSClientFactory.STSClient clientConnection =
129-
STSClientFactory.createClientConnection(
130-
builder.build(),
131-
new Invoker(new S3ARetryPolicy(conf), Invoker.LOG_EVENT));
132-
Credentials sessionCreds = clientConnection
133-
.requestSessionCredentials(TEST_SESSION_TOKEN_DURATION_SECONDS,
134-
TimeUnit.SECONDS);
128+
Credentials sessionCreds;
129+
try (STSClientFactory.STSClient clientConnection =
130+
STSClientFactory.createClientConnection(builder.build(),
131+
new Invoker(new S3ARetryPolicy(conf), Invoker.LOG_EVENT))) {
132+
sessionCreds = clientConnection
133+
.requestSessionCredentials(
134+
TEST_SESSION_TOKEN_DURATION_SECONDS, TimeUnit.SECONDS);
135+
}
135136

136137
// clone configuration so changes here do not affect the base FS.
137138
Configuration conf2 = new Configuration(conf);
@@ -379,11 +380,12 @@ public <E extends Exception> E expectedSessionRequestFailure(
379380
Invoker invoker = new Invoker(new S3ARetryPolicy(conf),
380381
LOG_AT_ERROR);
381382

382-
STSClientFactory.STSClient stsClient
383-
= STSClientFactory.createClientConnection(tokenService,
384-
invoker);
385-
386-
return stsClient.requestSessionCredentials(30, TimeUnit.MINUTES);
383+
try (STSClientFactory.STSClient stsClient =
384+
STSClientFactory.createClientConnection(
385+
tokenService, invoker)) {
386+
return stsClient.requestSessionCredentials(
387+
30, TimeUnit.MINUTES);
388+
}
387389
});
388390
}
389391
}
@@ -413,6 +415,7 @@ public void testTemporaryCredentialValidationOnLoad() throws Throwable {
413415
return sc.toString();
414416
});
415417
}
418+
416419
@Test
417420
public void testEmptyTemporaryCredentialValidation() throws Throwable {
418421
Configuration conf = new Configuration();

0 commit comments

Comments
 (0)