Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lua: dataset api for lua - v9 #12377

Closed
wants to merge 5 commits into from
Closed

Conversation

jasonish
Copy link
Member

@jasonish jasonish commented Jan 12, 2025

Rebase of #12121, with change to "require"
API.

For example, to use the script must:

local dataset = require("suricata.dataset")

SV_BRANCH=OISF/suricata-verify#2229

Ticket: https://redmine.openinfosecfoundation.org/issues/7243

victorjulien and others added 5 commits January 12, 2025 10:18
Only useful when debugging. Add a prefix and a stack size indication.
Add optional `thread_init` function support. This function is called per
script, per thread to allow a user to initialize the lua state.
dataset.new
  create a dataset object in lua

<dataset>:get
  gets a reference to an existing dataset

<dataset>:add
  returns 1 if a new entry was added
  returns 0 if entry was already in the set

Example:
```
function init (args)
    local needs = {}
    needs["packet"] = tostring(true)
    return needs
end

function thread_init (args)
    conn_new, dataset.new()
    ret, err conn_new:get("conn-seen")
    if err ~= nil then
        SCLogWarning("dataset warning: " .. err)
        return 0
    end
end

function match (args)
    ipver, srcip, dstip, proto, sp, dp = SCFlowTuple()
    str = ipver .. ":<" .. srcip .. ">:<" .. dstip .. ">:" .. dp

    ret, err = conn_new:add(str, #str);
    if ret == 1 then
        SCLogInfo(str .. " => " .. ret)
    end
    return ret
end
```

Ticket: OISF#7243.
Re-work the Lua dataset lib to be required into a user script like:

    local dataset = require("suricata.data")

The main difference from loading it into global space is providing a
custom require function (as we removed it in the sandbox) and load it on
demand, returning a table to the module.
This is mainly for header sanitization to avoid pulling in detect
modules into the Lua sandbox definition.

Plus if we namespace modules with names like "suricata.dataset", it
probably makes sense to keep those modules in their own files.
@jasonish jasonish mentioned this pull request Jan 12, 2025
Copy link

codecov bot commented Jan 12, 2025

Codecov Report

Attention: Patch coverage is 78.37838% with 16 lines in your changes missing coverage. Please review.

Project coverage is 82.48%. Comparing base (ad7ff1c) to head (4954ae4).
Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12377      +/-   ##
==========================================
- Coverage   82.49%   82.48%   -0.01%     
==========================================
  Files         912      914       +2     
  Lines      258220   258314      +94     
==========================================
+ Hits       213006   213069      +63     
- Misses      45214    45245      +31     
Flag Coverage Δ
fuzzcorpus 60.37% <0.00%> (-0.04%) ⬇️
livemode 19.39% <0.00%> (-0.02%) ⬇️
pcap 44.33% <0.00%> (-0.02%) ⬇️
suricata-verify 63.26% <75.67%> (+0.01%) ⬆️
unittests 58.06% <10.81%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@jasonish jasonish marked this pull request as ready for review January 12, 2025 17:17
@victorjulien victorjulien added this to the 8.0 milestone Jan 12, 2025
@suricata-qa
Copy link

WARNING:

field baseline test %
SURI_TLPR1_stats_chk
.uptime 651 622 95.55%

Pipeline 24172

@victorjulien
Copy link
Member

Merged in #12379, thanks!

@victorjulien
Copy link
Member

Nice, thanks for making the require stuff work 🎉

@jasonish jasonish deleted the lua-dataset/v9 branch January 13, 2025 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants