Skip to content

Commit

Permalink
Merge pull request #837 from snyk/chore/add-identifying-headers-on-un…
Browse files Browse the repository at this point in the history
…ibroker-conn-retrieval

chore: [unibroker] add client id data on conn sync calls
  • Loading branch information
aarlaud authored Sep 5, 2024
2 parents 51ba4e5 + df12650 commit 95bf037
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/client/config/configHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getConfig,
loadBrokerConfig,
} from '../../common/config/config';
import version from '../../common/utils/version';

export const reloadConfig = async (clientOpts) => {
// Reload config with connection
Expand All @@ -22,6 +23,8 @@ export const getClientConfigMetadata = (
clientConfig: Record<string, any>,
): ConfigMetadata => {
const configMetadata: ConfigMetadata = {
brokerClientId: clientConfig.brokerClientId,
version: `${version}`,
haMode: highAvailabilityModeEnabled(clientConfig),
debugMode: clientConfig.logLevel === 'debug' ? true : false,
bodyLogMode: clientConfig.logEnableBody ? true : false,
Expand Down
3 changes: 3 additions & 0 deletions lib/client/config/remoteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PostFilterPreparedRequest } from '../../common/relay/prepareRequest';
import { ClientOpts } from '../../common/types/options';
import { BrokerConnectionApiResponse } from '../types/api';
import { capitalizeKeys } from '../utils/configurations';
import version from '../../common/utils/version';

export const retrieveConnectionsForDeployment = async (
clientOpts: ClientOpts,
Expand All @@ -16,6 +17,8 @@ export const retrieveConnectionsForDeployment = async (
headers: {
'Content-Type': 'application/vnd.api+json',
Authorization: `${clientOpts.accessToken?.authHeader}`,
'x-broker-client-id': `${clientOpts.config.brokerClientId}`,
'x-broker-client-version': `${version}`,
},
method: 'GET',
};
Expand Down
14 changes: 10 additions & 4 deletions lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ export const main = async (clientOpts: ClientOpts) => {
try {
logger.info({ version }, 'Broker starting in client mode');
let hookResults: HookResults = {};
const brokerClientId = uuidv4();
logger.info({ brokerClientId }, 'generated broker client id');
clientOpts.config.brokerClientId = uuidv4();
logger.info(
{ brokerClientId: clientOpts.config.brokerClientId },
'generated broker client id',
);

clientOpts.config.API_BASE_URL =
clientOpts.config.API_BASE_URL ??
Expand All @@ -79,7 +82,10 @@ export const main = async (clientOpts: ClientOpts) => {
);
} else {
// universal broker logic is in connection manager
hookResults = await processStartUpHooks(clientOpts, brokerClientId);
hookResults = await processStartUpHooks(
clientOpts,
clientOpts.config.brokerClientId,
);
}

const loadedClientOpts: LoadedClientOpts = {
Expand All @@ -94,7 +100,7 @@ export const main = async (clientOpts: ClientOpts) => {

const globalIdentifyingMetadata: IdentifyingMetadata = {
capabilities: ['post-streams'],
clientId: brokerClientId,
clientId: clientOpts.config.brokerClientId,
filters: loadedClientOpts.filters,
preflightChecks: hookResults.preflightCheckResults,
version,
Expand Down
2 changes: 2 additions & 0 deletions lib/client/types/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface HookResults {
}

export interface ConfigMetadata {
brokerClientId: string;
version: string;
haMode: boolean;
debugMode: boolean;
bodyLogMode: boolean;
Expand Down
4 changes: 4 additions & 0 deletions test/functional/server-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('proxy requests originating from behind the broker server', () => {
role: 'primary',
clientConfig: {
bodyLogMode: false,
brokerClientId: expect.any(String),
credPooling: true, //client sets a PASSWORD_POOL
customAccept: true,
debugMode: false,
Expand All @@ -79,6 +80,7 @@ describe('proxy requests originating from behind the broker server', () => {
proxy: false,
tlsReject: false,
universalBroker: false,
version: 'local',
},
identifier:
'67f47824f806ee9c2fe6c7cc5849269fc1bc599c18e401ee2e2aea422bab6128',
Expand All @@ -92,6 +94,7 @@ describe('proxy requests originating from behind the broker server', () => {
role: 'secondary',
clientConfig: {
bodyLogMode: false,
brokerClientId: expect.any(String),
credPooling: true, //client sets a PASSWORD_POOL
customAccept: true,
debugMode: false,
Expand All @@ -101,6 +104,7 @@ describe('proxy requests originating from behind the broker server', () => {
proxy: false,
tlsReject: false,
universalBroker: false,
version: 'local',
},
identifier:
'67f47824f806ee9c2fe6c7cc5849269fc1bc599c18e401ee2e2aea422bab6128',
Expand Down
9 changes: 9 additions & 0 deletions test/unit/configHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ describe('config', () => {
it('everything is false for empty config', async () => {
await loadBrokerConfig();
const config = getConfig();
config.brokerClientId = '123';
expect(getClientConfigMetadata(config as LoadedClientOpts)).toEqual({
bodyLogMode: false,
brokerClientId: '123',
credPooling: false,
customAccept: false,
debugMode: false,
Expand All @@ -48,6 +50,7 @@ describe('config', () => {
tlsReject: false,
insecureDownstream: false,
universalBroker: false,
version: 'local',
});
});

Expand All @@ -64,8 +67,10 @@ describe('config', () => {
process.env.UNIVERSAL_BROKER_ENABLED = 'true';
await loadBrokerConfig();
const config = getConfig();
config.brokerClientId = '123';
expect(getClientConfigMetadata(config as LoadedClientOpts)).toEqual({
bodyLogMode: true,
brokerClientId: '123',
credPooling: true,
customAccept: true,
debugMode: true,
Expand All @@ -75,6 +80,7 @@ describe('config', () => {
tlsReject: true,
insecureDownstream: true,
universalBroker: true,
version: 'local',
});
});

Expand All @@ -85,8 +91,10 @@ describe('config', () => {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1';
await loadBrokerConfig();
const config = getConfig();
config.brokerClientId = '123';
expect(getClientConfigMetadata(config as LoadedClientOpts)).toEqual({
bodyLogMode: false,
brokerClientId: '123',
credPooling: false,
customAccept: false,
debugMode: false,
Expand All @@ -96,6 +104,7 @@ describe('config', () => {
tlsReject: false,
insecureDownstream: false,
universalBroker: false,
version: 'local',
});
});

Expand Down
2 changes: 2 additions & 0 deletions test/unit/connectionsManager/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('Connections Manager', () => {
id: '',
isDisabled: false,
clientConfig: {
brokerClientId: '123',
haMode: false,
debugMode: false,
bodyLogMode: false,
Expand All @@ -30,6 +31,7 @@ describe('Connections Manager', () => {
customAccept: false,
insecureDownstream: false,
universalBroker: false,
version: 'local',
},
role: Role.primary,
};
Expand Down

0 comments on commit 95bf037

Please sign in to comment.