Skip to content

Commit

Permalink
Merge pull request #722 from snyk/fix/replace-cfg-values-pointing-to-…
Browse files Browse the repository at this point in the history
…cfg-value

fix: interpolate reflected cfg values
  • Loading branch information
aarlaud authored Feb 29, 2024
2 parents eaec38c + 816dce6 commit 143bdc7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 40 deletions.
22 changes: 20 additions & 2 deletions lib/common/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ export const findProjectRoot = (startDir: string): string | null => {
return null;
};

export const loadBrokerConfig = () => {
export const loadBrokerConfig = (localConfigForTest?) => {
dotenv.config({
path: path.join(process.cwd(), '.env'),
});
try {
const localConfig = loadConfig(findProjectRoot(__dirname) ?? process.cwd());
const localConfig = localConfigForTest
? localConfigForTest
: loadConfig(findProjectRoot(__dirname) ?? process.cwd());
expand(process.env);
config = Object.assign({}, camelify(localConfig), camelify(process.env));
// for each in config.brokerClientConfiguration.common.default check if process env exist and if it does,
Expand Down Expand Up @@ -186,6 +188,22 @@ function expand(obj: Record<string, any>): Record<string, any> {
return obj;
}

export const expandPlaceholderValuesInFlatList = (
objectToExpand: Object,
referenceConfig: Object,
) => {
for (const key of Object.keys(objectToExpand)) {
if (
typeof objectToExpand[key] == 'string' &&
objectToExpand[key].startsWith('$')
) {
objectToExpand[key] =
referenceConfig[objectToExpand[key].replace('$', '')];
}
}
return objectToExpand;
};

export const expandConfigObjectRecursively = (
objToExpand: Object,
referenceConfig: Object,
Expand Down
28 changes: 21 additions & 7 deletions lib/common/config/universal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { log as logger } from '../../logs/logger';
import { ConnectionConfig } from '../../client/types/config';
import { getConfig } from './config';
import { expandPlaceholderValuesInFlatList, getConfig } from './config';

export const getConfigForType = (type: string) => {
const config = getConfig();
Expand Down Expand Up @@ -63,21 +63,35 @@ export const getConfigForIdentifier = (identifier: string, config) => {
// `Unable to find configuration type for ${identifier}. Please review config.`,
// );
}
return {
const configToOverload = {
...(connectionType ? getConfigForType(connectionType) : {}),
...(connectionKey ? config.connections[connectionKey] : {}),
...(connectionKey
? expandPlaceholderValuesInFlatList(
config.connections[connectionKey],
config.connections[connectionKey],
)
: {}),
};
return configToOverload;
};

export const overloadConfigWithConnectionSpecificConfig = (
connectionIdentifier,
localConfig,
) => {
const config = getConfig();
return {
...localConfig,
...getConfigForIdentifier(connectionIdentifier, config),
};
let overloadedConfig = Object.assign(
{},
{
...localConfig,
...getConfigForIdentifier(connectionIdentifier, config),
},
);
overloadedConfig = expandPlaceholderValuesInFlatList(
overloadedConfig,
overloadedConfig,
);
return overloadedConfig;
};

const findConnectionWithIdentifier = (connections: Object, identifier) => {
Expand Down
89 changes: 58 additions & 31 deletions test/unit/relay-response-body-universal.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const PORT = 8001;
process.env.BROKER_SERVER_URL = `http://localhost:${PORT}`;

jest.mock('../../lib/common/http/request');
jest.mock('../../lib/common/relay/requestsHelper');
import { WebSocketConnection } from '../../lib/client/types/client';
import { makeRequestToDownstream } from '../../lib/common/http/request';
import { loadBrokerConfig } from '../../lib/common/config/config';
import { loadAllFilters } from '../../lib/common/filter/filtersAsync';
import { makeLegacyRequest } from '../../lib/common/relay/requestsHelper';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const mockedFn = makeRequestToDownstream.mockImplementation((data) => {
const mockedFn = makeLegacyRequest.mockImplementation((data, emit) => {
emit();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return data;
Expand Down Expand Up @@ -37,20 +40,27 @@ const dummyWebsocketHandler: WebSocketConnection = {
send: () => {},
serverId: '0',
socket: {},
supportedIntegrationType: 'github',
transport: '',
url: '',
on: () => {},
readyState: 3,
supportedIntegrationType: 'github',
};

const dummyLoadedFilters = {
private: () => {
return { url: '/', auth: '', stream: true };
},
public: () => {
return { url: '/', auth: '', stream: true };
},
const dummyLoadedFilters = new Map();
dummyLoadedFilters['github'] = {
private: [
{
method: 'any',
url: '/*',
},
],
public: [
{
method: 'any',
url: '/*',
},
],
};

describe('body relay', () => {
Expand All @@ -63,21 +73,33 @@ describe('body relay', () => {
jest.clearAllMocks();
});

it('relay swaps body values found in BROKER_VAR_SUB', (done) => {
it('relay swaps body values found in BROKER_VAR_SUB', () => {
expect.hasAssertions();

const brokerToken = 'test-broker';

const config = {
universalBrokerEnabled: true,
brokerType: 'client',
connections: {
myconn: {
identifier: brokerToken,
HOST: 'localhost',
HOST: '$HOST2',
PORT: '8001',
HOST2: 'localhost',
type: 'github',
},
},
brokerClientConfiguration: {
common: {
default: {},
required: {},
},
github: { default: {} },
},
};
loadBrokerConfig(config);

const options: LoadedClientOpts | LoadedServerOpts = {
filters: {
private: [
Expand All @@ -90,16 +112,14 @@ describe('body relay', () => {
},
config,
port: 8001,
loadedFilters: dummyLoadedFilters,
loadedFilters: loadAllFilters(dummyLoadedFilters, config),
};

const route = relay(options, dummyWebsocketHandler)(brokerToken);

const body = {
BROKER_VAR_SUB: ['url'],
url: '${HOST}:${PORT}/webhook',
};

route(
{
url: '/',
Expand All @@ -110,34 +130,43 @@ describe('body relay', () => {
headers: {},
},
() => {
expect(makeRequestToDownstream).toHaveBeenCalledTimes(1);
expect(makeLegacyRequest).toHaveBeenCalledTimes(1);
const arg = mockedFn.mock.calls[0][0];
expect(JSON.parse(arg.body).url).toEqual(
`${config.connections.myconn.HOST}:${config.connections.myconn.PORT}/webhook`,
const url = JSON.parse(arg.body).url;
expect(url).toEqual(
`${config.connections.myconn.HOST2}:${config.connections.myconn.PORT}/webhook`,
);

done();
},
);
});

it('relay does NOT swap body values found in BROKER_VAR_SUB if disabled', (done) => {
it('relay does NOT swaps body values found in BROKER_VAR_SUB if disable substition true', () => {
expect.hasAssertions();

const brokerToken = 'test-broker';

const config = {
disableBodyVarsSubstitution: true,
universalBrokerEnabled: true,
brokerType: 'client',
connections: {
myconn: {
identifier: brokerToken,
HOST: 'localhost',
HOST: '$HOST2',
PORT: '8001',
HOST2: 'localhost',
type: 'github',
},
},
disableBodyVarsSubstitution: true,
brokerServerUrl: 'http://localhost:8001',
brokerClientConfiguration: {
common: {
default: {},
required: {},
},
github: { default: {} },
},
};
loadBrokerConfig(config);

const options: LoadedClientOpts | LoadedServerOpts = {
filters: {
Expand All @@ -151,15 +180,14 @@ describe('body relay', () => {
},
config,
port: 8001,
loadedFilters: dummyLoadedFilters,
loadedFilters: loadAllFilters(dummyLoadedFilters, config),
};
const route = relay(options, dummyWebsocketHandler)(brokerToken);

const body = {
BROKER_VAR_SUB: ['url'],
url: '${HOST}:${PORT}/webhook',
};

route(
{
url: '/',
Expand All @@ -170,11 +198,10 @@ describe('body relay', () => {
headers: {},
},
() => {
expect(makeRequestToDownstream).toHaveBeenCalledTimes(1);
expect(makeLegacyRequest).toHaveBeenCalledTimes(1);
const arg = mockedFn.mock.calls[0][0];
expect(JSON.parse(arg.body).url).toEqual('${HOST}:${PORT}/webhook');

done();
const url = JSON.parse(arg.body).url;
expect(url).toEqual('${HOST}:${PORT}/webhook');
},
);
});
Expand Down

0 comments on commit 143bdc7

Please sign in to comment.