-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
OpenAPIAppPermissionGenerator
(#334)
- Loading branch information
Showing
6 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
cosec-generator/src/main/kotlin/me/ahoo/cosec/generator/OpenAPIAppPermissionGenerator.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,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, | ||
) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
cosec-generator/src/test/kotlin/me/ahoo/cosec/generator/OpenAPIAppPermissionGeneratorTest.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,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)) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...n/kotlin/me/ahoo/cosec/spring/boot/starter/actuate/CoSecAppPermissionGeneratorEndpoint.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,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) | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
...tlin/me/ahoo/cosec/spring/boot/starter/actuate/CoSecAppPermissionGeneratorEndpointTest.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,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()) | ||
} | ||
} |
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