Skip to content

Commit

Permalink
fix(module-federation): remote proxies should use https when host is …
Browse files Browse the repository at this point in the history
…configured with ssl #27360
  • Loading branch information
Coly010 committed Aug 12, 2024
1 parent 8bba5b5 commit ecc3d03
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type {
SchemaWithBrowserTarget,
SchemaWithBuildTarget,
} from '../schema';
import { join } from 'path';
import { workspaceRoot } from '@nx/devkit';

export function normalizeOptions(schema: Schema): NormalizedSchema {
let buildTarget = (schema as SchemaWithBuildTarget).buildTarget;
Expand All @@ -24,5 +26,7 @@ export function normalizeOptions(schema: Schema): NormalizedSchema {
liveReload: schema.liveReload ?? true,
open: schema.open ?? false,
ssl: schema.ssl ?? false,
sslCert: schema.sslCert ? join(workspaceRoot, schema.sslCert) : undefined,
sslKey: schema.sslKey ? join(workspaceRoot, schema.sslKey) : undefined,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,16 @@ export async function* moduleFederationDevServerExecutor(
options
);

startRemoteProxies(staticRemotesConfig, mappedLocationsOfStaticRemotes);
startRemoteProxies(
staticRemotesConfig,
mappedLocationsOfStaticRemotes,
options.ssl
? {
pathToCert: options.sslCert,
pathToKey: options.sslKey,
}
: undefined
);

const removeBaseUrlEmission = (iter: AsyncIterable<unknown>) =>
mapAsyncIterable(iter, (v) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,16 @@ export default async function* moduleFederationDevServer(
options
);

startRemoteProxies(staticRemotesConfig, mappedLocationsOfStaticRemotes);
startRemoteProxies(
staticRemotesConfig,
mappedLocationsOfStaticRemotes,
options.ssl
? {
pathToCert: join(workspaceRoot, options.sslCert),
pathToKey: join(workspaceRoot, options.sslKey),
}
: undefined
);

return yield* combineAsyncIterables(
currIter,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
import type { Express } from 'express';
import { logger } from '@nx/devkit';
import { logger, ProjectGraph } from '@nx/devkit';
import { StaticRemotesConfig } from './parse-static-remotes-config';
import { existsSync, readFileSync } from 'fs';

export function startRemoteProxies(
staticRemotesConfig: StaticRemotesConfig,
mappedLocationsOfRemotes: Record<string, string>
mappedLocationsOfRemotes: Record<string, string>,
sslOptions?: { pathToCert: string; pathToKey: string }
) {
const { createProxyMiddleware } = require('http-proxy-middleware');
const express = require('express');
let sslCert: Buffer;
let sslKey: Buffer;
if (sslOptions && sslOptions.pathToCert && sslOptions.pathToKey) {
if (existsSync(sslOptions.pathToCert) && existsSync(sslOptions.pathToKey)) {
sslCert = readFileSync(sslOptions.pathToCert);
sslKey = readFileSync(sslOptions.pathToKey);
} else {
logger.warn(
`Encountered SSL options in project.json, however, the certificate files do not exist in the filesystem. Using http.`
);
logger.warn(
`Attempted to find '${sslOptions.pathToCert}' and '${sslOptions.pathToKey}'.`
);
}
}
const http = require('http');
const https = require('https');

logger.info(`NX Starting static remotes proxies...`);
for (const app of staticRemotesConfig.remotes) {
const expressProxy: Express = express();
expressProxy.use(
createProxyMiddleware({
target: mappedLocationsOfRemotes[app],
changeOrigin: true,
secure: sslCert ? false : undefined,
})
);
const proxyServer = expressProxy.listen(
staticRemotesConfig.config[app].port
);
const proxyServer = (sslCert ? https : http)
.createServer({ cert: sslCert, key: sslKey }, expressProxy)
.listen(staticRemotesConfig.config[app].port);
process.on('SIGTERM', () => proxyServer.close());
process.on('exit', () => proxyServer.close());
}
Expand Down

0 comments on commit ecc3d03

Please sign in to comment.