Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #33 from admin-ch/develop
Browse files Browse the repository at this point in the history
Log the code before deleting it
  • Loading branch information
fabe2913 authored Oct 1, 2020
2 parents 04b75a0 + bb03db7 commit 407ebfe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ authcodegeneration:
prometheus:
user: "prometheus"
password: "{noop}secret"
service:
sleepLogInterval: 1

jeap:
security:
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
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;

import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
@ActiveProfiles("local")
class AuthCodeDeletionServiceTest {

@Mock
Expand All @@ -28,8 +31,8 @@ class AuthCodeDeletionServiceTest {
void deleteOldAuthCode_foundTwo_deleteTwo() {
//given
List<AuthorizationCode> 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
Expand All @@ -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
Expand Down

0 comments on commit 407ebfe

Please sign in to comment.