Skip to content

Commit

Permalink
Test credentials using sts:GetCallerIdentity.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Nov 1, 2024
1 parent 93069ec commit 73aba70
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions s3/src/main/java/ch/cyberduck/core/s3/S3Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import ch.cyberduck.core.PathContainerService;
import ch.cyberduck.core.Scheme;
import ch.cyberduck.core.UrlProvider;
import ch.cyberduck.core.auth.AWSCredentialsConfigurator;
import ch.cyberduck.core.auth.AWSSessionCredentialsRetriever;
import ch.cyberduck.core.aws.CustomClientConfiguration;
import ch.cyberduck.core.cdn.Distribution;
import ch.cyberduck.core.cdn.DistributionConfiguration;
import ch.cyberduck.core.cloudfront.CloudFrontDistributionConfigurationPreloader;
Expand Down Expand Up @@ -59,6 +61,7 @@
import ch.cyberduck.core.ssl.X509KeyManager;
import ch.cyberduck.core.ssl.X509TrustManager;
import ch.cyberduck.core.sts.STSAssumeRoleCredentialsRequestInterceptor;
import ch.cyberduck.core.sts.STSExceptionMappingService;
import ch.cyberduck.core.threading.CancelCallback;

import org.apache.http.HttpHeaders;
Expand Down Expand Up @@ -87,6 +90,12 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
import com.amazonaws.services.securitytoken.model.AWSSecurityTokenServiceException;
import com.amazonaws.services.securitytoken.model.GetCallerIdentityRequest;
import com.amazonaws.services.securitytoken.model.GetCallerIdentityResult;

import static com.amazonaws.services.s3.Headers.*;

public class S3Session extends HttpSession<RequestEntityRestStorageService> {
Expand Down Expand Up @@ -335,32 +344,53 @@ public void login(final LoginCallback prompt, final CancelCallback cancel) throw
log.warn(String.format("Skip verifying credentials with previous successful authentication event for %s", this));
return;
}
try {
final Path home = new DelegatingHomeFeature(new DefaultPathHomeFeature(host)).find();
if(home.isRoot()) {
if(S3Session.isAwsHostname(host.getHostname(), false)) {
final CustomClientConfiguration configuration = new CustomClientConfiguration(host,
new ThreadLocalHostnameDelegatingTrustManager(trust, host.getHostname()), key);
final AWSSecurityTokenServiceClientBuilder builder = AWSSecurityTokenServiceClientBuilder.standard()
.withCredentials(AWSCredentialsConfigurator.toAWSCredentialsProvider(client.getProviderCredentials()))
.withClientConfiguration(configuration);
final AWSSecurityTokenService service = builder.build();
// Returns details about the IAM user or role whose credentials are used to call the operation.
// No permissions are required to perform this operation.
try {
final GetCallerIdentityResult identity = service.getCallerIdentity(new GetCallerIdentityRequest());
if(log.isDebugEnabled()) {
log.debug(String.format("Skip querying region for %s", home));
log.debug(String.format("Successfully verified credentials for %s", identity));
}
}
else {
final Location.Name location = new S3LocationFeature(S3Session.this, regions).getLocation(home);
if(log.isDebugEnabled()) {
log.debug(String.format("Retrieved region %s", location));
catch(AWSSecurityTokenServiceException e) {
throw new STSExceptionMappingService().map(e);
}
}
else {
try {
final Path home = new DelegatingHomeFeature(new DefaultPathHomeFeature(host)).find();
if(home.isRoot()) {
if(log.isDebugEnabled()) {
log.debug(String.format("Skip querying region for %s", home));
}
}
if(!Location.unknown.equals(location)) {
else {
final Location.Name location = new S3LocationFeature(S3Session.this, regions).getLocation(home);
if(log.isDebugEnabled()) {
log.debug(String.format("Set default region to %s determined from %s", location, home));
log.debug(String.format("Retrieved region %s", location));
}
if(!Location.unknown.equals(location)) {
if(log.isDebugEnabled()) {
log.debug(String.format("Set default region to %s determined from %s", location, home));
}
//
host.setProperty("s3.location", location.getIdentifier());
}
//
host.setProperty("s3.location", location.getIdentifier());
}
}
}
catch(AccessDeniedException | InteroperabilityException e) {
log.warn(String.format("Failure %s querying region", e));
final Path home = new DefaultHomeFinderService(this).find();
if(log.isDebugEnabled()) {
log.debug(String.format("Retrieved %s", home));
catch(AccessDeniedException | InteroperabilityException e) {
log.warn(String.format("Failure %s querying region", e));
final Path home = new DefaultHomeFinderService(this).find();
if(log.isDebugEnabled()) {
log.debug(String.format("Retrieved %s", home));
}
}
}
}
Expand Down

0 comments on commit 73aba70

Please sign in to comment.