use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsPlatformResources method encryptionKeys.
@Override
public CloudEncryptionKeys encryptionKeys(ExtendedCloudCredential 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);
AmazonKmsClient 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 CloudUnauthorizedException(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;
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsPlatformResources method sshKeys.
@Override
public CloudSshKeys sshKeys(ExtendedCloudCredential cloudCredential, Region region, Map<String, String> filters) {
Map<String, Set<CloudSshKey>> result = new HashMap<>();
if (region != null && !Strings.isNullOrEmpty(region.value())) {
CloudRegions regions = regions(cloudCredential, region, new HashMap<>(), true);
for (Region actualRegion : regions.getCloudRegions().keySet()) {
// If region is provided then should filter for those region
if (regionMatch(actualRegion, region)) {
Set<CloudSshKey> cloudSshKeys = new HashSet<>();
AmazonEc2Client ec2Client = awsClient.createEc2Client(new AwsCredentialView(cloudCredential), actualRegion.value());
// create sshkey filter view
PlatformResourceSshKeyFilterView filter = new PlatformResourceSshKeyFilterView(filters);
DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
// If the filtervalue is provided then we should filter only for those securitygroups
if (!Strings.isNullOrEmpty(filter.getKeyName())) {
describeKeyPairsRequest.withKeyNames(filter.getKeyName());
}
for (KeyPairInfo keyPairInfo : ec2Client.describeKeyPairs(describeKeyPairsRequest).getKeyPairs()) {
Map<String, Object> properties = new HashMap<>();
properties.put("fingerPrint", keyPairInfo.getKeyFingerprint());
cloudSshKeys.add(new CloudSshKey(keyPairInfo.getKeyName(), properties));
}
result.put(actualRegion.value(), cloudSshKeys);
}
}
}
return new CloudSshKeys(result);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsPlatformResources method regions.
@Override
@Cacheable(cacheNames = "cloudResourceRegionCache", key = "{ #cloudCredential?.id, #availabilityZonesNeeded }")
public CloudRegions regions(ExtendedCloudCredential cloudCredential, Region region, Map<String, String> filters, boolean availabilityZonesNeeded) {
AmazonEc2Client ec2Client = awsClient.createEc2Client(new AwsCredentialView(cloudCredential));
Map<Region, List<AvailabilityZone>> regionListMap = new HashMap<>();
Map<Region, String> displayNames = new HashMap<>();
Map<Region, Coordinate> coordinates = new HashMap<>();
DescribeRegionsResult describeRegionsResult = describeRegionsResult(ec2Client);
String defaultRegion = awsDefaultZoneProvider.getDefaultZone(cloudCredential);
for (com.amazonaws.services.ec2.model.Region awsRegion : describeRegionsResult.getRegions()) {
if (!enabledRegions.contains(region(awsRegion.getRegionName()))) {
continue;
}
if (region == null || Strings.isNullOrEmpty(region.value()) || awsRegion.getRegionName().equals(region.value())) {
try {
fetchAZsIfNeeded(availabilityZonesNeeded, regionListMap, awsRegion, cloudCredential);
} catch (AmazonEC2Exception e) {
LOGGER.info("Failed to retrieve AZ from Region: {}!", awsRegion.getRegionName(), e);
}
addDisplayName(displayNames, awsRegion);
addCoordinate(coordinates, awsRegion);
}
}
if (region != null && !Strings.isNullOrEmpty(region.value())) {
defaultRegion = region.value();
}
return new CloudRegions(regionListMap, displayNames, coordinates, defaultRegion, true);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsPlatformResources method gateways.
@Override
public CloudGateWays gateways(ExtendedCloudCredential cloudCredential, Region region, Map<String, String> filters) {
Map<String, Set<CloudGateWay>> resultCloudGateWayMap = new HashMap<>();
if (region != null && !Strings.isNullOrEmpty(region.value())) {
CloudRegions regions = regions(cloudCredential, region, filters, true);
for (Entry<Region, List<AvailabilityZone>> regionListEntry : regions.getCloudRegions().entrySet()) {
if (regionListEntry.getKey().value().equals(region.value())) {
AmazonEc2Client ec2Client = awsClient.createEc2Client(new AwsCredentialView(cloudCredential), regionListEntry.getKey().value());
DescribeInternetGatewaysRequest describeInternetGatewaysRequest = new DescribeInternetGatewaysRequest();
DescribeInternetGatewaysResult describeInternetGatewaysResult = ec2Client.describeInternetGateways(describeInternetGatewaysRequest);
Set<CloudGateWay> gateWays = new HashSet<>();
for (InternetGateway internetGateway : describeInternetGatewaysResult.getInternetGateways()) {
CloudGateWay cloudGateWay = new CloudGateWay();
cloudGateWay.setId(internetGateway.getInternetGatewayId());
cloudGateWay.setName(internetGateway.getInternetGatewayId());
Collection<String> vpcs = new ArrayList<>();
for (InternetGatewayAttachment internetGatewayAttachment : internetGateway.getAttachments()) {
vpcs.add(internetGatewayAttachment.getVpcId());
}
Map<String, Object> properties = new HashMap<>();
properties.put("attachment", vpcs);
cloudGateWay.setProperties(properties);
gateWays.add(cloudGateWay);
}
for (AvailabilityZone availabilityZone : regionListEntry.getValue()) {
resultCloudGateWayMap.put(availabilityZone.value(), gateWays);
}
}
}
}
return new CloudGateWays(resultCloudGateWayMap);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsPlatformResources method accessConfigs.
@Override
public CloudAccessConfigs accessConfigs(ExtendedCloudCredential cloudCredential, Region region, Map<String, String> filters) {
CloudAccessConfigs cloudAccessConfigs = new CloudAccessConfigs(new HashSet<>());
AwsCredentialView awsCredentialView = new AwsCredentialView(cloudCredential);
AmazonIdentityManagementClient client = awsClient.createAmazonIdentityManagement(awsCredentialView);
String accessConfigType = filters.get(CloudParameterConst.ACCESS_CONFIG_TYPE);
Set<CloudAccessConfig> cloudAccessConfigSet;
if (AwsAccessConfigType.ROLE.name().equals(accessConfigType)) {
cloudAccessConfigSet = getAccessConfigByRole(client);
} else {
cloudAccessConfigSet = getAccessConfigByInstanceProfile(client);
}
cloudAccessConfigs.getCloudAccessConfigs().addAll(cloudAccessConfigSet);
return cloudAccessConfigs;
}
Aggregations