Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, 21 February 2018

Using GuardedString in OIM custom code.

It was observed in several cases, where we write custom java code for OIM (adapter, event handler, UI code , scheduler etc) with password as string value. If we store the password as java.lang.String, it is kept in memory as a clear text password and stays in memory at least until it is garbage collected. Code reviewer will always reject such code, if they find any password as java.lang.String.

GuardedString class can eliminate this problem by storing the password as characters in memory in an encrypted form. The encryption key will be a randomly-generated key. In their serialized form, Guarded String will be encrypted using a known default key. This is to provide a minimum level of protection regardless of the transport. For communications with the Remote Connector Framework it is recommended that deployments enable SSL for true encryption.

In this example I will explained how you can use Guarded String in OIM custom code.

GuardedString class is the member of  org.identityconnectors.common.security package and can be found in framework-1.3.2.jar. That means in your java project you have to import framework-1.3.2.jar as a library.


Below code sample describe how to store string password in GuardedString.

import org.identityconnectors.common.security.GuardedString;

String password = "abcd1234";
char[] passwordToChar = password.toCharArray();
GuardedString guardedPassword = new GuardedString(passwordToChar);


Below code sample describe how to get the password from GuardedString

          guardedPassword.access(new GuardedString.Accessor() {
                        @Override
                        public void access(char[] clearChars) {
                            System.out.println( "String password::::::::::::::::::::::::"+new String(clearChars)); //print the password.
                            
                        }
                    });
            retrivePassword.dispose(); // dispose the GuardedString after use.


it is always better to dispose the GuardedString after authentication operation is done using dispose() method.



Monday, 5 June 2017

Retrive Weblogic CSF credentials using Java and Jps API

WebLogic CSF (Credential Store Framework) holds security data of WebLogic domain like credential. A credential can hold user name and password combinations, tickets, or public key certificates.

WebLogic CSF has 2 different types
          1. Password - A password credential encapsulates a user name and a password
          2. Generic - A generic credential encapsulates any customized data or arbitrary token.

This CSF can also be used for storing credential of other applications like OIM, SOA etc. and can be used for authentication from customized applications. For example if some one wants to create an Web Service to call OIM or SOA APIs, person need to authenticate in OIM or SOA. In that scenario we can use WebLogic CSF to store and use the credential of OIM or SOA.

The below code snippet will describe how to get the credential from WebLogic CSF using java. This code can be used only from the WebLogic domain application.



private void getCredential() throws Exception{
/*
* Initializing JpsContext with current factory context.
* oracle.security.jps.JpsContext can be found under jps-api.jar
*/
oracle.security.jps.JpsContext ctx = oracle.security.jps.JpsContextFactory.getContextFactory().getContext();

final oracle.security.jps.service.credstore.CredentialStore cs = (oracle.security.jps.service.credstore.CredentialStore) ctx
.getServiceInstance(oracle.security.jps.service.credstore.CredentialStore.class);

/*
* Below section is for getting data from CSF of type Passsowrd
* "oracle.wsm.security" is Credential Map name
* "SOAAdmin" is the Password CSF key name
*/
oracle.security.jps.service.credstore.CredentialMap cmap = cs.getCredentialMap("oracle.wsm.security");
oracle.security.jps.service.credstore.Credential cred = cmap.getCredential("SOAAdmin");

if (cred instanceof oracle.security.jps.service.credstore.PasswordCredential) {
oracle.security.jps.service.credstore.PasswordCredential pcred =
(oracle.security.jps.service.credstore.PasswordCredential)cred;

char[] p = pcred.getPassword();
String UserName = pcred.getName();
String Password = new String(p);

System.out.println("UserName : " + UserName);
System.out.println("Password : " + Password);

}

/*
* Below section is for getting data from CSF of type Generic.
* "oracle.wsm.security" is Credential Map name
* "oimurl" is the custom Generic CSF key name
*/


oracle.security.jps.service.credstore.CredentialMap cmapURL = cs.getCredentialMap("oracle.wsm.security");
oracle.security.jps.service.credstore.Credential credURL = cmapURL.getCredential("oimurl");

if (credURL instanceof oracle.security.jps.service.credstore.GenericCredential){
oracle.security.jps.service.credstore.GenericCredential gcred = (oracle.security.jps.service.credstore.GenericCredential) credURL;

if (gcred.getCredential() instanceof Hashtable){

/*
*This hash table contains all the key value pair of generic CSF
*/
Hashtable result = (Hashtable) gcred.getCredential();

String URL = new String((char[]) result.get("oimurl"));
String confPath = new String((char[]) result.get("confPath"));

System.out.println("URL : "+URL);
System.out.println("confPath : "+confPath);

}
}
}

