jython script to add Generic JVM arguments for Log4j Security Vulnerabilities fix

$ ./wsadmin.sh -lang jython -f addJVMArg_new.py server1 add "-Dlog4j2_formatMsgNoLookups=true"
WASX7209I: Connected to process "server1" on node AppNode01 using SOAP connector;  The type of process is: UnManagedProcess
WASX7303I: The following options are passed to the scripting environment and are available as arguments that are stored in the argv variable: "[server1, add, -Dlog4j2_formatMsgNoLookups=true]"
JVM Name is :  server1
Action is :  add
JVM ID is (cells/Cell01/nodes/AppNode01/servers/server1|server.xml#JavaVirtualMachine_1183122130078)
JVM Arguments are : -Xcomp -XX:-TieredCompilation
new_argument is :  -Dlog4j2_formatMsgNoLookups=true
-Xcomp -XX:-TieredCompilation
Need to add the arguments
modified arguments is as below :
-Xcomp -XX:-TieredCompilation -Dlog4j2_formatMsgNoLookups=true
### addJVMArg_new.py ###
import os
import sys
import java
import java.util as util
import java.io as javaio

def usage():
 print "./wsadmin.sh -lang jython -f addJVMArg_new.py <JVM_name> add <New_argument>"
 print "./wsadmin.sh -lang jython -f addJVMArg_new.py <JVM_name> update"
 
 
def getJVMConfigID(jvm_name):
 server_list=AdminConfig.list('Server').splitlines()
 for server in server_list:
  server_name=AdminConfig.showAttribute(server,'name')
  if (server_name==jvm_name):
   jvm_id=AdminConfig.list('JavaVirtualMachine',server)
   return jvm_id
   
def currentJvmArguments(jvm_id):
 #print "Current JVM arguments"
 current_arguments=AdminConfig.showAttribute(jvm_id,"genericJvmArguments")
 #print str(current_arguments)
 return str(current_arguments)
   
def updateJvmArguments(jvm_id):
 current_arguments=currentJvmArguments(jvm_id)
 print str(current_arguments)
 print "Input new arguments"
 new_arguments=raw_input("Provide the new arguments :")
 print new_arguments
 print AdminConfig.modify(jvm_id,[['genericJvmArguments',new_arguments]])
 print AdminConfig.save()
 

def addJvmArguments(jvm_id,action,new_argument):
 current_arguments=currentJvmArguments(jvm_id)
 print str(current_arguments)
 print "Need to add the arguments"
 current_arguments=str(current_arguments) + ' ' + new_argument
 print "modified arguments is as below :"
 print current_arguments
 print AdminConfig.modify(jvm_id,[['genericJvmArguments',current_arguments]])
 print AdminConfig.save()
 
   
if not (len(sys.argv) >= 2):
 print "Usage : ",
 usage()
 sys.exit(1)
 
 
 
#######################################################################
######################## Main script ##################################
#######################################################################

jvm_name=sys.argv[0]
action=sys.argv[1]

print "JVM Name is : ", jvm_name
print "Action is : ", action
#print "new_argument is : ", new_argument

# Retrieve the JVM Config id

jvm_id=getJVMConfigID(jvm_name)
jvm_arguments=currentJvmArguments(jvm_id)

print "JVM ID is", jvm_id
print "JVM Arguments are :", jvm_arguments

if (action == "add"):
 new_argument=sys.argv[2]
 print "new_argument is : ", new_argument
 addJvmArguments(jvm_id,action,new_argument)
else:
 updateJvmArguments(jvm_id)

### END of Main Script

Leave a Reply

Your email address will not be published. Required fields are marked *