@Test public void testCreateEncryptedImagesWhenEveryGroupNeedToBeEncryptedWithDifferentKmsKeys() throws InterruptedException, ExecutionException, TimeoutException { String encryptedImageId = "ami-87654321"; String secondEncryptedImageId = "ami-87652222"; List<Group> groups = new ArrayList<>(); InstanceTemplate temp = new InstanceTemplate("medium", "master", 0L, emptyList(), InstanceStatus.CREATE_REQUESTED, Map.of(AwsInstanceTemplate.EBS_ENCRYPTION_ENABLED, true, InstanceTemplate.VOLUME_ENCRYPTION_KEY_ID, "arn:aws:kms:eu-west-1:980678888888:key/7e9173f2-6ac8"), 0L, "imageId"); CloudInstance instance = new CloudInstance("SOME_ID", temp, null); groups.add(new Group("master", InstanceGroupType.GATEWAY, singletonList(instance), null, null, null, null, null, null, 30, identity)); InstanceTemplate temp2 = new InstanceTemplate("medium", "worker", 1L, emptyList(), InstanceStatus.CREATE_REQUESTED, Map.of(AwsInstanceTemplate.EBS_ENCRYPTION_ENABLED, true, InstanceTemplate.VOLUME_ENCRYPTION_KEY_ID, "arn:aws:kms:eu-west-1:980678888888:key/almafa23-6ac8"), 1L, "imageId"); CloudInstance instance2 = new CloudInstance("SECOND_ID", temp2, null); groups.add(new Group("worker", InstanceGroupType.CORE, singletonList(instance2), null, null, null, null, null, null, 30, identity)); when(cloudStack.getGroups()).thenReturn(groups); when(ec2Client.copyImage(any())) .thenReturn(new CopyImageResult().withImageId(encryptedImageId)) .thenReturn(new CopyImageResult().withImageId(secondEncryptedImageId)); when(ec2Client.waiters()).thenReturn(ec2Waiters); when(ec2Waiters.imageAvailable()).thenReturn(waiter); Map<String, String> encryptedImages = underTest.createEncryptedImages(authenticatedContext(), cloudStack, resourceNotifier); verify(ec2Client, times(2)).copyImage(any()); verify(resourceNotifier, times(2)).notifyAllocation(any(), any()); verify(waiter, times(1)).run(any()); assertThat(encryptedImages.size()).isEqualTo(2); assertThat(encryptedImages).containsValue(encryptedImageId); assertThat(encryptedImages).containsValue(secondEncryptedImageId); }
public Map<String, String> createEncryptedImages(AuthenticatedContext ac, CloudStack cloudStack, PersistenceNotifier resourceNotifier) { String selectedAMIName = cloudStack.getImage().getImageName(); AwsCredentialView awsCredentialView = new AwsCredentialView(ac.getCloudCredential()); String regionName = ac.getCloudContext().getLocation().getRegion().value(); AmazonEC2Client client = awsClient.createAccess(awsCredentialView, regionName); Map<String, EncryptedImageConfig> configByGroupName = getEncryptedImageConfigByGroup(cloudStack, selectedAMIName, ac, client, resourceNotifier); Map<String, String> imageIdByGroupName = configByGroupName.entrySet() .stream() .collect(Collectors.toMap(Entry::getKey, e -> e.getValue().getImageId())); if (!imageIdByGroupName.isEmpty()) { Collection<String> imageIds = new HashSet<>(imageIdByGroupName.values()); LOGGER.debug("Start polling the availability of the created encrypted AMIs: '{}'", String.join(",", imageIds)); Waiter<DescribeImagesRequest> imageWaiter = client.waiters().imageAvailable(); DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withImageIds(imageIds); StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId()); run(imageWaiter, describeImagesRequest, stackCancellationCheck, "Encrypted image creation failed"); LOGGER.info("All created encrypted AMIs are available: '{}'", String.join(",", imageIds)); } return imageIdByGroupName; }
EncryptedImageCopyService { public Map<String, String> createEncryptedImages(AuthenticatedContext ac, CloudStack cloudStack, PersistenceNotifier resourceNotifier) { String selectedAMIName = cloudStack.getImage().getImageName(); AwsCredentialView awsCredentialView = new AwsCredentialView(ac.getCloudCredential()); String regionName = ac.getCloudContext().getLocation().getRegion().value(); AmazonEC2Client client = awsClient.createAccess(awsCredentialView, regionName); Map<String, EncryptedImageConfig> configByGroupName = getEncryptedImageConfigByGroup(cloudStack, selectedAMIName, ac, client, resourceNotifier); Map<String, String> imageIdByGroupName = configByGroupName.entrySet() .stream() .collect(Collectors.toMap(Entry::getKey, e -> e.getValue().getImageId())); if (!imageIdByGroupName.isEmpty()) { Collection<String> imageIds = new HashSet<>(imageIdByGroupName.values()); LOGGER.debug("Start polling the availability of the created encrypted AMIs: '{}'", String.join(",", imageIds)); Waiter<DescribeImagesRequest> imageWaiter = client.waiters().imageAvailable(); DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withImageIds(imageIds); StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId()); run(imageWaiter, describeImagesRequest, stackCancellationCheck, "Encrypted image creation failed"); LOGGER.info("All created encrypted AMIs are available: '{}'", String.join(",", imageIds)); } return imageIdByGroupName; } }
EncryptedImageCopyService { public Map<String, String> createEncryptedImages(AuthenticatedContext ac, CloudStack cloudStack, PersistenceNotifier resourceNotifier) { String selectedAMIName = cloudStack.getImage().getImageName(); AwsCredentialView awsCredentialView = new AwsCredentialView(ac.getCloudCredential()); String regionName = ac.getCloudContext().getLocation().getRegion().value(); AmazonEC2Client client = awsClient.createAccess(awsCredentialView, regionName); Map<String, EncryptedImageConfig> configByGroupName = getEncryptedImageConfigByGroup(cloudStack, selectedAMIName, ac, client, resourceNotifier); Map<String, String> imageIdByGroupName = configByGroupName.entrySet() .stream() .collect(Collectors.toMap(Entry::getKey, e -> e.getValue().getImageId())); if (!imageIdByGroupName.isEmpty()) { Collection<String> imageIds = new HashSet<>(imageIdByGroupName.values()); LOGGER.debug("Start polling the availability of the created encrypted AMIs: '{}'", String.join(",", imageIds)); Waiter<DescribeImagesRequest> imageWaiter = client.waiters().imageAvailable(); DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withImageIds(imageIds); StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId()); run(imageWaiter, describeImagesRequest, stackCancellationCheck, "Encrypted image creation failed"); LOGGER.info("All created encrypted AMIs are available: '{}'", String.join(",", imageIds)); } return imageIdByGroupName; }  }
EncryptedImageCopyService { public Map<String, String> createEncryptedImages(AuthenticatedContext ac, CloudStack cloudStack, PersistenceNotifier resourceNotifier) { String selectedAMIName = cloudStack.getImage().getImageName(); AwsCredentialView awsCredentialView = new AwsCredentialView(ac.getCloudCredential()); String regionName = ac.getCloudContext().getLocation().getRegion().value(); AmazonEC2Client client = awsClient.createAccess(awsCredentialView, regionName); Map<String, EncryptedImageConfig> configByGroupName = getEncryptedImageConfigByGroup(cloudStack, selectedAMIName, ac, client, resourceNotifier); Map<String, String> imageIdByGroupName = configByGroupName.entrySet() .stream() .collect(Collectors.toMap(Entry::getKey, e -> e.getValue().getImageId())); if (!imageIdByGroupName.isEmpty()) { Collection<String> imageIds = new HashSet<>(imageIdByGroupName.values()); LOGGER.debug("Start polling the availability of the created encrypted AMIs: '{}'", String.join(",", imageIds)); Waiter<DescribeImagesRequest> imageWaiter = client.waiters().imageAvailable(); DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withImageIds(imageIds); StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId()); run(imageWaiter, describeImagesRequest, stackCancellationCheck, "Encrypted image creation failed"); LOGGER.info("All created encrypted AMIs are available: '{}'", String.join(",", imageIds)); } return imageIdByGroupName; }  Map<String, String> createEncryptedImages(AuthenticatedContext ac, CloudStack cloudStack, PersistenceNotifier resourceNotifier); void deleteResources(String regionName, AmazonEC2Client client, List<CloudResource> resources); }
EncryptedImageCopyService { public Map<String, String> createEncryptedImages(AuthenticatedContext ac, CloudStack cloudStack, PersistenceNotifier resourceNotifier) { String selectedAMIName = cloudStack.getImage().getImageName(); AwsCredentialView awsCredentialView = new AwsCredentialView(ac.getCloudCredential()); String regionName = ac.getCloudContext().getLocation().getRegion().value(); AmazonEC2Client client = awsClient.createAccess(awsCredentialView, regionName); Map<String, EncryptedImageConfig> configByGroupName = getEncryptedImageConfigByGroup(cloudStack, selectedAMIName, ac, client, resourceNotifier); Map<String, String> imageIdByGroupName = configByGroupName.entrySet() .stream() .collect(Collectors.toMap(Entry::getKey, e -> e.getValue().getImageId())); if (!imageIdByGroupName.isEmpty()) { Collection<String> imageIds = new HashSet<>(imageIdByGroupName.values()); LOGGER.debug("Start polling the availability of the created encrypted AMIs: '{}'", String.join(",", imageIds)); Waiter<DescribeImagesRequest> imageWaiter = client.waiters().imageAvailable(); DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest().withImageIds(imageIds); StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId()); run(imageWaiter, describeImagesRequest, stackCancellationCheck, "Encrypted image creation failed"); LOGGER.info("All created encrypted AMIs are available: '{}'", String.join(",", imageIds)); } return imageIdByGroupName; }  Map<String, String> createEncryptedImages(AuthenticatedContext ac, CloudStack cloudStack, PersistenceNotifier resourceNotifier); void deleteResources(String regionName, AmazonEC2Client client, List<CloudResource> resources);  }