Search in sources :

Example 1 with AmazonServiceExceptionMappingService

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);
    }
}
Also used : Path(ch.cyberduck.core.Path) AmazonClientException(com.amazonaws.AmazonClientException) URI(java.net.URI) AmazonServiceExceptionMappingService(ch.cyberduck.core.aws.AmazonServiceExceptionMappingService) PathContainerService(ch.cyberduck.core.PathContainerService) Distribution(ch.cyberduck.core.cdn.Distribution) AmazonCloudFront(com.amazonaws.services.cloudfront.AmazonCloudFront)

Example 2 with AmazonServiceExceptionMappingService

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);
    }
}
Also used : Path(ch.cyberduck.core.Path) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) S3BucketListService(ch.cyberduck.core.s3.S3BucketListService) AmazonClientException(com.amazonaws.AmazonClientException) DistributionLogging(ch.cyberduck.core.cdn.features.DistributionLogging) AmazonServiceExceptionMappingService(ch.cyberduck.core.aws.AmazonServiceExceptionMappingService) S3Protocol(ch.cyberduck.core.s3.S3Protocol) Purge(ch.cyberduck.core.cdn.features.Purge) Distribution(ch.cyberduck.core.cdn.Distribution)

Example 3 with AmazonServiceExceptionMappingService

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);
    }
}
Also used : Path(ch.cyberduck.core.Path) AccessDeniedException(ch.cyberduck.core.exception.AccessDeniedException) InteroperabilityException(ch.cyberduck.core.exception.InteroperabilityException) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) S3BucketListService(ch.cyberduck.core.s3.S3BucketListService) AmazonClientException(com.amazonaws.AmazonClientException) DistributionLogging(ch.cyberduck.core.cdn.features.DistributionLogging) AmazonServiceExceptionMappingService(ch.cyberduck.core.aws.AmazonServiceExceptionMappingService) S3Protocol(ch.cyberduck.core.s3.S3Protocol) Purge(ch.cyberduck.core.cdn.features.Purge) Distribution(ch.cyberduck.core.cdn.Distribution)

Example 4 with AmazonServiceExceptionMappingService

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;
}
Also used : Path(ch.cyberduck.core.Path) AliasListEntry(com.amazonaws.services.kms.model.AliasListEntry) AccessDeniedException(ch.cyberduck.core.exception.AccessDeniedException) HashMap(java.util.HashMap) AmazonClientException(com.amazonaws.AmazonClientException) AmazonServiceExceptionMappingService(ch.cyberduck.core.aws.AmazonServiceExceptionMappingService) AWSKMS(com.amazonaws.services.kms.AWSKMS) KeyListEntry(com.amazonaws.services.kms.model.KeyListEntry)

Example 5 with AmazonServiceExceptionMappingService

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);
    }
}
Also used : Path(ch.cyberduck.core.Path) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) AmazonServiceExceptionMappingService(ch.cyberduck.core.aws.AmazonServiceExceptionMappingService) PathContainerService(ch.cyberduck.core.PathContainerService) Distribution(ch.cyberduck.core.cdn.Distribution) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) AmazonCloudFront(com.amazonaws.services.cloudfront.AmazonCloudFront)

Aggregations

Path (ch.cyberduck.core.Path)6 AmazonServiceExceptionMappingService (ch.cyberduck.core.aws.AmazonServiceExceptionMappingService)6 AmazonClientException (com.amazonaws.AmazonClientException)6 Distribution (ch.cyberduck.core.cdn.Distribution)4 PathContainerService (ch.cyberduck.core.PathContainerService)3 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)2 DistributionLogging (ch.cyberduck.core.cdn.features.DistributionLogging)2 Purge (ch.cyberduck.core.cdn.features.Purge)2 AccessDeniedException (ch.cyberduck.core.exception.AccessDeniedException)2 S3BucketListService (ch.cyberduck.core.s3.S3BucketListService)2 S3Protocol (ch.cyberduck.core.s3.S3Protocol)2 AmazonCloudFront (com.amazonaws.services.cloudfront.AmazonCloudFront)2 AlphanumericRandomStringService (ch.cyberduck.core.AlphanumericRandomStringService)1 ConflictException (ch.cyberduck.core.exception.ConflictException)1 InteroperabilityException (ch.cyberduck.core.exception.InteroperabilityException)1 HostPreferences (ch.cyberduck.core.preferences.HostPreferences)1 AWSKMS (com.amazonaws.services.kms.AWSKMS)1 AliasListEntry (com.amazonaws.services.kms.model.AliasListEntry)1 KeyListEntry (com.amazonaws.services.kms.model.KeyListEntry)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1