u/zyphermonkey

[HOW TO] Toggle OPNsense Firewall Rule via Home Assistant (HASS)
▲ 17 r/opnsense+1 crossposts

[HOW TO] Toggle OPNsense Firewall Rule via Home Assistant (HASS)

Not sure how many people will find this helpful, but wanted to share my documentation for toggling firewall rules via Home Assistant.

https://gist.github.com/zyphermonkey/bb039afa3d382389c60c4d3a0200efb4

Toggle OPNsense Firewall Rule via Home Assistant (HASS)

OPNsense

Create Account for HASS to authenticate with

System > Access > Users

  1. Create account in opnsense with All pages privileges
  2. Click Create and download API key for this user
  3. Create basic auth string for use with cURL.
    1. combine key:secret
    2. base64 encode
    3. prefix with "Basic "
  4. Validate auth string via simple curl command
    curl -k https://192.168.1.1/api/core/system/status \
    -H "Authorization: $AUTH_STRING"
    

Create Alias

For my scenario I want to block a specific MAC so I got it via the DHCP Leases page.
Services>ISC DHCPv4[legacy]>Leases

  1. Go to Firewall > Aliases > New
  2. Info
    • Enabled: checked
    • Name: <Hostname>
      This can be anything you want, but I set it to the Hostname because it makes the most sense.
    • Type: MAC address
    • Content: <mac_address_recorded_from_leases_page>
  3. Apply

Create Firwall Rule

If you don't already have a rule you want to toggle of if you want to create one just for initial testing with HASS.

  1. Go to Firewall > Rules[new]
  2. Create whatever

Get Firewall Rule UUID

  1. Go to Firewall > Rules[new]
  2. Open Browers developer tools (F12)
  3. Click edit on rule
  4. The latest line in the Network tab in Developer Tools should be a GET request to /api/firewall/filter/get_rule/<uuid>
    1. Record uuid for use in HASS commands

Home Assistant (HASS)

Create opnsense auth var in secrets.yaml`

  1. Open secrets.yaml and create new variable with base64 encoded api key from OPNsense.
    opnsense_api_auth: "Basic <base64_encoded key:secret>"
    

Create sensor

Home Assistant needs to be able to know the current state of the firewall rule to effectively toggle it (and show it's status on dashboards).

configuration.yaml

sensor:
  - platform: rest
    name: opnsense_fw_rule_state_yourrulenamehere
    resource: https://192.168.1.1/api/firewall/filter/get_rule/<uuid>
    method: GET
    value_template: "{{ value_json.rule.enabled }}"
    headers:
      Authorization: !secret opnsense_api_auth
    verify_ssl: false

Create switch template & rest commands

  1. Create switch template in configuration.yaml
    template:
      - switch:
          - name: internet_access_yourrulenamehere
            unique_id: internet_access_yourrulenamehere
            state: "{{ is_state('sensor.opnsense_fw_rule_state_yourrulenamehere', '1') }}"
            turn_on:
              service: rest_command.opnsense_rule_on_yourrulenamehere
            turn_off:
              service: rest_command.opnsense_rule_off_yourrulenamehere
    
  2. Create rest commands (apply, enable, disable) in configuration.yaml
    rest_command:
      opnsense_apply:
        url: https://192.168.1.1/api/firewall/filter/apply
        method: POST
        headers:
          Authorization: !secret opnsense_api_auth
        verify_ssl: false
    
      opnsense_rule_on_yourrulenamehere:
        url: https://192.168.1.1/api/firewall/filter/toggle_rule/<uuid>/1
        method: POST
        headers:
          Authorization: !secret opnsense_api_auth
        verify_ssl: false
    
      opnsense_rule_off_yourrulenamehere:
        url: https://192.168.1.1/api/firewall/filter/toggle_rule/<uuid>/0
        method: POST
        headers:
          Authorization: !secret opnsense_api_auth
        verify_ssl: false
    

Validate

Ensure changes maded to secrets.yaml and configuration.yaml are stable check your configuration after saving.

  1. Settings > Developer tools > Check configuration

Use in your dashboards/automations

You should now be able to add the switch and sensor to your dashboards with the following:

  • entity: switch.internet_access_yourrulenamehere
  • entity: sensor.opnsense_fw_rule_state_yourrulenamehere
u/zyphermonkey — 1 day ago