com.amazonaws.services.rds.model.DescribeDBInstancesResult Java Examples

The following examples show how to use com.amazonaws.services.rds.model.DescribeDBInstancesResult. 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: RdsTableProviderTest.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUpRead()
{
    final AtomicLong requestCount = new AtomicLong(0);
    when(mockRds.describeDBInstances(any(DescribeDBInstancesRequest.class)))
            .thenAnswer((InvocationOnMock invocation) -> {
                DescribeDBInstancesResult mockResult = mock(DescribeDBInstancesResult.class);
                List<DBInstance> values = new ArrayList<>();
                values.add(makeValue(getIdValue()));
                values.add(makeValue(getIdValue()));
                values.add(makeValue("fake-id"));
                when(mockResult.getDBInstances()).thenReturn(values);

                if (requestCount.incrementAndGet() < 3) {
                    when(mockResult.getMarker()).thenReturn(String.valueOf(requestCount.get()));
                }
                return mockResult;
            });
}
 
Example #2
Source File: BeanstalkConnector.java    From cloudml with GNU Lesser General Public License v3.0 6 votes vote down vote up
public String getDBEndPoint(String dbInstanceId, int timeout){
    DescribeDBInstancesRequest ddbir = new DescribeDBInstancesRequest()
            .withDBInstanceIdentifier(dbInstanceId);
    System.out.println("Waiting for DB endpoints");
    while(timeout -- > 0){
        System.out.print("-");
        DescribeDBInstancesResult ddbi = rdsClient.describeDBInstances(ddbir);
        Endpoint endpoint = ddbi.getDBInstances().get(0).getEndpoint();
        if(endpoint != null && endpoint.toString().length()!=0)
            return endpoint.getAddress()+":"+endpoint.getPort();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(BeanstalkConnector.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return "";
}
 
Example #3
Source File: AmazonRdsDatabaseAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Bean
AmazonRDSClient amazonRDS() {
	AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class);
	when(client.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("test")
									.withDBInstanceIdentifier("test")
									.withEngine("mysql")
									.withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost")
											.withPort(3306))
									.withReadReplicaDBInstanceIdentifiers(
											"read1")));
	return client;
}
 
Example #4
Source File: DatabaseInstanceStatusRetryPolicyTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void canRetry_retryPossibleDueToAvailableDatabase_returnsTrue() throws Exception {
	// Arrange
	AmazonRDS amazonRDS = mock(AmazonRDS.class);

	DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(
			amazonRDS, "test");
	when(amazonRDS.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")));

	RetryContext retryContext = policy.open(new RetryContextSupport(null));

	// Act
	policy.registerThrowable(retryContext,
			new TransientDataAccessResourceException("not available"));

	// Assert
	assertThat(policy.canRetry(retryContext)).isTrue();
	policy.close(retryContext);
}
 
Example #5
Source File: AmazonRdsInstanceConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Bean
AmazonRDS amazonRDS() {
	AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class);
	when(client.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("test")
									.withDBInstanceIdentifier("test")
									.withEngine("mysql")
									.withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost")
											.withPort(3306))
									.withReadReplicaDBInstanceIdentifiers(
											"read1")));
	return client;
}
 
Example #6
Source File: AmazonRdsInstanceConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Bean
AmazonRDS amazonRDS() {
	AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class);
	when(client.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("test")
									.withDBInstanceIdentifier("test")
									.withEngine("mysql")
									.withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost")
											.withPort(3306))
									.withReadReplicaDBInstanceIdentifiers(
											"read1")));
	return client;
}
 
Example #7
Source File: AmazonRdsInstanceConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Bean
AmazonRDS amazonRDS() {
	AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class);
	when(client.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("test")
									.withDBInstanceIdentifier("test")
									.withEngine("mysql")
									.withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost")
											.withPort(3306))
									.withReadReplicaDBInstanceIdentifiers(
											"read1")));
	return client;
}
 
Example #8
Source File: AmazonRdsInstanceConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Bean
AmazonRDS amazonRDS() {
	AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class);
	when(client.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("test")
									.withDBInstanceIdentifier("test")
									.withEngine("mysql")
									.withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost")
											.withPort(3306))
									.withReadReplicaDBInstanceIdentifiers(
											"read1")));
	return client;
}
 
