Search in sources :

Example 1 with AmazonCloudFront

use of com.amazonaws.services.cloudfront.AmazonCloudFront 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 AmazonCloudFront

use of com.amazonaws.services.cloudfront.AmazonCloudFront in project cyberduck by iterate-ch.

the class CloudFrontDistributionConfiguration method updateStreamingDistribution.

protected UpdateStreamingDistributionResult updateStreamingDistribution(final Path container, final Distribution distribution) throws BackgroundException {
    final URI origin = this.getOrigin(container, distribution.getMethod());
    if (log.isDebugEnabled()) {
        log.debug(String.format("Update %s distribution with origin %s", distribution, origin));
    }
    final AmazonCloudFront client = this.client(container);
    final GetStreamingDistributionConfigResult response = client.getStreamingDistributionConfig(new GetStreamingDistributionConfigRequest(distribution.getId()));
    final StreamingDistributionConfig config = response.getStreamingDistributionConfig().withEnabled(distribution.isEnabled()).withS3Origin(new S3Origin(origin.getHost(), StringUtils.EMPTY)).withAliases(new Aliases().withItems(distribution.getCNAMEs()).withQuantity(distribution.getCNAMEs().length));
    if (distribution.isLogging()) {
        // Make bucket name fully qualified
        final String loggingTarget = ServiceUtils.generateS3HostnameForBucket(distribution.getLoggingContainer(), false, new S3Protocol().getDefaultHostname());
        if (log.isDebugEnabled()) {
            log.debug(String.format("Set logging target for %s to %s", distribution, loggingTarget));
        }
        config.setLogging(new StreamingLoggingConfig().withEnabled(distribution.isLogging()).withBucket(loggingTarget).withPrefix(new HostPreferences(session.getHost()).getProperty("cloudfront.logging.prefix")));
    }
    return client.updateStreamingDistribution(new UpdateStreamingDistributionRequest(config, distribution.getId(), response.getETag()));
}
Also used : AmazonCloudFront(com.amazonaws.services.cloudfront.AmazonCloudFront) URI(java.net.URI) S3Protocol(ch.cyberduck.core.s3.S3Protocol) HostPreferences(ch.cyberduck.core.preferences.HostPreferences)

Example 3 with AmazonCloudFront

use of com.amazonaws.services.cloudfront.AmazonCloudFront in project cyberduck by iterate-ch.

the class CloudFrontDistributionConfiguration method createStreamingDistribution.

/**
 * Amazon CloudFront Extension to create a new distribution configuration
 *
 * @return Distribution configuration
 */
protected StreamingDistribution createStreamingDistribution(final Path container, final Distribution distribution) throws BackgroundException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Create new %s distribution", distribution));
    }
    final AmazonCloudFront client = this.client(container);
    final URI origin = this.getOrigin(container, distribution.getMethod());
    final String originId = String.format("%s-%s", preferences.getProperty("application.name"), new AlphanumericRandomStringService().random());
    final StreamingDistributionConfig config = new StreamingDistributionConfig(new AlphanumericRandomStringService().random(), new S3Origin(origin.getHost(), StringUtils.EMPTY), distribution.isEnabled()).withComment(originId).withTrustedSigners(new TrustedSigners().withEnabled(false).withQuantity(0)).withAliases(new Aliases().withItems(distribution.getCNAMEs()).withQuantity(distribution.getCNAMEs().length));
    // Make bucket name fully qualified
    final String loggingTarget = ServiceUtils.generateS3HostnameForBucket(distribution.getLoggingContainer(), false, new S3Protocol().getDefaultHostname());
    if (log.isDebugEnabled()) {
        log.debug(String.format("Set logging target for %s to %s", distribution, loggingTarget));
    }
    config.setLogging(new StreamingLoggingConfig().withEnabled(distribution.isLogging()).withBucket(loggingTarget).withPrefix(new HostPreferences(session.getHost()).getProperty("cloudfront.logging.prefix")));
    return client.createStreamingDistribution(new CreateStreamingDistributionRequest(config)).getStreamingDistribution();
}
Also used : AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) AmazonCloudFront(com.amazonaws.services.cloudfront.AmazonCloudFront) URI(java.net.URI) S3Protocol(ch.cyberduck.core.s3.S3Protocol) HostPreferences(ch.cyberduck.core.preferences.HostPreferences)

Example 4 with AmazonCloudFront

use of com.amazonaws.services.cloudfront.AmazonCloudFront in project cyberduck by iterate-ch.

the class CloudFrontDistributionConfiguration method deleteDownloadDistribution.

