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.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.archiva.redback.rbac.RBACManager;
28 import org.apache.archiva.redback.rbac.RbacObjectNotFoundException;
29 import org.apache.archiva.redback.rbac.UserAssignment;
30 import org.apache.archiva.redback.role.RoleManager;
31 import org.apache.archiva.redback.system.SecuritySystem;
32 import org.apache.archiva.redback.users.User;
33 import org.apache.archiva.redback.users.UserManager;
34 import org.apache.archiva.redback.users.UserManagerException;
35 import org.apache.archiva.security.common.ArchivaRoleConstants;
36 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
37 import org.apache.commons.io.FileUtils;
38 import org.junit.Before;
39 import org.junit.runner.RunWith;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.test.context.ContextConfiguration;
44 import javax.inject.Inject;
45 import javax.inject.Named;
46 import java.nio.file.Files;
47 import java.nio.file.Path;
48 import java.nio.file.Paths;
51 * AbstractSecurityTest
53 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
54 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" })
55 public abstract class AbstractSecurityTest
59 protected Logger log = LoggerFactory.getLogger( getClass() );
61 protected static final String USER_GUEST = "guest";
63 protected static final String USER_ADMIN = "admin";
65 protected static final String USER_ALPACA = "alpaca";
68 @Named(value = "securitySystem#testable")
69 protected SecuritySystem securitySystem;
72 @Named(value = "rbacManager#memory")
73 protected RBACManager rbacManager;
76 protected RoleManager roleManager;
79 @Named(value = "archivaConfiguration#default")
80 private ArchivaConfiguration archivaConfiguration;
83 protected UserRepositories userRepos;
85 protected void setupRepository( String repoId )
88 // Add repo to configuration.
89 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
90 repoConfig.setId( repoId );
91 repoConfig.setName( "Testable repo <" + repoId + ">" );
92 repoConfig.setLocation( Paths.get( "target/test-repo/" + repoId ).toString() );
93 if ( !archivaConfiguration.getConfiguration().getManagedRepositoriesAsMap().containsKey( repoId ) )
95 archivaConfiguration.getConfiguration().addManagedRepository( repoConfig );
98 // Add repo roles to security.
99 userRepos.createMissingRepositoryRoles( repoId );
102 protected void assignRepositoryObserverRole( String principal, String repoId )
105 roleManager.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId, principal );
108 protected User createUser( String principal, String fullname )
109 throws UserManagerException
111 UserManager userManager = securitySystem.getUserManager();
113 User user = userManager.createUser( principal, fullname, principal + "@testable.archiva.apache.org" );
114 securitySystem.getPolicy().setEnabled( false );
115 userManager.addUser( user );
116 securitySystem.getPolicy().setEnabled( true );
128 Path srcConfig = Paths.get( "src/test/resources/repository-archiva.xml" );
129 Path destConfig = Paths.get( "target/test-conf/archiva.xml" );
131 Files.createDirectories(destConfig.getParent());
132 Files.deleteIfExists(destConfig);
134 FileUtils.copyFile( srcConfig.toFile(), destConfig.toFile() );
136 // Some basic asserts.
137 assertNotNull( securitySystem );
138 assertNotNull( rbacManager );
139 assertNotNull( roleManager );
140 assertNotNull( userRepos );
141 assertNotNull( archivaConfiguration );
144 User adminUser = createUser( USER_ADMIN, "Admin User" );
145 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_SYSTEM_ADMIN, adminUser.getUsername() );
148 User guestUser = createUser( USER_GUEST, "Guest User" );
149 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, guestUser.getUsername() );
152 protected void restoreGuestInitialValues( String userId )
155 UserAssignment userAssignment = null;
158 userAssignment = rbacManager.getUserAssignment( userId );
160 catch ( RbacObjectNotFoundException e )
162 log.info( "ignore RbacObjectNotFoundException for id {} during restoreGuestInitialValues", userId );
165 userAssignment.setRoleNames( Lists.newArrayList( "Guest" ) );
166 rbacManager.saveUserAssignment( userAssignment );
167 CacheManager.getInstance().clearAll();