@Override public void start() { if (isRunning()) { return; } try { InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(this.defaultPartitionSuffix); config.addAdditionalBindCredentials("uid=admin,ou=system", "secret"); config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port)); config.setEnforceSingleStructuralObjectClass(false); config.setEnforceAttributeSyntaxCompliance(true); DN dn = new DN(this.defaultPartitionSuffix); Entry entry = new Entry(dn); entry.addAttribute("objectClass", "top", "domain", "extensibleObject"); entry.addAttribute("dc", dn.getRDN().getAttributeValues()[0]); InMemoryDirectoryServer directoryServer = new InMemoryDirectoryServer(config); directoryServer.add(entry); importLdif(directoryServer); directoryServer.startListening(); this.port = directoryServer.getListenPort(); this.directoryServer = directoryServer; this.running = true; } catch (LDAPException ex) { throw new RuntimeException("Server startup failed", ex); } }
protected void sendResult ( InMemoryInterceptedSearchResult result, String base, Entry e ) throws LDAPException, MalformedURLException { URL turl = new URL(this.codebase, this.codebase.getRef().replace('.', '/').concat(".class")); System.out.println("Send LDAP reference result for " + base + " redirecting to " + turl); e.addAttribute("javaClassName", "foo"); String cbstring = this.codebase.toString(); int refPos = cbstring.indexOf('#'); if ( refPos > 0 ) { cbstring = cbstring.substring(0, refPos); } e.addAttribute("javaCodeBase", cbstring); e.addAttribute("objectClass", "javaNamingReference"); //$NON-NLS-1$ e.addAttribute("javaFactory", this.codebase.getRef()); result.sendSearchEntry(e); result.setResult(new LDAPResult(0, ResultCode.SUCCESS)); }
@Override public void createAccount(LdapAccount account) throws LdapSystemException { try (LDAPConnection conn = ldapPool.getConnection()) { String entryDN = getAccountDN(account); log.info("Creating user with DN {}", entryDN); Entry entry = new Entry(entryDN); entry.addAttribute("objectClass", "inetOrgPerson"); for (String email : account.getEmails()) { entry.addAttribute("mail", email); } entry.addAttribute("givenName", account.getGivenName()); entry.addAttribute("sn", account.getFamilyName()); entry.addAttribute("cn", account.getName()); entry.addAttribute("employeeNumber", account.getSubject()); entry.addAttribute("userPassword", account.getPassword()); entry.addAttribute("employeeType", account.getRole().toString()); conn.add(entry); } catch (LDAPException e) { throw new LdapSystemException(e); } }
/** * Adds the specified attribute value to this entry, if it is not already * present. * * @param attributeName The name for the attribute to be added. It must * not be {@code null}. * @param attributeValue The value for the attribute to be added. It must * not be {@code null}. * * @return {@code true} if the entry was updated, or {@code false} because * the specified attribute already existed with the given value. */ public boolean addAttribute(final String attributeName, final String attributeValue) { ensureNotNull(attributeName, attributeValue); return addAttribute(new Attribute(attributeName, schema, attributeValue)); }
private void creatOrgUnits(LDAPConnection conn) throws LDAPException { String peopleDn = config.getLdapUserBaseDN(); if (conn.getEntry(peopleDn) == null) { Entry entry = new Entry(peopleDn); entry.addAttribute("objectClass", "top"); entry.addAttribute("objectClass", "organizationalUnit"); conn.add(entry); } String groupDn = config.getLdapGroupsBaseDN(); if (conn.getEntry(groupDn) == null) { Entry entry = new Entry(groupDn); entry.addAttribute("objectClass", "top"); entry.addAttribute("objectClass", "organizationalUnit"); conn.add(entry); } }
/** * Adds the specified attribute value to this entry, if it is not already * present. * * @param attributeName The name for the attribute to be added. It must * not be {@code null}. * @param attributeValue The value for the attribute to be added. It must * not be {@code null}. * * @return {@code true} if the entry was updated, or {@code false} because * the specified attribute already existed with the given value. */ public boolean addAttribute(final String attributeName, final byte[] attributeValue) { ensureNotNull(attributeName, attributeValue); return addAttribute(new Attribute(attributeName, schema, attributeValue)); }
/** * Adds the provided attribute to this entry. If this entry already contains * an attribute with the same name, then their values will be merged. * * @param attributeName The name for the attribute to be added. It must * not be {@code null}. * @param attributeValues The value for the attribute to be added. It must * not be {@code null}. * * @return {@code true} if the entry was updated, or {@code false} because * the specified attribute already existed with all provided values. */ public boolean addAttribute(final String attributeName, final Collection<String> attributeValues) { ensureNotNull(attributeName, attributeValues); return addAttribute(new Attribute(attributeName, schema, attributeValues)); }
/** * Adds the specified attribute value to this entry, if it is not already * present. * * @param attributeName The name for the attribute to be added. It must * not be {@code null}. * @param attributeValue The value for the attribute to be added. It must * not be {@code null}. * * @return {@code true} if the entry was updated, or {@code false} because * the specified attribute already existed with the given value. */ public boolean addAttribute(final String attributeName, final String attributeValue) { ensureNotNull(attributeName, attributeValue); return addAttribute(new Attribute(attributeName, schema, attributeValue)); }
/** * Adds the provided attribute to this entry. If this entry already contains * an attribute with the same name, then their values will be merged. * * @param attributeName The name for the attribute to be added. It must * not be {@code null}. * @param attributeValues The value for the attribute to be added. It must * not be {@code null}. * * @return {@code true} if the entry was updated, or {@code false} because * the specified attribute already existed with all provided values. */ public boolean addAttribute(final String attributeName, final String... attributeValues) { ensureNotNull(attributeName, attributeValues); return addAttribute(new Attribute(attributeName, schema, attributeValues)); }
/** * Adds the provided attribute to this entry. If this entry already contains * an attribute with the same name, then their values will be merged. * * @param attributeName The name for the attribute to be added. It must * not be {@code null}. * @param attributeValues The value for the attribute to be added. It must * not be {@code null}. * * @return {@code true} if the entry was updated, or {@code false} because * the specified attribute already existed with all provided values. */ public boolean addAttribute(final String attributeName, final String... attributeValues) { ensureNotNull(attributeName, attributeValues); return addAttribute(new Attribute(attributeName, schema, attributeValues)); }
/** * Adds the provided attribute to this entry. If this entry already contains * an attribute with the same name, then their values will be merged. * * @param attributeName The name for the attribute to be added. It must * not be {@code null}. * @param attributeValues The value for the attribute to be added. It must * not be {@code null}. * * @return {@code true} if the entry was updated, or {@code false} because * the specified attribute already existed with all provided values. */ public boolean addAttribute(final String attributeName, final byte[]... attributeValues) { ensureNotNull(attributeName, attributeValues); return addAttribute(new Attribute(attributeName, schema, attributeValues)); }
/** * Adds the provided attribute to this entry. If this entry already contains * an attribute with the same name, then their values will be merged. * * @param attributeName The name for the attribute to be added. It must * not be {@code null}. * @param attributeValues The value for the attribute to be added. It must * not be {@code null}. * * @return {@code true} if the entry was updated, or {@code false} because * the specified attribute already existed with all provided values. */ public boolean addAttribute(final String attributeName, final byte[]... attributeValues) { ensureNotNull(attributeName, attributeValues); return addAttribute(new Attribute(attributeName, schema, attributeValues)); }
/** * Adds the specified attribute value to this entry, if it is not already * present. * * @param attributeName The name for the attribute to be added. It must * not be {@code null}. * @param attributeValue The value for the attribute to be added. It must * not be {@code null}. * * @return {@code true} if the entry was updated, or {@code false} because * the specified attribute already existed with the given value. */ public boolean addAttribute(final String attributeName, final byte[] attributeValue) { ensureNotNull(attributeName, attributeValue); return addAttribute(new Attribute(attributeName, schema, attributeValue)); }
/** * Adds the provided attribute to this entry. If this entry already contains * an attribute with the same name, then their values will be merged. * * @param attributeName The name for the attribute to be added. It must * not be {@code null}. * @param attributeValues The value for the attribute to be added. It must * not be {@code null}. * * @return {@code true} if the entry was updated, or {@code false} because * the specified attribute already existed with all provided values. */ public boolean addAttribute(final String attributeName, final Collection<String> attributeValues) { ensureNotNull(attributeName, attributeValues); return addAttribute(new Attribute(attributeName, schema, attributeValues)); }
/** * Merges the contents of all provided entries so that the resulting entry * will contain all attribute values present in at least one of the entries. * * @param entries The set of entries to be merged. At least one entry must * be provided. * * @return An entry containing all attribute values present in at least one * of the entries. */ public static Entry mergeEntries(final Entry... entries) { ensureNotNull(entries); ensureTrue(entries.length > 0); final Entry newEntry = entries[0].duplicate(); for (int i=1; i < entries.length; i++) { for (final Attribute a : entries[i].attributes.values()) { newEntry.addAttribute(a); } } return newEntry; }
/** * Merges the contents of all provided entries so that the resulting entry * will contain all attribute values present in at least one of the entries. * * @param entries The set of entries to be merged. At least one entry must * be provided. * * @return An entry containing all attribute values present in at least one * of the entries. */ public static Entry mergeEntries(final Entry... entries) { ensureNotNull(entries); ensureTrue(entries.length > 0); final Entry newEntry = entries[0].duplicate(); for (int i=1; i < entries.length; i++) { for (final Attribute a : entries[i].attributes.values()) { newEntry.addAttribute(a); } } return newEntry; }
private void initializeEmbeddedLDAPServer() throws Exception { log.info("Creating Embedded LDAP server"); InMemoryListenerConfig inMemoryListenerConfig = InMemoryListenerConfig.createLDAPConfig("default", ldapPort); InMemoryDirectoryServerConfig directoryServerConfig = new InMemoryDirectoryServerConfig(new DN(baseDN)); directoryServerConfig.setListenerConfigs(inMemoryListenerConfig); directoryServerConfig.addAdditionalBindCredentials(securityPrincipal, securityCredentials); ldapServer = new InMemoryDirectoryServer(directoryServerConfig); ldapServer.startListening(); Entry wso2Entry = new Entry(baseDN); wso2Entry.addAttribute("objectClass", "dcObject"); wso2Entry.addAttribute("objectClass", "organizationalUnit"); wso2Entry.addAttribute("ou", "WSO2"); wso2Entry.addAttribute("dc", "WSO2"); ldapServer.add(wso2Entry); Entry entry = new Entry(userBase); entry.addAttribute("objectClass", "organizationalUnit"); ldapServer.add(entry); }
public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, String defaultPartitionSuffix, int port) throws Exception { InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig( defaultPartitionSuffix); config.addAdditionalBindCredentials("uid=admin,ou=system", "secret"); config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port)); config.setEnforceSingleStructuralObjectClass(false); config.setEnforceAttributeSyntaxCompliance(true); Entry entry = new Entry(new DN(defaultPartitionSuffix)); entry.addAttribute("objectClass", "top", "domain", "extensibleObject"); entry.addAttribute("dc", defaultPartitionName); InMemoryDirectoryServer directoryServer = new InMemoryDirectoryServer(config); directoryServer.add(entry); directoryServer.startListening(); return new EmbeddedLdapServer(directoryServer); }
@Override public void addGroupMember(String accountDN, String groupName) throws LdapSystemException { try (LDAPConnection conn = ldapPool.getConnection()) { LdapGroup group = getGroup(groupName, conn); if (group != null && group.getMembersDn().contains(accountDN)) { log.info("Nothing to do. Account {} is already member of group {}", accountDN, group.getName()); } else { if (group == null) { log.debug("Creating group {}", groupName); DN groupDN = new DN(new RDN(GROUP_NAME_ATTR, groupName), new DN(config.getLdapGroupsBaseDN())); Entry groupEntry = new Entry(groupDN); groupEntry.addAttribute("objectClass", config.getLdapGroupsObjectClass()); groupEntry.addAttribute(config.getLdapGroupsMemberAttr(), accountDN); conn.add(groupEntry); log.info("Group {} added", groupDN); } else { Modification mod = new Modification(ADD, config.getLdapGroupsMemberAttr(), accountDN); conn.modify(new ModifyRequest(group.getDn(), mod)); log.info("Added membership {} to {}", accountDN, group.getName()); } } } catch (LDAPException e) { throw new LdapSystemException(e); } }
@Override public void start() { if (isRunning()) { return; } try { InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(this.defaultPartitionSuffix); config.addAdditionalBindCredentials("uid=admin,ou=system", "secret"); config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port)); config.setEnforceSingleStructuralObjectClass(false); config.setEnforceAttributeSyntaxCompliance(true); DN dn = new DN(this.defaultPartitionSuffix); Entry entry = new Entry(dn); entry.addAttribute("objectClass", "top", "domain", "extensibleObject"); entry.addAttribute("dc", dn.getRDN().getAttributeValues()[0]); InMemoryDirectoryServer directoryServer = new InMemoryDirectoryServer(config); directoryServer.add(entry); importLdif(directoryServer); directoryServer.startListening(); this.port = directoryServer.getListenPort(); this.directoryServer = directoryServer; this.running = true; } catch (LDAPException ex) { throw new RuntimeException("Server startup failed", ex); } }