protected void deleteDownloadDistribution(final Path container, final Distribution distribution) throws BackgroundException {
    final URI origin = this.getOrigin(container, distribution.getMethod());
    if (log.isDebugEnabled()) {
        log.debug(String.format("Update %s distribution with origin %s", distribution, origin));
    }
    final AmazonCloudFront client = this.client(container);
    client.deleteDistribution(new DeleteDistributionRequest(distribution.getId(), distribution.getEtag()));
}
Also used : AmazonCloudFront(com.amazonaws.services.cloudfront.AmazonCloudFront) URI(java.net.URI)

Example 5 with AmazonCloudFront

use of com.amazonaws.services.cloudfront.AmazonCloudFront in project teamcity-s3-artifact-storage-plugin by JetBrains.

the class S3CloudFrontDistributionCreationController method doPost.

@Override
protected void doPost(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Element xmlResponse) {
    final BasePropertiesBean bean = new BasePropertiesBean(null);
    PluginPropertiesUtil.bindPropertiesFromRequest(request, bean);
    Map<String, String> params = bean.getProperties();
    String projectId = request.getParameter("projectId");
    final ActionErrors errors = new ActionErrors();
    SProject project = myProjectManager.findProjectByExternalId(projectId);
    if (project == null) {
        errors.addError(S3_CLOUDFRONT_CREATE_DISTRIBUTION, String.format("Project %s not found", projectId));
    } else {
        myAccessChecker.checkCanEditProject(project);
        String projectName = project.getName();
        IOGuard.allowNetworkCall(() -> {
            try {
                KeyPair keyPair = generateKeyPair();
                String bucketName = S3Util.getBucketName(params);
                if (keyPair.getPrivate() != null && keyPair.getPublic() != null && bucketName != null) {
                    String privateKey = toPemString("PRIVATE KEY", keyPair.getPrivate().getEncoded());
                    String publicKey = toPemString("PUBLIC KEY", keyPair.getPublic().getEncoded());
                    DistributionDTO distributionDTO = AWSCommonParams.withAWSClients(params, clients -> {
                        AmazonCloudFront cloudFrontClient = clients.createCloudFrontClient();
                        AmazonS3 s3Client = clients.createS3Client();
                        String comment;
                        long distrCount = cloudFrontClient.listDistributions(new ListDistributionsRequest()).getDistributionList().getItems().stream().filter(d -> d.getComment().startsWith(String.format(COMMENT, projectName))).count();
                        if (distrCount > 0) {
                            comment = String.format(NUMBERED_COMMENT, projectName, distrCount);
                        } else {
                            comment = String.format(COMMENT, projectName);
                        }
                        String name = "generated_" + UUID.randomUUID().toString().substring(0, 8);
                        String publicKeyId = null;
                        String keyGroupId = null;
                        try {
                            publicKeyId = uploadPublicKey(publicKey, name, comment, cloudFrontClient);
                            keyGroupId = createKeyGroup(publicKeyId, name, comment, cloudFrontClient);
                            Distribution distribution = createDistribution(keyGroupId, comment, bucketName, cloudFrontClient, s3Client);
                            return new DistributionDTO(distribution.getId(), comment, publicKeyId, name, privateKey);
                        } catch (SdkClientException e) {
                            errors.addException(S3_CLOUDFRONT_CREATE_DISTRIBUTION, e);
                            if (keyGroupId != null) {
                                try {
                                    cloudFrontClient.deleteKeyGroup(new DeleteKeyGroupRequest().withId(keyGroupId));
                                } catch (SdkClientException clientException) {
                                    LOG.warnAndDebugDetails("Encountered exception while trying to delete CloudFront key group", clientException);
                                }
                            }
                            if (publicKeyId != null) {
                                try {
                                    cloudFrontClient.deletePublicKey(new DeletePublicKeyRequest().withId(publicKeyId));
                                } catch (SdkClientException clientException) {
                                    LOG.warnAndDebugDetails("Encountered exception while trying to delete CloudFront public key", clientException);
                                }
                            }
                        }
                        return null;
                    });
                    if (distributionDTO != null) {
                        Element element = S3XmlSerializerFactory.getInstance().serializeAsElement(distributionDTO);
                        xmlResponse.addContent(element);
                    }
                }
            } catch (IllegalArgumentException | SdkClientException | IOException | NoSuchAlgorithmException e) {
                errors.addException(S3_CLOUDFRONT_CREATE_DISTRIBUTION, e);
            }
        });
    }
    errors.serialize(xmlResponse);
}
Also used : Policy(com.amazonaws.auth.policy.Policy) Principal(com.amazonaws.auth.policy.Principal) BucketPolicy(com.amazonaws.services.s3.model.BucketPolicy) ZonedDateTime(java.time.ZonedDateTime) S3Util(jetbrains.buildServer.artifacts.s3.S3Util) S3ObjectResource(com.amazonaws.auth.policy.resources.S3ObjectResource) S3Actions(com.amazonaws.auth.policy.actions.S3Actions) IOGuard(jetbrains.buildServer.serverSide.IOGuard) AWSCommonParams(jetbrains.buildServer.util.amazon.AWSCommonParams) HttpServletRequest(javax.servlet.http.HttpServletRequest) Map(java.util.Map) BaseFormXmlController(jetbrains.buildServer.controllers.BaseFormXmlController) AmazonS3(com.amazonaws.services.s3.AmazonS3) ProjectManager(jetbrains.buildServer.serverSide.ProjectManager) Statement(com.amazonaws.auth.policy.Statement) ZoneOffset(java.time.ZoneOffset) Logger(com.intellij.openapi.diagnostic.Logger) PemObject(org.bouncycastle.util.io.pem.PemObject) java.security(java.security) Used(jetbrains.buildServer.Used) AmazonCloudFront(com.amazonaws.services.cloudfront.AmazonCloudFront) Predicate(java.util.function.Predicate) StringWriter(java.io.StringWriter) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) BasePropertiesBean(jetbrains.buildServer.controllers.BasePropertiesBean) AccessChecker(jetbrains.buildServer.serverSide.auth.AccessChecker) IOException(java.io.IOException) UUID(java.util.UUID) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Collectors(java.util.stream.Collectors) PluginDescriptor(jetbrains.buildServer.web.openapi.PluginDescriptor) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) Nullable(org.jetbrains.annotations.Nullable) ModelAndView(org.springframework.web.servlet.ModelAndView) SdkClientException(com.amazonaws.SdkClientException) List(java.util.List) PemWriter(org.bouncycastle.util.io.pem.PemWriter) S3XmlSerializerFactory(jetbrains.buildServer.artifacts.s3.serialization.S3XmlSerializerFactory) PluginPropertiesUtil(jetbrains.buildServer.controllers.admin.projects.PluginPropertiesUtil) CloudFrontConstants(jetbrains.buildServer.artifacts.s3.cloudfront.CloudFrontConstants) ActionErrors(jetbrains.buildServer.controllers.ActionErrors) SProject(jetbrains.buildServer.serverSide.SProject) WebControllerManager(jetbrains.buildServer.web.openapi.WebControllerManager) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) com.amazonaws.services.cloudfront.model(com.amazonaws.services.cloudfront.model) AmazonS3(com.amazonaws.services.s3.AmazonS3) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Element(org.jdom.Element) IOException(java.io.IOException) SProject(jetbrains.buildServer.serverSide.SProject) ActionErrors(jetbrains.buildServer.controllers.ActionErrors) BasePropertiesBean(jetbrains.buildServer.controllers.BasePropertiesBean) SdkClientException(com.amazonaws.SdkClientException) AmazonCloudFront(com.amazonaws.services.cloudfront.AmazonCloudFront)

