Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/config-utils/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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');
}
},
Comment on lines +247 to +254

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟠 Major | 🏗️ Heavy lift

Add unit tests for the new onProxyReq header-stripping and triggerReload reload signaling.

Neither the conditional-request header removal (Lines 247-254) nor the triggerReload → server.sendMessage(..., 'content-changed') wiring (Lines 357-365) appear to have accompanying tests. As per coding guidelines, "All functions in @redhat-cloud-services/frontend-components-config-utilities must have comprehensive unit tests."

Also applies to: 357-365

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/config-utils/src/proxy.ts` around lines 247 - 254, Add comprehensive
unit tests for the proxy’s onProxyReq handler and triggerReload flow. Verify
conditional headers are removed only when FEOFeaturesEnabled,
frontendCrdRef.current, and isInterceptAbleRequest(req.url) all permit
interception, while preserving them otherwise; also verify triggerReload invokes
server.sendMessage with the content-changed event. Use the existing proxy test
setup and mocks.

Source: Coding guidelines

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');
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// stub the original write function
const _write = res.write;
let body = '';
Expand Down Expand Up @@ -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';
Expand Down
192 changes: 192 additions & 0 deletions packages/config/src/bin/crd-interceptor-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
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<http.Server> {
const { frontendCRDPath, port = CRD_INTERCEPTOR_PORT, debug = false } = options;

const log = (logType: LogType, ...data: any[]) => {
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('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);
}
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

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 || '/';

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<string, string | string[] | undefined> = {
...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,
},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
(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('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' }));
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

req.pipe(proxyReq);
});

const cleanup = () => {
watcher.close();
};

process.on('SIGTERM', cleanup);
process.on('SIGINT', cleanup);

return new Promise((resolve, reject) => {
server.listen(port, () => {
log(LogType.info, `CRD interceptor server listening on port ${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: any[]) => 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,
},
(proxyRes) => {
res.writeHead(proxyRes.statusCode || 200, proxyRes.headers);
proxyRes.pipe(res);
},
);

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;
38 changes: 36 additions & 2 deletions packages/config/src/bin/dev-proxy-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -82,7 +84,7 @@ 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<string, RouteConfig> = new Map();

let fecRoutes = fecConfig?.routes || undefined;
Expand Down Expand Up @@ -130,6 +132,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));
Expand Down Expand Up @@ -217,6 +227,27 @@ 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');
interceptorServer = await startCRDInterceptorServer({
frontendCRDPath,
port: CRD_INTERCEPTOR_PORT,
debug,
});
interceptorPort = CRD_INTERCEPTOR_PORT;
}
} catch (e) {
fecLogger(LogType.debug, 'FEO features not available for CRD interception in dev-proxy mode');
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

try {
fs.statSync(webpackConfigPath);
webpackConfig = require(webpackConfigPath);
Expand All @@ -235,7 +266,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);
Expand Down Expand Up @@ -264,6 +295,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);
Expand Down
Loading