Wednesday, 24 May 2017

Weblogic bsu - java.lang.OutOfMemoryError

sometime you can find the java heap space issue while applying weblogic patches using bsu. The error looks like below


Exception in thread "main" Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
        at java.util.HashMap.createEntry(HashMap.java:897)
        at java.util.HashMap.addEntry(HashMap.java:884)
        at java.util.HashMap.put(HashMap.java:505)
        at com.bea.cie.common.dao.xbean.XBeanDataHandler.loadPropertyMap(XBeanDataHandler.java:778)
        at com.bea.cie.common.dao.xbean.XBeanDataHandler.<init>(XBeanDataHandler.java:99)
        at com.bea.cie.common.dao.xbean.XBeanDataHandler.createDataHandler(XBeanDataHandler.java:559)
        at com.bea.cie.common.dao.xbean.XBeanDataHandler.getComplexValue(XBeanDataHandler.java:455)
        at com.bea.plateng.patch.dao.cat.PatchCatalogHelper.getPatchDependencies(PatchCatalogHelper.java:442)
        at com.bea.plateng.patch.dao.cat.PatchCatalogHelper.getPatchDependencies(PatchCatalogHelper.java:464)
        at com.bea.plateng.patch.dao.cat.PatchCatalog.getPatchDependencies(PatchCatalog.java:56)
        at com.bea.plateng.patch.dao.cat.PatchCatalogHelper.getInvalidatedPatchMap(PatchCatalogHelper.java:1621)
        at com.bea.plateng.patch.PatchSystem.updatePatchCatalog(PatchSystem.java:436)
        at com.bea.plateng.patch.PatchSystem.refresh(PatchSystem.java:130)
        at com.bea.plateng.patch.PatchSystem.<init>(PatchSystem.java:114)
        at com.bea.plateng.patch.PatchSystem.<clinit>(PatchSystem.java:41)
        at com.bea.plateng.patch.Patch.main(Patch.java:279)
java.lang.NoClassDefFoundError: Could not initialize class com.bea.plateng.patch.PatchSystem
        at com.bea.plateng.patch.PatchClientHelper.getAllPatchDetails(PatchClientHelper.java:74)
        at com.bea.plateng.patch.PatchInstallationHelper.cleanupPatchSets(PatchInstallationHelper.java:130)
        at com.bea.plateng.patch.PatchTarget.<init>(PatchTarget.java:272)
        at com.bea.plateng.patch.PatchTargetFactory.create(PatchTargetFactory.java:30)
        at com.bea.plateng.patch.ProductAliasTarget.constructPatchTargetList(ProductAliasTarget.java:88)
        at com.bea.plateng.patch.ProductAliasTarget.<init>(ProductAliasTarget.java:46)
        at com.bea.plateng.patch.ProductAliasTargetHelper.getProdAliasTargetList(ProductAliasTargetHelper.java:55)
        at com.bea.plateng.patch.ProductAliasTargetHelper.getAllHomeToProdAliasesTargetMap(ProductAliasTargetHelper.java:32)
        at com.bea.plateng.patch.ProductAliasTargetHelper.checkProfilesInProductAliases(ProductAliasTargetHelper.java:133)
        at com.bea.plateng.patch.Patch$1.run(Patch.java:376)
        at java.lang.Thread.run(Thread.java:745)





Solution


1. open the bsu.sh (MIDDLEWARE_HOME/utils/bsu/bsu.sh) file edit mode
2. change the memory args value as below


From


MEM_ARGS="-Xms256m -Xmx512m"


To


MEM_ARGS="-Xms512m -Xmx1024m"


3. Save the file and run bsu again.

Tuesday, 23 May 2017

Calling Unix command from Java

Here is the code sample for calling Unix command from java. The below sample can only run the Unix command in the same system.


