From bb03db75448f14368e6962e2cf7f23ff04538760 Mon Sep 17 00:00:00 2001 From: Fabien Cerf Date: Thu, 1 Oct 2020 08:25:31 +0200 Subject: [PATCH] Log the code before deleting it --- .../service/AuthCodeDeletionService.java | 23 +++++++++++++++---- .../service/AuthCodeGenerationService.java | 2 +- src/main/resources/application-local.yml | 2 ++ src/main/resources/application.yml | 3 ++- .../service/AuthCodeDeletionServiceTest.java | 16 +++++++++++-- 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/admin/bag/covidcode/authcodegeneration/service/AuthCodeDeletionService.java b/src/main/java/ch/admin/bag/covidcode/authcodegeneration/service/AuthCodeDeletionService.java index 5dd4de5..f462327 100644 --- a/src/main/java/ch/admin/bag/covidcode/authcodegeneration/service/AuthCodeDeletionService.java +++ b/src/main/java/ch/admin/bag/covidcode/authcodegeneration/service/AuthCodeDeletionService.java @@ -4,6 +4,7 @@ import ch.admin.bag.covidcode.authcodegeneration.domain.AuthorizationCodeRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -18,11 +19,14 @@ @Slf4j @RequiredArgsConstructor @Service -@ConditionalOnProperty(value="CF_INSTANCE_INDEX", havingValue = "0") +@ConditionalOnProperty(value = "CF_INSTANCE_INDEX", havingValue = "0") public class AuthCodeDeletionService { private final AuthorizationCodeRepository authorizationCodeRepository; + @Value("${authcodegeneration.service.sleepLogInterval}") + private int sleepLogInterval; + @Transactional @Scheduled(cron = "${authcodegeneration.service.deletionCron}") public void deleteOldAuthCode() { @@ -34,11 +38,22 @@ public void deleteOldAuthCode() { expiredAuthCodes.forEach(ac -> { - if (ac.getCallCount() > 0) { - log.info("AuthorizationCode verified: '{}', '{}', '{}', '{}', '{}'", kv("id", ac.getId()), kv("callCount", ac.getCallCount()), kv("creationDateTime", ac.getCreationDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)), kv("onsetDate",ac.getOnsetDate().format(DateTimeFormatter.ISO_LOCAL_DATE)), kv("originalOnsetDate",ac.getOriginalOnsetDate().format(DateTimeFormatter.ISO_LOCAL_DATE))); + try { + Thread.sleep(sleepLogInterval); + } catch (InterruptedException e) { + log.error("Exception during sleep", e); + Thread.currentThread().interrupt(); + throw new IllegalStateException(e); } - log.info("Deleting code '{}' with expiryDate '{}'.", ac.getCode(), ac.getExpiryDate()); + log.info("AuthorizationCode-Statistic '{}', '{}', '{}', '{}', '{}', '{}'", + kv("id", ac.getId()), + kv("callCount", ac.getCallCount()), + kv("creationDateTime", ac.getCreationDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)), + kv("onsetDate", ac.getOnsetDate().format(DateTimeFormatter.ISO_LOCAL_DATE)), + kv("originalOnsetDate", ac.getOriginalOnsetDate().format(DateTimeFormatter.ISO_LOCAL_DATE)), + kv("expiryDate", ac.getExpiryDate().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))); + authorizationCodeRepository.delete(ac); }); diff --git a/src/main/java/ch/admin/bag/covidcode/authcodegeneration/service/AuthCodeGenerationService.java b/src/main/java/ch/admin/bag/covidcode/authcodegeneration/service/AuthCodeGenerationService.java index e7ecac3..07e8434 100644 --- a/src/main/java/ch/admin/bag/covidcode/authcodegeneration/service/AuthCodeGenerationService.java +++ b/src/main/java/ch/admin/bag/covidcode/authcodegeneration/service/AuthCodeGenerationService.java @@ -47,7 +47,7 @@ public AuthorizationCodeResponseDto create(AuthorizationCodeCreateDto createDto) AuthorizationCode authorizationCode = new AuthorizationCode(authCode, createDto.getOnsetDate(), createDto.getOnsetDate().minusDays(onsetSubtractionDays), ZonedDateTime.now().plusMinutes(codeExpirationDelay)); authorizationCodeRepository.saveAndFlush(authorizationCode); - log.info("New authorizationCode saved: {}, {}, {}.", kv("id", authorizationCode.getId()), kv("creationDateTime", authorizationCode.getCreationDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)), kv("onsetDate",authorizationCode.getOnsetDate())); + log.info("New authorizationCode saved: {}, {}, {}.", kv("id", authorizationCode.getId()), kv("creationDateTime", authorizationCode.getCreationDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)), kv("onsetDate",authorizationCode.getOnsetDate().format(DateTimeFormatter.ISO_LOCAL_DATE))); return new AuthorizationCodeResponseDto(authorizationCode.getCode()); } diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index a71a8b0..7f3d3b6 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -11,6 +11,8 @@ authcodegeneration: prometheus: user: "prometheus" password: "{noop}secret" + service: + sleepLogInterval: 1 jeap: security: diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ed12311..4991393 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -114,9 +114,10 @@ authcodegeneration: service: callCountLimit: 1 codeExpirationDelay: 1440 - deletionCron: "0 30 1 * * ?" + deletionCron: "0 0 2 * * ?" onsetSubtractionDays: 2 requestTime: 500 + sleepLogInterval: 30000 monitor: prometheus: secure: false diff --git a/src/test/java/ch/admin/bag/covidcode/authcodegeneration/service/AuthCodeDeletionServiceTest.java b/src/test/java/ch/admin/bag/covidcode/authcodegeneration/service/AuthCodeDeletionServiceTest.java index 4902ab8..d589013 100644 --- a/src/test/java/ch/admin/bag/covidcode/authcodegeneration/service/AuthCodeDeletionServiceTest.java +++ b/src/test/java/ch/admin/bag/covidcode/authcodegeneration/service/AuthCodeDeletionServiceTest.java @@ -7,7 +7,9 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.test.context.ActiveProfiles; +import java.time.LocalDate; import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.List; @@ -15,6 +17,7 @@ import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) +@ActiveProfiles("local") class AuthCodeDeletionServiceTest { @Mock @@ -28,8 +31,8 @@ class AuthCodeDeletionServiceTest { void deleteOldAuthCode_foundTwo_deleteTwo() { //given List codes = new ArrayList<>(); - codes.add(mock(AuthorizationCode.class)); - codes.add(mock(AuthorizationCode.class)); + codes.add(createAuthorizationCode()); + codes.add(createAuthorizationCode()); when(repository.findByExpiryDateBefore(any(ZonedDateTime.class))).thenReturn(codes); //when @@ -39,6 +42,15 @@ void deleteOldAuthCode_foundTwo_deleteTwo() { verify(repository, times(2)).delete(any(AuthorizationCode.class)); } + private AuthorizationCode createAuthorizationCode() { + AuthorizationCode authorizationCode = mock(AuthorizationCode.class); + when(authorizationCode.getCreationDateTime()).thenReturn(ZonedDateTime.now()); + when(authorizationCode.getExpiryDate()).thenReturn(ZonedDateTime.now()); + when(authorizationCode.getOnsetDate()).thenReturn(LocalDate.now()); + when(authorizationCode.getOriginalOnsetDate()).thenReturn(LocalDate.now()); + return authorizationCode; + } + @Test void deleteOldAuthCode_foundNone_deleteNone() { //given