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.

DefaultUserRepositories.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package org.apache.archiva.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.beans.ManagedRepository;
  22. import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
  23. import org.apache.archiva.redback.authentication.AuthenticationResult;
  24. import org.apache.archiva.redback.authorization.AuthorizationException;
  25. import org.apache.archiva.redback.role.RoleManager;
  26. import org.apache.archiva.redback.role.RoleManagerException;
  27. import org.apache.archiva.redback.system.DefaultSecuritySession;
  28. import org.apache.archiva.redback.system.SecuritySession;
  29. import org.apache.archiva.redback.system.SecuritySystem;
  30. import org.apache.archiva.redback.users.User;
  31. import org.apache.archiva.redback.users.UserManagerException;
  32. import org.apache.archiva.redback.users.UserNotFoundException;
  33. import org.apache.archiva.security.common.ArchivaRoleConstants;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;
  36. import org.springframework.stereotype.Service;
  37. import javax.inject.Inject;
  38. import java.util.ArrayList;
  39. import java.util.List;
  40. /**
  41. * DefaultUserRepositories
  42. */
  43. @Service( "userRepositories" )
  44. public class DefaultUserRepositories
  45. implements UserRepositories
  46. {
  47. @Inject
  48. private SecuritySystem securitySystem;
  49. @Inject
  50. private RoleManager roleManager;
  51. @Inject
  52. private ManagedRepositoryAdmin managedRepositoryAdmin;
  53. private Logger log = LoggerFactory.getLogger( getClass() );
  54. public List<String> getObservableRepositoryIds( String principal )
  55. throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
  56. {
  57. String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS;
  58. return getAccessibleRepositoryIds( principal, operation );
  59. }
  60. public List<String> getManagableRepositoryIds( String principal )
  61. throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
  62. {
  63. String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD;
  64. return getAccessibleRepositoryIds( principal, operation );
  65. }
  66. private List<String> getAccessibleRepositoryIds( String principal, String operation )
  67. throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
  68. {
  69. List<ManagedRepository> managedRepositories = getAccessibleRepositories( principal, operation );
  70. List<String> repoIds = new ArrayList<>( managedRepositories.size() );
  71. for ( ManagedRepository managedRepository : managedRepositories )
  72. {
  73. repoIds.add( managedRepository.getId() );
  74. }
  75. return repoIds;
  76. }
  77. public List<ManagedRepository> getAccessibleRepositories( String principal )
  78. throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
  79. {
  80. return getAccessibleRepositories( principal, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS );
  81. }
  82. private List<ManagedRepository> getAccessibleRepositories( String principal, String operation )
  83. throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
  84. {
  85. SecuritySession securitySession = createSession( principal );
  86. List<ManagedRepository> managedRepositories = new ArrayList<>();
  87. try
  88. {
  89. List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
  90. for ( ManagedRepository repo : repos )
  91. {
  92. try
  93. {
  94. String repoId = repo.getId();
  95. if ( securitySystem.isAuthorized( securitySession, operation, repoId ) )
  96. {
  97. managedRepositories.add( repo );
  98. }
  99. }
  100. catch ( AuthorizationException e )
  101. {
  102. // swallow.
  103. log.debug( "Not authorizing '{}' for repository '{}': {}", principal, repo.getId(),
  104. e.getMessage() );
  105. }
  106. }
  107. return managedRepositories;
  108. }
  109. catch ( RepositoryAdminException e )
  110. {
  111. throw new ArchivaSecurityException( e.getMessage(), e );
  112. }
  113. }
  114. private SecuritySession createSession( String principal )
  115. throws ArchivaSecurityException, AccessDeniedException
  116. {
  117. User user;
  118. try
  119. {
  120. user = securitySystem.getUserManager().findUser( principal );
  121. if ( user == null )
  122. {
  123. throw new ArchivaSecurityException(
  124. "The security system had an internal error - please check your system logs" );
  125. }
  126. }
  127. catch ( UserNotFoundException e )
  128. {
  129. throw new PrincipalNotFoundException( "Unable to find principal " + principal + "", e );
  130. }
  131. catch ( UserManagerException e )
  132. {
  133. throw new ArchivaSecurityException( e.getMessage(), e );
  134. }
  135. if ( user.isLocked() )
  136. {
  137. throw new AccessDeniedException( "User " + principal + "(" + user.getFullName() + ") is locked." );
  138. }
  139. AuthenticationResult authn = new AuthenticationResult( true, principal, null );
  140. authn.setUser( user );
  141. return new DefaultSecuritySession( authn, user );
  142. }
  143. public void createMissingRepositoryRoles( String repoId )
  144. throws ArchivaSecurityException
  145. {
  146. try
  147. {
  148. if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
  149. {
  150. roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
  151. }
  152. if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
  153. {
  154. roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
  155. }
  156. }
  157. catch ( RoleManagerException e )
  158. {
  159. throw new ArchivaSecurityException( "Unable to create roles for configured repositories: " + e.getMessage(),
  160. e );
  161. }
  162. }
  163. public boolean isAuthorizedToUploadArtifacts( String principal, String repoId )
  164. throws PrincipalNotFoundException, ArchivaSecurityException
  165. {
  166. try
  167. {
  168. SecuritySession securitySession = createSession( principal );
  169. return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
  170. repoId );
  171. }
  172. catch ( AuthorizationException e )
  173. {
  174. throw new ArchivaSecurityException( e.getMessage(), e);
  175. }
  176. }
  177. public boolean isAuthorizedToDeleteArtifacts( String principal, String repoId )
  178. throws ArchivaSecurityException
  179. {
  180. try
  181. {
  182. SecuritySession securitySession = createSession( principal );
  183. return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_DELETE,
  184. repoId );
  185. }
  186. catch ( AuthorizationException e )
  187. {
  188. throw new ArchivaSecurityException( e.getMessage(), e);
  189. }
  190. }
  191. public SecuritySystem getSecuritySystem()
  192. {
  193. return securitySystem;
  194. }
  195. public void setSecuritySystem( SecuritySystem securitySystem )
  196. {
  197. this.securitySystem = securitySystem;
  198. }
  199. public RoleManager getRoleManager()
  200. {
  201. return roleManager;
  202. }
  203. public void setRoleManager( RoleManager roleManager )
  204. {
  205. this.roleManager = roleManager;
  206. }
  207. }