Tuesday, 30 August 2011

Code to create Accesspolicy through code OIM API


public class AccessPolicy {

static ReadProperty readProperty = new ReadProperty();
public long returnVal = -1;
public static String homeDir = readProperty.getProp("homeDir");
public static String policyDir = readProperty.getProp("policyDir");
public static String confDir = readProperty.getProp("confDir");
public static String username = readProperty.getProp("username");
public static String password = readProperty.getProp("password");
public static String providerUrl = readProperty.getProp("providerUrl");

public tcUtilityFactory ioUtilityFactory;
public tcUserOperationsIntf moUserUtility;
public tcGroupOperationsIntf moGroupUtility;
public tcAccessPolicyOperationsIntf moAccesspolicyutility;
public tcObjectOperationsIntf moObjectUtility;

private HashMap policy;
private long[] provObjKeys;;
private boolean[] revokeObjectIfNotApply;;
private long[] denyObjKeys={};
private long[] groupKeys = new long[1];

public String roleAuthenticate(String usr, String pass){
try {
System.setProperty("XL.HomeDir", homeDir);
System.setProperty("java.security.policy", policyDir);//server or client
System.setProperty("java.security.auth.login.config", confDir);//server or client
System.setProperty("java.naming.provider.url", providerUrl);
usr = username;
pass = password;
ConfigurationClient.ComplexSetting config =
ConfigurationClient.getComplexSettingByPath("Discovery.CoreServer");
Hashtable env = config.getAllSettings();
System.out.println("test1");
tcUtilityFactory ioUtilityFactory = new tcUtilityFactory(env,usr,pass);
System.out.println("Getting utility interfaces...");
System.out.println("Connection Established");
moUserUtility = (tcUserOperationsIntf)ioUtilityFactory.getUtility("Thor.API.Operations.tcUserOperationsIntf");
moGroupUtility = (tcGroupOperationsIntf)ioUtilityFactory.getUtility("Thor.API.Operations.tcGroupOperationsIntf");
moAccesspolicyutility=(tcAccessPolicyOperationsIntf)ioUtilityFactory.getUtility("Thor.API.Operations.tcAccessPolicyOperationsIntf");
moObjectUtility=(tcObjectOperationsIntf)ioUtilityFactory.getUtility("Thor.API.Operations.tcObjectOperationsIntf");
Logger logger = oracle.j2ee.rmi.RMIMessages.getLogger();
logger.setLevel(Level.OFF);

return "Valid";
} catch(Exception e){
System.out.println("Exception ");
e.printStackTrace();
return "Invalid";
}
}

public AccessPolicy()
{
policy = new HashMap();

}

public long getGroupKey(String obj){
long key = 0;
HashMap userMap = new HashMap();

try {
userMap.put("Groups.Group Name", obj);
tcResultSet moResultSet = moGroupUtility.findGroups(userMap);

for (int i=0; i < moResultSet.getRowCount(); i++){ moResultSet.goToRow(i); key = moResultSet.getLongValue("Groups.Key"); } }catch(Exception e){ e.printStackTrace(); } return key; } public long getObjectKey(String object) { HashMap objectMap = new HashMap();
long key = 0;

try {
objectMap.put("Objects.Name", object);
tcResultSet moResultSet = moObjectUtility.findObjects(objectMap);
if (moResultSet.getRowCount()==0)
{
System.out.println("Cannot get key value");
}
else
{
moResultSet.goToRow(0);
key = moResultSet.getLongValue("Objects.Key");
System.out.println("Key value " +key);
}
}catch(Exception e){
e.printStackTrace();
}
return key;
}

public void setPolicy(String policyName, String policyDescription) {

policy.put("Access Policies.Name", policyName);
policy.put("Access Policies.Description", policyDescription);
policy.put("Access Policies.Note", policyName);
policy.put("Access Policies.Retrofit Flag", "1");
policy.put("Access Policies.By Request", "0");

}

public HashMap getPolicy() {
return policy;
}

public void setProvObjKeys(String objects) {

Scanner sc = new Scanner(objects).useDelimiter(";");

int count =0;
while(sc.hasNext())
{
count++;
sc.next();
}

System.out.println("Object count :" + count);
provObjKeys= new long[count];
revokeObjectIfNotApply= new boolean[count];

sc = new Scanner(objects).useDelimiter(";");
int i=0;
while(sc.hasNext())
{
String obj =sc.next();
System.out.println("Adding object" + obj);
provObjKeys[i]=getObjectKey(obj);
revokeObjectIfNotApply[i]=true;
i++;
}

}

public long[] getProvObjKeys() {
return provObjKeys;
}

public void setGroupKeys(String groups) {

Scanner sc = new Scanner(groups).useDelimiter(";");
int i=0;
while(sc.hasNext())
{
groupKeys[i]=getGroupKey(sc.next());
}
}

public long[] getGroupKeys() {
return groupKeys;
}

public boolean[] getRevokeObjectIfNotApply() {
return revokeObjectIfNotApply;
}

public long[] getDenyObjKeys() {
return denyObjKeys;
}

public long createAccessPolicy()
{
long policyKey = 0;

try {

policyKey= moAccesspolicyutility.createAccessPolicy(getPolicy(), getProvObjKeys(),getRevokeObjectIfNotApply(),getDenyObjKeys(),getGroupKeys());

} catch (tcAPIException e) {
e.printStackTrace();
} catch (tcObjectNotFoundException e) {
e.printStackTrace();
} catch (tcGroupNotFoundException e) {
e.printStackTrace();
} catch (tcInvalidAttributeException e) {
e.printStackTrace();
}

return policyKey;

}
public static void main (String[] args)
{
AccessPolicy temp = new AccessPolicy();
temp.roleAuthenticate("adminuser","test123");//MAkes connectio with OIM
temp.setPolicy("tempcode2","tempcode");//Passing accesspolicy name and description
temp.setProvObjKeys("OID;"resource"); //Policy name
temp.setGroupKeys("testgroup");//Group name
temp.createAccessPolicy();



}

}

1 comment:

  1. thanks buddy. appreciate. so, where do we stand??

    ReplyDelete