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.registry.Registry;
40 import org.codehaus.plexus.registry.RegistryListener;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * 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, repoConfig
102 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoConfig
106 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoConfig
109 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoConfig
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( "Unable to initialize the Redback Security Environment, "
141 + "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 log.info( "Running Environment Check: " + entry.getKey() );
150 check.validateEnvironment( violations );
153 if ( CollectionUtils.isNotEmpty( violations ) )
155 StringBuffer msg = new StringBuffer();
156 msg.append( "EnvironmentCheck Failure.\n" );
157 msg.append( "======================================================================\n" );
158 msg.append( " ENVIRONMENT FAILURE !! \n" );
161 for ( String violation : violations )
163 msg.append( violation ).append( "\n" );
167 msg.append( "======================================================================" );
168 log.error( msg.toString() );
170 throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size()
171 + "] violation(s) encountered, See log for details." );
175 private void assignRepositoryObserverToGuestUser( List<ManagedRepositoryConfiguration> repos )
177 for ( ManagedRepositoryConfiguration repoConfig : repos )
179 String repoId = repoConfig.getId();
181 // TODO: Use the Redback / UserConfiguration..getString( "redback.default.guest" ) to get the right name.
182 String principal = "guest";
188 if ( rbacManager.userAssignmentExists( principal ) )
190 ua = rbacManager.getUserAssignment( principal );
194 ua = rbacManager.createUserAssignment( principal );
197 ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
198 rbacManager.saveUserAssignment( ua );
200 catch ( RbacManagerException e )
202 log.warn( "Unable to add role [" + ArchivaRoleConstants.toRepositoryObserverRoleName( repoId )
203 + "] to " + principal + " user.", e );