com.amazonaws.auth.AWSStaticCredentialsProvider Java Examples

The following examples show how to use com.amazonaws.auth.AWSStaticCredentialsProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CredentialProvider.java    From pacbot with Apache License 2.0 7 votes vote down vote up
/**
 * Gets the credentials.
 *
 * @param account the account
 * @param roleName the role name
 * @return the credentials
 */
public  BasicSessionCredentials getCredentials(String account,String roleName){
	
	BasicSessionCredentials baseAccntCreds = getBaseAccountCredentials(baseAccount,baseRegion,roleName);
	if(baseAccount.equals(account)){
		return baseAccntCreds;
	}
	AWSSecurityTokenServiceClientBuilder stsBuilder = AWSSecurityTokenServiceClientBuilder.standard().withCredentials( new AWSStaticCredentialsProvider(baseAccntCreds)).withRegion(baseRegion);
	AWSSecurityTokenService stsClient = stsBuilder.build();
    AssumeRoleRequest assumeRequest = new AssumeRoleRequest().withRoleArn(getRoleArn(account,roleName)).withRoleSessionName("pic-ro-"+account);
    AssumeRoleResult assumeResult = stsClient.assumeRole(assumeRequest);
    return  new BasicSessionCredentials(
            assumeResult.getCredentials()
                        .getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(),
            assumeResult.getCredentials().getSessionToken());
}
 
