From 71bcdaa8d6b75276ea0f0111e828331bff58da18 Mon Sep 17 00:00:00 2001 From: Jeff Ortel Date: Thu, 12 Sep 2024 09:25:56 -0500 Subject: [PATCH] :sparkles: Add /kai reverse-proxy route. (#2089) Related: https://github.com/konveyor/tackle2-hub/pull/750 Signed-off-by: Jeff Ortel Signed-off-by: Shevijacobson --- common/src/proxies.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/common/src/proxies.ts b/common/src/proxies.ts index 94246c544b..a70039342c 100644 --- a/common/src/proxies.ts +++ b/common/src/proxies.ts @@ -60,4 +60,35 @@ export const proxyMap: Record = { } }, }, + + "/kai": { + target: KONVEYOR_ENV.TACKLE_HUB_URL || "http://localhost:9002", + logLevel: process.env.DEBUG ? "debug" : "info", + + changeOrigin: true, + pathRewrite: { + "^/kai": "/services/kai", + }, + + onProxyReq: (proxyReq, req, _res) => { + // Add the Bearer token to the request if it is not already present, AND if + // the token is part of the request as a cookie + if (req.cookies?.keycloak_cookie && !req.headers["authorization"]) { + proxyReq.setHeader( + "Authorization", + `Bearer ${req.cookies.keycloak_cookie}` + ); + } + }, + onProxyRes: (proxyRes, req, res) => { + const includesJsonHeaders = + req.headers.accept?.includes("application/json"); + if ( + (!includesJsonHeaders && proxyRes.statusCode === 401) || + (!includesJsonHeaders && proxyRes.statusMessage === "Unauthorized") + ) { + res.redirect("/"); + } + }, + }, };