1 package org.apache.archiva.web.startup;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.common.ArchivaException;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.ConfigurationNames;
25 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.archiva.security.common.ArchivaRoleConstants;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.codehaus.plexus.redback.rbac.RBACManager;
30 import org.codehaus.plexus.redback.rbac.RbacManagerException;
31 import org.codehaus.plexus.redback.rbac.UserAssignment;
32 import org.codehaus.plexus.redback.role.RoleManager;
33 import org.codehaus.plexus.redback.role.RoleManagerException;
34 import org.codehaus.plexus.redback.system.check.EnvironmentCheck;
35 import org.codehaus.plexus.redback.users.UserManager;
36 import org.codehaus.plexus.registry.Registry;
37 import org.codehaus.plexus.registry.RegistryListener;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.context.ApplicationContext;
41 import org.springframework.stereotype.Service;
43 import javax.annotation.PostConstruct;
44 import javax.inject.Inject;
45 import javax.inject.Named;
46 import java.util.ArrayList;
47 import java.util.HashMap;
48 import java.util.List;
50 import java.util.Map.Entry;
53 * ConfigurationSynchronization
58 public class SecuritySynchronization
59 implements RegistryListener
61 private Logger log = LoggerFactory.getLogger( SecuritySynchronization.class );
64 private RoleManager roleManager;
67 @Named( value = "rBACManager#cached" )
68 private RBACManager rbacManager;
70 private Map<String, EnvironmentCheck> checkers;
73 private ArchivaConfiguration archivaConfiguration;
76 private ApplicationContext applicationContext;
79 public void initialize()
81 checkers = getBeansOfType( EnvironmentCheck.class );
84 protected <T> Map<String, T> getBeansOfType( Class<T> clazz )
86 //TODO do some caching here !!!
87 // olamy : with plexus we get only roleHint
88 // as per convention we named spring bean role#hint remove role# if exists
89 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
91 Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
93 for ( Entry<String, T> entry : springBeans.entrySet() )
95 String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
96 beans.put( key, entry.getValue() );
101 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
103 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
105 synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
109 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
114 private void synchConfiguration( List<ManagedRepositoryConfiguration> repos )
116 // NOTE: Remote Repositories do not have roles or security placed around them.
118 for ( ManagedRepositoryConfiguration repoConfig : repos )
120 // manage roles for repositories
123 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
124 repoConfig.getId() ) )
126 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
127 repoConfig.getId() );
131 roleManager.verifyTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
132 repoConfig.getId() );
135 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER,
136 repoConfig.getId() ) )
138 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER,
139 repoConfig.getId() );
143 roleManager.verifyTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER,
144 repoConfig.getId() );
147 catch ( RoleManagerException e )
150 log.error( "Unable to create roles for configured repositories: " + e.getMessage(), e );
155 public void startup()
156 throws ArchivaException
158 executeEnvironmentChecks();
160 synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
161 archivaConfiguration.addChangeListener( this );
163 if ( archivaConfiguration.isDefaulted() )
165 assignRepositoryObserverToGuestUser( archivaConfiguration.getConfiguration().getManagedRepositories() );
169 private void executeEnvironmentChecks()
170 throws ArchivaException
172 if ( ( checkers == null ) || CollectionUtils.isEmpty( checkers.values() ) )
174 throw new ArchivaException(
175 "Unable to initialize the Redback Security Environment, " + "no Environment Check components found." );
178 List<String> violations = new ArrayList<String>();
180 for ( Entry<String, EnvironmentCheck> entry : checkers.entrySet() )
182 EnvironmentCheck check = entry.getValue();
183 List<String> v = new ArrayList<String>();
184 check.validateEnvironment( v );
185 log.info( "Environment Check: " + entry.getKey() + " -> " + v.size() + " violation(s)" );
188 violations.add( "[" + entry.getKey() + "] " + s );
192 if ( CollectionUtils.isNotEmpty( violations ) )
194 StringBuilder msg = new StringBuilder();
195 msg.append( "EnvironmentCheck Failure.\n" );
196 msg.append( "======================================================================\n" );
197 msg.append( " ENVIRONMENT FAILURE !! \n" );
200 for ( String violation : violations )
202 msg.append( violation ).append( "\n" );
206 msg.append( "======================================================================" );
207 log.error( msg.toString() );
209 throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size()
210 + "] violation(s) encountered, See log for details." );
214 private void assignRepositoryObserverToGuestUser( List<ManagedRepositoryConfiguration> repos )
216 for ( ManagedRepositoryConfiguration repoConfig : repos )
218 String repoId = repoConfig.getId();
220 String principal = UserManager.GUEST_USERNAME;
226 if ( rbacManager.userAssignmentExists( principal ) )
228 ua = rbacManager.getUserAssignment( principal );
232 ua = rbacManager.createUserAssignment( principal );
235 ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
236 rbacManager.saveUserAssignment( ua );
238 catch ( RbacManagerException e )
240 log.warn( "Unable to add role [" + ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) + "] to "
241 + principal + " user.", e );