Example #9
Source File: AmazonRdsInstanceConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Bean
AmazonRDS amazonRDS() {
	AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class);
	when(client.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("test")
									.withDBInstanceIdentifier("test")
									.withEngine("mysql")
									.withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost")
											.withPort(3306))
									.withReadReplicaDBInstanceIdentifiers(
											"read1")));
	return client;
}
 
Example #10
Source File: AmazonRdsDataSourceFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void afterPropertiesSet_customUserNameSet_createsInstanceWithCustomUserNameAndIgnoresMetaDataUserName()
		throws Exception {
	AmazonRDS amazonRDS = mock(AmazonRDS.class);
	DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class);

	when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest()
			.withDBInstanceIdentifier("test"))).thenReturn(
					new DescribeDBInstancesResult().withDBInstances(new DBInstance()
							.withDBInstanceStatus("available").withDBName("test")
							.withDBInstanceIdentifier("test").withEngine("mysql")
							.withMasterUsername("admin").withEndpoint(new Endpoint()
									.withAddress("localhost").withPort(3306))));

	AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(
			amazonRDS, "test", "secret");
	amazonRdsDataSourceFactoryBean.setUsername("superAdmin");
	amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory);
	amazonRdsDataSourceFactoryBean.afterPropertiesSet();
	amazonRdsDataSourceFactoryBean.getObject();

	verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "test", "superAdmin", "secret"));
}
 
Example #11
Source File: RdsTableProvider.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
/**
 * Calls DescribeDBInstances on the AWS RDS Client returning all DB Instances that match the supplied predicate and attempting
 * to push down certain predicates (namely queries for specific DB Instance) to EC2.
 *
 * @See TableProvider
 */
@Override
public void readWithConstraint(BlockSpiller spiller, ReadRecordsRequest recordsRequest, QueryStatusChecker queryStatusChecker)
{
    boolean done = false;
    DescribeDBInstancesRequest request = new DescribeDBInstancesRequest();

    ValueSet idConstraint = recordsRequest.getConstraints().getSummary().get("instance_id");
    if (idConstraint != null && idConstraint.isSingleValue()) {
        request.setDBInstanceIdentifier(idConstraint.getSingleValue().toString());
    }

    while (!done) {
        DescribeDBInstancesResult response = rds.describeDBInstances(request);

        for (DBInstance instance : response.getDBInstances()) {
            instanceToRow(instance, spiller);
        }

        request.setMarker(response.getMarker());

        if (response.getMarker() == null || !queryStatusChecker.isQueryRunning()) {
            done = true;
        }
    }
}
 
Example #12
Source File: AmazonRdsDataSourceFactoryBean.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the {@link com.amazonaws.services.rds.model.DBInstance} information.
 * @param identifier - the database identifier used
 * @return - the db instance
 * @throws IllegalStateException if the db instance is not found
 */
protected DBInstance getDbInstance(String identifier) throws IllegalStateException {
	DBInstance instance;
	try {
		DescribeDBInstancesResult describeDBInstancesResult = this.amazonRds
				.describeDBInstances(new DescribeDBInstancesRequest()
						.withDBInstanceIdentifier(identifier));
		instance = describeDBInstancesResult.getDBInstances().get(0);
	}
	catch (DBInstanceNotFoundException e) {
		throw new IllegalStateException(MessageFormat.format(
				"No database instance with id:''{0}'' found. Please specify a valid db instance",
				identifier));
	}
	return instance;
}
 
Example #13
Source File: CreateDbInstance.java    From obevo with Apache License 2.0 6 votes vote down vote up
private void describe(String dbInstanceIdentifier) throws Exception {
    while (true) {
        DescribeDBInstancesRequest request = new DescribeDBInstancesRequest()
                .withDBInstanceIdentifier(dbInstanceIdentifier);

        DescribeDBInstancesResult response = client.describeDBInstances(request);
        DBInstance dbInstance = response.getDBInstances().get(0);
        if (!dbInstance.getDBInstanceStatus().equalsIgnoreCase("creating")) {
            System.out.println("Done! " + response);
            System.out.println(dbInstance.getEndpoint().getAddress());
            System.out.println(dbInstance.getEndpoint().getPort());
            break;
        }

        System.out.println("Not done - will wait 10s: " + response);
        Thread.sleep(10000L);
    }
}
 
