Skip to content

Commit 68767bf

Browse files
committed
feat(authorization): Trigger migration automatically
Instead of doing a synchronization of roles in Keycloak, trigger a migration to new access rights structures on startup of the core component. Signed-off-by: Oliver Heger <[email protected]>
1 parent be5428f commit 68767bf

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

core/src/main/kotlin/di/Module.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ package org.eclipse.apoapsis.ortserver.core.di
2222
import com.typesafe.config.ConfigFactory
2323

2424
import io.ktor.server.config.ApplicationConfig
25+
import io.ktor.server.config.tryGetString
2526

2627
import kotlinx.serialization.json.Json
2728

2829
import org.eclipse.apoapsis.ortserver.clients.keycloak.DefaultKeycloakClient
2930
import org.eclipse.apoapsis.ortserver.clients.keycloak.KeycloakClient
31+
import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.migration.RolesToDbMigration
3032
import org.eclipse.apoapsis.ortserver.components.authorization.service.AuthorizationService
3133
import org.eclipse.apoapsis.ortserver.components.authorization.service.DbAuthorizationService
3234
import org.eclipse.apoapsis.ortserver.components.authorization.service.KeycloakUserService
@@ -210,4 +212,17 @@ fun ortServerModule(config: ApplicationConfig, db: Database?, authorizationServi
210212
singleOf(::PluginService)
211213
singleOf(::PluginTemplateEventStore)
212214
singleOf(::PluginTemplateService)
215+
216+
single { RolesToDbMigration(get(), get(), getKeycloakGroupPrefix(config), get()) }
213217
}
218+
219+
/**
220+
* Retrieve the prefix for Keycloak groups representing roles for hierarchy elements from the given [config]. This is
221+
* needed for the migration of roles managed by Keycloak to roles stored in the database. The prefix is obtained from
222+
* the configuration of the authorization component based on Keycloak. It is, however, possible to override it via a
223+
* special property for the migration. This is useful for instance, to test the migration on different ORT Server
224+
* deployments, e.g. a test environment.
225+
*/
226+
private fun getKeycloakGroupPrefix(config: ApplicationConfig): String =
227+
config.tryGetString("keycloak.migrationGroupPrefix")
228+
?: config.tryGetString("keycloak.groupPrefix").orEmpty()

core/src/main/kotlin/plugins/Lifecycle.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import kotlin.concurrent.thread
2626

2727
import kotlinx.coroutines.Dispatchers
2828
import kotlinx.coroutines.launch
29+
import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.migration.RolesToDbMigration
2930

30-
import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.service.AuthorizationService
3131
import org.eclipse.apoapsis.ortserver.utils.logging.runBlocking
3232
import org.eclipse.apoapsis.ortserver.utils.logging.withMdcContext
3333

@@ -41,26 +41,28 @@ import org.slf4j.MDC
4141
*/
4242
fun Application.configureLifecycle() {
4343
monitor.subscribe(ApplicationStarted) {
44-
val authorizationService by inject<AuthorizationService>()
44+
val rolesMigration by inject<RolesToDbMigration>()
4545

4646
val mdcContext = MDC.getCopyOfContextMap()
4747

4848
thread {
4949
MDC.setContextMap(mdcContext)
5050
runBlocking(Dispatchers.IO) {
51-
syncRoles(authorizationService)
51+
migrateRoles(rolesMigration)
5252
}
5353
}
5454
}
5555
}
5656

5757
/**
58-
* Trigger the synchronization of permissions and roles in Keycloak. The synchronization then runs in the background.
58+
* Perform a migration to new database-based structures for access rights if necessary. This makes sure that the
59+
* new structures are populated once when switching from access rights stored in Keycloak to the new storage in the
60+
* database. The migration then runs in the background.
5961
*/
60-
private suspend fun syncRoles(authorizationService: AuthorizationService) {
62+
private suspend fun migrateRoles(migration: RolesToDbMigration) {
6163
withMdcContext("component" to "core") {
6264
launch {
63-
authorizationService.ensureSuperuserAndSynchronizeRolesAndPermissions()
65+
migration.migrateRolesToDb()
6466
}
6567
}
6668
}

core/src/main/resources/application.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ keycloak {
7474
subjectClientId = ${?KEYCLOAK_SUBJECT_CLIENT_ID}
7575
groupPrefix = ""
7676
groupPrefix = ${?KEYCLOAK_GROUP_PREFIX}
77+
migrationGroupPrefix = ${?KEYCLOAK_MIGRATION_GROUP_PREFIX}
7778
timeoutSeconds = 60
7879
timeoutSeconds = ${?KEYCLOAK_TIMEOUT_SECONDS}
7980
}

0 commit comments

Comments
 (0)