Weblogic User Creation Script WLST

The Objective

The purpose of this post to share the WLST script which helps in creating a new user in weblogic 11g or 12c and map them to righteous Group for privileges. Like Administrators, Deployers, Monitors etc.

 

Version Compatibility

Weblogic 12c (or) 11g

 

The Properties file

This is the Properties file, Heart of the Python script which performs the primary objective which is creating a username and mapping them to the correct group.

  • Domain Name
  • Host Name where the AdminServer is Running
  • Listen Port of AdminServer
  • The realm in weblogic ( By Default it is myrealm )
  • Weblogic User Key file
  • Weblogic User Config file
  • UserName, Password and the Authorization Group

#WLSusers.properties

ckey=/Users/aksarav/aksarav-WebLogicKey.properties
conf=/Users/aksarav/aksarav-WebLogicConfig.properties
hostname=localhost
port=17001
dname=mwidomain
rname=myrealm

wlusers=user1,user2,user3

user1.Name=sarav
user1.passwd=sarav@43
user1.desc=admin
user1.group=Administrators

user2.Name=gopi
user2.passwd=gopi@432
user2.desc=monitoring team
user2.group=Monitors

user3.Name=karthik
user3.passwd=karthik@1
user3.desc=developer
user3.group=Deployers

The Script file

This is the Python script which should be invoked with Weblogic WLST.

#WLS_UserCreation.py

def file_parse():
    global _dict
    _dict={}
    usrprop = "WLSusers.properties"
    if os.path.exists(usrprop):
        fo = open(usrprop,'r+')
        lines = fo.readlines()
        for usr in lines:
            if "=" in usr:
                usr = usr.rstrip()
                key = usr.split('=')[0]
                value = usr.split('=')[1]
                _dict[key]=value
    else:
        print(usrprop+"property file is missing!")


def connect_domain():
    try:
        AdmSvr = _dict.get('hostname')
        AdmPort = _dict.get('port')
        AdmUrl = "t3://"+AdmSvr+":"+AdmPort
        AdmCfg = _dict.get('conf')
        AdmKey = _dict.get('ckey')
        print("connecting to Admin Server")
        if os.path.exists(AdmCfg) and os.path.exists(AdmKey):
            connect(userConfigFile=AdmCfg, userKeyFile=AdmKey, url = AdmUrl)
            print("connected to Admin Server")
            print"------------------------------------------------------------"
    except Exception, error:
        print("\n Unable to connect to admin server \n")
        print("\n please verify the url or make sure the AdminServer is up and Running:\n")
        print("Error description as follows:\n")
        print(error)
        print dumpStack()
        exit()

def creating_user():
    try:
        domainName = _dict.get('dname')
        realmName = _dict.get('rname')
        print("")
        authpath= '/SecurityConfiguration/' + domainName + '/Realms/' + realmName + '/AuthenticationProviders/DefaultAuthenticator'
        cd(authpath)
        cmo.createUser(uname,upwd,udesc)
        print(uname + " user created")
    except Exception, error:
        print("Failed to create User, Make sure that the User name is not already present")
        print("Make sure the the Password is atleast of 8 characters")
def adding_to_group():
    try:
        domainName = _dict.get('dname')
        realmName = _dict.get('rname')
        authpath= '/SecurityConfiguration/' + domainName + '/Realms/' + realmName + '/AuthenticationProviders/DefaultAuthenticator'
        cd(authpath)
        cmo.addMemberToGroup(grpname, uname)
        print("Added to group" "  -  " + grpname)
    except Exception, error:
        print("Failed to Add in group")


if __name__ != "__main__":
    import os
    import sys
    redirect("/dev/null",'false')
    file_parse()
    connect_domain()
    users = _dict.get('wlusers').split(',')
    for each_user in users:
        uname = _dict.get(each_user+'.Name')
        upwd = _dict.get(each_user+'.passwd')
        udesc = _dict.get(each_user+'.desc')
        grpname = _dict.get(each_user+'.group')
        creating_user()
        adding_to_group()
        print"------------------------------------------------------------"


if __name__ == "__main__":
    print('Please execute the script via WLST')

 

The Trail Run

Here is the Trail Run Output of the script.

aksarav@middlewareinventory:/opt/python_script$ java weblogic.WLST WLS_UserCreation.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

connecting to Admin Server

connected to Admin Server
------------------------------------------------------------

sarav user created
Added to group  -  Administrators
------------------------------------------------------------

gopi user created
Added to group  -  Monitors
------------------------------------------------------------

karthik user created
Added to group - Deployers
------------------------------------------------------------

 

How to use it

Step1:  Copy the Properties and Script file given above and save it as WLS_UserCreation.py and WLSusers.properties respectively

Step2:   Set your environment for running the WLST script by either executing SetWLSEnv.sh (or) SetDomainEnv.sh script

Step3:  Update the properties file with a number of users and each of their username and password and the Group they belong. ( To Know what groups are available in weblogic, refer this article )

Step4:  Launch the WLST Python script WLS_UserCreation.py with Java.Weblogic.WLST as follows

java weblogic.WLST WLS_UserCreation.py

 

Post Validation

If you would like to validate the users are created Successfully. Go Straight to console as  admin user and go to user and groups section

Home -> Security Realms -> myrealm -> Users and Groups 

 

The Trail Run ( Video )

 

Credits to my friend Karthik Raj for crafting this script and sharing it with us

Thanks,
SaravAK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading