Search in sources :

Example 26 with Attributes

use of javax.naming.directory.Attributes in project perun by CESNET.

the class ExtSourceLdap method getGroupSubjects.

public List<Map<String, String>> getGroupSubjects(Map<String, String> attributes) throws InternalErrorException {
    NamingEnumeration<SearchResult> results = null;
    List<String> ldapGroupSubjects = new ArrayList<String>();
    // Get the LDAP group name
    String ldapGroupName = attributes.get(GroupsManager.GROUPMEMBERSQUERY_ATTRNAME);
    // Get optional filter for members filtering
    String filter = attributes.get(GroupsManager.GROUPMEMBERSFILTER_ATTRNAME);
    try {
        log.trace("LDAP External Source: searching for group subjects [{}]", ldapGroupName);
        String attrName;
        if (getAttributes().containsKey("memberAttribute")) {
            attrName = (String) getAttributes().get("memberAttribute");
        } else {
            // Default value
            attrName = "uniqueMember";
        }
        List<String> retAttrs = new ArrayList<String>();
        retAttrs.add(attrName);
        String[] retAttrsArray = retAttrs.toArray(new String[retAttrs.size()]);
        Attributes attrs = getContext().getAttributes(ldapGroupName, retAttrsArray);
        Attribute ldapAttribute = null;
        // Get the list of returned groups, should be only one
        if (attrs.get(attrName) != null) {
            // Get the attribute which holds group subjects
            ldapAttribute = attrs.get(attrName);
        }
        if (ldapAttribute != null) {
            // Get the DNs of the subjects
            for (int i = 0; i < ldapAttribute.size(); i++) {
                String ldapSubjectDN = (String) ldapAttribute.get(i);
                ldapGroupSubjects.add(ldapSubjectDN);
                log.trace("LDAP External Source: found group subject [{}].", ldapSubjectDN);
            }
        }
        List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
        // If attribute filter not exists, use optional default filter from extSource definition
        if (filter == null)
            filter = filteredQuery;
        // Now query LDAP again and search for each subject
        for (String ldapSubjectName : ldapGroupSubjects) {
            subjects.addAll(this.querySource(filter, ldapSubjectName, 0));
        }
        return subjects;
    } catch (NamingException e) {
        log.error("LDAP exception during running query '{}'", ldapGroupName);
        throw new InternalErrorException("Entry '" + ldapGroupName + "' was not found in LDAP.", e);
    } finally {
        try {
            if (results != null) {
                results.close();
            }
        } catch (Exception e) {
            log.error("LDAP exception during closing result, while running query '{}'", ldapGroupName);
            throw new InternalErrorException(e);
        }
    }
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ExtSourceUnsupportedOperationException(cz.metacentrum.perun.core.api.exceptions.ExtSourceUnsupportedOperationException) NamingException(javax.naming.NamingException) SubjectNotExistsException(cz.metacentrum.perun.core.api.exceptions.SubjectNotExistsException) NamingException(javax.naming.NamingException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with Attributes

use of javax.naming.directory.Attributes in project perun by CESNET.

the class ExtSourceLdap method querySource.

/**
	 * Query LDAP using query in defined base. Results can be limited to the maxResults.
	 *
	 * @param query
	 * @param base
	 * @param maxResults
	 * @return List of Map of the LDAP attribute names and theirs values
	 * @throws InternalErrorException
	 */
protected List<Map<String, String>> querySource(String query, String base, int maxResults) throws InternalErrorException {
    NamingEnumeration<SearchResult> results = null;
    List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
    try {
        // If query is null, then we are finding object by the base
        if (query == null) {
            log.trace("search base [{}]", base);
            // TODO jmena atributu spise prijimiat pres vstupni parametr metody
            Attributes ldapAttributes = getContext().getAttributes(base);
            if (ldapAttributes.size() > 0) {
                Map<String, String> attributes = this.getSubjectAttributes(ldapAttributes);
                if (!attributes.isEmpty()) {
                    subjects.add(attributes);
                }
            }
        } else {
            log.trace("search string [{}]", query);
            SearchControls controls = new SearchControls();
            controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            // Set timeout to 5s
            controls.setTimeLimit(5000);
            if (maxResults > 0) {
                controls.setCountLimit(maxResults);
            }
            if (base == null)
                base = "";
            results = getContext().search(base, query, controls);
            while (results.hasMore()) {
                SearchResult searchResult = (SearchResult) results.next();
                Attributes attributes = searchResult.getAttributes();
                Map<String, String> subjectAttributes = this.getSubjectAttributes(attributes);
                if (!subjectAttributes.isEmpty()) {
                    subjects.add(subjectAttributes);
                }
            }
        }
        log.trace("Returning [{}] subjects", subjects.size());
        return subjects;
    } catch (NamingException e) {
        log.error("LDAP exception during running query '{}'", query);
        throw new InternalErrorException("LDAP exception during running query: " + query + ".", e);
    } finally {
        try {
            if (results != null) {
                results.close();
            }
        } catch (Exception e) {
            log.error("LDAP exception during closing result, while running query '{}'", query);
            throw new InternalErrorException(e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) SearchControls(javax.naming.directory.SearchControls) NamingException(javax.naming.NamingException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) HashMap(java.util.HashMap) Map(java.util.Map) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ExtSourceUnsupportedOperationException(cz.metacentrum.perun.core.api.exceptions.ExtSourceUnsupportedOperationException) NamingException(javax.naming.NamingException) SubjectNotExistsException(cz.metacentrum.perun.core.api.exceptions.SubjectNotExistsException)

Example 28 with Attributes

use of javax.naming.directory.Attributes in project cloudstack by apache.

the class OpenLdapUserManagerImpl method createUser.

protected LdapUser createUser(final SearchResult result) throws NamingException {
    final Attributes attributes = result.getAttributes();
    final String username = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getUsernameAttribute());
    final String email = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getEmailAttribute());
    final String firstname = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getFirstnameAttribute());
    final String lastname = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getLastnameAttribute());
    final String principal = result.getNameInNamespace();
    String domain = principal.replace("cn=" + LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getCommonNameAttribute()) + ",", "");
    domain = domain.replace("," + _ldapConfiguration.getBaseDn(), "");
    domain = domain.replace("ou=", "");
    boolean disabled = isUserDisabled(result);
    return new LdapUser(username, email, firstname, lastname, principal, domain, disabled);
}
Also used : Attributes(javax.naming.directory.Attributes)

