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;
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 // TODO: Use the Redback / UserConfiguration..getString( "redback.default.guest" ) to get the right name.
88 String principal = "guest";
94 if ( rbacManager.userAssignmentExists( principal ) )
96 ua = rbacManager.getUserAssignment( principal );
100 ua = rbacManager.createUserAssignment( principal );
103 ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
104 rbacManager.saveUserAssignment( ua );
106 catch ( RbacManagerException e )
109 "Unable to add role [" + ArchivaRoleConstants.toRepositoryObserverRoleName( repoId )
110 + "] to " + principal + " user.", e );
115 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
120 public void createMissingManagedRepositoryRoles( List<ManagedRepositoryConfiguration> repos )
122 // NOTE: Remote Repositories do not have roles or security placed around them.
124 for ( ManagedRepositoryConfiguration repoConfig : repos )
126 // manage roles for repositories
129 userRepos.createMissingRepositoryRoles( repoConfig.getId() );
131 catch ( ArchivaSecurityException e )
133 log.warn( e.getMessage(), e );
138 public void createMissingRepositoryRoles( List<String> repoIds )
140 for ( String repoId : repoIds )
142 // manage roles for repositories
145 userRepos.createMissingRepositoryRoles( repoId );
147 catch ( ArchivaSecurityException e )
149 log.warn( e.getMessage(), e );
154 public void executeEnvironmentChecks()
155 throws ArchivaException
157 if ( ( checkers == null ) || CollectionUtils.isEmpty( checkers.values() ) )
159 throw new ArchivaException( "Unable to initialize the Redback Security Environment, "
160 + "no Environment Check components found." );
163 List<String> violations = new ArrayList<String>();
165 for ( Entry<String, EnvironmentCheck> entry : checkers.entrySet() )
167 EnvironmentCheck check = entry.getValue();
168 log.info( "Running Environment Check: " + entry.getKey() );
169 check.validateEnvironment( violations );
172 if ( CollectionUtils.isNotEmpty( violations ) )
174 StringBuffer msg = new StringBuffer();
175 msg.append( "EnvironmentCheck Failure.\n" );
176 msg.append( "======================================================================\n" );
177 msg.append( " ENVIRONMENT FAILURE !! \n" );
180 for ( String violation : violations )
182 msg.append( violation ).append( "\n" );
186 msg.append( "======================================================================" );
187 log.error( msg.toString() );
189 throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size()
190 + "] violation(s) encountered, See log for details." );
194 public void startup()
195 throws ArchivaException
197 executeEnvironmentChecks();
199 createMissingManagedRepositoryRoles( archivaConfiguration.getConfiguration().getManagedRepositories() );
200 archivaConfiguration.addChangeListener( this );
202 if ( archivaConfiguration.isDefaulted() )
204 assignRepositoryObserverToGuestUser( archivaConfiguration.getConfiguration().getManagedRepositories() );