Skip to content

Commit

Permalink
large payload container tls
Browse files Browse the repository at this point in the history
Signed-off-by: simvalery <[email protected]>

Signed-off-by: simvalery <[email protected]>
  • Loading branch information
simvalery committed Oct 9, 2024
1 parent f46e3bb commit dbb657e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
51 changes: 49 additions & 2 deletions common/src/mq/large-payload-container.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import express from 'express'
import http from 'http'
import https from 'https'
import { hostname } from 'os';
import { GenerateUUIDv4 } from '@guardian/interfaces';
import { Singleton } from '../decorators/singleton.js';
Expand Down Expand Up @@ -51,7 +53,39 @@ export class LargePayloadContainer {
*/
private readonly DOMAIN: string;

/**
* Enable TLS
* @private
*/
public enableTLS: boolean;

/**
* TLS cert
* @private
*/
private readonly tlsCert: string;

/**
* TLS key
*/
private readonly tlsKey: string;

/**
* TLS CA
* @private
*/
private readonly tlsCA: string;

constructor() {
this.enableTLS = false;

if (process.env.TLS_SERVER_CERT && process.env.TLS_SERVER_KEY) {
this.enableTLS = true;
this.tlsCert = process.env.TLS_SERVER_CERT;
this.tlsKey = process.env.TLS_SERVER_KEY;
this.tlsCA = process.env.TLS_SERVER_CA
}

if (process.env.DIRECT_MESSAGE_PORT) {
this.PORT = parseInt(process.env.DIRECT_MESSAGE_PORT, 10);
this._portGenerated = false;
Expand All @@ -60,7 +94,8 @@ export class LargePayloadContainer {
this.PORT = this.generateRandom(50000, 59999);
}
this.DOMAIN = (process.env.DIRECT_MESSAGE_HOST) ? process.env.DIRECT_MESSAGE_HOST : hostname();
this.PROTOCOL = (process.env.DIRECT_MESSAGE_PROTOCOL) ? process.env.DIRECT_MESSAGE_PROTOCOL as any : 'http';
const defaultProtocol = this.enableTLS ? 'https' : 'http';
this.PROTOCOL = (process.env.DIRECT_MESSAGE_PROTOCOL) ? process.env.DIRECT_MESSAGE_PROTOCOL as any : defaultProtocol;

this.objectsMap = new Map();
this._started = false;
Expand All @@ -85,7 +120,19 @@ export class LargePayloadContainer {
res.send(buf);
})

const server = app.listen(this.PORT, () => {
let s: http.Server | https.Server;

if (this.enableTLS) {
s = https.createServer({
key: this.tlsKey,
cert: this.tlsCert,
ca: this.tlsCA
}, app);
} else {
s = http.createServer(app);
}

const server = s.listen(this.PORT, () => {
this._started = true;
try {
// this.logger.info(`Large objects server starts on ${this.PORT} port`, [process.env.SERVICE_CHANNEL?.toUpperCase()]);
Expand Down
9 changes: 9 additions & 0 deletions common/src/mq/zip-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ErrorCode, JSONCodec, NatsError } from 'nats';
// import { gzip, unzip } from 'zlib';
import { LargePayloadContainer } from './large-payload-container.js';
import axios from 'axios';
import https from 'https';

/**
* Zip Codec
Expand Down Expand Up @@ -42,6 +43,14 @@ export function ZipCodec() {
// const parsed = JSON.parse(decompressed.toString());
if (parsed?.hasOwnProperty('directLink')) {
const directLink = parsed.directLink;
if (process.env.TLS_CERT && process.env.TLS_KEY) {
const httpsAgent = new https.Agent({
cert: process.env.TLS_CERT,
key: process.env.TLS_KEY,
ca: process.env.TLS_CA
});
axios.defaults.httpsAgent = httpsAgent;
}
const response = await axios.get(directLink, {
responseType: 'arraybuffer'
});
Expand Down

0 comments on commit dbb657e

Please sign in to comment.