1 package org.apache.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.archiva.redback.rbac.RBACManager;
26 import org.apache.archiva.redback.rbac.RbacObjectNotFoundException;
27 import org.apache.archiva.redback.role.RoleManager;
28 import org.apache.archiva.redback.users.User;
29 import org.apache.archiva.redback.users.UserManagerException;
30 import org.apache.archiva.security.common.ArchivaRoleConstants;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.archiva.configuration.ArchivaConfiguration;
33 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.archiva.redback.rbac.UserAssignment;
35 import org.apache.archiva.redback.system.SecuritySystem;
36 import org.apache.archiva.redback.users.UserManager;
37 import org.junit.Before;
38 import org.junit.runner.RunWith;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.test.context.ContextConfiguration;
43 import javax.inject.Inject;
44 import javax.inject.Named;
47 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
50 * AbstractSecurityTest
52 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
53 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" })
54 public abstract class AbstractSecurityTest
58 protected Logger log = LoggerFactory.getLogger( getClass() );
60 protected static final String USER_GUEST = "guest";
62 protected static final String USER_ADMIN = "admin";
64 protected static final String USER_ALPACA = "alpaca";
67 @Named(value = "securitySystem#testable")
68 protected SecuritySystem securitySystem;
71 @Named(value = "rBACManager#memory")
72 protected RBACManager rbacManager;
75 protected RoleManager roleManager;
78 @Named(value = "archivaConfiguration#default")
79 private ArchivaConfiguration archivaConfiguration;
82 protected UserRepositories userRepos;
84 protected void setupRepository( String repoId )
87 // Add repo to configuration.
88 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
89 repoConfig.setId( repoId );
90 repoConfig.setName( "Testable repo <" + repoId + ">" );
91 repoConfig.setLocation( new File( "./target/test-repo/" + repoId ).getPath() );
92 if ( !archivaConfiguration.getConfiguration().getManagedRepositoriesAsMap().containsKey( repoId ) )
94 archivaConfiguration.getConfiguration().addManagedRepository( repoConfig );
97 // Add repo roles to security.
98 userRepos.createMissingRepositoryRoles( repoId );
101 protected void assignRepositoryObserverRole( String principal, String repoId )
104 roleManager.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId, principal );
107 protected User createUser( String principal, String fullname )
108 throws UserManagerException
110 UserManager userManager = securitySystem.getUserManager();
112 User user = userManager.createUser( principal, fullname, principal + "@testable.archiva.apache.org" );
113 securitySystem.getPolicy().setEnabled( false );
114 userManager.addUser( user );
115 securitySystem.getPolicy().setEnabled( true );
127 File srcConfig = new File( "./src/test/resources/repository-archiva.xml" );
128 File destConfig = new File( "./target/test-conf/archiva.xml" );
130 destConfig.getParentFile().mkdirs();
133 FileUtils.copyFile( srcConfig, destConfig );
135 // Some basic asserts.
136 assertNotNull( securitySystem );
137 assertNotNull( rbacManager );
138 assertNotNull( roleManager );
139 assertNotNull( userRepos );
140 assertNotNull( archivaConfiguration );
143 User adminUser = createUser( USER_ADMIN, "Admin User" );
144 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_SYSTEM_ADMIN, adminUser.getUsername() );
147 User guestUser = createUser( USER_GUEST, "Guest User" );
148 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, guestUser.getUsername() );
151 protected void restoreGuestInitialValues( String userId )
154 UserAssignment userAssignment = null;
157 userAssignment = rbacManager.getUserAssignment( userId );
159 catch ( RbacObjectNotFoundException e )
161 log.info( "ignore RbacObjectNotFoundException for id {} during restoreGuestInitialValues", userId );
164 userAssignment.setRoleNames( Lists.newArrayList( "Guest" ) );
165 rbacManager.saveUserAssignment( userAssignment );
166 CacheManager.getInstance().clearAll();