FetchRdsJob implements FullstopJob { @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)); } } } } @Autowired  FetchRdsJob(final AccountIdSupplier allAccountIds, final ClientProvider clientProvider,
                       final JobsProperties jobsProperties,
                       final ViolationSink violationSink,
                       final JobExceptionHandler jobExceptionHandler); @PostConstruct void init(); @Scheduled(fixedRate = 300_000) void run();  }
@SuppressWarnings("ResultOfMethodCallIgnored") @Test public void testCheck() { final FetchRdsJob fetchRdsJob = new FetchRdsJob(accountIdSupplierMock, clientProviderMock, jobsPropertiesMock, violationSinkMock, exceptionHandlerMock); when(amazonRDSClientMock.describeDBInstances(any(DescribeDBInstancesRequest.class))).thenReturn(describeDBInstancesResultMock); fetchRdsJob.run(); ArgumentCaptor<Violation> violations = ArgumentCaptor.forClass(Violation.class); verify(violationSinkMock, times(2)).put(violations.capture()); verify(accountIdSupplierMock, times(1)).get(); verify(amazonRDSClientMock, times(1)).describeDBInstances(any(DescribeDBInstancesRequest.class)); verify(jobsPropertiesMock, times(1)).getWhitelistedRegions(); verify(clientProviderMock, times(1)).getClient(any(), any(String.class), any(Region.class)); assertArrayEquals(new String[]{"aws.db.cn", "aws.db2.cn"}, violations.getAllValues().stream() .map(violation -> (Map) violation.getMetaInfo()) .map(metaInfo -> metaInfo.get("unsecuredDatabase")) .toArray()); }