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 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package org.apache.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.archiva.common.ArchivaException;
  21. import org.apache.archiva.configuration.ArchivaConfiguration;
  22. import org.apache.archiva.configuration.ConfigurationNames;
  23. import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
  24. import org.apache.archiva.redback.rbac.RBACManager;
  25. import org.apache.archiva.redback.role.RoleManagerException;
  26. import org.apache.archiva.redback.system.check.EnvironmentCheck;
  27. import org.apache.archiva.security.common.ArchivaRoleConstants;
  28. import org.apache.commons.collections.CollectionUtils;
  29. import org.apache.commons.lang.StringUtils;
  30. import org.apache.archiva.redback.rbac.RbacManagerException;
  31. import org.apache.archiva.redback.rbac.UserAssignment;
  32. import org.apache.archiva.redback.role.RoleManager;
  33. import org.apache.archiva.redback.users.UserManager;
  34. import org.apache.archiva.redback.components.registry.RegistryListener;
  35. import org.apache.commons.lang.time.StopWatch;
  36. import org.slf4j.Logger;
  37. import org.slf4j.LoggerFactory;
  38. import org.springframework.context.ApplicationContext;
  39. import org.springframework.stereotype.Service;
  40. import javax.annotation.PostConstruct;
  41. import javax.inject.Inject;
  42. import javax.inject.Named;
  43. import java.util.ArrayList;
  44. import java.util.HashMap;
  45. import java.util.List;
  46. import java.util.Map;
  47. import java.util.Map.Entry;
  48. /**
  49. * ConfigurationSynchronization
  50. */
  51. @Service
  52. public class SecuritySynchronization
  53. implements RegistryListener
  54. {
  55. private Logger log = LoggerFactory.getLogger( SecuritySynchronization.class );
  56. @Inject
  57. private RoleManager roleManager;
  58. @Inject
  59. @Named(value = "rbacManager#cached")
  60. private RBACManager rbacManager;
  61. private Map<String, EnvironmentCheck> checkers;
  62. @Inject
  63. private ArchivaConfiguration archivaConfiguration;
  64. @Inject
  65. private ApplicationContext applicationContext;
  66. @PostConstruct
  67. public void initialize()
  68. {
  69. checkers = getBeansOfType( EnvironmentCheck.class );
  70. }
  71. protected <T> Map<String, T> getBeansOfType( Class<T> clazz )
  72. {
  73. //TODO do some caching here !!!
  74. // olamy : with plexus we get only roleHint
  75. // as per convention we named spring bean role#hint remove role# if exists
  76. Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
  77. Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
  78. for ( Entry<String, T> entry : springBeans.entrySet() )
  79. {
  80. String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
  81. beans.put( key, entry.getValue() );
  82. }
  83. return beans;
  84. }
  85. public void afterConfigurationChange( org.apache.archiva.redback.components.registry.Registry registry,
  86. String propertyName, Object propertyValue )
  87. {
  88. if ( ConfigurationNames.isManagedRepositories( propertyName ) && propertyName.endsWith( ".id" ) )
  89. {
  90. if ( propertyValue != null )
  91. {
  92. syncRepoConfiguration( (String) propertyValue );
  93. }
  94. }
  95. }
  96. public void beforeConfigurationChange( org.apache.archiva.redback.components.registry.Registry registry,
  97. String propertyName, Object propertyValue )
  98. {
  99. /* do nothing */
  100. }
  101. private void synchConfiguration( List<ManagedRepositoryConfiguration> repos )
  102. {
  103. // NOTE: Remote Repositories do not have roles or security placed around them.
  104. for ( ManagedRepositoryConfiguration repoConfig : repos )
  105. {
  106. syncRepoConfiguration( repoConfig.getId() );
  107. }
  108. }
  109. private void syncRepoConfiguration( String id )
  110. {
  111. // manage roles for repositories
  112. try
  113. {
  114. if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id ) )
  115. {
  116. roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id );
  117. }
  118. else
  119. {
  120. roleManager.verifyTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id );
  121. }
  122. if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, id ) )
  123. {
  124. roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, id );
  125. }
  126. else
  127. {
  128. roleManager.verifyTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, id );
  129. }
  130. }
  131. catch ( RoleManagerException e )
  132. {
  133. // Log error.
  134. log.error( "Unable to create roles for configured repositories: " + e.getMessage(), e );
  135. }
  136. }
  137. public void startup()
  138. throws ArchivaException
  139. {
  140. executeEnvironmentChecks();
  141. synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
  142. archivaConfiguration.addChangeListener( this );
  143. if ( archivaConfiguration.isDefaulted() )
  144. {
  145. assignRepositoryObserverToGuestUser( archivaConfiguration.getConfiguration().getManagedRepositories() );
  146. }
  147. }
  148. private void executeEnvironmentChecks()
  149. throws ArchivaException
  150. {
  151. if ( ( checkers == null ) || CollectionUtils.isEmpty( checkers.values() ) )
  152. {
  153. throw new ArchivaException(
  154. "Unable to initialize the Redback Security Environment, " + "no Environment Check components found." );
  155. }
  156. StopWatch stopWatch = new StopWatch();
  157. stopWatch.reset();
  158. stopWatch.start();
  159. List<String> violations = new ArrayList<>();
  160. for ( Entry<String, EnvironmentCheck> entry : checkers.entrySet() )
  161. {
  162. EnvironmentCheck check = entry.getValue();
  163. List<String> v = new ArrayList<>();
  164. check.validateEnvironment( v );
  165. log.info( "Environment Check: {} -> {} violation(s)", entry.getKey(), v.size() );
  166. for ( String s : v )
  167. {
  168. violations.add( "[" + entry.getKey() + "] " + s );
  169. }
  170. }
  171. if ( CollectionUtils.isNotEmpty( violations ) )
  172. {
  173. StringBuilder msg = new StringBuilder();
  174. msg.append( "EnvironmentCheck Failure.\n" );
  175. msg.append( "======================================================================\n" );
  176. msg.append( " ENVIRONMENT FAILURE !! \n" );
  177. msg.append( "\n" );
  178. for ( String violation : violations )
  179. {
  180. msg.append( violation ).append( "\n" );
  181. }
  182. msg.append( "\n" );
  183. msg.append( "======================================================================" );
  184. log.error( msg.toString() );
  185. throw new ArchivaException( "Unable to initialize Redback Security Environment, [" + violations.size()
  186. + "] violation(s) encountered, See log for details." );
  187. }
  188. stopWatch.stop();
  189. log.info( "time to execute all EnvironmentCheck: {} ms", stopWatch.getTime() );
  190. }
  191. private void assignRepositoryObserverToGuestUser( List<ManagedRepositoryConfiguration> repos )
  192. {
  193. for ( ManagedRepositoryConfiguration repoConfig : repos )
  194. {
  195. String repoId = repoConfig.getId();
  196. String principal = UserManager.GUEST_USERNAME;
  197. try
  198. {
  199. UserAssignment ua;
  200. if ( rbacManager.userAssignmentExists( principal ) )
  201. {
  202. ua = rbacManager.getUserAssignment( principal );
  203. }
  204. else
  205. {
  206. ua = rbacManager.createUserAssignment( principal );
  207. }
  208. ua.addRoleName( ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ) );
  209. rbacManager.saveUserAssignment( ua );
  210. }
  211. catch ( RbacManagerException e )
  212. {
  213. log.warn( "Unable to add role [{}] to {} user.", ArchivaRoleConstants.toRepositoryObserverRoleName( repoId ), principal, e );
  214. }
  215. }
  216. }
  217. }