Skip to content

Commit

Permalink
Pass certs on the websocket
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Bygrave <chris.bygrave@kaleido.io>
  • Loading branch information
chrisbygrave authored and shorsher committed May 9, 2023
1 parent 1ece584 commit da70185
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/event-stream/event-stream.service.ts
Expand Up @@ -20,7 +20,7 @@ import { AxiosRequestConfig } from 'axios';
import { lastValueFrom } from 'rxjs';
import * as WebSocket from 'ws';
import { IAbiMethod } from '../tokens/tokens.interfaces';
import { getHttpRequestOptions } from '../utils';
import { getHttpRequestOptions, getWebsocketOptions } from '../utils';
import { Context } from '../request-context/request-context.decorator';
import { FFRequestIDHeader } from '../request-context/constants';
import {
Expand Down Expand Up @@ -58,9 +58,7 @@ export class EventStreamSocket {
this.disconnectDetected = false;
this.closeRequested = false;

const auth =
this.username && this.password ? { auth: `${this.username}:${this.password}` } : undefined;
this.ws = new WebSocket(this.url, auth);
this.ws = new WebSocket(this.url, getWebsocketOptions(this.username, this.password));
this.ws
.on('open', () => {
if (this.disconnectDetected) {
Expand Down
17 changes: 17 additions & 0 deletions src/utils.ts
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as https from 'https';
import { NestApplicationOptions } from '@nestjs/common';
import { AxiosRequestConfig } from 'axios';
import { ClientOptions } from 'ws';

interface Certificates {
key: string;
Expand Down Expand Up @@ -29,6 +30,22 @@ const getCertificates = (): Certificates | undefined => {
return { key, cert, ca };
};

export const getWebsocketOptions = (username: string, password: string): ClientOptions => {
const requestOptions: ClientOptions = {};
if (username && username !== '' && password && password !== '') {
requestOptions.headers = {
Authorization: `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`,
};
}
const certs = getCertificates();
if (certs) {
requestOptions.ca = certs.ca;
requestOptions.cert = certs.cert;
requestOptions.key = certs.key;
}
return requestOptions;
};

export const getHttpRequestOptions = (username: string, password: string) => {
const requestOptions: AxiosRequestConfig = {};
if (username !== '' && password !== '') {
Expand Down

0 comments on commit da70185

Please sign in to comment.