Code Monkey home page Code Monkey logo

pyfortinet's Introduction

Fortinet Python library

This project is a Python library for Fortinet products' REST API. Currently, only Fortimanager is supported, but extensions for various products are planned. Current state is rather a Proof of Concept.

Features

  • FMG API
    • Low level API access via passing dict to various calls (add, get, set, update, exec)
    • Automatic login (Currently, only user/password authentication is supported)
    • Automatic locking in workspace mode (Currently, only ADOM locking is supported)
    • High level API using all kind of objects (see some examples below)
      • Only couple of objects are supported yet (being POC project), but extension is planned for most used functions!
      • Task handling with waiting and callback function (to support progress bar, logging, etc.)
    • Async code is supported

Planned features

  • FMG API
    • Extended authentication capabilities (token, SAML)
    • Extended locking capabilities to support object and package level locking and fallback feature to ADOM locking
    • Proxy FortiOS API calls using objects of FortiOS API
  • FortiOS API
    • Similar capabilities to FMG API

Quick examples

FMG

from pyfortinet import FMG
from pyfortinet.fmg_api.firewall import Address
from pyfortinet.fmg_api.common import F

config = {
    "base_url": "https://myfmg.com",
    "username": "myuser",
    "password": "verysecret",
    "adom": "root",
    "verify": False
}
with FMG(**config) as fmg:
    # create and assign new address object to FMG
    server1 = fmg.get_obj(Address(name="server1", subnet="192.168.0.1/32"))
    server1.add()
    # get exact address object from FMG
    server2 = fmg.get(Address, F(name="server2")).first()
    print(server2.name)
    # get list of address object from FMG
    servers = fmg.get(Address, F(name__like="server%"))
    print(servers.data)

    # Low level call is also supported in case object was not available
    address_request = {
        "url": "/pm/config/adom/root/obj/firewall/address",
        "filter": [["name", "==", "test-address"]],
    }
    result = fmg.get(address_request)
    print(result.data["data"])

AsyncFMG

Async code is also supported via AsyncFMG. Intention is to support async frameworks like FastAPI.

import asyncio
from pyfortinet import AsyncFMG
from pyfortinet.fmg_api.firewall import Address
from pyfortinet.fmg_api.common import F

async def main():
    config = {
        "base_url": "https://myfmg.com",
        "username": "myuser",
        "password": "verysecret",
        "adom": "root",
        "verify": False
    }
    async with AsyncFMG(**config) as fmg:
        # create and assign new address object to FMG
        server1 = fmg.get_obj(Address(name="server1", subnet="192.168.0.1/32"))
        await server1.add()
        # get list of addresses from FMG and pick the first element
        address = (await fmg.get(Address, F(name__like="test-firewall-addr%"))).first()
        # update address object
        address.subnet = "10.0.1.0/24"
        result = await address.update()

asyncio.run(main())

FMGBase

FMGBase is a lower level API class which implements base functions. Purpose is to serve the inherited higher level classes like FMG.

from pyfortinet import FMGBase

config = {
    "base_url": "https://myfmg.com",
    "username": "myuser",
    "password": "verysecret",
    "adom": "root",
    "verify": False
}
with FMGBase(**config) as fmg:
    ver = fmg.get_version()
    print(ver)
    
    address_request = {
        "url": f"/pm/config/adom/root/obj/firewall/address",
        "filter": [["name", "==", "test-address"]],
    }
    result = fmg.get(address_request)
    print(result.data["data"])

Extending FMG capabilities

It is possible to extend FMG capabilities by inheriting from this FMG class and adding custom methods to it. Please check Fortimanager Template Sync project for an example of how to do it!

Installation

The library can be installed via PIP from PyPi.

# basic install
pip install pyfortinet

# install with async dependency
pip isntall pyfortinet[async]

# enable rich traceback
pip install pyfortinet[rich]

# simple install with all feature dependency
pip install pyfortinet[all]

pyfortinet's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

lendlsmith

pyfortinet's Issues

Pyhton 3.9 support doesn't work

Marino, Mariana reported:
Is not working with python 3.9 (TypeError: unsupported operand type(s) for |: 'NoneType' and '_SpecialGenericAlias') - there are some type hints with pipe which is not available in python 3.9. Since pyfortinet says it should be available for python 3.9 (and we are using 3.9 in Nautobot) is that something that can be updated?

Beside that, numerous other type hints are failing under 3.9.

Feature Request : add a model device without SN but with psk

Fortimanager api support adding model device without SN but with a PSK and a valid Platform_str; We use this a lot to provision our Fortigates; Would be great to have this supported as we never know the SN of the device which will be installed.

F filter is incapable of filtering for "-" and " " space characters

Need to implement substitute options, like substitute="-" or =" ", defaulted to "-".
So F function can convert this on the fly. Example:
F(tcp_portrange=["1234"]) would translate to ["tcp-portrange", "==", "1234"]

if space is needed then:
F(some_field="qweqwe", _substitude=" ") -> ["some field", "==", "qweqwe"]

Feature request: Clone Adom

We are currently building an autmoated provisioning tool and the first basic task we need is to clone our main Adom which contains a basic set of config and templates.

By supporting the clone adom, we could use this library and extend it to our needs. I will also provide pull request with new features.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.