SecurityGroupsCheckerImpl implements SecurityGroupsChecker { @Override public Map<String, SecurityGroupCheckDetails> check(final Collection<String> groupIds, final String account, final Region region) { final DescribeSecurityGroupsRequest describeSecurityGroupsRequest = new DescribeSecurityGroupsRequest(); describeSecurityGroupsRequest.setGroupIds(groupIds); final AmazonEC2Client amazonEC2Client = clientProvider.getClient( AmazonEC2Client.class, account, region); final DescribeSecurityGroupsResult describeSecurityGroupsResult = amazonEC2Client.describeSecurityGroups( describeSecurityGroupsRequest); final ImmutableMap.Builder<String, SecurityGroupCheckDetails> result = ImmutableMap.builder(); for (final SecurityGroup securityGroup : describeSecurityGroupsResult.getSecurityGroups()) { final List<String> offendingRules = securityGroup.getIpPermissions().stream() .filter(isOffending) .map(Object::toString) .collect(toList()); if (!offendingRules.isEmpty()) { final SecurityGroupCheckDetails details = new SecurityGroupCheckDetails( securityGroup.getGroupName(), ImmutableList.copyOf(offendingRules)); result.put(securityGroup.getGroupId(), details); } } return result.build(); } SecurityGroupsCheckerImpl(final ClientProvider clientProvider, Predicate<? super IpPermission> isOffending); @Override Map<String, SecurityGroupCheckDetails> check(final Collection<String> groupIds, final String account, final Region region);  }
@Test public void check() throws Exception { when(mockPredicate.test(any())).thenReturn(true); final Map<String, SecurityGroupCheckDetails> checkResult = securityGroupsChecker.check( singleton("sg-12345678"), "111222333444", getRegion(EU_WEST_1)); log.info("Check result: {}", checkResult); assertThat(checkResult).containsKey("sg-12345678"); }
@Test public void checkNoMatch() throws Exception { when(mockPredicate.test(any())).thenReturn(false); final Map<String, SecurityGroupCheckDetails> checkResult = securityGroupsChecker.check( singleton("sg-12345678"), "111222333444", getRegion(EU_WEST_1)); log.info("Check result: {}", checkResult); assertThat(checkResult).isEmpty(); }