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;
}
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);
}
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;
}
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;
}
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);
}
}
}
}
Aggregations