Fixes #1327 replacing py2neo with neo4j#1402
Conversation
Rafiot
left a comment
There was a problem hiding this comment.
Alright, I did a very quick review of the code and I have a few notes.
Just making sure: you're using neo4j regularly and know what you're doing, right? I have not tested your code yet, this review is simply based on a quick read through.
pyproject.toml
Outdated
|
|
||
| brotli = [ "urllib3[broti] (>=2.6.3)" ] | ||
|
|
||
| neo4j = [ "neo4j (>=5.0)" ] |
There was a problem hiding this comment.
You also need to update the poetry.lock file.
There was a problem hiding this comment.
Done, also changed the lowest version to 5.26 to support dynamic labels as in https://neo4j.com/blog/developer/cypher-dynamism/
There was a problem hiding this comment.
Quick question on that: why not using the version 6.1 of the package?
There was a problem hiding this comment.
5.6 is the oldest version that supports the changes I made but I've changed it to 6.1 since the neo4j driver docs recommend using the latest driver as it supports older server versions.
|
Thank you @Rafiot for the valuable feedback. I will look into the comments and respond/make the necessary changes within the course of the week |
|
That looks great, can you just add the typing markings so mypy is happy? It's not a very big deal, but nice to have. |
|
I've added the types, should be good now |
|
Can you make sure to update the lock file? I'd like to get the action to pass. |
|
I just realized that I messed something up with the poetry.lock file, I ran poetry update instead of poetry add, let me fix this, I will let you know once it's done |
29247a3 to
079c12c
Compare
|
Just double checked everything. All should be good now. |
Problem Statement
We need to migrate from
py2neoto officialneo4jorneomodelsdriver because py2neo has been deprecated since 2021 as pointed out in issue #1327.Issue analysis & solution
pymisp/tools/neo4j.pywhich migrates events into a Neo4j graph, establishing relationships between the events, attributes and attribute values.neo4jbecause of the simplicity of the script. Using it allows better performance and support because it is the official Neo4j libneo4jthe Cypher queries were defined directly below is a summarized mapping of the old functions and the new ones:authenticate() + Graph()GraphDatabase.driver()with Bolt protocolNode()objectsCREATE (e:Event {$event.uuid})Relationship()objectsCREATE (a)-[:type]->(b)tx.begin()/tx.commit()session.execute_write(callback)tx.merge(subgraph)MERGEkeywordFurther implementation details are as follows:
ip-src,domain|ip). Neo4j label names only accept alphanumeric + underscores. Solution:re.sub(r'[^A-Za-z0-9_]', '_', a.type)withcontext managers to guarantee session/connection cleanup$parametersyntax, preventing Cypher injectionneo4j = [ "neo4j (>=5.0)" ]topyproject.tomlunder optional-dependenciesTesting
test/test_neo4j.pyto test the new version ofneo4j.py.MISPEvent.add_attribute()import_event()which is the main function