Search in sources :

Example 51 with BasicAttributes

use of javax.naming.directory.BasicAttributes 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)

Example 52 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project zeppelin by apache.

the class LdapRealmTest method testRolesFor.

@Test
public void testRolesFor() throws NamingException {
    LdapRealm realm = new LdapRealm();
    realm.setGroupSearchBase("cn=groups,dc=apache");
    realm.setGroupObjectClass("posixGroup");
    realm.setMemberAttributeValueTemplate("cn={0},ou=people,dc=apache");
    HashMap<String, String> rolesByGroups = new HashMap<>();
    rolesByGroups.put("group-three", "zeppelin-role");
    realm.setRolesByGroup(rolesByGroups);
    LdapContextFactory ldapContextFactory = mock(LdapContextFactory.class);
    LdapContext ldapCtx = mock(LdapContext.class);
    Session session = mock(Session.class);
    // expected search results
    BasicAttributes group1 = new BasicAttributes();
    group1.put(realm.getGroupIdAttribute(), "group-one");
    group1.put(realm.getMemberAttribute(), "principal");
    // user doesn't belong to this group
    BasicAttributes group2 = new BasicAttributes();
    group2.put(realm.getGroupIdAttribute(), "group-two");
    group2.put(realm.getMemberAttribute(), "someoneelse");
    // mapped to a different Zeppelin role
    BasicAttributes group3 = new BasicAttributes();
    group3.put(realm.getGroupIdAttribute(), "group-three");
    group3.put(realm.getMemberAttribute(), "principal");
    NamingEnumeration<SearchResult> results = enumerationOf(group1, group2, group3);
    when(ldapCtx.search(any(String.class), any(String.class), any(SearchControls.class))).thenReturn(results);
    Set<String> roles = realm.rolesFor(new SimplePrincipalCollection("principal", "ldapRealm"), "principal", ldapCtx, ldapContextFactory, session);
    verify(ldapCtx).search("cn=groups,dc=apache", "(objectclass=posixGroup)", realm.getGroupSearchControls());
    assertEquals(new HashSet(Arrays.asList("group-one", "zeppelin-role")), roles);
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) HashMap(java.util.HashMap) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SearchResult(javax.naming.directory.SearchResult) LdapContextFactory(org.apache.shiro.realm.ldap.LdapContextFactory) SearchControls(javax.naming.directory.SearchControls) LdapContext(javax.naming.ldap.LdapContext) Session(org.apache.shiro.session.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 53 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project hive by apache.

the class LdapTestUtils method mockAttributes.

private static Attributes mockAttributes(NameValues... namedValues) throws NamingException {
    Attributes attributes = new BasicAttributes();
    for (NameValues namedValue : namedValues) {
        Attribute attr = new BasicAttribute(namedValue.name);
        for (String value : namedValue.values) {
            attr.add(value);
        }
        attributes.put(attr);
    }
    return attributes;
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes)

Example 54 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project tomcat by apache.

the class TestJNDIRealm method mockSearchResults.

private NamingEnumeration<SearchResult> mockSearchResults(String password) throws NamingException {
    NamingEnumeration<SearchResult> searchResults = EasyMock.createNiceMock(NamingEnumeration.class);
    EasyMock.expect(Boolean.valueOf(searchResults.hasMore())).andReturn(Boolean.TRUE).andReturn(Boolean.FALSE).andReturn(Boolean.TRUE).andReturn(Boolean.FALSE);
    EasyMock.expect(searchResults.next()).andReturn(new SearchResult("ANY RESULT", "", new BasicAttributes(USER_PASSWORD_ATTR, password))).times(2);
    EasyMock.replay(searchResults);
    return searchResults;
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) SearchResult(javax.naming.directory.SearchResult)

Example 55 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project CzechIdMng by bcvsolutions.

the class AdUserConnectorType method createTestUser.

/**
 * Create test certificates.
 */
protected String createTestUser(String username, String entryDN, String port, String host, String adUser, String adPassword, boolean ssl) {
    DirContext ldapContext = null;
    try {
        // Init LDAP context.
        Hashtable<String, String> ldapEnv = getAdEnvironment(host, port, adUser, adPassword, ssl);
        ldapContext = new InitialDirContext(ldapEnv);
        // Entry's attributes.
        Attribute cn = new BasicAttribute("cn", username);
        Attribute oc = new BasicAttribute("objectClass");
        oc.add("top");
        oc.add("person");
        oc.add("organizationalPerson");
        oc.add("inetOrgPerson");
        // Build the entry
        BasicAttributes entry = new BasicAttributes();
        entry.put(cn);
        entry.put(oc);
        // Add the entry.
        DirContext context = ldapContext.createSubcontext(MessageFormat.format("CN={0},{1}", username, entryDN), entry);
        Attributes attributes = context.getAttributes("");
        return (String) attributes.get("distinguishedname").get();
    } catch (NameAlreadyBoundException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_CONNECTOR_DN_ALREADY_EXISTS, ImmutableMap.of("dn", MessageFormat.format("CN={0},{1}", username, entryDN)), ex);
    } catch (CommunicationException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_COMMUNICATION_EXCEPTION, ImmutableMap.of("host", host), ex);
    } catch (NamingException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_OPERATION_FAILED, ImmutableMap.of("dn", MessageFormat.format("CN={0},{1}", username, entryDN)), ex);
    } finally {
        if (ldapContext != null) {
            try {
                ldapContext.close();
            } catch (NamingException e) {
                // Only log it.
                LOG.error(e.getLocalizedMessage(), e);
            }
        }
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) CommunicationException(javax.naming.CommunicationException) BasicAttribute(javax.naming.directory.BasicAttribute) IcAttribute(eu.bcvsolutions.idm.ic.api.IcAttribute) Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) BasicAttributes(javax.naming.directory.BasicAttributes) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) InitialDirContext(javax.naming.directory.InitialDirContext)

Aggregations

BasicAttributes (javax.naming.directory.BasicAttributes)100 Attributes (javax.naming.directory.Attributes)62 BasicAttribute (javax.naming.directory.BasicAttribute)57 Attribute (javax.naming.directory.Attribute)44 Test (org.junit.Test)22 SearchResult (javax.naming.directory.SearchResult)21 DirContext (javax.naming.directory.DirContext)18 NamingException (javax.naming.NamingException)15 InitialDirContext (javax.naming.directory.InitialDirContext)14 NamingEnumeration (javax.naming.NamingEnumeration)12 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)10 LdapContext (javax.naming.ldap.LdapContext)9 HashMap (java.util.HashMap)8 InitialLdapContext (javax.naming.ldap.InitialLdapContext)8 File (java.io.File)7 Map (java.util.Map)7 MutablePartitionConfiguration (org.apache.directory.server.core.configuration.MutablePartitionConfiguration)7 AbstractBootstrapSchema (org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema)7 IOException (java.io.IOException)6