Here I will be covering few most commonly used WLST topics as below:
- Creating application roles
- Deleting application roles
- Assigning users to the application roles
- Revoking users from the application roles
- Listing out users in an application role(s)
- Creating users
In all these scripts I will be using a csv file as an input source (as it is more convenient). You may also prefer to user various input sources for the WLST scripts which are indeed python scripts, but you would need to have those relevant modules installed as well. csv module comes with the OS package.
WLST scripts are python scripts (.py).
All the scripts are preferred to be kept under the below location:
<middleware_home>/oracle_common/common/bin
1. Creating application roles:
Here the source is Resp.csv file; this file contains the list of Application roles to be created as below.
Note: The source file in this scenario is placed at the same location of the file.
We use the createAppRole function in this script.
import sys
import os
WLS_HOST = '<hostname>'
WLS_PORT = '<admin server port number-non SSL port>'
WLS_USER = '<weblogic user>'
WLS_PW = '<weblogic password>'
connect (WLS_USER,WLS_PW,WLS_HOST+ ':'+WLS_PORT)
f = open('Resp.csv')
try:
for line in f.readlines():
items = line.split(',')
for item in items:
try:
createAppRole(appStripe='obi',appRoleName=item.strip())
print 'Creating new role: "'+item.strip()+'"'
except Exception, e:
print 'Cannot create "'+item.strip()+'" role, as it already exists'
print e
continue
except Exception, e:
print e
exit ()
2. Deleting application roles:
Here the source is DelResp.csv; this file contains the list of Application roles to be deleted as shown below.
We use the deleteAppRole function in this script.
import sys
import os
WLS_HOST = '<hostname>'
WLS_PORT = '<admin server port number-non SSL port>'
WLS_USER = '<weblogic user>'
WLS_PW = '<weblogic password>'
connect (WLS_USER,WLS_PW,WLS_HOST+ ':'+WLS_PORT)
f = open('DelResp.csv')
try:
for line in f.readlines():
items = line.split(',')
for item in items:
try:
deleteAppRole(appStripe='obi',appRoleName=item.strip())
print 'Deleting role: "'+item.strip()+'"'
except Exception, e:
print 'Cannot delete "'+item.strip()+'" role, as it does not exist'
print e
continue
except Exception, e:
print e
exit ()
3. Assigning users to the application roles:
Here the source is RespUsers.csv; this file contains the list of Application roles and users to be assigned to it as shown below.
import sys
import os
WLS_HOST = '<hostname>'
WLS_PORT = '<admin server port number-non SSL port>'
WLS_USER = '<weblogic user>'
WLS_PW = '<weblogic password>'
connect (WLS_USER,WLS_PW,WLS_HOST+ ':'+WLS_PORT)
f = open('RespUsers.csv')
v=0
try:
for line in f.readlines():
items = line.split(',')
try:
grantAppRole('obi',items[0].strip(),'weblogic.security.principal.WLSUserImpl',items[1].strip())
print 'User name "'+items[1].strip()+'" is successfully assigned to "'+items[0].strip()+'" Role'
except Exception, e:
print 'Cannot assign "' +items[1].strip()+ '" to "' +items[0].strip()+ '" Role, as the user is already assigned to it'
continue
except Exception, e:
print e
exit ()
4. Revoking users from the application roles:
Here the source is DelUsers.csv; this file contains the list of Application roles and users to be revoked from it as shown below.
import sys
import os
WLS_HOST = '<hostname>'
WLS_PORT = '<admin server port number-non SSL port>'
WLS_USER = '<weblogic user>'
WLS_PW = '<weblogic password>'
connect (WLS_USER,WLS_PW,WLS_HOST+ ':'+WLS_PORT)
f = open('DelUsers.csv')
v=0
try:
for line in f.readlines():
items = line.split(',')
try:
revokeAppRole('obi',items[0].strip(),'weblogic.security.principal.WLSUserImpl',items[1].strip())
print 'User name "'+items[1].strip()+'" is successfully revoked from "'+items[0].strip()+'" Role'
except Exception, e:
print 'Cannot revoke "' +items[1].strip()+ '" from "' +items[0].strip()+ '" Role, as the user is not assigned to this Role'
continue
except Exception, e:
print e
exit ()
5. Listing out users in an application role:
This displays the list of users present/assigned to BIAuthor role.
In case you need this list in a csv file, you can use the shell script to spool the output.
import sys
import os
WLS_HOST = '<hostname>'
WLS_PORT = '<admin server port number-non SSL port>'
WLS_USER = '<weblogic user>'
WLS_PW = '<weblogic password>'
connect (WLS_USER,WLS_PW,WLS_HOST+ ':'+WLS_PORT)
atnr=cmo.getSecurityConfiguration().getDefaultRealm().lookupAuthenticationProvider('DefaultAuthenticator')
listAppRoleMembers('obi','BIAuthor')
exit ()
6. Creating Users:
Here the source is Users.csv; this file contains the list of Users to be created.
We use the atnr.createUser function in this script.
import sys
import os
WLS_HOST = '<hostname>'
WLS_PORT = '<admin server port number-non SSL port>'
WLS_USER = '<weblogic user>'
WLS_PW = '<weblogic password>'
connect (WLS_USER,WLS_PW,WLS_HOST+ ':'+WLS_PORT)
f = open('Users.csv')
try:
for line in f.readlines():
items = line.split(',')
for item in items:
try:
print 'Creating User',item.strip()
atnr=cmo.getSecurityConfiguration().getDefaultRealm().lookupAuthenticationProvider("DefaultAuthenticator")
atnr.createUser(item.strip(),'oracle123','This is a loadrunner user')
print 'User',item.strip(),'Created'
except Exception, e:
continue
except Exception, e:
print e
exit ()
For further more WLST functions follow the below URL:
In case of further doubt please write a comment.
Hi,
ReplyDeleteWhen we are trying to create App role we are getting errors