Example #2
Source File: EC2InventoryUtilTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch SSM info test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchSSMInfoTest() throws Exception {
    
    mockStatic(AWSSimpleSystemsManagementClientBuilder.class);
    AWSSimpleSystemsManagement ssmClient = PowerMockito.mock(AWSSimpleSystemsManagement.class);
    AWSSimpleSystemsManagementClientBuilder simpleSystemsManagementClientBuilder = PowerMockito.mock(AWSSimpleSystemsManagementClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(simpleSystemsManagementClientBuilder.standard()).thenReturn(simpleSystemsManagementClientBuilder);
    when(simpleSystemsManagementClientBuilder.withCredentials(anyObject())).thenReturn(simpleSystemsManagementClientBuilder);
    when(simpleSystemsManagementClientBuilder.withRegion(anyString())).thenReturn(simpleSystemsManagementClientBuilder);
    when(simpleSystemsManagementClientBuilder.build()).thenReturn(ssmClient);
    
    DescribeInstanceInformationResult describeInstanceInfoRslt = new DescribeInstanceInformationResult();
    List<InstanceInformation> ssmInstanceListTemp = new ArrayList<>();
    ssmInstanceListTemp.add(new InstanceInformation());
    describeInstanceInfoRslt.setInstanceInformationList(ssmInstanceListTemp);
    when(ssmClient.describeInstanceInformation(anyObject())).thenReturn(describeInstanceInfoRslt);
    assertThat(ec2InventoryUtil.fetchSSMInfo(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
Example #3
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch NAT gateway info test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchNATGatewayInfoTest() throws Exception {
    
    mockStatic(AmazonEC2ClientBuilder.class);
    AmazonEC2 ec2Client = PowerMockito.mock(AmazonEC2.class);
    AmazonEC2ClientBuilder amazonEC2ClientBuilder = PowerMockito.mock(AmazonEC2ClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonEC2ClientBuilder.standard()).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withCredentials(anyObject())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withRegion(anyString())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.build()).thenReturn(ec2Client);
    
    DescribeNatGatewaysResult describeNatGatewaysResult = new DescribeNatGatewaysResult();
    List<NatGateway> natGatwayList = new ArrayList<>();
    natGatwayList.add(new NatGateway());
    describeNatGatewaysResult.setNatGateways(natGatwayList);
    when(ec2Client.describeNatGateways(anyObject())).thenReturn(describeNatGatewaysResult);
    assertThat(inventoryUtil.fetchNATGatewayInfo(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
Example #4
Source File: TestPrestoS3FileSystem.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testAssumeRoleStaticCredentials()
        throws Exception
{
    Configuration config = new Configuration(false);
    config.set(S3_ACCESS_KEY, "test_access_key");
    config.set(S3_SECRET_KEY, "test_secret_key");
    config.set(S3_IAM_ROLE, "test_role");

    try (PrestoS3FileSystem fs = new PrestoS3FileSystem()) {
        fs.initialize(new URI("s3n://test-bucket/"), config);
        AWSCredentialsProvider tokenService = getStsCredentialsProvider(fs, "test_role");
        assertInstanceOf(tokenService, AWSStaticCredentialsProvider.class);

        AWSCredentials credentials = tokenService.getCredentials();
        assertEquals(credentials.getAWSAccessKeyId(), "test_access_key");
        assertEquals(credentials.getAWSSecretKey(), "test_secret_key");
    }
}
 
Example #5
Source File: FileClient.java    From file-service with Apache License 2.0 6 votes vote down vote up
private void initAmazonS3() {
    BasicAWSCredentials credentials = new BasicAWSCredentials(
            fileClientConfig.getAccessKey(), fileClientConfig.getSecretKey());
    ClientConfiguration clientConfig = new ClientConfiguration();
    clientConfig.setSignerOverride("S3SignerType");
    String region = fileClientConfig.getRegion() == null ? FileClientConfiguration.US_EAST_1 : fileClientConfig.getRegion();
    this.amazonS3 = AmazonS3ClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(credentials))
            .withClientConfiguration(clientConfig)
            .withEndpointConfiguration(
                    new AwsClientBuilder.EndpointConfiguration(
                            fileClientConfig.getEndpoint(),
                            region))
            .withPathStyleAccessEnabled(fileClientConfig.getWithPath())
            .build();

}
 
Example #6
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch S 3 info test test exception.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchS3InfoTestTest_Exception() throws Exception {
    
    mockStatic(AmazonS3ClientBuilder.class);
    AmazonS3 amazonS3Client = PowerMockito.mock(AmazonS3.class);
    AmazonS3ClientBuilder amazonRDSClientBuilder = PowerMockito.mock(AmazonS3ClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonRDSClientBuilder.standard()).thenReturn(amazonRDSClientBuilder);
    when(amazonRDSClientBuilder.withCredentials(anyObject())).thenReturn(amazonRDSClientBuilder);
    when(amazonRDSClientBuilder.withRegion(anyString())).thenReturn(amazonRDSClientBuilder);
    when(amazonRDSClientBuilder.build()).thenReturn(amazonS3Client);
    
    List<Bucket> s3buckets = new ArrayList<>();
    Bucket bucket = new Bucket();
    bucket.setName("name");
    s3buckets.add(bucket);
    when(amazonS3Client.listBuckets()).thenReturn(s3buckets);
    
    when(amazonS3Client.getBucketLocation(anyString())).thenThrow(new AmazonServiceException("Error"));
    assertThat(inventoryUtil.fetchS3Info(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(0));
}
 
Example #7
Source File: InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch volumet 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<Volume>> fetchVolumetInfo(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) {
	Map<String,List<Volume>> volumeList = new LinkedHashMap<>();
	AmazonEC2 ec2Client ;
	String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Volume\" , \"region\":\"" ;
	for(Region region : RegionUtils.getRegions()){
		try{
			if(!skipRegions.contains(region.getName())){
				ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				DescribeVolumesResult  rslt = ec2Client.describeVolumes(); // No need to paginate as all volumes will be returned.
				List<Volume> volumeListTemp = rslt.getVolumes();

				if( !volumeListTemp.isEmpty() ) {
					log.debug(InventoryConstants.ACCOUNT + accountId +" Type : Volume "+region.getName() + " >> "+volumeListTemp.size());
					volumeList.put(accountId+delimiter+accountName+delimiter+region.getName(),volumeListTemp);
				}
			}

		}catch(Exception e){
			log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
			ErrorManageUtil.uploadError(accountId,region.getName(),"volume",e.getMessage());
		}
	}
	return volumeList;
}
 
Example #8
Source File: CredentialProvider.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the credentials.
 *
 * @param account the account
 * @param roleName the role name
 * @return the credentials
 */
public  BasicSessionCredentials getCredentials(String account,String roleName){
	BasicSessionCredentials baseAccntCreds = getBaseAccountCredentials(roleName);
	if(baseAccount.equals(account)){
		return baseAccntCreds;
	}
	AWSSecurityTokenServiceClientBuilder stsBuilder = AWSSecurityTokenServiceClientBuilder.standard().withCredentials( new AWSStaticCredentialsProvider(baseAccntCreds)).withRegion(baseRegion);
	AWSSecurityTokenService stsClient = stsBuilder.build();
    AssumeRoleRequest assumeRequest = new AssumeRoleRequest().withRoleArn(getRoleArn(account,roleName)).withRoleSessionName("pic-ro-"+account);
    AssumeRoleResult assumeResult = stsClient.assumeRole(assumeRequest);
    return  new BasicSessionCredentials(
            assumeResult.getCredentials()
                        .getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(),
            assumeResult.getCredentials().getSessionToken());
}
 
Example #9
Source File: InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #10
Source File: InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch subnets.
 *
 * @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<Subnet>> fetchSubnets(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) {
	Map<String,List<Subnet>> subnets = new LinkedHashMap<>();
	AmazonEC2 ec2Client ;
	String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Subnet\" , \"region\":\"" ;
	for(Region region : RegionUtils.getRegions()){
		try{
			if(!skipRegions.contains(region.getName())){
				ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				DescribeSubnetsResult rslt = ec2Client.describeSubnets();
				List<Subnet> subnetsTemp =rslt.getSubnets();
				if(! subnetsTemp.isEmpty() ){
					log.debug(InventoryConstants.ACCOUNT + accountId +" Type : Subnet "+region.getName() + " >> "+subnetsTemp.size());
					subnets.put(accountId+delimiter+accountName+delimiter+region.getName(),subnetsTemp);
				}

			}
		}catch(Exception e){
			log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
			ErrorManageUtil.uploadError(accountId,region.getName(),"subnet",e.getMessage());
		}
	}

	return subnets;
}
 
Example #11
Source File: EC2InventoryUtilTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch route tables test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchRouteTablesTest() throws Exception {
    
    mockStatic(AmazonEC2ClientBuilder.class);
    AmazonEC2 ec2Client = PowerMockito.mock(AmazonEC2.class);
    AmazonEC2ClientBuilder amazonEC2ClientBuilder = PowerMockito.mock(AmazonEC2ClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonEC2ClientBuilder.standard()).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withCredentials(anyObject())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withRegion(anyString())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.build()).thenReturn(ec2Client);
    
    DescribeRouteTablesResult describeRouteTablesResult = new DescribeRouteTablesResult();
    List<RouteTable> routeTableList = new ArrayList<>();
    routeTableList.add(new RouteTable());
    describeRouteTablesResult.setRouteTables(routeTableList);
    when(ec2Client.describeRouteTables()).thenReturn(describeRouteTablesResult);
    assertThat(ec2InventoryUtil.fetchRouteTables(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
Example #12
Source File: EC2InventoryUtilTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch internet gateway test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchInternetGatewayTest() throws Exception {
    
    mockStatic(AmazonEC2ClientBuilder.class);
    AmazonEC2 ec2Client = PowerMockito.mock(AmazonEC2.class);
    AmazonEC2ClientBuilder amazonEC2ClientBuilder = PowerMockito.mock(AmazonEC2ClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonEC2ClientBuilder.standard()).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withCredentials(anyObject())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withRegion(anyString())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.build()).thenReturn(ec2Client);
    
    DescribeInternetGatewaysResult describeInternetGatewaysResult = new DescribeInternetGatewaysResult();
    List<InternetGateway> internetGatewayList = new ArrayList<>();
    internetGatewayList.add(new InternetGateway());
    describeInternetGatewaysResult.setInternetGateways(internetGatewayList);
    when(ec2Client.describeInternetGateways()).thenReturn(describeInternetGatewaysResult);
    assertThat(ec2InventoryUtil.fetchInternetGateway(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
Example #13
Source File: EC2InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch reserved instances.
 *
 * @param temporaryCredentials the temporary credentials
 * @param skipRegions the skip regions
 * @param accountId the accountId
 * @return the map
 */
public static Map<String,List<ReservedInstances>> fetchReservedInstances(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
	
	Map<String,List<ReservedInstances>> reservedInstancesMap = 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<ReservedInstances> reservedInstancesList = ec2Client.describeReservedInstances().getReservedInstances();
				if(!reservedInstancesList.isEmpty() ) {
					log.debug(InventoryConstants.ACCOUNT + accountId + " Type : reservedinstance"+ region.getName()+" >> " + reservedInstancesList.size());
					reservedInstancesMap.put(accountId+delimiter+accountName+delimiter+region.getName(), reservedInstancesList);
				}
		   	}
		}catch(Exception e){
	   		log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
	   		ErrorManageUtil.uploadError(accountId,region.getName(),"reservedinstance",e.getMessage());
	   	}
	}
	return reservedInstancesMap;
}
 
Example #14
Source File: EC2InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #15
Source File: AmazonWebServicesClientProxy.java    From cloudformation-cli-java-plugin with Apache License 2.0 6 votes vote down vote up
public AmazonWebServicesClientProxy(final boolean inHandshakeMode,
                                    final LoggerProxy loggerProxy,
                                    final Credentials credentials,
                                    final Supplier<Long> remainingTimeToExecute,
                                    final DelayFactory override) {
    this.inHandshakeMode = inHandshakeMode;
    this.loggerProxy = loggerProxy;
    this.remainingTimeInMillis = remainingTimeToExecute;

    BasicSessionCredentials basicSessionCredentials = new BasicSessionCredentials(credentials.getAccessKeyId(),
                                                                                  credentials.getSecretAccessKey(),
                                                                                  credentials.getSessionToken());
    this.v1CredentialsProvider = new AWSStaticCredentialsProvider(basicSessionCredentials);

    AwsSessionCredentials awsSessionCredentials = AwsSessionCredentials.create(credentials.getAccessKeyId(),
        credentials.getSecretAccessKey(), credentials.getSessionToken());
    this.v2CredentialsProvider = StaticCredentialsProvider.create(awsSessionCredentials);
    this.override = Objects.requireNonNull(override);
}
 
Example #16
Source File: DirectConnectionInventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #17
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch volumet info test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchVolumetInfoTest() throws Exception {
    
    mockStatic(AmazonEC2ClientBuilder.class);
    AmazonEC2 ec2Client = PowerMockito.mock(AmazonEC2.class);
    AmazonEC2ClientBuilder amazonEC2ClientBuilder = PowerMockito.mock(AmazonEC2ClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonEC2ClientBuilder.standard()).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withCredentials(anyObject())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withRegion(anyString())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.build()).thenReturn(ec2Client);
    
    DescribeVolumesResult describeVolumesResult = new DescribeVolumesResult();
    List<Volume> volumeList = new ArrayList<>();
    volumeList.add(new Volume());
    describeVolumesResult.setVolumes(volumeList);
    when(ec2Client.describeVolumes()).thenReturn(describeVolumesResult);
    assertThat(inventoryUtil.fetchVolumetInfo(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
Example #18
Source File: InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the default root object.
 *
 * @param temporaryCredentials the temporary credentials
 * @param cloudFrontList the cloud front list
 */
private static void setConfigDetails(BasicSessionCredentials temporaryCredentials, List<CloudFrontVH> cloudFrontList){

	String[] regions = {"us-east-2","us-west-1"};
	int index = 0;
	AmazonCloudFront amazonCloudFront = AmazonCloudFrontClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(regions[index]).build();
	for(CloudFrontVH cfVH: cloudFrontList){
		try{
			DistributionConfig distConfig = amazonCloudFront.getDistributionConfig(new GetDistributionConfigRequest().withId(cfVH.getDistSummary().getId())).getDistributionConfig();
			cfVH.setDefaultRootObject(distConfig.getDefaultRootObject());
			cfVH.setBucketName(distConfig.getLogging().getBucket());
			cfVH.setAccessLogEnabled(distConfig.getLogging().getEnabled());
		}catch(Exception e){
			index = index==0?1:0;
			amazonCloudFront = AmazonCloudFrontClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(regions[index]).build();
		}
	}
}
 
Example #19
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch RDSDB snapshots test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchRDSDBSnapshotsTest() throws Exception {
    
    mockStatic(AmazonRDSClientBuilder.class);
    AmazonRDS rdsClient = PowerMockito.mock(AmazonRDS.class);
    AmazonRDSClientBuilder amazonRDSClientBuilder = PowerMockito.mock(AmazonRDSClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonRDSClientBuilder.standard()).thenReturn(amazonRDSClientBuilder);
    when(amazonRDSClientBuilder.withCredentials(anyObject())).thenReturn(amazonRDSClientBuilder);
    when(amazonRDSClientBuilder.withRegion(anyString())).thenReturn(amazonRDSClientBuilder);
    when(amazonRDSClientBuilder.build()).thenReturn(rdsClient);
    
    DescribeDBSnapshotsResult describeDBSnapshotsResult = new DescribeDBSnapshotsResult();
    List<DBSnapshot> snapshots = new ArrayList<>();
    snapshots.add(new DBSnapshot());
    describeDBSnapshotsResult.setDBSnapshots(snapshots);
    when(rdsClient.describeDBSnapshots(anyObject())).thenReturn(describeDBSnapshotsResult);
    assertThat(inventoryUtil.fetchRDSDBSnapshots(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
Example #20
Source File: EC2InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #21
Source File: EC2InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch internet gateway.
 *
 * @param temporaryCredentials the temporary credentials
 * @param skipRegions the skip regions
 * @param accountId the accountId
 * @return the map
 */
public static Map<String,List<InternetGateway>> fetchInternetGateway(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
	
	Map<String,List<InternetGateway>> internetGatewayMap = new LinkedHashMap<>();
	AmazonEC2 ec2Client ;
	String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"internetgateway\" , \"region\":\"" ;

	for(Region region : RegionUtils.getRegions()) { 
		try{
			if(!skipRegions.contains(region.getName())){ 
				ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				List<InternetGateway> internetGatewayList = ec2Client.describeInternetGateways().getInternetGateways();
				
				if(!internetGatewayList.isEmpty() ) {
					log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 Internet Gateway "+ region.getName()+" >> " + internetGatewayList.size());
					internetGatewayMap.put(accountId+delimiter+accountName+delimiter+region.getName(), internetGatewayList);
				}
		   	}
		}catch(Exception e){
	   		log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
			ErrorManageUtil.uploadError(accountId,region.getName(),"internetgateway",e.getMessage());
	   	}
	}
	return internetGatewayMap;
}
 
Example #22
Source File: EC2InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #23
Source File: EC2InventoryUtilTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch reserved instances test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchReservedInstancesTest() throws Exception {
    
    mockStatic(AmazonEC2ClientBuilder.class);
    AmazonEC2 ec2Client = PowerMockito.mock(AmazonEC2.class);
    AmazonEC2ClientBuilder amazonEC2ClientBuilder = PowerMockito.mock(AmazonEC2ClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonEC2ClientBuilder.standard()).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withCredentials(anyObject())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withRegion(anyString())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.build()).thenReturn(ec2Client);
    
    DescribeReservedInstancesResult describeReservedInstancesResult = new DescribeReservedInstancesResult();
    List<ReservedInstances> reservedInstancesList = new ArrayList<>();
    reservedInstancesList.add(new ReservedInstances());
    describeReservedInstancesResult.setReservedInstances(reservedInstancesList);
    when(ec2Client.describeReservedInstances()).thenReturn(describeReservedInstancesResult);
    assertThat(ec2InventoryUtil.fetchReservedInstances(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
Example #24
Source File: EC2InventoryUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #25
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch volumet info test exception.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchVolumetInfoTest_Exception() throws Exception {
    
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenThrow(new Exception());
    assertThat(inventoryUtil.fetchVolumetInfo(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(0));
}
 
Example #26
Source File: EC2InventoryUtilTest.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch network ACL test exception.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchNetworkACLTest_Exception() throws Exception {
    
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenThrow(new Exception());
    assertThat(ec2InventoryUtil.fetchNetworkACL(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(0));
}
 
Example #27
Source File: InventoryUtil.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch RDSDB 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<DBSnapshot>> fetchRDSDBSnapshots(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
	Map<String,List<DBSnapshot>> snapshots =  new LinkedHashMap<>();
	String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"RDS Snapshot\" , \"region\":\"" ;
	for(Region region : RegionUtils.getRegions()){
		try{
			if(!skipRegions.contains(region.getName())){
				AmazonRDS rdsClient = AmazonRDSClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				DescribeDBSnapshotsResult rslt ;
				List<DBSnapshot> snapshotsTemp = new ArrayList<>();
				String marker = null;
				do{
					rslt = rdsClient.describeDBSnapshots(new DescribeDBSnapshotsRequest().withIncludePublic(false).withIncludeShared(false).withMarker(marker));
					snapshotsTemp.addAll(rslt.getDBSnapshots());
					marker = rslt.getMarker();
				}while(marker!=null);

				if(! snapshotsTemp.isEmpty() ){
					log.debug(InventoryConstants.ACCOUNT + accountId +" Type : RDS Snapshot" +region.getName() + " >> "+snapshotsTemp.size());
					snapshots.put(accountId+delimiter+accountName+delimiter+region.getName(),  snapshotsTemp);
				}
			}

		}catch(Exception e){
			if(region.isServiceSupported(AmazonRDS.ENDPOINT_PREFIX)){
				log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
				ErrorManageUtil.uploadError(accountId,region.getName(),"rdssnapshot",e.getMessage());
			}
		}
	}
	return snapshots;
}
 
Example #28
Source File: InventoryUtil.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch cloud front info.
 *
 * @param temporaryCredentials the temporary credentials
 * @param accountId the accountId
 * @param accountName the account name
 * @return the map
 */
public static Map<String,List<CloudFrontVH>> fetchCloudFrontInfo(BasicSessionCredentials temporaryCredentials,String accountId,String accountName) {

	Map<String,List<CloudFrontVH>> cloudFront = new LinkedHashMap<>();
	List<DistributionSummary> distributionSummary = new ArrayList<>();
	AmazonCloudFront amazonCloudFront;
	String bucketName = null;
	boolean accessLogEnabled = false;
	String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource \" ,\"type\": \"CloudFront\"" ;
	try{
		amazonCloudFront = AmazonCloudFrontClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion("us-west-2").build();

		String marker = null;
		List<CloudFrontVH> cloudFrontList = new ArrayList<>();
		DistributionList distributionList ;
		do{
			distributionList = amazonCloudFront.listDistributions(new ListDistributionsRequest().withMarker(marker)).getDistributionList();
			distributionSummary = distributionList.getItems();
			marker = distributionList.getNextMarker();
			for(DistributionSummary ds : distributionSummary) {
				CloudFrontVH cf = new CloudFrontVH();
				cf.setDistSummary(ds);
				cloudFrontList.add(cf);
			}
		}while(marker!=null);

		setCloudFrontTags(temporaryCredentials,cloudFrontList);
		setConfigDetails(temporaryCredentials,cloudFrontList);

		log.debug(InventoryConstants.ACCOUNT + accountId +" Type : CloudFront "+ " >> "+cloudFrontList.size());
		cloudFront.put(accountId+delimiter+accountName,cloudFrontList);
	}catch(Exception e){
		log.error(expPrefix+ InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
		ErrorManageUtil.uploadError(accountId,"","cloudfront",e.getMessage());
	}
	return cloudFront;
}
 
Example #29
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch NAT gateway info test exception.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchNATGatewayInfoTest_Exception() throws Exception {
    
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenThrow(new Exception());
    assertThat(inventoryUtil.fetchNATGatewayInfo(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(0));
}
 
Example #30
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch EBS info test exception.
 *
 * @throws Exception the exception
 */
@SuppressWarnings({ "static-access"})
@Test
public void fetchEBSInfoTest_Exception() throws Exception {
    
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenThrow(new Exception());
    assertThat(inventoryUtil.fetchEBSInfo(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(0));
}