public class LinuxInJava {
    public static void main(String args[]) {
        String s;
        Process p;
        try {
            p = Runtime.getRuntime().exec("ls -aF");
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((s = br.readLine()) != null){
                System.out.println("line: " + s);
   
    }
            p.waitFor();
            System.out.println ("exit: " + p.exitValue());
            p.destroy();
        } catch (Exception e) {}
    }
}




Friday, 5 May 2017

OIM 11gR2 PS3 Revoke Requset using API

Revoke Application Instance request

public String createRevokeApplicationInstanceRequest(){
String requestKey="0";
RequestBeneficiaryEntity reqBenEnt = new RequestBeneficiaryEntity();
reqBenEnt.setRequestEntityType(oracle.iam.platform.utils.vo.OIMType.ApplicationInstance);
reqBenEnt.setEntitySubType("APP_INSTANCE_NAME"); //Application Instance Name as SubType
reqBenEnt.setOperation(RequestConstants.MODEL_REVOKE_ACCOUNT_OPERATION);
reqBenEnt.setEntityKey("ACCOUNT_ID"); //for AppInst it will be OIU key, for entitlement it will be ent_assign_key
List<RequestBeneficiaryEntityAttribute> reqBenEntAttrList = new ArrayList();
List<RequestBeneficiaryEntity> reqBenEntList = new ArrayList();
reqBenEntList.add(reqBenEnt);
//Create Beneficiary for the request
Beneficiary beneficiary = new Beneficiary();
beneficiary.setBeneficiaryKey("USR_KEY");
beneficiary.setBeneficiaryType(Beneficiary.USER_BENEFICIARY);
beneficiary.setTargetEntities(reqBenEntList);
List<Beneficiary> benList = new ArrayList<Beneficiary>();
benList.add(beneficiary);
//Create Request Data
RequestData reqData = new RequestData();
reqData.setBeneficiaries(benList);
reqData.setJustification("Business Justification");
OIMClient oimClientObjReq = OIMClientFactory.getOIMClient(requester);
RequestService reqAPI = (RequestService)oimClientObjReq.getService(RequestService.class);
try {
requestKey = reqAPI.submitRequest(reqData);
System.out.println(requestKey);
} catch (Exception rse) {
// TODO: Add catch code
rse.printStackTrace();
}
return requestKey;
}


Revoke Entitlement Request

public String createRevokeEntitlementRequest(){
String requestKey="0";
RequestBeneficiaryEntity reqBenEnt = new RequestBeneficiaryEntity();
reqBenEnt.setRequestEntityType(oracle.iam.platform.utils.vo.OIMType.Entitlement);
reqBenEnt.setEntitySubType("ENT_LIST_KEY");//it will be ent_list_key
reqBenEnt.setOperation(RequestConstants.MODEL_REVOKE_ENTITLEMENT_OPERATION);
reqBenEnt.setEntityKey("ENT_ASSIGN_KEY"); //It will be ent_assign_key which is entitlement instance key
List<RequestBeneficiaryEntityAttribute> reqBenEntAttrList = new ArrayList();
List<RequestBeneficiaryEntity> reqBenEntList = new ArrayList();
reqBenEntList.add(reqBenEnt);
//Create Beneficiary for the request
Beneficiary beneficiary = new Beneficiary();
beneficiary.setBeneficiaryKey("USR_KEY");
beneficiary.setBeneficiaryType(Beneficiary.USER_BENEFICIARY);
beneficiary.setTargetEntities(reqBenEntList);
List<Beneficiary> benList = new ArrayList<Beneficiary>();
benList.add(beneficiary);
//Create Request Data
RequestData reqData = new RequestData();
reqData.setBeneficiaries(benList);
reqData.setJustification("Business Justification");
OIMClient oimClientObjReq = OIMClientFactory.getOIMClient("REQUESTER_LOGIN_ID");
RequestService reqAPI = (RequestService)oimClientObjReq.getService(RequestService.class);
try {
requestKey = reqAPI.submitRequest(reqData);
System.out.println(requestKey);
} catch (Exception rse) {
// TODO: Add catch code
rse.printStackTrace();
}
return requestKey;
}


Revoke Role Request

For role revoke, it is almost same as Entitlement revoke request. Only changes required are as follows

reqBenEnt.setRequestEntityType(oracle.iam.platform.utils.vo.OIMType.Role);
reqBenEnt.setEntitySubType("ROLE_KEY");//it will be Role_key
reqBenEnt.setEntityKey("ROLE_KEY"); //It will be role_key
reqBenEnt.setOperation(RequestConstants.MODEL_REMOVE_ROLES_OPERATION);

