-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support dynamic configuration of security policies (#144)
- Loading branch information
Showing
5 changed files
with
98 additions
and
31 deletions.
There are no files selected for viewing
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
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
55 changes: 55 additions & 0 deletions
55
cosky-rest-api/src/main/kotlin/me/ahoo/cosky/rest/security/authorization/CoSkyPolicy.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,55 @@ | ||
package me.ahoo.cosky.rest.security.authorization | ||
|
||
import me.ahoo.cosec.api.policy.Policy | ||
import me.ahoo.cosec.serialization.CoSecJsonSerializer | ||
import me.ahoo.cosky.config.ConfigEventListenerContainer | ||
import me.ahoo.cosky.config.ConfigService | ||
import me.ahoo.cosky.core.Namespaced | ||
import me.ahoo.cosky.rest.security.authorization.InitialPolicyLoader.policyResourceName | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.InitializingBean | ||
import org.springframework.stereotype.Service | ||
import reactor.core.publisher.Mono | ||
import reactor.kotlin.core.publisher.switchIfEmpty | ||
import reactor.kotlin.core.publisher.toMono | ||
|
||
@Service | ||
class CoSkyPolicy( | ||
private val configService: ConfigService, | ||
private val configEventListenerContainer: ConfigEventListenerContainer | ||
) : InitializingBean { | ||
companion object { | ||
const val policyId = policyResourceName | ||
val namespacedPolicyId = me.ahoo.cosky.config.NamespacedConfigId(Namespaced.SYSTEM, policyId) | ||
private val log = LoggerFactory.getLogger(CoSkyPolicy::class.java) | ||
} | ||
|
||
private var policyCache: Mono<Policy> = getPolicyCache() | ||
|
||
override fun afterPropertiesSet() { | ||
configEventListenerContainer.receive(namespacedPolicyId) | ||
.subscribe { | ||
if (log.isInfoEnabled) { | ||
log.info("Policy[{}] is updated - ${it.event}.", policyId) | ||
} | ||
policyCache = getPolicyCache() | ||
} | ||
} | ||
|
||
private fun getPolicyCache(): Mono<Policy> { | ||
return configService.getConfig(Namespaced.SYSTEM, policyId) | ||
.map { config -> | ||
CoSecJsonSerializer.readValue(config.data, Policy::class.java) | ||
}.switchIfEmpty { | ||
if (log.isInfoEnabled) { | ||
log.info("Policy[{}] is not found, using initial policy.", policyId) | ||
} | ||
InitialPolicyLoader.policy.toMono() | ||
}.cache() | ||
} | ||
|
||
fun getPolicy(): Mono<Policy> { | ||
return policyCache | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...rest-api/src/main/kotlin/me/ahoo/cosky/rest/security/authorization/InitialPolicyLoader.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,18 @@ | ||
package me.ahoo.cosky.rest.security.authorization | ||
|
||
import me.ahoo.cosec.api.policy.Policy | ||
import me.ahoo.cosec.policy.DefaultPolicyEvaluator | ||
import me.ahoo.cosec.serialization.CoSecJsonSerializer | ||
|
||
object InitialPolicyLoader { | ||
const val policyResourceName = "cosky-policy.json" | ||
val policy: Policy by lazy(this) { | ||
requireNotNull(javaClass.classLoader.getResource(policyResourceName)).let { resource -> | ||
resource.openStream().use { | ||
val policy = CoSecJsonSerializer.readValue(it, Policy::class.java) | ||
DefaultPolicyEvaluator.evaluate(policy) | ||
policy | ||
} | ||
} | ||
} | ||
} |
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