基于 REST 的 API 接口,适用于 pfSense 2.3.x 和 2.4.x,以便支持 DevOps
A REST API interface for pfSense 2.3.x, 2.4.x, 2.5.x to facilitate devops:-
Additionally available are a set of client libraries that hence make programmatic access and management of pfSense hosts for devops tasks feasible.
/etc/fauxapi/credentials.ini file on the pfSense host before
you continue, see the API Authentication section below./etc/fauxapi/pfsense_function_calls.txt file if you want to use
the function_call API method. You may wish to copy the sample /etc/fauxapi/pfsense_function_calls.sample.txt
as a starting point.config.xml file.pfctl -sr -vv command on the pfSense host.At its core FauxAPI simply reads the core pfSense config.xml file, converts it
to JSON and returns to the API caller. Similarly it can take a JSON formatted
configuration and write it to the pfSense config.xml and handles the required
reload operations. The ability to programmatically interface with a running
pfSense host(s) is enormously useful however it should also be obvious that this
provides the API user the ability to create configurations that can break your
pfSense system.
FauxAPI provides easy backup and restore API interfaces that by default store configuration backups on all configuration write operations thus it is very easy to roll-back even if the API user manages to deploy a "very broken" configuration.
Multiple sanity checks take place to make sure a user provided JSON config will
correctly convert into the (slightly quirky) pfSense XML config.xml format and
then reload as expected in the same way. However, because it is not a real
per-action application-layer interface it is still possible for the API caller
to create configuration changes that make no sense and can potentially disrupt
your pfSense system - as the package name states, it is a "Faux" API to pfSense
filling a gap in functionality with the current pfSense product.
Because FauxAPI is a utility that interfaces with the pfSense config.xml there
are some cases where reloading the configuration file is not enough and you
may need to "tickle" pfSense a little more to do what you want. This is not
common however a good example is getting newly defined network interfaces or
VLANs to be recognized. These situations are easily handled by calling the
send_event action with the payload interface reload all - see the example
included below and refer to a the resolution to Issue #10
NB: As at FauxAPI v1.2 the function_call action has been introduced that now provides the ability to issue function calls directly into pfSense.
Until the FauxAPI is added to the pfSense FreeBSD-ports tree you will need to install manually from root as shown:-
set fauxapi_base_package_url='https://raw.githubusercontent.com/ndejong/pfsense_fauxapi_packages/master'
set fauxapi_latest=`fetch -qo - ${fauxapi_base_package_url}/LATEST`
fetch ${fauxapi_base_package_url}/${fauxapi_latest}
pkg-static install ${fauxapi_latest}
Installation and de-installation is quite straight forward, further examples can
be found in the README.md located here.
Refer to the published package SHA256SUMS
Hint: if not already, consider installing the jq tool on your local machine (not
pfSense host) to pipe and manage JSON outputs from FauxAPI - https://stedolan.github.io/jq/
NB: you MUST at least setup your /etc/fauxapi/credentials.ini file on the
pfSense host before you continue, see the API Authentication section below.
A Python interface
to pfSense was perhaps the most desired end-goal at the onset of the FauxAPI
package project. Anyone that has tried to parse the pfSense config.xml files
using a Python based library will understand that things don't quite work out as
expected or desired.
The Python client-library can be easily installed from PyPi as such
pip3 install pfsense-fauxapi
Use of the package should be easy enough as shown
import pprint, sys
from PfsenseFauxapi.PfsenseFauxapi import PfsenseFauxapi
PfsenseFauxapi = PfsenseFauxapi('', '', '')
aliases = PfsenseFauxapi.config_get('aliases')
## perform some kind of manipulation to `aliases` here ##
pprint.pprint(PfsenseFauxapi.config_set(aliases, 'aliases'))
It is recommended to review the Python code examples to observe worked examples with the client library. Of small note is that the Python library supports the ability to get and set single sections of the pfSense system, not just the entire system configuration as with the Bash library.
Python examples
usergroup-management.py - example code that provides the ability to get_users,
add_user, manage_user, remove_user and perform the same functions on groups.update-aws-aliases.py - example code that pulls in the latest AWS ip-ranges.json
data, parses it and injects them into the pfSense aliases section if required.function-iterate.py - iterates (almost) all the FauxAPI functions to
confirm operation.As distinct from the Bash library as described below the Python pip also introduces a command-line tool to interact with the API, which makes a wide range of actions possible directly from the command line, for example
fauxapi --host 192.168.1.200 gateway_status | jq .
The Bash client library
makes it possible to add a line with source pfsense-fauxapi.sh to your bash script
and then access a pfSense host configuration directly as a JSON string
source pfsense-fauxapi.sh
export fauxapi_auth=$(fauxapi_auth )
fauxapi_config_get | jq .data.config > /tmp/config.json
## perform some kind of manipulation to `/tmp/config.json` here ##
fauxapi_config_set /tmp/config.json
It is recommended to review the commented out samples in the provided
fauxapi-sample.sh file that cover all possible FauxAPI calls to gain a better
idea on usage.
A NodeJS client has been developed by a third party and is available here
A PHP client has been developed by a third party and is available here
A deliberate design decision to decouple FauxAPI authentication from both the
pfSense user authentication and the pfSense config.xml system. This was done
to limit the possibility of an accidental API change that removes access to the
host. It also seems more prudent to only establish API user(s) manually via the
FauxAPI /etc/fauxapi/credentials.ini file - happy to receive feedback about
this approach.
The two sample FauxAPI keys (PFFAexample01 and PFFAexample02) and their
associated secrets in the sample credentials.sample.ini file are hard-coded to
be inoperative, you must create entirely new values before your client scripts
will be able to issue commands to FauxAPI.
You can start your own /etc/fauxapi/credentials.ini file by copying the sample
file provided in credentials.sample.ini
API authentication itself is performed on a per-call basis with the auth value inserted as an additional fauxapi-auth HTTP request header, it can be calculated as such:-
fauxapi-auth: :::
For example:-
fauxapi-auth: PFFA4797d073:20161119Z144328:833a45d8:9c4f96ab042f5140386178618be1ae40adc68dd9fd6b158fb82c99f3aaa2bb55
Where the <hash> value is calculated like so:-
= sha256()
NB: that the timestamp value is internally passed to the PHP strtotime function
which can interpret a wide variety of timestamp formats together with a timezone.
A nice tidy timestamp format that the strtotime PHP function is able to process
can be obtained using bash command date --utc +%Y%m%dZ%H%M%S where the Z
date-time seperator hence also specifies the UTC timezone.
This is all handled in the client libraries provided, but as can be seen it is relatively easy to implement even in a Bash shell script.
Getting the API credentials right seems to be a common source of confusion in getting started with FauxAPI because the rules about valid API keys and secret values are pedantic to help make ensure poor choices are not made.
The API key + API secret values that you will need to create in /etc/fauxapi/credentials.ini
have the following rules:-
clog tool$ clog /var/log/system.log | grep fauxapi
All configuration edits through FauxAPI create configuration backups in the same way as pfSense does with the webapp GUI.
These backups are available in the same way as edits through the pfSense GUI and are thus able to be reviewed and diff'd in the same way under Diagnostics->Backup & Restore->Config History.
Changes made through the FauxAPI carry configuration change descriptions that name the un
暂无开放 Issues,或尚未同步最近议题。