-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HAI-1527 Audit logging for permission updates (#393)
- Loading branch information
Showing
10 changed files
with
331 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import assertk.assertThat | |
import assertk.assertions.containsExactly | ||
import assertk.assertions.containsExactlyInAnyOrder | ||
import assertk.assertions.each | ||
import assertk.assertions.first | ||
import assertk.assertions.hasClass | ||
import assertk.assertions.hasSize | ||
import assertk.assertions.isEmpty | ||
|
@@ -23,7 +24,12 @@ import fi.hel.haitaton.hanke.factory.HankeFactory | |
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withGeneratedOmistaja | ||
import fi.hel.haitaton.hanke.factory.HankeFactory.Companion.withYhteystiedot | ||
import fi.hel.haitaton.hanke.factory.HankeYhteystietoFactory | ||
import fi.hel.haitaton.hanke.logging.AuditLogRepository | ||
import fi.hel.haitaton.hanke.logging.ObjectType | ||
import fi.hel.haitaton.hanke.logging.Operation | ||
import fi.hel.haitaton.hanke.logging.UserRole | ||
import fi.hel.haitaton.hanke.test.Asserts.isRecent | ||
import fi.hel.haitaton.hanke.toChangeLogJsonString | ||
import java.time.OffsetDateTime | ||
import java.util.UUID | ||
import org.junit.jupiter.api.Nested | ||
|
@@ -51,6 +57,7 @@ class HankeKayttajaServiceITest : DatabaseTest() { | |
@Autowired private lateinit var hankeKayttajaRepository: HankeKayttajaRepository | ||
@Autowired private lateinit var permissionRepository: PermissionRepository | ||
@Autowired private lateinit var roleRepository: RoleRepository | ||
@Autowired private lateinit var auditLogRepository: AuditLogRepository | ||
|
||
@Autowired private lateinit var permissionService: PermissionService | ||
|
||
|
@@ -391,6 +398,36 @@ class HankeKayttajaServiceITest : DatabaseTest() { | |
} | ||
} | ||
|
||
@Test | ||
fun `Writes permission update to audit log`() { | ||
val hanke = hankeFactory.save() | ||
val kayttaja = saveUserAndPermission(hanke.id!!) | ||
val updates = mapOf(kayttaja.id to Role.HANKEMUOKKAUS) | ||
auditLogRepository.deleteAll() | ||
|
||
hankeKayttajaService.updatePermissions(hanke, updates, false, USERNAME) | ||
|
||
val logs = auditLogRepository.findAll() | ||
assertThat(logs).hasSize(1) | ||
assertThat(logs) | ||
.first() | ||
.transform { it.message.auditEvent } | ||
.all { | ||
transform { it.target.type }.isEqualTo(ObjectType.PERMISSION) | ||
transform { it.target.id }.isEqualTo(kayttaja.permission?.id.toString()) | ||
transform { it.operation }.isEqualTo(Operation.UPDATE) | ||
transform { it.actor.role }.isEqualTo(UserRole.USER) | ||
transform { it.actor.userId }.isEqualTo(USERNAME) | ||
val permission = kayttaja.permission!!.toDomain() | ||
transform { it.target.objectBefore } | ||
.isEqualTo(permission.toChangeLogJsonString()) | ||
transform { it.target.objectAfter } | ||
.isEqualTo( | ||
permission.copy(role = Role.HANKEMUOKKAUS).toChangeLogJsonString() | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Updates role to tunniste if permission doesn't exist`() { | ||
val hanke = hankeFactory.save() | ||
|
@@ -406,6 +443,34 @@ class HankeKayttajaServiceITest : DatabaseTest() { | |
} | ||
} | ||
|
||
@Test | ||
fun `Writes tunniste update to audit log`() { | ||
val hanke = hankeFactory.save() | ||
val kayttaja = saveUserAndToken(hanke.id!!, "Toinen Tohelo", "[email protected]") | ||
val updates = mapOf(kayttaja.id to Role.HANKEMUOKKAUS) | ||
auditLogRepository.deleteAll() | ||
|
||
hankeKayttajaService.updatePermissions(hanke, updates, false, USERNAME) | ||
|
||
val logs = auditLogRepository.findAll() | ||
assertThat(logs).hasSize(1) | ||
assertThat(logs) | ||
.first() | ||
.transform { it.message.auditEvent } | ||
.all { | ||
transform { it.target.type }.isEqualTo(ObjectType.KAYTTAJA_TUNNISTE) | ||
transform { it.target.id }.isEqualTo(kayttaja.kayttajaTunniste?.id.toString()) | ||
transform { it.operation }.isEqualTo(Operation.UPDATE) | ||
transform { it.actor.role }.isEqualTo(UserRole.USER) | ||
transform { it.actor.userId }.isEqualTo(USERNAME) | ||
val tunniste = | ||
kayttaja.kayttajaTunniste!!.toDomain().copy(hankeKayttajaId = kayttaja.id) | ||
transform { it.target.objectBefore }.isEqualTo(tunniste.toChangeLogJsonString()) | ||
transform { it.target.objectAfter } | ||
.isEqualTo(tunniste.copy(role = Role.HANKEMUOKKAUS).toChangeLogJsonString()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Updates role to only permission if both permission and tunniste exist`() { | ||
val hanke = hankeFactory.save() | ||
|
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
40 changes: 40 additions & 0 deletions
40
...anke-service/src/main/kotlin/fi/hel/haitaton/hanke/logging/HankeKayttajaLoggingService.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,40 @@ | ||
package fi.hel.haitaton.hanke.logging | ||
|
||
import fi.hel.haitaton.hanke.permissions.KayttajaTunniste | ||
import fi.hel.haitaton.hanke.permissions.Permission | ||
import fi.hel.haitaton.hanke.permissions.Role | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Propagation | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
class HankeKayttajaLoggingService(private val auditLogService: AuditLogService) { | ||
|
||
@Transactional(propagation = Propagation.MANDATORY) | ||
fun logUpdate(roleBefore: Role, permissionAfter: Permission, userId: String) { | ||
val permissionBefore = permissionAfter.copy(role = roleBefore) | ||
|
||
AuditLogService.updateEntry( | ||
userId, | ||
ObjectType.PERMISSION, | ||
permissionBefore, | ||
permissionAfter, | ||
) | ||
?.let { auditLogService.create(it) } | ||
} | ||
|
||
@Transactional(propagation = Propagation.MANDATORY) | ||
fun logUpdate( | ||
kayttajaTunnisteBefore: KayttajaTunniste, | ||
kayttajaTunnisteAfter: KayttajaTunniste, | ||
userId: String | ||
) { | ||
AuditLogService.updateEntry( | ||
userId, | ||
ObjectType.KAYTTAJA_TUNNISTE, | ||
kayttajaTunnisteBefore, | ||
kayttajaTunnisteAfter, | ||
) | ||
?.let { auditLogService.create(it) } | ||
} | ||
} |
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
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
24 changes: 24 additions & 0 deletions
24
...es/hanke-service/src/test/kotlin/fi/hel/haitaton/hanke/factory/KayttajaTunnisteFactory.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,24 @@ | ||
package fi.hel.haitaton.hanke.factory | ||
|
||
import fi.hel.haitaton.hanke.permissions.KayttajaTunniste | ||
import fi.hel.haitaton.hanke.permissions.Role | ||
import java.time.OffsetDateTime | ||
import java.util.UUID | ||
|
||
object KayttajaTunnisteFactory { | ||
val TUNNISTE_ID: UUID = UUID.fromString("b514795c-d982-430a-836b-91371829db51") | ||
const val TUNNISTE: String = "K6NqNdCJOrNRh45aCP08e9wc" | ||
val CREATED_AT: OffsetDateTime = OffsetDateTime.parse("2023-08-31T14:25:13Z") | ||
val SENT_AT: OffsetDateTime = OffsetDateTime.parse("2023-08-31T14:25:14Z") | ||
val ROLE: Role = Role.KATSELUOIKEUS | ||
val KAYTTAJA_ID: UUID = UUID.fromString("597431b3-3be1-4594-a07a-bef77c8167df") | ||
|
||
fun create( | ||
id: UUID = TUNNISTE_ID, | ||
tunniste: String = TUNNISTE, | ||
createdAt: OffsetDateTime = CREATED_AT, | ||
sentAt: OffsetDateTime? = SENT_AT, | ||
role: Role = ROLE, | ||
hankeKayttajaId: UUID? = KAYTTAJA_ID, | ||
) = KayttajaTunniste(id, tunniste, createdAt, sentAt, role, hankeKayttajaId) | ||
} |
19 changes: 19 additions & 0 deletions
19
services/hanke-service/src/test/kotlin/fi/hel/haitaton/hanke/factory/PermissionFactory.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,19 @@ | ||
package fi.hel.haitaton.hanke.factory | ||
|
||
import fi.hel.haitaton.hanke.permissions.Permission | ||
import fi.hel.haitaton.hanke.permissions.Role | ||
|
||
object PermissionFactory { | ||
|
||
const val PERMISSION_ID = 65110 | ||
const val USER_ID = "permissionUser" | ||
const val HANKE_ID = 984141 | ||
val ROLE = Role.KATSELUOIKEUS | ||
|
||
fun create( | ||
id: Int = PERMISSION_ID, | ||
userId: String = USER_ID, | ||
hankeId: Int = HANKE_ID, | ||
role: Role = ROLE, | ||
) = Permission(id, userId, hankeId, role) | ||
} |
Oops, something went wrong.