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;
73 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
75 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
77 createMissingManagedRepositoryRoles( archivaConfiguration.getConfiguration().getManagedRepositories() );
81 public void assignRepositoryObserverToGuestUser( List<ManagedRepositoryConfiguration> repos )
83 for ( ManagedRepositoryConfiguration repoConfig : repos )
85 String repoId = repoConfig.getId();
87 String principal = UserManager.GUEST_USERNAME;
93 if ( rbacManager.userAssignmentExists( principal ) )
95 ua = rbacManager.getUserAssignment( principal );
99 ua = rbacManager.createUserAssignment( principal );
102 ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
103 rbacManager.saveUserAssignment( ua );
105 catch ( RbacManagerException e )
108 "Unable to add role [" + ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) + "] to " +
109 principal + " user.", e );
114 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
119 public void createMissingManagedRepositoryRoles( List<ManagedRepositoryConfiguration> repos )
121 // NOTE: Remote Repositories do not have roles or security placed around them.
123 for ( ManagedRepositoryConfiguration repoConfig : repos )
125 // manage roles for repositories
128 userRepos.createMissingRepositoryRoles( repoConfig.getId() );
130 catch ( ArchivaSecurityException e )
132 log.warn( e.getMessage(), e );
137 public void createMissingRepositoryRoles( List<String> repoIds )
139 for ( String repoId : repoIds )
141 // manage roles for repositories
144 userRepos.createMissingRepositoryRoles( repoId );
146 catch ( ArchivaSecurityException e )
148 log.warn( e.getMessage(), e );
153 public void executeEnvironmentChecks()
154 throws ArchivaException
156 if ( ( checkers == null ) || CollectionUtils.isEmpty( checkers.values() ) )
158 throw new ArchivaException(
159 "Unable to initialize the Redback Security Environment, " + "no Environment Check components found." );
162 List<String> violations = new ArrayList<String>();
164 for ( Entry<String, EnvironmentCheck> entry : checkers.entrySet() )
166 EnvironmentCheck check = entry.getValue();
167 log.info( "Running Environment Check: " + entry.getKey() );
168 check.validateEnvironment( violations );
171 if ( CollectionUtils.isNotEmpty( violations ) )
173 StringBuffer msg = new StringBuffer();
174 msg.append( "EnvironmentCheck Failure.\n" );
175 msg.append( "======================================================================\n" );
176 msg.append( " ENVIRONMENT FAILURE !! \n" );
179 for ( String violation : violations )
181 msg.append( violation ).append( "\n" );
185 msg.append( "======================================================================" );
186 log.error( msg.toString() );
188 throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size() +
189 "] violation(s) encountered, See log for details." );
193 public void startup()
194 throws ArchivaException
196 executeEnvironmentChecks();
198 createMissingManagedRepositoryRoles( archivaConfiguration.getConfiguration().getManagedRepositories() );
199 archivaConfiguration.addChangeListener( this );
201 if ( archivaConfiguration.isDefaulted() )
203 assignRepositoryObserverToGuestUser( archivaConfiguration.getConfiguration().getManagedRepositories() );