-
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.
* Feature: support `negate` for `ConditionMatcher`
- Loading branch information
Showing
10 changed files
with
111 additions
and
23 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
32 changes: 32 additions & 0 deletions
32
cosec-core/src/main/kotlin/me/ahoo/cosec/policy/condition/AbstractConditionMatcher.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,32 @@ | ||
/* | ||
* 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.context.SecurityContext | ||
import me.ahoo.cosec.api.context.request.Request | ||
import me.ahoo.cosec.api.policy.ConditionMatcher | ||
|
||
const val CONDITION_MATCHER_NEGATE_KEY = "negate" | ||
|
||
abstract class AbstractConditionMatcher(final override val configuration: Configuration) : ConditionMatcher { | ||
protected val negate: Boolean = configuration.get(CONDITION_MATCHER_NEGATE_KEY)?.asBoolean() ?: false | ||
|
||
override fun match(request: Request, securityContext: SecurityContext): Boolean { | ||
val match = internalMatch(request, securityContext) | ||
return if (negate) !match else match | ||
} | ||
|
||
protected abstract fun internalMatch(request: Request, securityContext: SecurityContext): Boolean | ||
} |
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
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
53 changes: 53 additions & 0 deletions
53
cosec-core/src/test/kotlin/me/ahoo/cosec/policy/condition/AbstractConditionMatcherTest.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,53 @@ | ||
/* | ||
* 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.mockk | ||
import me.ahoo.cosec.api.context.SecurityContext | ||
import me.ahoo.cosec.api.context.request.Request | ||
import me.ahoo.cosec.configuration.JsonConfiguration | ||
import me.ahoo.cosec.configuration.JsonConfiguration.Companion.asConfiguration | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.* | ||
import org.junit.jupiter.api.Test | ||
|
||
class AbstractConditionMatcherTest { | ||
|
||
@Test | ||
fun match() { | ||
val conditionMatcher = | ||
object : AbstractConditionMatcher(mapOf(CONDITION_MATCHER_NEGATE_KEY to true).asConfiguration()) { | ||
override fun internalMatch(request: Request, securityContext: SecurityContext): Boolean { | ||
return true | ||
} | ||
|
||
override val type: String | ||
get() = throw UnsupportedOperationException() | ||
} | ||
assertThat(conditionMatcher.match(mockk(), mockk()), equalTo(false)) | ||
} | ||
|
||
@Test | ||
fun matchWhenNegateIsNull() { | ||
val conditionMatcher = object : AbstractConditionMatcher(JsonConfiguration.EMPTY) { | ||
override fun internalMatch(request: Request, securityContext: SecurityContext): Boolean { | ||
return true | ||
} | ||
|
||
override val type: String | ||
get() = throw UnsupportedOperationException() | ||
} | ||
assertThat(conditionMatcher.match(mockk(), mockk()), equalTo(true)) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -188,6 +188,9 @@ | |
} | ||
] | ||
}, | ||
"negate": { | ||
"type": "boolean" | ||
}, | ||
"pattern": { | ||
"type": "string" | ||
}, | ||
|