package connector; import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.AuthenticationException; import javax.naming.AuthenticationNotSupportedException; import javax.naming.NamingException; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.ModificationItem; import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; import javax.naming.ldap.Control; import openconnector.AbstractConnector; import openconnector.Connector; import openconnector.ConnectorException; import openconnector.Filter; import openconnector.Item; import openconnector.ObjectAlreadyExistsException; import openconnector.ObjectNotFoundException; import openconnector.Plan; import openconnector.Request; import openconnector.Result; import openconnector.Schema; /*** * * @author abhinov.dhonthula * * Custom connector class is used to connect IDN with OUD. * */ public class CustomConnector extends AbstractConnector implements Connector { final String firstName = "givenName"; final String lastName = "sn"; final String employeeNumber = "Employee Number"; final String department = "Department"; final String designation = "Designation"; final String memberOf = "memberOf"; final String distinguishName = "DN"; final String activeStatus = "title"; String sourceURL = ""; String authentication = ""; String userName = ""; String userPassword = ""; Hashtable lDapCredentials = new Hashtable(); DirContext context; String searchString = ""; /** * This is the default constructor of class. * For every aggregation method, provision method or testconnection method constructor will be called first * @param args none * */ public CustomConnector(){ System.out.println("------------------------I AM CALLING MY CONSTRUCTOR10------------------------80" ); } public DirContext getConnection() { if(this.config != null) { sourceURL = this.config.getConfig().get("url").toString()+":"+this.config.getConfig().get("port").toString(); authentication = "simple"; userName = this.config.getConfig().get("user").toString(); userPassword = this.config.getConfig().get("password").toString(); searchString = "ldap://"+sourceURL; if(sourceURL != "" && authentication != "" && userName != "" && userPassword != "") { lDapCredentials.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); lDapCredentials.put(Context.PROVIDER_URL, "ldap://"+sourceURL); lDapCredentials.put(Context.SECURITY_AUTHENTICATION, "simple"); lDapCredentials.put(Context.SECURITY_PRINCIPAL, userName); lDapCredentials.put(Context.SECURITY_CREDENTIALS, userPassword); try{ context = new InitialDirContext(lDapCredentials); } catch(AuthenticationNotSupportedException e){ System.out.println("AuthenticationNotSupportedException exception in LDAP "+e); throw new ConnectorException("AuthenticationNotSupportedException exception in LDAP "+e); } catch(AuthenticationException e){ System.out.println("AuthenticationException exception in LDAP "+e); throw new ConnectorException("AuthenticationException exception in LDAP "+e); } catch(NamingException e){ System.out.println("Naming exception in LDAP "+e); throw new ConnectorException("Naming exception in LDAP "+e); } } else { System.out.println("Required credentials is missing"); throw new ConnectorException("Required credentials is missing"); } } else { System.out.println("Config is null"); throw new ConnectorException("Config Data is null"); } return context; } /** * This is the main method of the program. As this is the custom connector execution main method will not be used at all. * @param args default arguments * @return type void */ public static void main(String[] args){ System.out.println("----------------------------------INSIDE MAIN----------------------------"); } /** * This is provision method of Openconnector.connector class. This method is used to provision the users into OUD and set the identity in IDN. * @param plan which contains the complete provision plan. * @return void this will not expect any return type. */ @Override public void provision(Plan plan) throws ConnectorException, ObjectAlreadyExistsException, ObjectNotFoundException, UnsupportedOperationException { System.out.println("------------------------I AM CALLING MY PROVISION METHOD11------------------------" ); System.out.println("------------------------I AM CALLING MY PROVISION METHOD------------------------" ); Boolean createAccountFlag; Boolean updateAccountFlag; if(plan != null) { if(plan.getRequests().get(0).getOperation().toString().equals("Create")) { createAccountFlag = createAccount(plan); if(createAccountFlag) { System.out.println("ACCOUNT CREATED"); } else { System.out.println("ERROR IN CREATING ACCOUNT"); } } else if(plan.getRequests().get(0).getOperation().toString().equals("Update")) { updateAccountFlag = updateAccount(plan); if(updateAccountFlag) { System.out.println("ACCOUNT UPDATED"); } else { System.out.println("ERROR IN UPDATING ACCOUNT"); } } } } /** * This method "iterate" is used for aggregation. In this method we connect to our OUD and get the accounts. * We loop through all the accounts and create a map with it. * * @param filter If we have any specific condition while looping through accounts then we can pass them in filter. * @return Iterator> This is the iterator which contains all accounts. */ @Override public Iterator> iterate(Filter filter){ ArrayList> accountsData = new ArrayList>(); Map oneAccount = new HashMap (); String searchDN = ""; SearchControls searchControls = new SearchControls(); NamingEnumeration namingEnumeration = null; SearchResult sr = null; if(this.config != null) { System.out.println(" ------------------INSIDE THE aggregation method... 9----------------"); sourceURL = this.config.getConfig().get("url").toString()+":"+this.config.getConfig().get("port").toString(); authentication = "simple"; userName = this.config.getConfig().get("user").toString(); userPassword = this.config.getConfig().get("password").toString(); searchString = "ldap://"+sourceURL; searchDN = this.config.getConfig().get("searchDN").toString(); if(sourceURL != "" && authentication != "" && userName != "" && userPassword != "") { lDapCredentials.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); lDapCredentials.put(Context.PROVIDER_URL, searchString); lDapCredentials.put(Context.SECURITY_AUTHENTICATION, "simple"); lDapCredentials.put(Context.SECURITY_PRINCIPAL, this.config.getConfig().get("user").toString()); lDapCredentials.put(Context.SECURITY_CREDENTIALS, this.config.getConfig().get("password").toString()); try{ System.out.println(" ------------------INSIDE THE aggregation method... vfg----------------"); context = new InitialDirContext(lDapCredentials); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); namingEnumeration = context.search(searchDN, "(cn=*)", new Object[]{}, searchControls); while (namingEnumeration.hasMore()) { sr = namingEnumeration.next(); System.out.println(sr); System.out.println(sr.getAttributes().get("member")); if(sr.getAttributes().get("member") == null) { oneAccount.put(firstName, sr.getAttributes().get("givenName").get()); oneAccount.put(lastName, sr.getAttributes().get("sn").get()); oneAccount.put(employeeNumber, sr.getAttributes().get("employeeNumber").get()); oneAccount.put(distinguishName, sr.getAttributes().get("uid").get()); oneAccount.put(activeStatus, sr.getAttributes().get("title").get()); oneAccount.put(department, "Sales"); oneAccount.put(designation, "Manager"); oneAccount.put(memberOf, "Default"); } //System.out.println(sr.getAttributes().g); accountsData.add(oneAccount); oneAccount = new HashMap (); } context.close(); System.out.println("Connected to LDAP"); } catch(AuthenticationNotSupportedException e){ System.out.println("AuthenticationNotSupportedException exception in LDAP "+e); throw new ConnectorException("AuthenticationNotSupportedException exception in LDAP "+e); } catch(AuthenticationException e){ System.out.println("AuthenticationException exception in LDAP "+e); throw new ConnectorException("AuthenticationException exception in LDAP "+e); } catch(NamingException e){ System.out.println("Naming exception in LDAP "+e); throw new ConnectorException("Naming exception in LDAP "+e); } } else { System.out.println("Required credentials is missing"); throw new ConnectorException("Required credentials is missing"); } } else { System.out.println("Config is null"); } System.out.println(" ------------------END OF PROVISION METHOD----------------"); return accountsData.iterator(); } /** * This method "testConnection" is used to check the connection of source. It check whether we can communicate with our target application or not. * * @param none * @return void */ @Override public void testConnection(){ System.out.println(" ------------------INSIDE THE TEST CONNETION1 ----------------"); DirContext testConn = null; testConn = getConnection(); try { testConn.close(); } catch (NamingException e) { System.out.println("Naming exception in LDAP "+e); throw new ConnectorException("Naming exception in LDAP "+e); } } @Override public Schema discoverSchema() throws ConnectorException, UnsupportedOperationException { // TODO Auto-generated method stub System.out.println("INSIDE DISCOVER SCHEMA"); return null; } /** * read is the default method which we should implement as we are using interface Connector */ @Override public Map read(String arg0) throws ConnectorException, ObjectNotFoundException, UnsupportedOperationException { return null; } public boolean createAccount(Plan plan) { Attributes attributes = new BasicAttributes(); Attribute attribute = new BasicAttribute("objectClass"); String contextString = ""; HashMap userData = new HashMap(); Request request = null; Result result = null; Map object = new HashMap(); DirContext testConn = null; testConn = getConnection(); Boolean returnFlag = true; System.out.println(plan.toJson()); System.out.println("Connected to LDAP"); System.out.println(plan.getRequests()); for(int i=0;i userData = new HashMap(); Iterator> itr = null; Map object = new HashMap(); String id; DirContext updateConn = getConnection(); ModificationItem[] item = new ModificationItem[1]; Attribute attribute ; for(int i=0;i entry = itr.next(); object.put(entry.getKey(), entry.getValue()); try { if(entry.getKey().toString().equals("title")) { System.out.println("INSIDE DISABLE ACCOUNT"); if(entry.getKey().toString().equals("inactive")) { item[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("ds-pwp-account-disabled", "true")); updateConn.modifyAttributes(id, item); System.out.println("ACCOUNT DISABLED1"); } else if(entry.getKey().toString().equals("active")) { item[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("ds-pwp-account-disabled", "false")); updateConn.modifyAttributes(id, item); System.out.println("ACCOUNT ENABLED1"); } } attribute = new BasicAttribute(entry.getKey(),entry.getValue()); item[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute); updateConn.modifyAttributes(id, item); } catch(NamingException e){ System.out.println("Naming Exception while inserting records."+e); throw new UnsupportedOperationException("Naming Exception while inserting records."+e); } } result.setObject(object); plan.getRequests().get(0).setResult(result); return true; } }