OPNsense Forum

English Forums => General Discussion => Topic started by: trumee on October 28, 2023, 02:59:53 PM

Title: Alias creation using API
Post by: trumee 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:

$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?
Title: Re: Alias creation using API
Post by: trumee on October 28, 2023, 04:09:31 PM
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))
Title: Re: Alias creation using API
Post by: 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?
Title: Re: Alias creation using API
Post by: skatopn 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:

What does this script do?

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)
Title: Re: Alias creation using API
Post by: 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 (https://forum.opnsense.org/index.php?topic=39297.0)
Title: Re: Alias creation using API
Post by: skatopn 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 (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.