1 package org.apache.maven.archiva.security;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.google.common.collect.Lists;
23 import junit.framework.TestCase;
24 import net.sf.ehcache.CacheManager;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.codehaus.plexus.redback.rbac.RBACManager;
29 import org.codehaus.plexus.redback.rbac.RbacObjectNotFoundException;
30 import org.codehaus.plexus.redback.rbac.UserAssignment;
31 import org.codehaus.plexus.redback.role.RoleManager;
32 import org.codehaus.plexus.redback.system.SecuritySystem;
33 import org.codehaus.plexus.redback.users.User;
34 import org.codehaus.plexus.redback.users.UserManager;
35 import org.junit.Before;
36 import org.junit.runner.RunWith;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.test.context.ContextConfiguration;
40 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
42 import javax.inject.Inject;
43 import javax.inject.Named;
47 * AbstractSecurityTest
49 * @version $Id: AbstractSecurityTest
51 @RunWith( SpringJUnit4ClassRunner.class )
52 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
53 public abstract class AbstractSecurityTest
57 protected Logger log = LoggerFactory.getLogger( getClass() );
59 protected static final String USER_GUEST = "guest";
61 protected static final String USER_ADMIN = "admin";
63 protected static final String USER_ALPACA = "alpaca";
66 @Named( value = "securitySystem#testable" )
67 protected SecuritySystem securitySystem;
70 @Named( value = "rBACManager#memory" )
71 protected RBACManager rbacManager;
74 protected RoleManager roleManager;
77 private ArchivaConfiguration archivaConfiguration;
80 protected UserRepositories userRepos;
82 protected void setupRepository( String repoId )
85 // Add repo to configuration.
86 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
87 repoConfig.setId( repoId );
88 repoConfig.setName( "Testable repo <" + repoId + ">" );
89 repoConfig.setLocation( new File( "./target/test-repo/" + repoId ).getPath() );
90 archivaConfiguration.getConfiguration().addManagedRepository( repoConfig );
92 // Add repo roles to security.
93 userRepos.createMissingRepositoryRoles( repoId );
96 protected void assignRepositoryObserverRole( String principal, String repoId )
99 roleManager.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId, principal );
102 protected User createUser( String principal, String fullname )
104 UserManager userManager = securitySystem.getUserManager();
106 User user = userManager.createUser( principal, fullname, principal + "@testable.archiva.apache.org" );
107 securitySystem.getPolicy().setEnabled( false );
108 userManager.addUser( user );
109 securitySystem.getPolicy().setEnabled( true );
121 File srcConfig = new File( "./src/test/resources/repository-archiva.xml" );
122 File destConfig = new File( "./target/test-conf/archiva.xml" );
124 destConfig.getParentFile().mkdirs();
127 FileUtils.copyFile( srcConfig, destConfig );
129 // Some basic asserts.
130 assertNotNull( securitySystem );
131 assertNotNull( rbacManager );
132 assertNotNull( roleManager );
133 assertNotNull( userRepos );
134 assertNotNull( archivaConfiguration );
137 User adminUser = createUser( USER_ADMIN, "Admin User" );
138 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_SYSTEM_ADMIN, adminUser.getPrincipal().toString() );
141 User guestUser = createUser( USER_GUEST, "Guest User" );
142 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, guestUser.getPrincipal().toString() );
145 protected void restoreGuestInitialValues( String userId )
148 UserAssignment userAssignment = null;
151 userAssignment = rbacManager.getUserAssignment( userId );
153 catch ( RbacObjectNotFoundException e )
155 log.info( "ignore RbacObjectNotFoundException for id {} during restoreGuestInitialValues", userId );
158 userAssignment.setRoleNames( Lists.newArrayList( "Guest" ) );
159 rbacManager.saveUserAssignment( userAssignment );
160 CacheManager.getInstance().clearAll();