Search in sources :

Example 36 with DirContext

use of javax.naming.directory.DirContext in project karaf by apache.

the class LDAPCache method doGetUserRoles.

private String[] doGetUserRoles(String user, String userDn, String userDnNamespace) throws NamingException {
    DirContext context = open();
    SearchControls controls = new SearchControls();
    if (options.getRoleSearchSubtree()) {
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    } else {
        controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    }
    String filter = options.getRoleFilter();
    if (filter != null) {
        filter = filter.replaceAll(Pattern.quote("%u"), Matcher.quoteReplacement(user));
        filter = filter.replaceAll(Pattern.quote("%dn"), Matcher.quoteReplacement(userDn));
        filter = filter.replaceAll(Pattern.quote("%fqdn"), Matcher.quoteReplacement(userDnNamespace));
        filter = filter.replace("\\", "\\\\");
        LOGGER.debug("Looking for the user roles in LDAP with ");
        LOGGER.debug("  base DN: " + options.getRoleBaseDn());
        LOGGER.debug("  filter: " + filter);
        NamingEnumeration namingEnumeration = context.search(options.getRoleBaseDn(), filter, controls);
        try {
            List<String> rolesList = new ArrayList<>();
            while (namingEnumeration.hasMore()) {
                SearchResult result = (SearchResult) namingEnumeration.next();
                Attributes attributes = result.getAttributes();
                Attribute roles1 = attributes.get(options.getRoleNameAttribute());
                if (roles1 != null) {
                    for (int i = 0; i < roles1.size(); i++) {
                        String role = (String) roles1.get(i);
                        if (role != null) {
                            LOGGER.debug("User {} is a member of role {}", user, role);
                            // handle role mapping
                            Set<String> roleMappings = tryMappingRole(role);
                            if (roleMappings.isEmpty()) {
                                rolesList.add(role);
                            } else {
                                for (String roleMapped : roleMappings) {
                                    rolesList.add(roleMapped);
                                }
                            }
                        }
                    }
                }
            }
            return rolesList.toArray(new String[rolesList.size()]);
        } finally {
            if (namingEnumeration != null) {
                try {
                    namingEnumeration.close();
                } catch (NamingException e) {
                // Ignore
                }
            }
        }
    } else {
        LOGGER.debug("The user role filter is null so no roles are retrieved");
        return new String[] {};
    }
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) EventDirContext(javax.naming.event.EventDirContext) DirContext(javax.naming.directory.DirContext)

Example 37 with DirContext

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

the class LDAPExtSampler method testEnded.

// Ensure any remaining contexts are closed
@Override
public void testEnded(String host) {
    for (Map.Entry<String, DirContext> entry : ldapContexts.entrySet()) {
        DirContext dc = entry.getValue();
        try {
            log.warn("Tidying old Context for thread: " + entry.getKey());
            dc.close();
        } catch (NamingException ignored) {
        // ignored
        }
    }
    ldapContexts.clear();
}
Also used : NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 38 with DirContext

use of javax.naming.directory.DirContext in project wildfly by wildfly.

the class LdapUrlTestServlet method runSearch.

/**
     * Try to search in LDAP with search base containing URL. Also try to retrieve RequestControls from LdapContext.
     *
     * @param hostname
     * @return
     * @throws Exception
     */
public static String runSearch(final String hostname, boolean testLdapCtx) throws Exception {
    final StringBuilder result = new StringBuilder();
    final String ldapUrl = "ldap://" + (hostname == null ? "localhost" : hostname) + ":10389";
    final Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.PROVIDER_URL, ldapUrl);
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    final SearchControls ctl = new SearchControls();
    ctl.setReturningAttributes(new String[] { "cn" });
    DirContext dirCtx = null;
    if (testLdapCtx) {
        // LdapContext must also work
        LdapContext ldapCtx = new InitialLdapContext(env, null);
        // next line tests if the LdapContext works
        ldapCtx.getRequestControls();
        dirCtx = ldapCtx;
    } else {
        dirCtx = new InitialDirContext(env);
    }
    final NamingEnumeration<SearchResult> nenum = dirCtx.search(ldapUrl + "/dc=jboss,dc=org", "(uid=jduke)", ctl);
    while (nenum.hasMore()) {
        SearchResult sr = nenum.next();
        Attributes attrs = sr.getAttributes();
        result.append("cn=").append(attrs.get("cn").get());
    }
    dirCtx.close();
    return result.toString();
}
Also used : Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 39 with DirContext

use of javax.naming.directory.DirContext in project jetcd by coreos.

the class DnsSrvNameResolver method getServers.

@Override
protected List<ResolvedServerInfo> getServers() {
    try {
        DirContext ctx = new InitialDirContext(ENV);
        NamingEnumeration<?> resolved = ctx.getAttributes(name, ATTRIBUTE_IDS).get("srv").getAll();
        List<ResolvedServerInfo> servers = new LinkedList<>();
        while (resolved.hasMore()) {
            servers.add(srvRecordToServerInfo((String) resolved.next()));
        }
        return servers;
    } catch (Exception e) {
        LOGGER.warn("", e);
    }
    return Collections.emptyList();
}
Also used : DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) ResolvedServerInfo(io.grpc.ResolvedServerInfo) LinkedList(java.util.LinkedList)

Example 40 with DirContext

use of javax.naming.directory.DirContext in project jdk8u_jdk by JetBrains.

the class ldapURLContextFactory method getUsingURLIgnoreRootDN.

static ResolveResult getUsingURLIgnoreRootDN(String url, Hashtable<?, ?> env) throws NamingException {
    LdapURL ldapUrl = new LdapURL(url);
    DirContext ctx = new LdapCtx("", ldapUrl.getHost(), ldapUrl.getPort(), env, ldapUrl.useSsl());
    String dn = (ldapUrl.getDN() != null ? ldapUrl.getDN() : "");
    // Represent DN as empty or single-component composite name.
    CompositeName remaining = new CompositeName();
    if (!"".equals(dn)) {
        // if nonempty, add component
        remaining.add(dn);
    }
    return new ResolveResult(ctx, remaining);
}
Also used : LdapURL(com.sun.jndi.ldap.LdapURL) LdapCtx(com.sun.jndi.ldap.LdapCtx) DirContext(javax.naming.directory.DirContext)

Aggregations

DirContext (javax.naming.directory.DirContext)111 NamingException (javax.naming.NamingException)51 InitialDirContext (javax.naming.directory.InitialDirContext)43 SearchResult (javax.naming.directory.SearchResult)27 SearchControls (javax.naming.directory.SearchControls)24 Attributes (javax.naming.directory.Attributes)21 Attribute (javax.naming.directory.Attribute)17 IOException (java.io.IOException)16 NamingEnumeration (javax.naming.NamingEnumeration)16 Hashtable (java.util.Hashtable)14 Test (org.junit.Test)14 DistinguishedName (org.springframework.ldap.core.DistinguishedName)11 ProxyDirContext (org.apache.naming.resources.ProxyDirContext)10 WebDirContext (org.apache.naming.resources.WebDirContext)9 ArrayList (java.util.ArrayList)8 BaseDirContext (org.apache.naming.resources.BaseDirContext)8 FileDirContext (org.apache.naming.resources.FileDirContext)8 WARDirContext (org.apache.naming.resources.WARDirContext)8 Name (javax.naming.Name)7 BasicAttribute (javax.naming.directory.BasicAttribute)7