-
Notifications
You must be signed in to change notification settings - Fork 2
[MS-894] Add a custom CoSync deserializer that accounts for past versions of EnrolmentRecordCreationEvent #1116
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
Merged
BurningAXE
merged 1 commit into
release/2025.1.0
from
MS-894-CoSync-enrolments-made-with-SID-2023.3.3-pre-tokenization-are-not-parsed
Feb 25, 2025
Merged
Changes from all commits
Commits
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 hidden or 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
61 changes: 61 additions & 0 deletions
61
...com/simprints/infra/events/event/cosync/CoSyncEnrolmentRecordCreationEventDeserializer.kt
This file contains hidden or 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,61 @@ | ||
| package com.simprints.infra.events.event.cosync | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParser | ||
| import com.fasterxml.jackson.databind.DeserializationContext | ||
| import com.fasterxml.jackson.databind.JsonNode | ||
| import com.fasterxml.jackson.databind.deser.std.StdDeserializer | ||
| import com.simprints.core.domain.tokenization.TokenizableString | ||
| import com.simprints.infra.events.event.domain.models.subject.BiometricReference | ||
| import com.simprints.infra.events.event.domain.models.subject.EnrolmentRecordCreationEvent | ||
|
|
||
| /** | ||
| * Deserializer for [EnrolmentRecordCreationEvent] that reads the JSON node and constructs the | ||
| * [EnrolmentRecordCreationEvent] object. | ||
| * Accounts for past versions of the event where moduleId and attendantId were plain strings. | ||
| */ | ||
| class CoSyncEnrolmentRecordCreationEventDeserializer : | ||
| StdDeserializer<EnrolmentRecordCreationEvent>( | ||
| EnrolmentRecordCreationEvent::class.java, | ||
| ) { | ||
| override fun deserialize( | ||
| p: JsonParser, | ||
| ctxt: DeserializationContext, | ||
| ): EnrolmentRecordCreationEvent { | ||
| val node: JsonNode = p.codec.readTree(p) | ||
| val id = node["id"].asText() | ||
| val payload = node["payload"] | ||
|
|
||
| val subjectId = payload["subjectId"].asText() | ||
| val projectId = payload["projectId"].asText() | ||
|
|
||
| // Try to parse as TokenizableString first, fall back to plain String | ||
| val moduleId = try { | ||
| ctxt.readTreeAsValue(payload["moduleId"], TokenizableString::class.java) | ||
| } catch (_: Exception) { | ||
| TokenizableString.Raw(payload["moduleId"].asText()) | ||
| } | ||
|
|
||
| // Try to parse as TokenizableString first, fall back to plain String | ||
| val attendantId = try { | ||
| ctxt.readTreeAsValue(payload["attendantId"], TokenizableString::class.java) | ||
| } catch (_: Exception) { | ||
| TokenizableString.Raw(payload["attendantId"].asText()) | ||
| } | ||
|
|
||
| val biometricReferences = ctxt.readTreeAsValue<List<BiometricReference>>( | ||
| payload["biometricReferences"], | ||
| ctxt.typeFactory.constructCollectionType(List::class.java, BiometricReference::class.java), | ||
| ) | ||
|
|
||
| return EnrolmentRecordCreationEvent( | ||
| id, | ||
| EnrolmentRecordCreationEvent.EnrolmentRecordCreationPayload( | ||
| subjectId, | ||
| projectId, | ||
| moduleId, | ||
| attendantId, | ||
| biometricReferences, | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
136 changes: 136 additions & 0 deletions
136
...simprints/infra/events/event/cosync/CoSyncEnrolmentRecordCreationEventDeserializerTest.kt
This file contains hidden or 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,136 @@ | ||
| package com.simprints.infra.events.event.cosync | ||
|
|
||
| import com.fasterxml.jackson.databind.DeserializationContext | ||
| import com.fasterxml.jackson.databind.JavaType | ||
| import com.fasterxml.jackson.databind.JsonNode | ||
| import com.fasterxml.jackson.databind.ObjectMapper | ||
| import com.simprints.core.domain.tokenization.TokenizableString | ||
| import com.simprints.infra.events.event.domain.models.subject.BiometricReference | ||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import org.junit.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class CoSyncEnrolmentRecordCreationEventDeserializerTest { | ||
| private val deserializer = CoSyncEnrolmentRecordCreationEventDeserializer() | ||
| private val objectMapper = ObjectMapper() | ||
|
|
||
| @Test | ||
| fun `deserialize handles old format with plain strings`() { | ||
| val json = JSON_TEMPLATE.format(PLAIN_MODULE, PLAIN_ATTENDANT) | ||
| val parser = objectMapper.createParser(json) | ||
| val context = mockk<DeserializationContext>() | ||
| every { | ||
| context.readTreeAsValue<List<BiometricReference>>( | ||
| any<JsonNode>(), | ||
| any<JavaType>(), | ||
| ) | ||
| } returns emptyList() | ||
|
|
||
| val result = deserializer.deserialize(parser, context) | ||
|
|
||
| assertEquals(EVENT_ID, result.id) | ||
| assertEquals(SUBJECT_ID, result.payload.subjectId) | ||
| assertEquals(PROJECT_ID, result.payload.projectId) | ||
| assertEquals(TokenizableString.Raw(MODULE_ID), result.payload.moduleId) | ||
| assertEquals(TokenizableString.Raw(ATTENDANT_ID), result.payload.attendantId) | ||
| assertEquals(emptyList<BiometricReference>(), result.payload.biometricReferences) | ||
| } | ||
|
|
||
| @Test | ||
| fun `deserialize handles new format with TokenizableString`() { | ||
| val json = JSON_TEMPLATE.format(TOKENIZED_MODULE, RAW_ATTENDANT) | ||
| val parser = objectMapper.createParser(json) | ||
| val context = mockk<DeserializationContext>() | ||
| every { | ||
| context.readTreeAsValue(any(), TokenizableString::class.java) | ||
| } returns TokenizableString.Tokenized(ENCRYPTED_MODULE) andThen TokenizableString.Raw(UNENCRYPTED_ATTENDANT) | ||
| every { | ||
| context.readTreeAsValue<List<BiometricReference>>( | ||
| any<JsonNode>(), | ||
| any<JavaType>(), | ||
| ) | ||
| } returns emptyList() | ||
|
|
||
| val result = deserializer.deserialize(parser, context) | ||
|
|
||
| assertEquals(EVENT_ID, result.id) | ||
| assertEquals(SUBJECT_ID, result.payload.subjectId) | ||
| assertEquals(PROJECT_ID, result.payload.projectId) | ||
| assertEquals(TokenizableString.Tokenized(ENCRYPTED_MODULE), result.payload.moduleId) | ||
| assertEquals(TokenizableString.Raw(UNENCRYPTED_ATTENDANT), result.payload.attendantId) | ||
| assertEquals(emptyList<BiometricReference>(), result.payload.biometricReferences) | ||
| } | ||
|
|
||
| @Test | ||
| fun `deserialize handles new format with TokenizableString but without explicit class`() { | ||
| val json = JSON_TEMPLATE.format(TOKENIZED_MODULE_NO_CLASS, RAW_ATTENDANT_NO_CLASS) | ||
| val parser = objectMapper.createParser(json) | ||
| val context = mockk<DeserializationContext>() | ||
| every { | ||
| context.readTreeAsValue(any(), TokenizableString::class.java) | ||
| } returns TokenizableString.Raw(ENCRYPTED_MODULE) andThen TokenizableString.Raw(UNENCRYPTED_ATTENDANT) | ||
| every { | ||
| context.readTreeAsValue<List<BiometricReference>>( | ||
| any<JsonNode>(), | ||
| any<JavaType>(), | ||
| ) | ||
| } returns emptyList() | ||
|
|
||
| val result = deserializer.deserialize(parser, context) | ||
|
|
||
| assertEquals(EVENT_ID, result.id) | ||
| assertEquals(SUBJECT_ID, result.payload.subjectId) | ||
| assertEquals(PROJECT_ID, result.payload.projectId) | ||
| assertEquals(TokenizableString.Raw(ENCRYPTED_MODULE), result.payload.moduleId) | ||
| assertEquals(TokenizableString.Raw(UNENCRYPTED_ATTENDANT), result.payload.attendantId) | ||
| assertEquals(emptyList<BiometricReference>(), result.payload.biometricReferences) | ||
| } | ||
|
|
||
| companion object { | ||
| const val EVENT_ID = "event-id" | ||
| const val SUBJECT_ID = "subject-1" | ||
| const val PROJECT_ID = "project-1" | ||
| const val MODULE_ID = "module-1" | ||
| const val ATTENDANT_ID = "attendant-1" | ||
| const val ENCRYPTED_MODULE = "encrypted-module-1" | ||
| const val UNENCRYPTED_ATTENDANT = "unencrypted-attendant-1" | ||
|
|
||
| const val JSON_TEMPLATE = """ | ||
| { | ||
| "id": "$EVENT_ID", | ||
| "payload": { | ||
| "subjectId": "$SUBJECT_ID", | ||
| "projectId": "$PROJECT_ID", | ||
| %s, | ||
| %s, | ||
| "biometricReferences": [] | ||
| } | ||
| }""" | ||
|
|
||
| const val PLAIN_MODULE = """ | ||
| "moduleId": "$MODULE_ID"""" | ||
| const val PLAIN_ATTENDANT = """ | ||
| "attendantId": "$ATTENDANT_ID"""" | ||
|
|
||
| const val TOKENIZED_MODULE = """ | ||
| "moduleId": { | ||
| "className": "TokenizableString.Tokenized", | ||
| "value": "$ENCRYPTED_MODULE" | ||
| }""" | ||
| const val RAW_ATTENDANT = """ | ||
| "attendantId": { | ||
| "className": "TokenizableString.Raw", | ||
| "value": "$UNENCRYPTED_ATTENDANT" | ||
| }""" | ||
|
|
||
| const val TOKENIZED_MODULE_NO_CLASS = """ | ||
| "moduleId": { | ||
| "value": "$ENCRYPTED_MODULE" | ||
| }""" | ||
| const val RAW_ATTENDANT_NO_CLASS = """ | ||
| "attendantId": { | ||
| "value": "$UNENCRYPTED_ATTENDANT" | ||
| }""" | ||
| } | ||
| } |
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.
I assume it would not be possible to see if the value is tokenized just without the appropriate meta-data, right?
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.
Unfortunately, no. We have the same problem for the "middle case" where we serilized TokenizableString without explicitly specifying the class (Raw or Tokenized). What we do downstream is try to decrypt
Rawvalues to check whether they are actually encrypted or not. For Tokenized we are sure they are actually what they say they are.