Skip to content

Commit

Permalink
fix: TW-86419 enabled region correction attempt for cloudfront creati…
Browse files Browse the repository at this point in the history
…on op

fix: properly remove keys in case cloudfront creation fails


Merge-request: TC-MR-9354
Merged-by: Iaroslav Molochkov <iaroslav.molochkov@jetbrains.com>
  • Loading branch information
iaroslav-molochkov authored and qodana-bot committed Mar 7, 2024
1 parent e08d116 commit cecbcf6
Showing 1 changed file with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,35 +131,43 @@ protected void doPost(@NotNull HttpServletRequest request, @NotNull HttpServletR
}

String name = "generated_" + UUID.randomUUID().toString().substring(0, 8);
CreatePublicKeyResult publicKeyResult = null;
CreateKeyGroupResult keyGroupResult = null;
String publicKeyId = null;
String keyGroupId = null;
try {
publicKeyId = uploadPublicKey(publicKey, name, comment, cloudFrontClient);
keyGroupId = createKeyGroup(publicKeyId, name, comment, cloudFrontClient);
publicKeyResult = uploadPublicKey(publicKey, name, comment, cloudFrontClient);
publicKeyId = publicKeyResult.getPublicKey().getId();
keyGroupResult = createKeyGroup(publicKeyId, name, comment, cloudFrontClient);
keyGroupId = keyGroupResult.getKeyGroup().getId();
Distribution uploadDistribution = createDistribution(keyGroupId, comment, bucketName, cloudFrontClient, s3Client, true);
final DistributionDTO uploadDTO = new DistributionDTO(uploadDistribution.getId(), uploadDistribution.getDistributionConfig().getComment());

Distribution downloadDistribution = createDistribution(keyGroupId, comment, bucketName, cloudFrontClient, s3Client, false);
final DistributionDTO downloadDTO = new DistributionDTO(downloadDistribution.getId(), downloadDistribution.getDistributionConfig().getComment());
return new DistributionCreationResultDTO(uploadDTO, downloadDTO, publicKeyId, name, privateKey);
} catch (SdkClientException e) {
errors.addException(S3_CLOUDFRONT_CREATE_DISTRIBUTIONS, e);
if (keyGroupId != null) {
if (keyGroupResult != null) {
try {
cloudFrontClient.deleteKeyGroup(new DeleteKeyGroupRequest().withId(keyGroupId));
cloudFrontClient.deleteKeyGroup(new DeleteKeyGroupRequest()
.withId(keyGroupId)
.withIfMatch(keyGroupResult.getETag()));
} catch (SdkClientException clientException) {
LOG.warnAndDebugDetails("Encountered exception while trying to delete CloudFront key group", clientException);
}
}
if (publicKeyId != null) {
if (publicKeyResult != null) {
try {
cloudFrontClient.deletePublicKey(new DeletePublicKeyRequest().withId(publicKeyId));
cloudFrontClient.deletePublicKey(new DeletePublicKeyRequest()
.withId(publicKeyId)
.withIfMatch(publicKeyResult.getETag()));
} catch (SdkClientException clientException) {
LOG.warnAndDebugDetails("Encountered exception while trying to delete CloudFront public key", clientException);
}
}

throw e;
}
return null;
});
});
if (distributionCreationResultDTO != null) {
Expand Down Expand Up @@ -204,27 +212,24 @@ private Distribution createDistribution(@NotNull String keyGroupId,
}

@NotNull
private String createKeyGroup(@NotNull String publicKeyId, @NotNull String name, @NotNull String comment, @NotNull AmazonCloudFront cloudFrontClient) {
private CreateKeyGroupResult createKeyGroup(@NotNull String publicKeyId, @NotNull String name, @NotNull String comment, @NotNull AmazonCloudFront cloudFrontClient) {
CreateKeyGroupRequest createKeyGroupRequest = new CreateKeyGroupRequest()
.withKeyGroupConfig(new KeyGroupConfig()
.withName(name)
.withComment(comment)
.withItems(publicKeyId));
CreateKeyGroupResult keyGroup = cloudFrontClient.createKeyGroup(createKeyGroupRequest);

return keyGroup.getKeyGroup().getId();
return cloudFrontClient.createKeyGroup(createKeyGroupRequest);
}

@NotNull
private String uploadPublicKey(@NotNull String publicKey, @NotNull String name, @NotNull String comment, @NotNull AmazonCloudFront cloudFrontClient) {
private CreatePublicKeyResult uploadPublicKey(@NotNull String publicKey, @NotNull String name, @NotNull String comment, @NotNull AmazonCloudFront cloudFrontClient) {
PublicKeyConfig config = new PublicKeyConfig()
.withName(name)
.withComment(comment)
.withEncodedKey(publicKey)
.withCallerReference(ZonedDateTime.now(ZoneOffset.UTC).toString());

CreatePublicKeyResult result = cloudFrontClient.createPublicKey(new CreatePublicKeyRequest().withPublicKeyConfig(config));
return result.getPublicKey().getId();
return cloudFrontClient.createPublicKey(new CreatePublicKeyRequest().withPublicKeyConfig(config));
}

@NotNull
Expand Down Expand Up @@ -287,7 +292,7 @@ private CachePolicy getOrCreateCachePolicy(@NotNull AmazonCloudFront cloudFrontC

return existingPolicies
.stream()
.map(p -> p.getCachePolicy())
.map(CachePolicySummary::getCachePolicy)
.filter(IS_GENERATED_POLICY)
.findAny()
.orElseGet(() -> createNewPolicy(cloudFrontClient, existingPolicies));
Expand All @@ -297,7 +302,7 @@ private CachePolicy getOrCreateCachePolicy(@NotNull AmazonCloudFront cloudFrontC
private CachePolicy createNewPolicy(@NotNull AmazonCloudFront cloudFrontClient, @NotNull List<CachePolicySummary> existingPolicies) {
CachePolicy defaultPolicy = existingPolicies
.stream()
.map(p -> p.getCachePolicy())
.map(CachePolicySummary::getCachePolicy)
.filter(IS_DEFAULT_POLICY)
.findAny()
.orElseThrow(() -> new AmazonCloudFrontException(String.format("Managed Cache policy '%s' not found", S3_CLOUDFRONT_DEFAULT_CACHE_POLICY)));
Expand Down

0 comments on commit cecbcf6

Please sign in to comment.