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.security.common.ArchivaRoleConstants;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.archiva.redback.rbac.RbacManagerException;
31 import org.apache.archiva.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.apache.archiva.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 ) && propertyName.endsWith( ".id" ) )
105 if ( propertyValue != null )
107 syncRepoConfiguration( (String) propertyValue );
112 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
117 private void synchConfiguration( List<ManagedRepositoryConfiguration> repos )
119 // NOTE: Remote Repositories do not have roles or security placed around them.
121 for ( ManagedRepositoryConfiguration repoConfig : repos )
123 syncRepoConfiguration( repoConfig.getId() );
127 private void syncRepoConfiguration( String id )
129 // manage roles for repositories
132 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id ) )
134 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id );
138 roleManager.verifyTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id );
141 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, id ) )
143 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, id );
147 roleManager.verifyTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, id );
150 catch ( RoleManagerException e )
153 log.error( "Unable to create roles for configured repositories: " + e.getMessage(), e );
157 public void startup()
158 throws ArchivaException
160 executeEnvironmentChecks();
162 synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
163 archivaConfiguration.addChangeListener( this );
165 if ( archivaConfiguration.isDefaulted() )
167 assignRepositoryObserverToGuestUser( archivaConfiguration.getConfiguration().getManagedRepositories() );
171 private void executeEnvironmentChecks()
172 throws ArchivaException
174 if ( ( checkers == null ) || CollectionUtils.isEmpty( checkers.values() ) )
176 throw new ArchivaException(
177 "Unable to initialize the Redback Security Environment, " + "no Environment Check components found." );
180 List<String> violations = new ArrayList<String>();
182 for ( Entry<String, EnvironmentCheck> entry : checkers.entrySet() )
184 EnvironmentCheck check = entry.getValue();
185 List<String> v = new ArrayList<String>();
186 check.validateEnvironment( v );
187 log.info( "Environment Check: " + entry.getKey() + " -> " + v.size() + " violation(s)" );
190 violations.add( "[" + entry.getKey() + "] " + s );
194 if ( CollectionUtils.isNotEmpty( violations ) )
196 StringBuilder msg = new StringBuilder();
197 msg.append( "EnvironmentCheck Failure.\n" );
198 msg.append( "======================================================================\n" );
199 msg.append( " ENVIRONMENT FAILURE !! \n" );
202 for ( String violation : violations )
204 msg.append( violation ).append( "\n" );
208 msg.append( "======================================================================" );
209 log.error( msg.toString() );
211 throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size()
212 + "] violation(s) encountered, See log for details." );
216 private void assignRepositoryObserverToGuestUser( List<ManagedRepositoryConfiguration> repos )
218 for ( ManagedRepositoryConfiguration repoConfig : repos )
220 String repoId = repoConfig.getId();
222 String principal = UserManager.GUEST_USERNAME;
228 if ( rbacManager.userAssignmentExists( principal ) )
230 ua = rbacManager.getUserAssignment( principal );
234 ua = rbacManager.createUserAssignment( principal );
237 ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
238 rbacManager.saveUserAssignment( ua );
240 catch ( RbacManagerException e )
242 log.warn( "Unable to add role [" + ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) + "] to "
243 + principal + " user.", e );