diff --git a/packages/config-utils/src/proxy.ts b/packages/config-utils/src/proxy.ts index bc23e3be2..44d249ea0 100644 --- a/packages/config-utils/src/proxy.ts +++ b/packages/config-utils/src/proxy.ts @@ -7,7 +7,7 @@ import type { Configuration } from 'webpack-dev-server'; import { HttpsProxyAgent } from 'https-proxy-agent'; import chokidar from 'chokidar'; import cookieTransform from './cookieTransform'; -import { isInterceptAbleRequest, matchNavigationRequest } from './feo/check-outgoing-requests'; +import { isInterceptAbleRequest } from './feo/check-outgoing-requests'; import { hasFEOFeaturesEnabled, readFrontendCRD } from './feo/crd-check'; import fecLogger, { LogType } from './fec-logger'; import { modifyRequest } from './feo/modify-response'; @@ -136,6 +136,8 @@ const proxy = ({ frontendCRDPath = path.resolve(process.cwd(), 'deploy/frontend.yaml'), }: ProxyOptions) => { const frontendCrdRef: { current?: FrontendCRD } = { current: undefined }; + // Callback set by onListening to trigger browser reload from the CRD watcher + let triggerReload: (() => void) | undefined; let FEOFeaturesEnabled = false; try { frontendCrdRef.current = readFrontendCRD(frontendCRDPath); @@ -156,6 +158,10 @@ const proxy = ({ fecLogger(LogType.info, 'Frontend CRD has changed, reloading the file'); try { frontendCrdRef.current = readFrontendCRD(frontendCRDPath); + // Trigger browser reload AFTER the in-memory CRD is updated so that + // subsequent requests read the new navigation data (avoids race with + // an independent watchFiles trigger). + triggerReload?.(); } catch (error) { fecLogger(LogType.error, 'Error reloading frontend CRD file', error); } @@ -238,11 +244,25 @@ const proxy = ({ secure: false, changeOrigin: true, autoRewrite: true, + onProxyReq: (proxyReq, req) => { + // Strip conditional request headers for intercepted FEO responses to + // prevent 304 responses that would bypass CRD content modifications + if (FEOFeaturesEnabled && frontendCrdRef.current && isInterceptAbleRequest(req.url)) { + proxyReq.removeHeader('if-none-match'); + proxyReq.removeHeader('if-modified-since'); + } + }, onProxyRes: (proxyRes, req, res) => { // this should reading the aggregated bundles filed generated from chrome service // The functionality is disabled until the interceptor is ready if (FEOFeaturesEnabled && frontendCrdRef.current && isInterceptAbleRequest(req.url)) { + // Prevent browser from caching intercepted responses so that + // CRD changes are reflected on page reload without stale data + res.removeHeader('etag'); + res.removeHeader('last-modified'); + res.setHeader('cache-control', 'no-store, no-cache, must-revalidate'); + // stub the original write function const _write = res.write; let body = ''; @@ -334,6 +354,15 @@ const proxy = ({ const config: Configuration = { ...(proxy.length > 0 && { proxy }), onListening(server) { + // Provide reload trigger to the CRD chokidar watcher so it can + // refresh the browser AFTER the in-memory CRD has been updated. + if (FEOFeaturesEnabled) { + triggerReload = () => { + if (server.webSocketServer?.clients) { + server.sendMessage(server.webSocketServer.clients, 'content-changed'); + } + }; + } if (useProxy) { // Dev is just a prod but SSO does not allow dev.foo.redhat.com origin const host = useProxy ? `${majorEnv === 'dev' ? 'prod' : majorEnv}.foo.redhat.com` : 'localhost'; diff --git a/packages/config/src/bin/crd-interceptor-server.ts b/packages/config/src/bin/crd-interceptor-server.ts new file mode 100644 index 000000000..b10a73a8c --- /dev/null +++ b/packages/config/src/bin/crd-interceptor-server.ts @@ -0,0 +1,212 @@ +import http from 'http'; +import https from 'https'; +import { URL } from 'url'; +import chokidar from 'chokidar'; +import { HttpsProxyAgent } from 'https-proxy-agent'; +import { readFrontendCRD } from '@redhat-cloud-services/frontend-components-config-utilities/feo/crd-check'; +import { FrontendCRD } from '@redhat-cloud-services/frontend-components-config-utilities/feo/feo-types'; +import { modifyRequest } from '@redhat-cloud-services/frontend-components-config-utilities/feo/modify-response'; +import { LogType, fecLogger } from '@redhat-cloud-services/frontend-components-config-utilities'; + +const PROXY_URL = 'http://squid.corp.redhat.com:3128'; + +export const CRD_INTERCEPTOR_PORT = 9997; + +/** + * The 4 chrome-service API endpoints that need CRD interception. + * These are the same URLs matched by isInterceptAbleRequest() in config-utils. + */ +export const INTERCEPTABLE_PATHS = [ + '/api/chrome-service/v1/static/bundles-generated.json', + '/api/chrome-service/v1/static/search-index-generated.json', + '/api/chrome-service/v1/static/service-tiles-generated.json', + '/api/chrome-service/v1/static/fed-modules-generated.json', +]; + +interface CRDInterceptorOptions { + frontendCRDPath: string; + port?: number; + debug?: boolean; +} + +/** + * Starts a Node HTTP server that intercepts chrome-service API responses + * and applies local CRD modifications via modifyRequest(). + * + * This brings CRD interception support to `fec dev-proxy` mode, where the + * Caddy container normally proxies /api/* directly to stage without any + * local CRD awareness. The Caddy routes config directs the 4 interceptable + * API paths to this server instead, which fetches from upstream, applies + * CRD modifications, and returns the modified response. + * + * Reuses 100% of the existing interception code from config-utils: + * navigationInterceptor, searchInterceptor, moduleInterceptor, + * serviceTilesInterceptor (via modifyRequest). + */ +export async function startCRDInterceptorServer(options: CRDInterceptorOptions): Promise { + const { frontendCRDPath, port = CRD_INTERCEPTOR_PORT, debug = false } = options; + + const log = (logType: LogType, ...data: unknown[]) => { + if (logType === LogType.debug && !debug) return; + fecLogger(logType, ...data); + }; + + // Read and watch the CRD file, same pattern as proxy.ts + const frontendCrdRef: { current?: FrontendCRD } = { current: undefined }; + try { + frontendCrdRef.current = readFrontendCRD(frontendCRDPath); + } catch (e) { + log(LogType.warn, `CRD interceptor: Unable to read frontend CRD at ${frontendCRDPath}`); + } + + const watcher = chokidar + .watch(frontendCRDPath) + .on('error', (error) => log(LogType.error, 'CRD interceptor: Watcher error', error)) + .on('change', () => { + log(LogType.info, 'CRD interceptor: Frontend CRD changed, reloading'); + try { + frontendCrdRef.current = readFrontendCRD(frontendCRDPath); + } catch (error) { + log(LogType.error, 'CRD interceptor: Error reloading CRD', error); + } + }); + + const hccEnvUrl = process.env.HCC_ENV_URL!; + const isStage = process.env.HCC_ENV === 'stage'; + const agent = isStage ? new HttpsProxyAgent(PROXY_URL) : undefined; + + const server = http.createServer((req, res) => { + const url = req.url || '/'; + + // Reject absolute URLs and non-interceptable paths + if (url.startsWith('//') || url.startsWith('http')) { + res.writeHead(400, { 'content-type': 'application/json' }); + res.end(JSON.stringify({ error: 'Absolute URLs not allowed' })); + return; + } + const pathname = url.split('?')[0]; + if (!INTERCEPTABLE_PATHS.includes(pathname)) { + res.writeHead(404, { 'content-type': 'application/json' }); + res.end(JSON.stringify({ error: 'Path not interceptable' })); + return; + } + + if (!frontendCrdRef.current) { + log(LogType.warn, `CRD interceptor: No CRD loaded, proxying ${url} without modification`); + proxyPassthrough(url, req, res, hccEnvUrl, agent, log); + return; + } + + // Proxy to upstream (always HTTPS for HCC environments) and apply + // CRD modifications to the response + const upstreamUrl = new URL(url, hccEnvUrl); + + const headers: Record = { + ...req.headers, + host: upstreamUrl.hostname, + }; + // Prevent 304 responses that would bypass content modification + delete headers['if-none-match']; + delete headers['if-modified-since']; + + const proxyReq = https.request( + { + hostname: upstreamUrl.hostname, + port: upstreamUrl.port || 443, + path: upstreamUrl.pathname + upstreamUrl.search, + method: req.method, + headers, + // HttpsProxyAgent v5 extends agent-base Agent, not https.Agent, + // but is fully compatible at runtime + agent: agent as https.Agent | undefined, + timeout: 15000, + }, + (proxyRes) => { + let body = ''; + proxyRes.on('data', (chunk: Buffer) => { + body += chunk.toString(); + }); + proxyRes.on('end', () => { + try { + const modified = modifyRequest(body, url, frontendCrdRef.current!); + res.writeHead(200, { + 'content-type': 'application/json', + 'content-length': Buffer.byteLength(modified), + 'cache-control': 'no-store, no-cache, must-revalidate', + }); + res.end(modified); + log(LogType.debug, `CRD interceptor: Modified response for ${url}`); + } catch (error) { + log(LogType.error, `CRD interceptor: Error modifying response for ${url}`, error); + res.writeHead(proxyRes.statusCode || 500, { + 'content-type': proxyRes.headers['content-type'] || 'application/json', + }); + res.end(body); + } + }); + }, + ); + + proxyReq.on('timeout', () => proxyReq.destroy(new Error('Upstream request timed out'))); + proxyReq.on('error', (error) => { + log(LogType.error, `CRD interceptor: Upstream request failed for ${url}`, error); + res.writeHead(502, { 'content-type': 'application/json' }); + res.end(JSON.stringify({ error: 'Upstream request failed' })); + }); + + req.pipe(proxyReq); + }); + + const cleanup = () => { + watcher.close(); + }; + + process.on('SIGTERM', cleanup); + process.on('SIGINT', cleanup); + + return new Promise((resolve, reject) => { + server.listen(port, '127.0.0.1', () => { + log(LogType.info, `CRD interceptor server listening on 127.0.0.1:${port}`); + resolve(server); + }); + server.on('error', reject); + }); +} + +function proxyPassthrough( + url: string, + req: http.IncomingMessage, + res: http.ServerResponse, + hccEnvUrl: string, + agent: HttpsProxyAgent | undefined, + log: (logType: LogType, ...data: unknown[]) => void, +) { + const upstreamUrl = new URL(url, hccEnvUrl); + + const proxyReq = https.request( + { + hostname: upstreamUrl.hostname, + port: upstreamUrl.port || 443, + path: upstreamUrl.pathname + upstreamUrl.search, + method: req.method, + headers: { ...req.headers, host: upstreamUrl.hostname }, + agent: agent as https.Agent | undefined, + timeout: 15000, + }, + (proxyRes) => { + res.writeHead(proxyRes.statusCode || 200, proxyRes.headers); + proxyRes.pipe(res); + }, + ); + + proxyReq.on('timeout', () => proxyReq.destroy(new Error('Upstream request timed out'))); + proxyReq.on('error', (error) => { + log(LogType.error, `CRD interceptor: Passthrough request failed for ${url}`, error); + res.writeHead(502); + res.end('Upstream error'); + }); + + req.pipe(proxyReq); +} + +export default startCRDInterceptorServer; diff --git a/packages/config/src/bin/dev-proxy-script.ts b/packages/config/src/bin/dev-proxy-script.ts index 4afec2b10..706ca2aa0 100644 --- a/packages/config/src/bin/dev-proxy-script.ts +++ b/packages/config/src/bin/dev-proxy-script.ts @@ -5,7 +5,9 @@ import os from 'os'; import path from 'path'; import treeKill from 'tree-kill'; import { LogType, fecLogger as fecLoggerDefault } from '@redhat-cloud-services/frontend-components-config-utilities'; +import { hasFEOFeaturesEnabled, readFrontendCRD } from '@redhat-cloud-services/frontend-components-config-utilities/feo/crd-check'; import { getCdnPath, setEnv, validateFECConfig } from './common'; +import startCRDInterceptorServer, { CRD_INTERCEPTOR_PORT, INTERCEPTABLE_PATHS } from './crd-interceptor-server'; import serveChrome, { CONTAINER_NAME as CHROME_CONTAINTER_NAME, ContainerRuntime, checkContainerRuntime } from './serve-chrome'; const PROXY_URL = 'http://squid.corp.redhat.com:3128'; @@ -82,7 +84,14 @@ function pullImage(containerName: string, repo: string, tag: string) { execSync(`${execBin} pull ${repo}:${tag}`, debug ? { stdio: 'inherit' } : { stdio: [] }); } -function createRoutesConfig(fecConfig: any, cdnPath: string, port: string, SPAFallback: boolean, filename: string = 'routes.json'): string { +function createRoutesConfig( + fecConfig: any, + cdnPath: string, + port: string, + SPAFallback: boolean, + filename: string = 'routes.json', + interceptorPort?: number, +): string { let routes: Map = new Map(); let fecRoutes = fecConfig?.routes || undefined; @@ -130,6 +139,14 @@ function createRoutesConfig(fecConfig: any, cdnPath: string, port: string, SPAFa } }); + // Add CRD interceptor routes so Caddy directs chrome-service API + // requests to the local interceptor instead of straight to stage + if (interceptorPort) { + INTERCEPTABLE_PATHS.forEach((interceptPath) => { + routes.set(interceptPath, { url: `${DEFAULT_LOCAL_ROUTE}:${interceptorPort}` }); + }); + } + const tempDir = os.tmpdir(); const tempFilePath = path.join(tempDir, filename); const jsonContent = JSON.stringify(Object.fromEntries(routes)); @@ -217,6 +234,31 @@ async function devProxyScript( process.exit(1); } + // Check if FEO features are enabled and start CRD interceptor server + // so that dev-proxy mode can apply local CRD changes to chrome-service + // API responses (navigation, search, modules, service tiles) + let interceptorPort: number | undefined; + let interceptorServer: import('http').Server | undefined; + const frontendCRDPath = fecConfig.frontendCRDPath ?? path.resolve(cwd, 'deploy/frontend.yaml'); + try { + const crd = readFrontendCRD(frontendCRDPath); + if (hasFEOFeaturesEnabled(crd)) { + fecLogger(LogType.info, 'FEO features enabled, starting CRD interceptor server for dev-proxy mode'); + try { + interceptorServer = await startCRDInterceptorServer({ + frontendCRDPath, + port: CRD_INTERCEPTOR_PORT, + debug, + }); + interceptorPort = CRD_INTERCEPTOR_PORT; + } catch (interceptorError) { + fecLogger(LogType.error, 'FEO is enabled but the CRD interceptor server failed to start:', interceptorError); + } + } + } catch (e) { + fecLogger(LogType.debug, 'FEO features not available for CRD interception in dev-proxy mode'); + } + try { fs.statSync(webpackConfigPath); webpackConfig = require(webpackConfigPath); @@ -235,7 +277,7 @@ async function devProxyScript( const staticPort = argv.staticPort ?? '8003'; try { cdnPath = getCdnPath(fecConfig, webpackConfig, cwd); - routesConfigPath = createRoutesConfig(fecConfig, cdnPath, staticPort, SPAFallback); + routesConfigPath = createRoutesConfig(fecConfig, cdnPath, staticPort, SPAFallback, 'routes.json', interceptorPort); } catch (error) { fecLogger(LogType.error, 'Failed to generate the proxy routes config:', error); process.exit(1); @@ -264,6 +306,9 @@ async function devProxyScript( if (waitOnProcess?.pid) { treeKill(waitOnProcess.pid, 'SIGKILL'); } + if (interceptorServer) { + interceptorServer.close(); + } stopContainer(DEV_PROXY_CONTAINER_NAME); if (SPAFallback) { stopContainer(CHROME_CONTAINTER_NAME);