Ejemplo n.º 1
0
    def test_downloadResults_unableToDownload(self, s3):
        client, bucket, bucket_name = s3
        target = "infosec.mozilla.org"
        path = "/abc/def"  # A path that does not exist

        # Ensure we have matching objects in the test S3 bucket to rule out other failure scenario
        client.put_object(Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_direnum.json'.format(target))
        client.put_object(Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_websearch.json'.format(target))
        test_apigw_event = {"body": '{"target": "' + target + '"}'}
        test_context = None
        results_handler = ResultsHandler(client,
                                         bucket_name,
                                         results_path=path)
        response = results_handler.downloadResults(test_apigw_event,
                                                   test_context)

        assert type(response) is dict
        assert response['statusCode'] == 500
        assert 'isBase64Encoded' not in response
        assert 'Content-Disposition' not in response['headers']
        assert response['headers']['Content-Type'] == "application/json"
        assert 'unable' in response['body'].lower()
Ejemplo n.º 2
0
    def test_downloadResults_partialResults(self, s3):
        client, bucket, bucket_name = s3
        target = "infosec.mozilla.org"
        # We only have some (partial) results in the S3 bucket
        client.put_object(ACL='authenticated-read',
                          Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_direnum.json'.format(target))
        client.put_object(ACL='authenticated-read',
                          Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_websearch.json'.format(target))
        client.put_object(ACL='authenticated-read',
                          Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_tcpscan.json'.format(target))

        test_apigw_event = {"body": '{"target": "' + target + '"}'}
        test_context = None
        results_handler = ResultsHandler(
            s3_client=client,
            bucket=bucket_name,
            results_path=TEST_SCAN_RESULTS_BASE_PATH)
        response = results_handler.downloadResults(test_apigw_event,
                                                   test_context)
        assert response['statusCode'] == 202
        assert response['isBase64Encoded'] is True
        assert 'Content-Disposition' in response['headers']
        assert response['headers']['Content-Type'] == "application/gzip"
Ejemplo n.º 3
0
    def test_getResults_fullResults(self, s3):
        client, bucket, bucket_name = s3
        target = "infosec.mozilla.org"
        # Ensure we have all matching objects in the test S3 bucket
        client.put_object(Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_direnum.json'.format(target))
        client.put_object(Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_websearch.json'.format(target))
        client.put_object(Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_portscan.json'.format(target))
        client.put_object(Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_httpobservatory.json'.format(target))
        client.put_object(Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_tlsobservatory.json'.format(target))
        client.put_object(Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_sshobservatory.json'.format(target))
        test_event = {"body": '{"target": "' + target + '"}'}
        test_context = None
        results_handler = ResultsHandler(
            client, bucket_name, results_path=TEST_SCAN_RESULTS_BASE_PATH)
        response = results_handler.getResults(test_event, test_context)

        assert type(response) is dict
        assert response['statusCode'] == 200
        assert response['isBase64Encoded'] is True
        assert 'Content-Disposition' in response['headers']
        assert response['headers']['Content-Type'] == "application/gzip"
Ejemplo n.º 4
0
    def test_generateDownloadLink_invalidEvent(self, s3):
        client, bucket, bucket_name = s3
        invalid_event = {"TEST": "TEST"}
        test_context = None
        results_handler = ResultsHandler(
            client, bucket_name, results_path=TEST_SCAN_RESULTS_BASE_PATH)
        response = results_handler.generateDownloadLink(
            invalid_event, test_context)

        assert response is False
Ejemplo n.º 5
0
    def test_downloadResults_invalidEvent(self, s3):
        client, bucket, bucket_name = s3
        invalid_event = {"TEST": "TEST"}
        test_context = None
        results_handler = ResultsHandler(
            client, bucket_name, results_path=TEST_SCAN_RESULTS_BASE_PATH)
        response = results_handler.downloadResults(invalid_event, test_context)

        assert type(response) is dict
        assert response['statusCode'] == 400
        assert 'unrecognized' in response['body'].lower()
Ejemplo n.º 6
0
    def test_generateDownloadLink_fullResults(self, s3):
        client, bucket, bucket_name = s3
        target = "infosec.mozilla.org"
        # Ensure we have all matching objects in the test S3 bucket
        client.put_object(ACL='authenticated-read',
                          Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_direnum.json'.format(target))
        client.put_object(ACL='authenticated-read',
                          Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_websearch.json'.format(target))
        client.put_object(ACL='authenticated-read',
                          Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_tcpscan.json'.format(target))
        client.put_object(ACL='authenticated-read',
                          Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_httpobservatory.json'.format(target))
        client.put_object(ACL='authenticated-read',
                          Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_tlsobservatory.json'.format(target))
        client.put_object(ACL='authenticated-read',
                          Bucket=bucket_name,
                          Body=b'ABCD',
                          Key='{}_sshobservatory.json'.format(target))

        test_stepf_event = {"target": target}
        test_context = None
        results_handler = ResultsHandler(
            s3_client=client,
            bucket=bucket_name,
            results_path=TEST_SCAN_RESULTS_BASE_PATH)
        response = results_handler.generateDownloadLink(
            test_stepf_event, test_context)

        assert type(response) is dict
        assert response['status'] == 200
        assert type(response['output']) is dict

        url = urlparse(response['url'])
        query = parse_qs(url.query)

        assert type(query) is dict
        assert url.scheme == "https"
        assert url.netloc == "{}.s3.amazonaws.com".format(bucket_name)
        assert url.path == "/results/{}.tgz".format(target)
        assert "AWSAccessKeyId" in query
        assert "Expires" in query
        assert "Signature" in query
Ejemplo n.º 7
0
    def test_generateDownloadLink_noResults(self, s3):
        client, bucket, bucket_name = s3
        target = "infosec.mozilla.org"
        # No objects in the S3 bucket, should return no results
        test_stepf_event = {"target": target}
        test_context = None
        results_handler = ResultsHandler(
            client, bucket_name, results_path=TEST_SCAN_RESULTS_BASE_PATH)
        response = results_handler.generateDownloadLink(
            test_stepf_event, test_context)

        assert type(response) is dict
        assert response['status'] == 404
        assert response['message'] == 'No results found for target'
Ejemplo n.º 8
0
    def test_getResults_noResults(self, s3):
        client, bucket, bucket_name = s3
        target = "infosec.mozilla.org"
        # No objects in the S3 bucket, should return no results
        test_event = {"body": '{"target": "' + target + '"}'}
        test_context = None
        results_handler = ResultsHandler(
            client, bucket_name, results_path=TEST_SCAN_RESULTS_BASE_PATH)
        response = results_handler.getResults(test_event, test_context)

        assert type(response) is dict
        assert response['statusCode'] == 404
        assert 'isBase64Encoded' not in response
        assert 'Content-Disposition' not in response['headers']
        assert response['headers']['Content-Type'] == "application/json"
        assert 'no' in response['body'].lower()
Ejemplo n.º 9
0
 def test_creation(self):
     results_handler = ResultsHandler()
     assert type(results_handler) is ResultsHandler
Ejemplo n.º 10
0
def download_results(event, context):
    results_handler = ResultsHandler(s3_client=S3_CLIENT, logger=logger)
    response = results_handler.getResults(event, context)
    return response
Ejemplo n.º 11
0
def check_results(event, context):
    results_handler = ResultsHandler(s3_client=S3_CLIENT, logger=logger)
    response = results_handler.generateDownloadLink(event, context)
    return response