Aggregations

AmazonCloudFront (com.amazonaws.services.cloudfront.AmazonCloudFront)13 URI (java.net.URI)9 HostPreferences (ch.cyberduck.core.preferences.HostPreferences)6 S3Protocol (ch.cyberduck.core.s3.S3Protocol)6 AlphanumericRandomStringService (ch.cyberduck.core.AlphanumericRandomStringService)4 Path (ch.cyberduck.core.Path)2 PathContainerService (ch.cyberduck.core.PathContainerService)2 AmazonServiceExceptionMappingService (ch.cyberduck.core.aws.AmazonServiceExceptionMappingService)2 Distribution (ch.cyberduck.core.cdn.Distribution)2 AmazonClientException (com.amazonaws.AmazonClientException)2 SdkClientException (com.amazonaws.SdkClientException)1 Policy (com.amazonaws.auth.policy.Policy)1 Principal (com.amazonaws.auth.policy.Principal)1 Statement (com.amazonaws.auth.policy.Statement)1 S3Actions (com.amazonaws.auth.policy.actions.S3Actions)1 S3ObjectResource (com.amazonaws.auth.policy.resources.S3ObjectResource)1 com.amazonaws.services.cloudfront.model (com.amazonaws.services.cloudfront.model)1 CreateInvalidationRequest (com.amazonaws.services.cloudfront.model.CreateInvalidationRequest)1 CreateInvalidationResult (com.amazonaws.services.cloudfront.model.CreateInvalidationResult)1 InvalidationBatch (com.amazonaws.services.cloudfront.model.InvalidationBatch)1