Pybatfish

repository·master·Indexed 42 days ago

https://github.com/batfish/pybatfish

Python API and utilities for Batfish, a network validation tool that analyzes device configurations to provide correctness guarantees for security, reliability, and compliance. Pybatfish version 0.36.0 provides a client for managing networks, snapshots, and the Reference Library, as well as assertion helpers for programmatic verification and data models for analyzing ACLs, flows, and routing data.

Tokens
44.5K
Snippets
165
Records
208
Agent score
53%

What's inside Pybatfish

  1. What is Pybatfish and Batfish?

    master

    Pybatfish is a Python client for Batfish.

    Batfish is a network validation tool that analyzes network device configurations to provide correctness guarantees for security, reliability, and compliance. It builds complete models of network behavior to find violations of built-in, user-defined, and best-practice network policies.

    Key characteristics:

    • Pre-deployment validation: Validates configuration changes before they are deployed to prevent errors.
    • No direct access required: Batfish does not need direct access to live network devices; it only requires device configurations. Analysis can be enhanced with BGP routes or topology information (LLDP/CDP).
  2. Query the Batfish network model with Batfish Questions

    master

    Batfish allows you to query the network model and parsed configuration settings using a set of specialized queries known as 'Batfish Questions'. These questions enable you to verify network behavior, inspect topology, and validate routing and forwarding logic based on the configuration and data you have provided to Batfish.

    Questions are categorized into several domains, including:

    • Config Properties: Inspecting parsed configuration settings.
    • Topology: Querying the network structure and device relationships.
    • Routing Protocols: Analyzing protocol-specific behavior (e.g., BGP, OSPF).
    • Routing Tables: Inspecting RIB/FIB data.
    • Forwarding: Simulating traffic flows and reachability.
    • Filters: Verifying ACLs and policy application.
    • Snapshot: Querying metadata about the network snapshot.
    • IPsec & VXLAN/EVPN: Specialized queries for tunnel and overlay technologies.
    • Resolving Specifiers: Checking how names or specifiers resolve.
    • Differential Questions: Comparing differences between two network snapshots.

    For detailed instructions on how to execute these questions, refer to the interacting with Batfish notebooks.

  3. Specify Packet Locations with Location Specifiers

    master

    Location specifiers define where a packet is in the network (origin or termination point).

    Types of Locations

    • InterfaceLocation: At the interface. For nodeName, this implies all interfaces on that node. For nodeName[interfaceName], it is that specific interface.
    • InterfaceLinkLocation: On the link connected to the interface. Use the @enter(locationInterface) syntax to specify packets entering an interface.

    Automatic IP Assignment

    • For InterfaceLocation, Batfish uses the interface's configured IP(s).
    • For InterfaceLinkLocation, Batfish uses hypothetical host IPs in the subnet (excluding the interface IP and the first/last subnet addresses).
    • If Batfish cannot determine a viable IP, the location is ignored unless an explicit source IP is provided.
  4. Use Pybatfish assertion helpers to validate network configurations

    master
    Pybatfish provides a suite of assertion helpers in the pybatfish.client.asserts module. These helpers allow you to programmatically verify that your network configuration meets specific correctness criteria (e.g., ensuring certain routes exist, checking reachability, or verifying ACL behavior). These assertions are typically used after performing a bf.init_cfg() and subsequent analysis commands to automate network validation.
  5. Use Pybatfish datamodel primitives for filtering answers

    master

    Pybatfish provides several primitive classes in pybatfish.datamodel.primitives to represent basic network elements and structured outputs. These classes are used in the return values of Batfish questions and can be used to filter or process your results.

    Key primitive classes include:

    • Edge: Represents a connection between two points.
    • Interface: Represents a network interface.
    • FileLines: A structured output representing lines from a file.
    from pybatfish.datamodel.primitives import Edge, Interface, FileLines
  6. Understand Batfish Specifier Grammars

    master

    Batfish questions use specifiers to define sets of entities like nodes, interfaces, or IP addresses. Specifiers support rich selection logic including regex, set operations, and functional selectors.

    Core Grammar Rules

    • Set Operations: Use , for union, & for intersection, and \ for difference. Example: (node1, node2)\node1 resolves to node2.
    • Regexes: Must be enclosed in forward slashes (e.g., /abc/). Batfish uses Java's regex syntax. Matching is case-insensitive.
    • Escaping Names: Names starting with a digit, quote, or slash, or containing spaces/special characters (e.g., [ ] ( ) @ ! # $ % ^ ; ? < > = { }), must be double-quoted. Example: "1startsWithADigit" or "has space".
    • Enum Set Grammar: Many specifiers follow a common pattern for selecting values from a set:
      • val1: Singleton set.
      • /val.*/: Regex match.
      • val1, val2: Union of values.
      • ! val1: All values except val1.
      • !/regex/: All values not matching the regex.
  7. Understand the types of data supported by Batfish

    master

    Batfish accepts two primary categories of data to build and enhance its network model:

    1. Device Configurations: The actual configuration files from the network devices (e.g., Cisco, Arista, AWS) which Batfish parses to understand the network topology and behavior.
    2. Supplemental Data: Additional data used to enhance or augment the existing network model.
  8. Correctness checks supported by Batfish

    master

    Batfish supports several categories of network correctness checks:

    • Configuration Compliance: Validates that settings (MTU, AAA, NTP, logging, etc.) match templates and flags undefined or unreferenced structures like ACLs or route maps. It can also ensure devices are only accessible via SSHv2 with non-null passwords.
    • Reliability: Checks if end-to-end reachability is maintained during single-link or single-device failures and ensures global reachability for critical services like DNS.
    • Security: Verifies that sensitive services are restricted to specific subnets/devices and ensures traffic paths traverse expected elements (e.g., firewalls) or meet ECMP requirements.
    • Change Analysis: Compares current vs. planned configurations to ensure reachability remains identical, verifies that ACL/firewall changes cause no collateral damage, and checks functional equivalence between different vendor configurations.
  9. Understand configuration annotation labels

    master

    When using the configuration annotation tool, the following labels are used to categorize why a line or portion of a line is being ignored by Batfish:

    • UNRECOGNIZED SYNTAX: Batfish does not understand the content of the line.
    • PARTIALLY UNSUPPORTED: Batfish does not support the specific feature mentioned in the line.
    • SILENTLY IGNORED: Batfish deems the line irrelevant to its network model (these lines are not typically reported in initIssues).

    Each annotation is followed by the specific part of the line to which the annotation applies.

  10. Organize device configuration files in a Batfish snapshot

    master

    For most vendors, configuration files must be placed in a configs folder located directly below the top-level snapshot folder. Batfish recursively reads all files within this folder and its sub-folders. You can mix files from different vendors in the same directory.

    Exceptions to the configs folder rule:

    • AWS: Use a folder named aws_configs.
    • Azure: Use a folder named azure_configs.
    • Check Point: Use a folder named checkpoint_management (parallel to configs).
    • SONiC: Use a folder named sonic_configs.
  11. Specify external BGP announcements

    master

    To analyze how your network handles specific external prefixes or to simulate routes coming from an external peer, you can provide external BGP announcements.

    Setup Requirements:

    1. Create a file named external_bgp_announcements.json.
    2. Place it directly inside the top-level snapshot folder.

    Announcement Schema:

    • type: Either ebgp_sent (attributes as sent by the external peer) or ebgp_received (attributes after local import policy processing).
    • network: The prefix (e.g., 4.0.0.0/8).
    • nextHopIp: The next hop IP.
    • srcIp: The source IP.
    • dstNode: The destination node in your network.
    • dstIp: The destination IP.
    • srcProtocol: The protocol (e.g., AGGREGATE).
    • originType: The origin type (e.g., egp).
    • localPreference: BGP local preference.
    • med: Multi-Exit Discriminator.
    • asPath: An array of AS path segments.
    • communities: An array of community values.
    • dstVrf: The destination VRF.
    {
      "Announcements": [
          {
              "type" : "ebgp_sent",
              "network" : "4.0.0.0/8",
              "nextHopIp" : "10.14.22.4",
              "srcIp" : "10.14.22.4",
              "dstNode" : "as1border2",
              "dstIp" : "10.14.22.1",
              "srcProtocol" : "AGGREGATE",
              "originType" : "egp",
              "localPreference" : 0,
              "med" : 20,
              "originatorIp" : "0.0.0.0",
              "asPath" : [ [ 1239 ], [7018, 7019]],
              "communities" : [ 262145 ],
              "dstVrf":"default",
              "clusterList":[]
          }
      ]
    }
  12. Explore Batfish analysis and validation via Jupyter notebooks

    master

    The jupyter_notebooks directory contains a collection of notebooks demonstrating various network analysis and validation tasks using Batfish. These notebooks serve as practical guides for different use cases, ranging from basic configuration checks to complex failure-impact analysis. All network snapshots required by these notebooks are located in the networks subfolder of the repository.

    Available notebook categories include:

    Basics

    • Getting Started with Batfish: Overview of Batfish capabilities.
    • Pandas Examples: Using Pandas to manipulate Batfish query results.
    • Validating Configuration Settings: Validating settings like NTP, TACACS, AAA, and SNMP.
    • Uncovering Configuration and Behavior Drift: Comparing two network snapshots to find differences in settings or behavior.

    ACLs and Firewalls

    • Analyzing ACLs and Firewall Rules: General analysis of access control lists and firewall rules.
    • Provably Safe ACL and Firewall Changes: Workflow for ensuring rule changes are correct and safe.
    • Safely refactoring ACLs and firewall rules: Understanding the impact of refactoring complex rules.

    Cloud Networking

    • Analyzing public and hybrid cloud networks: Analysis of cloud and hybrid network environments.

    Forwarding Analysis

    • Introduction to Forwarding Analysis: Using traceroute and reachability queries to analyze forwarding paths.
    • Introduction to Forwarding Change Validation: Ensuring network changes do not impact packet forwarding.

    Failure-impact Analysis

    • Analyzing the Impact of Failures: Conducting Chaos Monkey style testing to see how failures affect the network.

    Routing Analysis

    • Introduction to Route Analysis: Analyzing routing tables computed by Batfish.
    • Introduction to BGP Analysis: Analyzing BGP session configurations.
    • Analyzing Routing Policies: Analyzing routing policies using Batfish.