1 package org.apache.maven.archiva.security;
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.codehaus.plexus.redback.rbac.RBACManager;
33 import org.codehaus.plexus.redback.rbac.RbacManagerException;
34 import org.codehaus.plexus.redback.rbac.UserAssignment;
35 import org.codehaus.plexus.redback.system.check.EnvironmentCheck;
36 import org.codehaus.plexus.redback.users.UserManager;
37 import org.codehaus.plexus.registry.Registry;
38 import org.codehaus.plexus.registry.RegistryListener;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
46 * @plexus.component role="org.apache.maven.archiva.security.SecurityStartup"
48 public class SecurityStartup
49 implements RegistryListener
51 private Logger log = LoggerFactory.getLogger( SecurityStartup.class );
56 private UserRepositories userRepos;
59 * @plexus.requirement role-hint="cached"
61 private RBACManager rbacManager;
64 * @plexus.requirement role="org.codehaus.plexus.redback.system.check.EnvironmentCheck"
66 private Map<String, EnvironmentCheck> checkers;
71 private ArchivaConfiguration archivaConfiguration;
76 private ArchivaXworkUser archivaXworkUser;
78 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
80 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
82 createMissingManagedRepositoryRoles( archivaConfiguration.getConfiguration().getManagedRepositories() );
86 public void assignRepositoryObserverToGuestUser( List<ManagedRepositoryConfiguration> repos )
88 for ( ManagedRepositoryConfiguration repoConfig : repos )
90 String repoId = repoConfig.getId();
92 String principal = UserManager.GUEST_USERNAME;
98 if ( rbacManager.userAssignmentExists( principal ) )
100 ua = rbacManager.getUserAssignment( principal );
104 ua = rbacManager.createUserAssignment( principal );
107 ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
108 rbacManager.saveUserAssignment( ua );
110 catch ( RbacManagerException e )
113 "Unable to add role [" + ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) + "] to " +
114 principal + " user.", e );
119 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
124 public void createMissingManagedRepositoryRoles( List<ManagedRepositoryConfiguration> repos )
126 // NOTE: Remote Repositories do not have roles or security placed around them.
128 for ( ManagedRepositoryConfiguration repoConfig : repos )
130 // manage roles for repositories
133 userRepos.createMissingRepositoryRoles( repoConfig.getId() );
135 catch ( ArchivaSecurityException e )
137 log.warn( e.getMessage(), e );
142 public void createMissingRepositoryRoles( List<String> repoIds )
144 for ( String repoId : repoIds )
146 // manage roles for repositories
149 userRepos.createMissingRepositoryRoles( repoId );
151 catch ( ArchivaSecurityException e )
153 log.warn( e.getMessage(), e );
158 public void executeEnvironmentChecks()
159 throws ArchivaException
161 if ( ( checkers == null ) || CollectionUtils.isEmpty( checkers.values() ) )
163 throw new ArchivaException(
164 "Unable to initialize the Redback Security Environment, " + "no Environment Check components found." );
167 List<String> violations = new ArrayList<String>();
169 for ( Entry<String, EnvironmentCheck> entry : checkers.entrySet() )
171 EnvironmentCheck check = entry.getValue();
172 log.info( "Running Environment Check: " + entry.getKey() );
173 check.validateEnvironment( violations );
176 if ( CollectionUtils.isNotEmpty( violations ) )
178 StringBuffer msg = new StringBuffer();
179 msg.append( "EnvironmentCheck Failure.\n" );
180 msg.append( "======================================================================\n" );
181 msg.append( " ENVIRONMENT FAILURE !! \n" );
184 for ( String violation : violations )
186 msg.append( violation ).append( "\n" );
190 msg.append( "======================================================================" );
191 log.error( msg.toString() );
193 throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size() +
194 "] violation(s) encountered, See log for details." );
198 public void startup()
199 throws ArchivaException
201 executeEnvironmentChecks();
203 createMissingManagedRepositoryRoles( archivaConfiguration.getConfiguration().getManagedRepositories() );
204 archivaConfiguration.addChangeListener( this );
206 if ( archivaConfiguration.isDefaulted() )
208 assignRepositoryObserverToGuestUser( archivaConfiguration.getConfiguration().getManagedRepositories() );