forked from helidon-io/helidon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4.x: Policy statement method matching added (helidon-io#9501)
Policy statement method matching added Signed-off-by: David Kral <[email protected]>
- Loading branch information
Showing
6 changed files
with
138 additions
and
14 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
53 changes: 53 additions & 0 deletions
53
...rc/main/java/io/helidon/tests/integration/security/abac/policy/PolicyStatementMethod.java
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 (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* 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 io.helidon.tests.integration.security.abac.policy; | ||
|
||
import io.helidon.security.abac.policy.PolicyValidator; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
|
||
/** | ||
* Policy statement matched by the method test resource. | ||
*/ | ||
@Path("method") | ||
public class PolicyStatementMethod { | ||
|
||
/** | ||
* Endpoint which should be matched via method. | ||
* | ||
* @return passed value | ||
*/ | ||
@GET | ||
@PolicyValidator.PolicyStatement("${env.time.year < 2017}") | ||
public String get() { | ||
return "passed"; | ||
} | ||
|
||
/** | ||
* Endpoint which should not be matched because of the different method. | ||
* | ||
* @return should not pass value | ||
*/ | ||
@POST | ||
@PolicyValidator.PolicyStatement("${env.time.year < 2017}") | ||
public String post() { | ||
return "should not pass"; | ||
} | ||
|
||
} |
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
...icy/src/test/java/io/helidon/tests/integration/security/abac/policy/MethodPolicyTest.java
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 (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* 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 io.helidon.tests.integration.security.abac.policy; | ||
|
||
import io.helidon.http.Status; | ||
import io.helidon.microprofile.testing.junit5.HelidonTest; | ||
|
||
import jakarta.ws.rs.client.Entity; | ||
import jakarta.ws.rs.client.WebTarget; | ||
import jakarta.ws.rs.core.Response; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
@HelidonTest | ||
public class MethodPolicyTest { | ||
|
||
@Test | ||
void testPolicyMatchedByMethod(WebTarget target) { | ||
try (Response response = target.path("/method") | ||
.request() | ||
.get()) { | ||
assertThat(response.getStatus(), is(Status.OK_200.code())); | ||
assertThat(response.readEntity(String.class), is("passed")); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
void testPolicyNotMatchedByMethod(WebTarget target) { | ||
try (Response response = target.path("/method") | ||
.request() | ||
.post(Entity.text("Test value"))) { | ||
assertThat(response.getStatus(), is(Status.FORBIDDEN_403.code())); | ||
} | ||
} | ||
|
||
} |