OIM 11gR2 PS3 Provisioning Request using API

Provisioning request in OIM 11gR2 PS3 can be made for following:

1. Application Instance
2. Entitlements
3. Roles

Below are the API usage of all the request operations. You can use this sample methods and modify accordingly for bulk operations.

Application Instance Provisioning Request


/**
* Requesting Application Instance. This method
* can be further modified to use for bulk action.
* @param args
*/
private String createApplicationRequest() throws Exception{
RequestBeneficiaryEntity requestEntity = new RequestBeneficiaryEntity();
requestEntity.setRequestEntityType(oracle.iam.platform.utils.vo.OIMType.ApplicationInstance); //Type of the Request
requestEntity.setEntitySubType("APP_INSTANCE_NAME"); //Name of the Application Instance
requestEntity.setEntityKey("APP_INSTANCE_KEY"); //Application instance key
requestEntity.setOperation(RequestConstants.MODEL_PROVISION_APPLICATION_INSTANCE_OPERATION); //Request operation type.
/**
* here in each RequestBeneficiaryEntityAttribute object we will
* be setting the request data
* FIELD_1,FIELD_2,FIELD_3 are the request data set filed label name and the "value" is the corresponding value.
*/
List<RequestBeneficiaryEntityAttribute> attrs = new ArrayList<RequestBeneficiaryEntityAttribute>();
RequestBeneficiaryEntityAttribute attr;
attr = new RequestBeneficiaryEntityAttribute("FIELD_1", "value", RequestBeneficiaryEntityAttribute.TYPE.String);
attrs.add(attr);
attr = new RequestBeneficiaryEntityAttribute("FIELD_2", "value", RequestBeneficiaryEntityAttribute.TYPE.String);
attrs.add(attr);
attr = new RequestBeneficiaryEntityAttribute("FIELD_3", "value", RequestBeneficiaryEntityAttribute.TYPE.String);
attrs.add(attr);
//Continue setting RequestBeneficiaryEntity
requestEntity.setEntityData(attrs);
//Adding RequestBeneficiaryEntity to List
List<RequestBeneficiaryEntity> entities = new ArrayList<RequestBeneficiaryEntity>();
entities.add(requestEntity);
//creating new Beneficiary
Beneficiary beneficiary = new Beneficiary();
beneficiary.setBeneficiaryKey("USR_KEY"); //set BeneficiaryKey as User key
beneficiary.setBeneficiaryType(Beneficiary.USER_BENEFICIARY); //set the type as user
beneficiary.setTargetEntities(entities); //set target entities as list of RequestBeneficiaryEntity
//Adding Beneficiary to List
List<Beneficiary> beneficiaries = new ArrayList<Beneficiary>();
beneficiaries.add(beneficiary);
//Creating new RequestData and set the Beneficiaries with List of Beneficiaries
RequestData requestData = new RequestData();
requestData.setBeneficiaries(beneficiaries);
/**
* getRequesterConnection() is a seperate method to create OIM connection.
*/
OIMClient oimClientObjReq = getRequesterConnection("REQUESTER_LOGIN_ID"); //create an OIM connection with requester's login
RequestService requestAPI = (RequestService)oimClientObjReq.getService(RequestService.class);
String requestId = requestAPI.submitRequest(requestData);
System.out.println("requestId: "+requestId);
return requestId;
}

Entitlement Provisioning Request


