Skip to content

Commit

Permalink
feat: add OpenAPIAppPermissionGenerator (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Apr 8, 2024
1 parent 51c2453 commit 7cdb9be
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.cosec.generator

import io.swagger.v3.oas.models.OpenAPI
import me.ahoo.cosec.api.permission.AppPermission
import me.ahoo.cosec.api.permission.Permission
import me.ahoo.cosec.configuration.JsonConfiguration.Companion.asConfiguration
import me.ahoo.cosec.permission.AppPermissionData
import me.ahoo.cosec.permission.PermissionData
import me.ahoo.cosec.permission.PermissionGroupData
import me.ahoo.cosec.policy.action.ACTION_MATCHER_METHOD_KEY
import me.ahoo.cosec.policy.action.PathActionMatcherFactory

object OpenAPIAppPermissionGenerator {
private const val APP_ID = "AppId"

fun generate(openAPI: OpenAPI): AppPermission {
val tagGroupOperations = mutableMapOf<String, MutableList<Permission>>()

for ((path, item) in openAPI.paths.orEmpty()) {
for ((method, operation) in item.readOperationsMap()) {
val action = PathActionMatcherFactory.INSTANCE.create(
mapOf<String, String>(
PathActionMatcherFactory.PATTERN_KEY to path,
ACTION_MATCHER_METHOD_KEY to method.name
).asConfiguration()
)
val permission = PermissionData(
id = operation.operationId.orEmpty(),
name = operation.summary.orEmpty(),
description = operation.description.orEmpty(),
action = action
)
for (tag in operation.tags.orEmpty()) {
val grouped = tagGroupOperations[tag]
if (grouped == null) {
tagGroupOperations[tag] = mutableListOf(permission)
} else {
grouped.add(permission)
}
}
}
}

val groups = tagGroupOperations.map { (tag, permissions) ->
PermissionGroupData(
name = tag,
description = "",
permissions = permissions
)
}
val appId = openAPI.info?.title ?: APP_ID
return AppPermissionData(
id = appId,
groups = groups,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package me.ahoo.cosec.generator

import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.Operation
import io.swagger.v3.oas.models.PathItem
import io.swagger.v3.oas.models.Paths
import io.swagger.v3.oas.models.info.Info
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.*
import org.hamcrest.Matchers.*
import org.junit.jupiter.api.Test

class OpenAPIAppPermissionGeneratorTest {

@Test
fun generate() {
val openAPI = OpenAPI().apply {
info = Info().title("test")
paths = Paths()
PathItem()
.summary("test")
.get(Operation().tags(listOf("test")).summary("get"))
.post(Operation().tags(listOf("test")).summary("set"))
.also {
paths.addPathItem("/test", it)
}
PathItem()
.summary("test2")
.get(Operation().tags(listOf("test2")))
.also {
paths.addPathItem("/test2", it)
}
}

val appPermission = OpenAPIAppPermissionGenerator.generate(openAPI)
assertThat(appPermission.id, equalTo("test"))
assertThat(appPermission.groups, hasSize(2))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.cosec.spring.boot.starter.actuate

import io.swagger.v3.oas.models.OpenAPI
import me.ahoo.cosec.api.permission.AppPermission
import me.ahoo.cosec.generator.OpenAPIAppPermissionGenerator
import org.springframework.beans.factory.ObjectProvider
import org.springframework.boot.actuate.endpoint.annotation.Endpoint
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation

@Endpoint(id = "cosecAppPermissionGenerator")
class CoSecAppPermissionGeneratorEndpoint(private val openAPIProvider: ObjectProvider<OpenAPI>) {
@ReadOperation
fun generate(): AppPermission? {
val openAPI = openAPIProvider.getIfAvailable() ?: return null
return OpenAPIAppPermissionGenerator.generate(openAPI)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ class CoSecEndpointAutoConfiguration {
fun coSecPolicyGeneratorEndpoint(openAPIProvider: ObjectProvider<OpenAPI>): CoSecPolicyGeneratorEndpoint {
return CoSecPolicyGeneratorEndpoint(openAPIProvider)
}

@Bean
@ConditionalOnClass(value = [OpenAPI::class])
fun coSecAppPermissionGeneratorEndpoint(
openAPIProvider: ObjectProvider<OpenAPI>
): CoSecAppPermissionGeneratorEndpoint {
return CoSecAppPermissionGeneratorEndpoint(openAPIProvider)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.ahoo.cosec.spring.boot.starter.actuate

import io.mockk.every
import io.mockk.mockk
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.Paths
import org.hamcrest.MatcherAssert.*
import org.hamcrest.Matchers.*
import org.junit.jupiter.api.Test

class CoSecAppPermissionGeneratorEndpointTest {

@Test
fun generate() {
val appPermission = CoSecAppPermissionGeneratorEndpoint(
mockk {
every { getIfAvailable() } returns OpenAPI().paths(Paths())
}
).generate()

assertThat(appPermission, notNullValue())
}

@Test
fun generateIfNull() {
val appPermission = CoSecAppPermissionGeneratorEndpoint(
mockk {
every { getIfAvailable() } returns null
}
).generate()

assertThat(appPermission, nullValue())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CoSecEndpointAutoConfigurationTest {
.run { context: AssertableApplicationContext ->
AssertionsForInterfaceTypes.assertThat(context)
.hasSingleBean(CoSecPolicyGeneratorEndpoint::class.java)
.hasSingleBean(CoSecAppPermissionGeneratorEndpoint::class.java)
}
}
}

0 comments on commit 7cdb9be

Please sign in to comment.