Skip to content

Commit 756efc2

Browse files
committed
Review locking.
1 parent 7b22b91 commit 756efc2

File tree

4 files changed

+27
-37
lines changed

4 files changed

+27
-37
lines changed

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,12 @@
2929
import org.apache.logging.log4j.LogManager;
3030
import org.apache.logging.log4j.Logger;
3131

32-
import java.util.concurrent.locks.ReentrantLock;
33-
3432
/**
3533
* Swap static access key id and secret access key with temporary credentials obtained from STS AssumeRole
3634
*/
3735
public class STSAssumeRoleCredentialsStrategy extends STSCredentialsStrategy implements S3CredentialsStrategy {
3836
private static final Logger log = LogManager.getLogger(STSAssumeRoleCredentialsStrategy.class);
3937

40-
private final ReentrantLock lock = new ReentrantLock();
4138
private final Host host;
4239

4340
public STSAssumeRoleCredentialsStrategy(final Host host, final X509TrustManager trust, final X509KeyManager key, final LoginCallback prompt) {
@@ -47,14 +44,8 @@ public STSAssumeRoleCredentialsStrategy(final Host host, final X509TrustManager
4744

4845
@Override
4946
public TemporaryAccessTokens refresh(final Credentials credentials) throws BackgroundException {
50-
lock.lock();
51-
try {
52-
final String arn = new ProxyPreferencesReader(host, credentials).getProperty(Profile.STS_ROLE_ARN_PROPERTY_KEY, "s3.assumerole.rolearn");
53-
log.debug("Retrieve temporary credentials with {} for role ARN {}", credentials, arn);
54-
return this.assumeRole(credentials, arn);
55-
}
56-
finally {
57-
lock.unlock();
58-
}
47+
final String arn = new ProxyPreferencesReader(host, credentials).getProperty(Profile.STS_ROLE_ARN_PROPERTY_KEY, "s3.assumerole.rolearn");
48+
log.debug("Retrieve temporary credentials with {} for role ARN {}", credentials, arn);
49+
return this.assumeRole(credentials, arn);
5950
}
6051
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,12 @@
3131
import org.apache.logging.log4j.LogManager;
3232
import org.apache.logging.log4j.Logger;
3333

34-
import java.util.concurrent.locks.ReentrantLock;
35-
3634
/**
3735
* Swap OIDC Id token for temporary security credentials
3836
*/
3937
public class STSAssumeRoleWithWebIdentityCredentialsStrategy extends STSCredentialsStrategy implements S3CredentialsStrategy {
4038
private static final Logger log = LogManager.getLogger(STSAssumeRoleWithWebIdentityCredentialsStrategy.class);
4139

42-
private final ReentrantLock lock = new ReentrantLock();
43-
4440
/**
4541
* Handle authentication with OpenID connect retrieving token for STS
4642
*/
@@ -57,7 +53,6 @@ public STSAssumeRoleWithWebIdentityCredentialsStrategy(final OAuth2RequestInterc
5753

5854
@Override
5955
public TemporaryAccessTokens refresh(final Credentials credentials) throws BackgroundException {
60-
lock.lock();
6156
final String arn = new ProxyPreferencesReader(host, credentials).getProperty(Profile.STS_ROLE_ARN_PROPERTY_KEY, "s3.assumerole.rolearn");
6257
try {
6358
log.debug("Retrieve temporary credentials with {} for role ARN {}", credentials, arn);
@@ -68,8 +63,5 @@ public TemporaryAccessTokens refresh(final Credentials credentials) throws Backg
6863
log.warn("Failure {} authorizing. Retry with refreshed OAuth tokens", e.getMessage());
6964
return this.assumeRoleWithWebIdentity(oauth.authorize(), arn);
7065
}
71-
finally {
72-
lock.unlock();
73-
}
7466
}
7567
}

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@
2424
import ch.cyberduck.core.ssl.X509KeyManager;
2525
import ch.cyberduck.core.ssl.X509TrustManager;
2626

27+
import org.apache.logging.log4j.LogManager;
28+
import org.apache.logging.log4j.Logger;
29+
30+
import java.util.concurrent.locks.ReentrantLock;
31+
2732
/**
2833
* Swap static access key id and secret access key with temporary credentials obtained from STS AssumeRole
2934
*/
3035
public abstract class STSCredentialsStrategy extends STSAuthorizationService implements S3CredentialsStrategy {
36+
private static final Logger log = LogManager.getLogger(STSCredentialsStrategy.class);
3137

38+
private final ReentrantLock lock = new ReentrantLock();
3239
private final Host host;
3340

3441
public STSCredentialsStrategy(final Host host, final X509TrustManager trust, final X509KeyManager key, final LoginCallback prompt) {
@@ -46,9 +53,19 @@ public STSCredentialsStrategy(final Host host, final X509TrustManager trust, fin
4653

4754
@Override
4855
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);
56+
lock.lock();
57+
try {
58+
final Credentials credentials = host.getCredentials();
59+
final TemporaryAccessTokens tokens = credentials.getTokens();
60+
// Get temporary credentials from STS using static long-lived credentials
61+
if(tokens.isExpired()) {
62+
log.debug("Refresh expired tokens {} for {}", tokens, host);
63+
credentials.setTokens(this.refresh(credentials));
64+
}
65+
return credentials;
66+
}
67+
finally {
68+
lock.unlock();
69+
}
5370
}
5471
}

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,12 @@
2929
import org.apache.logging.log4j.LogManager;
3030
import org.apache.logging.log4j.Logger;
3131

32-
import java.util.concurrent.locks.ReentrantLock;
33-
3432
/**
3533
* Swap static access key id and secret access key with temporary credentials obtained from STS AssumeRole
3634
*/
3735
public class STSGetSessionTokenCredentialsStrategy extends STSCredentialsStrategy implements S3CredentialsStrategy {
3836
private static final Logger log = LogManager.getLogger(STSGetSessionTokenCredentialsStrategy.class);
3937

40-
private final ReentrantLock lock = new ReentrantLock();
41-
4238
private final Host host;
4339

4440
public STSGetSessionTokenCredentialsStrategy(final Host host, final X509TrustManager trust, final X509KeyManager key, final LoginCallback prompt) {
@@ -48,14 +44,8 @@ public STSGetSessionTokenCredentialsStrategy(final Host host, final X509TrustMan
4844

4945
@Override
5046
public TemporaryAccessTokens refresh(final Credentials credentials) throws BackgroundException {
51-
lock.lock();
52-
try {
53-
final String arn = new ProxyPreferencesReader(host, credentials).getProperty(Profile.STS_MFA_ARN_PROPERTY_KEY);
54-
log.debug("Retrieve temporary credentials with {} for role ARN {}", credentials, arn);
55-
return this.getSessionToken(credentials, arn);
56-
}
57-
finally {
58-
lock.unlock();
59-
}
47+
final String arn = new ProxyPreferencesReader(host, credentials).getProperty(Profile.STS_MFA_ARN_PROPERTY_KEY);
48+
log.debug("Retrieve temporary credentials with {} for role ARN {}", credentials, arn);
49+
return this.getSessionToken(credentials, arn);
6050
}
6151
}

0 commit comments

Comments
 (0)