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.codehaus.plexus.redback.rbac.RBACManager;
34 import org.codehaus.plexus.redback.rbac.RbacManagerException;
35 import org.codehaus.plexus.redback.rbac.UserAssignment;
36 import org.codehaus.plexus.redback.role.RoleManager;
37 import org.codehaus.plexus.redback.role.RoleManagerException;
38 import org.codehaus.plexus.redback.system.check.EnvironmentCheck;
39 import org.codehaus.plexus.redback.users.UserManager;
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;
77 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
79 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
81 synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
85 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
90 private void synchConfiguration( List<ManagedRepositoryConfiguration> repos )
92 // NOTE: Remote Repositories do not have roles or security placed around them.
94 for ( ManagedRepositoryConfiguration repoConfig : repos )
96 // manage roles for repositories
99 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
100 repoConfig.getId() ) )
102 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
103 repoConfig.getId() );
106 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER,
107 repoConfig.getId() ) )
109 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER,
110 repoConfig.getId() );
113 catch ( RoleManagerException e )
116 log.error( "Unable to create roles for configured repositories: " + e.getMessage(), e );
121 public void startup()
122 throws ArchivaException
124 executeEnvironmentChecks();
126 synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
127 archivaConfiguration.addChangeListener( this );
129 if ( archivaConfiguration.isDefaulted() )
131 assignRepositoryObserverToGuestUser( archivaConfiguration.getConfiguration().getManagedRepositories() );
135 private void executeEnvironmentChecks()
136 throws ArchivaException
138 if ( ( checkers == null ) || CollectionUtils.isEmpty( checkers.values() ) )
140 throw new ArchivaException(
141 "Unable to initialize the Redback Security Environment, " + "no Environment Check components found." );
144 List<String> violations = new ArrayList<String>();
146 for ( Entry<String, EnvironmentCheck> entry : checkers.entrySet() )
148 EnvironmentCheck check = entry.getValue();
149 List<String> v = new ArrayList<String>();
150 check.validateEnvironment( v );
151 log.info( "Environment Check: " + entry.getKey() + " -> " + v.size() + " violation(s)" );
154 violations.add( "[" + entry.getKey() + "] " + s );
158 if ( CollectionUtils.isNotEmpty( violations ) )
160 StringBuffer msg = new StringBuffer();
161 msg.append( "EnvironmentCheck Failure.\n" );
162 msg.append( "======================================================================\n" );
163 msg.append( " ENVIRONMENT FAILURE !! \n" );
166 for ( String violation : violations )
168 msg.append( violation ).append( "\n" );
172 msg.append( "======================================================================" );
173 log.error( msg.toString() );
175 throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size() +
176 "] violation(s) encountered, See log for details." );
180 private void assignRepositoryObserverToGuestUser( List<ManagedRepositoryConfiguration> repos )
182 for ( ManagedRepositoryConfiguration repoConfig : repos )
184 String repoId = repoConfig.getId();
186 String principal = UserManager.GUEST_USERNAME;
192 if ( rbacManager.userAssignmentExists( principal ) )
194 ua = rbacManager.getUserAssignment( principal );
198 ua = rbacManager.createUserAssignment( principal );
201 ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
202 rbacManager.saveUserAssignment( ua );
204 catch ( RbacManagerException e )
207 "Unable to add role [" + ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) + "] to " +
208 principal + " user.", e );