1 package org.apache.archiva.redback.rbac.ldap;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import junit.framework.TestCase;
22 import org.apache.archiva.redback.components.apacheds.ApacheDs;
23 import org.apache.archiva.redback.policy.PasswordEncoder;
24 import org.apache.archiva.redback.policy.encoders.SHA1PasswordEncoder;
25 import org.apache.archiva.redback.users.UserManager;
26 import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
27 import org.fest.assertions.Assertions;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.test.annotation.DirtiesContext;
35 import org.springframework.test.context.ContextConfiguration;
36 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import javax.naming.NameClassPair;
41 import javax.naming.NamingEnumeration;
42 import javax.naming.NamingException;
43 import javax.naming.directory.Attribute;
44 import javax.naming.directory.Attributes;
45 import javax.naming.directory.BasicAttribute;
46 import javax.naming.directory.BasicAttributes;
47 import javax.naming.directory.DirContext;
48 import javax.naming.directory.InitialDirContext;
49 import javax.naming.directory.SearchControls;
50 import javax.naming.directory.SearchResult;
51 import java.util.ArrayList;
52 import java.util.Arrays;
53 import java.util.HashMap;
54 import java.util.List;
58 * @author Olivier Lamy
60 @RunWith( SpringJUnit4ClassRunner.class )
61 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
62 @DirtiesContext( classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD )
63 public class TestLdapRoleMapper
67 Logger log = LoggerFactory.getLogger( getClass() );
70 @Named( value = "userManager#ldap" )
71 private UserManager userManager;
74 @Named( value = "apacheDS#test" )
75 private ApacheDs apacheDs;
77 private String suffix;
79 private String groupSuffix;
81 private PasswordEncoder passwordEncoder;
84 private LdapCacheService ldapCacheService;
87 @Named( value = "ldapRoleMapper#test" )
88 LdapRoleMapper ldapRoleMapper;
90 private Map<String, List<String>> usersPerGroup;
92 private List<String> users;
100 usersPerGroup = new HashMap<String, List<String>>( 3 );
102 usersPerGroup.put( "internal-repo-manager", Arrays.asList( "admin", "user.9" ) );
103 usersPerGroup.put( "internal-repo-observer", Arrays.asList( "admin", "user.7", "user.8" ) );
104 usersPerGroup.put( "archiva-admin", Arrays.asList( "admin", "user.7" ) );
106 users = new ArrayList<String>( 4 );
107 users.add( "admin" );
108 users.add( "user.7" );
109 users.add( "user.8" );
110 users.add( "user.9" );
112 passwordEncoder = new SHA1PasswordEncoder();
114 groupSuffix = apacheDs.addSimplePartition( "test", new String[]{ "archiva", "apache", "org" } ).getSuffix();
116 log.info( "groupSuffix: {}", groupSuffix );
118 suffix = "ou=People,dc=archiva,dc=apache,dc=org";
120 log.info( "DN Suffix: {}", suffix );
122 apacheDs.startServer();
124 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
125 objectClass.add( "top" );
126 objectClass.add( "organizationalUnit" );
128 Attributes attributes = new BasicAttributes( true );
129 attributes.put( objectClass );
130 attributes.put( "organizationalUnitName", "foo" );
131 //attributes.put( "ou", "People" );
133 apacheDs.getAdminContext().createSubcontext( suffix, attributes );
141 public void tearDown()
145 ldapCacheService.removeAllUsers();
147 InitialDirContext context = apacheDs.getAdminContext();
149 for ( String uid : users )
151 context.unbind( createDn( uid ) );
154 for ( Map.Entry<String, List<String>> group : usersPerGroup.entrySet() )
156 context.unbind( createGroupDn( group.getKey() ) );
159 context.unbind( suffix );
161 apacheDs.stopServer();
166 private void createGroups()
169 InitialDirContext context = apacheDs.getAdminContext();
171 for ( Map.Entry<String, List<String>> group : usersPerGroup.entrySet() )
173 createGroup( context, group.getKey(), createGroupDn( group.getKey() ), group.getValue() );
178 private void createGroup( DirContext context, String groupName, String dn, List<String> users )
182 Attributes attributes = new BasicAttributes( true );
183 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
184 objectClass.add( "top" );
185 objectClass.add( "groupOfUniqueNames" );
186 attributes.put( objectClass );
187 attributes.put( "cn", groupName );
188 BasicAttribute basicAttribute = new BasicAttribute( "uniquemember" );
189 for ( String user : users )
191 basicAttribute.add( "uid=" + user + ",dc=archiva,dc=apache,dc=org" );
194 attributes.put( basicAttribute );
195 context.createSubcontext( dn, attributes );
198 private void bindUserObject( DirContext context, String cn, String dn )
201 Attributes attributes = new BasicAttributes( true );
202 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
203 objectClass.add( "top" );
204 objectClass.add( "inetOrgPerson" );
205 objectClass.add( "person" );
206 objectClass.add( "organizationalperson" );
207 attributes.put( objectClass );
208 attributes.put( "cn", cn );
209 attributes.put( "sn", "foo" );
210 attributes.put( "mail", cn + "@apache.org" );
211 attributes.put( "userPassword", passwordEncoder.encodePassword( "foo" ) );
212 attributes.put( "givenName", "foo" );
213 context.createSubcontext( dn, attributes );
216 private void makeUsers()
220 for ( String uid : users )
227 private void makeUser( String uid )
230 InitialDirContext context = apacheDs.getAdminContext();
232 bindUserObject( context, uid, createDn( uid ) );
233 assertExist( context, createDn( uid ), "cn", uid );
238 private void assertExist( DirContext context, String dn, String attribute, String value )
239 throws NamingException
241 SearchControls ctls = new SearchControls();
243 ctls.setDerefLinkFlag( true );
244 ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
245 ctls.setReturningAttributes( new String[]{ "*" } );
247 BasicAttributes matchingAttributes = new BasicAttributes();
248 matchingAttributes.put( attribute, value );
249 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
250 objectClass.add( "inetOrgPerson" );
251 matchingAttributes.put( objectClass );
253 NamingEnumeration<SearchResult> results = context.search( suffix, matchingAttributes );
255 assertTrue( results.hasMoreElements() );
256 SearchResult result = results.nextElement();
257 Attributes attrs = result.getAttributes();
258 Attribute testAttr = attrs.get( attribute );
259 assertEquals( value, testAttr.get() );
263 private String createDn( String cn )
265 return "cn=" + cn + "," + suffix;
268 private String createGroupDn( String cn )
270 return "cn=" + cn + "," + groupSuffix;
274 public void getAllGroups()
277 List<String> allGroups = ldapRoleMapper.getAllGroups();
279 log.info( "allGroups: {}", allGroups );
281 Assertions.assertThat( allGroups ).isNotNull().isNotEmpty().contains( "archiva-admin",
282 "internal-repo-manager" );
286 public void getGroupsMember()
289 List<String> users = ldapRoleMapper.getGroupsMember( "archiva-admin" );
291 log.info( "users for archiva-admin: {}", users );
293 Assertions.assertThat( users ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "admin", "user.7" );
295 users = ldapRoleMapper.getGroupsMember( "internal-repo-observer" );
297 Assertions.assertThat( users ).isNotNull().isNotEmpty().hasSize( 3 ).contains( "admin", "user.7", "user.8" );
301 public void getGroups()
304 List<String> roles = ldapRoleMapper.getGroups( "admin" );
306 log.info( "roles for admin: {}", roles );
308 Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 3 ).contains( "archiva-admin",
309 "internal-repo-manager",
310 "internal-repo-observer" );
312 roles = ldapRoleMapper.getGroups( "user.8" );
314 Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "internal-repo-observer" );
316 roles = ldapRoleMapper.getGroups( "user.7" );
318 Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "archiva-admin",
319 "internal-repo-observer" );