]> source.dussan.org Git - archiva.git/blob
81b58aa946c97009bd2916a3a35a0510f1709228
[archiva.git] /
1 package org.apache.archiva.rest.services;
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
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;
46
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;
55 import java.util.Map;
56 import java.util.Properties;
57
58 /**
59  * @author Olivier Lamy
60  * @since 1.4-M4
61  */
62 @Service("redbackRuntimeConfigurationService#rest")
63 public class DefaultRedbackRuntimeConfigurationService
64     extends AbstractRestService
65     implements RedbackRuntimeConfigurationService
66 {
67
68     @Inject
69     private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
70
71     @Inject
72     @Named(value = "userManager#default")
73     private UserManager userManager;
74
75     @Inject
76     @Named(value = "rbacManager#default")
77     private RBACManager rbacManager;
78
79     @Inject
80     private RoleManager roleManager;
81
82     @Inject
83     private ApplicationContext applicationContext;
84
85     @Inject
86     @Named(value = "ldapConnectionFactory#configurable")
87     private LdapConnectionFactory ldapConnectionFactory;
88
89     @Inject
90     @Named(value = "cache#users")
91     private Cache usersCache;
92
93     @Inject
94     private LdapUserMapper ldapUserMapper;
95
96
97     @Override
98     public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
99         throws ArchivaRestServiceException
100     {
101         try
102         {
103             RedbackRuntimeConfiguration redbackRuntimeConfiguration =
104                 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
105
106             log.debug( "getRedbackRuntimeConfiguration -> {}", redbackRuntimeConfiguration );
107
108             return redbackRuntimeConfiguration;
109         }
110         catch ( RepositoryAdminException e )
111         {
112             throw new ArchivaRestServiceException( e.getMessage(), e );
113         }
114     }
115
116     @Override
117     public Boolean updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
118         throws ArchivaRestServiceException
119     {
120         try
121         {
122             // has user manager impl changed ?
123             boolean userManagerChanged = redbackRuntimeConfiguration.getUserManagerImpls().size()
124                 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().size();
125
126             userManagerChanged =
127                 userManagerChanged || ( redbackRuntimeConfiguration.getUserManagerImpls().toString().hashCode()
128                     != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().toString().hashCode() );
129
130             boolean rbacManagerChanged = redbackRuntimeConfiguration.getRbacManagerImpls().size()
131                 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls().size();
132
133             rbacManagerChanged =
134                 rbacManagerChanged || ( redbackRuntimeConfiguration.getRbacManagerImpls().toString().hashCode()
135                     != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls().toString().hashCode() );
136
137             boolean ldapConfigured = false;
138             for (String um : redbackRuntimeConfiguration.getUserManagerImpls()) {
139                 if (um.contains("ldap")) {
140                     ldapConfigured=true;
141                 }
142             }
143             if (!ldapConfigured) {
144                 for (String rbm : redbackRuntimeConfiguration.getRbacManagerImpls()) {
145                     if (rbm.contains("ldap")) {
146                         ldapConfigured = true;
147                     }
148                 }
149             }
150
151             redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
152
153             if ( userManagerChanged )
154             {
155                 log.info( "user managerImpls changed to {} so reload it",
156                           redbackRuntimeConfiguration.getUserManagerImpls() );
157                 userManager.initialize();
158             }
159
160             if ( rbacManagerChanged )
161             {
162                 log.info( "rbac manager changed to {} so reload it",
163                           redbackRuntimeConfiguration.getRbacManagerImpls() );
164                 rbacManager.initialize();
165                 roleManager.initialize();
166             }
167
168             if (ldapConfigured) {
169                 try {
170                     ldapConnectionFactory.initialize();
171                 } catch (Exception e) {
172                     ArchivaRestServiceException newEx = new ArchivaRestServiceException(e.getMessage(), e);
173                     newEx.setErrorKey("error.ldap.connectionFactory.init.failed");
174                     throw newEx;
175                 }
176             }
177             Collection<PasswordRule> passwordRules = applicationContext.getBeansOfType( PasswordRule.class ).values();
178
179             for ( PasswordRule passwordRule : passwordRules )
180             {
181                 passwordRule.initialize();
182             }
183
184             Collection<CookieSettings> cookieSettingsList =
185                 applicationContext.getBeansOfType( CookieSettings.class ).values();
186
187             for ( CookieSettings cookieSettings : cookieSettingsList )
188             {
189                 cookieSettings.initialize();
190             }
191
192             Collection<Authenticator> authenticators =
193                 applicationContext.getBeansOfType( Authenticator.class ).values();
194
195             for ( Authenticator authenticator : authenticators )
196             {
197                 try {
198                     log.debug("Initializing authenticatior "+authenticator.getId());
199                     authenticator.initialize();
200                 } catch (Exception e) {
201                     log.error("Initialization of authenticator failed "+authenticator.getId(),e);
202                 }
203             }
204
205             // users cache
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() );
214
215             if (ldapConfigured) {
216                 try {
217                     ldapUserMapper.initialize();
218                 } catch (Exception e) {
219                     ArchivaRestServiceException newEx = new ArchivaRestServiceException(e.getMessage(), e);
220                     newEx.setErrorKey("error.ldap.userMapper.init.failed");
221                     throw newEx;
222                 }
223             }
224
225
226             return Boolean.TRUE;
227         }
228         catch (ArchivaRestServiceException e) {
229             log.error(e.getMessage(), e);
230             throw e;
231         } catch ( Exception e )
232         {
233             log.error( e.getMessage(), e );
234             throw new ArchivaRestServiceException(e.getMessage(), e);
235         }
236     }
237
238     @Override
239     public List<UserManagerImplementationInformation> getUserManagerImplementationInformations()
240         throws ArchivaRestServiceException
241     {
242
243         Map<String, UserManager> beans = applicationContext.getBeansOfType( UserManager.class );
244
245         if ( beans.isEmpty() )
246         {
247             return Collections.emptyList();
248         }
249
250         List<UserManagerImplementationInformation> informations = new ArrayList<>( beans.size() );
251
252         for ( Map.Entry<String, UserManager> entry : beans.entrySet() )
253         {
254             UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
255             if ( userManager.isFinalImplementation() )
256             {
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 );
262             }
263         }
264
265         return informations;
266     }
267
268     @Override
269     public List<RBACManagerImplementationInformation> getRbacManagerImplementationInformations()
270         throws ArchivaRestServiceException
271     {
272         Map<String, RBACManager> beans = applicationContext.getBeansOfType( RBACManager.class );
273
274         if ( beans.isEmpty() )
275         {
276             return Collections.emptyList();
277         }
278
279         List<RBACManagerImplementationInformation> informations = new ArrayList<>( beans.size() );
280
281         for ( Map.Entry<String, RBACManager> entry : beans.entrySet() )
282         {
283             RBACManager rbacManager = applicationContext.getBean( entry.getKey(), RBACManager.class );
284             if ( rbacManager.isFinalImplementation() )
285             {
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 );
291             }
292         }
293
294         return informations;
295     }
296
297     @Override
298     public RedbackImplementationsInformations getRedbackImplementationsInformations()
299         throws ArchivaRestServiceException
300     {
301         return new RedbackImplementationsInformations( getUserManagerImplementationInformations(),
302                                                        getRbacManagerImplementationInformations() );
303     }
304
305     @Override
306     public Boolean checkLdapConnection()
307         throws ArchivaRestServiceException
308     {
309         LdapConnection ldapConnection = null;
310         try
311         {
312             ldapConnection = ldapConnectionFactory.getConnection();
313         }
314         catch ( LdapException e )
315         {
316             log.warn( "fail to get ldapConnection: {}", e.getMessage(), e );
317             throw new ArchivaRestServiceException( e.getMessage(), e );
318         }
319         finally
320         {
321
322             if ( ldapConnection != null )
323             {
324                 try
325                 {
326                     ldapConnection.close();
327                 }
328                 catch ( NamingException e )
329                 {
330                     log.error( "Could not close connection: {}", e.getMessage( ), e );
331                 }
332             }
333         }
334
335         return Boolean.TRUE;
336     }
337
338     @Override
339     public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
340         throws ArchivaRestServiceException
341     {
342         LdapConnection ldapConnection = null;
343         try
344         {
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() );
352
353             ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
354
355             ldapConnection.close();
356
357             // verify groups dn value too
358
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() ) );
366
367             ldapConnectionConfiguration.setSsl( ldapConfiguration.isSsl() );
368
369             ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
370         }
371         catch ( InvalidNameException e )
372         {
373             log.warn( "fail to get ldapConnection: {}", e.getMessage(), e );
374             throw new ArchivaRestServiceException( e.getMessage(), e );
375         }
376         catch ( LdapException e )
377         {
378             log.warn( "fail to get ldapConnection: {}", e.getMessage(), e );
379             throw new ArchivaRestServiceException( e.getMessage(), e );
380         }
381         catch ( NamingException e )
382         {
383             log.error( "Could not close connection: {}", e.getMessage( ), e );
384         }
385         finally
386         {
387
388             if ( ldapConnection != null )
389             {
390                 try
391                 {
392                     ldapConnection.close();
393                 }
394                 catch ( NamingException e )
395                 {
396                     log.error( "Could not close connection: {}", e.getMessage( ), e );
397                 }
398             }
399         }
400
401         return Boolean.TRUE;
402     }
403
404     private Properties toProperties( Map<String, String> map )
405     {
406         Properties properties = new Properties();
407         if ( map == null || map.isEmpty() )
408         {
409             return properties;
410         }
411         for ( Map.Entry<String, String> entry : map.entrySet() )
412         {
413             properties.put( entry.getKey(), entry.getValue() );
414         }
415         return properties;
416     }
417
418 }
419
420