Skip to content

Commit 5939781

Browse files
committed
Remove redundant implementation of request interceptor interface.
1 parent cd9f3a0 commit 5939781

File tree

6 files changed

+86
-136
lines changed

6 files changed

+86
-136
lines changed

s3/src/main/java/ch/cyberduck/core/s3/S3Session.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@
5252
import ch.cyberduck.core.ssl.DisabledX509TrustManager;
5353
import ch.cyberduck.core.ssl.X509KeyManager;
5454
import ch.cyberduck.core.ssl.X509TrustManager;
55-
import ch.cyberduck.core.sts.STSAssumeRoleRequestInterceptor;
56-
import ch.cyberduck.core.sts.STSAssumeRoleWithWebIdentityRequestInterceptor;
55+
import ch.cyberduck.core.sts.STSAssumeRoleCredentialsStrategy;
56+
import ch.cyberduck.core.sts.STSAssumeRoleWithWebIdentityCredentialsStrategy;
5757
import ch.cyberduck.core.sts.STSAuthorizationService;
58-
import ch.cyberduck.core.sts.STSGetSessionTokenRequestInterceptor;
59-
import ch.cyberduck.core.sts.STSRequestInterceptor;
58+
import ch.cyberduck.core.sts.STSCredentialsStrategy;
59+
import ch.cyberduck.core.sts.STSGetSessionTokenCredentialsStrategy;
6060
import ch.cyberduck.core.threading.CancelCallback;
6161

6262
import org.apache.commons.lang3.StringUtils;
@@ -261,11 +261,10 @@ protected S3CredentialsStrategy configureCredentialsStrategy(final HttpClientBui
261261
}
262262
log.debug("Add interceptor {}", oauth);
263263
configuration.addInterceptorLast(oauth);
264-
final STSAssumeRoleWithWebIdentityRequestInterceptor interceptor
265-
= new STSAssumeRoleWithWebIdentityRequestInterceptor(oauth, host, trust, key, prompt);
266-
log.debug("Add interceptor {}", interceptor);
267-
configuration.addInterceptorLast(interceptor);
268-
return interceptor;
264+
final STSAssumeRoleWithWebIdentityCredentialsStrategy strategy
265+
= new STSAssumeRoleWithWebIdentityCredentialsStrategy(oauth, host, trust, key, prompt);
266+
log.debug("Return authenticator {}", strategy);
267+
return strategy;
269268
}
270269
if(S3Session.isAwsHostname(host.getHostname())) {
271270
// Try auto-configure
@@ -276,16 +275,14 @@ protected S3CredentialsStrategy configureCredentialsStrategy(final HttpClientBui
276275
}
277276
}
278277
if(host.getProtocol().isRoleConfigurable()) {
279-
final STSRequestInterceptor interceptor = new STSAssumeRoleRequestInterceptor(host, trust, key, prompt);
280-
log.debug("Add interceptor {}", interceptor);
281-
configuration.addInterceptorLast(interceptor);
282-
return interceptor;
278+
final STSCredentialsStrategy strategy = new STSAssumeRoleCredentialsStrategy(host, trust, key, prompt);
279+
log.debug("Return authenticator {}", strategy);
280+
return strategy;
283281
}
284282
if(host.getProtocol().isMultiFactorConfigurable()) {
285-
final STSRequestInterceptor interceptor = new STSGetSessionTokenRequestInterceptor(host, trust, key, prompt);
286-
log.debug("Add interceptor {}", interceptor);
287-
configuration.addInterceptorLast(interceptor);
288-
return interceptor;
283+
final STSCredentialsStrategy strategy = new STSGetSessionTokenCredentialsStrategy(host, trust, key, prompt);
284+
log.debug("Return authenticator {}", strategy);
285+
return strategy;
289286
}
290287
// Keep copy of credentials
291288
final Credentials credentials = new Credentials(host.getCredentials());

s3/src/main/java/ch/cyberduck/core/sts/STSAssumeRoleRequestInterceptor.java renamed to s3/src/main/java/ch/cyberduck/core/sts/STSAssumeRoleCredentialsStrategy.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import ch.cyberduck.core.ssl.X509KeyManager;
2727
import ch.cyberduck.core.ssl.X509TrustManager;
2828

29-
import org.apache.http.HttpRequestInterceptor;
3029
import org.apache.logging.log4j.LogManager;
3130
import org.apache.logging.log4j.Logger;
3231

