Skip to content

Commit 7603d29

Browse files
committed
Add csmPlatformProperties.identity.adminGroup in CsmAdmin.verifyRolesAdmin
1 parent 3c960f4 commit 7603d29

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/main/kotlin/com/cosmotech/api/rbac/CsmAdmin.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ class CsmAdmin(val csmPlatformProperties: CsmPlatformProperties) {
1515

1616
fun verifyRolesAdmin(roles: List<String>): Boolean {
1717
logger.debug("Verifying if token roles contains Platform Admin")
18-
return roles.contains(ROLE_PLATFORM_ADMIN)
18+
val customAdminGroup = csmPlatformProperties.identityProvider?.adminGroup
19+
if (customAdminGroup.isNullOrBlank()) {
20+
return roles.contains(ROLE_PLATFORM_ADMIN)
21+
}
22+
return roles.any { it == ROLE_PLATFORM_ADMIN || it == customAdminGroup }
1923
}
2024

2125
fun verifyCurrentRolesAdmin(): Boolean {

src/test/kotlin/com/cosmotech/api/rbac/CsmRbacTests.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,15 @@ const val OWNER_ID = "3a869905-e9f5-4851-a7a9-3079aad49dfa"
5656
const val USER_ID = "2a869905-e9f5-4851-a7a9-3079aad49dfb"
5757
const val COMPONENT_ID = "component_id"
5858

59+
@Suppress("LargeClass")
5960
class CsmRbacTests {
6061
private val ROLE_NONE_PERMS: List<String> = listOf()
6162
private val ROLE_READER_PERMS = listOf(PERM_READ)
6263
private val ROLE_WRITER_PERMS = listOf(PERM_READ, PERM_WRITE)
6364
private val ROLE_ADMIN_PERMS = listOf(PERM_ADMIN)
65+
val CUSTOM_ADMIN_GROUP = "MyCustomAdminGroup"
66+
val CUSTOM_USER_GROUP = "MyCustomUserGroup"
67+
val CUSTOM_VIEWER_GROUP = "MyCustomViewerGroup"
6468

6569
private val USER_READER_ROLE = ROLE_READER
6670
private val USER_WRITER_ROLE = ROLE_WRITER
@@ -82,13 +86,23 @@ class CsmRbacTests {
8286
lateinit var parentRbacSecurity: RbacSecurity
8387
lateinit var rbacSecurity: RbacSecurity
8488

89+
private val DEFAULT_IDENTITY_PROVIDER =
90+
CsmPlatformProperties.CsmIdentityProvider(
91+
"identityProviderCode",
92+
authorizationUrl = "http://my-fake-authorization.url/autorize",
93+
tokenUrl = "http://my-fake-token.url/token",
94+
adminGroup = CUSTOM_ADMIN_GROUP,
95+
userGroup = CUSTOM_USER_GROUP,
96+
viewerGroup = CUSTOM_VIEWER_GROUP)
97+
8598
@BeforeTest
8699
fun beforeEachTest() {
87100
logger.trace("Begin test")
88101
csmPlatformProperties = mockk<CsmPlatformProperties>(relaxed = true)
89102
every { csmPlatformProperties.rbac.enabled } answers { true }
90103
every { csmPlatformProperties.authorization.rolesJwtClaim } answers { "roles" }
91104
every { csmPlatformProperties.authorization.mailJwtClaim } answers { "upn" }
105+
every { csmPlatformProperties.identityProvider } answers { DEFAULT_IDENTITY_PROVIDER }
92106
rolesDefinition =
93107
RolesDefinition(
94108
adminRole = ROLE_ADMIN,
@@ -161,6 +175,24 @@ class CsmRbacTests {
161175
assertTrue(admin.verifyRolesAdmin(userRoles))
162176
}
163177

178+
@Test
179+
fun `Custom role Platform Admin OK`() {
180+
val userRoles = listOf(CUSTOM_ADMIN_GROUP)
181+
assertTrue(admin.verifyRolesAdmin(userRoles))
182+
}
183+
184+
@Test
185+
fun `Custom role and regular Platform Admin OK`() {
186+
val userRoles = listOf(CUSTOM_ADMIN_GROUP, ROLE_PLATFORM_ADMIN)
187+
assertTrue(admin.verifyRolesAdmin(userRoles))
188+
}
189+
190+
@Test
191+
fun `Custom role Platform Admin NOK`() {
192+
val userRoles = listOf(CUSTOM_USER_GROUP, CUSTOM_VIEWER_GROUP)
193+
assertFalse(admin.verifyRolesAdmin(userRoles))
194+
}
195+
164196
@Test
165197
fun `roles with Platform Admin OK`() {
166198
val userRoles = listOf(ROLE_PLATFORM_ADMIN, ROLE_ORGANIZATION_USER)

0 commit comments

Comments
 (0)