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