下面列出了com.amazonaws.services.s3.model.BucketVersioningConfiguration#com.amazonaws.regions.RegionUtils 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Fetch direct connections.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @return the map
*/
public static Map<String,List<Connection>> fetchDirectConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) {
Map<String,List<Connection>> connectionMap = new LinkedHashMap<>();
String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Direct Connections\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
AmazonDirectConnectClient directConnectClient = (AmazonDirectConnectClient) AmazonDirectConnectClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<Connection> connectionList = directConnectClient.describeConnections().getConnections();
if(!connectionList.isEmpty() ) {
log.debug("Account : " + accountId + " Type : Direct Connections "+ region.getName()+" >> " + connectionList.size());
connectionMap.put(accountId+delimiter+accountName+delimiter+region.getName(), connectionList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"directconnect",e.getMessage());
}
}
return connectionMap;
}
/**
* Fetch direct connections virtual interfaces.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @return the map
*/
public static Map<String,List<VirtualInterface>> fetchDirectConnectionsVirtualInterfaces(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) {
Map<String,List<VirtualInterface>> virtualInterfacesMap = new LinkedHashMap<>();
String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Direct Connections\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
AmazonDirectConnectClient directConnectClient = (AmazonDirectConnectClient) AmazonDirectConnectClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<VirtualInterface> virtualInterfacesList = directConnectClient.describeVirtualInterfaces().getVirtualInterfaces();
if(!virtualInterfacesList.isEmpty() ) {
log.debug("Account : " + accountId + " Type : Direct Connections "+ region.getName()+" >> " + virtualInterfacesList.size());
virtualInterfacesMap.put(accountId+delimiter+accountName+delimiter+region.getName(), virtualInterfacesList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"virtualinterface",e.getMessage());
}
}
return virtualInterfacesMap;
}
/**
* Fetch route tables.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @return the map
*/
public static Map<String,List<RouteTable>> fetchRouteTables(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
Map<String,List<RouteTable>> routeTableMap = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + InventoryConstants.ERROR_PREFIX_EC2 ;
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<RouteTable> routeTableList = ec2Client.describeRouteTables().getRouteTables();
if(!routeTableList.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Route table "+ region.getName()+" >> " + routeTableList.size());
routeTableMap.put(accountId+delimiter+accountName+delimiter+region.getName(), routeTableList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"routetable",e.getMessage());
}
}
return routeTableMap;
}
/**
* Fetch network ACL.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @return the map
*/
public static Map<String,List<NetworkAcl>> fetchNetworkACL(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
Map<String,List<NetworkAcl>> networkAclMap = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + InventoryConstants.ERROR_PREFIX_EC2 ;
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<NetworkAcl> networkAclList = ec2Client.describeNetworkAcls().getNetworkAcls();
if(!networkAclList.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Network Acl "+ region.getName()+" >> " + networkAclList.size());
networkAclMap.put(accountId+delimiter+accountName+delimiter+region.getName(), networkAclList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"networkacl",e.getMessage());
}
}
return networkAclMap;
}
/**
* Fetch elastic IP addresses.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @return the map
*/
public static Map<String,List<Address>> fetchElasticIPAddresses(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
Map<String,List<Address>> elasticIPMap = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + InventoryConstants.ERROR_PREFIX_EC2 ;
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<Address> elasticIPList = ec2Client.describeAddresses().getAddresses();
if(!elasticIPList.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Elastic IP "+ region.getName()+" >> " + elasticIPList.size());
elasticIPMap.put(accountId+delimiter+accountName+delimiter+region.getName(), elasticIPList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"elasticip",e.getMessage());
}
}
return elasticIPMap;
}
/**
* Fetch VPN gateway.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @return the map
*/
public static Map<String,List<VpnGateway>> fetchVPNGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
Map<String,List<VpnGateway>> vpnGatewayMap = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"vpngateway\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<VpnGateway> vpnGatewayList = ec2Client.describeVpnGateways().getVpnGateways();
if(!vpnGatewayList.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 VPN Gateway "+ region.getName()+" >> " + vpnGatewayList.size());
vpnGatewayMap.put(accountId+delimiter+accountName+delimiter+region.getName(), vpnGatewayList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"vpngateway",e.getMessage());
}
}
return vpnGatewayMap;
}
/**
* Fetch egress gateway.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @return the map
*/
public static Map<String,List<EgressOnlyInternetGateway>> fetchEgressGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
Map<String,List<EgressOnlyInternetGateway>> egressGatewayMap = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"egressgateway\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<EgressOnlyInternetGateway> egressGatewayList = ec2Client.describeEgressOnlyInternetGateways(new DescribeEgressOnlyInternetGatewaysRequest()).getEgressOnlyInternetGateways();
if(!egressGatewayList.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Egress Gateway "+ region.getName()+" >> " + egressGatewayList.size());
egressGatewayMap.put(accountId+delimiter+accountName+delimiter+region.getName(), egressGatewayList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"egressgateway",e.getMessage());
}
}
return egressGatewayMap;
}
/**
* Fetch DHCP options.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @return the map
*/
public static Map<String,List<DhcpOptions>> fetchDHCPOptions(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
Map<String,List<DhcpOptions>> dhcpOptionsMap = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"dhcpoption\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<DhcpOptions> dhcpOptionsList = ec2Client.describeDhcpOptions().getDhcpOptions();
if(!dhcpOptionsList.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 DHCP Options "+ region.getName()+" >> " + dhcpOptionsList.size());
dhcpOptionsMap.put(accountId+delimiter+accountName+delimiter+region.getName(), dhcpOptionsList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"dhcpoption",e.getMessage());
}
}
return dhcpOptionsMap;
}
/**
* Fetch peering connections.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @return the map
*/
public static Map<String,List<VpcPeeringConnection>> fetchPeeringConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
Map<String,List<VpcPeeringConnection>> peeringConnectionMap = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"peeringconnection\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<VpcPeeringConnection> peeringConnectionList = ec2Client.describeVpcPeeringConnections().getVpcPeeringConnections();
if(!peeringConnectionList.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Peering Connections "+ region.getName()+" >> " + peeringConnectionList.size());
peeringConnectionMap.put(accountId+delimiter+accountName+delimiter+region.getName(), peeringConnectionList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"peeringconnection",e.getMessage());
}
}
return peeringConnectionMap;
}
/**
* Fetch customer gateway.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @return the map
*/
public static Map<String,List<CustomerGateway>> fetchCustomerGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
Map<String,List<CustomerGateway>> customerGatewayMap = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"customergateway\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<CustomerGateway> customerGatewayList = ec2Client.describeCustomerGateways().getCustomerGateways();
if(!customerGatewayList.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Customer Gateway "+ region.getName()+" >> " + customerGatewayList.size());
customerGatewayMap.put(accountId+delimiter+accountName+delimiter+region.getName(), customerGatewayList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"customergateway",e.getMessage());
}
}
return customerGatewayMap;
}
/**
* Fetch VPN connections.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @return the map
*/
public static Map<String,List<VpnConnection>> fetchVPNConnections(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
Map<String,List<VpnConnection>> vpnConnectionMap = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"vpnconnection\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<VpnConnection> vpnConnectionsList = ec2Client.describeVpnConnections().getVpnConnections();
if(!vpnConnectionsList.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 VPN Connections"+ region.getName()+" >> " + vpnConnectionsList.size());
vpnConnectionMap.put(accountId+delimiter+accountName+delimiter+region.getName(), vpnConnectionsList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"vpnconnection",e.getMessage());
}
}
return vpnConnectionMap;
}
/**
* Fetch network intefaces.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @param accountName the account name
* @return the map
*/
public static Map<String,List<NetworkInterface>> fetchNetworkIntefaces(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
Map<String,List<NetworkInterface>> niMap = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Network Interface\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
DescribeNetworkInterfacesResult descNIRslt = ec2Client.describeNetworkInterfaces();
List<NetworkInterface> niList = descNIRslt.getNetworkInterfaces();
if(!niList.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : Network Interface " +region.getName()+" >> " + niList.size());
niMap.put(accountId+delimiter+accountName+delimiter+region.getName(),niList);
}
}
}catch(Exception e){
log.error("Exception fetching Network Interfaces for "+region.getName() + e);
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"eni",e.getMessage());
}
}
return niMap;
}
/**
* Fetch security groups.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @param accountName the account name
* @return the map
*/
public static Map<String,List<SecurityGroup>> fetchSecurityGroups(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
log.info("skipRegionseee" + skipRegions);
Map<String,List<SecurityGroup>> secGrpList = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Security Group\" , \"region\":\"" ;
log.info("sgregion" + RegionUtils.getRegions().toString());
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
DescribeSecurityGroupsResult rslt = ec2Client.describeSecurityGroups();
List<SecurityGroup> secGrpListTemp = rslt.getSecurityGroups();
if( !secGrpListTemp.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId +" Type : Security Group "+region.getName()+" >> " + secGrpListTemp.size());
secGrpList.put(accountId+delimiter+accountName+delimiter+region.getName(),secGrpListTemp);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"sg",e.getMessage());
}
}
return secGrpList;
}
/**
* Helper method to create Elasticsearch cluster at set correct endpoint.
*/
private void createElasticsearchCluster() {
// Create stack if not already up
AmazonCloudFormation cloudFormationClient = new AmazonCloudFormationClient(config.AWS_CREDENTIALS_PROVIDER);
cloudFormationClient.setRegion(RegionUtils.getRegion(config.REGION_NAME));
CloudFormationUtils.createStackIfNotExists(cloudFormationClient, config);
// Update the elasticsearch endpoint to use endpoint in created cluster
AmazonEC2 ec2Client = new AmazonEC2Client(config.AWS_CREDENTIALS_PROVIDER);
ec2Client.setRegion(RegionUtils.getRegion(config.REGION_NAME));
config.ELASTICSEARCH_ENDPOINT =
EC2Utils.getEndpointForFirstActiveInstanceWithTag(ec2Client,
EC2_ELASTICSEARCH_FILTER_NAME,
EC2_ELASTICSEARCH_FILTER_VALUE);
if (config.ELASTICSEARCH_ENDPOINT == null || config.ELASTICSEARCH_ENDPOINT.isEmpty()) {
throw new RuntimeException("Could not find active Elasticsearch endpoint from cluster.");
}
}
/**
* Fetch NAT gateway info.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @param accountName the account name
* @return the map
*/
public static Map<String,List<NatGateway>> fetchNATGatewayInfo(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
Map<String,List<NatGateway>> natGatwayMap = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Nat Gateway\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()){
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
DescribeNatGatewaysResult rslt = ec2Client.describeNatGateways(new DescribeNatGatewaysRequest());
List<NatGateway> natGatwayList =rslt.getNatGateways();
if(! natGatwayList.isEmpty() ){
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : Nat Gateway "+region.getName() + " >> "+natGatwayList.size());
natGatwayMap.put(accountId+delimiter+accountName+delimiter+region.getName(), natGatwayList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"nat",e.getMessage());
}
}
return natGatwayMap;
}
/** Returns an S3 client given the configuration **/
public static AmazonS3Client getS3Client(Map conf) {
AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain();
AWSCredentials credentials = provider.getCredentials();
ClientConfiguration config = new ClientConfiguration();
AmazonS3Client client = new AmazonS3Client(credentials, config);
String regionName = ConfUtils.getString(conf, REGION);
if (StringUtils.isNotBlank(regionName)) {
client.setRegion(RegionUtils.getRegion(regionName));
}
String endpoint = ConfUtils.getString(conf, ENDPOINT);
if (StringUtils.isNotBlank(endpoint)) {
client.setEndpoint(endpoint);
}
return client;
}
/**
* Fetch snapshots.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @param accountName the account name
* @return the map
*/
public static Map<String,List<Snapshot>> fetchSnapshots(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) {
Map<String,List<Snapshot>> snapShots = new LinkedHashMap<>();
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Snapshot\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()){
try{
if(!skipRegions.contains(region.getName())){
AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<Snapshot> snapShotsList = ec2Client.describeSnapshots(new DescribeSnapshotsRequest().withOwnerIds(accountId)).getSnapshots();// No need to paginate as all results will be returned
if( !snapShotsList.isEmpty() ){
log.debug(InventoryConstants.ACCOUNT + accountId +" Type : Snapshot " +region.getName() + " >> "+snapShotsList.size());
snapShots.put(accountId+delimiter+accountName+delimiter+region.getName(),snapShotsList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"snapshot",e.getMessage());
}
}
return snapShots;
}
/**
* Fetch CloudTrails info.
*
* @param temporaryCredentials the temporary credentials
* @param account the account
* @return the map
*/
public static Map<String,List<Trail>> fetchCloudTrails(BasicSessionCredentials temporaryCredentials, String skipRegions,String account, String accountName){
log.info("Fetch CloudTrails info start");
Map<String,List<Trail>> cloudTrails = new LinkedHashMap<>();
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+account + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Cloud Trail\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()){
try{
if(!skipRegions.contains(region.getName())){
AWSCloudTrail cloudTrailClient = AWSCloudTrailClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
DescribeTrailsResult rslt = cloudTrailClient.describeTrails();
List<Trail> trailTemp = rslt.getTrailList();
if(! trailTemp.isEmpty() ){
cloudTrails.put(account+delimiter+accountName+delimiter+region.getName(), trailTemp);
}
}
}catch(Exception e){
if(region.isServiceSupported(AmazonRDS.ENDPOINT_PREFIX)){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(account,region.getName(),"cloudtrail",e.getMessage());
}
}
}
return cloudTrails;
}
public GenericDynamoDB(AmazonDynamoDB client, AWSCredentialsProvider awsCredentials,
ClientConfiguration clientConfiguration,
SecretsGroupIdentifier groupIdentifier, Class<Entry> clazz, Converters converters,
ReadWriteLock readWriteLock) {
this.clazz = clazz;
buildMappings();
this.converters = converters;
this.awsCredentials = awsCredentials;
this.clientConfiguration = clientConfiguration;
this.client = client;
this.region = RegionUtils.getRegion(groupIdentifier.region.getName());
this.readWriteLock = readWriteLock;
RegionLocalResourceName resourceName = new RegionLocalResourceName(groupIdentifier);
this.tableName = resourceName.toString();
}
/**
* Calculates DynamoDB end-point.
*
* Algorithm details:
* <ol>
* <li> Use endpoint in job configuration "dynamodb.endpoint" value if available
* <li> Use endpoint from region in job configuration "dynamodb.region" value if available
* <li> Use endpoint from region in job configuration "dynamodb.regionid" value if available
* <li> Use endpoint from EC2 Metadata of instance if available
* <li> If all previous attempts at retrieving endpoint fail, default to us-east-1 endpoint
* </ol>
*
* @param conf Job Configuration
* @param region optional preferred region
* @return end-point for DynamoDb service
*/
public static String getDynamoDBEndpoint(Configuration conf, String region) {
String endpoint = getValueFromConf(conf, DynamoDBConstants.ENDPOINT);
if (Strings.isNullOrEmpty(endpoint)) {
if (Strings.isNullOrEmpty(region)) {
region = getValueFromConf(conf, DynamoDBConstants.REGION);
}
if (Strings.isNullOrEmpty(region)) {
region = getValueFromConf(conf, DynamoDBConstants.REGION_ID);
}
if (Strings.isNullOrEmpty(region)) {
try {
region = EC2MetadataUtils.getEC2InstanceRegion();
} catch (Exception e) {
log.warn(String.format("Exception when attempting to get AWS region information. Will "
+ "ignore and default " + "to %s", DynamoDBConstants.DEFAULT_AWS_REGION), e);
}
}
if (Strings.isNullOrEmpty(region)) {
region = DynamoDBConstants.DEFAULT_AWS_REGION;
}
endpoint = RegionUtils.getRegion(region).getServiceEndpoint(ServiceAbbreviations.Dynamodb);
}
log.info("Using endpoint for DynamoDB: " + endpoint);
return endpoint;
}
/**
* AWS Access Key ID [None]: ACESSKEY
AWS Secret Access Key [None]: EXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: ENTER
*/
@Test
public void testConnection() {
// ClientConfiguration configuration = new ClientConfiguration();
// configuration.set
amazonECR.setRegion(RegionUtils.getRegion("us-west-2"));
GetAuthorizationTokenResult authorizationToken = amazonECR.getAuthorizationToken(new GetAuthorizationTokenRequest());
log.info("authorizationToken: {}", authorizationToken);
List<AuthorizationData> authorizationData = authorizationToken.getAuthorizationData();
log.info("token: {}", authorizationData.get(0).getAuthorizationToken());
log.info("endpoint: {}", authorizationData.get(0).getProxyEndpoint());
// amazonECR.setEndpoint(authorizationData.get(0).getProxyEndpoint());
// BatchGetImageResult batchGetImageResult = amazonECR.batchGetImage(new BatchGetImageRequest());
// log.info("batchGetImageResult {}", batchGetImageResult);
}
@ReactMethod
public void initCredentialsProvider(String identityPoolId, String token, String region)
{
RegionUtils regionUtils = new RegionUtils();
Region awsRegion = regionUtils.getRegion(region);
cognitoCredentialsProvider = new CognitoCachingCredentialsProvider(
mActivityContext.getApplicationContext(),
identityPoolId,
// awsRegion);
Regions.EU_WEST_1);
cognitoClient = new CognitoSyncManager(
mActivityContext.getApplicationContext(),
// awsRegion,
Regions.EU_WEST_1,
cognitoCredentialsProvider);
}
private DescribeAvailabilityZonesRequest getDescribeAvailabilityZonesRequest(AmazonEC2Client ec2Client, com.amazonaws.services.ec2.model.Region awsRegion) {
DescribeAvailabilityZonesRequest describeAvailabilityZonesRequest = new DescribeAvailabilityZonesRequest();
ec2Client.setRegion(RegionUtils.getRegion(awsRegion.getRegionName()));
Filter filter = new Filter();
filter.setName("region-name");
Collection<String> list = new ArrayList<>();
list.add(awsRegion.getRegionName());
filter.setValues(list);
describeAvailabilityZonesRequest.withFilters(filter);
return describeAvailabilityZonesRequest;
}
protected Region region() {
String az = null;
if (isEC2MetadataAvailable()) {
az = EC2MetadataUtils.getAvailabilityZone();
}
String regionName = awsRegion;
if (!Strings.isNullOrEmpty(az)) {
regionName = az.substring(0, az.length() - 1); // strip the AZ letter
}
return RegionUtils.getRegion(regionName);
}
public AmazonCloudFormationClient createCloudFormationClient(AwsCredentialView awsCredential, String regionName) {
AmazonCloudFormationClient client = isRoleAssumeRequired(awsCredential) ?
new AmazonCloudFormationClient(createAwsSessionCredentialProvider(awsCredential), getDefaultClientConfiguration()) :
new AmazonCloudFormationClient(createAwsCredentials(awsCredential), getDefaultClientConfiguration());
client.setRegion(RegionUtils.getRegion(regionName));
return client;
}
/**
* Fetch SSM info.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @return the map
*/
public static Map<String,List<InstanceInformation>> fetchSSMInfo(BasicSessionCredentials temporaryCredentials, String skipRegions, String accountId,String accountName) {
Map<String,List<InstanceInformation>> ssmInstanceList = new LinkedHashMap<>();
AWSSimpleSystemsManagement ssmClient;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE + accountId
+ "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"SSM\" , \"region\":\"";
List<InstanceInformation> ssmInstanceListTemp ;
for (Region region : RegionUtils.getRegions()) {
try {
if (!skipRegions.contains(region.getName())) {
ssmInstanceListTemp = new ArrayList<>();
ssmClient = AWSSimpleSystemsManagementClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials))
.withRegion(region.getName()).build();
String nextToken = null;
DescribeInstanceInformationResult describeInstanceInfoRslt;
do {
describeInstanceInfoRslt = ssmClient.describeInstanceInformation(
new DescribeInstanceInformationRequest().withNextToken(nextToken));
nextToken = describeInstanceInfoRslt.getNextToken();
ssmInstanceListTemp.addAll(describeInstanceInfoRslt
.getInstanceInformationList());
} while (nextToken != null);
if(! ssmInstanceListTemp.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : SSM "+region.getName() + " >> "+ssmInstanceListTemp.size());
ssmInstanceList.put(accountId+delimiter+accountName+delimiter+region.getName(), ssmInstanceListTemp);
}
}
} catch (Exception e) {
log.warn(expPrefix + region.getName() + InventoryConstants.ERROR_CAUSE + e.getMessage() + "\"}");
ErrorManageUtil.uploadError(accountId, region.getName(), "SSM", e.getMessage());
}
}
return ssmInstanceList;
}
/**
* Fetch instances.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @param accountName the account name
* @param ec2Filters the ec 2 filters
* @return the map
*/
public static Map<String,List<Instance>> fetchInstances(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName,String ec2Filters){
Map<String,List<Instance>> instanceMap = new LinkedHashMap<>();
AmazonEC2 ec2Client ;
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"EC2\" , \"region\":\"" ;
List<String> stateNameFilters = Arrays.asList(ec2Filters.split(","));
for(Region region : RegionUtils.getRegions()) {
try{
if(!skipRegions.contains(region.getName())){
ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<Instance> instanceList = new ArrayList<>();
DescribeInstancesResult descInstResult ;
String nextToken = null;
do{
descInstResult = ec2Client.describeInstances(new DescribeInstancesRequest().withNextToken(nextToken));
descInstResult.getReservations().forEach(
reservation -> instanceList.addAll(reservation.getInstances().stream().filter(instance->stateNameFilters.contains(instance.getState().getName())).collect(Collectors.toList())));
nextToken = descInstResult.getNextToken();
}while(nextToken!=null);
if(!instanceList.isEmpty() ) {
log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 "+ region.getName()+" >> " + instanceList.size());
instanceMap.put(accountId+delimiter+accountName+delimiter+region.getName(), instanceList);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"ec2",e.getMessage());
}
}
return instanceMap;
}
/**
* Fetch cloud formation stack.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @param accountName the account name
* @return the map
*/
public static Map<String,List<Stack>> fetchCloudFormationStack(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
AmazonCloudFormation cloudFormClient ;
Map<String,List<Stack>> stacks = new LinkedHashMap<>();
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Stack\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()){
try{
if(!skipRegions.contains(region.getName())){
List<Stack> stacksTemp = new ArrayList<>();
String nextToken = null;
cloudFormClient = AmazonCloudFormationClientBuilder.standard().
withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
DescribeStacksResult describeResult ;
do{
describeResult = cloudFormClient.describeStacks(new DescribeStacksRequest().withNextToken(nextToken));
stacksTemp.addAll(describeResult.getStacks());
nextToken = describeResult.getNextToken();
}while(nextToken!=null);
if(! stacksTemp.isEmpty() ){
log.debug(InventoryConstants.ACCOUNT + accountId +" Type : Cloud Formation Stack "+region.getName() + " >> " + stacksTemp.size());
stacks.put(accountId+delimiter+accountName+delimiter+region.getName(), stacksTemp);
}
}
}catch(Exception e){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"stack",e.getMessage());
}
}
return stacks;
}
/**
* Deletes the input stream specified by config.KINESIS_INPUT_STREAM
*
* @param config
* The configuration containing the stream name and {@link AWSCredentialsProvider}
*/
public static void deleteInputStream(KinesisConnectorConfiguration config) {
AmazonKinesisClient kinesisClient = new AmazonKinesisClient(config.AWS_CREDENTIALS_PROVIDER);
kinesisClient.setRegion(RegionUtils.getRegion(config.REGION_NAME));
if (config.KINESIS_ENDPOINT != null) {
kinesisClient.setEndpoint(config.KINESIS_ENDPOINT);
}
deleteStream(kinesisClient, config.KINESIS_INPUT_STREAM);
}
/**
* Fetch EMR info.
*
* @param temporaryCredentials the temporary credentials
* @param skipRegions the skip regions
* @param accountId the accountId
* @param accountName the account name
* @return the map
*/
public static Map<String,List<Cluster>> fetchEMRInfo(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
Map<String,List<Cluster>> clusterList = new LinkedHashMap<>();
String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"EMR\" , \"region\":\"" ;
for(Region region : RegionUtils.getRegions()){
try{
if(!skipRegions.contains(region.getName())){
AmazonElasticMapReduce emrClient = AmazonElasticMapReduceClientBuilder.standard().
withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
List<ClusterSummary> clusters = new ArrayList<>();
String marker = null;
ListClustersResult clusterResult ;
do{
clusterResult = emrClient.listClusters(new ListClustersRequest().withMarker(marker));
clusters.addAll(clusterResult.getClusters());
marker = clusterResult.getMarker();
}while(marker!=null);
List<Cluster> clustersList = new ArrayList<>();
clusters.forEach(cluster ->
{
DescribeClusterResult descClstrRslt = emrClient.describeCluster(new DescribeClusterRequest().withClusterId(cluster.getId()));
clustersList.add(descClstrRslt.getCluster());
});
if( !clustersList.isEmpty() ){
log.debug(InventoryConstants.ACCOUNT + accountId +" Type : EMR "+region.getName() + " >> "+clustersList.size());
clusterList.put(accountId+delimiter+accountName+delimiter+region.getName(),clustersList);
}
}
}catch(Exception e){
if(region.isServiceSupported(AmazonElasticMapReduce.ENDPOINT_PREFIX)){
log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
ErrorManageUtil.uploadError(accountId,region.getName(),"emr",e.getMessage());
}
}
}
return clusterList;
}