OPNsense
  • Home
  • Help
  • Search
  • Login
  • Register

  • OPNsense Forum »
  • English Forums »
  • General Discussion »
  • Alias creation using API
« previous next »
  • Print
Pages: [1]

Author Topic: Alias creation using API  (Read 2211 times)

trumee

  • Newbie
  • *
  • Posts: 5
  • Karma: 4
    • View Profile
Alias creation using API
« on: October 28, 2023, 02:59:53 pm »
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:

Code: [Select]
$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?
Logged

trumee

  • Newbie
  • *
  • Posts: 5
  • Karma: 4
    • View Profile
Re: Alias creation using API
« Reply #1 on: October 28, 2023, 04:09:31 pm »
Here is a python script which creates a json file for upload.

Code: [Select]
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))
Logged

neptunus

  • Newbie
  • *
  • Posts: 12
  • Karma: 1
    • View Profile
Re: Alias creation using API
« Reply #2 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?
Logged

skatopn

  • Newbie
  • *
  • Posts: 37
  • Karma: 0
    • View Profile
Re: Alias creation using API
« Reply #3 on: March 07, 2024, 06:24:52 am »
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:

Code: [Select]
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)
« Last Edit: March 07, 2024, 06:30:50 am by skatopn »
Logged

skatopn

  • Newbie
  • *
  • Posts: 37
  • Karma: 0
    • View Profile
Re: Alias creation using API
« Reply #4 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
Logged

skatopn

  • Newbie
  • *
  • Posts: 37
  • Karma: 0
    • View Profile
Re: Alias creation using API
« Reply #5 on: March 11, 2024, 02:41:09 am »
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:
Code: [Select]
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.
Logged

  • Print
Pages: [1]
« previous next »
  • OPNsense Forum »
  • English Forums »
  • General Discussion »
  • Alias creation using API
 

OPNsense is an OSS project © Deciso B.V. 2015 - 2024 All rights reserved
  • SMF 2.0.19 | SMF © 2021, Simple Machines
    Privacy Policy
    | XHTML | RSS | WAP2