Example #14
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch RDS instance info test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchRDSInstanceInfoTest() 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);
    
    DescribeDBInstancesResult describeDBInstancesResult = new DescribeDBInstancesResult();
    List<DBInstance> rdsList = new ArrayList<>();
    DBInstance dBInstance = new DBInstance();
    dBInstance.setDBInstanceArn("dBInstanceArn");
    rdsList.add(dBInstance);
    describeDBInstancesResult.setDBInstances(rdsList);
    when(rdsClient.describeDBInstances(anyObject())).thenReturn(describeDBInstancesResult);
    
    ListTagsForResourceResult listTagsForResourceResult = new ListTagsForResourceResult();
    listTagsForResourceResult.setTagList(new ArrayList<>());
    when(rdsClient.listTagsForResource(anyObject())).thenReturn(listTagsForResourceResult);
    assertThat(inventoryUtil.fetchRDSInstanceInfo(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
Example #15
Source File: BeanstalkConnector.java    From cloudml with GNU Lesser General Public License v3.0 5 votes vote down vote up
public String getDBStatus(String dbInstanceId){
    DescribeDBInstancesRequest ddbir = new DescribeDBInstancesRequest()
            .withDBInstanceIdentifier(dbInstanceId);
    DescribeDBInstancesResult ddbi = rdsClient.describeDBInstances(ddbir);
    try{
        return ddbi.getDBInstances().get(0).getStatusInfos().toString();
    }catch(Exception e){
        throw new RuntimeException("DBInstance not found");
    }
}
 
Example #16
Source File: AmazonRdsDatabaseAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Bean
AmazonRDS amazonRDS() {
	AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class);
	when(client.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("test")
									.withDBInstanceIdentifier("test")
									.withEngine("mysql")
									.withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost")
											.withPort(3306))
									.withReadReplicaDBInstanceIdentifiers(
											"read1")));
	when(client.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("read1")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("read1")
									.withDBInstanceIdentifier("read1")
									.withEngine("mysql")
									.withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost")
											.withPort(3306))));
	return client;
}
 
Example #17
Source File: AmazonRdsDatabaseAutoConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Bean
AmazonRDS amazonRDS() {
	AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class);
	when(client.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("test")
									.withDBInstanceIdentifier("test")
									.withEngine("mysql")
									.withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost")
											.withPort(3306))));
	when(client.describeDBInstances(new DescribeDBInstancesRequest()
			.withDBInstanceIdentifier("anotherOne")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("test")
									.withDBInstanceIdentifier("anotherOne")
									.withEngine("mysql")
									.withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost")
											.withPort(3306))));
	return client;
}
 
Example #18
Source File: PublicAccessAutoFix.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the DB instance for rds db resource.
 *
 * @param clientMap the client map
 * @param resourceId the resource id
 * @return the DB instance for rds db resource
 * @throws Exception the exception
 */
public static List<DBInstance> getDBInstanceForRdsDbResource(Map<String,Object> clientMap,String resourceId) throws Exception {
    AmazonRDS amazonRDS = (AmazonRDS) clientMap.get("client");
    DescribeDBInstancesRequest instancesRequest = new DescribeDBInstancesRequest();
    instancesRequest.setDBInstanceIdentifier(resourceId);
    DescribeDBInstancesResult dbInstancesResult = amazonRDS.describeDBInstances(instancesRequest);
    
    return dbInstancesResult.getDBInstances();

}
 
