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