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