SaveSecurityGroupsPlugin extends AbstractFullstopPlugin { @Override public void processEvent(final CloudTrailEvent event) { final List<String> securityGroupIds = readSecurityGroupIds(event); final Region region = getRegion(event); final String accountId = getAccountId(event); final List<String> instanceIds = getInstanceIds(event); if (instanceIds.isEmpty()) { LOG.warn("No instanceIds for event : {}, skip processing", CloudTrailEventSupport.getEventId(event)); return; } final String securityGroup = getSecurityGroup(securityGroupIds, region, accountId); if (securityGroup == null) { return; } for (final String instanceId : instanceIds) { final List<String> instanceBuckets = Lists.newArrayList(); final DateTime instanceLaunchTime; try { instanceLaunchTime = new DateTime(getInstanceLaunchTime(event).get(instanceIds.indexOf(instanceId))); } catch (final Exception e) { LOG.warn("No 'launchTime' for event : {}, skip processing", CloudTrailEventSupport.getEventId(event)); return; } final String prefix = PrefixBuilder.build(accountId, region.getName(), instanceLaunchTime); final List<String> s3InstanceObjects = listS3Objects(bucketName, prefix); for (final String s3InstanceObject : s3InstanceObjects) { final String s = Paths.get(s3InstanceObject).getFileName().toString(); if (s.startsWith(instanceId)) { instanceBuckets.add(s); } } if (instanceBuckets.isEmpty()) { continue; } String instanceBucketNameControlElement = null; DateTime instanceBootTimeControlElement = null; for (final String instanceBucket : instanceBuckets) { final List<String> currentBucket = Lists.newArrayList( Splitter.on('-').limit(3).trimResults() .omitEmptyStrings().split(instanceBucket)); final String currentBucketName = currentBucket.get(0) + "-" + currentBucket.get(1); final DateTime currentBucketDate; try { currentBucketDate = new DateTime(currentBucket.get(2), UTC); } catch (final IllegalArgumentException e) { continue; } if (instanceBucketNameControlElement != null) { if (instanceLaunchTime.getMillis() - currentBucketDate.getMillis() < instanceLaunchTime.getMillis() - instanceBootTimeControlElement.getMillis()) { instanceBucketNameControlElement = currentBucketName; instanceBootTimeControlElement = currentBucketDate; } } else { instanceBucketNameControlElement = currentBucketName; instanceBootTimeControlElement = currentBucketDate; } } final String result = prefix + instanceBucketNameControlElement + "-" + instanceBootTimeControlElement; writeToS3(securityGroup, result, instanceId); } } @Autowired  SaveSecurityGroupsPlugin(final SecurityGroupProvider securityGroupProvider,
                                    final S3Service s3Writer,
                                    @Value("${fullstop.instanceData.bucketName}") final String bucketName); @Override boolean supports(final CloudTrailEvent event); @Override void processEvent(final CloudTrailEvent event); String getSecurityGroup(final List<String> securityGroupIds, final Region region, final String accountId); static final String SECURITY_GROUPS; static final String JSON; }
@Test public void testProcessEvent() throws Exception { plugin.processEvent(cloudTrailEvent); verify(mockSecurityGroupProvider).getSecurityGroup(eq(singletonList("sg-24051988")), eq(getRegion(EU_WEST_1)), eq("123456789111")); verify(mockS3Service, times(5)).listCommonPrefixesS3Objects(eq("saved-security-groups"), eq("123456789111/eu-west-1/2015/06/18/")); verify(mockS3Service, times(3)).putObjectToS3(eq("saved-security-groups"), anyString(), anyString(), any(), any()); }