]> source.dussan.org Git - archiva.git/blob
84d6b8c51938fd8f05bb393e5b2b6da027b9ae51
[archiva.git] /
1 package org.apache.maven.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.commons.io.FileUtils;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.codehaus.plexus.redback.rbac.RBACManager;
29 import org.codehaus.plexus.redback.rbac.RbacObjectNotFoundException;
30 import org.codehaus.plexus.redback.rbac.UserAssignment;
31 import org.codehaus.plexus.redback.role.RoleManager;
32 import org.codehaus.plexus.redback.system.SecuritySystem;
33 import org.codehaus.plexus.redback.users.User;
34 import org.codehaus.plexus.redback.users.UserManager;
35 import org.junit.Before;
36 import org.junit.runner.RunWith;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.test.context.ContextConfiguration;
40 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
41
42 import javax.inject.Inject;
43 import javax.inject.Named;
44 import java.io.File;
45
46 /**
47  * AbstractSecurityTest
48  *
49  * @version $Id: AbstractSecurityTest
50  */
51 @RunWith( SpringJUnit4ClassRunner.class )
52 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
53 public abstract class AbstractSecurityTest
54     extends TestCase
55 {
56
57     protected Logger log = LoggerFactory.getLogger( getClass() );
58
59     protected static final String USER_GUEST = "guest";
60
61     protected static final String USER_ADMIN = "admin";
62
63     protected static final String USER_ALPACA = "alpaca";
64
65     @Inject
66     @Named( value = "securitySystem#testable" )
67     protected SecuritySystem securitySystem;
68
69     @Inject
70     @Named( value = "rBACManager#memory" )
71     protected RBACManager rbacManager;
72
73     @Inject
74     protected RoleManager roleManager;
75
76     @Inject
77     private ArchivaConfiguration archivaConfiguration;
78
79     @Inject
80     protected UserRepositories userRepos;
81
82     protected void setupRepository( String repoId )
83         throws Exception
84     {
85         // Add repo to configuration.
86         ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
87         repoConfig.setId( repoId );
88         repoConfig.setName( "Testable repo <" + repoId + ">" );
89         repoConfig.setLocation( new File( "./target/test-repo/" + repoId ).getPath() );
90         archivaConfiguration.getConfiguration().addManagedRepository( repoConfig );
91
92         // Add repo roles to security.
93         userRepos.createMissingRepositoryRoles( repoId );
94     }
95
96     protected void assignRepositoryObserverRole( String principal, String repoId )
97         throws Exception
98     {
99         roleManager.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId, principal );
100     }
101
102     protected User createUser( String principal, String fullname )
103     {
104         UserManager userManager = securitySystem.getUserManager();
105
106         User user = userManager.createUser( principal, fullname, principal + "@testable.archiva.apache.org" );
107         securitySystem.getPolicy().setEnabled( false );
108         userManager.addUser( user );
109         securitySystem.getPolicy().setEnabled( true );
110
111         return user;
112     }
113
114     @Override
115     @Before
116     public void setUp()
117         throws Exception
118     {
119         super.setUp();
120
121         File srcConfig = new File( "./src/test/resources/repository-archiva.xml" );
122         File destConfig = new File( "./target/test-conf/archiva.xml" );
123
124         destConfig.getParentFile().mkdirs();
125         destConfig.delete();
126
127         FileUtils.copyFile( srcConfig, destConfig );
128
129         // Some basic asserts.
130         assertNotNull( securitySystem );
131         assertNotNull( rbacManager );
132         assertNotNull( roleManager );
133         assertNotNull( userRepos );
134         assertNotNull( archivaConfiguration );
135
136         // Setup Admin User.
137         User adminUser = createUser( USER_ADMIN, "Admin User" );
138         roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_SYSTEM_ADMIN, adminUser.getPrincipal().toString() );
139
140         // Setup Guest User.
141         User guestUser = createUser( USER_GUEST, "Guest User" );
142         roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, guestUser.getPrincipal().toString() );
143     }
144
145     protected void restoreGuestInitialValues( String userId )
146         throws Exception
147     {
148         UserAssignment userAssignment = null;
149         try
150         {
151             userAssignment = rbacManager.getUserAssignment( userId );
152         }
153         catch ( RbacObjectNotFoundException e )
154         {
155             log.info( "ignore RbacObjectNotFoundException for id {} during restoreGuestInitialValues", userId );
156             return;
157         }
158         userAssignment.setRoleNames( Lists.newArrayList( "Guest" ) );
159         rbacManager.saveUserAssignment( userAssignment );
160         CacheManager.getInstance().clearAll();
161     }
162 }