Skip to content

Commit

Permalink
feat(webflux): add data masking function to load aggregate state (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Dec 18, 2024
1 parent 6f4e6f6 commit 5ad7127
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ interface DataMasking<SOURCE : DataMasking<SOURCE>> : DataMasker {
fun mask(): SOURCE
}

fun <S : Any> S.tryMask(): S {
if (this is DataMasking<*>) {
@Suppress("UNCHECKED_CAST")
return this.mask() as S
}
return this
}

fun <S : Any> MaterializedSnapshot<S>.tryMask(): MaterializedSnapshot<S> {
val state = this.state
if (state is DataMasking<*>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ package me.ahoo.wow.query.mask

import me.ahoo.wow.api.query.MaterializedSnapshot
import me.ahoo.wow.api.query.PagedList
import org.hamcrest.CoreMatchers
import org.hamcrest.MatcherAssert
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.CoreMatchers.sameInstance
import org.hamcrest.MatcherAssert.*
import org.junit.jupiter.api.Test

class DataMaskingKtTest {
Expand All @@ -39,7 +40,7 @@ class DataMaskingKtTest {
deleted = false
)
val masked = snapshot.tryMask()
MatcherAssert.assertThat(masked, CoreMatchers.sameInstance(snapshot))
assertThat(masked, sameInstance(snapshot))
}

@Test
Expand All @@ -60,7 +61,7 @@ class DataMaskingKtTest {
deleted = false
)
val masked = snapshot.tryMask()
MatcherAssert.assertThat(masked.state.pwd, CoreMatchers.equalTo("******"))
assertThat(masked.state.pwd, equalTo("******"))
}

@Test
Expand All @@ -82,7 +83,7 @@ class DataMaskingKtTest {
)
val pagedList = PagedList(1, listOf(snapshot))
val masked = pagedList.tryMask()
MatcherAssert.assertThat(masked, CoreMatchers.sameInstance(pagedList))
assertThat(masked, sameInstance(pagedList))
}

@Test
Expand All @@ -104,7 +105,21 @@ class DataMaskingKtTest {
)
val pagedList = PagedList(1, listOf(snapshot))
val masked = pagedList.tryMask()
MatcherAssert.assertThat(masked.list.first().state.pwd, CoreMatchers.equalTo("******"))
assertThat(masked.list.first().state.pwd, equalTo("******"))
}

@Test
fun tryMaskState() {
val state = MockMaskingData("pwd")
val masked = state.tryMask()
assertThat(masked.pwd, equalTo("******"))
}

@Test
fun tryMaskAnyState() {
val source = Any()
val masked = source.tryMask()
assertThat(masked, sameInstance(source))
}

data class MockMaskingData(val pwd: String) : DataMasking<MockMaskingData> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import me.ahoo.wow.modeling.matedata.AggregateMetadata
import me.ahoo.wow.modeling.state.StateAggregate
import me.ahoo.wow.modeling.state.StateAggregateRepository
import me.ahoo.wow.openapi.RoutePaths
import me.ahoo.wow.query.mask.tryMask
import me.ahoo.wow.webflux.exception.RequestExceptionHandler
import me.ahoo.wow.webflux.exception.toServerResponse
import me.ahoo.wow.webflux.route.command.CommandParser.getTenantIdOrDefault
Expand Down Expand Up @@ -48,7 +49,7 @@ abstract class AbstractLoadAggregateHandlerFunction(
}
.map {
checkVersion(version, it)
it.state
it.state.tryMask()
}
.throwNotFoundIfEmpty()
.toServerResponse(request, exceptionHandler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import me.ahoo.wow.modeling.aggregateId
import me.ahoo.wow.modeling.matedata.AggregateMetadata
import me.ahoo.wow.modeling.state.StateAggregateRepository
import me.ahoo.wow.openapi.state.IdsQueryAggregateRouteSpec
import me.ahoo.wow.query.mask.tryMask
import me.ahoo.wow.webflux.exception.RequestExceptionHandler
import me.ahoo.wow.webflux.exception.toServerResponse
import me.ahoo.wow.webflux.route.RouteHandlerFunctionFactory
Expand Down Expand Up @@ -47,7 +48,7 @@ class IdsQueryAggregateHandlerFunction(
}.filter {
it.initialized && !it.deleted
}
.map { it.state }
.map { it.state.tryMask() }
.collectList()
.toServerResponse(request, exceptionHandler)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import me.ahoo.wow.modeling.matedata.AggregateMetadata
import me.ahoo.wow.modeling.state.StateAggregateRepository
import me.ahoo.wow.openapi.RoutePaths
import me.ahoo.wow.openapi.state.ScanAggregateRouteSpec
import me.ahoo.wow.query.mask.tryMask
import me.ahoo.wow.webflux.exception.RequestExceptionHandler
import me.ahoo.wow.webflux.exception.toServerResponse
import me.ahoo.wow.webflux.route.RouteHandlerFunctionFactory
Expand All @@ -44,7 +45,7 @@ class ScanAggregateHandlerFunction(
stateAggregateRepository.load<Any>(it)
}.filter {
it.initialized && !it.deleted
}.map { it.state }
}.map { it.state.tryMask() }
.collectList()
.toServerResponse(request, exceptionHandler)
}
Expand Down

0 comments on commit 5ad7127

Please sign in to comment.