Example 29 with Attributes

use of javax.naming.directory.Attributes in project jmeter by apache.

the class LDAPExtSampler method writeSearchResult.

private void writeSearchResult(final SearchResult sr, final XMLBuffer xmlb) throws NamingException {
    final Attributes attrs = sr.getAttributes();
    final int size = attrs.size();
    final ArrayList<Attribute> sortedAttrs = new ArrayList<>(size);
    // $NON-NLS-1$
    xmlb.openTag("searchresult");
    // $NON-NLS-1$
    xmlb.tag("dn", sr.getName());
    // $NON-NLS-1$
    xmlb.tag("returnedattr", Integer.toString(size));
    // $NON-NLS-1$
    xmlb.openTag("attributes");
    try {
        for (NamingEnumeration<? extends Attribute> en = attrs.getAll(); en.hasMore(); ) {
            final Attribute attr = en.next();
            sortedAttrs.add(attr);
        }
        sortAttributes(sortedAttrs);
        for (final Attribute attr : sortedAttrs) {
            StringBuilder sb = new StringBuilder();
            if (attr.size() == 1) {
                sb.append(getWriteValue(attr.get()));
            } else {
                final ArrayList<String> sortedVals = new ArrayList<>(attr.size());
                boolean first = true;
                for (NamingEnumeration<?> ven = attr.getAll(); ven.hasMore(); ) {
                    final Object value = getWriteValue(ven.next());
                    sortedVals.add(value.toString());
                }
                Collections.sort(sortedVals);
                for (final String value : sortedVals) {
                    if (first) {
                        first = false;
                    } else {
                        // $NON-NLS-1$
                        sb.append(", ");
                    }
                    sb.append(value);
                }
            }
            xmlb.tag(attr.getID(), sb);
        }
    } finally {
        // $NON-NLS-1$
        xmlb.closeTag("attributes");
        // $NON-NLS-1$
        xmlb.closeTag("searchresult");
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) ArrayList(java.util.ArrayList)

Example 30 with Attributes

use of javax.naming.directory.Attributes in project jmeter by apache.

the class LDAPExtSampler method getUserAttributes.

/***************************************************************************
     * Collect all the values from the table (Arguments), using this create the
     * Attributes, this will create the Attributes for the User
     * defined TestCase for Add Test
     *
     * @return The Attributes
     **************************************************************************/
private Attributes getUserAttributes() {
    Attributes attrs = new BasicAttributes(true);
    Attribute attr;
    for (JMeterProperty jMeterProperty : getArguments()) {
        Argument item = (Argument) jMeterProperty.getObjectValue();
        attr = attrs.get(item.getName());
        if (attr == null) {
            attr = getBasicAttribute(item.getName(), item.getValue());
        } else {
            attr.add(item.getValue());
        }
        attrs.put(attr);
    }
    return attrs;
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) LDAPArgument(org.apache.jmeter.protocol.ldap.config.gui.LDAPArgument) Argument(org.apache.jmeter.config.Argument) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes)

Aggregations

Attributes (javax.naming.directory.Attributes)81 Attribute (javax.naming.directory.Attribute)57 SearchResult (javax.naming.directory.SearchResult)32 BasicAttributes (javax.naming.directory.BasicAttributes)31 NamingException (javax.naming.NamingException)26 BasicAttribute (javax.naming.directory.BasicAttribute)24 SearchControls (javax.naming.directory.SearchControls)23 NamingEnumeration (javax.naming.NamingEnumeration)19 DirContext (javax.naming.directory.DirContext)19 ArrayList (java.util.ArrayList)17 InitialDirContext (javax.naming.directory.InitialDirContext)12 IOException (java.io.IOException)10 Hashtable (java.util.Hashtable)9 HashSet (java.util.HashSet)8 LdapContext (javax.naming.ldap.LdapContext)8 File (java.io.File)7 MutablePartitionConfiguration (org.apache.directory.server.core.configuration.MutablePartitionConfiguration)7 AbstractBootstrapSchema (org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema)7 Test (org.junit.Test)6 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)5