Skip to content

Commit e84c5ee

Browse files
author
Dennis Labordus
authored
Merge pull request #59 from com-pas/basic_api_security
Added role based API access
2 parents 20c64f4 + 86f27e0 commit e84c5ee

File tree

7 files changed

+47
-0
lines changed

7 files changed

+47
-0
lines changed

app/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ SPDX-License-Identifier: Apache-2.0
8181
<groupId>io.quarkus</groupId>
8282
<artifactId>quarkus-hibernate-validator</artifactId>
8383
</dependency>
84+
<dependency>
85+
<groupId>io.quarkus</groupId>
86+
<artifactId>quarkus-oidc</artifactId>
87+
</dependency>
8488

8589
<!-- Test Dependencies -->
8690
<dependency>
@@ -98,6 +102,11 @@ SPDX-License-Identifier: Apache-2.0
98102
<artifactId>rest-assured</artifactId>
99103
<scope>test</scope>
100104
</dependency>
105+
<dependency>
106+
<groupId>io.quarkus</groupId>
107+
<artifactId>quarkus-test-security-oidc</artifactId>
108+
<scope>test</scope>
109+
</dependency>
101110
<dependency>
102111
<groupId>io.quarkus</groupId>
103112
<artifactId>quarkus-jacoco</artifactId>

app/src/main/java/org/lfenergy/compas/scl/data/rest/Constants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ private Constants() {
1010
public static final String TYPE_PATH_PARAM = "type";
1111
public static final String ID_PATH_PARAM = "id";
1212
public static final String VERSION_PATH_PARAM = "version";
13+
14+
public static final String READ_ROLE = "Read";
15+
public static final String CREATE_ROLE = "Create";
16+
public static final String UPDATE_ROLE = "Update";
17+
public static final String DELETE_ROLE = "Delete";
1318
}

app/src/main/java/org/lfenergy/compas/scl/data/rest/v1/CompasCommonResource.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@
77
import org.lfenergy.compas.scl.data.rest.v1.model.Type;
88
import org.lfenergy.compas.scl.data.rest.v1.model.TypeListResponse;
99

10+
import javax.annotation.security.RolesAllowed;
1011
import javax.ws.rs.GET;
1112
import javax.ws.rs.Path;
1213
import javax.ws.rs.Produces;
1314
import javax.ws.rs.core.MediaType;
1415
import java.util.Arrays;
1516
import java.util.stream.Collectors;
1617

