You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SecuritySynchronization.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package org.apache.maven.archiva.web.startup;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.commons.collections.CollectionUtils;
  21. import org.apache.maven.archiva.common.ArchivaException;
  22. import org.apache.maven.archiva.configuration.ArchivaConfiguration;
  23. import org.apache.maven.archiva.configuration.ConfigurationNames;
  24. import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
  25. import org.apache.maven.archiva.security.ArchivaRoleConstants;
  26. import org.codehaus.plexus.logging.AbstractLogEnabled;
  27. import org.codehaus.plexus.redback.rbac.RBACManager;
  28. import org.codehaus.plexus.redback.rbac.RbacManagerException;
  29. import org.codehaus.plexus.redback.rbac.UserAssignment;
  30. import org.codehaus.plexus.redback.role.RoleManager;
  31. import org.codehaus.plexus.redback.role.RoleManagerException;
  32. import org.codehaus.plexus.redback.system.check.EnvironmentCheck;
  33. import org.codehaus.plexus.registry.Registry;
  34. import org.codehaus.plexus.registry.RegistryListener;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. import java.util.Map;
  38. import java.util.Map.Entry;
  39. /**
  40. * ConfigurationSynchronization
  41. *
  42. * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
  43. * @version $Id$
  44. *
  45. * @plexus.component role="org.apache.maven.archiva.web.startup.SecuritySynchronization"
  46. * role-hint="default"
  47. */
  48. public class SecuritySynchronization
  49. extends AbstractLogEnabled
  50. implements RegistryListener
  51. {
  52. /**
  53. * @plexus.requirement role-hint="default"
  54. */
  55. private RoleManager roleManager;
  56. /**
  57. * @plexus.requirement role-hint="cached"
  58. */
  59. private RBACManager rbacManager;
  60. /**
  61. * @plexus.requirement role="org.codehaus.plexus.redback.system.check.EnvironmentCheck"
  62. */
  63. private Map<String, EnvironmentCheck> checkers;
  64. /**
  65. * @plexus.requirement
  66. */
  67. private ArchivaConfiguration archivaConfiguration;
  68. public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  69. {
  70. if ( ConfigurationNames.isManagedRepositories( propertyName ) )
  71. {
  72. synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
  73. }
  74. }
  75. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  76. {
  77. /* do nothing */
  78. }
  79. private void synchConfiguration( List<ManagedRepositoryConfiguration> repos )
  80. {
  81. // NOTE: Remote Repositories do not have roles or security placed around them.
  82. for ( ManagedRepositoryConfiguration repoConfig : repos )
  83. {
  84. // manage roles for repositories
  85. try
  86. {
  87. if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoConfig
  88. .getId() ) )
  89. {
  90. roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoConfig
  91. .getId() );
  92. }
  93. if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoConfig
  94. .getId() ) )
  95. {
  96. roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoConfig
  97. .getId() );
  98. }
  99. }
  100. catch ( RoleManagerException e )
  101. {
  102. // Log error.
  103. getLogger().error( "Unable to create roles for configured repositories: " + e.getMessage(), e );
  104. }
  105. }
  106. }
  107. public void startup()
  108. throws ArchivaException
  109. {
  110. executeEnvironmentChecks();
  111. synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
  112. archivaConfiguration.addChangeListener( this );
  113. if ( archivaConfiguration.isDefaulted() )
  114. {
  115. assignRepositoryObserverToGuestUser( archivaConfiguration.getConfiguration().getManagedRepositories() );
  116. }
  117. }
  118. private void executeEnvironmentChecks()
  119. throws ArchivaException
  120. {
  121. if ( ( checkers == null ) || CollectionUtils.isEmpty( checkers.values() ) )
  122. {
  123. throw new ArchivaException( "Unable to initialize the Redback Security Environment, "
  124. + "no Environment Check components found." );
  125. }
  126. List<String> violations = new ArrayList<String>();
  127. for ( Entry<String, EnvironmentCheck> entry : checkers.entrySet() )
  128. {
  129. EnvironmentCheck check = entry.getValue();
  130. getLogger().info( "Running Environment Check: " + entry.getKey() );
  131. check.validateEnvironment( violations );
  132. }
  133. if ( CollectionUtils.isNotEmpty( violations ) )
  134. {
  135. StringBuffer msg = new StringBuffer();
  136. msg.append( "EnvironmentCheck Failure.\n" );
  137. msg.append( "======================================================================\n" );
  138. msg.append( " ENVIRONMENT FAILURE !! \n" );
  139. msg.append( "\n" );
  140. for ( String violation : violations )
  141. {
  142. msg.append( violation ).append( "\n" );
  143. }
  144. msg.append( "\n" );
  145. msg.append( "======================================================================" );
  146. getLogger().fatalError( msg.toString() );
  147. throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size()
  148. + "] violation(s) encountered, See log for details." );
  149. }
  150. }
  151. private void assignRepositoryObserverToGuestUser( List<ManagedRepositoryConfiguration> repos )
  152. {
  153. for ( ManagedRepositoryConfiguration repoConfig : repos )
  154. {
  155. String repoId = repoConfig.getId();
  156. // TODO: Use the Redback / UserConfiguration..getString( "redback.default.guest" ) to get the right name.
  157. String principal = "guest";
  158. try
  159. {
  160. UserAssignment ua;
  161. if ( rbacManager.userAssignmentExists( principal ) )
  162. {
  163. ua = rbacManager.getUserAssignment( principal );
  164. }
  165. else
  166. {
  167. ua = rbacManager.createUserAssignment( principal );
  168. }
  169. ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
  170. rbacManager.saveUserAssignment( ua );
  171. }
  172. catch ( RbacManagerException e )
  173. {
  174. getLogger().warn( "Unable to add role [" + ArchivaRoleConstants.toRepositoryObserverRoleName( repoId )
  175. + "] to " + principal + " user.", e );
  176. }
  177. }
  178. }
  179. }