1 package org.apache.archiva.redback.integration.checks.security;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.redback.authentication.PasswordBasedAuthenticationDataSource;
23 import org.apache.archiva.redback.configuration.UserConfiguration;
24 import org.apache.archiva.redback.configuration.UserConfigurationKeys;
25 import org.apache.archiva.redback.rbac.RBACManager;
26 import org.apache.archiva.redback.role.RoleManager;
27 import org.apache.archiva.redback.role.RoleManagerException;
28 import org.apache.archiva.redback.system.SecuritySession;
29 import org.apache.archiva.redback.system.SecuritySystem;
30 import org.apache.archiva.redback.system.check.EnvironmentCheck;
31 import org.apache.archiva.redback.users.User;
32 import org.apache.archiva.redback.users.UserManager;
33 import org.apache.archiva.redback.users.UserNotFoundException;
34 import org.apache.commons.io.IOUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Service;
40 import javax.inject.Inject;
41 import javax.inject.Named;
43 import java.io.FileInputStream;
44 import java.util.Date;
45 import java.util.List;
46 import java.util.Properties;
49 * @author Olivier Lamy
52 @Service( "environmentCheck#adminAutoCreateCheck" )
53 public class AdminAutoCreateCheck
54 implements EnvironmentCheck
57 private Logger log = LoggerFactory.getLogger( getClass() );
59 public static final String FORCE_ADMIN_FILE_PATH = "redback.admin.creation.file";
61 public static final String ADMIN_FULL_NAME_KEY = "redback.admin.fullname";
63 public static final String ADMIN_EMAIL_KEY = "redback.admin.email";
65 public static final String ADMIN_PASSWORD_KEY = "redback.admin.password";
68 @Named( value = "userManager#configurable" )
69 private UserManager userManager;
72 @Named( value = "userConfiguration#default" )
73 private UserConfiguration config;
76 protected SecuritySystem securitySystem;
79 private RoleManager roleManager;
82 @Named( value = "rBACManager#cached" )
83 private RBACManager rbacManager;
85 public void validateEnvironment( List<String> violations )
89 User user = userManager.findUser( getAdminUid() );
92 useForceAdminCreationFile();
97 catch ( UserNotFoundException e )
99 useForceAdminCreationFile();
103 private void useForceAdminCreationFile()
107 String forceAdminFilePath = System.getProperty( FORCE_ADMIN_FILE_PATH );
108 if ( StringUtils.isBlank( forceAdminFilePath ) )
110 log.info( "{} system props is empty don't use an auto creation admin ", FORCE_ADMIN_FILE_PATH );
113 File file = new File( forceAdminFilePath );
114 if ( !file.exists() )
116 log.warn( "file set in sysprops {} not exists skip admin auto creation", FORCE_ADMIN_FILE_PATH );
119 log.debug( "user {} not found try auto creation", getAdminUid() );
120 Properties properties = new Properties();
121 FileInputStream fis = new FileInputStream( file );
124 properties.load( fis );
126 catch ( Exception e )
128 log.warn( "error loading properties from file {} skip admin auto creation", forceAdminFilePath );
133 IOUtils.closeQuietly( fis );
136 // ensure we have all properties
137 String password = properties.getProperty( ADMIN_PASSWORD_KEY );
138 String email = properties.getProperty( ADMIN_EMAIL_KEY );
139 String fullName = properties.getProperty( ADMIN_FULL_NAME_KEY );
141 if ( StringUtils.isBlank( password ) )
143 log.warn( "property {} not set skip auto admin creation", ADMIN_PASSWORD_KEY );
147 if ( StringUtils.isBlank( email ) )
149 log.warn( "property not set skip auto admin creation", ADMIN_EMAIL_KEY );
153 if ( StringUtils.isBlank( fullName ) )
155 log.warn( "property {} not set skip auto admin creation", ADMIN_FULL_NAME_KEY );
159 User u = userManager.createUser( getAdminUid(), fullName, email );
161 u.setPassword( password );
162 u.setLocked( false );
163 u.setPasswordChangeRequired( false );
164 u.setPermanent( true );
165 u.setValidated( true );
167 u = userManager.addUser( u );
168 u.setPassword( password );
170 PasswordBasedAuthenticationDataSource authdatasource = new PasswordBasedAuthenticationDataSource();
171 authdatasource.setPrincipal( u.getUsername() );
172 authdatasource.setPassword( u.getPassword() );
173 SecuritySession securitySession = securitySystem.authenticate( authdatasource );
174 if ( securitySession.getAuthenticationResult().isAuthenticated() )
176 // good add various tokens.
177 u = securitySession.getUser();
178 u.setLastLoginDate( new Date() );
179 securitySystem.getUserManager().updateUser( u );
181 assignAdminRole( u );
184 catch ( Exception e )
186 log.warn( "failed to automatically create an admin account {}", e.getMessage(), e );
190 private void assignAdminRole( User user )
191 throws RoleManagerException
193 roleManager.assignRole( "system-administrator", user.getUsername() );
196 private String getAdminUid()
198 return config.getString( UserConfigurationKeys.DEFAULT_ADMIN );