1 package org.apache.archiva.rest.services;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.LdapConfiguration;
23 import org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration;
24 import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
25 import org.apache.archiva.redback.authentication.Authenticator;
26 import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
27 import org.apache.archiva.redback.common.ldap.connection.LdapConnectionConfiguration;
28 import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
29 import org.apache.archiva.redback.common.ldap.connection.LdapException;
30 import org.apache.archiva.redback.common.ldap.user.LdapUserMapper;
31 import org.apache.archiva.redback.components.cache.Cache;
32 import org.apache.archiva.redback.policy.CookieSettings;
33 import org.apache.archiva.redback.policy.PasswordRule;
34 import org.apache.archiva.redback.rbac.RBACManager;
35 import org.apache.archiva.redback.role.RoleManager;
36 import org.apache.archiva.redback.users.UserManager;
37 import org.apache.archiva.rest.api.model.RBACManagerImplementationInformation;
38 import org.apache.archiva.rest.api.model.RedbackImplementationsInformations;
39 import org.apache.archiva.rest.api.model.UserManagerImplementationInformation;
40 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
41 import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
42 import org.apache.commons.lang.StringUtils;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.stereotype.Service;
46 import javax.inject.Inject;
47 import javax.inject.Named;
48 import javax.naming.InvalidNameException;
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.Collections;
52 import java.util.List;
54 import java.util.Properties;
57 * @author Olivier Lamy
60 @Service("redbackRuntimeConfigurationService#rest")
61 public class DefaultRedbackRuntimeConfigurationService
62 extends AbstractRestService
63 implements RedbackRuntimeConfigurationService
67 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
70 @Named(value = "userManager#configurable")
71 private UserManager userManager;
74 @Named(value = "rbacManager#default")
75 private RBACManager rbacManager;
78 private RoleManager roleManager;
81 private ApplicationContext applicationContext;
84 @Named(value = "ldapConnectionFactory#configurable")
85 private LdapConnectionFactory ldapConnectionFactory;
88 @Named(value = "cache#users")
89 private Cache usersCache;
92 private LdapUserMapper ldapUserMapper;
96 public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
97 throws ArchivaRestServiceException
101 RedbackRuntimeConfiguration redbackRuntimeConfiguration =
102 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
104 log.debug( "getRedbackRuntimeConfiguration -> {}", redbackRuntimeConfiguration );
106 return redbackRuntimeConfiguration;
108 catch ( RepositoryAdminException e )
110 throw new ArchivaRestServiceException( e.getMessage(), e );
115 public Boolean updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
116 throws ArchivaRestServiceException
120 // has user manager impl changed ?
121 boolean userManagerChanged = redbackRuntimeConfiguration.getUserManagerImpls().size()
122 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().size();
125 userManagerChanged || ( redbackRuntimeConfiguration.getUserManagerImpls().toString().hashCode()
126 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().toString().hashCode() );
128 boolean rbacManagerChanged = redbackRuntimeConfiguration.getRbacManagerImpls().size()
129 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls().size();
132 rbacManagerChanged || ( redbackRuntimeConfiguration.getRbacManagerImpls().toString().hashCode()
133 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls().toString().hashCode() );
135 boolean ldapConfigured = false;
136 for (String um : redbackRuntimeConfiguration.getUserManagerImpls()) {
137 if (um.contains("ldap")) {
141 if (!ldapConfigured) {
142 for (String rbm : redbackRuntimeConfiguration.getRbacManagerImpls()) {
143 if (rbm.contains("ldap")) {
144 ldapConfigured = true;
149 redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
151 if ( userManagerChanged )
153 log.info( "user managerImpls changed to {} so reload it",
154 redbackRuntimeConfiguration.getUserManagerImpls() );
155 userManager.initialize();
158 if ( rbacManagerChanged )
160 log.info( "rbac manager changed to {} so reload it",
161 redbackRuntimeConfiguration.getRbacManagerImpls() );
162 rbacManager.initialize();
163 roleManager.initialize();
166 if (ldapConfigured) {
168 ldapConnectionFactory.initialize();
169 } catch (Exception e) {
170 ArchivaRestServiceException newEx = new ArchivaRestServiceException(e.getMessage(), e);
171 newEx.setErrorKey("error.ldap.connectionFactory.init.failed");
175 Collection<PasswordRule> passwordRules = applicationContext.getBeansOfType( PasswordRule.class ).values();
177 for ( PasswordRule passwordRule : passwordRules )
179 passwordRule.initialize();
182 Collection<CookieSettings> cookieSettingsList =
183 applicationContext.getBeansOfType( CookieSettings.class ).values();
185 for ( CookieSettings cookieSettings : cookieSettingsList )
187 cookieSettings.initialize();
190 Collection<Authenticator> authenticators =
191 applicationContext.getBeansOfType( Authenticator.class ).values();
193 for ( Authenticator authenticator : authenticators )
196 log.debug("Initializing authenticatior "+authenticator.getId());
197 authenticator.initialize();
198 } catch (Exception e) {
199 log.error("Initialization of authenticator failed "+authenticator.getId(),e);
204 usersCache.setTimeToIdleSeconds(
205 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
206 usersCache.setTimeToLiveSeconds(
207 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
208 usersCache.setMaxElementsInMemory(
209 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
210 usersCache.setMaxElementsOnDisk(
211 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
213 if (ldapConfigured) {
215 ldapUserMapper.initialize();
216 } catch (Exception e) {
217 ArchivaRestServiceException newEx = new ArchivaRestServiceException(e.getMessage(), e);
218 newEx.setErrorKey("error.ldap.userMapper.init.failed");
228 catch (ArchivaRestServiceException e) {
229 log.error(e.getMessage(), e);
231 } catch ( Exception e )
233 log.error( e.getMessage(), e );
234 throw new ArchivaRestServiceException(e.getMessage(), e);
239 public List<UserManagerImplementationInformation> getUserManagerImplementationInformations()
240 throws ArchivaRestServiceException
243 Map<String, UserManager> beans = applicationContext.getBeansOfType( UserManager.class );
245 if ( beans.isEmpty() )
247 return Collections.emptyList();
250 List<UserManagerImplementationInformation> informations = new ArrayList<>( beans.size() );
252 for ( Map.Entry<String, UserManager> entry : beans.entrySet() )
254 UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
255 if ( userManager.isFinalImplementation() )
257 UserManagerImplementationInformation information = new UserManagerImplementationInformation();
258 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
259 information.setDescriptionKey( userManager.getDescriptionKey() );
260 information.setReadOnly( userManager.isReadOnly() );
261 informations.add( information );
269 public List<RBACManagerImplementationInformation> getRbacManagerImplementationInformations()
270 throws ArchivaRestServiceException
272 Map<String, RBACManager> beans = applicationContext.getBeansOfType( RBACManager.class );
274 if ( beans.isEmpty() )
276 return Collections.emptyList();
279 List<RBACManagerImplementationInformation> informations = new ArrayList<>( beans.size() );
281 for ( Map.Entry<String, RBACManager> entry : beans.entrySet() )
283 RBACManager rbacManager = applicationContext.getBean( entry.getKey(), RBACManager.class );
284 if ( rbacManager.isFinalImplementation() )
286 RBACManagerImplementationInformation information = new RBACManagerImplementationInformation();
287 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
288 information.setDescriptionKey( rbacManager.getDescriptionKey() );
289 information.setReadOnly( rbacManager.isReadOnly() );
290 informations.add( information );
298 public RedbackImplementationsInformations getRedbackImplementationsInformations()
299 throws ArchivaRestServiceException
301 return new RedbackImplementationsInformations( getUserManagerImplementationInformations(),
302 getRbacManagerImplementationInformations() );
306 public Boolean checkLdapConnection()
307 throws ArchivaRestServiceException
309 LdapConnection ldapConnection = null;
312 ldapConnection = ldapConnectionFactory.getConnection();
314 catch ( LdapException e )
316 log.warn( "fail to get ldapConnection: {}", e.getMessage(), e );
317 throw new ArchivaRestServiceException( e.getMessage(), e );
322 if ( ldapConnection != null )
324 ldapConnection.close();
332 public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
333 throws ArchivaRestServiceException
335 LdapConnection ldapConnection = null;
338 LdapConnectionConfiguration ldapConnectionConfiguration =
339 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
340 ldapConfiguration.getBaseDn(), ldapConfiguration.getContextFactory(),
341 ldapConfiguration.getBindDn(), ldapConfiguration.getPassword(),
342 ldapConfiguration.getAuthenticationMethod(),
343 toProperties( ldapConfiguration.getExtraProperties() ) );
344 ldapConnectionConfiguration.setSsl( ldapConfiguration.isSsl() );
346 ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
348 ldapConnection.close();
350 // verify groups dn value too
352 ldapConnectionConfiguration =
353 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
354 ldapConfiguration.getBaseGroupsDn(),
355 ldapConfiguration.getContextFactory(), ldapConfiguration.getBindDn(),
356 ldapConfiguration.getPassword(),
357 ldapConfiguration.getAuthenticationMethod(),
358 toProperties( ldapConfiguration.getExtraProperties() ) );
360 ldapConnectionConfiguration.setSsl( ldapConfiguration.isSsl() );
362 ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
364 catch ( InvalidNameException e )
366 log.warn( "fail to get ldapConnection: {}", e.getMessage(), e );
367 throw new ArchivaRestServiceException( e.getMessage(), e );
369 catch ( LdapException e )
371 log.warn( "fail to get ldapConnection: {}", e.getMessage(), e );
372 throw new ArchivaRestServiceException( e.getMessage(), e );
377 if ( ldapConnection != null )
379 ldapConnection.close();
386 private Properties toProperties( Map<String, String> map )
388 Properties properties = new Properties();
389 if ( map == null || map.isEmpty() )
393 for ( Map.Entry<String, String> entry : map.entrySet() )
395 properties.put( entry.getKey(), entry.getValue() );