Alias creation using API

Started by trumee, October 28, 2023, 02:59:53 PM

Previous topic - Next topic
Hello,

I want to create a 'new' alias using the API. I am able to modify an existing alias but get the following error when creating a new alias:

$curl -X POST -d '{"address":"192.168.42.42"}' -H "Content-Type: application/json" -k -u $key:$secret https://opnsense/api/firewall/alias_util/add/test
{"status":"failed","status_msg":"nonexistent alias test"}


How do i create a new alias using the API?

Here is a python script which creates a json file for upload.


import json
import uuid
import csv

with open('opnsense_aliases.json') as user_file:
  parsed_json = json.load(user_file)

cur_items=parsed_json['aliases']['alias']
with open('/tmp/pfsense_alias.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
    #print(row)
    item_name = row['name']
    item_data = row['data']
    item_type = row['type']
    item_description = row['description']

    if len(row['data'].split(" "))>1:
       item_data = "\n".join(row['data'].split(" "))

    item_uuid = str(uuid.uuid4())



    y =    {'enabled': '1',
            'name': item_name,
            'type': item_type,
            'proto': '',
            'interface': '',
            'counters': '0',
            'updatefreq': '',
            'content': item_data,
            'categories': '',
            'description': item_description
          }

    cur_items[item_uuid] = y
print(json.dumps(parsed_json,  indent=2))

Quote from: trumee on October 28, 2023, 04:09:31 PM
Here is a python script which creates a json file for upload.

@trumee would you be willing to explain how to use your script?

March 07, 2024, 06:24:52 AM #3 Last Edit: March 07, 2024, 06:30:50 AM by skatopn
Quote from: neptunus on November 03, 2023, 06:47:06 PM
Quote from: trumee on October 28, 2023, 04:09:31 PM
Here is a python script which creates a json file for upload.

@trumee would you be willing to explain how to use your script?

Steps:

  • Install Python if you do not already have it
  • Install any of the required Python modules if you don't have them (json, uuid, csv) - see below *
  • Save the script as a *.py file
  • Download your current list of Aliases from your OPNsense device: Firewall>Aliases>'download' (button bottom right of the Alias list) - save the file as 'opnsense_aliases.json'
  • Create a CSV file called 'pfsense_alias.csv' with four columns called 'name', 'data', 'type' and 'description' in the first row, and which then contains your new Aliases, one per row (be sure the 'name' field meets the name constraints for Aliases)
  • Update the two 'with open...' script lines to include the full path to each of the above files
  • Run the script
  • Upload the resultant json output file back into your OPNsense device: Firewall>Aliases>'upload' (next to the 'download' button)

What does this script do?

  • It reads in the current list of aliases you downloaded from your device into a Python variable (dict)
  • It then reads in the CSV file containing your list of new (additional) aliases, also into a Python dict
  • It ADDS (appends) all the new aliases to the current list (no deletions, assuming you don't experience a uuid collision)
  • It then saves the new expanded list of aliases over the previously downloaded alias json file

Obviously, you need Python and the required modules to run the script.

*How to install Python modules after installing Python:

python -m pip install MODULE_NAME

You can create the CSV file in any Spreadsheet or text program (e.g. Notepad, Excel, LibreOffice Calc, Notepad++, etc.)

This only uses the API in the sense that whenever you are interacting with the OPNsense GUI, the API is being used under the hood. As a GUI user, you are just clicking buttons and typing text. You are not required to use tools such as curl or wget or Powershell Invoke-RestMethod in order to use this script.

HTH  8)

My previous post is how I believe the whole process should work, but it appears there may currently be a bug with the alias.json import function.

See this thread for details:
https://forum.opnsense.org/index.php?topic=39297.0

Quote from: skatopn on March 08, 2024, 04:12:40 AM
My previous post is how I believe the whole process should work, but it appears there may currently be a bug with the alias.json import function.

See this thread for details:
https://forum.opnsense.org/index.php?topic=39297.0

The JSON import bug I referred to is not a bug. It was my error.

But below I have added a slightly updated version of the Python script posted by @trumee above:

import json
import uuid
import csv

jsonfile = 'C:/path/to/aliases.json'
csvfile = 'C:/path/to/opnsense_alias.csv'
# jsonfile = '/path/to/aliases.json'
# csvfile = '/path/to/opnsense_alias.csv'

with open(jsonfile) as user_file:
    parsed_json = json.load(user_file)

cur_items=parsed_json['aliases']['alias']

with open(csvfile, newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        #print(row)
        item_name = row['name']
        item_data = row['data']
        item_type = row['type']
        item_description = row['description']

        if len(row['data'].split(" "))>1:
            item_data = "\n".join(row['data'].split(" "))

        item_uuid = str(uuid.uuid4())

        new_alias =    {'enabled': '1',
                'name': item_name,
                'type': item_type,
                'proto': '',
                'interface': '',
                'counters': '0',
                'updatefreq': '',
                'content': item_data,
                'categories': '',
                'description': item_description
            }

        cur_items[item_uuid] = new_alias

with open(jsonfile, 'w', encoding="utf-8") as f:
    json.dump(parsed_json, f, indent=2)


Instructions from my previous post remaining unchanged.