Example #19
Source File: DatabaseInstanceStatusRetryPolicyTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void canRetry_multipleDatabasesFoundForInstanceIdentifier_reportsException()
		throws Exception {
	// Arrange
	AmazonRDS amazonRDS = mock(AmazonRDS.class);

	DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(
			amazonRDS, "test");

	DescribeDBInstancesResult describeDBInstancesResult = new DescribeDBInstancesResult()
			.withDBInstances(new DBInstance(), new DBInstance());
	when(amazonRDS.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(describeDBInstancesResult);

	RetryContext retryContext = policy.open(new RetryContextSupport(null));

	// Act
	policy.registerThrowable(retryContext,
			new TransientDataAccessResourceException("not available"));

	// Assert

	assertThatThrownBy(() -> policy.canRetry(retryContext))
			.isInstanceOf(IllegalStateException.class)
			.hasMessageContaining("Multiple databases found for same identifier");
}
 
Example #20
Source File: DatabaseInstanceStatusRetryPolicyTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void canRetry_withResourceIdResolver_returnsTrue() throws Exception {
	// Arrange
	AmazonRDS amazonRDS = mock(AmazonRDS.class);
	ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class);

	DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(
			amazonRDS, "foo");
	when(amazonRDS.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")));

	when(resourceIdResolver.resolveToPhysicalResourceId("foo")).thenReturn("test");

	policy.setResourceIdResolver(resourceIdResolver);

	RetryContext retryContext = policy.open(new RetryContextSupport(null));

	// Act
	policy.registerThrowable(retryContext,
			new TransientDataAccessResourceException("not available"));

	// Assert
	assertThat(policy.canRetry(retryContext)).isTrue();
	policy.close(retryContext);
}
 
Example #21
Source File: AmazonRdsInstanceConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Bean
AmazonRDS amazonRDS() {
	AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class);
	when(client.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("test")
									.withDBInstanceIdentifier("test")
									.withEngine("mysql")
									.withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost")
											.withPort(3306))
									.withReadReplicaDBInstanceIdentifiers(
											"read1")));
	when(client.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("read1")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("read1")
									.withDBInstanceIdentifier("read1")
									.withEngine("mysql")
									.withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost")
											.withPort(3306))));
	return client;
}
 
Example #22
Source File: AmazonRdsReadReplicaAwareDataSourceFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void afterPropertiesSet_instanceWithoutReadReplica_createsNoDataSourceRouter()
		throws Exception {
	// Arrange
	AmazonRDS amazonRDS = mock(AmazonRDS.class);
	DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class);

	when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest()
			.withDBInstanceIdentifier("test"))).thenReturn(
					new DescribeDBInstancesResult().withDBInstances(new DBInstance()
							.withDBInstanceStatus("available").withDBName("test")
							.withDBInstanceIdentifier("test").withEngine("mysql")
							.withMasterUsername("admin").withEndpoint(new Endpoint()
									.withAddress("localhost").withPort(3306))));

	AmazonRdsReadReplicaAwareDataSourceFactoryBean factoryBean = new AmazonRdsReadReplicaAwareDataSourceFactoryBean(
			amazonRDS, "test", "secret");
	factoryBean.setDataSourceFactory(dataSourceFactory);
	when(dataSourceFactory.createDataSource(new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")))
					.thenReturn(mock(DataSource.class));

	// Act
	factoryBean.afterPropertiesSet();

	// Assert
	DataSource datasource = factoryBean.getObject();
	assertThat(datasource).isNotNull();

	verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"));
}
 
Example #23
Source File: FetchRdsJob.java    From fullstop with Apache License 2.0 5 votes vote down vote up
@Scheduled(fixedRate = 300_000)
public void run() {
    for (final String accountId : allAccountIds.get()) {
        for (final String region : jobsProperties.getWhitelistedRegions()) {
            try {
                final AmazonRDSClient amazonRDSClient = clientProvider.getClient(AmazonRDSClient.class, accountId,
                        Region.getRegion(Regions.fromName(region)));

                Optional<String> marker = Optional.empty();

                do {
                    final DescribeDBInstancesRequest request = new DescribeDBInstancesRequest();
                    marker.ifPresent(request::setMarker);
                    final DescribeDBInstancesResult result = amazonRDSClient.describeDBInstances(request);
                    marker = Optional.ofNullable(trimToNull(result.getMarker()));

                    result.getDBInstances().stream()
                            .filter(DBInstance::getPubliclyAccessible)
                            .filter(dbInstance -> dbInstance.getEndpoint() != null)
                            .forEach(dbInstance -> {
                                final Map<String, Object> metadata = newHashMap();
                                metadata.put("unsecuredDatabase", dbInstance.getEndpoint().getAddress());
                                metadata.put("errorMessages", "Unsecured Database! Your DB can be reached from outside");
                                writeViolation(accountId, region, metadata, dbInstance.getEndpoint().getAddress());

                            });

                } while (marker.isPresent());

            } catch (final Exception e) {
                jobExceptionHandler.onException(e, ImmutableMap.of(
                        "job", this.getClass().getSimpleName(),
                        "aws_account_id", accountId,
                        "aws_region", region));
            }
        }
    }
}
 
Example #24
Source File: FetchRdsJobTest.java    From fullstop with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    this.clientProviderMock = mock(ClientProvider.class);
    this.jobsPropertiesMock = mock(JobsProperties.class);
    this.violationSinkMock = mock(ViolationSink.class);
    this.amazonRDSClientMock = mock(AmazonRDSClient.class);
    this.accountIdSupplierMock = mock(AccountIdSupplier.class);
    this.exceptionHandlerMock = mock(JobExceptionHandler.class);

    when(accountIdSupplierMock.get()).thenReturn(newHashSet("54321"));

    // Jobsproperties
    when(jobsPropertiesMock.getWhitelistedRegions()).thenReturn(newArrayList("eu-west-1"));

    // Dbinstances
    final Endpoint endpoint = new Endpoint();
    endpoint.setAddress("aws.db.cn");
    final Endpoint endpoint2 = new Endpoint();
    endpoint2.setAddress("aws.db2.cn");
    final DBInstance dbInstance1 = new DBInstance();
    dbInstance1.setPubliclyAccessible(true);
    dbInstance1.setEndpoint(endpoint);
    final DBInstance dbInstance2 = new DBInstance();
    dbInstance2.setPubliclyAccessible(false);
    dbInstance2.setEndpoint(endpoint);
    final DBInstance dbInstance3 = new DBInstance();
    dbInstance3.setPubliclyAccessible(true);
    dbInstance3.setEndpoint(endpoint2);
    describeDBInstancesResultMock = new DescribeDBInstancesResult();
    describeDBInstancesResultMock.setDBInstances(newArrayList(dbInstance1, dbInstance2, dbInstance3));

    // clientprovider
    when(clientProviderMock.getClient(any(), any(String.class), any(Region.class))).thenReturn(amazonRDSClientMock);

}
 
