OpenGraph
- class bhopengraph.OpenGraph.OpenGraph(source_kind=None)[source]
Bases:
objectOpenGraph class for managing a graph structure compatible with BloodHound OpenGraph.
Follows BloodHound OpenGraph schema requirements and best practices.
Sources:
- Parameters:
source_kind (str)
- __init__(source_kind=None)[source]
Initialize an OpenGraph.
- Parameters:
source_kind (-) – Optional source kind for all nodes in the graph
- add_edge(edge)[source]
Add an edge to the graph if it doesn’t already exist and if the start and end nodes exist.
- Parameters:
edge (-) – Edge to add
- Returns:
True if edge was added, False if start or end node doesn’t exist
- Return type:
bool
- add_edge_without_validation(edge)[source]
Add an edge to the graph. If an edge with the same key already exists, it will be overwritten.
- Parameters:
edge (-) – Edge to add
- Returns:
True if edge was added, False if edge is invalid
- Return type:
bool
- add_edges_without_validation(edges)[source]
Add a list of edges to the graph without validation.
- Parameters:
edges (-) – List of edges to add
- Returns:
True if edges were added successfully
- Return type:
bool
- get_edges_by_kind(kind)[source]
Get all edges of a specific kind.
- Parameters:
kind (-) – Kind/type to filter by
- Returns:
List of edges with the specified kind
- Return type:
List[Edge]
- get_edges_from_node(node_id)[source]
Get all edges starting from a specific node.
- Parameters:
node_id (-) – ID of the source node
- Returns:
List of edges starting from the specified node
- Return type:
List[Edge]
- get_edges_to_node(node_id)[source]
Get all edges ending at a specific node.
- Parameters:
node_id (-) – ID of the destination node
- Returns:
List of edges ending at the specified node
- Return type:
List[Edge]
- get_isolated_edges()[source]
Get all edges that have no start or end node. These are edges that are not connected to any other nodes in the graph.
- Returns:
List of edges with no start or end node
- Return type:
List[Edge]
- get_isolated_edges_count()[source]
Get the total number of Isolated edges in the graph. These are edges that are not connected to any other nodes in the graph.
- Returns:
Number of Isolated edges
- Return type:
int
- get_edge_count()[source]
Get the total number of edges in the graph.
- Returns:
Number of edges
- Return type:
int
- add_node(node)[source]
Add a node to the graph.
- Parameters:
node (-) – Node to add
- Returns:
True if node was added, False if node with same ID already exists
- Return type:
bool
- add_node_without_validation(node)[source]
Add a node to the graph without validation.
- Parameters:
node (-) – Node to add
- Returns:
True if node was added, False if node is invalid
- Return type:
bool
- add_nodes_without_validation(nodes)[source]
Add a list of nodes to the graph without validation.
- Parameters:
nodes (-) – List of nodes to add
- Returns:
True if nodes were added successfully
- Return type:
bool
- get_node_by_id(id)[source]
Get a node by ID.
- Parameters:
id (-) – ID of the node to retrieve
- Returns:
The node if found, None otherwise
- Return type:
Node
- get_nodes_by_kind(kind)[source]
Get all nodes of a specific kind.
- Parameters:
kind (-) – Kind/type to filter by
- Returns:
List of nodes with the specified kind
- Return type:
List[Node]
- get_node_count()[source]
Get the total number of nodes in the graph.
- Returns:
Number of nodes
- Return type:
int
- get_isolated_nodes()[source]
- Get all nodes that have no edges.
These are nodes that are not connected to any other nodes in the graph.
- Returns:
List of nodes with no edges
- Return type:
List[Node]
- get_isolated_nodes_count()[source]
Get the total number of Isolated nodes in the graph. These are nodes that are not connected to any other nodes in the graph.
- Returns:
Number of Isolated nodes
- Return type:
int
- remove_node_by_id(id)[source]
Remove a node and all its associated edges from the graph.
- Parameters:
id (-) – ID of the node to remove
- Returns:
True if node was removed, False if node doesn’t exist
- Return type:
bool
- find_paths(start_id, end_id, max_depth=10)[source]
Find all paths between two nodes using BFS.
- Parameters:
start_id (-) – Starting node ID
end_id (-) – Target node ID
max_depth (-) – Maximum path length to search
- Returns:
List of paths, where each path is a list of node IDs
- Return type:
List[List[str]]
- get_connected_components()[source]
Find all connected components in the graph.
- Returns:
List of connected component sets
- Return type:
List[Set[str]]
- validate_graph()[source]
Validate the graph for common issues including node and edge validation.
Validates: - All nodes using their individual validate() methods - All edges using their individual validate() methods - Graph structure issues (isolated nodes/edges)
- Returns:
(is_valid, list_of_errors)
- Return type:
tuple[bool, list[str]]
- export_json(include_metadata=True, indent=None)[source]
Export the graph to JSON format compatible with BloodHound OpenGraph.
- Parameters:
include_metadata (-) – Whether to include metadata in the export
indent (None | int)
- Returns:
JSON string representation of the graph
- Return type:
str
- export_to_file(filename, include_metadata=True, indent=None)[source]
Export the graph to a JSON file.
- Parameters:
filename (-) – Name of the file to write
include_metadata (-) – Whether to include metadata in the export
indent (None | int)
- Returns:
True if export was successful, False otherwise
- Return type:
bool
- import_from_file(filename)[source]
Load graph data from a JSON file.
- Parameters:
filename (-) – Name of the file to read
- Returns:
True if load was successful, False otherwise
- Return type:
bool