Skip to content

Commit

Permalink
Add visibility log for clean up services. #1364
Browse files Browse the repository at this point in the history
  • Loading branch information
syntrydy committed Nov 21, 2018
1 parent 3b1cb72 commit 6dfdfa3
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 176 deletions.
Expand Up @@ -9,6 +9,7 @@
import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
Expand Down Expand Up @@ -39,7 +40,6 @@
import org.xdi.config.oxtrust.AppConfiguration;
import org.xdi.model.SmtpConfiguration;
import org.xdi.service.MailService;
import org.xdi.util.StringHelper;

/**
* User: Dejan Maric
Expand Down Expand Up @@ -127,7 +127,7 @@ public String requestReminderImpl() throws Exception {
passwordResetService.prepareBranch();
PasswordResetRequest request = new PasswordResetRequest();
String guid = passwordResetService.generateGuidForNewPasswordResetRequest();
request.setCreationDate(Calendar.getInstance().getTime());
request.setCreationDate(Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime());
request.setPersonInum(matchedPersons.get(0).getInum());
request.setOxGuid(guid);
request.setDn(passwordResetService.getDnForPasswordResetRequest(guid));
Expand Down
Expand Up @@ -82,7 +82,7 @@ public class PasswordResetAction implements Serializable {

@Inject
private AppConfiguration appConfiguration;

@Inject
private JsonConfigurationService jsonConfigurationService;

Expand Down Expand Up @@ -161,14 +161,13 @@ public String start() throws ParseException {
}

protected void sendExpirationError() {
facesMessages.add(FacesMessage.SEVERITY_ERROR, "The reset link is no longer valid.\n\n "
+ "Re-enter your e-mail to generate a new link.");
facesMessages.add(FacesMessage.SEVERITY_ERROR,
"The reset link is no longer valid.\n\n " + "Re-enter your e-mail to generate a new link.");
conversationService.endConversation();
}

public String update() throws ParseException {
public String update() {
String outcome = updateImpl();

if (OxTrustConstants.RESULT_SUCCESS.equals(outcome)) {
facesMessages.add(FacesMessage.SEVERITY_INFO, "Password reset successful.");
conversationService.endConversation();
Expand All @@ -177,13 +176,12 @@ public String update() throws ParseException {
"Your secret answer or Captcha code may have been wrong. Please try to correct it or contact your administrator to change your password.");
conversationService.endConversation();
}

return outcome;
}

public String updateImpl() throws ParseException {
public String updateImpl() {
boolean valid = true;
if (recaptchaService.isEnabled()) {
if (recaptchaService.isEnabled() && getAuthenticationRecaptchaEnabled()) {
valid = recaptchaService.verifyRecaptchaResponse();
}

Expand All @@ -209,9 +207,12 @@ public String updateImpl() throws ParseException {
PasswordResetRequest removeRequest = new PasswordResetRequest();
removeRequest.setBaseDn(request.getBaseDn());
ldapEntryManager.remove(removeRequest);
oxTrustAuditService.audit("PASSWORD RESET REQUEST" + removeRequest.getBaseDn() + " REMOVED",
identity.getUser(),
(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest());
try {
oxTrustAuditService.audit("PASSWORD RESET REQUEST" + removeRequest.getBaseDn() + " REMOVED",
identity.getUser(),
(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest());
} catch (Exception e) {
}
if (question != null && answer != null) {
String correctAnswer = answer.getValue();
Boolean securityQuestionAnswered = (securityAnswer != null) && securityAnswer.equals(correctAnswer);
Expand Down Expand Up @@ -287,7 +288,7 @@ public String getConfirm() {
public void setConfirm(String confirm) {
this.confirm = confirm;
}

public boolean getAuthenticationRecaptchaEnabled() {
return jsonConfigurationService.getOxTrustappConfiguration().isAuthenticationRecaptchaEnabled();
}
Expand Down
31 changes: 31 additions & 0 deletions server/src/main/java/org/gluu/oxtrust/service/CleanUpLogger.java
@@ -0,0 +1,31 @@
package org.gluu.oxtrust.service;

import java.io.Serializable;

import javax.inject.Inject;

import org.slf4j.Logger;

public class CleanUpLogger implements Serializable {

/**
*
*/
private static final long serialVersionUID = 717536440969366892L;

@Inject
private Logger logger;

public void addNewLogLine(String message) {
logger.info(message);
}

public void addNewLogLineAsError(String message) {
logger.error(message);
}

public void addNewLogLineAsWarning(String message) {
logger.warn(message);
}

}
190 changes: 98 additions & 92 deletions server/src/main/java/org/gluu/oxtrust/service/CleanerTimer.java
Expand Up @@ -13,7 +13,6 @@
import javax.inject.Inject;
import javax.inject.Named;