Example #25
Source File: DatabaseInstanceStatusRetryPolicy.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
private boolean isDatabaseAvailable(RetryContext context) {
	DescribeDBInstancesResult describeDBInstancesResult;
	try {
		describeDBInstancesResult = this.amazonRDS.describeDBInstances(
				new DescribeDBInstancesRequest().withDBInstanceIdentifier(
						(String) context.getAttribute(DB_INSTANCE_ATTRIBUTE_NAME)));
	}
	catch (DBInstanceNotFoundException e) {
		LOGGER.warn(
				"Database Instance with name {} has been removed or is not configured correctly, no retry possible",
				getDbInstanceIdentifier());
		// Database has been deleted while operating, hence we can not retry
		return false;
	}

	if (describeDBInstancesResult.getDBInstances().size() == 1) {
		DBInstance dbInstance = describeDBInstancesResult.getDBInstances().get(0);
		InstanceStatus instanceStatus = InstanceStatus
				.fromDatabaseStatus(dbInstance.getDBInstanceStatus());
		if (LOGGER.isTraceEnabled()) {
			LOGGER.trace("Status of database to be retried is {}", instanceStatus);
		}
		return instanceStatus.isRetryable();
	}
	else {
		throw new IllegalStateException(
				"Multiple databases found for same identifier, this is likely an incompatibility with the Amazon SDK");
	}
}
 