private String createEntitlementRequest() throws Exception{
/**
* Creating a new RequestBeneficiaryEntity object which
* will hold all the requested entitlement related data.
*/
RequestBeneficiaryEntity requestEntity = new RequestBeneficiaryEntity();
requestEntity.setRequestEntityType(oracle.iam.platform.utils.vo.OIMType.Entitlement); //Type of the Request
requestEntity.setEntitySubType("ENT_CODE"); //Name of the Entitlement
requestEntity.setEntityKey("ENT_LIST_KEY"); //Entitlement key
requestEntity.setOperation(RequestConstants.MODEL_PROVISION_ENTITLEMENT_OPERATION); //Request operation type.
//Adding RequestBeneficiaryEntity to List
List<RequestBeneficiaryEntity> entities = new ArrayList<RequestBeneficiaryEntity>();
entities.add(requestEntity);
//creating new Beneficiary
Beneficiary beneficiary = new Beneficiary();
beneficiary.setBeneficiaryKey("USR_KEY"); //set BeneficiaryKey as User key
beneficiary.setBeneficiaryType(Beneficiary.USER_BENEFICIARY); //set the type as user
beneficiary.setTargetEntities(entities); //set target entities as list of RequestBeneficiaryEntity
//Adding Beneficiary to List
List<Beneficiary> beneficiaries = new ArrayList<Beneficiary>();
beneficiaries.add(beneficiary);
//Creating new RequestData and set the Beneficiaries with List of Beneficiaries
RequestData requestData = new RequestData();
requestData.setBeneficiaries(beneficiaries);
/**
* getRequesterConnection() is a seperate method to create OIM connection.
*/
OIMClient oimClientObjReq = getRequesterConnection("REQUESTER_LOGIN_ID"); //create an OIM connection with requester's login
RequestService requestAPI = (RequestService)oimClientObjReq.getService(RequestService.class);
String requestId = requestAPI.submitRequest(requestData); //Return request ID
System.out.println("requestId: "+requestId);
return requestId;
}


Role Provisioning Request


private String createRoleRequest() throws Exception{

/**
* Creating a new RequestBeneficiaryEntity object which
* will hold all the requested Role related data.
*/

RequestBeneficiaryEntity requestEntity = new RequestBeneficiaryEntity();
requestEntity.setRequestEntityType(oracle.iam.platform.utils.vo.OIMType.Role); //Type of the Request
requestEntity.setEntitySubType("UGP_KEY"); //Name of the Role
requestEntity.setEntityKey("UGP_NAME"); //Role key
requestEntity.setOperation(RequestConstants.MODEL_ASSIGN_ROLES_OPERATION); //Request operation type.

//Adding RequestBeneficiaryEntity to List
List<RequestBeneficiaryEntity> entities = new ArrayList<RequestBeneficiaryEntity>();
entities.add(requestEntity);

//creating new Beneficiary
Beneficiary beneficiary = new Beneficiary();
beneficiary.setBeneficiaryKey("USR_KEY"); //set BeneficiaryKey as User key
beneficiary.setBeneficiaryType(Beneficiary.USER_BENEFICIARY); //set the type as user
beneficiary.setTargetEntities(entities); //set target entities as list of RequestBeneficiaryEntity

//Adding Beneficiary to List
List<Beneficiary> beneficiaries = new ArrayList<Beneficiary>();
beneficiaries.add(beneficiary);

//Creating new RequestData and set the Beneficiaries with List of Beneficiaries
RequestData requestData = new RequestData();
requestData.setBeneficiaries(beneficiaries);

/**
* getRequesterConnection() is a seperate method to create OIM connection.
*/

OIMClient oimClientObjReq = getRequesterConnection("REQUESTER_LOGIN_ID"); //create an OIM connection with requester's login
RequestService requestAPI = (RequestService)oimClientObjReq.getService(RequestService.class);
String requestId = requestAPI.submitRequest(requestData); //Return request ID

System.out.println("requestId: "+requestId);
return requestId;
}


Thursday, 4 May 2017

SOA Composite Build failed due to Java Heap Space issue

Problem Description:

Some time you may face the below issue while deploying SOA composite from JDeveloper. I faced the same issue using Jdevelper 11.1.1.6.

The issue says that the compiler couldn't create the jvm.

Issue may happen if you have an internal weblogic server integrated with your JDeveloper IDE. And you do not need weblogic to run while deploying SOA composites to SAR file.

Resolution:

open the [MIDDLEWARE_HOME]\jdeveloper\bin\ant-sca-compile.xml file. and change the jvmarg value as below.

Previous:



 After:



After changing the ant-sca-compile.xml , open the jdev.conf file located at [MIDDLEWARE_HOME]\jdeveloper\jdev\bin and change as below

comment the line "AddVMOption  -XX:MaxPermSize=256M" and add the below 3 lines

AddVMOption –Xmx1280M
AddVMOption -XX:MaxPermSize=256M
AddVMOption -XX:MaxHeapSize=512M




Save the file and redo the deployment again. Now this time it is successful.



Followers

OIM API for adding process task and retry failed task

 In this blog you can find how to add new process task and retry any failed/rejected tasks using API. Adding new process task: /************...