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.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.ActionStatus;
38 import org.apache.archiva.rest.api.model.RBACManagerImplementationInformation;
39 import org.apache.archiva.rest.api.model.RedbackImplementationsInformations;
40 import org.apache.archiva.rest.api.model.UserManagerImplementationInformation;
41 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
42 import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
43 import org.apache.commons.lang3.StringUtils;
44 import org.springframework.context.ApplicationContext;
45 import org.springframework.stereotype.Service;
47 import javax.inject.Inject;
48 import javax.inject.Named;
49 import javax.naming.InvalidNameException;
50 import javax.naming.NamingException;
51 import java.util.ArrayList;
52 import java.util.Collection;
53 import java.util.Collections;
54 import java.util.List;
56 import java.util.Properties;
59 * @author Olivier Lamy
62 @Service("redbackRuntimeConfigurationService#rest")
63 public class DefaultRedbackRuntimeConfigurationService
64 extends AbstractRestService
65 implements RedbackRuntimeConfigurationService
69 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
72 @Named(value = "userManager#default")
73 private UserManager userManager;
76 @Named(value = "rbacManager#default")
77 private RBACManager rbacManager;
80 private RoleManager roleManager;
83 private ApplicationContext applicationContext;
86 @Named(value = "ldapConnectionFactory#configurable")
87 private LdapConnectionFactory ldapConnectionFactory;
90 @Named(value = "cache#users")
91 private Cache usersCache;
94 private LdapUserMapper ldapUserMapper;
98 public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
99 throws ArchivaRestServiceException
103 RedbackRuntimeConfiguration redbackRuntimeConfiguration =
104 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
106 log.debug( "getRedbackRuntimeConfiguration -> {}", redbackRuntimeConfiguration );
108 return redbackRuntimeConfiguration;
110 catch ( RepositoryAdminException e )
112 throw new ArchivaRestServiceException( e.getMessage(), e );
117 public Boolean updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
118 throws ArchivaRestServiceException
122 // has user manager impl changed ?
123 boolean userManagerChanged = redbackRuntimeConfiguration.getUserManagerImpls().size()
124 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().size();
127 userManagerChanged || ( redbackRuntimeConfiguration.getUserManagerImpls().toString().hashCode()
128 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().toString().hashCode() );
130 boolean rbacManagerChanged = redbackRuntimeConfiguration.getRbacManagerImpls().size()
131 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls().size();
134 rbacManagerChanged || ( redbackRuntimeConfiguration.getRbacManagerImpls().toString().hashCode()
135 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls().toString().hashCode() );
137 boolean ldapConfigured = false;
138 for (String um : redbackRuntimeConfiguration.getUserManagerImpls()) {
139 if (um.contains("ldap")) {
143 if (!ldapConfigured) {
144 for (String rbm : redbackRuntimeConfiguration.getRbacManagerImpls()) {
145 if (rbm.contains("ldap")) {
146 ldapConfigured = true;
151 redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
153 if ( userManagerChanged )
155 log.info( "user managerImpls changed to {} so reload it",
156 redbackRuntimeConfiguration.getUserManagerImpls() );
157 userManager.initialize();
160 if ( rbacManagerChanged )
162 log.info( "rbac manager changed to {} so reload it",
163 redbackRuntimeConfiguration.getRbacManagerImpls() );
164 rbacManager.initialize();
165 roleManager.initialize();
168 if (ldapConfigured) {
170 ldapConnectionFactory.initialize();
171 } catch (Exception e) {
172 ArchivaRestServiceException newEx = new ArchivaRestServiceException(e.getMessage(), e);
173 newEx.setErrorKey("error.ldap.connectionFactory.init.failed");
177 Collection<PasswordRule> passwordRules = applicationContext.getBeansOfType( PasswordRule.class ).values();
179 for ( PasswordRule passwordRule : passwordRules )
181 passwordRule.initialize();
184 Collection<CookieSettings> cookieSettingsList =
185 applicationContext.getBeansOfType( CookieSettings.class ).values();
187 for ( CookieSettings cookieSettings : cookieSettingsList )
189 cookieSettings.initialize();
192 Collection<Authenticator> authenticators =
193 applicationContext.getBeansOfType( Authenticator.class ).values();
195 for ( Authenticator authenticator : authenticators )
198 log.debug("Initializing authenticatior "+authenticator.getId());
199 authenticator.initialize();
200 } catch (Exception e) {
201 log.error("Initialization of authenticator failed "+authenticator.getId(),e);
206 usersCache.setTimeToIdleSeconds(
207 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
208 usersCache.setTimeToLiveSeconds(
209 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
210 usersCache.setMaxElementsInMemory(
211 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
212 usersCache.setMaxElementsOnDisk(
213 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
215 if (ldapConfigured) {
217 ldapUserMapper.initialize();
218 } catch (Exception e) {
219 ArchivaRestServiceException newEx = new ArchivaRestServiceException(e.getMessage(), e);
220 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 )
326 ldapConnection.close();
328 catch ( NamingException e )
330 log.error( "Could not close connection: {}", e.getMessage( ), e );
339 public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
340 throws ArchivaRestServiceException
342 LdapConnection ldapConnection = null;
345 LdapConnectionConfiguration ldapConnectionConfiguration =
346 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
347 ldapConfiguration.getBaseDn(), ldapConfiguration.getContextFactory(),
348 ldapConfiguration.getBindDn(), ldapConfiguration.getPassword(),
349 ldapConfiguration.getAuthenticationMethod(),
350 toProperties( ldapConfiguration.getExtraProperties() ) );
351 ldapConnectionConfiguration.setSsl( ldapConfiguration.isSsl() );
353 ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
355 ldapConnection.close();
357 // verify groups dn value too
359 ldapConnectionConfiguration =
360 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
361 ldapConfiguration.getBaseGroupsDn(),
362 ldapConfiguration.getContextFactory(), ldapConfiguration.getBindDn(),
363 ldapConfiguration.getPassword(),
364 ldapConfiguration.getAuthenticationMethod(),
365 toProperties( ldapConfiguration.getExtraProperties() ) );
367 ldapConnectionConfiguration.setSsl( ldapConfiguration.isSsl() );
369 ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
371 catch ( InvalidNameException e )
373 log.warn( "fail to get ldapConnection: {}", e.getMessage(), e );
374 throw new ArchivaRestServiceException( e.getMessage(), e );
376 catch ( LdapException e )
378 log.warn( "fail to get ldapConnection: {}", e.getMessage(), e );
379 throw new ArchivaRestServiceException( e.getMessage(), e );
381 catch ( NamingException e )
383 log.error( "Could not close connection: {}", e.getMessage( ), e );
388 if ( ldapConnection != null )
392 ldapConnection.close();
394 catch ( NamingException e )
396 log.error( "Could not close connection: {}", e.getMessage( ), e );
404 private Properties toProperties( Map<String, String> map )
406 Properties properties = new Properties();
407 if ( map == null || map.isEmpty() )
411 for ( Map.Entry<String, String> entry : map.entrySet() )
413 properties.put( entry.getKey(), entry.getValue() );