-
Notifications
You must be signed in to change notification settings - Fork 27
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
CORE-20721: Add RBAC Group rest resource #6233
Merged
Tom-Fitzpatrick
merged 19 commits into
release/os/5.3
from
tomf/CORE-20721/add-rbac-group-rest-resource
Jul 1, 2024
Merged
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5720d46
initial classes for RBAC group work
Tom-Fitzpatrick fff2cf8
add Create Operation to RBAC Group
Tom-Fitzpatrick e370f86
new endpoints for getGroupContent, add and remove Role operations to …
Tom-Fitzpatrick 89be24b
delete and changeParentId endpoints emplemented
Tom-Fitzpatrick 5c38681
add GroupEndpointImplTest
Tom-Fitzpatrick e332493
update GroupEndpointImpl to be PluggableRestResource
Tom-Fitzpatrick 8c8f8a9
add PermissionGroupManagerImplTest
Tom-Fitzpatrick 42a8264
detekt
Tom-Fitzpatrick 0f968fb
point to alpha version
Tom-Fitzpatrick 6a7c1b0
Merge branch 'release/os/5.3' into tomf/CORE-20721/add-rbac-group-res…
Tom-Fitzpatrick 5e381c6
add version to Group data types
Tom-Fitzpatrick e4d3e92
Add HttpRestResource annotation to GroupEndpoint, update swagger base…
Tom-Fitzpatrick 2df9c23
bump alpha version
Tom-Fitzpatrick 28e8c83
remove wildcard import, address comments
Tom-Fitzpatrick d5d8f54
fix detekt
Tom-Fitzpatrick 527c1cd
Update DeleteGroup description to specify requirement for group to be…
Tom-Fitzpatrick 277b28c
fix detekt
Tom-Fitzpatrick 126d254
update baseline with new description
Tom-Fitzpatrick 8271d6f
point back to cordaApi beta version
Tom-Fitzpatrick 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
132 changes: 132 additions & 0 deletions
132
...l/src/main/kotlin/net/corda/libs/permissions/endpoints/v1/group/impl/GroupEndpointImpl.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,132 @@ | ||
package net.corda.libs.permissions.endpoints.v1.group.impl | ||
|
||
import net.corda.libs.permissions.endpoints.common.PermissionEndpointEventHandler | ||
import net.corda.libs.permissions.endpoints.common.withPermissionManager | ||
import net.corda.libs.permissions.endpoints.v1.converter.convertToDto | ||
import net.corda.libs.permissions.endpoints.v1.converter.convertToEndpointType | ||
import net.corda.libs.permissions.endpoints.v1.group.GroupEndpoint | ||
import net.corda.libs.permissions.endpoints.v1.group.types.CreateGroupType | ||
import net.corda.libs.permissions.endpoints.v1.group.types.GroupContentResponseType | ||
import net.corda.libs.permissions.endpoints.v1.group.types.GroupResponseType | ||
import net.corda.libs.permissions.endpoints.v1.user.UserEndpoint | ||
import net.corda.libs.permissions.manager.request.AddRoleToGroupRequestDto | ||
import net.corda.libs.permissions.manager.request.ChangeGroupParentIdDto | ||
import net.corda.libs.permissions.manager.request.DeleteGroupRequestDto | ||
import net.corda.libs.permissions.manager.request.RemoveRoleFromGroupRequestDto | ||
import net.corda.libs.platform.PlatformInfoProvider | ||
import net.corda.lifecycle.Lifecycle | ||
import net.corda.lifecycle.LifecycleCoordinatorFactory | ||
import net.corda.lifecycle.createCoordinator | ||
import net.corda.permissions.management.PermissionManagementService | ||
import net.corda.rest.PluggableRestResource | ||
import net.corda.rest.exception.ExceptionDetails | ||
import net.corda.rest.exception.ResourceNotFoundException | ||
import net.corda.rest.response.ResponseEntity | ||
import net.corda.rest.security.CURRENT_REST_CONTEXT | ||
import org.osgi.service.component.annotations.Activate | ||
import org.osgi.service.component.annotations.Component | ||
import org.osgi.service.component.annotations.Reference | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
@Component(service = [PluggableRestResource::class]) | ||
class GroupEndpointImpl @Activate constructor( | ||
@Reference(service = LifecycleCoordinatorFactory::class) | ||
private val coordinatorFactory: LifecycleCoordinatorFactory, | ||
@Reference(service = PermissionManagementService::class) | ||
private val permissionManagementService: PermissionManagementService, | ||
@Reference(service = PlatformInfoProvider::class) | ||
private val platformInfoProvider: PlatformInfoProvider | ||
) : GroupEndpoint, PluggableRestResource<UserEndpoint>, Lifecycle { | ||
|
||
private companion object { | ||
val logger: Logger = LoggerFactory.getLogger(this::class.java.enclosingClass) | ||
} | ||
|
||
override val targetInterface: Class<UserEndpoint> = UserEndpoint::class.java | ||
|
||
override val protocolVersion get() = platformInfoProvider.localWorkerPlatformVersion | ||
|
||
private val coordinator = coordinatorFactory.createCoordinator<UserEndpoint>( | ||
PermissionEndpointEventHandler("UserEndpoint") | ||
) | ||
|
||
override fun createGroup(createGroupType: CreateGroupType): ResponseEntity<GroupResponseType> { | ||
val principal = getRestThreadLocalContext() | ||
|
||
val createGroupResult = withPermissionManager(permissionManagementService.permissionManager, logger) { | ||
createGroup(createGroupType.convertToDto(principal)) | ||
} | ||
|
||
return ResponseEntity.created(createGroupResult.convertToEndpointType()) | ||
} | ||
|
||
override fun changeParentGroup(groupId: String, newParentGroupId: String): ResponseEntity<GroupResponseType> { | ||
val principal = getRestThreadLocalContext() | ||
|
||
val groupResponseDto = withPermissionManager(permissionManagementService.permissionManager, logger) { | ||
try { | ||
changeParentGroup(ChangeGroupParentIdDto(principal, groupId, newParentGroupId)) | ||
} catch (e: NoSuchElementException) { | ||
throw ResourceNotFoundException( | ||
e::class.java.simpleName, | ||
ExceptionDetails(e::class.java.name, e.message ?: "No resource found for this request.") | ||
) | ||
} | ||
} | ||
|
||
return ResponseEntity.updated(groupResponseDto.convertToEndpointType()) | ||
} | ||
|
||
override fun addRole(groupId: String, roleId: String): ResponseEntity<GroupResponseType> { | ||
val principal = getRestThreadLocalContext() | ||
|
||
val result = withPermissionManager(permissionManagementService.permissionManager, logger) { | ||
addRoleToGroup(AddRoleToGroupRequestDto(principal, groupId, roleId)) | ||
} | ||
return ResponseEntity.updated(result.convertToEndpointType()) | ||
} | ||
|
||
override fun removeRole(groupId: String, roleId: String): ResponseEntity<GroupResponseType> { | ||
val principal = getRestThreadLocalContext() | ||
|
||
val result = withPermissionManager(permissionManagementService.permissionManager, logger) { | ||
removeRoleFromGroup(RemoveRoleFromGroupRequestDto(principal, groupId, roleId)) | ||
} | ||
return ResponseEntity.updated(result.convertToEndpointType()) | ||
} | ||
|
||
override fun getGroupContent(groupId: String): GroupContentResponseType { | ||
val groupContentResponseDto = withPermissionManager(permissionManagementService.permissionManager, logger) { | ||
getGroupContent(groupId) | ||
} ?: throw ResourceNotFoundException("Group", groupId) | ||
|
||
return groupContentResponseDto.convertToEndpointType() | ||
} | ||
|
||
override fun deleteGroup(groupId: String): ResponseEntity<GroupResponseType> { | ||
val principal = getRestThreadLocalContext() | ||
|
||
val groupResponseDto = withPermissionManager(permissionManagementService.permissionManager, logger) { | ||
deleteGroup(DeleteGroupRequestDto(principal, groupId)) | ||
} | ||
|
||
return ResponseEntity.deleted(groupResponseDto.convertToEndpointType()) | ||
} | ||
|
||
private fun getRestThreadLocalContext(): String { | ||
val restContext = CURRENT_REST_CONTEXT.get() | ||
return restContext.principal | ||
} | ||
|
||
override val isRunning: Boolean | ||
get() = coordinator.isRunning | ||
|
||
override fun start() { | ||
coordinator.start() | ||
} | ||
|
||
override fun stop() { | ||
coordinator.stop() | ||
} | ||
} |
176 changes: 176 additions & 0 deletions
176
...c/test/kotlin/net/corda/libs/permissions/endpoints/v1/group/impl/GroupEndpointImplTest.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,176 @@ | ||
package net.corda.libs.permissions.endpoints.v1.group.impl | ||
|
||
import net.corda.libs.permissions.endpoints.v1.group.types.CreateGroupType | ||
import net.corda.libs.permissions.manager.PermissionManager | ||
import net.corda.libs.permissions.manager.request.AddRoleToGroupRequestDto | ||
import net.corda.libs.permissions.manager.request.CreateGroupRequestDto | ||
import net.corda.libs.permissions.manager.request.DeleteGroupRequestDto | ||
import net.corda.libs.permissions.manager.request.RemoveRoleFromGroupRequestDto | ||
import net.corda.libs.permissions.manager.response.GroupContentResponseDto | ||
import net.corda.libs.permissions.manager.response.GroupResponseDto | ||
import net.corda.libs.platform.PlatformInfoProvider | ||
import net.corda.lifecycle.LifecycleCoordinatorFactory | ||
import net.corda.permissions.management.PermissionManagementService | ||
import net.corda.rest.ResponseCode | ||
import net.corda.rest.security.CURRENT_REST_CONTEXT | ||
import net.corda.rest.security.RestAuthContext | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertNotNull | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.argumentCaptor | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
import java.time.Instant | ||
import java.util.UUID | ||
|
||
internal class GroupEndpointImplTest { | ||
|
||
private val now = Instant.now() | ||
private val parentGroup = UUID.randomUUID().toString() | ||
|
||
private val createGroupType = CreateGroupType( | ||
"groupName1", | ||
parentGroup | ||
) | ||
|
||
private val groupResponseDto = GroupResponseDto( | ||
"uuid", | ||
now, | ||
"groupName1", | ||
parentGroup, | ||
emptyList(), | ||
emptyList(), | ||
) | ||
|
||
private val groupContentResponseDto = GroupContentResponseDto( | ||
"uuid", | ||
now, | ||
"groupName1", | ||
parentGroup, | ||
emptyList(), | ||
emptyList(), | ||
emptyList(), | ||
emptyList() | ||
) | ||
|
||
private val permissionManager = mock<PermissionManager>() | ||
private val permissionService = mock<PermissionManagementService>().also { | ||
whenever(it.permissionManager).thenReturn(permissionManager) | ||
} | ||
|
||
private val platformInfoProvider = mock<PlatformInfoProvider>().also { | ||
whenever(it.localWorkerPlatformVersion).thenReturn(1) | ||
} | ||
|
||
private val lifecycleCoordinatorFactory = mock<LifecycleCoordinatorFactory>() | ||
|
||
private val endpoint = GroupEndpointImpl(lifecycleCoordinatorFactory, permissionService, platformInfoProvider) | ||
|
||
@BeforeEach | ||
fun beforeEach() { | ||
val authContext = mock<RestAuthContext>().apply { | ||
whenever(principal).thenReturn("aRestUser") | ||
} | ||
CURRENT_REST_CONTEXT.set(authContext) | ||
} | ||
|
||
@Test | ||
fun `create a group successfully`() { | ||
val createGroupDtoCapture = argumentCaptor<CreateGroupRequestDto>() | ||
whenever(permissionManager.createGroup(createGroupDtoCapture.capture())).thenReturn(groupResponseDto) | ||
|
||
val response = endpoint.createGroup(createGroupType) | ||
val responseType = response.responseBody | ||
|
||
assertEquals(ResponseCode.CREATED, response.responseCode) | ||
assertNotNull(responseType) | ||
assertEquals("uuid", responseType.id) | ||
assertEquals(now, responseType.updateTimestamp) | ||
assertEquals("groupName1", responseType.name) | ||
assertEquals(parentGroup, responseType.parentGroupId) | ||
|
||
val capturedDto = createGroupDtoCapture.firstValue | ||
assertEquals("groupName1", capturedDto.groupName) | ||
assertEquals(parentGroup, capturedDto.parentGroupId) | ||
} | ||
|
||
@Test | ||
fun `get a group successfully`() { | ||
whenever(permissionManager.getGroupContent(any())).thenReturn(groupContentResponseDto) | ||
|
||
val groupId = "uuid" | ||
val responseType = endpoint.getGroupContent(groupId) | ||
|
||
assertNotNull(responseType) | ||
assertEquals("uuid", responseType.id) | ||
assertEquals(now, responseType.updateTimestamp) | ||
assertEquals("groupName1", responseType.name) | ||
assertEquals(parentGroup, responseType.parentGroupId) | ||
} | ||
|
||
@Test | ||
fun `add role to group`() { | ||
val addRoleDtoCapture = argumentCaptor<AddRoleToGroupRequestDto>() | ||
whenever(permissionManager.addRoleToGroup(addRoleDtoCapture.capture())).thenReturn(groupResponseDto) | ||
|
||
val groupId = "uuid" | ||
val roleId = "roleId1" | ||
val response = endpoint.addRole(groupId, roleId) | ||
val responseType = response.responseBody | ||
|
||
assertEquals(ResponseCode.OK, response.responseCode) | ||
assertNotNull(responseType) | ||
assertEquals("uuid", responseType.id) | ||
assertEquals(now, responseType.updateTimestamp) | ||
assertEquals("groupName1", responseType.name) | ||
assertEquals(parentGroup, responseType.parentGroupId) | ||
|
||
val capturedDto = addRoleDtoCapture.firstValue | ||
assertEquals(groupId, capturedDto.groupId) | ||
assertEquals(roleId, capturedDto.roleId) | ||
} | ||
|
||
@Test | ||
fun `remove role from group`() { | ||
val removeRoleDtoCapture = argumentCaptor<RemoveRoleFromGroupRequestDto>() | ||
whenever(permissionManager.removeRoleFromGroup(removeRoleDtoCapture.capture())).thenReturn(groupResponseDto) | ||
|
||
val groupId = "uuid" | ||
val roleId = "roleId1" | ||
val response = endpoint.removeRole(groupId, roleId) | ||
val responseType = response.responseBody | ||
|
||
assertEquals(ResponseCode.OK, response.responseCode) | ||
assertNotNull(responseType) | ||
assertEquals("uuid", responseType.id) | ||
assertEquals(now, responseType.updateTimestamp) | ||
assertEquals("groupName1", responseType.name) | ||
assertEquals(parentGroup, responseType.parentGroupId) | ||
|
||
val capturedDto = removeRoleDtoCapture.firstValue | ||
assertEquals(groupId, capturedDto.groupId) | ||
assertEquals(roleId, capturedDto.roleId) | ||
} | ||
|
||
@Test | ||
fun `delete a group`() { | ||
val deleteGroupDtoCapture = argumentCaptor<DeleteGroupRequestDto>() | ||
whenever(permissionManager.deleteGroup(deleteGroupDtoCapture.capture())).thenReturn(groupResponseDto) | ||
|
||
val groupId = "uuid" | ||
val response = endpoint.deleteGroup(groupId) | ||
val responseType = response.responseBody | ||
|
||
assertEquals(ResponseCode.OK, response.responseCode) | ||
assertNotNull(responseType) | ||
assertEquals("uuid", responseType.id) | ||
assertEquals(now, responseType.updateTimestamp) | ||
assertEquals("groupName1", responseType.name) | ||
assertEquals(parentGroup, responseType.parentGroupId) | ||
|
||
val capturedDto = deleteGroupDtoCapture.firstValue | ||
assertEquals(groupId, capturedDto.groupId) | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
...on-endpoint/src/main/java/net/corda/libs/permissions/endpoints/v1/group/package-info.java
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,4 @@ | ||
@Export | ||
package net.corda.libs.permissions.endpoints.v1.group; | ||
|
||
import org.osgi.annotation.bundle.Export; |
4 changes: 4 additions & 0 deletions
4
...point/src/main/java/net/corda/libs/permissions/endpoints/v1/group/types/package-info.java
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,4 @@ | ||
@Export | ||
package net.corda.libs.permissions.endpoints.v1.group.types; | ||
|
||
import org.osgi.annotation.bundle.Export; |
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.
I will revert this once corda/corda-api#1666 has been merged