]> source.dussan.org Git - archiva.git/blob
6487e9d23f272b7c3201c9296e3e4dd8bc8d170d
[archiva.git] /
1 package org.apache.archiva.security;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
43
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;
51
52 /**
53  * AbstractSecurityTest
54  */
55 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
56 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" })
57 public abstract class AbstractSecurityTest
58     extends TestCase
59 {
60
61     protected Logger log = LoggerFactory.getLogger( getClass() );
62
63     protected static final String USER_GUEST = "guest";
64
65     protected static final String USER_ADMIN = "admin";
66
67     protected static final String USER_ALPACA = "alpaca";
68
69     @Inject
70     @Named(value = "securitySystem#testable")
71     protected SecuritySystem securitySystem;
72
73     @Inject
74     @Named(value = "rbacManager#memory")
75     protected RBACManager rbacManager;
76
77     @Inject
78     protected RoleManager roleManager;
79
80     @Inject
81     @Named(value = "archivaConfiguration#default")
82     private ArchivaConfiguration archivaConfiguration;
83
84     @Inject
85     private RepositoryRegistry repositoryRegistry;
86
87     @Inject
88     protected UserRepositories userRepos;
89
90     protected void setupRepository( String repoId )
91         throws Exception
92     {
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 ) )
99         {
100             repositoryRegistry.putRepository( repoConfig );
101         }
102
103         // Add repo roles to security.
104         userRepos.createMissingRepositoryRoles( repoId );
105     }
106
107     protected void assignRepositoryObserverRole( String principal, String repoId )
108         throws Exception
109     {
110         roleManager.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId, principal );
111     }
112
113     protected User createUser( String principal, String fullname )
114         throws UserManagerException
115     {
116         UserManager userManager = securitySystem.getUserManager();
117
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 );
122
123         return user;
124     }
125
126     @Override
127     @Before
128     public void setUp()
129         throws Exception
130     {
131         super.setUp();
132
133         Path srcConfig = Paths.get( "src/test/resources/repository-archiva.xml" );
134         Path destConfig = Paths.get( "target/test-conf/archiva.xml" );
135
136         Files.createDirectories(destConfig.getParent());
137         Files.deleteIfExists(destConfig);
138
139         FileUtils.copyFile( srcConfig.toFile(), destConfig.toFile() );
140
141         // Some basic asserts.
142         assertNotNull( securitySystem );
143         assertNotNull( rbacManager );
144         assertNotNull( roleManager );
145         assertNotNull( userRepos );
146         assertNotNull( archivaConfiguration );
147
148         // Setup Admin User.
149         User adminUser = createUser( USER_ADMIN, "Admin User" );
150         roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_SYSTEM_ADMIN, adminUser.getUsername() );
151
152         // Setup Guest User.
153         User guestUser = createUser( USER_GUEST, "Guest User" );
154         roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, guestUser.getUsername() );
155
156         repositoryRegistry.setArchivaConfiguration(archivaConfiguration);
157         repositoryRegistry.reload();
158     }
159
160     protected void restoreGuestInitialValues( String userId )
161         throws Exception
162     {
163         UserAssignment userAssignment = null;
164         try
165         {
166             userAssignment = rbacManager.getUserAssignment( userId );
167         }
168         catch ( RbacObjectNotFoundException e )
169         {
170             log.info( "ignore RbacObjectNotFoundException for id {} during restoreGuestInitialValues", userId );
171             return;
172         }
173         userAssignment.setRoleNames( new ArrayList( Arrays.asList( "Guest" ) ) );
174         rbacManager.saveUserAssignment( userAssignment );
175         CacheManager.getInstance().clearAll();
176     }
177 }