--- /dev/null
+package org.apache.archiva.rest.services.interceptors;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.ext.Provider;
+import java.io.IOException;
+
+/**
+ * @since
+ */
+@Service("auditInfoFilter#rest")
+@Provider
+public class AuditInfoFilter implements ContainerRequestFilter
+{
+
+ private static final Logger log = LoggerFactory.getLogger( AuditInfoFilter.class );
+
+ @Context
+ private HttpServletRequest servletRequest;
+
+ private static final AuditInfoThreadLocal auditInfoThreadLocal = new AuditInfoThreadLocal();
+
+ public AuditInfoFilter() {
+
+ }
+
+ public static class AuditInfoThreadLocal extends ThreadLocal<AuditInfo> {
+
+ public AuditInfoThreadLocal() {
+
+ }
+
+ @Override
+ protected AuditInfo initialValue( )
+ {
+ return new AuditInfo();
+ }
+ }
+
+ public static class AuditInfo {
+
+ private String remoteAddress = "0.0.0.0";
+ private String localAddress = "0.0.0.0";
+ private String remoteHost = "0.0.0.0";
+ private String protocol = "";
+ private int remotePort = 0;
+ private String method = "";
+
+ public AuditInfo() {
+
+ }
+
+ public String getRemoteAddress( )
+ {
+ return remoteAddress;
+ }
+
+ public void setRemoteAddress( String remoteAddress )
+ {
+ this.remoteAddress = remoteAddress;
+ }
+
+ public String getLocalAddress( )
+ {
+ return localAddress;
+ }
+
+ public void setLocalAddress( String localAddress )
+ {
+ this.localAddress = localAddress;
+ }
+
+ public String getRemoteHost( )
+ {
+ return remoteHost;
+ }
+
+ public void setRemoteHost( String remoteHost )
+ {
+ this.remoteHost = remoteHost;
+ }
+
+ public int getRemotePort( )
+ {
+ return remotePort;
+ }
+
+ public void setRemotePort( int remotePort )
+ {
+ this.remotePort = remotePort;
+ }
+
+ public String getMethod( )
+ {
+ return method;
+ }
+
+ public void setMethod( String method )
+ {
+ this.method = method;
+ }
+
+ public String getProtocol( )
+ {
+ return protocol;
+ }
+
+ public void setProtocol( String protocol )
+ {
+ this.protocol = protocol;
+ }
+ }
+
+
+
+ @Override
+ public void filter( ContainerRequestContext containerRequestContext ) throws IOException
+ {
+ if (log.isDebugEnabled())
+ {
+ log.debug( "Filter {}, {}", servletRequest.getRemoteAddr( ), servletRequest.getRemoteHost( ) );
+ }
+ AuditInfo auditInfo = auditInfoThreadLocal.get( );
+ auditInfo.setRemoteAddress( servletRequest.getRemoteAddr( ) );
+ auditInfo.setLocalAddress( servletRequest.getLocalAddr( ) );
+ auditInfo.setProtocol( servletRequest.getProtocol( ) );
+ auditInfo.setRemoteHost( servletRequest.getRemoteHost( ) );
+ auditInfo.setRemotePort( servletRequest.getRemotePort( ) );
+ auditInfo.setMethod( containerRequestContext.getMethod( ) );
+ }
+
+ public static AuditInfo getAuditInfo() {
+ return auditInfoThreadLocal.get( );
+ }
+}
import org.apache.archiva.admin.model.RepositoryAdminException;
import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
+import org.apache.archiva.metadata.model.facets.AuditEvent;
import org.apache.archiva.redback.authentication.AbstractAuthenticator;
import org.apache.archiva.redback.authentication.AuthenticationConstants;
import org.apache.archiva.redback.authentication.AuthenticationDataSource;
import org.apache.archiva.redback.users.User;
import org.apache.archiva.redback.users.UserManager;
import org.apache.archiva.redback.users.UserNotFoundException;
+import org.apache.archiva.repository.events.AuditListener;
+import org.apache.archiva.rest.services.interceptors.AuditInfoFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
@Inject
private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
+ @Inject
+ private List<AuditListener> auditListeners = new ArrayList<>();
+
private List<UserManager> userManagers;
private boolean valid = false;
}
}
+ protected AuditInfoFilter.AuditInfo getAuditInformation()
+ {
+ return AuditInfoFilter.getAuditInfo( );
+ }
+
+ public List<AuditListener> getAuditListeners()
+ {
+ return auditListeners;
+ }
+
+ protected void triggerAuditEvent( String repositoryId, String filePath, String action, String user )
+ {
+ AuditEvent auditEvent = new AuditEvent( repositoryId, user, filePath, action );
+ AuditInfoFilter.AuditInfo auditInformation = getAuditInformation();
+ auditEvent.setUserId( user );
+ auditEvent.setRemoteIP( auditInformation.getRemoteHost() + ":" + auditInformation.getRemotePort() );
+ for ( AuditListener auditListener : getAuditListeners() )
+ {
+ auditListener.auditEvent( auditEvent );
+ }
+ }
@Override
public AuthenticationResult authenticate( AuthenticationDataSource ds )
Exception resultException = null;
PasswordBasedAuthenticationDataSource source = (PasswordBasedAuthenticationDataSource) ds;
List<AuthenticationFailureCause> authnResultErrors = new ArrayList<>();
+ final String loginUserId = source.getUsername( );
for ( UserManager userManager : userManagers )
{
try
{
log.debug( "Authenticate: {} with userManager: {}", source, userManager.getId() );
- User user = userManager.findUser( source.getUsername() );
+ User user = userManager.findUser( loginUserId );
username = user.getUsername();
if ( user.isLocked() )
{
//throw new AccountLockedException( "Account " + source.getUsername() + " is locked.", user );
AccountLockedException e =
- new AccountLockedException( "Account " + source.getUsername() + " is locked.", user );
+ new AccountLockedException( "Account " + loginUserId + " is locked.", user );
log.warn( "{}", e.getMessage() );
+ triggerAuditEvent( "", "", "login-account-locked", loginUserId );
resultException = e;
authnResultErrors.add(
new AuthenticationFailureCause( AuthenticationConstants.AUTHN_LOCKED_USER_EXCEPTION,
MustChangePasswordException e = new MustChangePasswordException( "Password expired.", user );
log.warn( "{}", e.getMessage() );
resultException = e;
+ triggerAuditEvent( "", "", "login-password-change-required", loginUserId );
authnResultErrors.add(
new AuthenticationFailureCause( AuthenticationConstants.AUTHN_MUST_CHANGE_PASSWORD_EXCEPTION,
e.getMessage() ) );
boolean isPasswordValid = encoder.isPasswordValid( user.getEncodedPassword(), source.getPassword() );
if ( isPasswordValid )
{
- log.debug( "User {} provided a valid password", source.getUsername() );
+ log.debug( "User {} provided a valid password", loginUserId );
try
{
securityPolicy.extensionPasswordExpiration( user );
authenticationSuccess = true;
+ triggerAuditEvent( "", "", "login-success", loginUserId );
+
//REDBACK-151 do not make unnessesary updates to the user object
if ( user.getCountFailedLoginAttempts() > 0 )
}
}
- return new AuthenticationResult( true, source.getUsername(), null );
+ return new AuthenticationResult( true, loginUserId, null );
}
catch ( MustChangePasswordException e )
{
user.setPasswordChangeRequired( true );
+ triggerAuditEvent( "", "", "login-password-change-required", loginUserId );
//throw e;
resultException = e;
authnResultErrors.add( new AuthenticationFailureCause(
{
log.warn( "Password is Invalid for user {} and userManager '{}'.", source.getUsername(),
userManager.getId() );
+ triggerAuditEvent( "", "", "login-authentication-failed", loginUserId );
+
authnResultErrors.add( new AuthenticationFailureCause( AuthenticationConstants.AUTHN_NO_SUCH_USER,
"Password is Invalid for user "
+ source.getUsername() + "." ).user( user ) );
}
catch ( UserNotFoundException e )
{
- log.warn( "Login for user {} and userManager {} failed. user not found.", source.getUsername(),
+ log.warn( "Login for user {} and userManager {} failed. user not found.", loginUserId,
userManager.getId() );
resultException = e;
+ triggerAuditEvent( "", "", "login-user-unknown", loginUserId );
authnResultErrors.add( new AuthenticationFailureCause( AuthenticationConstants.AUTHN_NO_SUCH_USER,
"Login for user " + source.getUsername()
+ " failed. user not found." ) );
}
catch ( Exception e )
{
- log.warn( "Login for user {} and userManager {} failed, message: {}", source.getUsername(),
+ log.warn( "Login for user {} and userManager {} failed, message: {}", loginUserId,
userManager.getId(), e.getMessage() );
resultException = e;
+ triggerAuditEvent( "", "", "login-error", loginUserId );
authnResultErrors.add( new AuthenticationFailureCause( AuthenticationConstants.AUTHN_RUNTIME_EXCEPTION,
"Login for user " + source.getUsername()
+ " failed, message: " + e.getMessage() ) );