From b93a75046807d87da3392c4f08e96bc446078c13 Mon Sep 17 00:00:00 2001 From: Gwenneg Lepage Date: Mon, 26 Apr 2021 17:38:39 +0200 Subject: [PATCH] NOTIF-55 Migrate to resteasy-reactive and rest-client-reactive --- pom.xml | 16 ++++------------ .../auth/rbac/RbacClientResponseFilter.java | 13 ++++++------- .../auth/rbac/RbacRestClientRequestFilter.java | 14 +++++++------- .../notifications/events/EndpointProcessor.java | 3 +-- .../routers/JaxRsExceptionMapper.java | 9 ++++----- .../redhat/cloud/notifications/db/DbCleaner.java | 7 ++++--- .../routers/AuthenticationTest.java | 16 ++++++++++++++++ .../routers/NotificationServiceTest.java | 5 +++++ 8 files changed, 47 insertions(+), 36 deletions(-) diff --git a/pom.xml b/pom.xml index 1a92f6ee8f..76bfe1eb4f 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ 11 UTF-8 UTF-8 - 1.13.2.Final + 1.13.3.Final ${quarkus.platform.version} quarkus-bom io.quarkus @@ -54,24 +54,20 @@ io.quarkus - quarkus-resteasy + quarkus-resteasy-reactive-jackson io.quarkus - quarkus-rest-client + quarkus-resteasy-reactive-qute io.quarkus - quarkus-resteasy-jackson + quarkus-rest-client-reactive io.quarkus quarkus-mutiny - - io.quarkus - quarkus-resteasy-mutiny - io.quarkus quarkus-smallrye-context-propagation @@ -112,10 +108,6 @@ io.quarkus quarkus-cache - - io.quarkus - quarkus-resteasy-qute - io.quarkus quarkus-hibernate-reactive diff --git a/src/main/java/com/redhat/cloud/notifications/auth/rbac/RbacClientResponseFilter.java b/src/main/java/com/redhat/cloud/notifications/auth/rbac/RbacClientResponseFilter.java index 841c741d23..9294050325 100644 --- a/src/main/java/com/redhat/cloud/notifications/auth/rbac/RbacClientResponseFilter.java +++ b/src/main/java/com/redhat/cloud/notifications/auth/rbac/RbacClientResponseFilter.java @@ -1,22 +1,21 @@ package com.redhat.cloud.notifications.auth.rbac; -import javax.ws.rs.client.ClientRequestContext; -import javax.ws.rs.client.ClientResponseContext; -import javax.ws.rs.client.ClientResponseFilter; +import org.jboss.resteasy.reactive.server.ServerResponseFilter; + +import javax.ws.rs.container.ContainerResponseContext; import javax.ws.rs.core.Response; -import java.io.IOException; import java.util.logging.Logger; /** * Filter to look at the response (code) from the Rbac server. * Log a warning if we have trouble reaching the server. */ -public class RbacClientResponseFilter implements ClientResponseFilter { +public class RbacClientResponseFilter { private final Logger log = Logger.getLogger(this.getClass().getName()); - @Override - public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { + @ServerResponseFilter + public void filter(ContainerResponseContext responseContext) { Response.StatusType statusInfo = responseContext.getStatusInfo(); int status = statusInfo.getStatusCode(); if (status != 200) { diff --git a/src/main/java/com/redhat/cloud/notifications/auth/rbac/RbacRestClientRequestFilter.java b/src/main/java/com/redhat/cloud/notifications/auth/rbac/RbacRestClientRequestFilter.java index fd826b6658..b068fa2700 100644 --- a/src/main/java/com/redhat/cloud/notifications/auth/rbac/RbacRestClientRequestFilter.java +++ b/src/main/java/com/redhat/cloud/notifications/auth/rbac/RbacRestClientRequestFilter.java @@ -1,8 +1,8 @@ package com.redhat.cloud.notifications.auth.rbac; -import javax.ws.rs.client.ClientRequestContext; -import javax.ws.rs.client.ClientRequestFilter; -import java.io.IOException; +import org.jboss.resteasy.reactive.server.ServerRequestFilter; + +import javax.ws.rs.container.ContainerRequestContext; import java.net.URI; import java.util.Base64; @@ -10,7 +10,7 @@ * Filter to optionally add data on outgoing requests to the RBAC service. * This is meant for local development and not production. */ -public class RbacRestClientRequestFilter implements ClientRequestFilter { +public class RbacRestClientRequestFilter { private String authInfo; @@ -21,11 +21,11 @@ public RbacRestClientRequestFilter() { } } - @Override - public void filter(ClientRequestContext requestContext) throws IOException { + @ServerRequestFilter + public void filter(ContainerRequestContext requestContext) { if (authInfo != null) { - URI uri = requestContext.getUri(); + URI uri = requestContext.getUriInfo().getRequestUri(); if (uri.toString().startsWith("https://ci.cloud.redhat.com")) { requestContext.getHeaders().putSingle("Authorization", "Basic " + authInfo); } diff --git a/src/main/java/com/redhat/cloud/notifications/events/EndpointProcessor.java b/src/main/java/com/redhat/cloud/notifications/events/EndpointProcessor.java index 2b38011356..c9ac236437 100644 --- a/src/main/java/com/redhat/cloud/notifications/events/EndpointProcessor.java +++ b/src/main/java/com/redhat/cloud/notifications/events/EndpointProcessor.java @@ -16,7 +16,6 @@ import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.Uni; import org.hibernate.reactive.mutiny.Mutiny; -import org.reactivestreams.Publisher; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; @@ -108,7 +107,7 @@ public EndpointTypeProcessor endpointTypeToProcessor(EndpointType endpointType) // TODO [BG Phase 2] Delete this method public Multi getEndpoints(String tenant, String bundleName, String applicationName, String eventTypeName) { return resources.getTargetEndpoints(tenant, bundleName, applicationName, eventTypeName) - .onItem().transformToMultiAndConcatenate((Function>) endpoint -> { + .onItem().transformToMultiAndConcatenate((Function>) endpoint -> { // If the tenant has a default endpoint for the eventType, then add the target endpoints here if (endpoint.getType() == EndpointType.DEFAULT) { return defaultProcessor.getDefaultEndpoints(endpoint); diff --git a/src/main/java/com/redhat/cloud/notifications/routers/JaxRsExceptionMapper.java b/src/main/java/com/redhat/cloud/notifications/routers/JaxRsExceptionMapper.java index 6368d0b7a8..809d997f4a 100644 --- a/src/main/java/com/redhat/cloud/notifications/routers/JaxRsExceptionMapper.java +++ b/src/main/java/com/redhat/cloud/notifications/routers/JaxRsExceptionMapper.java @@ -1,17 +1,16 @@ package com.redhat.cloud.notifications.routers; +import org.jboss.resteasy.reactive.server.ServerExceptionMapper; + import javax.ws.rs.BadRequestException; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; /** * Map thrown Exceptions to Responses with appropriate status codes */ -@Provider -public class JaxRsExceptionMapper implements ExceptionMapper { - @Override +public class JaxRsExceptionMapper { + @ServerExceptionMapper public Response toResponse(WebApplicationException exception) { if (exception instanceof BadRequestException) { diff --git a/src/test/java/com/redhat/cloud/notifications/db/DbCleaner.java b/src/test/java/com/redhat/cloud/notifications/db/DbCleaner.java index 383980832b..e1c7884c53 100644 --- a/src/test/java/com/redhat/cloud/notifications/db/DbCleaner.java +++ b/src/test/java/com/redhat/cloud/notifications/db/DbCleaner.java @@ -50,8 +50,8 @@ public class DbCleaner { * is a temporary workaround to make our tests more reliable and easy to maintain. */ @DELETE - public void clean() { - session.withTransaction(transaction -> deleteAllFrom(EmailAggregation.class) + public Uni clean() { + return session.withTransaction(transaction -> deleteAllFrom(EmailAggregation.class) .chain(() -> deleteAllFrom(EmailSubscription.class)) .chain(() -> deleteAllFrom(NotificationHistory.class)) .chain(() -> deleteAllFrom(EndpointDefault.class)) // TODO [BG Phase 2] Delete this line @@ -82,7 +82,8 @@ public void clean() { eventType.setDescription(DEFAULT_EVENT_TYPE_DESCRIPTION); return appResources.addEventTypeToApplication(app.getId(), eventType); }) - ).await().indefinitely(); + .replaceWith(Uni.createFrom().voidItem()) + ); } private Uni deleteAllFrom(Class classname) { diff --git a/src/test/java/com/redhat/cloud/notifications/routers/AuthenticationTest.java b/src/test/java/com/redhat/cloud/notifications/routers/AuthenticationTest.java index 72f50d2826..e8de6158ce 100644 --- a/src/test/java/com/redhat/cloud/notifications/routers/AuthenticationTest.java +++ b/src/test/java/com/redhat/cloud/notifications/routers/AuthenticationTest.java @@ -5,6 +5,9 @@ import com.redhat.cloud.notifications.TestConstants; import com.redhat.cloud.notifications.TestHelpers; import com.redhat.cloud.notifications.TestLifecycleManager; +import io.quarkus.cache.Cache; +import io.quarkus.cache.CacheName; +import io.quarkus.cache.runtime.caffeine.CaffeineCache; import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; import io.restassured.RestAssured; @@ -26,6 +29,9 @@ void beforeEach() { @MockServerConfig MockServerClientConfig mockServerConfig; + @CacheName("rbac-cache") + Cache cache; + @Test void testEndpointRoles() { String tenant = "empty"; @@ -40,6 +46,8 @@ void testEndpointRoles() { .then() .statusCode(401); + clearRbacCache(); + // Fetch endpoint without any Rbac details - errors cause 401 given() // Set header to x-rh-identity @@ -48,6 +56,8 @@ void testEndpointRoles() { .then() .statusCode(401); + clearRbacCache(); + // Fetch endpoint with no access - Rbac succeed returns 403 mockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerClientConfig.RbacAccess.NO_ACCESS); @@ -58,6 +68,8 @@ void testEndpointRoles() { .then() .statusCode(403); + clearRbacCache(); + // Test bogus x-rh-identity header that fails Base64 decoding given() .header(new Header("x-rh-identity", "00000")) @@ -65,4 +77,8 @@ void testEndpointRoles() { .then() .statusCode(401); } + + private void clearRbacCache() { + ((CaffeineCache) cache).invalidateAll(); + } } diff --git a/src/test/java/com/redhat/cloud/notifications/routers/NotificationServiceTest.java b/src/test/java/com/redhat/cloud/notifications/routers/NotificationServiceTest.java index 78779e4b53..198035a7a8 100644 --- a/src/test/java/com/redhat/cloud/notifications/routers/NotificationServiceTest.java +++ b/src/test/java/com/redhat/cloud/notifications/routers/NotificationServiceTest.java @@ -9,6 +9,7 @@ import com.redhat.cloud.notifications.db.DbIsolatedTest; import com.redhat.cloud.notifications.db.ResourceHelpers; import com.redhat.cloud.notifications.models.Application; +import com.redhat.cloud.notifications.models.BehaviorGroup; import com.redhat.cloud.notifications.models.Endpoint; import com.redhat.cloud.notifications.models.EndpointType; import com.redhat.cloud.notifications.models.EventType; @@ -573,6 +574,8 @@ void testInsufficientPrivileges() { given() .header(readAccessIdentityHeader) .contentType(ContentType.JSON) + // TODO Remove the body when https://github.com/quarkusio/quarkus/issues/16897 is fixed + .body(Json.encode(new BehaviorGroup())) .when() .post("/notifications/behaviorGroups") .then() @@ -582,6 +585,8 @@ void testInsufficientPrivileges() { .header(readAccessIdentityHeader) .contentType(ContentType.JSON) .pathParam("id", UUID.randomUUID()) + // TODO Remove the body when https://github.com/quarkusio/quarkus/issues/16897 is fixed + .body(Json.encode(new BehaviorGroup())) .when() .put("/notifications/behaviorGroups/{id}") .then()