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 committed May 3, 2023
1 parent 471f814 commit 74b9e7b
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import WebSocket from 'ws';
import { FFRequestIDHeader } from '../request-context/constants';
import { Context } from '../request-context/request-context.decorator';
import { IAbiMethod } from '../tokens/tokens.interfaces';
import { getHttpRequestOptions } from '../utils';
import { getHttpRequestOptions, getWebsocketOptions } from '../utils';
import {
Event,
EventBatch,
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
Original file line number Diff line number Diff line change
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 74b9e7b

Please sign in to comment.