Skip to content

Commit

Permalink
NOTIF-55 Migrate to resteasy-reactive and rest-client-reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenneg committed Apr 30, 2021
1 parent 792b19b commit b93a750
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 36 deletions.
16 changes: 4 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.version>1.13.2.Final</quarkus.platform.version>
<quarkus.platform.version>1.13.3.Final</quarkus.platform.version>
<quarkus-plugin.version>${quarkus.platform.version}</quarkus-plugin.version>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
Expand Down Expand Up @@ -54,24 +54,20 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client</artifactId>
<artifactId>quarkus-resteasy-reactive-qute</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
<artifactId>quarkus-rest-client-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-mutiny</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-mutiny</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-context-propagation</artifactId>
Expand Down Expand Up @@ -112,10 +108,6 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-cache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-qute</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-reactive</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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;

/**
* 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;

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,7 +107,7 @@ public EndpointTypeProcessor endpointTypeToProcessor(EndpointType endpointType)
// TODO [BG Phase 2] Delete this method
public Multi<Endpoint> getEndpoints(String tenant, String bundleName, String applicationName, String eventTypeName) {
return resources.getTargetEndpoints(tenant, bundleName, applicationName, eventTypeName)
.onItem().transformToMultiAndConcatenate((Function<Endpoint, Publisher<Endpoint>>) endpoint -> {
.onItem().transformToMultiAndConcatenate((Function<Endpoint, Multi<Endpoint>>) 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<WebApplicationException> {
@Override
public class JaxRsExceptionMapper {
@ServerExceptionMapper
public Response toResponse(WebApplicationException exception) {

if (exception instanceof BadRequestException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> 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
Expand Down Expand Up @@ -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<Integer> deleteAllFrom(Class<?> classname) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +29,9 @@ void beforeEach() {
@MockServerConfig
MockServerClientConfig mockServerConfig;

@CacheName("rbac-cache")
Cache cache;

@Test
void testEndpointRoles() {
String tenant = "empty";
Expand All @@ -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
Expand All @@ -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);

Expand All @@ -58,11 +68,17 @@ void testEndpointRoles() {
.then()
.statusCode(403);

clearRbacCache();

// Test bogus x-rh-identity header that fails Base64 decoding
given()
.header(new Header("x-rh-identity", "00000"))
.when().get("/endpoints")
.then()
.statusCode(401);
}

private void clearRbacCache() {
((CaffeineCache) cache).invalidateAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit b93a750

Please sign in to comment.