18+
import static org.lfenergy.compas.scl.data.rest.Constants.READ_ROLE;
19+
1720
@Path("/common/v1/")
1821
public class CompasCommonResource {
1922
@GET
2023
@Path("/type/list")
24+
@RolesAllowed(READ_ROLE)
2125
@Produces(MediaType.APPLICATION_XML)
2226
public TypeListResponse list() {
2327
var response = new TypeListResponse();

app/src/main/java/org/lfenergy/compas/scl/data/rest/v1/CompasSclDataResource.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.lfenergy.compas.scl.data.rest.v1.model.*;
1010
import org.lfenergy.compas.scl.data.service.CompasSclDataService;
1111

12+
import javax.annotation.security.RolesAllowed;
1213
import javax.inject.Inject;
1314
import javax.validation.Valid;
1415
import javax.ws.rs.*;
@@ -29,6 +30,7 @@ public CompasSclDataResource(CompasSclDataService compasSclDataService) {
2930
}
3031

3132
@POST
33+
@RolesAllowed(CREATE_ROLE)
3234
@Consumes(MediaType.APPLICATION_XML)
3335
@Produces(MediaType.APPLICATION_XML)
3436
public CreateResponse create(@PathParam(TYPE_PATH_PARAM) SclType type,
@@ -40,6 +42,7 @@ public CreateResponse create(@PathParam(TYPE_PATH_PARAM) SclType type,
4042

4143
@GET
4244
@Path("/list")
45+
@RolesAllowed(READ_ROLE)
4346
@Produces(MediaType.APPLICATION_XML)
4447
public ListResponse list(@PathParam(TYPE_PATH_PARAM) SclType type) {
4548
var response = new ListResponse();
@@ -49,6 +52,7 @@ public ListResponse list(@PathParam(TYPE_PATH_PARAM) SclType type) {
4952

5053
@GET
5154
@Path("/{" + ID_PATH_PARAM + "}/versions")
55+
@RolesAllowed(READ_ROLE)
5256
@Produces(MediaType.APPLICATION_XML)
5357
public VersionsResponse listVersionsByUUID(@PathParam(TYPE_PATH_PARAM) SclType type,
5458
@PathParam(ID_PATH_PARAM) UUID id) {
@@ -59,6 +63,7 @@ public VersionsResponse listVersionsByUUID(@PathParam(TYPE_PATH_PARAM) SclType t
5963

6064
@GET
6165
@Path("/{" + ID_PATH_PARAM + "}")
66+
@RolesAllowed(READ_ROLE)
6267
@Produces(MediaType.APPLICATION_XML)
6368
public GetResponse findByUUID(@PathParam(TYPE_PATH_PARAM) SclType type,
6469
@PathParam(ID_PATH_PARAM) UUID id) {
@@ -69,6 +74,7 @@ public GetResponse findByUUID(@PathParam(TYPE_PATH_PARAM) SclType type,
6974

7075
@GET
7176
@Path("/{" + ID_PATH_PARAM + "}/{" + VERSION_PATH_PARAM + "}")
77+
@RolesAllowed(READ_ROLE)
7278
@Produces(MediaType.APPLICATION_XML)
7379
public GetResponse findByUUIDAndVersion(@PathParam(TYPE_PATH_PARAM) SclType type,
7480
@PathParam(ID_PATH_PARAM) UUID id,
@@ -80,6 +86,7 @@ public GetResponse findByUUIDAndVersion(@PathParam(TYPE_PATH_PARAM) SclType type
8086

8187
@GET
8288
@Path("/{" + ID_PATH_PARAM + "}/scl")
89+
@RolesAllowed(READ_ROLE)
8390
@Produces(MediaType.APPLICATION_XML)
8491
public String findRawSCLByUUID(@PathParam(TYPE_PATH_PARAM) SclType type,
8592
@PathParam(ID_PATH_PARAM) UUID id) {
@@ -89,6 +96,7 @@ public String findRawSCLByUUID(@PathParam(TYPE_PATH_PARAM) SclType type,
8996

9097
@GET
9198
@Path("/{" + ID_PATH_PARAM + "}/{" + VERSION_PATH_PARAM + "}/scl")
99+
@RolesAllowed(READ_ROLE)
92100
@Produces(MediaType.APPLICATION_XML)
93101
public String findRawSCLByUUIDAndVersion(@PathParam(TYPE_PATH_PARAM) SclType type,
94102
@PathParam(ID_PATH_PARAM) UUID id,
@@ -99,6 +107,7 @@ public String findRawSCLByUUIDAndVersion(@PathParam(TYPE_PATH_PARAM) SclType typ
99107

100108
@PUT
101109
@Path("/{" + ID_PATH_PARAM + "}")
110+
@RolesAllowed(UPDATE_ROLE)
102111
@Consumes(MediaType.APPLICATION_XML)
103112
@Produces(MediaType.APPLICATION_XML)
104113
public void update(@PathParam(TYPE_PATH_PARAM) SclType type,
@@ -109,6 +118,7 @@ public void update(@PathParam(TYPE_PATH_PARAM) SclType type,
109118

110119
@DELETE
111120
@Path("/{" + ID_PATH_PARAM + "}")
121+
@RolesAllowed(DELETE_ROLE)
112122
@Produces(MediaType.APPLICATION_XML)
113123
public void deleteAll(@PathParam(TYPE_PATH_PARAM) SclType type,
114124
@PathParam(ID_PATH_PARAM) UUID id) {
@@ -117,6 +127,7 @@ public void deleteAll(@PathParam(TYPE_PATH_PARAM) SclType type,
117127

118128
@DELETE
119129
@Path("/{" + ID_PATH_PARAM + "}/{" + VERSION_PATH_PARAM + "}")
130+
@RolesAllowed(DELETE_ROLE)
120131
@Produces(MediaType.APPLICATION_XML)
121132
public void deleteVersion(@PathParam(TYPE_PATH_PARAM) SclType type,
122133
@PathParam(ID_PATH_PARAM) UUID id,

app/src/main/resources/application.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ basex.password = ${BASEX_PASSWORD:admin}
2121

2222
%dev.quarkus.log.level = DEBUG
2323
%dev.quarkus.log.category."org.lfenergy.compas.scl.data".level = DEBUG
24+
25+
# Open ID Connect
26+
quarkus.oidc.auth-server-url = http://${KEYCLOAK_HOST:localhost}:${KEYCLOAK_PORT:8080}/auth/realms/${KEYCLOAK_REALM:compas}
27+
quarkus.oidc.client-id = scl-data-service

app/src/test/java/org/lfenergy/compas/scl/data/rest/v1/CompasCommonResourceTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55

66
import io.quarkus.test.common.http.TestHTTPEndpoint;
77
import io.quarkus.test.junit.QuarkusTest;
8+
import io.quarkus.test.security.TestSecurity;
89
import org.junit.jupiter.api.Test;
910
import org.lfenergy.compas.scl.data.model.SclType;
1011

1112
import static io.restassured.RestAssured.given;
1213
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.lfenergy.compas.scl.data.rest.Constants.READ_ROLE;
1315

1416
@QuarkusTest
1517
@TestHTTPEndpoint(CompasCommonResource.class)
1618
class CompasCommonResourceTest {
1719
@Test
20+
@TestSecurity(user = "test-user", roles = {READ_ROLE})
1821
void list_WhenCalled_ThenItemResponseRetrieved() {
1922
var response = given()
2023
.when().get("/type/list")

app/src/test/java/org/lfenergy/compas/scl/data/rest/v1/CompasSclDataResourceTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.quarkus.test.common.http.TestHTTPEndpoint;
77
import io.quarkus.test.junit.QuarkusTest;
88
import io.quarkus.test.junit.mockito.InjectMock;
9+
import io.quarkus.test.security.TestSecurity;
910
import io.restassured.http.ContentType;
1011
import org.junit.jupiter.api.Test;
1112
import org.lfenergy.compas.core.commons.ElementConverter;
@@ -39,6 +40,7 @@ class CompasSclDataResourceTest {
3940
private final ElementConverter converter = new ElementConverter();
4041

4142
@Test
43+
@TestSecurity(user = "test-user", roles = {READ_ROLE})
4244
void list_WhenCalled_ThenItemResponseRetrieved() {
4345
var type = SclType.SCD;
4446
var uuid = UUID.randomUUID();
@@ -64,6 +66,7 @@ void list_WhenCalled_ThenItemResponseRetrieved() {
6466
}
6567

6668
@Test
69+
@TestSecurity(user = "test-user", roles = {READ_ROLE})
6770
void listVersionsByUUID_WhenCalled_ThenItemResponseRetrieved() {
6871
var type = SclType.SCD;
6972
var uuid = UUID.randomUUID();
@@ -90,6 +93,7 @@ void listVersionsByUUID_WhenCalled_ThenItemResponseRetrieved() {
9093
}
9194

9295
@Test
96+
@TestSecurity(user = "test-user", roles = {READ_ROLE})
9397
void findByUUID_WhenCalled_ThenSCLResponseRetrieved() {
9498
var type = SclType.SCD;
9599
var uuid = UUID.randomUUID();
@@ -113,6 +117,7 @@ void findByUUID_WhenCalled_ThenSCLResponseRetrieved() {
113117
}
114118

115119
@Test
120+
@TestSecurity(user = "test-user", roles = {READ_ROLE})
116121
void findByUUIDAndVersion_WhenCalled_ThenSCLResponseRetrieved() {
117122
var type = SclType.SCD;
118123
var uuid = UUID.randomUUID();
@@ -138,6 +143,7 @@ void findByUUIDAndVersion_WhenCalled_ThenSCLResponseRetrieved() {
138143
}
139144

140145
@Test
146+
@TestSecurity(user = "test-user", roles = {READ_ROLE})
141147
void findRawSCLByUUID_WhenCalledOnlySCL_ThenSCLRetrieved() {
142148
var type = SclType.SCD;
143149
var uuid = UUID.randomUUID();
@@ -161,6 +167,7 @@ void findRawSCLByUUID_WhenCalledOnlySCL_ThenSCLRetrieved() {
161167
}
162168

163169
@Test
170+
@TestSecurity(user = "test-user", roles = {READ_ROLE})
164171
void findRawSCLByUUIDAndVersion_WhenCalled_ThenSCLRetrieved() {
165172
var type = SclType.SCD;
166173
var uuid = UUID.randomUUID();
@@ -186,6 +193,7 @@ void findRawSCLByUUIDAndVersion_WhenCalled_ThenSCLRetrieved() {
186193
}
187194

188195
@Test
196+
@TestSecurity(user = "test-user", roles = {CREATE_ROLE})
189197
void create_WhenCalled_ThenServiceCalledAndUUIDRetrieved() {
190198
var uuid = UUID.randomUUID();
191199
var type = SclType.SCD;
@@ -214,6 +222,7 @@ void create_WhenCalled_ThenServiceCalledAndUUIDRetrieved() {
214222
}
215223

216224
@Test
225+
@TestSecurity(user = "test-user", roles = {UPDATE_ROLE})
217226
void update_WhenCalled_ThenServiceCalledAndNewUUIDRetrieved() {
218227
var uuid = UUID.randomUUID();
219228
var type = SclType.SCD;
@@ -240,6 +249,7 @@ void update_WhenCalled_ThenServiceCalledAndNewUUIDRetrieved() {
240249
}
241250

242251
@Test
252+
@TestSecurity(user = "test-user", roles = {DELETE_ROLE})
243253
void deleteAll_WhenCalled_ThenServiceCalled() {
244254
var uuid = UUID.randomUUID();
245255
var type = SclType.SCD;
@@ -257,6 +267,7 @@ void deleteAll_WhenCalled_ThenServiceCalled() {
257267
}
258268

259269
@Test
270+
@TestSecurity(user = "test-user", roles = {DELETE_ROLE})
260271
void deleteVersion_WhenCalled_ThenServiceCalled() {
261272
var uuid = UUID.randomUUID();
262273
var type = SclType.SCD;

0 commit comments

Comments
 (0)