Merged 5.2.N-AUDIT-API (5.2.2) to 5.2.N (5.2.2)

137955 jvonka: REPO-2643 - add "clearAuditByIdRange" to underlying Audit Service/Component/DAO 
   - to enable deletion of audit entries within an audit app based on id range (fromId / toId) in addition to time range
   - required for REST API Delete Application Audit Entries (REPO-1519)


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@137982 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jan Vonka
2017-07-12 12:12:13 +00:00
parent c9806ab898
commit 5730cefc7c
9 changed files with 370 additions and 217 deletions

View File

@@ -149,6 +149,12 @@
<if test="auditToTime != null">
<![CDATA[and audit_time < #{auditToTime}]]>
</if>
<if test="auditFromId != null">
<![CDATA[and id >= #{auditFromId}]]>
</if>
<if test="auditToId != null">
<![CDATA[and id < #{auditToId}]]>
</if>
</where>
</delete>

View File

@@ -108,7 +108,7 @@ public interface AuditComponent
/**
* Delete audit entries for the given application and time range
*
* @param applicationName the name of the application being logged to
* @param applicationName the name of the application
* @param fromTime the start time of entries to remove (inclusive and optional)
* @param toTime the end time of entries to remove (exclusive and optional)
* @return Returns the number of entries deleted
@@ -117,6 +117,18 @@ public interface AuditComponent
*/
int deleteAuditEntries(String applicationName, Long fromTime, Long toTime);
/**
* Delete audit entries for the given application and id range
*
* @param applicationName the name of the application
* @param fromId the start time of entries to remove (inclusive and optional)
* @param toId the end time of entries to remove (exclusive and optional)
* @return Returns the number of entries deleted
*
* @since 5.2.2
*/
int deleteAuditEntriesByIdRange(String applicationName, Long fromId, Long toId);
/**
* Delete a discrete list of audit entries based on ID
*

View File

@@ -148,21 +148,12 @@ public class AuditComponentImpl implements AuditComponent
*/
public int deleteAuditEntries(String applicationName, Long fromTime, Long toTime)
{
ParameterCheck.mandatory("applicationName", applicationName);
AlfrescoTransactionSupport.checkTransactionReadState(true);
AuditApplication application = auditModelRegistry.getAuditApplicationByName(applicationName);
if (application == null)
Long applicationId = getApplicationId(applicationName);
if (applicationId == null)
{
if (logger.isDebugEnabled())
{
logger.debug("No audit application named '" + applicationName + "' has been registered.");
}
return 0;
}
Long applicationId = application.getApplicationId();
int deleted = auditDAO.deleteAuditEntries(applicationId, fromTime, toTime);
// Done
if (logger.isDebugEnabled())
@@ -174,6 +165,47 @@ public class AuditComponentImpl implements AuditComponent
return deleted;
}
/**
* {@inheritDoc}
* @since 5.2.2
*/
public int deleteAuditEntriesByIdRange(String applicationName, Long fromId, Long toId)
{
Long applicationId = getApplicationId(applicationName);
if (applicationId == null)
{
return 0;
}
int deleted = auditDAO.deleteAuditEntriesByIdRange(applicationId, fromId, toId);
// Done
if (logger.isDebugEnabled())
{
logger.debug(
"Delete audit " + deleted + " entries for " + applicationName +
" (" + fromId + " to " + toId);
}
return deleted;
}
private Long getApplicationId(String applicationName)
{
ParameterCheck.mandatory("applicationName", applicationName);
AlfrescoTransactionSupport.checkTransactionReadState(true);
AuditApplication application = auditModelRegistry.getAuditApplicationByName(applicationName);
if (application == null)
{
if (logger.isDebugEnabled())
{
logger.debug("No audit application named '" + applicationName + "' has been registered.");
}
return null;
}
return application.getApplicationId();
}
/**
* {@inheritDoc}
* @since 3.2

View File

@@ -140,6 +140,16 @@ public class AuditServiceImpl implements AuditService
return auditComponent.deleteAuditEntries(applicationName, fromTime, toTime);
}
/**
* {@inheritDoc}
* @since 5.2.2
*/
@Override
public int clearAuditByIdRange(String applicationName, Long fromId, Long toId)
{
return auditComponent.deleteAuditEntriesByIdRange(applicationName, fromId, toId);
}
/**
* {@inheritDoc}
* @since 3.4

View File

@@ -165,14 +165,26 @@ public interface AuditDAO
/**
* Delete audit entries for the application, possibly limiting the time range.
*
* @param applicationId and existing audit application ID
* @param from the minimum entry time (inclusive, optional)
* @param to the maximum entry time (exclusive, optional)
* @param applicationId an existing audit application ID
* @param fromTime the minimum entry time (inclusive, optional)
* @param toTime the maximum entry time (exclusive, optional)
* @return Returns the number of entries deleted
*
* @since 3.2
*/
int deleteAuditEntries(Long applicationId, Long from, Long to);
int deleteAuditEntries(Long applicationId, Long fromTime, Long toTime);
/**
* Delete audit entries for the application for given id range.
*
* @param applicationId an existing audit application ID
* @param fromId the minimum fromId (inclusive, optional)
* @param toId the maximum toId (exclusive, optional)
* @return Returns the number of entries deleted
*
* @since 5.2.2
*/
int deleteAuditEntriesByIdRange(Long applicationId, Long fromId, Long toId);
/**
* Delete a discrete list of audit entries. Duplicate entries are collapsed

View File

@@ -39,6 +39,8 @@ public class AuditDeleteParameters
private Long auditApplicationId;
private Long auditFromTime;
private Long auditToTime;
private Long auditFromId;
private Long auditToId;
private List<Long> auditEntryIds;
public AuditDeleteParameters()
@@ -53,6 +55,8 @@ public class AuditDeleteParameters
.append("[ auditApplicationId=").append(auditApplicationId)
.append(", auditFromTime").append(auditFromTime == null ? null : new Date(auditFromTime))
.append(", auditToTime").append(auditToTime == null ? null : new Date(auditToTime))
.append(", auditFromId").append(auditFromId)
.append(", auditToId").append(auditToId)
.append(", auditEntryIds").append(auditEntryIds == null ? null : auditEntryIds.size())
.append("]");
return sb.toString();
@@ -88,6 +92,26 @@ public class AuditDeleteParameters
this.auditToTime = auditToTime;
}
public Long getAuditFromId()
{
return auditFromId;
}
public void setAuditFromId(Long auditFromId)
{
this.auditFromId = auditFromId;
}
public Long getAuditToId()
{
return auditToId;
}
public void setAuditToId(Long auditToId)
{
this.auditToId = auditToId;
}
public List<Long> getAuditEntryIds()
{
return auditEntryIds;

View File

@@ -181,6 +181,15 @@ public class AuditDAOImpl extends AbstractAuditDAOImpl
return template.delete(DELETE_ENTRIES, params);
}
public int deleteAuditEntriesByIdRange(Long applicationId, Long fromId, Long toId)
{
AuditDeleteParameters params = new AuditDeleteParameters();
params.setAuditApplicationId(applicationId);
params.setAuditFromId(fromId);
params.setAuditToId(toId);
return template.delete(DELETE_ENTRIES, params);
}
@Override
protected int deleteAuditEntriesImpl(List<Long> auditEntryIds)
{

View File

@@ -150,6 +150,18 @@ public interface AuditService
*/
int clearAudit(String applicationName, Long fromTime, Long toTime);
/**
* Remove audit entries for the given application between the audit entry ids.
*
* @param applicationName the name of the application for which to remove entries
* @param fromId the start time of entries to remove (inclusive and optional)
* @param toId the end time of entries to remove (exclusive and optional)
* @return Returns the number of audit entries deleted
*
* @since 5.2.2
*/
int clearAuditByIdRange(String applicationName, Long fromId, Long toId);
/**
* Delete a discrete list of audit entries.
* <p/>

View File

@@ -685,11 +685,31 @@ public class AuditComponentTest extends TestCase
logger.debug(sb.toString());
assertEquals("Incorrect number of audit entries after failed login", iterations, results.size());
// Check that we can delete explicit entries
Collections.sort(results);
long minId = results.get(0);
long maxId = results.get(100);
List<Long> remainingResults = new ArrayList<>(results.subList(100, results.size()));
// Check that we can delete entries based on range of ids
long before = System.currentTimeMillis();
deleteAuditEntries(results);
int deleted = deleteAuditEntries(APPLICATION_API_TEST, minId, maxId);
System.out.println(
"Clearing " + results.size() + " entries by ID took " + (System.currentTimeMillis() - before) + "ms.");
"Clearing " + deleted + " entries by from/to ID took " + (System.currentTimeMillis() - before) + "ms.");
results.clear();
sb.delete(0, sb.length());
queryAuditLog(auditQueryCallback, params, Integer.MAX_VALUE);
logger.debug(sb.toString());
assertEquals("Range of audit entries were not deleted", remainingResults.size(), results.size());
// delete the rest ...
// Check that we can delete set of explicit entries
before = System.currentTimeMillis();
deleteAuditEntries(remainingResults);
System.out.println(
"Clearing " + remainingResults.size() + " entries by set of IDs took " + (System.currentTimeMillis() - before) + "ms.");
results.clear();
sb.delete(0, sb.length());
queryAuditLog(auditQueryCallback, params, Integer.MAX_VALUE);
@@ -1081,7 +1101,7 @@ public class AuditComponentTest extends TestCase
}
/**
* Clearn the audit log as 'admin'
* Clear the audit log as 'admin'
*/
private void clearAuditLog(final String applicationName)
{
@@ -1098,7 +1118,7 @@ public class AuditComponentTest extends TestCase
}
/**
* Clearn the audit log as 'admin'
* Clear the audit log as 'admin'
*/
private void deleteAuditEntries(final List<Long> auditEntryIds)
{
@@ -1115,7 +1135,23 @@ public class AuditComponentTest extends TestCase
}
/**
* Clearn the audit log as 'admin'
* Clear the audit log as 'admin'
*/
private Integer deleteAuditEntries(final String applicationName, final long fromId, final long toId)
{
RunAsWork<Integer> work = new RunAsWork<Integer>()
{
@Override
public Integer doWork() throws Exception
{
return new Integer(auditService.clearAuditByIdRange(applicationName, fromId, toId));
}
};
return AuthenticationUtil.runAs(work, AuthenticationUtil.getAdminRoleName());
}
/**
* Query the audit log as 'admin'
*/
private void queryAuditLog(final AuditQueryCallback callback, final AuditQueryParameters parameters, final int maxResults)
{