def test_config_reload(mock_requests_get):

    fauxapi = PfsenseFauxapi(host=None, apikey=None, apisecret=None)
    mock_requests_get.return_value.status_code = 200
    mock_requests_get.return_value.text = '{"callid": "5c8dbe9a6331e", "action": "config_reload", "message": "ok"}'

    response = fauxapi.config_reload()
    assert response['action'] == 'config_reload'
    assert response['message'] == 'ok'
Пример #2
0
    }
}
print(json.dumps(
    FauxapiLib.config_patch(config_patch)
))

# config get the full configuration again so we can manually confirm it has been restored
config = FauxapiLib.config_get()
print(json.dumps(
    config
))

# config reload
# =============================================================================
print(json.dumps(
    FauxapiLib.config_reload())
)

# config backuo
# =============================================================================
print(json.dumps(
    FauxapiLib.config_backup())
)

# config_backup_list
# =============================================================================
print(json.dumps(
    FauxapiLib.config_backup_list())
)

# # config_restore
Пример #3
0
class PfSense:
    """
    PfSense Thingy...
    ...
    Methods
    -------
    get_openvpn_settings(self) -> dict:
    set_pfsense_config(self, data: dict, vpnid: list, refresh: bool = None) -> dict:
    get_pf_openvpn_clients(self) -> dict:
    get_pf_openvpn_locations(self, vpn_clients: dict) -> set:
    """
    def __init__(self):
        self.host = getenv("HOST_ADDRESS")
        self.port = 443
        self.key = getenv("FAUXAPI_KEY")
        self.secret = getenv("FAUXAPI_SECRET")
        try:
            PfsenseFauxapi(f"{self.host}:{self.port}", self.key, self.secret)
        except PfsenseFauxapiException as e:
            print(f"error: {str(e)}")
        else:
            self.pfapi = PfsenseFauxapi(f"{self.host}:{self.port}", self.key,
                                        self.secret)

    def get_openvpn_settings(self) -> dict:
        try:
            pf_openvpn_settings = self.pfapi.config_get('openvpn')
        except PfsenseFauxapiException as e:
            return {"error": str(e)}
        else:
            return pf_openvpn_settings

    def set_pfsense_config(self,
                           data: dict,
                           vpnid: list,
                           refresh: bool = None) -> dict:
        """
        Parameters
        ----------
        data : dict
            .
        vpnid : list
            .
        refresh : bool
            .
        """
        try:
            resp = self.pfapi.config_set(data, 'openvpn')
        except PfsenseFauxapiException as e:
            return {"error": str(e)}
        else:
            if refresh:
                self.pfapi.config_reload()
            if len(vpnid):
                for vid in vpnid:
                    data = self.pfapi.function_call({
                        "function":
                        "openvpn_restart_by_vpnid",
                        "args": ["client", f"{vid}"]
                    })
            return resp

    def get_pf_openvpn_clients(self) -> dict:
        clients: list = []
        vpn_clients = self.get_openvpn_settings()
        if "error" in vpn_clients.keys():
            return vpn_clients

        locations: set = self.get_pf_openvpn_locations(vpn_clients)
        for vpnclient in vpn_clients["openvpn-client"]:
            clients.append(vpnclient["server_addr"])

        return {"clients": clients, "locations": list(locations)}

    def get_pf_openvpn_locations(self, vpn_clients: dict) -> set:
        """
        Parameters
        ----------
        vpn_clients : dict
            .
        """
        locations = set()
        for client in vpn_clients["openvpn-client"]:
            loc = is_vpn_address(client["server_addr"])
            if loc is not None:
                locations.add(loc[1].lower())
        return locations
Пример #4
0
        'dnsserver':
        config['system']['dnsserver']
        if 'dnsserver' in config['system'] else [''],
        'hostname':
        config['system']['hostname'],
    }
}
print(json.dumps(FauxapiLib.config_patch(config_patch)))

# config get the full configuration again so we can manually confirm it has been restored
config = FauxapiLib.config_get()
print(json.dumps(config))

# config reload
# =============================================================================
print(json.dumps(FauxapiLib.config_reload()))

# config backuo
# =============================================================================
print(json.dumps(FauxapiLib.config_backup()))

# config_backup_list
# =============================================================================
print(json.dumps(FauxapiLib.config_backup_list()))

# # config_restore
# =============================================================================
# print(json.dumps(
#     FauxapiLib.config_restore('/cf/conf/backup/config-1530604754.xml'))
# )