This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.web.security;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020
021import org.apache.archiva.admin.model.RepositoryAdminException;
022import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
023import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
024import org.apache.archiva.redback.rbac.RBACManager;
025import org.apache.archiva.redback.rbac.RbacManagerException;
026import org.apache.archiva.redback.rbac.UserAssignment;
027import org.apache.archiva.redback.system.check.EnvironmentCheck;
028import org.apache.archiva.redback.users.User;
029import org.apache.archiva.redback.users.UserManager;
030import org.apache.archiva.redback.users.UserManagerException;
031import org.apache.archiva.redback.users.UserNotFoundException;
032import org.apache.commons.lang3.StringUtils;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035import org.springframework.context.ApplicationContext;
036import org.springframework.stereotype.Service;
037
038import javax.annotation.PostConstruct;
039import javax.inject.Inject;
040import javax.inject.Named;
041import java.util.ArrayList;
042import java.util.List;
043
044/**
045 * @author Olivier Lamy
046 */
047@Service( "environmentCheck#archiva-locked-admin-check" )
048public class ArchivaLockedAdminEnvironmentCheck
049    implements EnvironmentCheck
050{
051
052    protected Logger log = LoggerFactory.getLogger( getClass() );
053
054
055    @Inject
056    @Named( value = "rbacManager#cached" )
057    private RBACManager rbacManager;
058
059    /**
060     * boolean detailing if this environment check has been executed
061     */
062    private boolean checked = false;
063
064    @Inject
065    private ApplicationContext applicationContext;
066
067    @Inject
068    private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
069
070    private List<UserManager> userManagers;
071
072    @PostConstruct
073    protected void initialize()
074        throws RepositoryAdminException
075    {
076        List<String> userManagerImpls =
077            redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls();
078
079        List<String> updated = new ArrayList<>(  );
080        userManagers = new ArrayList<>( userManagerImpls.size() );
081
082        for ( String beanId : userManagerImpls )
083        {
084            // for migration purpose to help users
085            if ( StringUtils.equalsIgnoreCase( beanId, "jdo" ))
086            {
087                log.info( "jdo is not anymore supported we auto update to jpa" );
088                beanId = "jpa";
089            }
090            updated.add( beanId );
091            userManagers.add( applicationContext.getBean( "userManager#" + beanId, UserManager.class ) );
092        }
093        redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().setUserManagerImpls( updated );
094    }
095
096    /**
097     * This environment check will unlock system administrator accounts that are locked on the restart of the
098     * application when the environment checks are processed.
099     *
100     * @param violations
101     */
102    @Override
103    public void validateEnvironment( List<String> violations )
104    {
105        if ( !checked )
106        {
107
108            for ( UserManager userManager : userManagers )
109            {
110                if ( userManager.isReadOnly() )
111                {
112                    continue;
113                }
114                List<String> roles = new ArrayList<>();
115                roles.add( RedbackRoleConstants.SYSTEM_ADMINISTRATOR_ROLE );
116
117                List<? extends UserAssignment> systemAdminstrators;
118                try
119                {
120                    systemAdminstrators = rbacManager.getUserAssignmentsForRoles( roles );
121
122                    for ( UserAssignment userAssignment : systemAdminstrators )
123                    {
124                        try
125                        {
126                            User admin = userManager.findUser( userAssignment.getPrincipal() );
127
128                            if ( admin.isLocked() )
129                            {
130                                log.info( "Unlocking system administrator: {}", admin.getUsername() );
131                                admin.setLocked( false );
132                                userManager.updateUser( admin );
133                            }
134                        }
135                        catch ( UserNotFoundException ne )
136                        {
137                            log.warn( "Dangling UserAssignment -> {}", userAssignment.getPrincipal() );
138                        }
139                        catch ( UserManagerException e )
140                        {
141                            log.warn( "fail to find user {} for admin unlock check: {}", userAssignment.getPrincipal(),
142                                      e.getMessage() );
143                        }
144                    }
145                }
146                catch ( RbacManagerException e )
147                {
148                    log.warn( "Exception when checking for locked admin user: {}", e.getMessage(), e );
149                }
150
151                checked = true;
152            }
153
154        }
155
156    }
157}