-
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.
Merge pull request #260 from VAuthenticator/clean-up-entry-with-ttl-job
Clean up entry with ttl job
- Loading branch information
Showing
9 changed files
with
179 additions
and
4 deletions.
There are no files selected for viewing
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
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,13 @@ | ||
# Management | ||
|
||
Custom Actuator Endpoint | ||
|
||
## Clean Database Entry with TTL | ||
|
||
Actuator Clean Database enabled for database profile activated | ||
|
||
*URI:* ```Post /actuator/database-clean-up``` | ||
|
||
*Request:* Empty request body | ||
|
||
*Response Status:* ```204 NoContent``` |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"LOCAL": { | ||
"host": "http://local.management.vauthenticator.com:9090" | ||
"host": "http://local.management.vauthenticator.com:9090", | ||
"actuatorHost": "http://local.management.vauthenticator.com:9091" | ||
} | ||
} |
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
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
69 changes: 69 additions & 0 deletions
69
src/main/kotlin/com/vauthenticator/server/management/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,69 @@ | ||
package com.vauthenticator.server.management | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.jdbc.core.JdbcTemplate | ||
import org.springframework.jdbc.core.queryForList | ||
import org.springframework.transaction.annotation.Transactional | ||
import java.time.Clock | ||
|
||
@Transactional | ||
class DatabaseTtlEntryCleanJob( | ||
private val jdbcTemplate: JdbcTemplate, | ||
private val clock: Clock | ||
) { | ||
|
||
private val logger = LoggerFactory.getLogger(DatabaseTtlEntryCleanJob::class.java) | ||
|
||
fun execute() { | ||
logger.info("Job Running") | ||
val now = clock.instant().epochSecond | ||
|
||
deleteOldTicket(now) | ||
deleteOldKeys(now) | ||
logger.info("Job Completed") | ||
|
||
} | ||
|
||
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) | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
@Profile("database") | ||
@Configuration(proxyBeanMethods = false) | ||
class DatabaseTtlEntryCleanJobConfig() { | ||
|
||
@Bean | ||
fun databaseTtlEntryCleanJob( | ||
jdbcTemplate: JdbcTemplate | ||
) = DatabaseTtlEntryCleanJob(jdbcTemplate, Clock.systemUTC()) | ||
|
||
@Bean | ||
fun databaseTtlEntryCleanJobEndPoint(databaseTtlEntryCleanJob: DatabaseTtlEntryCleanJob) = | ||
DatabaseTtlEntryCleanJobEndPoint(databaseTtlEntryCleanJob) | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/vauthenticator/server/management/DatabaseTtlEntryCleanJobEndPoint.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,17 @@ | ||
package com.vauthenticator.server.management | ||
|
||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint | ||
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation | ||
import org.springframework.http.ResponseEntity | ||
|
||
@Endpoint(id = "database-clean-up") | ||
class DatabaseTtlEntryCleanJobEndPoint( | ||
private val databaseTtlEntryCleanJob: DatabaseTtlEntryCleanJob | ||
) { | ||
|
||
@WriteOperation | ||
fun cleanUp(): ResponseEntity<Unit> { | ||
databaseTtlEntryCleanJob.execute() | ||
return ResponseEntity.noContent().build() | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
src/test/kotlin/com/vauthenticator/server/management/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,63 @@ | ||
package com.vauthenticator.server.management | ||
|
||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.vauthenticator.server.keys.adapter.jdbc.JdbcKeyStorage | ||
import com.vauthenticator.server.keys.domain.DataKey | ||
import com.vauthenticator.server.keys.domain.KeyPurpose.SIGNATURE | ||
import com.vauthenticator.server.keys.domain.KeyType | ||
import com.vauthenticator.server.keys.domain.Kid | ||
import com.vauthenticator.server.keys.domain.MasterKid | ||
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.assertThrows | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
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")) | ||
assertTrue(actualTicket.isEmpty) | ||
assertThrows(NoSuchElementException::class.java) { | ||
keyStorage.findOne(kid, SIGNATURE) | ||
} | ||
} | ||
} |