@@ -35,13 +34,13 @@
3534
/**
3635
* Swap static access key id and secret access key with temporary credentials obtained from STS AssumeRole
3736
*/
38-
public class STSAssumeRoleRequestInterceptor extends STSRequestInterceptor implements S3CredentialsStrategy, HttpRequestInterceptor {
39-
private static final Logger log = LogManager.getLogger(STSAssumeRoleRequestInterceptor.class);
37+
public class STSAssumeRoleCredentialsStrategy extends STSCredentialsStrategy implements S3CredentialsStrategy {
38+
private static final Logger log = LogManager.getLogger(STSAssumeRoleCredentialsStrategy.class);
4039

4140
private final ReentrantLock lock = new ReentrantLock();
4241
private final Host host;
4342

44-
public STSAssumeRoleRequestInterceptor(final Host host, final X509TrustManager trust, final X509KeyManager key, final LoginCallback prompt) {
43+
public STSAssumeRoleCredentialsStrategy(final Host host, final X509TrustManager trust, final X509KeyManager key, final LoginCallback prompt) {
4544
super(host, trust, key, prompt);
4645
this.host = host;
4746
}
@@ -51,10 +50,8 @@ public TemporaryAccessTokens refresh(final Credentials credentials) throws Backg
5150
lock.lock();
5251
try {
5352
final String arn = new ProxyPreferencesReader(host, credentials).getProperty(Profile.STS_ROLE_ARN_PROPERTY_KEY, "s3.assumerole.rolearn");
54-
log.debug("Use ARN {}", arn);
55-
log.debug("Retrieve temporary credentials with {}", credentials);
56-
// AssumeRoleRequest
57-
return tokens = this.assumeRole(credentials, arn);
53+
log.debug("Retrieve temporary credentials with {} for role ARN {}", credentials, arn);
54+
return this.assumeRole(credentials, arn);
5855
}
5956
finally {
6057
lock.unlock();
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import ch.cyberduck.core.ssl.X509KeyManager;
2929
import ch.cyberduck.core.ssl.X509TrustManager;
3030

31-
import org.apache.http.HttpRequestInterceptor;
3231
import org.apache.logging.log4j.LogManager;
3332
import org.apache.logging.log4j.Logger;
3433

@@ -37,8 +36,8 @@
3736
/**
3837
* Swap OIDC Id token for temporary security credentials
3938
*/
40-
public class STSAssumeRoleWithWebIdentityRequestInterceptor extends STSRequestInterceptor implements S3CredentialsStrategy, HttpRequestInterceptor {
41-
private static final Logger log = LogManager.getLogger(STSAssumeRoleWithWebIdentityRequestInterceptor.class);
39+
public class STSAssumeRoleWithWebIdentityCredentialsStrategy extends STSCredentialsStrategy implements S3CredentialsStrategy {
40+
private static final Logger log = LogManager.getLogger(STSAssumeRoleWithWebIdentityCredentialsStrategy.class);
4241

4342
private final ReentrantLock lock = new ReentrantLock();
4443

@@ -48,9 +47,9 @@ public class STSAssumeRoleWithWebIdentityRequestInterceptor extends STSRequestIn
4847
private final OAuth2RequestInterceptor oauth;
4948
private final Host host;
5049

51-
public STSAssumeRoleWithWebIdentityRequestInterceptor(final OAuth2RequestInterceptor oauth, final Host host,
52-
final X509TrustManager trust, final X509KeyManager key,
53-
final LoginCallback prompt) {
50+
public STSAssumeRoleWithWebIdentityCredentialsStrategy(final OAuth2RequestInterceptor oauth, final Host host,
51+
final X509TrustManager trust, final X509KeyManager key,
52+
final LoginCallback prompt) {
5453
super(host, trust, key, prompt);
5554
this.oauth = oauth;
5655
this.host = host;
@@ -60,14 +59,14 @@ public STSAssumeRoleWithWebIdentityRequestInterceptor(final OAuth2RequestInterce
6059
public TemporaryAccessTokens refresh(final Credentials credentials) throws BackgroundException {
6160
lock.lock();
6261
final String arn = new ProxyPreferencesReader(host, credentials).getProperty(Profile.STS_ROLE_ARN_PROPERTY_KEY, "s3.assumerole.rolearn");
63-
log.debug("Use ARN {}", arn);
6462
try {
65-
return tokens = this.assumeRoleWithWebIdentity(oauth.validate(credentials.getOauth()), arn);
63+
log.debug("Retrieve temporary credentials with {} for role ARN {}", credentials, arn);
64+
return this.assumeRoleWithWebIdentity(oauth.validate(credentials.getOauth()), arn);
6665
}
6766
catch(LoginFailureException e) {
6867
// Expired or invalid OAuth tokens
6968
log.warn("Failure {} authorizing. Retry with refreshed OAuth tokens", e.getMessage());
70-
return this.tokens = this.assumeRoleWithWebIdentity(oauth.refresh(), arn);
69+
return this.assumeRoleWithWebIdentity(oauth.refresh(), arn);
7170
}
7271
finally {
7372
lock.unlock();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ch.cyberduck.core.sts;
2+
3+
/*
4+
* Copyright (c) 2002-2025 iterate GmbH. All rights reserved.
5+
* https://cyberduck.io/
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*/
17+
18+
import ch.cyberduck.core.Credentials;
19+
import ch.cyberduck.core.Host;
20+
import ch.cyberduck.core.LoginCallback;
21+
import ch.cyberduck.core.TemporaryAccessTokens;
22+
import ch.cyberduck.core.exception.BackgroundException;
23+
import ch.cyberduck.core.s3.S3CredentialsStrategy;
24+
import ch.cyberduck.core.ssl.X509KeyManager;
25+
import ch.cyberduck.core.ssl.X509TrustManager;
26+
27+
/**
28+
* Swap static access key id and secret access key with temporary credentials obtained from STS AssumeRole
29+
*/
30+
public abstract class STSCredentialsStrategy extends STSAuthorizationService implements S3CredentialsStrategy {
31+
32+
private final Host host;
33+
34+
public STSCredentialsStrategy(final Host host, final X509TrustManager trust, final X509KeyManager key, final LoginCallback prompt) {
35+
super(host, trust, key, prompt);
36+
this.host = host;
37+
}
38+
39+
/**
40+
* Request new temporary access tokens from static access key in credentials
41+
*
42+
* @param credentials Static long-lived credentials
43+
* @return Temporary access tokens from STS service
44+
*/
45+
public abstract TemporaryAccessTokens refresh(final Credentials credentials) throws BackgroundException;
46+
47+
@Override
48+
public Credentials get() throws BackgroundException {
49+
final Credentials credentials = host.getCredentials();
50+
final TemporaryAccessTokens tokens = credentials.getTokens();
51+
// Get temporary credentials from STS using static long-lived credentials
52+
return credentials.setTokens(tokens.isExpired() ? this.refresh(credentials) : tokens);
53+
}
54+
}

s3/src/main/java/ch/cyberduck/core/sts/STSGetSessionTokenRequestInterceptor.java renamed to s3/src/main/java/ch/cyberduck/core/sts/STSGetSessionTokenCredentialsStrategy.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import ch.cyberduck.core.ssl.X509KeyManager;
2727
import ch.cyberduck.core.ssl.X509TrustManager;
2828

29-
import org.apache.http.HttpRequestInterceptor;
3029
import org.apache.logging.log4j.LogManager;
3130
import org.apache.logging.log4j.Logger;
3231

@@ -35,14 +34,14 @@
3534
/**
3635
* Swap static access key id and secret access key with temporary credentials obtained from STS AssumeRole
3736
*/
38-
public class STSGetSessionTokenRequestInterceptor extends STSRequestInterceptor implements S3CredentialsStrategy, HttpRequestInterceptor {
39-
private static final Logger log = LogManager.getLogger(STSGetSessionTokenRequestInterceptor.class);
37+
public class STSGetSessionTokenCredentialsStrategy extends STSCredentialsStrategy implements S3CredentialsStrategy {
38+
private static final Logger log = LogManager.getLogger(STSGetSessionTokenCredentialsStrategy.class);
4039

4140
private final ReentrantLock lock = new ReentrantLock();
4241

4342
private final Host host;
4443

45-
public STSGetSessionTokenRequestInterceptor(final Host host, final X509TrustManager trust, final X509KeyManager key, final LoginCallback prompt) {
44+
public STSGetSessionTokenCredentialsStrategy(final Host host, final X509TrustManager trust, final X509KeyManager key, final LoginCallback prompt) {
4645
super(host, trust, key, prompt);
4746
this.host = host;
4847
}
@@ -52,10 +51,8 @@ public TemporaryAccessTokens refresh(final Credentials credentials) throws Backg
5251
lock.lock();
5352
try {
5453
final String arn = new ProxyPreferencesReader(host, credentials).getProperty(Profile.STS_MFA_ARN_PROPERTY_KEY);
55-
log.debug("Use ARN {}", arn);
56-
log.debug("Retrieve temporary credentials with {}", credentials);
57-
// GetSessionToken
58-
return tokens = this.getSessionToken(credentials, arn);
54+
log.debug("Retrieve temporary credentials with {} for role ARN {}", credentials, arn);
55+
return this.getSessionToken(credentials, arn);
5956
}
6057
finally {
6158
lock.unlock();

s3/src/main/java/ch/cyberduck/core/sts/STSRequestInterceptor.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)