Esempio n. 1
0
def test_collector(historical_role, buckets, mock_lambda_environment,
                   swag_accounts, current_s3_table):
    from historical.s3.collector import handler

    now = datetime.utcnow().replace(tzinfo=None, microsecond=0)
    create_event = CloudwatchEventFactory(
        detail=DetailFactory(requestParameters={"bucketName": "testbucket1"},
                             source="aws.s3",
                             eventName="CreateBucket",
                             eventTime=now))
    data = json.dumps(create_event, default=serialize)
    data = KinesisRecordsFactory(
        records=[KinesisRecordFactory(kinesis=KinesisDataFactory(data=data))])
    data = json.dumps(data, default=serialize)
    data = json.loads(data)

    handler(data, None)
    result = list(CurrentS3Model.query("arn:aws:s3:::testbucket1"))
    assert len(result) == 1
    # Verify that the tags are duplicated in the top level and configuration:
    assert len(result[0].Tags.attribute_values) == len(
        result[0].configuration.attribute_values["Tags"]) == 1
    assert result[0].Tags.attribute_values["theBucketName"] == \
           result[0].configuration.attribute_values["Tags"]["theBucketName"] == "testbucket1"  # noqa

    # Polling (make sure the date is included):
    polling_event = CloudwatchEventFactory(
        detail=DetailFactory(requestParameters={
            "bucketName": "testbucket1",
            "creationDate": now
        },
                             source="aws.s3",
                             eventName="DescribeBucket",
                             eventTime=now))
    data = json.dumps(polling_event, default=serialize)
    data = KinesisRecordsFactory(
        records=[KinesisRecordFactory(kinesis=KinesisDataFactory(data=data))])
    data = json.dumps(data, default=serialize)
    data = json.loads(data)

    handler(data, None)
    assert CurrentS3Model.count() == 1

    # Load the config and verify the polling timestamp is in there:
    result = list(CurrentS3Model.query("arn:aws:s3:::testbucket1"))
    assert result[0].configuration["CreationDate"] == now.isoformat() + "Z"

    # And deletion:
    delete_event = CloudwatchEventFactory(
        detail=DetailFactory(requestParameters={"bucketName": "testbucket1"},
                             source="aws.s3",
                             eventName="DeleteBucket",
                             eventTime=now))
    data = json.dumps(delete_event, default=serialize)
    data = KinesisRecordsFactory(
        records=[KinesisRecordFactory(kinesis=KinesisDataFactory(data=data))])
    data = json.dumps(data, default=serialize)
    data = json.loads(data)
    handler(data, None)
    assert CurrentS3Model.count() == 0
Esempio n. 2
0
def test_collector(historical_role, mock_lambda_environment, security_groups,
                   current_security_group_table):
    from historical.security_group.models import CurrentSecurityGroupModel
    from historical.security_group.collector import handler
    event = CloudwatchEventFactory(detail=DetailFactory(
        requestParameters={'groupId': security_groups['GroupId']},
        eventName='CreateSecurityGroup'), )
    data = json.dumps(event, default=serialize)
    data = KinesisRecordsFactory(
        records=[KinesisRecordFactory(kinesis=KinesisDataFactory(data=data))])
    data = json.dumps(data, default=serialize)
    data = json.loads(data)

    handler(data, None)

    assert CurrentSecurityGroupModel.count() == 1

    event = CloudwatchEventFactory(detail=DetailFactory(
        requestParameters={'groupId': security_groups['GroupId']},
        eventName='DeleteSecurityGroup'), )
    data = json.dumps(event, default=serialize)
    data = KinesisRecordsFactory(
        records=[KinesisRecordFactory(kinesis=KinesisDataFactory(data=data))])
    data = json.dumps(data, default=serialize)
    data = json.loads(data)

    handler(data, None)

    assert CurrentSecurityGroupModel.count() == 0
Esempio n. 3
0
def test_collector(historical_role, mock_lambda_environment, vpcs,
                   current_vpc_table):
    from historical.vpc.models import CurrentVPCModel
    from historical.vpc.collector import handler
    event = CloudwatchEventFactory(detail=DetailFactory(
        requestParameters={'vpcId': vpcs['VpcId']}, eventName='CreateVpc'), )
    data = json.dumps(event, default=serialize)
    data = KinesisRecordsFactory(
        records=[KinesisRecordFactory(kinesis=KinesisDataFactory(data=data))])
    data = json.dumps(data, default=serialize)
    data = json.loads(data)

    handler(data, None)

    assert CurrentVPCModel.count() == 1

    event = CloudwatchEventFactory(detail=DetailFactory(
        requestParameters={'vpcId': vpcs['VpcId']}, eventName='DeleteVpc'), )
    data = json.dumps(event, default=serialize)
    data = KinesisRecordsFactory(
        records=[KinesisRecordFactory(kinesis=KinesisDataFactory(data=data))])
    data = json.dumps(data, default=serialize)
    data = json.loads(data)

    handler(data, None)

    assert CurrentVPCModel.count() == 0
Esempio n. 4
0
def test_collector_on_deleted_bucket(historical_role, buckets, mock_lambda_environment, swag_accounts,
                                     current_s3_table):
    from historical.s3.collector import handler

    # If an event arrives on a bucket that is deleted, then it should skip
    # and wait until the Deletion event arrives.
    create_event = CloudwatchEventFactory(
        detail=DetailFactory(
            requestParameters={
                "bucketName": "not-a-bucket"
            },
            source="aws.s3",
            eventName="PutBucketPolicy",
        )
    )
    create_event_data = json.dumps(create_event, default=serialize)
    data = KinesisRecordsFactory(
        records=[
            KinesisRecordFactory(
                kinesis=KinesisDataFactory(data=create_event_data))
        ]
    )
    data = json.dumps(data, default=serialize)
    data = json.loads(data)

    handler(data, None)
    assert CurrentS3Model.count() == 0
def test_collector(historical_role, mock_lambda_environment, {{cookiecutter.technology_slug}}s):
    from .models import Current{{cookiecutter.technology_slug | titlecase}}Model
    from .collector import handler

    # TODO modify event
    event = CloudwatchEventFactory(
        detail=DetailFactory(
            requestParameters={}, # e.g. {'vpcId': vpcs['VpcId']},
            eventName='', # e.g. 'CreateVpc'
        ),
    )
    data = json.dumps(event, default=serialize)
    data = RecordsFactory(
        records=[
            KinesisRecordFactory(
                kinesis=KinesisDataFactory(data=data))
        ]
    )
    data = json.dumps(data, default=serialize)
    data = json.loads(data)

    handler(data, None)

    assert Current{{cookiecutter.technology_slug | titlecase}}Model.count() == 1

    # TODO modify delete event
    event = CloudwatchEventFactory(
        detail=DetailFactory(
            requestParameters={}, # e.g. {'vpcId': vpcs['VpcId']},
            eventName='', # e.g. ''DeleteVpc'
        ),