Example #26
Source File: AmazonRdsDataSourceFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void destroyInstance_shutdownInitiated_destroysDynamicDataSource() throws Exception {
	AmazonRDS amazonRDS = mock(AmazonRDS.class);
	DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class);
	DataSource dataSource = mock(DataSource.class);

	when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest()
			.withDBInstanceIdentifier("test"))).thenReturn(
					new DescribeDBInstancesResult().withDBInstances(new DBInstance()
							.withDBInstanceStatus("available").withDBName("test")
							.withDBInstanceIdentifier("test").withEngine("mysql")
							.withMasterUsername("admin").withEndpoint(new Endpoint()
									.withAddress("localhost").withPort(3306))));

	when(dataSourceFactory.createDataSource(new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")))
					.thenReturn(dataSource);

	AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(
			amazonRDS, "test", "secret");
	amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory);
	amazonRdsDataSourceFactoryBean.afterPropertiesSet();

	amazonRdsDataSourceFactoryBean.getObject();

	amazonRdsDataSourceFactoryBean.destroy();

	verify(dataSourceFactory, times(1)).closeDataSource(dataSource);
}
 
Example #27
Source File: AmazonRdsDataSourceFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void afterPropertiesSet_noUserNameSet_createsInstanceWithUserNameFromMetaData()
		throws Exception {
	// Arrange
	AmazonRDS amazonRDS = mock(AmazonRDS.class);
	DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class);
	DataSource dataSource = mock(DataSource.class);

	when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest()
			.withDBInstanceIdentifier("test"))).thenReturn(
					new DescribeDBInstancesResult().withDBInstances(new DBInstance()
							.withDBInstanceStatus("available").withDBName("test")
							.withDBInstanceIdentifier("test").withEngine("mysql")
							.withMasterUsername("admin").withEndpoint(new Endpoint()
									.withAddress("localhost").withPort(3306))));

	when(dataSourceFactory.createDataSource(new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")))
					.thenReturn(dataSource);

	AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(
			amazonRDS, "test", "secret");
	amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory);

	// Act
	amazonRdsDataSourceFactoryBean.afterPropertiesSet();

	// Assert
	DataSource datasource = amazonRdsDataSourceFactoryBean.getObject();
	assertThat(datasource).isNotNull();

	verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"));
}
 
Example #28
Source File: AmazonRdsDataSourceFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void newInstance_withResourceIdResolver_createsInstanceWithResolvedName()
		throws Exception {
	// Arrange
	AmazonRDS amazonRDS = mock(AmazonRDS.class);
	DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class);
	ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class);
	DataSource dataSource = mock(DataSource.class);

	when(resourceIdResolver.resolveToPhysicalResourceId("test")).thenReturn("bar");

	when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest()
			.withDBInstanceIdentifier("bar"))).thenReturn(
					new DescribeDBInstancesResult().withDBInstances(new DBInstance()
							.withDBInstanceStatus("available").withDBName("test")
							.withDBInstanceIdentifier("bar").withEngine("mysql")
							.withMasterUsername("admin").withEndpoint(new Endpoint()
									.withAddress("localhost").withPort(3306))));

	when(dataSourceFactory.createDataSource(new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")))
					.thenReturn(dataSource);

	AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(
			amazonRDS, "test", "secret");
	amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory);
	amazonRdsDataSourceFactoryBean.setResourceIdResolver(resourceIdResolver);

	// Act
	amazonRdsDataSourceFactoryBean.afterPropertiesSet();

	// Assert
	assertThat(amazonRdsDataSourceFactoryBean.getObject()).isNotNull();

	verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"));
}
 
