# Buggy code
def load_ecr_repository_images(neo4j_session, data, region, aws_update_tag):
    query = """
    MERGE (repo_image:ECRRepositoryImage{id: {RepositoryImageUri}})
    ON CREATE SET repo_image.firstseen = timestamp()
    SET repo_image.lastupdated = {aws_update_tag}, repo_image.tag = {ImageTag},
        repo_image.uri = {RepositoryImageUri}
    WITH repo_image

    MERGE (image:ECRImage{id: {ImageDigest}})
    ON CREATE SET image.firstseen = timestamp(), image.digest = {ImageDigest}
    SET image.lastupdated = {aws_update_tag},
    image.region = {Region}
    WITH repo_image, image
    MERGE (repo_image)-[r1:IMAGE]->(image)
    ON CREATE SET r1.firstseen = timestamp()
    SET r1.lastupdated = {aws_update_tag}
    WITH repo_image

    MATCH (repo:ECRRepository{uri: {RepositoryUri}})
    MERGE (repo)-[r2:REPO_IMAGE]->(repo_image)
    ON CREATE SET r2.firstseen = timestamp()
    SET r2.lastupdated = {aws_update_tag}
    """

    for repo_uri, repo_images in data.items():
        for repo_image in repo_images:
            image_tag = repo_image.get("imageTag", "")
            # TODO this assumes image tags and uris are immutable
            repo_image_uri = f"{repo_uri}:{image_tag}" if image_tag else repo_uri
            neo4j_session.run(
                query,
                RepositoryImageUri=repo_image_uri,
                ImageDigest=repo_image["imageDigest"],
                ImageTag=image_tag,
                RepositoryUri=repo_uri,
                aws_update_tag=aws_update_tag,
                Region=region,
            )


# 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_repository_images(neo4j_session, data, region, aws_update_tag):
    query = """
    MERGE (repo_image:ECRRepositoryImage{id: {RepositoryImageUri}})
    ON CREATE SET repo_image.firstseen = timestamp()
    SET repo_image.lastupdated = {aws_update_tag}, repo_image.tag = {ImageTag},
        repo_image.uri = {RepositoryImageUri}
    WITH repo_image

    MERGE (image:ECRImage{id: {ImageDigest}})
    ON CREATE SET image.firstseen = timestamp(), image.digest = {ImageDigest}
    SET image.lastupdated = {aws_update_tag},
    image.region = {Region}
    WITH repo_image, image
    MERGE (repo_image)-[r1:IMAGE]->(image)
    ON CREATE SET r1.firstseen = timestamp()
    SET r1.lastupdated = {aws_update_tag}
    WITH repo_image

    MATCH (repo:ECRRepository{uri: {RepositoryUri}})
    MERGE (repo)-[r2:REPO_IMAGE]->(repo_image)
    ON CREATE SET r2.firstseen = timestamp()
    SET r2.lastupdated = {aws_update_tag}
    """

    for repo_uri, repo_images in data.items():
        for repo_image in repo_images:
            image_tag = repo_image.get("imageTag", "")
            # TODO this assumes image tags and uris are immutable
            repo_image_uri = f"{repo_uri}:{image_tag}" if image_tag else repo_uri
            neo4j_session.run(
                query,
                RepositoryImageUri=repo_image_uri,
                ImageDigest=repo_image["imageDigest"],
                ImageTag=image_tag,
                RepositoryUri=repo_uri,
                aws_update_tag=aws_update_tag,
                Region=region,
            ).consume()  # See issue #440