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);
}
}
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()));
}
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();
}
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()));
}
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);
}
Aggregations