-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
now we are able to clean up old keys and ticket from the database
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/main/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJob.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.vauthenticator.server.job | ||
|
||
import org.springframework.jdbc.core.JdbcTemplate | ||
import org.springframework.jdbc.core.queryForList | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.transaction.annotation.Transactional | ||
import java.time.Clock | ||
|
||
|
||
@Transactional | ||
class DatabaseTtlEntryCleanJob( | ||
private val jdbcTemplate: JdbcTemplate, | ||
private val clock: Clock | ||
) { | ||
|
||
@Scheduled(cron = "\${scheduled.database-cleanup.cron}") | ||
fun execute() { | ||
val now = clock.instant().epochSecond | ||
|
||
deleteOldTicket(now) | ||
deleteOldKeys(now) | ||
} | ||
|
||
private fun deleteOldKeys(now: Long) { | ||
val keysToBeDeleted = | ||
jdbcTemplate.queryForList("SELECT key_id,key_purpose FROM KEYS WHERE key_expiration_date_timestamp < ?", now) | ||
keysToBeDeleted.forEach { | ||
jdbcTemplate.update( | ||
"DELETE FROM KEYS WHERE key_id = ? AND key_purpose = ?;", it["key_id"], it["key_purpose"] | ||
) | ||
} | ||
} | ||
|
||
private fun deleteOldTicket(now: Long) { | ||
val ticketToBeDeleted = | ||
jdbcTemplate.queryForList<String>( | ||
"SELECT ticket FROM TICKET WHERE ttl < ?", | ||
arrayOf(now) | ||
) | ||
ticketToBeDeleted.forEach { | ||
jdbcTemplate.update("DELETE FROM TICKET WHERE ticket = ?", it) | ||
} | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
src/test/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJobTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.vauthenticator.server.job | ||
|
||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.vauthenticator.server.keys.adapter.jdbc.JdbcKeyStorage | ||
import com.vauthenticator.server.keys.domain.* | ||
import com.vauthenticator.server.keys.domain.KeyPurpose.SIGNATURE | ||
import com.vauthenticator.server.support.AccountTestFixture | ||
import com.vauthenticator.server.support.ClientAppFixture | ||
import com.vauthenticator.server.support.JdbcUtils.jdbcTemplate | ||
import com.vauthenticator.server.support.JdbcUtils.resetDb | ||
import com.vauthenticator.server.support.TicketFixture | ||
import com.vauthenticator.server.ticket.adapter.jdbc.JdbcTicketRepository | ||
import com.vauthenticator.server.ticket.domain.TicketId | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import java.time.Clock | ||
import java.time.Duration | ||
import java.util.* | ||
|
||
|
||
class DatabaseTtlEntryCleanJobTest { | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
resetDb() | ||
} | ||
|
||
@Test | ||
fun `when the old entries are deleted`() { | ||
val ticketRepository = JdbcTicketRepository(jdbcTemplate, jacksonObjectMapper()) | ||
val keyStorage = JdbcKeyStorage(jdbcTemplate, Clock.systemDefaultZone()) | ||
|
||
val uut = DatabaseTtlEntryCleanJob(jdbcTemplate, Clock.systemUTC()) | ||
|
||
val kid = Kid("") | ||
val anAccount = AccountTestFixture.anAccount() | ||
val aClientAppId = ClientAppFixture.aClientAppId() | ||
|
||
ticketRepository.store(TicketFixture.ticketFor("A_TICKET", anAccount.email, aClientAppId.content)) | ||
keyStorage.store( | ||
MasterKid(""), | ||
kid, | ||
DataKey(ByteArray(0), Optional.empty()), | ||
KeyType.ASYMMETRIC, | ||
SIGNATURE | ||
) | ||
keyStorage.keyDeleteJodPlannedFor(kid, Duration.ofSeconds(-200), SIGNATURE) | ||
|
||
uut.execute() | ||
|
||
val actualTicket = ticketRepository.loadFor(TicketId("A_TICKET")) | ||
Assertions.assertTrue(actualTicket.isEmpty) | ||
Assertions.assertThrows(NoSuchElementException::class.java) { | ||
keyStorage.findOne(kid, SIGNATURE) | ||
} | ||
} | ||
} |