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.redback.rbac.RBACManager;
27 import org.apache.archiva.redback.role.RoleManagerException;
28 import org.apache.archiva.redback.system.check.EnvironmentCheck;
29 import org.apache.archiva.security.common.ArchivaRoleConstants;
30 import org.apache.commons.collections.CollectionUtils;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.archiva.redback.rbac.RbacManagerException;
33 import org.apache.archiva.redback.rbac.UserAssignment;
34 import org.apache.archiva.redback.role.RoleManager;
35 import org.apache.archiva.redback.users.UserManager;
36 import org.apache.archiva.redback.components.registry.RegistryListener;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.context.ApplicationContext;
40 import org.springframework.stereotype.Service;
42 import javax.annotation.PostConstruct;
43 import javax.inject.Inject;
44 import javax.inject.Named;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.List;
49 import java.util.Map.Entry;
52 * ConfigurationSynchronization
57 public class SecuritySynchronization
58 implements RegistryListener
60 private Logger log = LoggerFactory.getLogger( SecuritySynchronization.class );
63 private RoleManager roleManager;
66 @Named( value = "rBACManager#cached" )
67 private RBACManager rbacManager;
69 private Map<String, EnvironmentCheck> checkers;
72 private ArchivaConfiguration archivaConfiguration;
75 private ApplicationContext applicationContext;
78 public void initialize()
80 checkers = getBeansOfType( EnvironmentCheck.class );
83 protected <T> Map<String, T> getBeansOfType( Class<T> clazz )
85 //TODO do some caching here !!!
86 // olamy : with plexus we get only roleHint
87 // as per convention we named spring bean role#hint remove role# if exists
88 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
90 Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
92 for ( Entry<String, T> entry : springBeans.entrySet() )
94 String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
95 beans.put( key, entry.getValue() );
100 public void afterConfigurationChange( org.apache.archiva.redback.components.registry.Registry registry, String propertyName, Object propertyValue )
102 if ( ConfigurationNames.isManagedRepositories( propertyName ) && propertyName.endsWith( ".id" ) )
104 if ( propertyValue != null )
106 syncRepoConfiguration( (String) propertyValue );
111 public void beforeConfigurationChange( org.apache.archiva.redback.components.registry.Registry registry, String propertyName, Object propertyValue )
116 private void synchConfiguration( List<ManagedRepositoryConfiguration> repos )
118 // NOTE: Remote Repositories do not have roles or security placed around them.
120 for ( ManagedRepositoryConfiguration repoConfig : repos )
122 syncRepoConfiguration( repoConfig.getId() );
126 private void syncRepoConfiguration( String id )
128 // manage roles for repositories
131 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id ) )
133 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id );
137 roleManager.verifyTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id );
140 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, id ) )
142 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, id );
146 roleManager.verifyTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, id );
149 catch ( RoleManagerException e )
152 log.error( "Unable to create roles for configured repositories: " + e.getMessage(), e );
156 public void startup()
157 throws ArchivaException
159 executeEnvironmentChecks();
161 synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
162 archivaConfiguration.addChangeListener( this );
164 if ( archivaConfiguration.isDefaulted() )
166 assignRepositoryObserverToGuestUser( archivaConfiguration.getConfiguration().getManagedRepositories() );
170 private void executeEnvironmentChecks()
171 throws ArchivaException
173 if ( ( checkers == null ) || CollectionUtils.isEmpty( checkers.values() ) )
175 throw new ArchivaException(
176 "Unable to initialize the Redback Security Environment, " + "no Environment Check components found." );
179 List<String> violations = new ArrayList<String>();
181 for ( Entry<String, EnvironmentCheck> entry : checkers.entrySet() )
183 EnvironmentCheck check = entry.getValue();
184 List<String> v = new ArrayList<String>();
185 check.validateEnvironment( v );
186 log.info( "Environment Check: " + entry.getKey() + " -> " + v.size() + " violation(s)" );
189 violations.add( "[" + entry.getKey() + "] " + s );
193 if ( CollectionUtils.isNotEmpty( violations ) )
195 StringBuilder msg = new StringBuilder();
196 msg.append( "EnvironmentCheck Failure.\n" );
197 msg.append( "======================================================================\n" );
198 msg.append( " ENVIRONMENT FAILURE !! \n" );
201 for ( String violation : violations )
203 msg.append( violation ).append( "\n" );
207 msg.append( "======================================================================" );
208 log.error( msg.toString() );
210 throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size()
211 + "] violation(s) encountered, See log for details." );
215 private void assignRepositoryObserverToGuestUser( List<ManagedRepositoryConfiguration> repos )
217 for ( ManagedRepositoryConfiguration repoConfig : repos )
219 String repoId = repoConfig.getId();
221 String principal = UserManager.GUEST_USERNAME;
227 if ( rbacManager.userAssignmentExists( principal ) )
229 ua = rbacManager.getUserAssignment( principal );
233 ua = rbacManager.createUserAssignment( principal );
236 ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
237 rbacManager.saveUserAssignment( ua );
239 catch ( RbacManagerException e )
241 log.warn( "Unable to add role [" + ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) + "] to "
242 + principal + " user.", e );