def set_rule_state(self, action): """Setup the pfSense Rules platform.""" import pprint, sys from PfsenseFauxapi.PfsenseFauxapi import PfsenseFauxapi _LOGGER.debug("Connecting to pfSense firewall to change rule states.") try: # Setup connection with devices/cloud FauxapiLib = PfsenseFauxapi(self._host, self._api_key, self._access_token, debug=True) # Get the current set of filters filters = FauxapiLib.config_get('filter') except: _LOGGER.error( "Problem retrieving rule set from pfSense host: %s. Likely due to API key or secret.", self._host) i = 0 for rule in filters['rule']: if (rule.get('tracker') == self._tracker_id): _LOGGER.info("Found rule changing state rule: %s", self._rule_name) if (action == True): if ('disabled' in rule): del filters['rule'][i]['disabled'] _LOGGER.debug( "Rule %s enabled in config (this has not been pushed back to firewall yet!)", self._rule_name) elif (action == False): filters['rule'][i]['disabled'] = "" _LOGGER.debug( "Rule %s disabled in config (this has not been pushed back to firewall yet!)", self._rule_name) i = i + 1 try: _LOGGER.debug("Sending updated rule set to pfSense firewall") # Push the config back to pfSense filters = FauxapiLib.config_set(filters, 'filter') _LOGGER.debug( "Reloading the config on pfSense firewall to accept rule changes" ) # Reload the config FauxapiLib.send_event("filter reload") except: _LOGGER.error( "Problem sending & reloading rule set from pfSense host: %s. Likely due to API key or secret.", self._host)
def test_send_event(mock_requests_post): fauxapi = PfsenseFauxapi(host=None, apikey=None, apisecret=None) mock_requests_post.return_value.status_code = 200 mock_requests_post.return_value.text = '{"callid": "5c8dc64e73efc", "action": "send_event", "message": "ok"}' response = fauxapi.send_event(None) assert response['action'] == 'send_event' assert response['message'] == 'ok'
# interface_stats - NB: the real interface name, not an interface alias such as "WAN" or "LAN" # ============================================================================= print(json.dumps( FauxapiLib.interface_stats('em0')) ) # gateway_status # ============================================================================= print(json.dumps( FauxapiLib.gateway_status()) ) # send_event - filter reload # ============================================================================= print(json.dumps( FauxapiLib.send_event('filter reload')) ) # send_event - interface all reload # ============================================================================= print(json.dumps( FauxapiLib.send_event('interface all reload')) ) # rule_get - get all rules # ============================================================================= print(json.dumps( FauxapiLib.rule_get()) ) # rule_get - get rule number 5
# system_stats # ============================================================================= print(json.dumps(FauxapiLib.system_stats())) # interface_stats - NB: the real interface name, not an interface alias such as "WAN" or "LAN" # ============================================================================= print(json.dumps(FauxapiLib.interface_stats('em0'))) # gateway_status # ============================================================================= print(json.dumps(FauxapiLib.gateway_status())) # send_event - filter reload # ============================================================================= print(json.dumps(FauxapiLib.send_event('filter reload'))) # send_event - interface all reload # ============================================================================= print(json.dumps(FauxapiLib.send_event('interface all reload'))) # rule_get - get all rules # ============================================================================= print(json.dumps(FauxapiLib.rule_get())) # rule_get - get rule number 5 # ============================================================================= print(json.dumps(FauxapiLib.rule_get(5))) # alias_update_urltables # =============================================================================