Skip to content

Commit

Permalink
Use ConfigMapping to map duplicated properties and avoid unknown conf…
Browse files Browse the repository at this point in the history
…ig warning
  • Loading branch information
radcortez committed Jul 27, 2022
1 parent 0075ba0 commit 2549ab6
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private List<ConfigDocItem> recursivelyFindConfigItems(Element element, String r
}

for (Element enclosedElement : element.getEnclosedElements()) {
if (!enclosedElement.getKind().isField() && (!enclosedElement.getKind().equals(ElementKind.METHOD) || !isMapping)) {
if (!enclosedElement.getKind().isField() && (!isMapping || !enclosedElement.getKind().equals(ElementKind.METHOD))) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void addConfigRoot(final PackageElement pkg, TypeElement clazz) {

for (AnnotationMirror mirror : clazz.getAnnotationMirrors()) {
if (mirror.getAnnotationType().toString().equals(Constants.ANNOTATION_CONFIG_MAPPING)) {
isMapping = true;
for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : mirror.getElementValues()
.entrySet()) {
if ("prefix()".equals(entry.getKey().toString())) {
Expand Down Expand Up @@ -145,17 +146,16 @@ public Set<ConfigDocGeneratedOutput> scanExtensionsConfigurationItems(Properties
* Loads the list of configuration items per configuration root
*
*/
private Properties loadAllExtensionConfigItemsParConfigRoot() throws IOException {
private Properties loadAllExtensionConfigItemsParConfigRoot() {
return allExtensionGeneratedDocs.asProperties();
}

/**
* Update extensions config roots. We need to gather the complete list of configuration roots of an extension
* when generating the documentation.
*
* @throws IOException
*/
private void updateConfigurationRootsList(Map.Entry<ConfigRootInfo, List<ConfigDocItem>> entry) throws IOException {
private void updateConfigurationRootsList(Map.Entry<ConfigRootInfo, List<ConfigDocItem>> entry) {
String extensionFileName = entry.getKey().getFileName();
String clazz = entry.getKey().getClazz().getQualifiedName().toString();
configurationRootsParExtensionFileName.put(extensionFileName, clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final b
}
if (runTime || bootstrap) {
builder.addDefaultSources();
// Validator only for runtime. We cannot use the current validator for build time (chicken / egg problem)
builder.addDiscoveredValidator();
builder.withDefaultValue(UUID_KEY, UUID.randomUUID().toString());
builder.withSources(new DotEnvConfigSourceProvider());
builder.withSources(
Expand Down Expand Up @@ -179,7 +181,6 @@ public OptionalInt getPriority() {
builder.addDefaultInterceptors();
builder.addDiscoveredInterceptors();
builder.addDiscoveredConverters();
builder.addDiscoveredValidator();
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
#io.smallrye.config.validator.BeanValidationConfigValidatorImpl
io.quarkus.hibernate.validator.runtime.HibernateBeanValidationConfigValidator
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class JaxRsSecurityConfig {
*
* The role of '**' means any authenticated user, which is equivalent to the {@link io.quarkus.security.Authenticated}
* annotation.
*
*/
@ConfigItem
public Optional<List<String>> defaultRolesAllowed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import javax.ws.rs.Priorities;
import javax.ws.rs.ext.RuntimeDelegate;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.CompositeIndex;
Expand Down Expand Up @@ -45,6 +43,7 @@
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
import io.quarkus.resteasy.reactive.common.runtime.JaxRsSecurityConfig;
import io.quarkus.resteasy.reactive.common.runtime.ResteasyReactiveConfig;
import io.quarkus.resteasy.reactive.spi.AbstractInterceptorBuildItem;
import io.quarkus.resteasy.reactive.spi.AdditionalResourceClassBuildItem;
Expand All @@ -66,22 +65,15 @@ public class ResteasyReactiveCommonProcessor {
private static final int LEGACY_WRITER_PRIORITY = Priorities.USER / 2; // writers are compared by increased priority

@BuildStep
void setUpDenyAllJaxRs(CombinedIndexBuildItem index,
void setUpDenyAllJaxRs(
CombinedIndexBuildItem index,
ResteasyReactiveConfig rrConfig,
JaxRsSecurityConfig securityConfig,
Optional<ResourceScanningResultBuildItem> resteasyDeployment,
BuildProducer<AdditionalSecuredClassesBuildItem> additionalSecuredClasses) {

Config config = ConfigProvider.getConfig();

// we do this in order to avoid having 'io.quarkus.resteasy.reactive.common.runtime.JaxRsSecurityConfig' conflict with 'io.quarkus.resteasy.runtime.JaxRsSecurityConfig'
Optional<Boolean> denyUnannotatedEndpointsConfig = config
.getOptionalValue("quarkus.security.jaxrs.deny-unannotated-endpoints", Boolean.class);
Optional<List<String>> defaultRolesAllowedConfig = config
.getOptionalValues("quarkus.security.jaxrs.default-roles-allowed", String.class);

if (denyUnannotatedEndpointsConfig.orElse(false) && resteasyDeployment.isPresent()) {
final List<ClassInfo> classes = new ArrayList<>();

if (securityConfig.denyJaxRs() && resteasyDeployment.isPresent()) {
List<ClassInfo> classes = new ArrayList<>();
Set<DotName> resourceClasses = resteasyDeployment.get().getResult().getScannedResourcePaths().keySet();
for (DotName className : resourceClasses) {
ClassInfo classInfo = index.getIndex().getClassByName(className);
Expand All @@ -91,9 +83,8 @@ void setUpDenyAllJaxRs(CombinedIndexBuildItem index,
}

additionalSecuredClasses.produce(new AdditionalSecuredClassesBuildItem(classes));
} else if (defaultRolesAllowedConfig.isPresent() && resteasyDeployment.isPresent()) {

final List<ClassInfo> classes = new ArrayList<>();
} else if (securityConfig.defaultRolesAllowed().isPresent() && resteasyDeployment.isPresent()) {
List<ClassInfo> classes = new ArrayList<>();
Set<DotName> resourceClasses = resteasyDeployment.get().getResult().getScannedResourcePaths().keySet();
for (DotName className : resourceClasses) {
ClassInfo classInfo = index.getIndex().getClassByName(className);
Expand All @@ -102,7 +93,7 @@ void setUpDenyAllJaxRs(CombinedIndexBuildItem index,
}
}
additionalSecuredClasses
.produce(new AdditionalSecuredClassesBuildItem(classes, defaultRolesAllowedConfig));
.produce(new AdditionalSecuredClassesBuildItem(classes, securityConfig.defaultRolesAllowed()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.resteasy.reactive.common.runtime;

import java.util.List;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithName;

@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
@ConfigMapping(prefix = "quarkus.security.jaxrs")
public interface JaxRsSecurityConfig {
/**
* if set to true, access to all JAX-RS resources will be denied by default
*/
@WithName("deny-unannotated-endpoints")
@WithDefault("false")
boolean denyJaxRs();

/**
* If no security annotations are affecting a method then they will default to requiring these roles,
* (equivalent to adding an @RolesAllowed annotation with the roles to every endpoint class).
*
* The role of '**' means any authenticated user, which is equivalent to the {@link io.quarkus.security.Authenticated}
* annotation.
*/
Optional<List<String>> defaultRolesAllowed();
}

0 comments on commit 2549ab6

Please sign in to comment.