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.

ArchivaLockedAdminEnvironmentCheck.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package org.apache.archiva.web.security;
  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.admin.model.RepositoryAdminException;
  21. import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
  22. import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
  23. import org.apache.archiva.redback.rbac.RBACManager;
  24. import org.apache.archiva.redback.rbac.RbacManagerException;
  25. import org.apache.archiva.redback.rbac.UserAssignment;
  26. import org.apache.archiva.redback.system.check.EnvironmentCheck;
  27. import org.apache.archiva.redback.users.User;
  28. import org.apache.archiva.redback.users.UserManager;
  29. import org.apache.archiva.redback.users.UserManagerException;
  30. import org.apache.archiva.redback.users.UserNotFoundException;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import org.springframework.context.ApplicationContext;
  34. import org.springframework.stereotype.Service;
  35. import javax.annotation.PostConstruct;
  36. import javax.inject.Inject;
  37. import javax.inject.Named;
  38. import java.util.ArrayList;
  39. import java.util.List;
  40. /**
  41. * @author Olivier Lamy
  42. */
  43. @Service( "environmentCheck#archiva-locked-admin-check" )
  44. public class ArchivaLockedAdminEnvironmentCheck
  45. implements EnvironmentCheck
  46. {
  47. protected Logger log = LoggerFactory.getLogger( getClass() );
  48. @Inject
  49. @Named( value = "rbacManager#cached" )
  50. private RBACManager rbacManager;
  51. /**
  52. * boolean detailing if this environment check has been executed
  53. */
  54. private boolean checked = false;
  55. @Inject
  56. private ApplicationContext applicationContext;
  57. @Inject
  58. private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
  59. private List<UserManager> userManagers;
  60. @PostConstruct
  61. protected void initialize()
  62. throws RepositoryAdminException
  63. {
  64. List<String> userManagerImpls =
  65. redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls();
  66. userManagers = new ArrayList<>( userManagerImpls.size() );
  67. for ( String beanId : userManagerImpls )
  68. {
  69. userManagers.add( applicationContext.getBean( "userManager#" + beanId, UserManager.class ) );
  70. }
  71. }
  72. /**
  73. * This environment check will unlock system administrator accounts that are locked on the restart of the
  74. * application when the environment checks are processed.
  75. *
  76. * @param violations
  77. */
  78. public void validateEnvironment( List<String> violations )
  79. {
  80. if ( !checked )
  81. {
  82. for ( UserManager userManager : userManagers )
  83. {
  84. if ( userManager.isReadOnly() )
  85. {
  86. continue;
  87. }
  88. List<String> roles = new ArrayList<>();
  89. roles.add( RedbackRoleConstants.SYSTEM_ADMINISTRATOR_ROLE );
  90. List<UserAssignment> systemAdminstrators;
  91. try
  92. {
  93. systemAdminstrators = rbacManager.getUserAssignmentsForRoles( roles );
  94. for ( UserAssignment userAssignment : systemAdminstrators )
  95. {
  96. try
  97. {
  98. User admin = userManager.findUser( userAssignment.getPrincipal() );
  99. if ( admin.isLocked() )
  100. {
  101. log.info( "Unlocking system administrator: {}", admin.getUsername() );
  102. admin.setLocked( false );
  103. userManager.updateUser( admin );
  104. }
  105. }
  106. catch ( UserNotFoundException ne )
  107. {
  108. log.warn( "Dangling UserAssignment -> {}", userAssignment.getPrincipal() );
  109. }
  110. catch ( UserManagerException e )
  111. {
  112. log.warn( "fail to find user {} for admin unlock check: {}", userAssignment.getPrincipal(),
  113. e.getMessage() );
  114. }
  115. }
  116. }
  117. catch ( RbacManagerException e )
  118. {
  119. log.warn( "Exception when checking for locked admin user: {}", e.getMessage(), e );
  120. }
  121. checked = true;
  122. }
  123. }
  124. }
  125. }