Example #29
Source File: InventoryUtil.java    From pacbot with Apache License 2.0 4 votes vote down vote up
/**
 * Fetch RDS instance 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<DBInstanceVH>> fetchRDSInstanceInfo(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
	Map<String,List<DBInstanceVH>> dbInstMap =  new LinkedHashMap<>();
	AmazonRDS rdsClient ;
	String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"RDS Instance\" , \"region\":\"" ;
	for(Region region : RegionUtils.getRegions()){
		try{
			if(!skipRegions.contains(region.getName())){
				rdsClient = AmazonRDSClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				String nextMarker = null;
				DescribeDBInstancesResult rslt;
				List<DBInstanceVH> dbInstList = new ArrayList<>();
				do{
					rslt = rdsClient.describeDBInstances(new DescribeDBInstancesRequest().withMarker(nextMarker));
					List<DBInstance> dbInstListTemp = rslt.getDBInstances();
					for(DBInstance db : dbInstListTemp){
						DBInstanceVH vh = new DBInstanceVH(db, rdsClient.listTagsForResource(new ListTagsForResourceRequest().
													withResourceName(db.getDBInstanceArn())).
														getTagList(),
														db.getDBSubnetGroup().getSubnets().stream().map(subnet -> subnet.getSubnetIdentifier()).collect(Collectors.joining(",")),
														db.getVpcSecurityGroups().stream().map(group->group.getVpcSecurityGroupId()+":"+group.getStatus()).collect(Collectors.joining(","))
										);
						dbInstList.add(vh);
					}
					nextMarker = rslt.getMarker();
				}while(nextMarker!=null);

				if(! dbInstList.isEmpty() ){
					log.debug(InventoryConstants.ACCOUNT + accountId +" Type : RDS Instance" +region.getName() + " >> "+dbInstList.size());
					dbInstMap.put(accountId+delimiter+accountName+delimiter+region.getName(),  dbInstList);
				}
			}
		}catch(Exception e){
			if(region.isServiceSupported(AmazonRDS.ENDPOINT_PREFIX)){
				log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
				ErrorManageUtil.uploadError(accountId,region.getName(),"rdsdb",e.getMessage());
			}
		}
	}
	return dbInstMap;
}
 
Example #30
Source File: AmazonRdsReadReplicaAwareDataSourceFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 4 votes vote down vote up
@Test
void afterPropertiesSet_instanceWithReadReplica_createsDataSourceRouter()
		throws Exception {
	// Arrange
	AmazonRDS amazonRDS = mock(AmazonRDS.class);
	DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class);

	when(amazonRDS.describeDBInstances(
			new DescribeDBInstancesRequest().withDBInstanceIdentifier("test")))
					.thenReturn(new DescribeDBInstancesResult().withDBInstances(
							new DBInstance().withDBInstanceStatus("available")
									.withDBName("test")
									.withDBInstanceIdentifier("test")
									.withEngine("mysql").withMasterUsername("admin")
									.withEndpoint(new Endpoint()
											.withAddress("localhost").withPort(3306))
									.withReadReplicaDBInstanceIdentifiers("read1",
											"read2")));

	when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest()
			.withDBInstanceIdentifier("read1"))).thenReturn(
					new DescribeDBInstancesResult().withDBInstances(new DBInstance()
							.withDBInstanceStatus("available").withDBName("read1")
							.withDBInstanceIdentifier("read1").withEngine("mysql")
							.withMasterUsername("admin").withEndpoint(new Endpoint()
									.withAddress("localhost").withPort(3306))));

	when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest()
			.withDBInstanceIdentifier("read2"))).thenReturn(
					new DescribeDBInstancesResult().withDBInstances(new DBInstance()
							.withDBInstanceStatus("available").withDBName("read2")
							.withDBInstanceIdentifier("read2").withEngine("mysql")
							.withMasterUsername("admin").withEndpoint(new Endpoint()
									.withAddress("localhost").withPort(3306))));

	DataSource createdDataSource = mock(DataSource.class);
	Connection connection = mock(Connection.class);

	when(dataSourceFactory.createDataSource(new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")))
					.thenReturn(createdDataSource);
	when(dataSourceFactory.createDataSource(new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "read1", "admin", "secret")))
					.thenReturn(createdDataSource);
	when(dataSourceFactory.createDataSource(new DataSourceInformation(
			DatabaseType.MYSQL, "localhost", 3306, "read2", "admin", "secret")))
					.thenReturn(createdDataSource);
	when(createdDataSource.getConnection()).thenReturn(connection);

	AmazonRdsReadReplicaAwareDataSourceFactoryBean factoryBean = new AmazonRdsReadReplicaAwareDataSourceFactoryBean(
			amazonRDS, "test", "secret");
	factoryBean.setDataSourceFactory(dataSourceFactory);

	// Act
	factoryBean.afterPropertiesSet();

	// Assert
	DataSource datasource = factoryBean.getObject();
	assertThat(datasource).isNotNull();
	assertThat(datasource instanceof LazyConnectionDataSourceProxy).isTrue();

	ReadOnlyRoutingDataSource source = (ReadOnlyRoutingDataSource) ((LazyConnectionDataSourceProxy) datasource)
			.getTargetDataSource();
	assertThat(source.getDataSources().size()).isEqualTo(3);
}