use of ch.cyberduck.core.aws.AmazonServiceExceptionMappingService in project cyberduck by iterate-ch.
the class CloudFrontDistributionConfiguration method read.
@Override
public Distribution read(final Path file, final Distribution.Method method, final LoginCallback prompt) throws BackgroundException {
final Path container = session.getFeature(PathContainerService.class).getContainer(file);
try {
if (log.isDebugEnabled()) {
log.debug(String.format("List %s distributions", method));
}
final AmazonCloudFront client = this.client(container);
if (method.equals(Distribution.STREAMING)) {
for (StreamingDistributionSummary d : client.listStreamingDistributions(new ListStreamingDistributionsRequest()).getStreamingDistributionList().getItems()) {
final S3Origin config = d.getS3Origin();
if (config != null) {
final URI origin = this.getOrigin(container, method);
if (config.getDomainName().equals(origin.getHost())) {
// We currently only support one distribution per bucket
return this.readStreamingDistribution(client, d, container, method);
}
}
}
} else if (method.equals(Distribution.DOWNLOAD)) {
// List distributions restricting to bucket name origin
for (DistributionSummary d : client.listDistributions(new ListDistributionsRequest()).getDistributionList().getItems()) {
for (Origin o : d.getOrigins().getItems()) {
final S3OriginConfig config = o.getS3OriginConfig();
if (config != null) {
if (o.getDomainName().equals(this.getOrigin(container, method).getHost())) {
// We currently only support one distribution per bucket
return this.readDownloadDistribution(client, d, container, method);
}
}
}
}
} else if (method.equals(Distribution.CUSTOM) || method.equals(Distribution.WEBSITE_CDN)) {
for (DistributionSummary d : client.listDistributions(new ListDistributionsRequest()).getDistributionList().getItems()) {
final URI origin = this.getOrigin(container, method);
for (Origin o : d.getOrigins().getItems()) {
// Listing all distributions and look for custom origin
final CustomOriginConfig config = o.getCustomOriginConfig();
if (config != null) {
if (o.getDomainName().equals(origin.getHost())) {
// We currently only support one distribution per bucket
return this.readDownloadDistribution(client, d, container, method);
}
}
}
}
}
final URI origin = this.getOrigin(container, method);
// Return disabled configuration
return new Distribution(method, this.getName(), origin, false);
} catch (AmazonClientException e) {
throw new AmazonServiceExceptionMappingService().map("Cannot read CDN configuration", e);
}
}
use of ch.cyberduck.core.aws.AmazonServiceExceptionMappingService in project cyberduck by iterate-ch.
the class CloudFrontDistributionConfiguration method readDownloadDistribution.
private Distribution readDownloadDistribution(final AmazonCloudFront client, final DistributionSummary summary, final Path container, final Distribution.Method method) throws BackgroundException {
// Retrieve distributions configuration to access current logging status settings.
try {
final GetDistributionConfigResult response = client.getDistributionConfig(new GetDistributionConfigRequest(summary.getId()));
final DistributionConfig configuration = response.getDistributionConfig();
final Distribution distribution = new Distribution(method, this.getName(), this.getOrigin(container, method), summary.isEnabled());
distribution.setId(summary.getId());
distribution.setDeployed("Deployed".equals(summary.getStatus()));
distribution.setUrl(URI.create(String.format("%s://%s%s", method.getScheme(), summary.getDomainName(), method.getContext())));
distribution.setSslUrl(method.equals(Distribution.DOWNLOAD) || method.equals(Distribution.CUSTOM) ? URI.create(String.format("https://%s%s", summary.getDomainName(), method.getContext())) : null);
distribution.setReference(configuration.getCallerReference());
distribution.setEtag(response.getETag());
distribution.setStatus(LocaleFactory.localizedString(summary.getStatus(), "S3"));
distribution.setCNAMEs(configuration.getAliases().getItems().toArray(new String[configuration.getAliases().getItems().size()]));
distribution.setLogging(configuration.getLogging().isEnabled());
distribution.setLoggingContainer(StringUtils.isNotBlank(configuration.getLogging().getBucket()) ? ServiceUtils.findBucketNameInHostname(configuration.getLogging().getBucket(), new S3Protocol().getDefaultHostname()) : null);
if (StringUtils.isNotBlank(configuration.getDefaultRootObject())) {
distribution.setIndexDocument(configuration.getDefaultRootObject());
}
if (this.getFeature(Purge.class, method) != null) {
distribution.setInvalidationStatus(this.readInvalidationStatus(client, distribution));
}
if (this.getFeature(DistributionLogging.class, method) != null) {
distribution.setContainers(new S3BucketListService(session, new S3LocationFeature.S3Region(bookmark.getRegion())).list(new Path(String.valueOf(Path.DELIMITER), EnumSet.of(Path.Type.volume, Path.Type.directory)), new DisabledListProgressListener()).toList());
}
return distribution;
} catch (AmazonClientException e) {
throw new AmazonServiceExceptionMappingService().map("Cannot read CDN configuration", e);
}
}
use of ch.cyberduck.core.aws.AmazonServiceExceptionMappingService in project cyberduck by iterate-ch.
the class CloudFrontDistributionConfiguration method readStreamingDistribution.
private Distribution readStreamingDistribution(final AmazonCloudFront client, final StreamingDistributionSummary summary, final Path container, final Distribution.Method method) throws BackgroundException {
// Retrieve distributions configuration to access current logging status settings.
try {
final GetStreamingDistributionConfigResult response = client.getStreamingDistributionConfig(new GetStreamingDistributionConfigRequest(summary.getId()));
final StreamingDistributionConfig configuration = response.getStreamingDistributionConfig();
final Distribution distribution = new Distribution(method, this.getName(), this.getOrigin(container, method), summary.isEnabled());
distribution.setId(summary.getId());
distribution.setDeployed("Deployed".equals(summary.getStatus()));
distribution.setUrl(URI.create(String.format("%s://%s%s", method.getScheme(), summary.getDomainName(), method.getContext())));
distribution.setSslUrl(method.equals(Distribution.DOWNLOAD) || method.equals(Distribution.CUSTOM) ? URI.create(String.format("https://%s%s", summary.getDomainName(), method.getContext())) : null);
distribution.setReference(configuration.getCallerReference());
distribution.setEtag(response.getETag());
distribution.setStatus(LocaleFactory.localizedString(summary.getStatus(), "S3"));
distribution.setCNAMEs(configuration.getAliases().getItems().toArray(new String[configuration.getAliases().getItems().size()]));
distribution.setLogging(configuration.getLogging().isEnabled());
distribution.setLoggingContainer(StringUtils.isNotBlank(configuration.getLogging().getBucket()) ? ServiceUtils.findBucketNameInHostname(configuration.getLogging().getBucket(), new S3Protocol().getDefaultHostname()) : null);
if (this.getFeature(Purge.class, method) != null) {
distribution.setInvalidationStatus(this.readInvalidationStatus(client, distribution));
}
if (this.getFeature(DistributionLogging.class, method) != null) {
try {
distribution.setContainers(new S3BucketListService(session, new S3LocationFeature.S3Region(bookmark.getRegion())).list(new Path(String.valueOf(Path.DELIMITER), EnumSet.of(Path.Type.volume, Path.Type.directory)), new DisabledListProgressListener()).toList());
} catch (AccessDeniedException | InteroperabilityException e) {
log.warn(String.format("Failure listing buckets. %s", e.getMessage()));
}
}
return distribution;
} catch (AmazonClientException e) {
throw new AmazonServiceExceptionMappingService().map("Cannot read CDN configuration", e);
}
}
use of ch.cyberduck.core.aws.AmazonServiceExceptionMappingService in project cyberduck by iterate-ch.
the class KMSEncryptionFeature method getKeys.
/**
* @return List of IDs of KMS managed keys
*/
@Override
public Set<Algorithm> getKeys(final Path file, final LoginCallback prompt) throws BackgroundException {
final Path container = containerService.getContainer(file);
final Set<Algorithm> keys = super.getKeys(container, prompt);
if (container.isRoot()) {
return keys;
}
try {
final AWSKMS client = this.client(container);
try {
final Map<String, String> aliases = new HashMap<>();
for (AliasListEntry entry : client.listAliases().getAliases()) {
aliases.put(entry.getTargetKeyId(), entry.getAliasName());
}
for (KeyListEntry entry : client.listKeys().getKeys()) {
keys.add(new AliasedAlgorithm(entry, aliases.get(entry.getKeyId())));
}
} catch (AmazonClientException e) {
throw new AmazonServiceExceptionMappingService().map("Cannot read AWS KMS configuration", e);
} finally {
client.shutdown();
}
} catch (AccessDeniedException e) {
log.warn(String.format("Ignore failure reading keys from KMS. %s", e.getMessage()));
keys.add(SSE_KMS_DEFAULT);
}
return keys;
}
use of ch.cyberduck.core.aws.AmazonServiceExceptionMappingService in project cyberduck by iterate-ch.
the class CloudFrontDistributionConfiguration method invalidate.
/**
* You can make any number of invalidation requests, but you can have only three invalidation requests in progress
* at one time. Each request can contain up to 1,000 objects to invalidate. If you exceed these limits, you get an
* error message.
* <p>
* It usually takes 10 to 15 minutes to complete your invalidation request, depending on the size of your request.
*/
@Override
public void invalidate(final Path container, final Distribution.Method method, final List<Path> files, final LoginCallback prompt) throws BackgroundException {
try {
final Distribution d = this.read(container, method, prompt);
if (d.isEnabled()) {
final List<String> keys = new ArrayList<>();
for (Path file : files) {
if (session.getFeature(PathContainerService.class).isContainer(file)) {
// To invalidate all of the objects in a distribution
keys.add(String.format("%s*", Path.DELIMITER));
} else {
if (file.isDirectory()) {
// The *, which replaces 0 or more characters, must be the last character in the invalidation path
keys.add(String.format("/%s*", session.getFeature(PathContainerService.class).getKey(file)));
} else {
keys.add(String.format("/%s", session.getFeature(PathContainerService.class).getKey(file)));
}
}
}
if (keys.isEmpty()) {
log.warn("No keys selected for invalidation");
} else {
final AmazonCloudFront client = this.client(container);
client.createInvalidation(new CreateInvalidationRequest(d.getId(), new InvalidationBatch(new Paths().withItems(keys).withQuantity(keys.size()), new AlphanumericRandomStringService().random())));
}
}
} catch (AmazonClientException e) {
throw new AmazonServiceExceptionMappingService().map("Cannot write CDN configuration", e);
}
}
Aggregations