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.redback.users.UserManager;
41 import org.codehaus.plexus.registry.Registry;
42 import org.codehaus.plexus.registry.RegistryListener;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * ConfigurationSynchronization
50 * @plexus.component role="org.apache.maven.archiva.web.startup.SecuritySynchronization"
53 public class SecuritySynchronization
54 implements RegistryListener
56 private Logger log = LoggerFactory.getLogger( SecuritySynchronization.class );
59 * @plexus.requirement role-hint="default"
61 private RoleManager roleManager;
64 * @plexus.requirement role-hint="cached"
66 private RBACManager rbacManager;
69 * @plexus.requirement role="org.codehaus.plexus.redback.system.check.EnvironmentCheck"
71 private Map<String, EnvironmentCheck> checkers;
76 private ArchivaConfiguration archivaConfiguration;
81 private ArchivaXworkUser archivaXworkUser;
83 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
85 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
87 synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
91 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
96 private void synchConfiguration( List<ManagedRepositoryConfiguration> repos )
98 // NOTE: Remote Repositories do not have roles or security placed around them.
100 for ( ManagedRepositoryConfiguration repoConfig : repos )
102 // manage roles for repositories
105 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
106 repoConfig.getId() ) )
108 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
109 repoConfig.getId() );
112 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER,
113 repoConfig.getId() ) )
115 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER,
116 repoConfig.getId() );
119 catch ( RoleManagerException e )
122 log.error( "Unable to create roles for configured repositories: " + e.getMessage(), e );
127 public void startup()
128 throws ArchivaException
130 executeEnvironmentChecks();
132 synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
133 archivaConfiguration.addChangeListener( this );
135 if ( archivaConfiguration.isDefaulted() )
137 assignRepositoryObserverToGuestUser( archivaConfiguration.getConfiguration().getManagedRepositories() );
141 private void executeEnvironmentChecks()
142 throws ArchivaException
144 if ( ( checkers == null ) || CollectionUtils.isEmpty( checkers.values() ) )
146 throw new ArchivaException(
147 "Unable to initialize the Redback Security Environment, " + "no Environment Check components found." );
150 List<String> violations = new ArrayList<String>();
152 for ( Entry<String, EnvironmentCheck> entry : checkers.entrySet() )
154 EnvironmentCheck check = entry.getValue();
155 List<String> v = new ArrayList<String>();
156 check.validateEnvironment( v );
157 log.info( "Environment Check: " + entry.getKey() + " -> " + v.size() + " violation(s)" );
160 violations.add( "[" + entry.getKey() + "] " + s );
164 if ( CollectionUtils.isNotEmpty( violations ) )
166 StringBuffer msg = new StringBuffer();
167 msg.append( "EnvironmentCheck Failure.\n" );
168 msg.append( "======================================================================\n" );
169 msg.append( " ENVIRONMENT FAILURE !! \n" );
172 for ( String violation : violations )
174 msg.append( violation ).append( "\n" );
178 msg.append( "======================================================================" );
179 log.error( msg.toString() );
181 throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size() +
182 "] violation(s) encountered, See log for details." );
186 private void assignRepositoryObserverToGuestUser( List<ManagedRepositoryConfiguration> repos )
188 for ( ManagedRepositoryConfiguration repoConfig : repos )
190 String repoId = repoConfig.getId();
192 String principal = UserManager.GUEST_USERNAME;
198 if ( rbacManager.userAssignmentExists( principal ) )
200 ua = rbacManager.getUserAssignment( principal );
204 ua = rbacManager.createUserAssignment( principal );
207 ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
208 rbacManager.saveUserAssignment( ua );
210 catch ( RbacManagerException e )
213 "Unable to add role [" + ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) + "] to " +
214 principal + " user.", e );