@Test public void collectEncryptionKeysWhenWeGetBackInfoThenItShouldReturnListWithElements() { ListKeysResult listKeysResult = new ListKeysResult(); Set<KeyListEntry> listEntries = new HashSet<>(); listEntries.add(keyListEntry(1)); listEntries.add(keyListEntry(2)); listEntries.add(keyListEntry(3)); listEntries.add(keyListEntry(4)); listKeysResult.setKeys(listEntries); DescribeKeyResult describeKeyResult = new DescribeKeyResult(); describeKeyResult.setKeyMetadata(new KeyMetadata()); ListAliasesResult describeAliasResult = new ListAliasesResult(); Set<AliasListEntry> aliasListEntries = new HashSet<>(); aliasListEntries.add(aliasListEntry(1)); aliasListEntries.add(aliasListEntry(2)); aliasListEntries.add(aliasListEntry(3)); aliasListEntries.add(aliasListEntry(4)); describeAliasResult.setAliases(aliasListEntries); when(awsClient.createAWSKMS(any(AwsCredentialView.class), anyString())).thenReturn(awskmsClient); when(awskmsClient.listKeys(any(ListKeysRequest.class))).thenReturn(listKeysResult); when(awskmsClient.describeKey(any(DescribeKeyRequest.class))).thenReturn(describeKeyResult); when(awskmsClient.listAliases(any(ListAliasesRequest.class))).thenReturn(describeAliasResult); CloudEncryptionKeys cloudEncryptionKeys = underTest.encryptionKeys(new CloudCredential("crn", "aws-credential"), region("London"), new HashMap<>()); Assert.assertEquals(4L, cloudEncryptionKeys.getCloudEncryptionKeys().size()); }
@Override public CloudEncryptionKeys encryptionKeys(CloudCredential cloudCredential, Region region, Map<String, String> filters) { String queryFailedMessage = "Could not get encryption keys from Amazon: "; CloudEncryptionKeys cloudEncryptionKeys = new CloudEncryptionKeys(new HashSet<>()); AwsCredentialView awsCredentialView = new AwsCredentialView(cloudCredential); AWSKMS client = awsClient.createAWSKMS(awsCredentialView, region.value()); try { ListKeysRequest listKeysRequest = new ListKeysRequest(); ListKeysResult listKeysResult = client.listKeys(listKeysRequest); ListAliasesResult listAliasesResult = client.listAliases(new ListAliasesRequest()); for (AliasListEntry keyListEntry : listAliasesResult.getAliases()) { try { listKeysResult.getKeys().stream() .filter(item -> item.getKeyId().equals(keyListEntry.getTargetKeyId())).findFirst() .ifPresent(item -> { DescribeKeyRequest describeKeyRequest = new DescribeKeyRequest().withKeyId(item.getKeyId()); DescribeKeyResult describeKeyResult = client.describeKey(describeKeyRequest); Map<String, Object> meta = new HashMap<>(); meta.put("aWSAccountId", describeKeyResult.getKeyMetadata().getAWSAccountId()); meta.put("creationDate", describeKeyResult.getKeyMetadata().getCreationDate()); meta.put("enabled", describeKeyResult.getKeyMetadata().getEnabled()); meta.put("expirationModel", describeKeyResult.getKeyMetadata().getExpirationModel()); meta.put("keyManager", describeKeyResult.getKeyMetadata().getKeyManager()); meta.put("keyState", describeKeyResult.getKeyMetadata().getKeyState()); meta.put("keyUsage", describeKeyResult.getKeyMetadata().getKeyUsage()); meta.put("origin", describeKeyResult.getKeyMetadata().getOrigin()); meta.put("validTo", describeKeyResult.getKeyMetadata().getValidTo()); if (!CloudConstants.AWS.equalsIgnoreCase(describeKeyResult.getKeyMetadata().getKeyManager())) { CloudEncryptionKey key = new CloudEncryptionKey( item.getKeyArn(), describeKeyResult.getKeyMetadata().getKeyId(), describeKeyResult.getKeyMetadata().getDescription(), keyListEntry.getAliasName().replace("alias/", ""), meta); cloudEncryptionKeys.getCloudEncryptionKeys().add(key); } }); } catch (AmazonServiceException e) { if (e.getStatusCode() == UNAUTHORIZED) { String policyMessage = "Could not get encryption keys because the user does not have enough permission."; LOGGER.error(policyMessage + e); } else { LOGGER.info(queryFailedMessage, e); } } catch (Exception e) { LOGGER.warn(queryFailedMessage, e); } } } catch (AmazonServiceException ase) { if (ase.getStatusCode() == UNAUTHORIZED) { String policyMessage = "Could not get encryption keys because the user does not have enough permission."; LOGGER.error(policyMessage + ase); throw new CloudConnectorException(policyMessage, ase); } else { LOGGER.info(queryFailedMessage, ase); throw new CloudConnectorException(queryFailedMessage + ase.getMessage(), ase); } } catch (Exception e) { LOGGER.warn(queryFailedMessage, e); throw new CloudConnectorException(queryFailedMessage + e.getMessage(), e); } return cloudEncryptionKeys; }
AwsPlatformResources implements PlatformResources { @Override public CloudEncryptionKeys encryptionKeys(CloudCredential cloudCredential, Region region, Map<String, String> filters) { String queryFailedMessage = "Could not get encryption keys from Amazon: "; CloudEncryptionKeys cloudEncryptionKeys = new CloudEncryptionKeys(new HashSet<>()); AwsCredentialView awsCredentialView = new AwsCredentialView(cloudCredential); AWSKMS client = awsClient.createAWSKMS(awsCredentialView, region.value()); try { ListKeysRequest listKeysRequest = new ListKeysRequest(); ListKeysResult listKeysResult = client.listKeys(listKeysRequest); ListAliasesResult listAliasesResult = client.listAliases(new ListAliasesRequest()); for (AliasListEntry keyListEntry : listAliasesResult.getAliases()) { try { listKeysResult.getKeys().stream() .filter(item -> item.getKeyId().equals(keyListEntry.getTargetKeyId())).findFirst() .ifPresent(item -> { DescribeKeyRequest describeKeyRequest = new DescribeKeyRequest().withKeyId(item.getKeyId()); DescribeKeyResult describeKeyResult = client.describeKey(describeKeyRequest); Map<String, Object> meta = new HashMap<>(); meta.put("aWSAccountId", describeKeyResult.getKeyMetadata().getAWSAccountId()); meta.put("creationDate", describeKeyResult.getKeyMetadata().getCreationDate()); meta.put("enabled", describeKeyResult.getKeyMetadata().getEnabled()); meta.put("expirationModel", describeKeyResult.getKeyMetadata().getExpirationModel()); meta.put("keyManager", describeKeyResult.getKeyMetadata().getKeyManager()); meta.put("keyState", describeKeyResult.getKeyMetadata().getKeyState()); meta.put("keyUsage", describeKeyResult.getKeyMetadata().getKeyUsage()); meta.put("origin", describeKeyResult.getKeyMetadata().getOrigin()); meta.put("validTo", describeKeyResult.getKeyMetadata().getValidTo()); if (!CloudConstants.AWS.equalsIgnoreCase(describeKeyResult.getKeyMetadata().getKeyManager())) { CloudEncryptionKey key = new CloudEncryptionKey( item.getKeyArn(), describeKeyResult.getKeyMetadata().getKeyId(), describeKeyResult.getKeyMetadata().getDescription(), keyListEntry.getAliasName().replace("alias/", ""), meta); cloudEncryptionKeys.getCloudEncryptionKeys().add(key); } }); } catch (AmazonServiceException e) { if (e.getStatusCode() == UNAUTHORIZED) { String policyMessage = "Could not get encryption keys because the user does not have enough permission."; LOGGER.error(policyMessage + e); } else { LOGGER.info(queryFailedMessage, e); } } catch (Exception e) { LOGGER.warn(queryFailedMessage, e); } } } catch (AmazonServiceException ase) { if (ase.getStatusCode() == UNAUTHORIZED) { String policyMessage = "Could not get encryption keys because the user does not have enough permission."; LOGGER.error(policyMessage + ase); throw new CloudConnectorException(policyMessage, ase); } else { LOGGER.info(queryFailedMessage, ase); throw new CloudConnectorException(queryFailedMessage + ase.getMessage(), ase); } } catch (Exception e) { LOGGER.warn(queryFailedMessage, e); throw new CloudConnectorException(queryFailedMessage + e.getMessage(), e); } return cloudEncryptionKeys; } }
AwsPlatformResources implements PlatformResources { @Override public CloudEncryptionKeys encryptionKeys(CloudCredential cloudCredential, Region region, Map<String, String> filters) { String queryFailedMessage = "Could not get encryption keys from Amazon: "; CloudEncryptionKeys cloudEncryptionKeys = new CloudEncryptionKeys(new HashSet<>()); AwsCredentialView awsCredentialView = new AwsCredentialView(cloudCredential); AWSKMS client = awsClient.createAWSKMS(awsCredentialView, region.value()); try { ListKeysRequest listKeysRequest = new ListKeysRequest(); ListKeysResult listKeysResult = client.listKeys(listKeysRequest); ListAliasesResult listAliasesResult = client.listAliases(new ListAliasesRequest()); for (AliasListEntry keyListEntry : listAliasesResult.getAliases()) { try { listKeysResult.getKeys().stream() .filter(item -> item.getKeyId().equals(keyListEntry.getTargetKeyId())).findFirst() .ifPresent(item -> { DescribeKeyRequest describeKeyRequest = new DescribeKeyRequest().withKeyId(item.getKeyId()); DescribeKeyResult describeKeyResult = client.describeKey(describeKeyRequest); Map<String, Object> meta = new HashMap<>(); meta.put("aWSAccountId", describeKeyResult.getKeyMetadata().getAWSAccountId()); meta.put("creationDate", describeKeyResult.getKeyMetadata().getCreationDate()); meta.put("enabled", describeKeyResult.getKeyMetadata().getEnabled()); meta.put("expirationModel", describeKeyResult.getKeyMetadata().getExpirationModel()); meta.put("keyManager", describeKeyResult.getKeyMetadata().getKeyManager()); meta.put("keyState", describeKeyResult.getKeyMetadata().getKeyState()); meta.put("keyUsage", describeKeyResult.getKeyMetadata().getKeyUsage()); meta.put("origin", describeKeyResult.getKeyMetadata().getOrigin()); meta.put("validTo", describeKeyResult.getKeyMetadata().getValidTo()); if (!CloudConstants.AWS.equalsIgnoreCase(describeKeyResult.getKeyMetadata().getKeyManager())) { CloudEncryptionKey key = new CloudEncryptionKey( item.getKeyArn(), describeKeyResult.getKeyMetadata().getKeyId(), describeKeyResult.getKeyMetadata().getDescription(), keyListEntry.getAliasName().replace("alias/", ""), meta); cloudEncryptionKeys.getCloudEncryptionKeys().add(key); } }); } catch (AmazonServiceException e) { if (e.getStatusCode() == UNAUTHORIZED) { String policyMessage = "Could not get encryption keys because the user does not have enough permission."; LOGGER.error(policyMessage + e); } else { LOGGER.info(queryFailedMessage, e); } } catch (Exception e) { LOGGER.warn(queryFailedMessage, e); } } } catch (AmazonServiceException ase) { if (ase.getStatusCode() == UNAUTHORIZED) { String policyMessage = "Could not get encryption keys because the user does not have enough permission."; LOGGER.error(policyMessage + ase); throw new CloudConnectorException(policyMessage, ase); } else { LOGGER.info(queryFailedMessage, ase); throw new CloudConnectorException(queryFailedMessage + ase.getMessage(), ase); } } catch (Exception e) { LOGGER.warn(queryFailedMessage, e); throw new CloudConnectorException(queryFailedMessage + e.getMessage(), e); } return cloudEncryptionKeys; }  }
AwsPlatformResources implements PlatformResources { @Override public CloudEncryptionKeys encryptionKeys(CloudCredential cloudCredential, Region region, Map<String, String> filters) { String queryFailedMessage = "Could not get encryption keys from Amazon: "; CloudEncryptionKeys cloudEncryptionKeys = new CloudEncryptionKeys(new HashSet<>()); AwsCredentialView awsCredentialView = new AwsCredentialView(cloudCredential); AWSKMS client = awsClient.createAWSKMS(awsCredentialView, region.value()); try { ListKeysRequest listKeysRequest = new ListKeysRequest(); ListKeysResult listKeysResult = client.listKeys(listKeysRequest); ListAliasesResult listAliasesResult = client.listAliases(new ListAliasesRequest()); for (AliasListEntry keyListEntry : listAliasesResult.getAliases()) { try { listKeysResult.getKeys().stream() .filter(item -> item.getKeyId().equals(keyListEntry.getTargetKeyId())).findFirst() .ifPresent(item -> { DescribeKeyRequest describeKeyRequest = new DescribeKeyRequest().withKeyId(item.getKeyId()); DescribeKeyResult describeKeyResult = client.describeKey(describeKeyRequest); Map<String, Object> meta = new HashMap<>(); meta.put("aWSAccountId", describeKeyResult.getKeyMetadata().getAWSAccountId()); meta.put("creationDate", describeKeyResult.getKeyMetadata().getCreationDate()); meta.put("enabled", describeKeyResult.getKeyMetadata().getEnabled()); meta.put("expirationModel", describeKeyResult.getKeyMetadata().getExpirationModel()); meta.put("keyManager", describeKeyResult.getKeyMetadata().getKeyManager()); meta.put("keyState", describeKeyResult.getKeyMetadata().getKeyState()); meta.put("keyUsage", describeKeyResult.getKeyMetadata().getKeyUsage()); meta.put("origin", describeKeyResult.getKeyMetadata().getOrigin()); meta.put("validTo", describeKeyResult.getKeyMetadata().getValidTo()); if (!CloudConstants.AWS.equalsIgnoreCase(describeKeyResult.getKeyMetadata().getKeyManager())) { CloudEncryptionKey key = new CloudEncryptionKey( item.getKeyArn(), describeKeyResult.getKeyMetadata().getKeyId(), describeKeyResult.getKeyMetadata().getDescription(), keyListEntry.getAliasName().replace("alias/", ""), meta); cloudEncryptionKeys.getCloudEncryptionKeys().add(key); } }); } catch (AmazonServiceException e) { if (e.getStatusCode() == UNAUTHORIZED) { String policyMessage = "Could not get encryption keys because the user does not have enough permission."; LOGGER.error(policyMessage + e); } else { LOGGER.info(queryFailedMessage, e); } } catch (Exception e) { LOGGER.warn(queryFailedMessage, e); } } } catch (AmazonServiceException ase) { if (ase.getStatusCode() == UNAUTHORIZED) { String policyMessage = "Could not get encryption keys because the user does not have enough permission."; LOGGER.error(policyMessage + ase); throw new CloudConnectorException(policyMessage, ase); } else { LOGGER.info(queryFailedMessage, ase); throw new CloudConnectorException(queryFailedMessage + ase.getMessage(), ase); } } catch (Exception e) { LOGGER.warn(queryFailedMessage, e); throw new CloudConnectorException(queryFailedMessage + e.getMessage(), e); } return cloudEncryptionKeys; }  @PostConstruct void init(); String resourceDefinition(String resource); @Override CloudNetworks networks(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudSshKeys sshKeys(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudSecurityGroups securityGroups(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override @Cacheable(cacheNames = "cloudResourceRegionCache", key = "{ #cloudCredential?.id, #availabilityZonesNeeded }") CloudRegions regions(CloudCredential cloudCredential, Region region, Map<String, String> filters, boolean availabilityZonesNeeded); void addDisplayName(Map<Region, String> displayNames, com.amazonaws.services.ec2.model.Region awsRegion); void addCoordinate(Map<Region, Coordinate> coordinates, com.amazonaws.services.ec2.model.Region awsRegion); @Override @Cacheable(cacheNames = "cloudResourceVmTypeCache", key = "#cloudCredential?.id + #region.getRegionName()") CloudVmTypes virtualMachines(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override @Cacheable(cacheNames = "cloudResourceVmTypeCache", key = "#cloudCredential?.id + #region.getRegionName() + 'distrox'") CloudVmTypes virtualMachinesForDistroX(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudGateWays gateways(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudIpPools publicIpPool(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudAccessConfigs accessConfigs(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudEncryptionKeys encryptionKeys(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudNoSqlTables noSqlTables(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudResourceGroups resourceGroups(CloudCredential cloudCredential, Region region, Map<String, String> filters); }
AwsPlatformResources implements PlatformResources { @Override public CloudEncryptionKeys encryptionKeys(CloudCredential cloudCredential, Region region, Map<String, String> filters) { String queryFailedMessage = "Could not get encryption keys from Amazon: "; CloudEncryptionKeys cloudEncryptionKeys = new CloudEncryptionKeys(new HashSet<>()); AwsCredentialView awsCredentialView = new AwsCredentialView(cloudCredential); AWSKMS client = awsClient.createAWSKMS(awsCredentialView, region.value()); try { ListKeysRequest listKeysRequest = new ListKeysRequest(); ListKeysResult listKeysResult = client.listKeys(listKeysRequest); ListAliasesResult listAliasesResult = client.listAliases(new ListAliasesRequest()); for (AliasListEntry keyListEntry : listAliasesResult.getAliases()) { try { listKeysResult.getKeys().stream() .filter(item -> item.getKeyId().equals(keyListEntry.getTargetKeyId())).findFirst() .ifPresent(item -> { DescribeKeyRequest describeKeyRequest = new DescribeKeyRequest().withKeyId(item.getKeyId()); DescribeKeyResult describeKeyResult = client.describeKey(describeKeyRequest); Map<String, Object> meta = new HashMap<>(); meta.put("aWSAccountId", describeKeyResult.getKeyMetadata().getAWSAccountId()); meta.put("creationDate", describeKeyResult.getKeyMetadata().getCreationDate()); meta.put("enabled", describeKeyResult.getKeyMetadata().getEnabled()); meta.put("expirationModel", describeKeyResult.getKeyMetadata().getExpirationModel()); meta.put("keyManager", describeKeyResult.getKeyMetadata().getKeyManager()); meta.put("keyState", describeKeyResult.getKeyMetadata().getKeyState()); meta.put("keyUsage", describeKeyResult.getKeyMetadata().getKeyUsage()); meta.put("origin", describeKeyResult.getKeyMetadata().getOrigin()); meta.put("validTo", describeKeyResult.getKeyMetadata().getValidTo()); if (!CloudConstants.AWS.equalsIgnoreCase(describeKeyResult.getKeyMetadata().getKeyManager())) { CloudEncryptionKey key = new CloudEncryptionKey( item.getKeyArn(), describeKeyResult.getKeyMetadata().getKeyId(), describeKeyResult.getKeyMetadata().getDescription(), keyListEntry.getAliasName().replace("alias/", ""), meta); cloudEncryptionKeys.getCloudEncryptionKeys().add(key); } }); } catch (AmazonServiceException e) { if (e.getStatusCode() == UNAUTHORIZED) { String policyMessage = "Could not get encryption keys because the user does not have enough permission."; LOGGER.error(policyMessage + e); } else { LOGGER.info(queryFailedMessage, e); } } catch (Exception e) { LOGGER.warn(queryFailedMessage, e); } } } catch (AmazonServiceException ase) { if (ase.getStatusCode() == UNAUTHORIZED) { String policyMessage = "Could not get encryption keys because the user does not have enough permission."; LOGGER.error(policyMessage + ase); throw new CloudConnectorException(policyMessage, ase); } else { LOGGER.info(queryFailedMessage, ase); throw new CloudConnectorException(queryFailedMessage + ase.getMessage(), ase); } } catch (Exception e) { LOGGER.warn(queryFailedMessage, e); throw new CloudConnectorException(queryFailedMessage + e.getMessage(), e); } return cloudEncryptionKeys; }  @PostConstruct void init(); String resourceDefinition(String resource); @Override CloudNetworks networks(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudSshKeys sshKeys(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudSecurityGroups securityGroups(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override @Cacheable(cacheNames = "cloudResourceRegionCache", key = "{ #cloudCredential?.id, #availabilityZonesNeeded }") CloudRegions regions(CloudCredential cloudCredential, Region region, Map<String, String> filters, boolean availabilityZonesNeeded); void addDisplayName(Map<Region, String> displayNames, com.amazonaws.services.ec2.model.Region awsRegion); void addCoordinate(Map<Region, Coordinate> coordinates, com.amazonaws.services.ec2.model.Region awsRegion); @Override @Cacheable(cacheNames = "cloudResourceVmTypeCache", key = "#cloudCredential?.id + #region.getRegionName()") CloudVmTypes virtualMachines(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override @Cacheable(cacheNames = "cloudResourceVmTypeCache", key = "#cloudCredential?.id + #region.getRegionName() + 'distrox'") CloudVmTypes virtualMachinesForDistroX(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudGateWays gateways(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudIpPools publicIpPool(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudAccessConfigs accessConfigs(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudEncryptionKeys encryptionKeys(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudNoSqlTables noSqlTables(CloudCredential cloudCredential, Region region, Map<String, String> filters); @Override CloudResourceGroups resourceGroups(CloudCredential cloudCredential, Region region, Map<String, String> filters);  }