Posted in : NetScaler By Simon Gottschlag

3 years ago

As I’ve stated, my knowledge of Lua and development is very limited. My good friend Ulrik pointed out to me that the code I was using (in my previous blog post) wasn’t optimized and helped me get it both faster and easier to understand. It’s actually works better and is about 21 times faster (the last one took about 533 437 nanoseconds/0,5ms to process and the new one takes 25 278 nanoseconds/0,025ms in my tests).
The Lua code:

function NSTEXT:FILTER_XML_LIST(pattern : NSTEXT, delimiter : NSTEXT, xmltag : NSTEXT) : NSTEXT
    local out = ""
    -- Examples:
    -- local input = "Group1, Group2, Grupp1, Grupp2, SG-AD-SAML-User, SG-AD-SAML-User-Office365, SG-AD-SAML-Admin ABC, SG-AD-SAML-Admin-Office365, SG-AD-Role-Employee"
    -- local pattern = "%-AD%-SAML%-"
    -- local delimiter = ","
    local input = self
    local value
    for match in (input..delimiter):gmatch("(.-)"..delimiter) do -- split up string
        match = match:gsub("^%s","") -- remove whitespace from beginning of string
        if (match:find(pattern)) then -- if pattern matches, add to output
            out = out .. "<" .. xmltag .. ">" .. match .. "</" .. xmltag .. ">"
        end
    end
    return(out)
end

A few things to note:

  • The last one did actually take the input and match it to lower, so if you entered ”Group” instead of ”group”, it didn’t work.
  • The processing used another function, which I’ve removed
  • I’ve tested this one with pattern matching for example ”SG-AD-SAML-User” etc.

If a user is member of the following groups:
Group1, Group2, Grupp1, Grupp2, SG-AD-SAML-User, SG-AD-SAML-User-Office365, SG-AD-SAML-Admin ABC, SG-AD-SAML-Admin-Office365, SG-AD-Role-Employee
And we want to extract everything that contains -AD-SAML-, we’ll use an expression like this:

HTTP.REQ.USER.GROUPS.FILTER_XML_LIST("%-AD%-SAML%-",",","xmltag")

This will output all the groups containing ”-AD-SAML-” into an XML-list  with the tag ”xmltag” like this:
<xmltag>SG-AD-SAML-User<xmltag></xmltag>SG-AD-SAML-User-Office365<xmltag></xmltag>SG-AD-SAML-Admin ABC<xmltag></xmltag>SG-AD-SAML-Admin-Office365</xmltag>
As always, please leave a comment if you have any feedback!

Tags : Lua, NetScaler, Policy Extension, XML

Add comment

Your comment will be revised by the site if needed.