1 package org.apache.maven.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 java.util.ArrayList;
23 import java.util.List;
25 import java.util.Map.Entry;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.maven.archiva.common.ArchivaException;
29 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
30 import org.apache.maven.archiva.configuration.ConfigurationNames;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.security.ArchivaRoleConstants;
33 import org.apache.maven.archiva.security.ArchivaXworkUser;
34 import org.codehaus.plexus.redback.rbac.RBACManager;
35 import org.codehaus.plexus.redback.rbac.RbacManagerException;
36 import org.codehaus.plexus.redback.rbac.UserAssignment;
37 import org.codehaus.plexus.redback.role.RoleManager;
38 import org.codehaus.plexus.redback.role.RoleManagerException;
39 import org.codehaus.plexus.redback.system.check.EnvironmentCheck;
40 import org.codehaus.plexus.registry.Registry;
41 import org.codehaus.plexus.registry.RegistryListener;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * ConfigurationSynchronization
49 * @plexus.component role="org.apache.maven.archiva.web.startup.SecuritySynchronization"
52 public class SecuritySynchronization
53 implements RegistryListener
55 private Logger log = LoggerFactory.getLogger( SecuritySynchronization.class );
58 * @plexus.requirement role-hint="default"
60 private RoleManager roleManager;
63 * @plexus.requirement role-hint="cached"
65 private RBACManager rbacManager;
68 * @plexus.requirement role="org.codehaus.plexus.redback.system.check.EnvironmentCheck"
70 private Map<String, EnvironmentCheck> checkers;
75 private ArchivaConfiguration archivaConfiguration;
80 private ArchivaXworkUser archivaXworkUser;
82 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
84 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
86 synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
90 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
95 private void synchConfiguration( List<ManagedRepositoryConfiguration> repos )
97 // NOTE: Remote Repositories do not have roles or security placed around them.
99 for ( ManagedRepositoryConfiguration repoConfig : repos )
101 // manage roles for repositories
104 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
105 repoConfig.getId() ) )
107 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
108 repoConfig.getId() );
111 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER,
112 repoConfig.getId() ) )
114 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER,
115 repoConfig.getId() );
118 catch ( RoleManagerException e )
121 log.error( "Unable to create roles for configured repositories: " + e.getMessage(), e );
126 public void startup()
127 throws ArchivaException
129 executeEnvironmentChecks();
131 synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
132 archivaConfiguration.addChangeListener( this );
134 if ( archivaConfiguration.isDefaulted() )
136 assignRepositoryObserverToGuestUser( archivaConfiguration.getConfiguration().getManagedRepositories() );
140 private void executeEnvironmentChecks()
141 throws ArchivaException
143 if ( ( checkers == null ) || CollectionUtils.isEmpty( checkers.values() ) )
145 throw new ArchivaException(
146 "Unable to initialize the Redback Security Environment, " + "no Environment Check components found." );
149 List<String> violations = new ArrayList<String>();
151 for ( Entry<String, EnvironmentCheck> entry : checkers.entrySet() )
153 EnvironmentCheck check = entry.getValue();
154 List<String> v = new ArrayList<String>();
155 check.validateEnvironment( v );
156 log.info( "Environment Check: " + entry.getKey() + " -> " + v.size() + " violation(s)" );
159 violations.add( "[" + entry.getKey() + "] " + s );
163 if ( CollectionUtils.isNotEmpty( violations ) )
165 StringBuffer msg = new StringBuffer();
166 msg.append( "EnvironmentCheck Failure.\n" );
167 msg.append( "======================================================================\n" );
168 msg.append( " ENVIRONMENT FAILURE !! \n" );
171 for ( String violation : violations )
173 msg.append( violation ).append( "\n" );
177 msg.append( "======================================================================" );
178 log.error( msg.toString() );
180 throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size() +
181 "] violation(s) encountered, See log for details." );
185 private void assignRepositoryObserverToGuestUser( List<ManagedRepositoryConfiguration> repos )
187 for ( ManagedRepositoryConfiguration repoConfig : repos )
189 String repoId = repoConfig.getId();
191 String principal = archivaXworkUser.getGuest();
197 if ( rbacManager.userAssignmentExists( principal ) )
199 ua = rbacManager.getUserAssignment( principal );
203 ua = rbacManager.createUserAssignment( principal );
206 ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
207 rbacManager.saveUserAssignment( ua );
209 catch ( RbacManagerException e )
212 "Unable to add role [" + ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) + "] to " +
213 principal + " user.", e );