import org.gluu.persist.PersistenceEntryManager;
import org.slf4j.Logger;
import org.xdi.config.oxtrust.AppConfiguration;
import org.xdi.model.ApplicationType;
Expand All @@ -35,95 +34,102 @@
@Named
public class CleanerTimer {

public final static int BATCH_SIZE = 100;
private final static int DEFAULT_INTERVAL = 600; // 10 minutes

@Inject
private Logger log;

@Inject
private PersistenceEntryManager ldapEntryManager;

@Inject
private PasswordResetService passwordResetService;

@Inject
private CacheService cacheService;

@Inject
private MetricService metricService;

@Inject
private AppConfiguration appConfiguration;

@Inject
private Event<TimerEvent> cleanerEvent;

private AtomicBoolean isActive;

public void initTimer() {
log.debug("Initializing Cleaner Timer");
this.isActive = new AtomicBoolean(false);

int interval = appConfiguration.getCleanServiceInterval();
if (interval <= 0) {
interval = DEFAULT_INTERVAL;
}

cleanerEvent.fire(new TimerEvent(new TimerSchedule(interval, interval), new CleanerEvent(), Scheduled.Literal.INSTANCE));
}

@Asynchronous
public void process(@Observes @Scheduled CleanerEvent cleanerEvent) {
if (this.isActive.get()) {
return;
}

if (!this.isActive.compareAndSet(false, true)) {
return;
}

try {
Date now = new Date();

processCache(now);
processPasswordReset();
processMetricEntries();
} finally {
this.isActive.set(false);
}
}

protected void processPasswordReset() {
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.add(Calendar.SECOND, -appConfiguration.getPasswordResetRequestExpirationTime());
final Date expirationDate = calendar.getTime();

passwordResetService.cleanup(expirationDate);
}

private void processCache(Date now) {
try {
if (cacheService.getCacheProvider() instanceof NativePersistenceCacheProvider) {
((NativePersistenceCacheProvider) cacheService.getCacheProvider()).cleanup(now, BATCH_SIZE);
}
} catch (Exception e) {
log.error("Failed to clean up cache.", e);
}
}

private void processMetricEntries() {
log.debug("Start metric entries clean up");

int keepDataDays = appConfiguration.getMetricReporterKeepDataDays();

Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.add(Calendar.DATE, -keepDataDays);
Date expirationDate = calendar.getTime();

metricService.removeExpiredMetricEntries(expirationDate, ApplicationType.OX_AUTH, metricService.applianceInum(), 0, BATCH_SIZE);

log.debug("End metric entries clean up");
}

public final static int BATCH_SIZE = 100;
private final static int DEFAULT_INTERVAL = 600; // 10 minutes

@Inject
private Logger log;

@Inject
private PasswordResetService passwordResetService;

@Inject
private CacheService cacheService;

@Inject
private MetricService metricService;

@Inject
private AppConfiguration appConfiguration;

@Inject
private Event<TimerEvent> cleanerEvent;

@Inject
private CleanUpLogger cleanUpLogger;

private AtomicBoolean isActive;

public void initTimer() {
log.debug("Initializing Cleaner Timer");
cleanUpLogger.addNewLogLine("Initializing Cleaner Timer at:" + new Date());
this.isActive = new AtomicBoolean(false);
int interval = appConfiguration.getCleanServiceInterval();
if (interval <= 0) {
interval = DEFAULT_INTERVAL;
}
cleanerEvent.fire(
new TimerEvent(new TimerSchedule(interval, interval), new CleanerEvent(), Scheduled.Literal.INSTANCE));
cleanUpLogger.addNewLogLine("Initialization Done at :" + new Date());
}

@Asynchronous
public void process(@Observes @Scheduled CleanerEvent cleanerEvent) {
cleanUpLogger.addNewLogLine("++++Starting processing clean up services at:" + new Date());
if (this.isActive.get()) {
return;
}
if (!this.isActive.compareAndSet(false, true)) {
return;
}
try {
Date now = new Date();
processCache(now);
processPasswordReset();
processMetricEntries();
} finally {
this.isActive.set(false);
}
cleanUpLogger.addNewLogLine("+Processing clean up services done at:" + new Date());
}

protected void processPasswordReset() {
cleanUpLogger.addNewLogLine("-Starting processing PasswordReset clean up at:" + new Date());
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.add(Calendar.SECOND, -appConfiguration.getPasswordResetRequestExpirationTime());
final Date expirationDate = calendar.getTime();
cleanUpLogger.addNewLogLine("-Running password reset clean up with expiration date :" + expirationDate);
passwordResetService.cleanup(expirationDate);
cleanUpLogger.addNewLogLine("-Processing PasswordReset clean up at:" + new Date());
}

private void processCache(Date now) {
cleanUpLogger.addNewLogLine("~Starting processing cache at:" + now);
try {
if (cacheService.getCacheProvider() instanceof NativePersistenceCacheProvider) {
((NativePersistenceCacheProvider) cacheService.getCacheProvider()).cleanup(now, BATCH_SIZE);
}
} catch (Exception e) {
log.error("Failed to clean up cache.", e);
cleanUpLogger.addNewLogLineAsError("~Error occurs while processing cache");
cleanUpLogger.addNewLogLineAsError("~Error message: " + e.getMessage());
}
cleanUpLogger.addNewLogLine("~Processing cache done at:" + new Date());
}

private void processMetricEntries() {
cleanUpLogger.addNewLogLine("#Starting processing Metric entries at:" + new Date());
log.debug("Start metric entries clean up");
int keepDataDays = appConfiguration.getMetricReporterKeepDataDays();
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.add(Calendar.DATE, -keepDataDays);
Date expirationDate = calendar.getTime();
cleanUpLogger.addNewLogLine(String.format(
"#Ready to remove expired entries with parameters batch size: %s, expiration date: %s and appliance inum %s",
BATCH_SIZE, expirationDate, metricService.applianceInum()));
metricService.removeExpiredMetricEntries(expirationDate, ApplicationType.OX_AUTH, metricService.applianceInum(),
0, BATCH_SIZE);
log.debug("End metric entries clean up");
cleanUpLogger.addNewLogLine("#Processing Metric entries done at:" + new Date());
}
}

0 comments on commit 6dfdfa3

Please sign in to comment.