Skip to content

Commit

Permalink
feat: add @TenantExclude annotation for tenant resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Jan 26, 2024
1 parent 46e7956 commit 407a96d
Show file tree
Hide file tree
Showing 44 changed files with 1,009 additions and 181 deletions.
5 changes: 5 additions & 0 deletions extensions/log/rs/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-jwt-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions extensions/rest-context/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ tkit.rs.context.correlation-id.header-param-name=X-Correlation-ID
tkit.rs.context.business-context.enabled=false
# Business context header parameter
tkit.rs.context.business-context.header-param-name=business-context
# Add token to the context
tkit.rs.context.token-context=true
# Add token to the application context
tkit.rs.context.add-token-to-context=true
# Principal token is mandatory
tkit.rs.context.token-mandatory=false
# Principal is mandatory
Expand Down
70 changes: 70 additions & 0 deletions extensions/rest-context/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.tkit.quarkus.lib</groupId>
<artifactId>tkit-quarkus-rest-context-parent</artifactId>
<version>999-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>tkit-quarkus-rest-context-deployment</artifactId>
<name>tkit-quarkus-rest-context-deployment</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-jwt-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.tkit.quarkus.lib</groupId>
<artifactId>tkit-quarkus-rest-context</artifactId>
<version>${project.version}</version>
</dependency>

<!-- TEST -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.tkit.quarkus.rs.context.deployment;

import org.tkit.quarkus.rs.context.runtime.TenantAnnotationData;

import io.quarkus.builder.item.SimpleBuildItem;

public final class AnnotationDataBuildItem extends SimpleBuildItem {

TenantAnnotationData data;

public AnnotationDataBuildItem(TenantAnnotationData data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.tkit.quarkus.rs.context.deployment;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

/**
* Rest context build configuration.
*/
@ConfigRoot(prefix = "tkit", name = "rs.context.build", phase = ConfigPhase.BUILD_TIME)
public class RestContextBuildConfig {

/**
* Auto-discovery configuration.
*/
@ConfigItem(name = "tenant")
public TenantBuildTimeConfig tenant;

/**
* Build tenant annotation config.
*/
@ConfigGroup
public static class TenantBuildTimeConfig {
/**
* Enable tenant annotation discovery
*/
@ConfigItem(name = "enabled", defaultValue = "true")
public boolean enabled;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.tkit.quarkus.rs.context.deployment;

import java.util.stream.Collectors;

import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.MethodInfo;
import org.tkit.quarkus.rs.context.runtime.RestContextTenantRecorder;
import org.tkit.quarkus.rs.context.runtime.TenantAnnotationData;
import org.tkit.quarkus.rs.context.tenant.TenantExclude;

import io.quarkus.arc.deployment.BeanDiscoveryFinishedBuildItem;
import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem;
import io.quarkus.arc.processor.BeanInfo;
import io.quarkus.deployment.annotations.*;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.RuntimeConfigSetupCompleteBuildItem;

public class RestContextProcessor {

private static final DotName TENANT_EXCLUDE = DotName.createSimple(TenantExclude.class.getName());
static final String FEATURE_NAME = "tkit-rest-context";

@BuildStep
void build(BuildProducer<FeatureBuildItem> feature) {
feature.produce(new FeatureBuildItem(FEATURE_NAME));
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
@Consume(RuntimeConfigSetupCompleteBuildItem.class)
public void restContextInit(RestContextTenantRecorder recorder, AnnotationDataBuildItem dataBuildItem) {
recorder.init(dataBuildItem.data);
}

@BuildStep
void services(BeanDiscoveryFinishedBuildItem beanDiscoveryFinishedBuildItem,
BeanRegistrationPhaseBuildItem beanRegistrationPhase,
RestContextBuildConfig buildConfig,
BuildProducer<AnnotationDataBuildItem> producer) {

if (!buildConfig.tenant.enabled) {
return;
}

TenantAnnotationData tenant = new TenantAnnotationData();

for (BeanInfo bean : beanRegistrationPhase.getContext().beans().classBeans()) {
ClassInfo ci = bean.getImplClazz();

// check @TenantExclude for class
if (ci.hasDeclaredAnnotation(TENANT_EXCLUDE)) {
tenant.addExcludeClass(ci.name().toString());
continue;
}

// check @TenantExclude for class methods
for (MethodInfo methodInfo : ci.methods()) {
if (methodInfo.hasAnnotation(TENANT_EXCLUDE)) {

var params = "";
if (!methodInfo.parameterTypes().isEmpty()) {
params = methodInfo.parameterTypes()
.stream()
.map(org.jboss.jandex.Type::toString)
.collect(Collectors.joining());
}
tenant.addExcludeMethod(ci.name().toString(), methodInfo.name(), params);
}
}
}

producer.produce(new AnnotationDataBuildItem(tenant));
}
}
28 changes: 7 additions & 21 deletions extensions/rest-context/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,12 @@
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>tkit-quarkus-rest-context</artifactId>
<packaging>jar</packaging>
<artifactId>tkit-quarkus-rest-context-parent</artifactId>
<packaging>pom</packaging>

<modules>
<module>deployment</module>
<module>runtime</module>
</modules>

<dependencies>
<dependency>
<groupId>org.tkit.quarkus.lib</groupId>
<artifactId>tkit-quarkus-context</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-jwt</artifactId>
</dependency>
</dependencies>
</project>
73 changes: 73 additions & 0 deletions extensions/rest-context/runtime/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.tkit.quarkus.lib</groupId>
<artifactId>tkit-quarkus-rest-context-parent</artifactId>
<version>999-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>tkit-quarkus-rest-context</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.tkit.quarkus.lib</groupId>
<artifactId>tkit-quarkus-context</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-jwt</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>extension-descriptor</goal>
</goals>
<phase>compile</phase>
<configuration>
<deployment>${project.groupId}:${project.artifactId}-deployment:${project.version}
</deployment>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>io.smallrye</groupId>
<artifactId>jandex-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Loading

0 comments on commit 407a96d

Please sign in to comment.