-
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.
Refactor: Support
BoolConditionMatcher
(#61)
* Refactor: Support `BoolConditionMatcher` * update README.md
- v2.9.9
- v2.9.8
- v2.9.6
- v2.9.5
- v2.9.4
- v2.9.3
- v2.9.2
- v2.9.1
- v2.9.0
- v2.8.2
- v2.8.0
- v2.7.4
- v2.7.3
- v2.7.2
- v2.7.1
- v2.7.0
- v2.6.10
- v2.6.9
- v2.6.8
- v2.6.7
- v2.6.6
- v2.6.5
- v2.6.4
- v2.6.2
- v2.6.0
- v2.5.1
- v2.5.0
- v2.4.3
- v2.4.2
- v2.4.0
- v2.3.3
- v2.3.2
- v2.3.1
- v2.2.9
- v2.2.8
- v2.2.6
- v2.2.5
- v2.2.3
- v2.2.2
- v2.2.0
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v2.0.0-M1
- v1.18.2
- v1.18.0
- v1.17.0
- v1.16.16
- v1.16.15
- v1.16.13
- v1.16.12
- v1.16.11
- v1.16.10
- v1.16.9
- v1.16.8
- v1.16.7
- v1.16.6
- v1.16.5
- v1.16.4
- v1.16.3
- v1.16.2
- v1.16.1
- v1.16.0
- v1.15.4
- v1.15.3
- v1.15.2
- v1.15.0
- v1.12.4
- v1.12.3
- v1.12.2
- v1.12.1
- v1.12.0
- v1.11.1
- v1.11.0
- v1.10.9
- v1.10.8
- v1.10.6
- v1.10.5
- v1.10.4
- v1.10.3
- v1.10.2
- v1.10.1
- v1.10.0
- v1.9.3
- v1.9.2
Showing
10 changed files
with
360 additions
and
6 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
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
72 changes: 72 additions & 0 deletions
72
cosec-core/src/main/kotlin/me/ahoo/cosec/policy/condition/BoolConditionMatcher.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,72 @@ | ||
/* | ||
* 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.policy.condition | ||
|
||
import me.ahoo.cosec.api.configuration.Configuration | ||
import me.ahoo.cosec.api.configuration.asPojo | ||
import me.ahoo.cosec.api.context.SecurityContext | ||
import me.ahoo.cosec.api.context.request.Request | ||
import me.ahoo.cosec.api.policy.ConditionMatcher | ||
|
||
const val BOOL_CONDITION_MATCHER_AND_KEY = "and" | ||
const val BOOL_CONDITION_MATCHER_OR_KEY = "or" | ||
|
||
class BoolConditionMatcher(configuration: Configuration) : AbstractConditionMatcher(configuration) { | ||
override val type: String | ||
get() = BoolConditionMatcherFactory.TYPE | ||
val and: List<ConditionMatcher> = | ||
configuration.get(BoolConditionMatcherFactory.TYPE) | ||
?.get(BOOL_CONDITION_MATCHER_AND_KEY) | ||
?.asList() | ||
?.map { it.asPojo<ConditionMatcher>() }.orEmpty() | ||
val or: List<ConditionMatcher> = | ||
configuration.get(BoolConditionMatcherFactory.TYPE) | ||
?.get(BOOL_CONDITION_MATCHER_OR_KEY) | ||
?.asList() | ||
?.map { it.asPojo<ConditionMatcher>() }.orEmpty() | ||
|
||
override fun internalMatch(request: Request, securityContext: SecurityContext): Boolean { | ||
and.any { | ||
!it.match(request, securityContext) | ||
}.let { | ||
if (it) { | ||
return false | ||
} | ||
} | ||
if (or.isEmpty()) { | ||
return true | ||
} | ||
or.any { | ||
it.match(request, securityContext) | ||
}.let { | ||
if (it) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
} | ||
|
||
class BoolConditionMatcherFactory : ConditionMatcherFactory { | ||
companion object { | ||
const val TYPE = "bool" | ||
} | ||
|
||
override val type: String | ||
get() = TYPE | ||
|
||
override fun create(configuration: Configuration): ConditionMatcher { | ||
return BoolConditionMatcher(configuration) | ||
} | ||
} |
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
137 changes: 137 additions & 0 deletions
137
cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/BoolConditionMatcherTest.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,137 @@ | ||
/* | ||
* 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.policy.condition | ||
|
||
import io.mockk.every | ||
import io.mockk.mockk | ||
import me.ahoo.cosec.api.context.SecurityContext | ||
import me.ahoo.cosec.api.context.request.Request | ||
import me.ahoo.cosec.api.policy.Policy | ||
import me.ahoo.cosec.configuration.JsonConfiguration | ||
import me.ahoo.cosec.configuration.JsonConfiguration.Companion.asConfiguration | ||
import me.ahoo.cosec.policy.MATCHER_TYPE_KEY | ||
import me.ahoo.cosec.serialization.CoSecJsonSerializer | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.* | ||
import org.junit.jupiter.api.Test | ||
|
||
class BoolConditionMatcherTest { | ||
@Test | ||
fun matchWhenEmpty() { | ||
val conditionMatcher = BoolConditionMatcherFactory().create(JsonConfiguration.EMPTY) | ||
assertThat(conditionMatcher.type, `is`(BoolConditionMatcherFactory.TYPE)) | ||
assertThat(conditionMatcher.configuration, `is`(JsonConfiguration.EMPTY)) | ||
assertThat(conditionMatcher.match(mockk(), mockk()), `is`(true)) | ||
} | ||
|
||
@Test | ||
fun matchWhenAndOneAll() { | ||
val conditionMatcher = BoolConditionMatcherFactory().create( | ||
mapOf( | ||
MATCHER_TYPE_KEY to BoolConditionMatcherFactory.TYPE, | ||
BoolConditionMatcherFactory.TYPE to mapOf<String, Any>( | ||
BOOL_CONDITION_MATCHER_AND_KEY to listOf( | ||
mapOf<String, Any>( | ||
MATCHER_TYPE_KEY to AllConditionMatcherFactory.TYPE, | ||
) | ||
) | ||
) | ||
).asConfiguration() | ||
) as BoolConditionMatcher | ||
assertThat(conditionMatcher.type, `is`(BoolConditionMatcherFactory.TYPE)) | ||
assertThat(conditionMatcher.and, hasSize(1)) | ||
assertThat(conditionMatcher.and.first(), instanceOf(AllConditionMatcher::class.java)) | ||
assertThat(conditionMatcher.or, empty()) | ||
assertThat(conditionMatcher.match(mockk(), mockk()), `is`(true)) | ||
} | ||
|
||
@Test | ||
fun matchWhenOrOneAll() { | ||
val conditionMatcher = BoolConditionMatcherFactory().create( | ||
mapOf( | ||
MATCHER_TYPE_KEY to BoolConditionMatcherFactory.TYPE, | ||
BoolConditionMatcherFactory.TYPE to mapOf<String, Any>( | ||
BOOL_CONDITION_MATCHER_OR_KEY to listOf( | ||
mapOf<String, Any>( | ||
MATCHER_TYPE_KEY to AllConditionMatcherFactory.TYPE, | ||
) | ||
) | ||
) | ||
).asConfiguration() | ||
) as BoolConditionMatcher | ||
assertThat(conditionMatcher.type, `is`(BoolConditionMatcherFactory.TYPE)) | ||
assertThat(conditionMatcher.or, hasSize(1)) | ||
assertThat(conditionMatcher.or.first(), instanceOf(AllConditionMatcher::class.java)) | ||
assertThat(conditionMatcher.and, empty()) | ||
assertThat(conditionMatcher.match(mockk(), mockk()), `is`(true)) | ||
} | ||
|
||
@Test | ||
fun matchWhenAndOneAllOrOneAll() { | ||
val conditionMatcher = BoolConditionMatcherFactory().create( | ||
mapOf( | ||
MATCHER_TYPE_KEY to BoolConditionMatcherFactory.TYPE, | ||
BoolConditionMatcherFactory.TYPE to mapOf<String, Any>( | ||
BOOL_CONDITION_MATCHER_AND_KEY to listOf( | ||
mapOf<String, Any>( | ||
MATCHER_TYPE_KEY to AllConditionMatcherFactory.TYPE, | ||
) | ||
), | ||
BOOL_CONDITION_MATCHER_OR_KEY to listOf( | ||
mapOf<String, Any>( | ||
MATCHER_TYPE_KEY to AllConditionMatcherFactory.TYPE, | ||
) | ||
) | ||
) | ||
).asConfiguration() | ||
) as BoolConditionMatcher | ||
assertThat(conditionMatcher.type, `is`(BoolConditionMatcherFactory.TYPE)) | ||
assertThat(conditionMatcher.and, hasSize(1)) | ||
assertThat(conditionMatcher.and.first(), instanceOf(AllConditionMatcher::class.java)) | ||
assertThat(conditionMatcher.or, hasSize(1)) | ||
assertThat(conditionMatcher.or.first(), instanceOf(AllConditionMatcher::class.java)) | ||
assertThat(conditionMatcher.match(mockk(), mockk()), `is`(true)) | ||
} | ||
|
||
@Test | ||
fun matchGivenJson() { | ||
val testPolicy = requireNotNull(javaClass.classLoader.getResource("test-policy.json")).let { resource -> | ||
resource.openStream().use { | ||
CoSecJsonSerializer.readValue(it, Policy::class.java) | ||
} | ||
} | ||
|
||
val conditionMatcher = testPolicy.statements.first { it.name == "AllowDeveloperOrIpRange" }.conditions.first() | ||
assertThat(conditionMatcher, instanceOf(BoolConditionMatcher::class.java)) | ||
val developerId = "developerId" | ||
val matchedContext: SecurityContext = mockk { | ||
every { principal.authenticated() } returns true | ||
every { principal.id } returns developerId | ||
} | ||
val notMatchedContext: SecurityContext = mockk { | ||
every { principal.authenticated() } returns true | ||
every { principal.id } returns "not-developerId" | ||
} | ||
val matchedRequest: Request = mockk { | ||
every { remoteIp } returns "192.168.0.1" | ||
} | ||
val notMatchedRequest: Request = mockk { | ||
every { remoteIp } returns "192.168.1.1" | ||
} | ||
|
||
assertThat(conditionMatcher.match(notMatchedRequest, matchedContext), `is`(true)) | ||
assertThat(conditionMatcher.match(matchedRequest, notMatchedContext), `is`(true)) | ||
assertThat(conditionMatcher.match(notMatchedRequest, notMatchedContext), `is`(false)) | ||
} | ||
} |
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
Oops, something went wrong.