Skip to content

Commit 8abf108

Browse files
committed
Add RBAC/Admin management on findAllDatasets
1 parent d817388 commit 8abf108

File tree

3 files changed

+124
-20
lines changed

3 files changed

+124
-20
lines changed

dataset/src/integrationTest/kotlin/com/cosmotech/dataset/service/DatasetServiceIntegrationTest.kt

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class DatasetServiceIntegrationTest : CsmRedisTestBase() {
142142

143143
@BeforeEach
144144
fun beforeEach() {
145-
every { getCurrentAccountIdentifier(any()) } returns CONNECTED_ADMIN_USER
145+
every { getCurrentAccountIdentifier(any()) } returns TEST_USER_MAIL
146146
every { getCurrentAuthenticatedUserName(csmPlatformProperties) } returns "test.user"
147147
every { getCurrentAuthenticatedRoles(any()) } returns listOf()
148148
rediSearchIndexer.createIndexFor(Connector::class.java)
@@ -153,11 +153,11 @@ class DatasetServiceIntegrationTest : CsmRedisTestBase() {
153153

154154
connectorSaved = connectorApiService.registerConnector(makeConnector())
155155

156-
organization = makeOrganizationWithRole("Organization")
156+
organization = makeOrganizationWithRole()
157157
organizationSaved = organizationApiService.registerOrganization(organization)
158158
dataset = makeDatasetWithRole()
159159
datasetSaved = datasetApiService.createDataset(organizationSaved.id!!, dataset)
160-
dataset2 = makeDatasetWithRole()
160+
dataset2 = makeDataset()
161161
solution = makeSolution()
162162
solutionSaved = solutionApiService.createSolution(organizationSaved.id!!, solution)
163163
workspace = makeWorkspace()
@@ -334,6 +334,69 @@ class DatasetServiceIntegrationTest : CsmRedisTestBase() {
334334
assertTrue { datasetCompatibilityList.isEmpty() }
335335
}
336336

337+
@Test
338+
fun `test find All Datasets as Platform Admin`() {
339+
organizationSaved = organizationApiService.registerOrganization(organization)
340+
val numberOfDatasets = 20
341+
val defaultPageSize = csmPlatformProperties.twincache.dataset.defaultPageSize
342+
val expectedSize = 15
343+
IntRange(1, numberOfDatasets).forEach {
344+
datasetApiService.createDataset(
345+
organizationSaved.id!!, makeDataset("d-dataset-$it", "dataset-$it"))
346+
}
347+
logger.info("Change current user...")
348+
every { getCurrentAccountIdentifier(any()) } returns CONNECTED_ADMIN_USER
349+
every { getCurrentAuthenticatedUserName(csmPlatformProperties) } returns "test.admin"
350+
every { getCurrentAuthenticatedRoles(any()) } returns listOf(ROLE_PLATFORM_ADMIN)
351+
352+
logger.info("should find all datasets and assert there are $numberOfDatasets")
353+
var datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, null, null)
354+
assertEquals(numberOfDatasets + 1, datasetList.size)
355+
356+
logger.info("should find all datasets and assert it equals defaultPageSize: $defaultPageSize")
357+
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 0, null)
358+
assertEquals(defaultPageSize, datasetList.size)
359+
360+
logger.info("should find all datasets and assert there are expected size: $expectedSize")
361+
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 0, expectedSize)
362+
assertEquals(expectedSize, datasetList.size)
363+
364+
logger.info("should find all solutions and assert it returns the second / last page")
365+
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 1, expectedSize)
366+
assertEquals(numberOfDatasets - expectedSize + 1, datasetList.size)
367+
}
368+
369+
@Test
370+
fun `test find All Datasets as Organization User`() {
371+
organizationSaved = organizationApiService.registerOrganization(organization)
372+
val numberOfDatasets = 20
373+
val defaultPageSize = csmPlatformProperties.twincache.dataset.defaultPageSize
374+
val expectedSize = 15
375+
IntRange(1, numberOfDatasets).forEach {
376+
datasetApiService.createDataset(
377+
organizationSaved.id!!,
378+
makeDatasetWithRole(
379+
organizationId = "d-dataset-$it",
380+
parentId = "dataset-$it",
381+
userName = "ANOTHER_USER"))
382+
}
383+
logger.info("should find all datasets and assert there are $numberOfDatasets")
384+
var datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, null, null)
385+
assertEquals(0, datasetList.size)
386+
387+
logger.info("should find all datasets and assert it equals defaultPageSize: $defaultPageSize")
388+
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 0, null)
389+
assertEquals(0, datasetList.size)
390+
391+
logger.info("should find all datasets and assert there are expected size: $expectedSize")
392+
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 0, expectedSize)
393+
assertEquals(0, datasetList.size)
394+
395+
logger.info("should find all solutions and assert it returns the second / last page")
396+
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 1, expectedSize)
397+
assertEquals(0, datasetList.size)
398+
}
399+
337400
@Test
338401
fun `test find All Datasets with different pagination params`() {
339402
organizationSaved = organizationApiService.registerOrganization(organization)
@@ -650,8 +713,7 @@ class DatasetServiceIntegrationTest : CsmRedisTestBase() {
650713
@Test
651714
fun `access control list shouldn't contain more than one time each user on creation`() {
652715
connectorSaved = connectorApiService.registerConnector(makeConnector())
653-
organizationSaved =
654-
organizationApiService.registerOrganization(makeOrganizationWithRole("organization"))
716+
organizationSaved = organizationApiService.registerOrganization(makeOrganizationWithRole())
655717
val brokenDataset =
656718
Dataset(
657719
name = "dataset",
@@ -671,8 +733,7 @@ class DatasetServiceIntegrationTest : CsmRedisTestBase() {
671733
@Test
672734
fun `access control list shouldn't contain more than one time each user on ACL addition`() {
673735
connectorSaved = connectorApiService.registerConnector(makeConnector())
674-
organizationSaved =
675-
organizationApiService.registerOrganization(makeOrganizationWithRole("organization"))
736+
organizationSaved = organizationApiService.registerOrganization(makeOrganizationWithRole())
676737
val workingDataset = makeDatasetWithRole("dataset", sourceType = DatasetSourceType.None)
677738
val datasetSaved = datasetApiService.createDataset(organizationSaved.id!!, workingDataset)
678739

@@ -925,7 +986,7 @@ class DatasetServiceIntegrationTest : CsmRedisTestBase() {
925986

926987
fun makeOrganizationWithRole(
927988
userName: String = TEST_USER_MAIL,
928-
role: String = ROLE_ADMIN
989+
role: String = ROLE_EDITOR
929990
): Organization {
930991
return Organization(
931992
id = UUID.randomUUID().toString(),
@@ -939,6 +1000,23 @@ class DatasetServiceIntegrationTest : CsmRedisTestBase() {
9391000
OrganizationAccessControl(id = CONNECTED_ADMIN_USER, role = ROLE_ADMIN),
9401001
OrganizationAccessControl(id = userName, role = role))))
9411002
}
1003+
fun makeDataset(
1004+
organizationId: String = organizationSaved.id!!,
1005+
parentId: String = "",
1006+
sourceType: DatasetSourceType = DatasetSourceType.File
1007+
): Dataset {
1008+
return Dataset(
1009+
id = UUID.randomUUID().toString(),
1010+
name = "My datasetRbac",
1011+
organizationId = organizationId,
1012+
parentId = parentId,
1013+
ownerId = "ownerId",
1014+
connector = DatasetConnector(connectorSaved.id!!),
1015+
twingraphId = "graph",
1016+
source = SourceInfo("location", "name", "path"),
1017+
tags = mutableListOf("dataset"),
1018+
sourceType = sourceType)
1019+
}
9421020

9431021
fun makeDatasetWithRole(
9441022
organizationId: String = organizationSaved.id!!,
@@ -967,7 +1045,11 @@ class DatasetServiceIntegrationTest : CsmRedisTestBase() {
9671045
DatasetAccessControl(id = userName, role = role))))
9681046
}
9691047

970-
fun makeSolution(organizationId: String = organizationSaved.id!!): Solution {
1048+
fun makeSolution(
1049+
organizationId: String = organizationSaved.id!!,
1050+
userName: String = TEST_USER_MAIL,
1051+
role: String = ROLE_EDITOR
1052+
): Solution {
9711053
return Solution(
9721054
id = "solutionId",
9731055
key = UUID.randomUUID().toString(),
@@ -979,7 +1061,8 @@ class DatasetServiceIntegrationTest : CsmRedisTestBase() {
9791061
default = ROLE_NONE,
9801062
accessControlList =
9811063
mutableListOf(
982-
SolutionAccessControl(id = CONNECTED_ADMIN_USER, role = ROLE_ADMIN))))
1064+
SolutionAccessControl(id = CONNECTED_ADMIN_USER, role = ROLE_ADMIN),
1065+
SolutionAccessControl(id = userName, role = role))))
9831066
}
9841067

9851068
fun makeWorkspace(

dataset/src/main/kotlin/com/cosmotech/dataset/service/DatasetServiceImpl.kt

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.cosmotech.api.events.TwingraphImportJobInfoRequest
1818
import com.cosmotech.api.exceptions.CsmAccessForbiddenException
1919
import com.cosmotech.api.exceptions.CsmClientException
2020
import com.cosmotech.api.exceptions.CsmResourceNotFoundException
21+
import com.cosmotech.api.rbac.CsmAdmin
2122
import com.cosmotech.api.rbac.CsmRbac
2223
import com.cosmotech.api.rbac.PERMISSION_CREATE_CHILDREN
2324
import com.cosmotech.api.rbac.PERMISSION_DELETE
@@ -125,21 +126,40 @@ class DatasetServiceImpl(
125126
private val datasetRepository: DatasetRepository,
126127
private val unifiedJedis: UnifiedJedis,
127128
private val csmRbac: CsmRbac,
129+
private val csmAdmin: CsmAdmin,
128130
private val resourceScanner: ResourceScanner
129131
) : CsmPhoenixService(), DatasetApiServiceInterface {
130132

131133
override fun findAllDatasets(organizationId: String, page: Int?, size: Int?): List<Dataset> {
132134
organizationService.getVerifiedOrganization(organizationId)
133-
134-
val currentUser = getCurrentAccountIdentifier(this.csmPlatformProperties)
135135
val defaultPageSize = csmPlatformProperties.twincache.dataset.defaultPageSize
136136
val pageable = constructPageRequest(page, size, defaultPageSize)
137-
if (pageable != null) {
138-
return datasetRepository.findByOrganizationId(organizationId, currentUser, pageable).toList()
139-
}
140-
return findAllPaginated(defaultPageSize) {
141-
datasetRepository.findByOrganizationId(organizationId, currentUser, it).toList()
137+
val isAdmin = csmAdmin.verifyCurrentRolesAdmin()
138+
val result: MutableList<Dataset>
139+
140+
val rbacEnabled = !isAdmin && this.csmPlatformProperties.rbac.enabled
141+
142+
if (pageable == null) {
143+
result =
144+
findAllPaginated(defaultPageSize) {
145+
if (rbacEnabled) {
146+
val currentUser = getCurrentAccountIdentifier(this.csmPlatformProperties)
147+
datasetRepository.findByOrganizationId(organizationId, currentUser, it).toList()
148+
} else {
149+
datasetRepository.findAll(it).toList()
150+
}
151+
}
152+
} else {
153+
result =
154+
if (rbacEnabled) {
155+
val currentUser = getCurrentAccountIdentifier(this.csmPlatformProperties)
156+
datasetRepository.findByOrganizationId(organizationId, currentUser, pageable).toList()
157+
} else {
158+
datasetRepository.findAll(pageable).toList()
159+
}
142160
}
161+
162+
return result
143163
}
144164

145165
override fun findDatasetById(organizationId: String, datasetId: String): Dataset {

dataset/src/test/kotlin/com/cosmotech/dataset/service/DatasetServiceImplTests.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import org.junit.jupiter.api.assertThrows
4444
import org.junit.jupiter.api.extension.ExtendWith
4545
import org.springframework.core.io.ByteArrayResource
4646
import org.springframework.data.domain.Page
47+
import org.springframework.data.domain.Pageable
4748
import org.springframework.web.context.request.RequestContextHolder
4849
import org.springframework.web.context.request.ServletRequestAttributes
4950
import redis.clients.jedis.UnifiedJedis
@@ -100,10 +101,10 @@ class DatasetServiceImplTests {
100101
@Test
101102
fun `findAllDatasets should return empty list when no dataset exists`() {
102103
every { organizationService.getVerifiedOrganization(ORGANIZATION_ID) } returns Organization()
103-
every { datasetRepository.findByOrganizationId(ORGANIZATION_ID, any(), any()) } returns
104-
Page.empty()
104+
every { datasetRepository.findAll(any<Pageable>()) } returns Page.empty()
105+
105106
val result = datasetService.findAllDatasets(ORGANIZATION_ID, null, null)
106-
assertEquals(emptyList<Dataset>(), result)
107+
assertEquals(emptyList(), result)
107108
}
108109

109110
@Test

0 commit comments

Comments
 (0)