Skip to content

Enhance parsing of JSON for firewall IP aliases #8277

Closed

Description

@gglockner

Important notices

Before you add a new report, we ask you kindly to acknowledge the following:

Is your feature request related to a problem? Please describe.

Amazon Web Services (AWS) publishes their IP address ranges in JSON format. Here is the documentation and here is the direct link to the JSON data. Using OPNsense 25.1 (amd64), I would like to use Firewall > Aliases to import this data as Type "URL Table in JSON format (IPs)". I have looked at the source in src/opnsense/scripts/filter/lib/alias/uri.py; as best as I can determine, this JSON data cannot be parsed by this OPNsense feature.

Describe the solution you like

The "Path expression" field should support more query options, so that this data can be used for firewall rules. Consider jq, a popular tool to process JSON data. The following Linux shell command gives all IPv4 address ranges from AWS:

curl https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[].ip_prefix'

jq is quite powerful, so you could do a more sophisticated filter such as: all IPv4 ranges for AWS EC2 in the us-east-1 region:

curl https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[] | select(.region=="us-east-1") | select(.service=="EC2") | .ip_prefix'

This type of logic would be difficult to replicate using the limited single-line parsing implementation in src/opnsense/scripts/filter/lib/alias/uri.py.

Describe alternatives you considered

It would be straightforward to implement this in src/opnsense/scripts/filter/lib/alias/uri.py if the jq shell package were installed by default in OPNsense. Alternately, there are a few pure-Python data transformation libraries, but they either have a nonstandard syntax or implement only a subset of jq syntax.

Additional context

I started discussing this topic in #8107 but split this into a separate Github issue.

Activity

  1. self-assigned this
    on Feb 3, 2025
  2. AdSchellevis commented on Feb 3, 2025

    @AdSchellevis
    Member

    I might need to revise my original answer, for some reason I missed the native python binding available via https://github.com/opnsense/ports/blob/master/textproc/py-jq, which is more or less a stub around the library itself.
    Usually these are not too complicated in maintenance.

    I might do a small proof of concept with this, there are pro's and cons here. jqlang is used by quite some people, has an online tester available (https://jqplay.org/), but is not very easy to learn for less experienced users.

    Offering only simple patterns a.b, a.*.b might be easier to explain, but has the downside of not being extendable in the future. As we already released the a.b pattern in 25.1, there's also the small issue of different formats in its current state.

  3. gglockner commented on Feb 3, 2025

    @gglockner
    Author

    If we agree that this would be implemented via a Python front-end to jq, I would be happy to contribute the code for this. The key thing that prevents me from implementing it myself and submitting a pull request is that I cannot figure out how to ensure that the packages for jq and textproc are installed by default.

    I agree there is a very small chance this could break some installations. However, the chance should be tiny considering this feature was introduced merely a few days ago in 25.1.

  4. AdSchellevis commented on Feb 3, 2025

    @AdSchellevis
    Member

    let me fiddle a bit and get back to you, compiling the port in this case is rather quick

  5. Zerophase commented on Feb 5, 2025

    @Zerophase

    Are the Spamhaus block lists valid for testing this?

    I believe it's valid JSON as well, but can't be interpreted line by line.

  6. AdSchellevis commented on Feb 5, 2025

    @AdSchellevis
    Member

    Are the Spamhaus block lists valid for testing this?

    I believe it's valid JSON as well, but can't be interpreted line by line.

    These should already work in 25.1, see #8107

  7. Zerophase commented on Feb 5, 2025

    @Zerophase

    Figured out what I was doing wrong. I was looking up JSONPath expression format and putting it in in the form of $.*.cidr, rather than just cidr. Most of the online path expression validators can't handle either form.

  8. AdSchellevis commented on Feb 5, 2025

    @AdSchellevis
    Member

    There's no documentation yet, other than what's in the help text, This ticket is about extending the support, possibly with jq expressions, JSONPath is another option, neither unfortunately exist by default in python.

  9. AdSchellevis commented on Feb 10, 2025

    @AdSchellevis
    Member

    Fiddled a bit with the jq python binding and the native jsonpath library for python, both work, but have pros and cons.
    jq is faster (native c), but the query format is not standardized. jsonpath has an rfc in draft mode (https://datatracker.ietf.org/doc/rfc9535/) has more in common with xpath, but is a bit more difficult to validate locally.

    Sample code below counting the number of us-east-1 / EC2 ranges:

    #!/usr/bin/env python3
    
    import ujson
    import jq
    import time
    from jsonpath_ng.ext import parse
    
    data = ujson.loads(open('/tmp/ip-ranges.json', 'rb').read())
    
    
    t_start = time.time()
    count = len(list(iter(jq.compile('.prefixes[] | select(.region=="us-east-1") | select(.service=="EC2") | .ip_prefix').input_value(data))))
    print("%0.2f seconds - %d ranges" % (time.time() - t_start, count))
    
    t_start = time.time()
    count = len(parse('$.prefixes[?region == "us-east-1" & service=="EC2"].ip_prefix').find(data))
    print("%0.2f seconds - %d ranges" % (time.time() - t_start, count))
    

    Haven't decided yet what we will integrate, but wanted to share the options we're considering in case anyone has arguments for one or the other.

  10. mcleanra commented on Feb 17, 2025

    @mcleanra

    Just throwing another use case out there. I would use this feature to create an alias for all of GitHub's IPv4 public IPs: https://api.github.com/meta

    curl https://api.github.com/meta | jq -r --arg pattern "\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b" '.. | select(test($pattern))?'

  11. AdSchellevis commented on Mar 12, 2025

    @AdSchellevis
    Member

    @gglockner this c7c0785 should do the trick (you do need to compile the dependancies first via the ports tree as these are not yet in OPNsense)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

featureAdding new functionalityroadmapMajor roadmap item

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions