-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HAI-1527 Audit logging for permission updates #393
Merged
+331
−6
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b8c53db
HAI-1527 Audit logging for permission updates
corvidian 37cc429
fixup! HAI-1527 Audit logging for permission updates
corvidian abe60a8
fixup! HAI-1527 Audit logging for permission updates
corvidian c90d9f6
fixup! HAI-1527 Audit logging for permission updates
corvidian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are all these functions unit tested?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, some of these were copied over and unchanged. Removing them.
I added unit tests for the ones that are still in use.