Filtering list in NetScaler and converting to XML using Policy Extension

UPDATE: Updated the Lua-code, which seems to work better and is about 21 times faster. Big thanks to Ulrik! You can find the updated code here.

There was a question on the Citrix discussion forums where an administrator wants to output the groups a user is member of in a SAML Assertion, but with the caveat that only groups with a specific names should be included.

In this case the following expression is usually used: HTTP.REQ.USER.GROUPS_AS_XML(“xmltag”)

I was thinking about it and couldn’t figure out what the easiest way to do this was, so I started thinking about Lua since it’s extremely flexible and it’s fun learning new stuff. (the NetScaler developers helped me last time with a bug in the JSON-parser – see this post.)

Since I’m no developer myself, and my Lua-knowledge is very limited (at best), I started trying it out using an example Citrix have written.

The result:

When you’ve imported the policy extension, you can use it like this:

In my case, the output for HTTP.REQ.USER.GROUPS would be:
group2,group1,grupp2,grupp1

When using the policy extension, the output is:
<xmltag>group2</xmltag><xmltag>group1</xmltag>

Now remember, the above is an example and should as always be tested (and most likely optimized) before being used in production. But it works (at least in my lab) and it’s really cool to be able to modify the functionality of NetScaler this easliy.

Do you know of any way to do this without policy extensions? Or do you know Lua and there’s a better way of doing the policy extension?

Disclaimer: All information on this blog is offered "as is" with no warranty. It is strongly recommended that you verify all information and validate all scripts in isolated test environments before using them in production environments.