Skip to content

Commit

Permalink
Fix user-friendly Quarkus REST and RESTEasy combination err msg
Browse files Browse the repository at this point in the history
(cherry picked from commit 9e7462c)
  • Loading branch information
michalvavrik authored and gsmet committed May 23, 2024
1 parent a326683 commit dea8801
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.quarkus.resteasy.test;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.builder.Version;
import io.quarkus.deployment.Capability;
import io.quarkus.maven.dependency.Dependency;
import io.quarkus.test.QuarkusUnitTest;

class UserFriendlyQuarkusRESTCapabilityCombinationTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setForcedDependencies(
List.of(Dependency.of("io.quarkus", "quarkus-resteasy-reactive-deployment", Version.getVersion())))
.assertException(t -> {
assertTrue(t.getMessage().contains("only one provider of the following capabilities"), t.getMessage());
assertTrue(t.getMessage().contains("capability %s is provided by".formatted(Capability.REST)), t.getMessage());
});

@Test
public void test() {
fail();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import io.quarkus.arc.processor.BuildExtension;
import io.quarkus.arc.processor.ObserverInfo;
import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand Down Expand Up @@ -518,6 +519,7 @@ void transformSecurityAnnotations(BuildProducer<AnnotationsTransformerBuildItem>
}
}

@Consume(Capabilities.class) // make sure extension combinations are validated before default security check
@BuildStep
@Record(ExecutionTime.STATIC_INIT)
void gatherSecurityChecks(BuildProducer<SyntheticBeanBuildItem> syntheticBeans,
Expand All @@ -528,7 +530,7 @@ void gatherSecurityChecks(BuildProducer<SyntheticBeanBuildItem> syntheticBeans,
BuildProducer<RunTimeConfigBuilderBuildItem> configBuilderProducer,
List<AdditionalSecuredMethodsBuildItem> additionalSecuredMethods,
SecurityCheckRecorder recorder,
Optional<DefaultSecurityCheckBuildItem> defaultSecurityCheckBuildItem,
List<DefaultSecurityCheckBuildItem> defaultSecurityCheckBuildItem,
BuildProducer<ReflectiveClassBuildItem> reflectiveClassBuildItemBuildProducer,
List<AdditionalSecurityCheckBuildItem> additionalSecurityChecks, SecurityBuildTimeConfig config) {
classPredicate.produce(new ApplicationClassPredicateBuildItem(new SecurityCheckStorageAppPredicate()));
Expand Down Expand Up @@ -562,8 +564,14 @@ void gatherSecurityChecks(BuildProducer<SyntheticBeanBuildItem> syntheticBeans,
methodEntry.getValue());
}

if (defaultSecurityCheckBuildItem.isPresent()) {
var roles = defaultSecurityCheckBuildItem.get().getRolesAllowed();
if (!defaultSecurityCheckBuildItem.isEmpty()) {
if (defaultSecurityCheckBuildItem.size() > 1) {
int itemCount = defaultSecurityCheckBuildItem.size();
throw new IllegalStateException("Found %d DefaultSecurityCheckBuildItem items, ".formatted(itemCount)
+ "please make sure the item is produced exactly once");
}

var roles = defaultSecurityCheckBuildItem.get(0).getRolesAllowed();
if (roles == null) {
recorder.registerDefaultSecurityCheck(builder, recorder.denyAll());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
import java.util.List;
import java.util.Objects;

import io.quarkus.builder.item.SimpleBuildItem;

public final class DefaultSecurityCheckBuildItem extends SimpleBuildItem {
import io.quarkus.builder.item.MultiBuildItem;

/**
* Registers default SecurityCheck with the SecurityCheckStorage.
* Please make sure this build item is produced exactly once or validation will fail and exception will be thrown.
*/
public final class DefaultSecurityCheckBuildItem
// we make this Multi to run CapabilityAggregationStep#aggregateCapabilities first
// so that user-friendly error message is logged when Quarkus REST and RESTEasy are used together
extends MultiBuildItem {

public final List<String> rolesAllowed;

Expand Down

0 comments on commit dea8801

Please sign in to comment.