# Buggy code
def load_ecr_image_scan_findings(neo4j_session, data, aws_update_tag):
    """
    Creates the path (:Risk:CVE:ECRScanFinding)-[:AFFECTS]->(:Package)-[:DEPLOYED]->(:ECRImage)
    :param neo4j_session: The Neo4j session object
    :param data: A dict that has been run through transform_ecr_scan_finding_attributes().
    :param aws_update_tag: The AWS update tag
    """
    query = """
    UNWIND {Risks} as risk
        MATCH (image:ECRImage{id: {ImageDigest}})
        MERGE (pkg:Package{id: risk.package_version + "|" + risk.package_name})
        ON CREATE SET pkg.firstseen = timestamp(),
        pkg.name = risk.package_name,
        pkg.version = risk.package_version
        SET pkg.lastupdated = {aws_update_tag}
        WITH image, risk, pkg

        MERGE (pkg)-[r1:DEPLOYED]->(image)
        ON CREATE SET r1.firstseen = timestamp()
        SET r1.lastupdated = {aws_update_tag}
        WITH pkg, risk

        MERGE (r:Risk:CVE:ECRScanFinding{id: risk.name})
        ON CREATE SET r.firstseen = timestamp(),
        r.name = risk.name,
        r.severity = risk.severity
        SET r.lastupdated = {aws_update_tag},
        r.uri = risk.uri,
        r.cvss2_score = risk.CVSS2_SCORE

        MERGE (r)-[a:AFFECTS]->(pkg)
        ON CREATE SET a.firstseen = timestamp()
        SET r.lastupdated = {aws_update_tag}
        """
    neo4j_session.run(
        query,
        Risks=data["findings"],
        ImageDigest=data["imageDigest"],
        aws_update_tag=aws_update_tag,
    )


# Traceback (ConnectionResetError)
Traceback (most recent call last):
File "{PATH}/lib/python3.6/site-packages/cartography/sync.py", line 71, in run
stage_func(neo4j_session, config)
File "{OTHER_PATH}/intelmodules/lyft/aws.py", line 234, in lyft_start_aws_ingestion
common_job_parameters
File "{PATH}/lib/python3.6/site-packages/cartography/intel/aws/__init__.py", line 98, in _sync_multiple_accounts
_sync_one_account(neo4j_session, boto3_session, account_id, sync_tag, common_job_parameters)
File "{PATH}/lib/python3.6/site-packages/cartography/intel/aws/__init__.py", line 46, in _sync_one_account
ecr.sync(neo4j_session, boto3_session, regions, account_id, sync_tag, common_job_parameters)
File "{PATH}/lib/python3.6/site-packages/cartography/util.py", line 58, in timed
result = method(*args, **kwargs)
File "{PATH}/lib/python3.6/site-packages/cartography/intel/aws/ecr.py", line 241, in sync
load_ecr_repository_images(neo4j_session, image_data, region, aws_update_tag)
File "{PATH}/lib/python3.6/site-packages/cartography/util.py", line 58, in timed
result = method(*args, **kwargs)
File "{PATH}/lib/python3.6/site-packages/cartography/intel/aws/ecr.py", line 171, in load_ecr_repository_images
Region=region,
File "{PATH}/lib/python3.6/site-packages/neo4j/__init__.py", line 502, in run
self._connection.send()
File "{PATH}/lib/python3.6/site-packages/neobolt/direct.py", line 394, in send
self._send()
File "{PATH}/lib/python3.6/site-packages/neobolt/direct.py", line 409, in _send
self.socket.sendall(data)
File "/usr/lib/python3.6/ssl.py", line 965, in sendall
v = self.send(data[count:])
File "/usr/lib/python3.6/ssl.py", line 935, in send
return self._sslobj.write(data)
File "/usr/lib/python3.6/ssl.py", line 636, in write
return self._sslobj.write(data)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "{PATH}/lib/python3.6/site-packages/neo4j/__init__.py", line 399, in close
self._connection.sync()
File "{PATH}/lib/python3.6/site-packages/neobolt/direct.py", line 505, in sync
self.send()
File "{PATH}/lib/python3.6/site-packages/neobolt/direct.py", line 394, in send
self._send()
File "{PATH}/lib/python3.6/site-packages/neobolt/direct.py", line 409, in _send
self.socket.sendall(data)
File "/usr/lib/python3.6/ssl.py", line 965, in sendall
v = self.send(data[count:])
File "/usr/lib/python3.6/ssl.py", line 935, in send
return self._sslobj.write(data)
File "/usr/lib/python3.6/ssl.py", line 636, in write
return self._sslobj.write(data)
BrokenPipeError: [Errno 32] Broken pipe

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "{OTHER_PATH}/intelmodules/syncgraph.py", line 220, in <module>
main(argv)
File "{OTHER_PATH}/intelmodules/syncgraph.py", line 210, in main
return cartography.sync.run_with_config(sync, config)
File "{PATH}/lib/python3.6/site-packages/cartography/sync.py", line 145, in run_with_config
return sync.run(neo4j_driver, config)
File "{PATH}/lib/python3.6/site-packages/cartography/sync.py", line 78, in run
logger.info("Finishing sync stage '%s'", stage_name)
File "{PATH}/lib/python3.6/site-packages/neo4j/__init__.py", line 374, in __exit__
self.close()
File "{PATH}/lib/python3.6/site-packages/neo4j/__init__.py", line 401, in close
ServiceUnavailable, SessionError):
NameError: name 'ServiceUnavailable' is not defined

# Fixed code
def load_ecr_image_scan_findings(neo4j_session, data, aws_update_tag):
    """
    Creates the path (:Risk:CVE:ECRScanFinding)-[:AFFECTS]->(:Package)-[:DEPLOYED]->(:ECRImage)
    :param neo4j_session: The Neo4j session object
    :param data: A dict that has been run through transform_ecr_scan_finding_attributes().
    :param aws_update_tag: The AWS update tag
    """
    query = """
    UNWIND {Risks} as risk
        MATCH (image:ECRImage{id: {ImageDigest}})
        MERGE (pkg:Package{id: risk.package_version + "|" + risk.package_name})
        ON CREATE SET pkg.firstseen = timestamp(),
        pkg.name = risk.package_name,
        pkg.version = risk.package_version
        SET pkg.lastupdated = {aws_update_tag}
        WITH image, risk, pkg

        MERGE (pkg)-[r1:DEPLOYED]->(image)
        ON CREATE SET r1.firstseen = timestamp()
        SET r1.lastupdated = {aws_update_tag}
        WITH pkg, risk

        MERGE (r:Risk:CVE:ECRScanFinding{id: risk.name})
        ON CREATE SET r.firstseen = timestamp(),
        r.name = risk.name,
        r.severity = risk.severity
        SET r.lastupdated = {aws_update_tag},
        r.uri = risk.uri,
        r.cvss2_score = risk.CVSS2_SCORE

        MERGE (r)-[a:AFFECTS]->(pkg)
        ON CREATE SET a.firstseen = timestamp()
        SET r.lastupdated = {aws_update_tag}
        """
    neo4j_session.run(
        query,
        Risks=data["findings"],
        ImageDigest=data["imageDigest"],
        aws_update_tag=aws_update_tag,
    ).consume()  # See issue #440