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.registry.Registry;
37 import org.codehaus.plexus.registry.RegistryListener;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
45 * @plexus.component role="org.apache.maven.archiva.security.SecurityStartup"
47 public class SecurityStartup
48 implements RegistryListener
50 private Logger log = LoggerFactory.getLogger( SecurityStartup.class );
55 private UserRepositories userRepos;
58 * @plexus.requirement role-hint="cached"
60 private RBACManager rbacManager;
63 * @plexus.requirement role="org.codehaus.plexus.redback.system.check.EnvironmentCheck"
65 private Map<String, EnvironmentCheck> checkers;
70 private ArchivaConfiguration archivaConfiguration;
75 private ArchivaXworkUser archivaXworkUser;
77 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
79 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
81 createMissingManagedRepositoryRoles( archivaConfiguration.getConfiguration().getManagedRepositories() );
85 public void assignRepositoryObserverToGuestUser( List<ManagedRepositoryConfiguration> repos )
87 for ( ManagedRepositoryConfiguration repoConfig : repos )
89 String repoId = repoConfig.getId();
91 String principal = archivaXworkUser.getGuest();
97 if ( rbacManager.userAssignmentExists( principal ) )
99 ua = rbacManager.getUserAssignment( principal );
103 ua = rbacManager.createUserAssignment( principal );
106 ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
107 rbacManager.saveUserAssignment( ua );
109 catch ( RbacManagerException e )
112 "Unable to add role [" + ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) + "] to " +
113 principal + " user.", e );
118 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
123 public void createMissingManagedRepositoryRoles( List<ManagedRepositoryConfiguration> repos )
125 // NOTE: Remote Repositories do not have roles or security placed around them.
127 for ( ManagedRepositoryConfiguration repoConfig : repos )
129 // manage roles for repositories
132 userRepos.createMissingRepositoryRoles( repoConfig.getId() );
134 catch ( ArchivaSecurityException e )
136 log.warn( e.getMessage(), e );
141 public void createMissingRepositoryRoles( List<String> repoIds )
143 for ( String repoId : repoIds )
145 // manage roles for repositories
148 userRepos.createMissingRepositoryRoles( repoId );
150 catch ( ArchivaSecurityException e )
152 log.warn( e.getMessage(), e );
157 public void executeEnvironmentChecks()
158 throws ArchivaException
160 if ( ( checkers == null ) || CollectionUtils.isEmpty( checkers.values() ) )
162 throw new ArchivaException(
163 "Unable to initialize the Redback Security Environment, " + "no Environment Check components found." );
166 List<String> violations = new ArrayList<String>();
168 for ( Entry<String, EnvironmentCheck> entry : checkers.entrySet() )
170 EnvironmentCheck check = entry.getValue();
171 log.info( "Running Environment Check: " + entry.getKey() );
172 check.validateEnvironment( violations );
175 if ( CollectionUtils.isNotEmpty( violations ) )
177 StringBuffer msg = new StringBuffer();
178 msg.append( "EnvironmentCheck Failure.\n" );
179 msg.append( "======================================================================\n" );
180 msg.append( " ENVIRONMENT FAILURE !! \n" );
183 for ( String violation : violations )
185 msg.append( violation ).append( "\n" );
189 msg.append( "======================================================================" );
190 log.error( msg.toString() );
192 throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size() +
193 "] violation(s) encountered, See log for details." );
197 public void startup()
198 throws ArchivaException
200 executeEnvironmentChecks();
202 createMissingManagedRepositoryRoles( archivaConfiguration.getConfiguration().getManagedRepositories() );
203 archivaConfiguration.addChangeListener( this );
205 if ( archivaConfiguration.isDefaulted() )
207 assignRepositoryObserverToGuestUser( archivaConfiguration.getConfiguration().getManagedRepositories() );