From c81788d5a51e23b3499ebf49024d44737ca20d18 Mon Sep 17 00:00:00 2001 From: Jeff Ortel Date: Wed, 11 Sep 2024 11:21:16 -0700 Subject: [PATCH] checkpoint Signed-off-by: Jeff Ortel --- 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("/"); + } + }, + }, };