Skip to main content

Python tools for interacting with Palo Alto Networks REST API.

Project description

PyPanRestV2

PyPanRestV2 is a Python library designed to simplify interactions with Palo Alto Networks firewalls and Panorama via their REST API. It provides a higher level of abstraction, allowing users to manage firewalls and Panorama without needing to construct REST requests manually or work with XML for areas of the firewall configuration that still require it.


Features

  • High-Level Abstraction: Simplifies interaction with the Palo Alto Networks API.
  • Support for Firewalls and Panorama: Manage both individual firewalls and Panorama devices.
  • REST API Integration: Allows seamless communication with devices using REST API.
  • XML API Support: Handles XML API calls for configurations not yet available in REST API.
  • Convenient Pythonic Objects: Intuitive Python objects for interacting with specific sections of Palo Alto firewall configurations.
  • Error Handling: Custom exceptions for better error management and troubleshooting.

Installation

You can install PyPanRestV2 using pip:

pip install pypanrestv2

Alternatively, you can clone the repository and install it as a package for development:

# Clone the repository
git clone https://github.com/mrzepa/pypanrestv2.git

# Navigate to the project directory
cd pypanrestv2

# Install the package in development mode
pip install -e .

This will install the package and all required dependencies automatically. The -e flag installs the package in "editable" mode, which is useful if you plan to modify the code or contribute to the project.


Basic Usage

Import the Required Classes

Start by importing the necessary classes from the library:

from pypanrestv2 import Firewall, Panorama

Connect to a Firewall or Panorama Device

Create a Firewall or Panorama object by providing the required connection details:

For a Firewall:

firewall = Firewall(base_url="192.168.1.1", api_key="12345")

For Panorama:

panorama = Panorama(base_url="192.168.2.1", username="admin", password="my_password")

Common Use Cases

1. Managing Security Rules

from pypanrestv2.Policies import SecurityRules

# Create a new security rule
security_rule = SecurityRules(firewall, name='allow_web')
security_rule.from_ = {'member': ['trust']}
security_rule.to = {'member': ['untrust']}
security_rule.source = {'member': ['any']}
security_rule.destination = {'member': ['any']}
security_rule.application = {'member': ['web-browsing']}
security_rule.service = {'member': ['application-default']}
security_rule.action = 'allow'
security_rule.create()

# Modify an existing rule
existing_rule = SecurityRules(firewall, name='existing_rule')
existing_rule.refresh()  # Load current configuration
existing_rule.action = 'deny'
existing_rule.update()

2. Managing Address Objects

from pypanrestv2.Objects import Addresses

# Create a new address object
address = Addresses(firewall, name='web_server')
address.value = '192.168.1.100'
address.type = 'ip-netmask'
address.create()

# Get all address objects
all_addresses = Addresses.get_all(firewall)

3. Working with Panorama Policies and Rulebase

from pypanrestv2 import Panorama
from pypanrestv2.Policies import SecurityRules

# Initialize Panorama connection
panorama = Panorama(base_url='panorama.example.com', api_key='YOUR_API_KEY')

# Create a security rule in the pre-rulebase of a device group
security_rule = SecurityRules(panorama, name='allow_internal', rulebase='Pre')
security_rule.from_ = {'member': ['trust']}
security_rule.to = {'member': ['untrust']}
security_rule.source = {'member': ['any']}
security_rule.destination = {'member': ['any']}
security_rule.application = {'member': ['web-browsing']}
security_rule.service = {'member': ['application-default']}
security_rule.action = 'allow'
security_rule.create()

Repository

Visit the project's GitHub repository for source code, documentation, enhancements, and contributions:

PyPanRestV2 Repository on GitHub


Requirements

  • Python 3.11+
  • Palo Alto Networks Devices running PAN-OS 9.0+ or Panorama
  • Python dependencies:
    • dnspython
    • icecream
    • pycountry
    • python-dotenv
    • requests
    • tqdm
    • validators

API Documentation

The SDK provides access to the following main components:

Core Modules

  • Firewall/Panorama: Base connection and authentication
  • Policies: Security rules, NAT rules, and policy management
  • Objects: Address objects, service objects, and security profiles
  • Network: Interfaces, zones, and routing configuration
  • Device: System settings and device management

Error Handling

The SDK uses custom exceptions for better error handling:

from pypanrestv2.Exceptions import PANConnectionError, PANConfigError

try:
    firewall = Firewall(base_url='192.168.1.1', api_key='invalid_key')
    firewall.test_connection()
except PANConnectionError as e:
    print(f'Connection failed: {e}')
except PANConfigError as e:
    print(f'Configuration error: {e}')

Common errors and solutions:

  • PANConnectionError: Check network connectivity and API credentials
  • PANConfigError: Verify object names and configuration values
  • PANNotFoundError: Ensure referenced objects exist

Status and Updates

This SDK is actively maintained and regularly updated to support new PAN-OS versions. While not all API endpoints are implemented, core functionality is stable and production-ready. Check the GitHub repository for the latest updates and supported features.

Contributing

Contributions are welcome! If you want to report issues, request features, or contribute to the library:

  1. Fork the repository.
  2. Create a feature branch: git checkout -b my-feature.
  3. Commit your changes: git commit -m "Add detailed description of changes".
  4. Push to the branch: git push origin my-feature.
  5. Submit a pull request.

Be sure to check the documentation, if provided, before starting contributions.


License

This project is licensed under the MIT License. See the LICENSE file for details.


Author

Mark Rzepa mark@rzepa.com


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypanrestv2-2.1.13.tar.gz (60.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pypanrestv2-2.1.13-py3-none-any.whl (62.5 kB view details)

Uploaded Python 3

File details

Details for the file pypanrestv2-2.1.13.tar.gz.

File metadata

  • Download URL: pypanrestv2-2.1.13.tar.gz
  • Upload date:
  • Size: 60.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.9 Darwin/24.6.0

File hashes

Hashes for pypanrestv2-2.1.13.tar.gz
Algorithm Hash digest
SHA256 83f8b9776083f58c06810a3b475192c9dd9dd6168256edc4035a4f88debb3c4a
MD5 3a7352e6381f1c1af4be209c0b07472a
BLAKE2b-256 e2eb92595f95cce906e22d2beb1ae0cffb540d7daf876f6ee9b643ea139b829c

See more details on using hashes here.

File details

Details for the file pypanrestv2-2.1.13-py3-none-any.whl.

File metadata

  • Download URL: pypanrestv2-2.1.13-py3-none-any.whl
  • Upload date:
  • Size: 62.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.9 Darwin/24.6.0

File hashes

Hashes for pypanrestv2-2.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 d234bda501213bbf25436d85fa3ed5b12e327e6185fb87d3e47151c55f6c30f0
MD5 6ee05d81809569424649a514b6177d60
BLAKE2b-256 5cc49fe84cb6e3b881b0c4986352dad4db8d7e67c23ece0f56b913400fef59ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page