]> source.dussan.org Git - archiva.git/blob
ca9b6d95e6ca4e1a2905e9ca618960014b6f745e
[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.security.common.ArchivaRoleConstants;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.archiva.configuration.ArchivaConfiguration;
32 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.archiva.redback.rbac.UserAssignment;
34 import org.apache.archiva.redback.system.SecuritySystem;
35 import org.apache.archiva.redback.users.UserManager;
36 import org.junit.Before;
37 import org.junit.runner.RunWith;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.test.context.ContextConfiguration;
41
42 import javax.inject.Inject;
43 import javax.inject.Named;
44 import java.io.File;
45 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
46
47 /**
48  * AbstractSecurityTest
49  *
50  * @version $Id: 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     {
109         UserManager userManager = securitySystem.getUserManager();
110
111         User user = userManager.createUser( principal, fullname, principal + "@testable.archiva.apache.org" );
112         securitySystem.getPolicy().setEnabled( false );
113         userManager.addUser( user );
114         securitySystem.getPolicy().setEnabled( true );
115
116         return user;
117     }
118
119     @Override
120     @Before
121     public void setUp()
122         throws Exception
123     {
124         super.setUp();
125
126         File srcConfig = new File( "./src/test/resources/repository-archiva.xml" );
127         File destConfig = new File( "./target/test-conf/archiva.xml" );
128
129         destConfig.getParentFile().mkdirs();
130         destConfig.delete();
131
132         FileUtils.copyFile( srcConfig, destConfig );
133
134         // Some basic asserts.
135         assertNotNull( securitySystem );
136         assertNotNull( rbacManager );
137         assertNotNull( roleManager );
138         assertNotNull( userRepos );
139         assertNotNull( archivaConfiguration );
140
141         // Setup Admin User.
142         User adminUser = createUser( USER_ADMIN, "Admin User" );
143         roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_SYSTEM_ADMIN, adminUser.getPrincipal().toString() );
144
145         // Setup Guest User.
146         User guestUser = createUser( USER_GUEST, "Guest User" );
147         roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, guestUser.getPrincipal().toString() );
148     }
149
150     protected void restoreGuestInitialValues( String userId )
151         throws Exception
152     {
153         UserAssignment userAssignment = null;
154         try
155         {
156             userAssignment = rbacManager.getUserAssignment( userId );
157         }
158         catch ( RbacObjectNotFoundException e )
159         {
160             log.info( "ignore RbacObjectNotFoundException for id {} during restoreGuestInitialValues", userId );
161             return;
162         }
163         userAssignment.setRoleNames( Lists.newArrayList( "Guest" ) );
164         rbacManager.saveUserAssignment( userAssignment );
165         CacheManager.getInstance().clearAll();
166     }
167 }