Tuesday 10 May 2011

Change order of JAAS login modules

I have recently been installing some 3rd party software into a WPS environment. Part of the install instructions was to add some new JAAS login modules but also to change the order of these and the existing modules.

Unfortunately, the order of these, even though you can change this via the amdin console, is not an attribute and is determined by the order they appear in the security.xml file.

With a bit of googling it is easy enough to find out how to create the new modules, so the only way to script the whole process that I could see was to delete the existing modules and then add them again but in the correct order:

import java.lang.System as sys
sec = AdminConfig.list("Security")
slc = AdminConfig.showAttribute(sec, "systemLoginConfig")
entries = AdminConfig.showAttribute(slc, "entries")
entries = entries.replace('[','')
entries = entries.replace(']','')
entries = entries.split(' ')
for entry in entries:
print entry
alias = AdminConfig.showAttribute(entry, "alias")
if ( alias != "WEB_INBOUND" ) :
print "Not the module we are interested in"
else:
print "Changing mods"
loginMods = AdminConfig.showAttribute(entry, "loginModules")
loginMods = loginMods.replace('[','')
loginMods = loginMods.replace(']','')
loginMods = loginMods.split(' ')
for loginMod in loginMods:
print "Deleting login module " + loginMod
AdminConfig.remove(loginMod)

nmid="com.myco.1st.jaas.class.name"
newModuleId = AdminConfig.create("JAASLoginModule", entry, [["moduleClassName", nmid ]])
AdminConfig.modify( newModuleId , [["authenticationStrategy", "REQUIRED" ]] )
#


nmid="com.myco.2nd.jaas.class.name"
newModuleId = AdminConfig.create("JAASLoginModule", entry, [["moduleClassName", nmid ]])
AdminConfig.modify( newModuleId , [["authenticationStrategy", "REQUIRED" ]] )
#


AdminConfig.save()