🎉 Init
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
####
|
||||
# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode
|
||||
#
|
||||
# Before building the container image run:
|
||||
#
|
||||
# ./mvnw package
|
||||
#
|
||||
# Then, build the image with:
|
||||
#
|
||||
# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/core-jvm .
|
||||
#
|
||||
# Then run the container using:
|
||||
#
|
||||
# docker run -i --rm -p 8080:8080 quarkus/core-jvm
|
||||
#
|
||||
# If you want to include the debug port into your docker image
|
||||
# you will have to expose the debug port (default 5005 being the default) like this : EXPOSE 8080 5005.
|
||||
# Additionally you will have to set -e JAVA_DEBUG=true and -e JAVA_DEBUG_PORT=*:5005
|
||||
# when running the container
|
||||
#
|
||||
# Then run the container using :
|
||||
#
|
||||
# docker run -i --rm -p 8080:8080 quarkus/core-jvm
|
||||
#
|
||||
# This image uses the `run-java.sh` script to run the application.
|
||||
# This scripts computes the command line to execute your Java application, and
|
||||
# includes memory/GC tuning.
|
||||
# You can configure the behavior using the following environment properties:
|
||||
# - JAVA_OPTS: JVM options passed to the `java` command (example: "-verbose:class") - Be aware that this will override
|
||||
# the default JVM options, use `JAVA_OPTS_APPEND` to append options
|
||||
# - JAVA_OPTS_APPEND: User specified Java options to be appended to generated options
|
||||
# in JAVA_OPTS (example: "-Dsome.property=foo")
|
||||
# - JAVA_MAX_MEM_RATIO: Is used when no `-Xmx` option is given in JAVA_OPTS. This is
|
||||
# used to calculate a default maximal heap memory based on a containers restriction.
|
||||
# If used in a container without any memory constraints for the container then this
|
||||
# option has no effect. If there is a memory constraint then `-Xmx` is set to a ratio
|
||||
# of the container available memory as set here. The default is `50` which means 50%
|
||||
# of the available memory is used as an upper boundary. You can skip this mechanism by
|
||||
# setting this value to `0` in which case no `-Xmx` option is added.
|
||||
# - JAVA_INITIAL_MEM_RATIO: Is used when no `-Xms` option is given in JAVA_OPTS. This
|
||||
# is used to calculate a default initial heap memory based on the maximum heap memory.
|
||||
# If used in a container without any memory constraints for the container then this
|
||||
# option has no effect. If there is a memory constraint then `-Xms` is set to a ratio
|
||||
# of the `-Xmx` memory as set here. The default is `25` which means 25% of the `-Xmx`
|
||||
# is used as the initial heap size. You can skip this mechanism by setting this value
|
||||
# to `0` in which case no `-Xms` option is added (example: "25")
|
||||
# - JAVA_MAX_INITIAL_MEM: Is used when no `-Xms` option is given in JAVA_OPTS.
|
||||
# This is used to calculate the maximum value of the initial heap memory. If used in
|
||||
# a container without any memory constraints for the container then this option has
|
||||
# no effect. If there is a memory constraint then `-Xms` is limited to the value set
|
||||
# here. The default is 4096MB which means the calculated value of `-Xms` never will
|
||||
# be greater than 4096MB. The value of this variable is expressed in MB (example: "4096")
|
||||
# - JAVA_DIAGNOSTICS: Set this to get some diagnostics information to standard output
|
||||
# when things are happening. This option, if set to true, will set
|
||||
# `-XX:+UnlockDiagnosticVMOptions`. Disabled by default (example: "true").
|
||||
# - JAVA_DEBUG: If set remote debugging will be switched on. Disabled by default (example:
|
||||
# true").
|
||||
# - JAVA_DEBUG_PORT: Port used for remote debugging. Defaults to 5005 (example: "8787").
|
||||
# - CONTAINER_CORE_LIMIT: A calculated core limit as described in
|
||||
# https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt. (example: "2")
|
||||
# - CONTAINER_MAX_MEMORY: Memory limit given to the container (example: "1024").
|
||||
# - GC_MIN_HEAP_FREE_RATIO: Minimum percentage of heap free after GC to avoid expansion.
|
||||
# (example: "20")
|
||||
# - GC_MAX_HEAP_FREE_RATIO: Maximum percentage of heap free after GC to avoid shrinking.
|
||||
# (example: "40")
|
||||
# - GC_TIME_RATIO: Specifies the ratio of the time spent outside the garbage collection.
|
||||
# (example: "4")
|
||||
# - GC_ADAPTIVE_SIZE_POLICY_WEIGHT: The weighting given to the current GC time versus
|
||||
# previous GC times. (example: "90")
|
||||
# - GC_METASPACE_SIZE: The initial metaspace size. (example: "20")
|
||||
# - GC_MAX_METASPACE_SIZE: The maximum metaspace size. (example: "100")
|
||||
# - GC_CONTAINER_OPTIONS: Specify Java GC to use. The value of this variable should
|
||||
# contain the necessary JRE command-line options to specify the required GC, which
|
||||
# will override the default of `-XX:+UseParallelGC` (example: -XX:+UseG1GC).
|
||||
# - HTTPS_PROXY: The location of the https proxy. (example: "myuser@127.0.0.1:8080")
|
||||
# - HTTP_PROXY: The location of the http proxy. (example: "myuser@127.0.0.1:8080")
|
||||
# - NO_PROXY: A comma separated lists of hosts, IP addresses or domains that can be
|
||||
# accessed directly. (example: "foo.example.com,bar.example.com")
|
||||
#
|
||||
# You can find more information about the UBI base runtime images and their configuration here:
|
||||
# https://rh-openjdk.github.io/redhat-openjdk-containers/
|
||||
###
|
||||
FROM registry.access.redhat.com/ubi9/openjdk-25-runtime:1.24
|
||||
|
||||
ENV LANGUAGE='en_US:en'
|
||||
|
||||
|
||||
# We make four distinct layers so if there are application changes the library layers can be re-used
|
||||
COPY --chown=185 target/quarkus-app/lib/ /deployments/lib/
|
||||
COPY --chown=185 target/quarkus-app/*.jar /deployments/
|
||||
COPY --chown=185 target/quarkus-app/app/ /deployments/app/
|
||||
COPY --chown=185 target/quarkus-app/quarkus/ /deployments/quarkus/
|
||||
|
||||
EXPOSE 8080
|
||||
USER 185
|
||||
ENV JAVA_OPTS_APPEND="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager"
|
||||
ENV JAVA_APP_JAR="/deployments/quarkus-run.jar"
|
||||
|
||||
ENTRYPOINT [ "/opt/jboss/container/java/run/run-java.sh" ]
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
####
|
||||
# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode
|
||||
#
|
||||
# Before building the container image run:
|
||||
#
|
||||
# ./mvnw package -Dquarkus.package.jar.type=legacy-jar
|
||||
#
|
||||
# Then, build the image with:
|
||||
#
|
||||
# docker build -f src/main/docker/Dockerfile.legacy-jar -t quarkus/core-legacy-jar .
|
||||
#
|
||||
# Then run the container using:
|
||||
#
|
||||
# docker run -i --rm -p 8080:8080 quarkus/core-legacy-jar
|
||||
#
|
||||
# If you want to include the debug port into your docker image
|
||||
# you will have to expose the debug port (default 5005 being the default) like this : EXPOSE 8080 5005.
|
||||
# Additionally you will have to set -e JAVA_DEBUG=true and -e JAVA_DEBUG_PORT=*:5005
|
||||
# when running the container
|
||||
#
|
||||
# Then run the container using :
|
||||
#
|
||||
# docker run -i --rm -p 8080:8080 quarkus/core-legacy-jar
|
||||
#
|
||||
# This image uses the `run-java.sh` script to run the application.
|
||||
# This scripts computes the command line to execute your Java application, and
|
||||
# includes memory/GC tuning.
|
||||
# You can configure the behavior using the following environment properties:
|
||||
# - JAVA_OPTS: JVM options passed to the `java` command (example: "-verbose:class") - Be aware that this will override
|
||||
# the default JVM options, use `JAVA_OPTS_APPEND` to append options
|
||||
# - JAVA_OPTS_APPEND: User specified Java options to be appended to generated options
|
||||
# in JAVA_OPTS (example: "-Dsome.property=foo")
|
||||
# - JAVA_MAX_MEM_RATIO: Is used when no `-Xmx` option is given in JAVA_OPTS. This is
|
||||
# used to calculate a default maximal heap memory based on a containers restriction.
|
||||
# If used in a container without any memory constraints for the container then this
|
||||
# option has no effect. If there is a memory constraint then `-Xmx` is set to a ratio
|
||||
# of the container available memory as set here. The default is `50` which means 50%
|
||||
# of the available memory is used as an upper boundary. You can skip this mechanism by
|
||||
# setting this value to `0` in which case no `-Xmx` option is added.
|
||||
# - JAVA_INITIAL_MEM_RATIO: Is used when no `-Xms` option is given in JAVA_OPTS. This
|
||||
# is used to calculate a default initial heap memory based on the maximum heap memory.
|
||||
# If used in a container without any memory constraints for the container then this
|
||||
# option has no effect. If there is a memory constraint then `-Xms` is set to a ratio
|
||||
# of the `-Xmx` memory as set here. The default is `25` which means 25% of the `-Xmx`
|
||||
# is used as the initial heap size. You can skip this mechanism by setting this value
|
||||
# to `0` in which case no `-Xms` option is added (example: "25")
|
||||
# - JAVA_MAX_INITIAL_MEM: Is used when no `-Xms` option is given in JAVA_OPTS.
|
||||
# This is used to calculate the maximum value of the initial heap memory. If used in
|
||||
# a container without any memory constraints for the container then this option has
|
||||
# no effect. If there is a memory constraint then `-Xms` is limited to the value set
|
||||
# here. The default is 4096MB which means the calculated value of `-Xms` never will
|
||||
# be greater than 4096MB. The value of this variable is expressed in MB (example: "4096")
|
||||
# - JAVA_DIAGNOSTICS: Set this to get some diagnostics information to standard output
|
||||
# when things are happening. This option, if set to true, will set
|
||||
# `-XX:+UnlockDiagnosticVMOptions`. Disabled by default (example: "true").
|
||||
# - JAVA_DEBUG: If set remote debugging will be switched on. Disabled by default (example:
|
||||
# true").
|
||||
# - JAVA_DEBUG_PORT: Port used for remote debugging. Defaults to 5005 (example: "8787").
|
||||
# - CONTAINER_CORE_LIMIT: A calculated core limit as described in
|
||||
# https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt. (example: "2")
|
||||
# - CONTAINER_MAX_MEMORY: Memory limit given to the container (example: "1024").
|
||||
# - GC_MIN_HEAP_FREE_RATIO: Minimum percentage of heap free after GC to avoid expansion.
|
||||
# (example: "20")
|
||||
# - GC_MAX_HEAP_FREE_RATIO: Maximum percentage of heap free after GC to avoid shrinking.
|
||||
# (example: "40")
|
||||
# - GC_TIME_RATIO: Specifies the ratio of the time spent outside the garbage collection.
|
||||
# (example: "4")
|
||||
# - GC_ADAPTIVE_SIZE_POLICY_WEIGHT: The weighting given to the current GC time versus
|
||||
# previous GC times. (example: "90")
|
||||
# - GC_METASPACE_SIZE: The initial metaspace size. (example: "20")
|
||||
# - GC_MAX_METASPACE_SIZE: The maximum metaspace size. (example: "100")
|
||||
# - GC_CONTAINER_OPTIONS: Specify Java GC to use. The value of this variable should
|
||||
# contain the necessary JRE command-line options to specify the required GC, which
|
||||
# will override the default of `-XX:+UseParallelGC` (example: -XX:+UseG1GC).
|
||||
# - HTTPS_PROXY: The location of the https proxy. (example: "myuser@127.0.0.1:8080")
|
||||
# - HTTP_PROXY: The location of the http proxy. (example: "myuser@127.0.0.1:8080")
|
||||
# - NO_PROXY: A comma separated lists of hosts, IP addresses or domains that can be
|
||||
# accessed directly. (example: "foo.example.com,bar.example.com")
|
||||
#
|
||||
# You can find more information about the UBI base runtime images and their configuration here:
|
||||
# https://rh-openjdk.github.io/redhat-openjdk-containers/
|
||||
###
|
||||
FROM registry.access.redhat.com/ubi9/openjdk-25-runtime:1.24
|
||||
|
||||
ENV LANGUAGE='en_US:en'
|
||||
|
||||
|
||||
COPY target/lib/* /deployments/lib/
|
||||
COPY target/*-runner.jar /deployments/quarkus-run.jar
|
||||
|
||||
EXPOSE 8080
|
||||
USER 185
|
||||
ENV JAVA_OPTS_APPEND="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager"
|
||||
ENV JAVA_APP_JAR="/deployments/quarkus-run.jar"
|
||||
|
||||
ENTRYPOINT [ "/opt/jboss/container/java/run/run-java.sh" ]
|
||||
@@ -0,0 +1,29 @@
|
||||
####
|
||||
# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode.
|
||||
#
|
||||
# Before building the container image run:
|
||||
#
|
||||
# ./mvnw package -Dnative
|
||||
#
|
||||
# Then, build the image with:
|
||||
#
|
||||
# docker build -f src/main/docker/Dockerfile.native -t quarkus/core .
|
||||
#
|
||||
# Then run the container using:
|
||||
#
|
||||
# docker run -i --rm -p 8080:8080 quarkus/core
|
||||
#
|
||||
# The ` registry.access.redhat.com/ubi9/ubi-minimal:9.7` base image is based on UBI 9.
|
||||
# To use UBI 8, switch to `quay.io/ubi8/ubi-minimal:8.10`.
|
||||
###
|
||||
FROM registry.access.redhat.com/ubi9/ubi-minimal:9.7
|
||||
WORKDIR /work/
|
||||
RUN chown 1001 /work \
|
||||
&& chmod "g+rwX" /work \
|
||||
&& chown 1001:root /work
|
||||
COPY --chown=1001:root --chmod=0755 target/*-runner /work/application
|
||||
|
||||
EXPOSE 8080
|
||||
USER 1001
|
||||
|
||||
ENTRYPOINT ["./application", "-Dquarkus.http.host=0.0.0.0"]
|
||||
@@ -0,0 +1,32 @@
|
||||
####
|
||||
# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode.
|
||||
# It uses a micro base image, tuned for Quarkus native executables.
|
||||
# It reduces the size of the resulting container image.
|
||||
# Check https://quarkus.io/guides/quarkus-runtime-base-image for further information about this image.
|
||||
#
|
||||
# Before building the container image run:
|
||||
#
|
||||
# ./mvnw package -Dnative
|
||||
#
|
||||
# Then, build the image with:
|
||||
#
|
||||
# docker build -f src/main/docker/Dockerfile.native-micro -t quarkus/core .
|
||||
#
|
||||
# Then run the container using:
|
||||
#
|
||||
# docker run -i --rm -p 8080:8080 quarkus/core
|
||||
#
|
||||
# The `quay.io/quarkus/ubi9-quarkus-micro-image:2.0` base image is based on UBI 9.
|
||||
# To use UBI 8, switch to `quay.io/quarkus/quarkus-micro-image:2.0`.
|
||||
###
|
||||
FROM quay.io/quarkus/ubi9-quarkus-micro-image:2.0
|
||||
WORKDIR /work/
|
||||
RUN chown 1001 /work \
|
||||
&& chmod "g+rwX" /work \
|
||||
&& chown 1001:root /work
|
||||
COPY --chown=1001:root --chmod=0755 target/*-runner /work/application
|
||||
|
||||
EXPOSE 8080
|
||||
USER 1001
|
||||
|
||||
ENTRYPOINT ["./application", "-Dquarkus.http.host=0.0.0.0"]
|
||||
@@ -0,0 +1,9 @@
|
||||
package dev.dinauer.oidcproxy;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
@ApplicationScoped
|
||||
public class ForwardService
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package dev.dinauer.oidcproxy;
|
||||
|
||||
import dev.dinauer.oidcproxy.callback.CallbackService;
|
||||
import dev.dinauer.oidcproxy.callback.SessionRepository;
|
||||
import dev.dinauer.oidcproxy.startup.ProxyRoute;
|
||||
import dev.dinauer.oidcproxy.startup.RouteService;
|
||||
import io.quarkus.vertx.web.Route;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.http.Cookie;
|
||||
import io.vertx.core.http.HttpMethod;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.Link;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.*;
|
||||
|
||||
@ApplicationScoped
|
||||
public class ProxyResource
|
||||
{
|
||||
@Inject
|
||||
RouteService routeService;
|
||||
|
||||
@Inject
|
||||
CallbackService callbackService;
|
||||
|
||||
@Inject
|
||||
SessionRepository sessionRepository;
|
||||
|
||||
@Inject
|
||||
ForwardService forwardService;
|
||||
|
||||
@Route(path = "/*", order = 1)
|
||||
public void hello(@Context RoutingContext context)
|
||||
{
|
||||
List<String> requestSegments = List.of(context.request().path().split("/"));
|
||||
|
||||
Optional<ProxyRoute> routeOptional = routeService.match(requestSegments);
|
||||
if (routeOptional.isPresent())
|
||||
{
|
||||
ProxyRoute route = routeOptional.get();
|
||||
try
|
||||
{
|
||||
HttpResponse<byte[]> response = forward(context.request().method(), extractSession(context.request().cookies()), route.target(), concat(dropRoute(route.segments(), requestSegments)));
|
||||
ResponseHandler.success(context, response);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ResponseHandler.error(context);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ResponseHandler.notFound(context);
|
||||
}
|
||||
}
|
||||
|
||||
public HttpResponse<byte[]> forward(HttpMethod method, String auth, String forwardRoot, String forwardPath) throws IOException, InterruptedException
|
||||
{
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder().method(method.name(), HttpRequest.BodyPublishers.noBody()).uri(URI.create(forwardRoot + "/" + forwardPath));
|
||||
if (auth != null)
|
||||
{
|
||||
builder.header("Authorization", String.format("Bearer %s", sessionRepository.get(auth)));
|
||||
}
|
||||
try(HttpClient client = HttpClient.newHttpClient())
|
||||
{
|
||||
return client.send(builder.build(), HttpResponse.BodyHandlers.ofByteArray());
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> dropRoute(List<String> route, List<String> request)
|
||||
{
|
||||
List<String> requestSegments = new LinkedList<>(request);
|
||||
for (int i = 0; i < route.size(); i++)
|
||||
{
|
||||
requestSegments.removeFirst();
|
||||
}
|
||||
return requestSegments;
|
||||
}
|
||||
|
||||
private String concat(List<String> segments)
|
||||
{
|
||||
return String.join("/", segments);
|
||||
}
|
||||
|
||||
private String extractSession(Set<Cookie> cookies)
|
||||
{
|
||||
for (Cookie cookie : cookies)
|
||||
{
|
||||
if ("session".equals(cookie.getName()))
|
||||
{
|
||||
return cookie.getValue();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package dev.dinauer.oidcproxy;
|
||||
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ResponseHandler
|
||||
{
|
||||
public static void success(RoutingContext context, HttpResponse<byte[]> response)
|
||||
{
|
||||
context.response().setStatusCode(response.statusCode());
|
||||
|
||||
for (Map.Entry<String, List<String>> headers : response.headers().map().entrySet())
|
||||
{
|
||||
try
|
||||
{
|
||||
context.response().putHeader(headers.getKey(), headers.getValue());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
}
|
||||
context.response().send(Buffer.buffer(response.body()));
|
||||
}
|
||||
|
||||
public static void error(RoutingContext context)
|
||||
{
|
||||
context.response().setStatusCode(502);
|
||||
context.response().send();
|
||||
}
|
||||
|
||||
public static void notFound(RoutingContext context)
|
||||
{
|
||||
context.response().setStatusCode(404);
|
||||
context.response().send();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package dev.dinauer.oidcproxy.callback;
|
||||
|
||||
import dev.dinauer.oidcproxy.callback.model.TokenResponse;
|
||||
import io.quarkus.vertx.web.Route;
|
||||
import io.vertx.core.http.Cookie;
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import io.vertx.core.http.HttpServerResponse;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
@ApplicationScoped
|
||||
public class CallbackService
|
||||
{
|
||||
@Inject
|
||||
OidcClient client;
|
||||
|
||||
@Inject
|
||||
SessionRepository sessionRepository;
|
||||
|
||||
@ConfigProperty(name = "oidc.proxy.client.redirect")
|
||||
String redirectURI;
|
||||
|
||||
@Route(path = "/callback", order = 0)
|
||||
public void get(@Context HttpServerResponse response, @Context HttpServerRequest request)
|
||||
{
|
||||
String code = request.params().get("code");
|
||||
TokenResponse token = client.exchangeAuthorizationCode(code);
|
||||
String sessionId = sessionRepository.add(token.accessToken());
|
||||
response.addCookie(Cookie.cookie("session", sessionId).setHttpOnly(true).setSecure(true).setMaxAge((int) (token.expiresAt() - ZonedDateTime.now().toEpochSecond())));
|
||||
response.setStatusCode(302);
|
||||
response.putHeader("Location", redirectURI);
|
||||
response.send();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package dev.dinauer.oidcproxy.callback;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import dev.dinauer.oidcproxy.callback.model.TokenResponse;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
@ApplicationScoped
|
||||
public class OidcClient
|
||||
{
|
||||
private static final String APPLICATION_URLENCODED = "application/x-www-form-urlencoded";
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
private static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient();
|
||||
|
||||
@ConfigProperty(name = "oidc.proxy.client.auth-url")
|
||||
String oidcAuthUrl;
|
||||
|
||||
@ConfigProperty(name = "oidc.proxy.client.id")
|
||||
String clientId;
|
||||
|
||||
@ConfigProperty(name = "oidc.proxy.client.secret")
|
||||
String clientSecret;
|
||||
|
||||
public TokenResponse exchangeAuthorizationCode(String code)
|
||||
{
|
||||
String body = formatBody(code);
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.method("POST", HttpRequest.BodyPublishers.ofString(body))
|
||||
.header("Content-Type", APPLICATION_URLENCODED)
|
||||
.header("Authorization", formatAuthHeader(clientId, clientSecret)).uri(URI.create(oidcAuthUrl)).build();
|
||||
try
|
||||
{
|
||||
HttpResponse<String> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
if (response.statusCode() == 200)
|
||||
{
|
||||
return OBJECT_MAPPER.readValue(response.body(), TokenResponse.class);
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
catch (IOException | InterruptedException e)
|
||||
{
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private String formatBody(String code)
|
||||
{
|
||||
return String.format("grant_type=authorization_code&code=%s", URLEncoder.encode(code, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private String formatAuthHeader(String clientId, String clientSecret)
|
||||
{
|
||||
return String.format("Basic %s", Base64.getEncoder().encodeToString(String.format("%s:%s", clientId, clientSecret).getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package dev.dinauer.oidcproxy.callback;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@ApplicationScoped
|
||||
public class SessionRepository
|
||||
{
|
||||
private final Map<String, String> tokens = new HashMap<>();
|
||||
|
||||
public String add(String token)
|
||||
{
|
||||
String sessionId = UUID.randomUUID().toString();
|
||||
tokens.put(sessionId, token);
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
public String get(String sessionId)
|
||||
{
|
||||
String token = tokens.get(sessionId);
|
||||
if (token != null)
|
||||
{
|
||||
return token;
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package dev.dinauer.oidcproxy.callback.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public record TokenResponse(@JsonProperty("access_token") String accessToken, @JsonProperty("expires_at") Long expiresAt)
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package dev.dinauer.oidcproxy.startup;
|
||||
|
||||
import org.apache.commons.lang3.Strings;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record ProxyRoute(String path, String target, String strategy)
|
||||
{
|
||||
public String path()
|
||||
{
|
||||
return Strings.CI.removeEnd(target, "/");
|
||||
}
|
||||
|
||||
public String target()
|
||||
{
|
||||
return Strings.CI.removeEnd(target, "/");
|
||||
}
|
||||
|
||||
public List<String> segments()
|
||||
{
|
||||
return List.of(path.split("/"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package dev.dinauer.oidcproxy.startup;
|
||||
|
||||
import dev.dinauer.oidcproxy.startup.model.ConfigRoute;
|
||||
import dev.dinauer.oidcproxy.startup.model.ConfigRules;
|
||||
import io.quarkus.runtime.Startup;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.Strings;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
import tools.jackson.dataformat.yaml.YAMLFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@ApplicationScoped
|
||||
public class RouteService
|
||||
{
|
||||
private List<ProxyRoute> routes;
|
||||
|
||||
@Startup
|
||||
void start()
|
||||
{
|
||||
ConfigRules rules = new ObjectMapper(new YAMLFactory()).readValue(new File("/home/andreas/Documents/dev/oidc-proxy/src/main/resources/routes.yaml"), ConfigRules.class);
|
||||
|
||||
List<ProxyRoute> result = new LinkedList<>();
|
||||
for (ConfigRoute route : rules.routes())
|
||||
{
|
||||
if (StringUtils.isBlank(rules.root()))
|
||||
{
|
||||
result.add(new ProxyRoute(route.path(), route.target(), route.strategy()));
|
||||
}
|
||||
else
|
||||
{
|
||||
result.add(new ProxyRoute(StringUtils.join(rules.root(), route.path()), route.target(), route.strategy()));
|
||||
}
|
||||
}
|
||||
this.routes = result;
|
||||
}
|
||||
|
||||
public Optional<ProxyRoute> match(List<String> request)
|
||||
{
|
||||
Pair<Integer, ProxyRoute> best = null;
|
||||
for (ProxyRoute proxyRoute : this.routes)
|
||||
{
|
||||
Optional<Integer> match = SegmentMatcher.match(proxyRoute.segments(), request);
|
||||
if (match.isPresent())
|
||||
{
|
||||
int matchLength = match.get();
|
||||
if (best == null || best.getLeft() < matchLength)
|
||||
{
|
||||
best = Pair.ofNonNull(matchLength, proxyRoute);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (best != null)
|
||||
{
|
||||
return Optional.of(best.getRight());
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package dev.dinauer.oidcproxy.startup;
|
||||
|
||||
import org.apache.commons.lang3.Strings;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class SegmentMatcher
|
||||
{
|
||||
public static Optional<Integer> match(List<String> route, List<String> request)
|
||||
{
|
||||
if (request.size() < route.size())
|
||||
{
|
||||
return Optional.empty();
|
||||
}
|
||||
for (int i = 0; i < route.size(); i++)
|
||||
{
|
||||
String candidateSegment = request.get(i);
|
||||
String prefixSegment = route.get(i);
|
||||
if (!Strings.CI.equals(candidateSegment, prefixSegment))
|
||||
{
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
return Optional.of(route.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package dev.dinauer.oidcproxy.startup.model;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record ConfigRoute(String path, String target, String strategy)
|
||||
{
|
||||
public List<String> segments()
|
||||
{
|
||||
if (StringUtils.isBlank(path))
|
||||
{
|
||||
return List.of();
|
||||
}
|
||||
return List.of(path.split("/"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package dev.dinauer.oidcproxy.startup.model;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record ConfigRules(String root, List<ConfigRoute> routes)
|
||||
{
|
||||
public List<String> segments()
|
||||
{
|
||||
if (StringUtils.isBlank(root))
|
||||
{
|
||||
return List.of();
|
||||
}
|
||||
return List.of(root.split("/"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
oidc.proxy.client.auth-url=http://localhost:8089/api/iam-backend/realms/maven/protocol/openid-connect/token
|
||||
oidc.proxy.client.id=backend
|
||||
oidc.proxy.client.secret=backend
|
||||
oidc.proxy.client.redirect=http://localhost:3000
|
||||
@@ -0,0 +1,8 @@
|
||||
root: /api
|
||||
routes:
|
||||
- path: /oidc
|
||||
target: http://localhost:8089/api/iam-backend
|
||||
strategy: NONE
|
||||
- path: /
|
||||
target: http://localhost:8081
|
||||
strategy: NONE
|
||||
@@ -0,0 +1,44 @@
|
||||
package dev.dinauer.oidcproxy;
|
||||
|
||||
import dev.dinauer.oidcproxy.startup.SegmentMatcher;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class SegmentMatcherTests
|
||||
{
|
||||
@Test
|
||||
void testExactMatch_successful()
|
||||
{
|
||||
List<String> prefix = List.of("api", "products");
|
||||
List<String> candidate = List.of("api", "products");
|
||||
|
||||
Optional<Integer> result = SegmentMatcher.match(prefix, candidate);
|
||||
Assertions.assertTrue(result.isPresent());
|
||||
Assertions.assertEquals(2, result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExactMatch_failure_01()
|
||||
{
|
||||
List<String> prefix = List.of("api", "products");
|
||||
List<String> candidate = List.of("api", "products", "387483");
|
||||
|
||||
Optional<Integer> result = SegmentMatcher.match(prefix, candidate);
|
||||
Assertions.assertTrue(result.isPresent());
|
||||
Assertions.assertEquals(2, result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExactMatch_failure_02()
|
||||
{
|
||||
List<String> prefix = List.of("api", "products", "482028");
|
||||
List<String> candidate = List.of("api", "products", "387483");
|
||||
|
||||
Optional<Integer> result = SegmentMatcher.match(prefix, candidate);
|
||||
Assertions.assertTrue(result.isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user