mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 00:52:55 +00:00
refactor: update dependencies
This commit is contained in:
committed by
Rainer Killinger
parent
9512bca329
commit
053a6ce23f
207
src/main.ts
207
src/main.ts
@@ -13,111 +13,117 @@
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import * as config from 'config';
|
||||
import * as Dockerode from 'dockerode';
|
||||
import {existsSync, readFile} from 'fs-extra';
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import Dockerode from 'dockerode';
|
||||
import {render} from 'mustache';
|
||||
import {join} from 'path';
|
||||
import {
|
||||
ConfigFile,
|
||||
logger,
|
||||
asyncReadFile,
|
||||
configFile,
|
||||
isFileType,
|
||||
protocolHardeningParameters,
|
||||
SSLFilePaths,
|
||||
sslHardeningParameters,
|
||||
TemplateView,
|
||||
} from './common';
|
||||
|
||||
const configFile: ConfigFile = config.util.toObject();
|
||||
|
||||
/**
|
||||
* Checks if a ContainerInfo matches a name and version regex
|
||||
* @param {string} name
|
||||
* @param {RegExp} versionRegex
|
||||
* @param {Dockerode.ContainerInfo} container
|
||||
* @return {boolean}
|
||||
*
|
||||
* @param name Name to check
|
||||
* @param versionRegex Version regex to check
|
||||
* @param container Container info for check
|
||||
*/
|
||||
export function containerMatchesRegex(name: string, versionRegex: RegExp, container: Dockerode.ContainerInfo): boolean {
|
||||
return typeof container.Labels['stapps.version'] === 'string' &&
|
||||
container.Labels['stapps.version'].match(versionRegex) !== null &&
|
||||
typeof container.Labels['com.docker.compose.service'] === 'string' &&
|
||||
container.Labels['com.docker.compose.service'] === name;
|
||||
return typeof container.Labels['stapps.version'] === 'string'
|
||||
&& container.Labels['stapps.version'].match(versionRegex) !== null
|
||||
&& typeof container.Labels['com.docker.compose.service'] === 'string'
|
||||
&& container.Labels['com.docker.compose.service'] === name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets Gateway (ip:port) of given ContainerInfo. Returns an empty String if there is no Gateway.
|
||||
* Gets Gateway (ip:port) of given ContainerInfo
|
||||
*
|
||||
* Returns an empty String if there is no Gateway.
|
||||
* This assumes that a backend runs in the container and it exposes one port.
|
||||
* @param {Dockerode.ContainerInfo} container
|
||||
* @return {string}
|
||||
*
|
||||
* @param container Container info of which to get address from
|
||||
*/
|
||||
export function getGatewayOfStAppsBackend(container: Dockerode.ContainerInfo): string {
|
||||
|
||||
export async function getGatewayOfStAppsBackend(container: Dockerode.ContainerInfo): Promise<string> {
|
||||
if (container.Ports.length === 0) {
|
||||
logger.error(
|
||||
'Container',
|
||||
container.Id,
|
||||
'does not advertise any Port. Please expose a Port if the container should be accessible by NGINX.',
|
||||
);
|
||||
await Logger.error(`Container ${container.Id} does not advertise any port.
|
||||
Please expose a port if the container should be accessible by NGINX.`);
|
||||
|
||||
return '';
|
||||
} else {
|
||||
// ip:port
|
||||
return container.Ports[0].IP + ':' + container.Ports[0].PublicPort;
|
||||
}
|
||||
|
||||
// ip:port
|
||||
return `${container.Ports[0].IP}:${container.Ports[0].PublicPort}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an upstream map. It maps all stapps-backend-containers to an gateway
|
||||
* @param {string[]} activeVersions
|
||||
* @param {string[]} outdatedVersions
|
||||
* @param {Dockerode.ContainerInfo[]} containers
|
||||
* @return {string}
|
||||
* Generates an upstream map
|
||||
*
|
||||
* It maps all stapps-backend-containers to an gateway.
|
||||
*
|
||||
* @param activeVersions List of active versions
|
||||
* @param outdatedVersions List of outdated versions
|
||||
* @param containers List of container infos
|
||||
*/
|
||||
export function generateUpstreamMap(
|
||||
export async function generateUpstreamMap(
|
||||
activeVersions: string[],
|
||||
outdatedVersions: string[],
|
||||
containers: Dockerode.ContainerInfo[],
|
||||
): string {
|
||||
): Promise<string> {
|
||||
let result = 'map $http_x_stapps_version $proxyurl {\n default unsupported;\n';
|
||||
let upstreams = '';
|
||||
|
||||
let foundMatchingContainer = false;
|
||||
|
||||
// active versions
|
||||
result += activeVersions.map((activeVersionRegex) => {
|
||||
const upstreamName = activeVersionRegex.replace(/[\\|\.|\+]/g, '_');
|
||||
result += (await Promise.all(
|
||||
activeVersions
|
||||
.map(async (activeVersionRegex) => {
|
||||
const upstreamName = activeVersionRegex.replace(/[\\|.+]/g, '_');
|
||||
|
||||
const activeBackends = containers.filter((container) => {
|
||||
return containerMatchesRegex('backend', new RegExp(activeVersionRegex), container);
|
||||
});
|
||||
const activeBackends = containers.filter((container) => {
|
||||
return containerMatchesRegex('backend', new RegExp(activeVersionRegex), container);
|
||||
});
|
||||
|
||||
if (activeBackends.length > 0) {
|
||||
if (activeBackends.length > 0) {
|
||||
foundMatchingContainer = true;
|
||||
|
||||
foundMatchingContainer = true;
|
||||
if (activeBackends.length > 1) {
|
||||
throw new Error('Multiple backends for one version found.');
|
||||
}
|
||||
|
||||
if (activeBackends.length > 1) {
|
||||
throw new Error('Multiple backends for one version found.');
|
||||
}
|
||||
const gateWayOfContainer = await getGatewayOfStAppsBackend(activeBackends[0]);
|
||||
|
||||
const gateWayOfContainer = getGatewayOfStAppsBackend(activeBackends[0]);
|
||||
if (gateWayOfContainer.length !== 0) {
|
||||
upstreams += `\nupstream ${upstreamName} {\n server ${gateWayOfContainer};\n}`;
|
||||
|
||||
return ` \"~${activeVersionRegex}\" ${upstreamName};\n`;
|
||||
}
|
||||
|
||||
return ` \"~${activeVersionRegex}\" unavailable;\n`;
|
||||
}
|
||||
await Logger.error('No backend for version', activeVersionRegex, 'found');
|
||||
|
||||
if (gateWayOfContainer.length !== 0) {
|
||||
upstreams += `\nupstream ${upstreamName} {\n server ${gateWayOfContainer};\n}`;
|
||||
return ` \"~${activeVersionRegex}\" ${upstreamName};\n`;
|
||||
} else {
|
||||
return ` \"~${activeVersionRegex}\" unavailable;\n`;
|
||||
}
|
||||
} else {
|
||||
logger.error('No backend for version', activeVersionRegex, 'found');
|
||||
return ` \"~${activeVersionRegex}\" unavailable;\n`;
|
||||
}
|
||||
}).join('');
|
||||
}),
|
||||
)).join('');
|
||||
|
||||
// outdated versions
|
||||
result += outdatedVersions.map((outdatedVersionRegex) => {
|
||||
return ` \"~${outdatedVersionRegex}\" outdated;`;
|
||||
}).join('') + '\n\}';
|
||||
result += outdatedVersions
|
||||
.map((outdatedVersionRegex) => {
|
||||
return ` \"~${outdatedVersionRegex}\" outdated;`;
|
||||
})
|
||||
.join('');
|
||||
|
||||
result += '\n\}';
|
||||
|
||||
if (!foundMatchingContainer) {
|
||||
logger.error(
|
||||
await Logger.error(
|
||||
'No container with matching version label found. Please start a container with a matching version Label.',
|
||||
);
|
||||
}
|
||||
@@ -127,74 +133,65 @@ export function generateUpstreamMap(
|
||||
|
||||
/**
|
||||
* Generates http or https listener
|
||||
* @param sslFiles
|
||||
* @returns {string}
|
||||
*/
|
||||
function generateListener(sslFilePaths: SSLFilePaths) {
|
||||
|
||||
function isSSLCert(path: string) {
|
||||
return existsSync(path) && /.*\.crt$/.test(path);
|
||||
}
|
||||
|
||||
function isSSLKey(path: string) {
|
||||
return existsSync(path) && /.*\.key$/.test(path);
|
||||
}
|
||||
|
||||
function isPEMFile(path: string) {
|
||||
return existsSync(path) && /.*\.pem$/.test(path);
|
||||
}
|
||||
|
||||
export function generateListener(sslFilePaths: SSLFilePaths) {
|
||||
let listener = '';
|
||||
|
||||
if (typeof sslFilePaths !== 'undefined' &&
|
||||
typeof sslFilePaths.certificate === 'string' && isSSLCert(sslFilePaths.certificate) &&
|
||||
typeof sslFilePaths.certificateChain === 'string' && isSSLCert(sslFilePaths.certificate) &&
|
||||
typeof sslFilePaths.certificateKey === 'string' && isSSLKey(sslFilePaths.certificate) &&
|
||||
typeof sslFilePaths.dhparam === 'string' && isPEMFile(sslFilePaths.dhparam)
|
||||
) {
|
||||
typeof sslFilePaths.certificate !== 'undefined' && isFileType(sslFilePaths.certificate,'crt') &&
|
||||
typeof sslFilePaths.certificateChain !== 'undefined' && isFileType(sslFilePaths.certificateChain,'crt') &&
|
||||
typeof sslFilePaths.certificateKey !== 'undefined' && isFileType(sslFilePaths.certificateKey,'key') &&
|
||||
typeof sslFilePaths.dhparam !== 'undefined' && isFileType(sslFilePaths.dhparam,'pem')
|
||||
) {
|
||||
// https listener
|
||||
listener = 'listen 443 ssl default_server;\n' +
|
||||
`ssl_certificate ${sslFilePaths.certificate};\n` +
|
||||
`ssl_certificate_key ${sslFilePaths.certificateKey};\n` +
|
||||
`ssl_trusted_certificate ${sslFilePaths.certificateChain};\n` +
|
||||
`ssl_dhparam ${sslFilePaths.dhparam};\n` +
|
||||
`${sslHardeningParameters}`;
|
||||
listener = `listen 443 ssl default_server;
|
||||
ssl_certificate ${sslFilePaths.certificate};
|
||||
ssl_certificate_key ${sslFilePaths.certificateKey};
|
||||
ssl_trusted_certificate ${sslFilePaths.certificateChain};
|
||||
ssl_dhparam ${sslFilePaths.dhparam};
|
||||
${sslHardeningParameters}`;
|
||||
} else {
|
||||
// default http listener
|
||||
listener = 'listen 80 default_server;';
|
||||
logger.warn('Https usage is not setup properly, falling back to http!');
|
||||
|
||||
Logger.warn('Https usage is not setup properly, falling back to http!');
|
||||
}
|
||||
listener = `${listener}\n${protocolHardeningParameters}\n`;
|
||||
listener = `${listener}
|
||||
|
||||
${protocolHardeningParameters}
|
||||
`;
|
||||
|
||||
return listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a mustache template file with given view object
|
||||
* @param path (path to file)
|
||||
* @param view
|
||||
* @param callback
|
||||
*
|
||||
* @param path Path to template file
|
||||
* @param view Data to render template with
|
||||
*/
|
||||
async function renderTemplate(path: string, view: any): Promise<string> {
|
||||
const content = await readFile(path, 'utf8');
|
||||
async function renderTemplate(path: string, view: unknown): Promise<string> {
|
||||
const content = await asyncReadFile(path, 'utf8');
|
||||
|
||||
return render(content, view);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns view for nginx config file
|
||||
* @param containers
|
||||
*
|
||||
* @param containers List of container info
|
||||
*/
|
||||
export async function getTemplateView(containers: Dockerode.ContainerInfo[]): Promise<TemplateView> {
|
||||
const cors = await asyncReadFile('./fixtures/cors.template', 'utf8');
|
||||
|
||||
const cors = await readFile('./fixtures/cors.template', 'utf8');
|
||||
|
||||
const visibleRoutesPromises = configFile.visibleRoutes.map((route) => {
|
||||
const visibleRoutesPromises = configFile.visibleRoutes.map(async (route) => {
|
||||
return renderTemplate(join('fixtures', 'visibleRoute.template'), {
|
||||
cors,
|
||||
route,
|
||||
});
|
||||
});
|
||||
|
||||
const hiddenRoutesPromises = configFile.hiddenRoutes.map((route) => {
|
||||
const hiddenRoutesPromises = configFile.hiddenRoutes.map(async (route) => {
|
||||
return renderTemplate(join('fixtures', 'hiddenRoute.template'), {
|
||||
cors,
|
||||
route,
|
||||
@@ -202,7 +199,7 @@ export async function getTemplateView(containers: Dockerode.ContainerInfo[]): Pr
|
||||
});
|
||||
|
||||
return {
|
||||
dockerVersionMap: generateUpstreamMap(configFile.activeVersions, configFile.outdatedVersions, containers),
|
||||
dockerVersionMap: await generateUpstreamMap(configFile.activeVersions, configFile.outdatedVersions, containers),
|
||||
hiddenRoutes: (await Promise.all(hiddenRoutesPromises)).join(''),
|
||||
listener: generateListener(configFile.sslFilePaths),
|
||||
staticRoute: await renderTemplate(join('fixtures', 'staticRoute.template'), {cors}),
|
||||
@@ -212,7 +209,8 @@ export async function getTemplateView(containers: Dockerode.ContainerInfo[]): Pr
|
||||
|
||||
/**
|
||||
* Read the list of docker containers
|
||||
* @param pathToDockerSocket
|
||||
*
|
||||
* @param pathToDockerSocket Path to docker socket
|
||||
*/
|
||||
export async function getContainers(pathToDockerSocket = '/var/run/docker.sock'): Promise<Dockerode.ContainerInfo[]> {
|
||||
const docker = new Dockerode({
|
||||
@@ -221,12 +219,15 @@ export async function getContainers(pathToDockerSocket = '/var/run/docker.sock')
|
||||
|
||||
const containers = await docker.listContainers();
|
||||
|
||||
/* istanbul ignore next */
|
||||
if (containers.length === 0) {
|
||||
throw new Error(
|
||||
'No running docker containers found.' +
|
||||
`Please check if docker is running and Node.js can access the docker socket (${pathToDockerSocket})`,
|
||||
`No running docker containers found.
|
||||
|
||||
Please check if docker is running and Node.js can access the docker socket (${pathToDockerSocket})`,
|
||||
);
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
return containers;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user