FetchElasticLoadBalancersJob implements FullstopJob { @Scheduled(fixedRate = 300_000, initialDelay = 120_000) public void run() { log.info("Running job {}", getClass().getSimpleName()); for (final String account : allAccountIds.get()) { for (final String region : jobsProperties.getWhitelistedRegions()) { log.debug("Scanning ELBs for {}/{}", account, region); final Map<String, String> accountRegionCtx = ImmutableMap.of( "job", this.getClass().getSimpleName(), "aws_account_id", account, "aws_region", region); try { final Region awsRegion = getRegion(fromName(region)); final AmazonElasticLoadBalancingClient elbClient = clientProvider.getClient( AmazonElasticLoadBalancingClient.class, account, getRegion(fromName(region))); Optional<String> marker = Optional.empty(); do { final DescribeLoadBalancersRequest request = new DescribeLoadBalancersRequest(); marker.ifPresent(request::setMarker); final DescribeLoadBalancersResult result = elbClient.describeLoadBalancers(request); marker = Optional.ofNullable(trimToNull(result.getNextMarker())); final List<String> publicElbNames = result.getLoadBalancerDescriptions().stream() .filter(this::isInternetFacing) .map(LoadBalancerDescription::getLoadBalancerName) .collect(toList()); final Map<String, List<Tag>> tagsByPublicElb = getElbTags(elbClient, publicElbNames); for (final LoadBalancerDescription elb : result.getLoadBalancerDescriptions()) { if (!isInternetFacing(elb)) { continue; } if (hasKubernetesTag(tagsByPublicElb.getOrDefault(elb.getLoadBalancerName(), emptyList()))) { continue; } try { processELB(account, awsRegion, elb); } catch (Exception e) { final Map<String, String> elbCtx = ImmutableMap.<String, String>builder() .putAll(accountRegionCtx) .put("load_balancer_name", elb.getLoadBalancerName()) .build(); jobExceptionHandler.onException(e, elbCtx); } } } while (marker.isPresent()); } catch (final Exception e) { jobExceptionHandler.onException(e, accountRegionCtx); } } } } @Autowired  FetchElasticLoadBalancersJob(final ViolationSink violationSink,
                                        final ClientProvider clientProvider,
                                        final AccountIdSupplier allAccountIds, final JobsProperties jobsProperties,
                                        @Qualifier("elbSecurityGroupsChecker") final SecurityGroupsChecker securityGroupsChecker,
                                        final PortsChecker portsChecker,
                                        final AwsApplications awsApplications,
                                        final ViolationService violationService,
                                        final FetchTaupageYaml fetchTaupageYaml,
                                        final AmiDetailsProvider amiDetailsProvider,
                                        final EC2InstanceProvider ec2Instance,
                                        final CloseableHttpClient httpClient,
                                        final JobExceptionHandler jobExceptionHandler); @PostConstruct void init(); @Scheduled(fixedRate = 300_000, initialDelay = 120_000) // 5 min rate, 2 min delay void run();  }
@Test public void testCheck() throws Exception { when(accountIdSupplierMock.get()).thenReturn(newHashSet(ACCOUNT_ID)); when(jobsPropertiesMock.getWhitelistedRegions()).thenReturn(regions); when(portsChecker.check(any(LoadBalancerDescription.class))).thenReturn(Collections.<Integer>emptyList()); when(securityGroupsChecker.check(any(), any(), any())).thenReturn(emptyMap()); when(mockAwsELBClient.describeLoadBalancers(any(DescribeLoadBalancersRequest.class))).thenReturn(mockDescribeELBResult); when(mockAwsELBClient.describeTags(any(DescribeTagsRequest.class))).thenReturn(mockDescribeTagsResult); when(mockAwsApplications.isPubliclyAccessible(anyString(), anyString(), anyListOf(String.class))) .thenReturn(Optional.of(false)); final FetchElasticLoadBalancersJob fetchELBJob = new FetchElasticLoadBalancersJob( violationSinkMock, clientProviderMock, accountIdSupplierMock, jobsPropertiesMock, securityGroupsChecker, portsChecker, mockAwsApplications, mockViolationService, fetchTaupageYamlMock, mockAmiDetailsProvider, mockEC2InstanceProvider, mock(CloseableHttpClient.class), mock(JobExceptionHandler.class)); fetchELBJob.run(); verify(accountIdSupplierMock).get(); verify(jobsPropertiesMock, atLeast(1)).getWhitelistedRegions(); verify(jobsPropertiesMock).getElbAllowedPorts(); verify(securityGroupsChecker, atLeast(1)).check(any(), any(), any()); verify(portsChecker, atLeast(1)).check(any()); verify(mockAwsELBClient).describeLoadBalancers(any(DescribeLoadBalancersRequest.class)); verify(mockAwsELBClient, times(2)).describeTags(any(DescribeTagsRequest.class)); verify(clientProviderMock).getClient(any(), any(String.class), any(Region.class)); verify(mockAwsApplications).isPubliclyAccessible(eq(ACCOUNT_ID), eq(REGION1), eq(asList("i1", "i2"))); verify(mockEC2InstanceProvider).getById(eq(ACCOUNT_ID), eq(getRegion(fromName(REGION1))), eq("i1")); verify(mockAmiDetailsProvider).getAmiDetails(eq(ACCOUNT_ID), eq(getRegion(fromName(REGION1))), eq("bar")); }