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 junit.framework.TestCase;
23 import net.sf.ehcache.CacheManager;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.archiva.redback.rbac.RBACManager;
27 import org.apache.archiva.redback.rbac.RbacObjectNotFoundException;
28 import org.apache.archiva.redback.rbac.UserAssignment;
29 import org.apache.archiva.redback.role.RoleManager;
30 import org.apache.archiva.redback.system.SecuritySystem;
31 import org.apache.archiva.redback.users.User;
32 import org.apache.archiva.redback.users.UserManager;
33 import org.apache.archiva.redback.users.UserManagerException;
34 import org.apache.archiva.repository.RepositoryRegistry;
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;
49 import java.util.ArrayList;
50 import java.util.Arrays;
53 * AbstractSecurityTest
55 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
56 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" })
57 public abstract class AbstractSecurityTest
61 protected Logger log = LoggerFactory.getLogger( getClass() );
63 protected static final String USER_GUEST = "guest";
65 protected static final String USER_ADMIN = "admin";
67 protected static final String USER_ALPACA = "alpaca";
70 @Named(value = "securitySystem#testable")
71 protected SecuritySystem securitySystem;
74 @Named(value = "rbacManager#memory")
75 protected RBACManager rbacManager;
78 protected RoleManager roleManager;
81 @Named(value = "archivaConfiguration#default")
82 private ArchivaConfiguration archivaConfiguration;
85 private RepositoryRegistry repositoryRegistry;
88 protected UserRepositories userRepos;
90 protected void setupRepository( String repoId )
93 // Add repo to configuration.
94 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
95 repoConfig.setId( repoId );
96 repoConfig.setName( "Testable repo <" + repoId + ">" );
97 repoConfig.setLocation( Paths.get( "target/test-repo/" + repoId ).toString() );
98 if ( !archivaConfiguration.getConfiguration().getManagedRepositoriesAsMap().containsKey( repoId ) )
100 repositoryRegistry.putRepository( repoConfig );
103 // Add repo roles to security.
104 userRepos.createMissingRepositoryRoles( repoId );
107 protected void assignRepositoryObserverRole( String principal, String repoId )
110 roleManager.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId, principal );
113 protected User createUser( String principal, String fullname )
114 throws UserManagerException
116 UserManager userManager = securitySystem.getUserManager();
118 User user = userManager.createUser( principal, fullname, principal + "@testable.archiva.apache.org" );
119 securitySystem.getPolicy().setEnabled( false );
120 userManager.addUser( user );
121 securitySystem.getPolicy().setEnabled( true );
133 Path srcConfig = Paths.get( "src/test/resources/repository-archiva.xml" );
134 Path destConfig = Paths.get( "target/test-conf/archiva.xml" );
136 Files.createDirectories(destConfig.getParent());
137 Files.deleteIfExists(destConfig);
139 FileUtils.copyFile( srcConfig.toFile(), destConfig.toFile() );
141 // Some basic asserts.
142 assertNotNull( securitySystem );
143 assertNotNull( rbacManager );
144 assertNotNull( roleManager );
145 assertNotNull( userRepos );
146 assertNotNull( archivaConfiguration );
149 User adminUser = createUser( USER_ADMIN, "Admin User" );
150 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_SYSTEM_ADMIN, adminUser.getUsername() );
153 User guestUser = createUser( USER_GUEST, "Guest User" );
154 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, guestUser.getUsername() );
156 repositoryRegistry.setArchivaConfiguration(archivaConfiguration);
157 repositoryRegistry.reload();
160 protected void restoreGuestInitialValues( String userId )
163 UserAssignment userAssignment = null;
166 userAssignment = rbacManager.getUserAssignment( userId );
168 catch ( RbacObjectNotFoundException e )
170 log.info( "ignore RbacObjectNotFoundException for id {} during restoreGuestInitialValues", userId );
173 userAssignment.setRoleNames( new ArrayList( Arrays.asList( "Guest" ) ) );
174 rbacManager.saveUserAssignment( userAssignment );
175 CacheManager.getInstance().clearAll();