Skip to content

Commit

Permalink
Merge pull request #795 from snyk/fix/unibroker-handle-broker-token-r…
Browse files Browse the repository at this point in the history
…otation-hot-reload

fix: unibroker handle runtime rotating brkr identifier
  • Loading branch information
aarlaud authored Jul 25, 2024
2 parents 0c379a8 + 57436be commit 8b3b2ab
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
7 changes: 0 additions & 7 deletions lib/client/brokerClientPlugins/pluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ export const runStartupPlugins = async (clientOpts, connectionKey) => {
string,
BrokerPlugin[]
>;

// const connectionsKeys = clientOpts.config.connections
// ? Object.keys(clientOpts.config.connections)
// : [];

// for (const connectionKey of connectionsKeys) {
if (
loadedPlugins.has(`${clientOpts.config.connections[connectionKey].type}`)
) {
Expand All @@ -81,7 +75,6 @@ export const runStartupPlugins = async (clientOpts, connectionKey) => {
);
}
}
// }
};

export const runPreRequestPlugins = async (
Expand Down
19 changes: 19 additions & 0 deletions lib/client/connectionsManager/connectionHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { WebSocketConnection } from '../types/client';

export const shutDownConnectionPair = (
websocketConnections: WebSocketConnection[],
connectionIndex: number,
) => {
const friendlyName = websocketConnections[connectionIndex].friendlyName;
websocketConnections[connectionIndex].end();
websocketConnections[connectionIndex].destroy();
websocketConnections.splice(connectionIndex, 1);
const secondTunnelIndex = websocketConnections.findIndex(
(websocketConnection) => websocketConnection.friendlyName == friendlyName,
);
websocketConnections[secondTunnelIndex].end();
websocketConnections[secondTunnelIndex].destroy();
websocketConnections.splice(secondTunnelIndex, 1);

//TODO: Clean up plugins elements (intervals, etc)
};
34 changes: 24 additions & 10 deletions lib/client/connectionsManager/mainWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { log as logger } from '../../logs/logger';
import { createWebSocketConnectionPairs } from '../socket';
import { runStartupPlugins } from '../brokerClientPlugins/pluginManager';
import { addTimerToTerminalHandlers } from '../../common/utils/signals';
import { shutDownConnectionPair } from './connectionHelpers';
export const setMainWatcher = async (
clientOpts: LoadedClientOpts,
websocketConnections: WebSocketConnection[],
Expand Down Expand Up @@ -50,16 +51,7 @@ export const setMainWatcher = async (
},
`Shutting down unused connection`,
);
websocketConnections[currentWebsocketConnectionIndex].end();
websocketConnections[currentWebsocketConnectionIndex].destroy();
websocketConnections.splice(currentWebsocketConnectionIndex, 1);
const secondTunnelIndex = websocketConnections.findIndex(
(websocketConnection) =>
websocketConnection.friendlyName == integrationsKeys[i],
);
websocketConnections[secondTunnelIndex].end();
websocketConnections[secondTunnelIndex].destroy();
websocketConnections.splice(secondTunnelIndex, 1);
shutDownConnectionPair(websocketConnections, i);
} else {
logger.info(
{
Expand All @@ -81,6 +73,28 @@ export const setMainWatcher = async (
);
await runStartupPlugins(clientOpts, integrationsKeys[i]);

await createWebSocketConnectionPairs(
websocketConnections,
clientOpts,
globalIdentifyingMetadata,
integrationsKeys[i],
);
} else if (
// Token rotation for the connection at hand
clientOpts.config.connections[`${integrationsKeys[i]}`].identifier !=
websocketConnections[currentWebsocketConnectionIndex].identifier
) {
logger.info(
{ connectionName: integrationsKeys[i] },
'Updating configured connection for new identifier.',
);
// shut down previous tunnels
shutDownConnectionPair(websocketConnections, i);

// setup new tunnels

await runStartupPlugins(clientOpts, integrationsKeys[i]);

await createWebSocketConnectionPairs(
websocketConnections,
clientOpts,
Expand Down
12 changes: 10 additions & 2 deletions lib/client/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,18 @@ export const createWebSocketConnectionPairs = async (
);
}
if (serverId === null) {
logger.warn({}, 'could not receive server id from Broker Dispatcher');
if (clientOpts.config.BROKER_HA_MODE_ENABLED == 'true') {
logger.warn({}, 'could not receive server id from Broker Dispatcher');
}
serverId = '';
} else {
logger.info({ serverId }, 'received server id');
logger.info(
{
connection: socketIdentifyingMetadata.friendlyName,
serverId: serverId,
},
'received server id',
);
clientOpts.config.connections[
`${socketIdentifyingMetadata.friendlyName}`
].serverId = serverId;
Expand Down

0 comments on commit 8b3b2ab

Please sign in to comment.