-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #565 from snyk/feat/add-universal-broker-midlwr-lo…
…g-warn feat: add broker type validtion middlwr
- Loading branch information
Showing
6 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"BROKER_SERVER_UNIVERSAL_CONFIG_ENABLED": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import * as logger from '../log'; | ||
import * as config from '../config'; | ||
export const validateBrokerTypeMiddleware = ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
const localConfig = config as unknown as Record<string, string>; | ||
if ( | ||
localConfig.brokerServerUniversalConfigEnabled && | ||
!req?.headers['x-snyk-broker-type'] | ||
) { | ||
const logContext = { url: req.url, headers: req.headers }; | ||
logger.warn( | ||
{ logContext }, | ||
'Error: Request does not contain the x-snyk-broker-type header', | ||
); | ||
// Will eventually return an error when all services will have this enabled | ||
// return res.status(400).json({ error: 'Missing x-broker-type header' }); | ||
} | ||
next(); // Passes the request to the next middleware | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
process.env.SNYK_BROKER_SERVER_UNIVERSAL_CONFIG_ENABLED = 'true'; | ||
import * as path from 'path'; | ||
import { axiosClient } from '../setup/axios-client'; | ||
import { | ||
BrokerClient, | ||
closeBrokerClient, | ||
createBrokerClient, | ||
} from '../setup/broker-client'; | ||
import { | ||
BrokerServer, | ||
closeBrokerServer, | ||
createBrokerServer, | ||
waitForBrokerClientConnection, | ||
} from '../setup/broker-server'; | ||
import { TestWebServer, createTestWebServer } from '../setup/test-web-server'; | ||
|
||
const fixtures = path.resolve(__dirname, '..', 'fixtures'); | ||
const serverAccept = path.join(fixtures, 'server', 'filters.json'); | ||
const clientAccept = path.join(fixtures, 'client', 'filters.json'); | ||
|
||
describe('proxy requests originating from behind the broker server', () => { | ||
let tws: TestWebServer; | ||
let bs: BrokerServer; | ||
let bc: BrokerClient; | ||
let brokerToken: string; | ||
|
||
const spyLogWarn = jest | ||
.spyOn(require('bunyan').prototype, 'warn') | ||
.mockImplementation((value) => { | ||
return value; | ||
}); | ||
|
||
beforeAll(async () => { | ||
tws = await createTestWebServer(); | ||
|
||
bs = await createBrokerServer({ filters: serverAccept, port: 8100 }); | ||
|
||
bc = await createBrokerClient({ | ||
brokerServerUrl: `http://localhost:${bs.port}`, | ||
brokerToken: 'broker-token-12345', | ||
filters: clientAccept, | ||
type: 'client', | ||
}); | ||
({ brokerToken } = await waitForBrokerClientConnection(bs)); | ||
}); | ||
|
||
afterEach(async () => { | ||
spyLogWarn.mockReset(); | ||
}); | ||
afterAll(async () => { | ||
spyLogWarn.mockReset(); | ||
await tws.server.close(); | ||
await closeBrokerClient(bc); | ||
await closeBrokerServer(bs); | ||
}); | ||
|
||
it('successfully broker GET', async () => { | ||
const response = await axiosClient.get( | ||
`http://localhost:${bs.port}/broker/${brokerToken}/echo-param/xyz`, | ||
); | ||
|
||
expect(response.status).toEqual(200); | ||
expect(response.data).toEqual('xyz'); | ||
}); | ||
|
||
it('successfully warn logs requests without x-snyk-broker-type header', async () => { | ||
const response = await axiosClient.get( | ||
`http://localhost:${bs.port}/broker/${brokerToken}/echo-param/xyz`, | ||
); | ||
|
||
expect(response.status).toEqual(200); | ||
expect(response.data).toEqual('xyz'); | ||
|
||
expect(spyLogWarn).toHaveBeenCalledTimes(1); | ||
expect(spyLogWarn).toHaveBeenCalledWith( | ||
expect.any(Object), | ||
'Error: Request does not contain the x-snyk-broker-type header', | ||
); | ||
}); | ||
}); |