in s3-artifact-storage-server/src/main/java/jetbrains/buildServer/artifacts/s3/web/S3CloudFrontDistributionCreationController.java [141:243]
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_DISTRIBUTIONS, 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());
DistributionCreationResultDTO distributionCreationResultDTO = myAmazonS3Provider.withCloudFrontClient(params, projectId, cloudFrontClient -> {
return myAmazonS3Provider.withS3Client(params, projectId, s3Client -> {
String comment;
long distrCount = 0;
DistributionList distributionsList;
String marker = null;
do {
ListDistributionsRequest.Builder requestBuilder = ListDistributionsRequest.builder().maxItems("1000").marker(marker);
distributionsList = cloudFrontClient.listDistributions(requestBuilder.build()).distributionList();
distrCount += distributionsList.items()
.stream()
.filter(d -> d.comment() != null && d.comment().startsWith(String.format(COMMENT, projectName)))
.count();
marker = distributionsList.nextMarker();
} while ( marker != null);
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);
CreatePublicKeyResponse publicKeyResult = null;
CreateKeyGroupResponse keyGroupResult = null;
String publicKeyId = null;
String keyGroupId = null;
try {
publicKeyResult = uploadPublicKey(publicKey, name, comment, cloudFrontClient);
publicKeyId = publicKeyResult.publicKey().id();
keyGroupResult = createKeyGroup(publicKeyId, name, comment, cloudFrontClient);
keyGroupId = keyGroupResult.keyGroup().id();
Distribution uploadDistribution = createDistribution(keyGroupId, comment, bucketName, cloudFrontClient, s3Client, true);
final DistributionDTO uploadDTO = new DistributionDTO(uploadDistribution.id(), uploadDistribution.distributionConfig().comment());
Distribution downloadDistribution = createDistribution(keyGroupId, comment, bucketName, cloudFrontClient, s3Client, false);
final DistributionDTO downloadDTO = new DistributionDTO(downloadDistribution.id(), downloadDistribution.distributionConfig().comment());
return new DistributionCreationResultDTO(uploadDTO, downloadDTO, publicKeyId, name, privateKey);
} catch (SdkClientException e) {
if (keyGroupResult != null) {
try {
cloudFrontClient.deleteKeyGroup(DeleteKeyGroupRequest.builder()
.id(keyGroupId)
.ifMatch(keyGroupResult.eTag())
.build());
} catch (SdkClientException clientException) {
LOG.warnAndDebugDetails("Encountered exception while trying to delete CloudFront key group", clientException);
}
}
if (publicKeyResult != null) {
try {
cloudFrontClient.deletePublicKey(DeletePublicKeyRequest.builder()
.id(publicKeyId)
.ifMatch(publicKeyResult.eTag())
.build());
} catch (SdkClientException clientException) {
LOG.warnAndDebugDetails("Encountered exception while trying to delete CloudFront public key", clientException);
}
}
throw e;
}
});
});
if (distributionCreationResultDTO != null) {
Element element = S3XmlSerializerFactory.getInstance().serializeAsElement(distributionCreationResultDTO);
xmlResponse.addContent(element);
}
}
} catch (IllegalArgumentException | SdkClientException | IOException | NoSuchAlgorithmException | ConnectionCredentialsException e) {
errors.addException(S3_CLOUDFRONT_CREATE_DISTRIBUTIONS, e);
}
});
}
errors.serialize(xmlResponse);
}