-
Notifications
You must be signed in to change notification settings - Fork 344
Endpoints discovery for fastify #6258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ca5966e
Endpoints discovery for fastify
IlyasShabi c3ef6d0
fix config tests
IlyasShabi 63463fc
add is_first logic
IlyasShabi 981b83b
fix test
IlyasShabi ac92b3f
remove unecessary checks
IlyasShabi 31a4395
fix test
IlyasShabi eb2ebe2
move endpoint config check to the top
IlyasShabi 9529a03
Expand integration test coverage
CarlesDD 8849743
Fix all methods endpoint collection
CarlesDD a4d84fc
Fix config test
CarlesDD 4d1537a
Fix typo
CarlesDD f60a0c4
revamp endpoints.spec.js
simon-id dd3d2f1
lint
simon-id 4cc823a
fix
simon-id 37a5ee5
try to fix test
simon-id 02366aa
it was a misspelling
simon-id f17a15e
Check endpoint detection count
CarlesDD c87d105
Test wildcard route without slash
CarlesDD 4448b47
Fix lint
CarlesDD 63de9fa
Add test to check telemetry endpoints does not subscribe if it is not…
CarlesDD e6337ab
add delayed route test
simon-id 856df59
lint
simon-id 25c5f6a
remove redundant check
simon-id File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,148 @@ | ||
'use strict' | ||
|
||
const { createSandbox, FakeAgent, spawnProc } = require('../helpers') | ||
const path = require('path') | ||
|
||
describe('Endpoints collection', () => { | ||
let sandbox, cwd | ||
|
||
before(async function () { | ||
this.timeout(process.platform === 'win32' ? 90000 : 30000) | ||
|
||
sandbox = await createSandbox( | ||
['fastify'], | ||
false | ||
) | ||
|
||
cwd = sandbox.folder | ||
}) | ||
|
||
after(async function () { | ||
this.timeout(60000) | ||
await sandbox.remove() | ||
}) | ||
|
||
function getExpectedEndpoints (framework) { | ||
const expectedEndpoints = [ | ||
// Basic routes | ||
{ method: 'GET', path: '/users' }, | ||
{ method: 'HEAD', path: '/users' }, | ||
{ method: 'POST', path: '/users/' }, | ||
{ method: 'PUT', path: '/users/:id' }, | ||
{ method: 'DELETE', path: '/users/:id' }, | ||
{ method: 'PATCH', path: '/users/:id/:name' }, | ||
{ method: 'OPTIONS', path: '/users/:id?' }, | ||
|
||
// Route with regex | ||
{ method: 'DELETE', path: '/regex/:hour(^\\d{2})h:minute(^\\d{2})m' }, | ||
|
||
// Additional methods | ||
{ method: 'TRACE', path: '/trace-test' }, | ||
{ method: 'HEAD', path: '/head-test' }, | ||
|
||
// Custom method | ||
{ method: 'MKCOL', path: '/example/near/:lat-:lng/radius/:r' }, | ||
|
||
// Using app.route() | ||
{ method: 'POST', path: '/multi-method' }, | ||
{ method: 'PUT', path: '/multi-method' }, | ||
{ method: 'PATCH', path: '/multi-method' }, | ||
|
||
// All supported methods route | ||
{ method: 'GET', path: '/all-methods' }, | ||
{ method: 'HEAD', path: '/all-methods' }, | ||
{ method: 'TRACE', path: '/all-methods' }, | ||
{ method: 'DELETE', path: '/all-methods' }, | ||
{ method: 'OPTIONS', path: '/all-methods' }, | ||
{ method: 'PATCH', path: '/all-methods' }, | ||
{ method: 'PUT', path: '/all-methods' }, | ||
{ method: 'POST', path: '/all-methods' }, | ||
{ method: 'MKCOL', path: '/all-methods' }, // Added with addHttpMethod | ||
|
||
// Nested routes with Router | ||
{ method: 'PUT', path: '/v1/nested/:id' }, | ||
|
||
// Deeply nested routes | ||
{ method: 'GET', path: '/api/nested' }, | ||
{ method: 'HEAD', path: '/api/nested' }, | ||
{ method: 'GET', path: '/api/sub/deep' }, | ||
{ method: 'HEAD', path: '/api/sub/deep' }, | ||
{ method: 'POST', path: '/api/sub/deep/:id' }, | ||
|
||
// Wildcard routes | ||
{ method: 'GET', path: '/wildcard/*' }, | ||
{ method: 'HEAD', path: '/wildcard/*' }, | ||
{ method: 'GET', path: '*' }, | ||
{ method: 'HEAD', path: '*' }, | ||
|
||
{ method: 'GET', path: '/later' }, | ||
{ method: 'HEAD', path: '/later' }, | ||
] | ||
|
||
return expectedEndpoints | ||
} | ||
|
||
async function runEndpointTest (framework) { | ||
let agent, proc | ||
const appFile = path.join(cwd, 'appsec', 'endpoints-collection', `${framework}.js`) | ||
|
||
try { | ||
agent = await new FakeAgent().start() | ||
|
||
const expectedEndpoints = getExpectedEndpoints(framework) | ||
const endpointsFound = [] | ||
const isFirstFlags = [] | ||
|
||
const telemetryPromise = agent.assertTelemetryReceived(({ payload }) => { | ||
isFirstFlags.push(Boolean(payload.payload.is_first)) | ||
|
||
if (payload.payload.endpoints) { | ||
payload.payload.endpoints.forEach(endpoint => { | ||
endpointsFound.push({ | ||
method: endpoint.method, | ||
path: endpoint.path, | ||
type: endpoint.type, | ||
operation_name: endpoint.operation_name, | ||
resource_name: endpoint.resource_name | ||
}) | ||
}) | ||
} | ||
}, 'app-endpoints', 5_000, 4) | ||
|
||
proc = await spawnProc(appFile, { | ||
cwd, | ||
env: { | ||
DD_TRACE_AGENT_PORT: agent.port, | ||
DD_TELEMETRY_HEARTBEAT_INTERVAL: 1, | ||
DD_API_SECURITY_ENDPOINT_COLLECTION_MESSAGE_LIMIT: '10' | ||
} | ||
}) | ||
|
||
await telemetryPromise | ||
|
||
const trueCount = isFirstFlags.filter(v => v === true).length | ||
expect(trueCount).to.equal(1) | ||
|
||
// Check that all expected endpoints were found | ||
expectedEndpoints.forEach(expected => { | ||
const found = endpointsFound.find(e => | ||
e.method === expected.method && e.path === expected.path | ||
) | ||
expect(found).to.exist | ||
expect(found.type).to.equal('REST') | ||
expect(found.operation_name).to.equal('http.request') | ||
expect(found.resource_name).to.equal(`${expected.method} ${expected.path}`) | ||
}) | ||
|
||
// check that no additional endpoints were found | ||
expect(endpointsFound.length).to.equal(expectedEndpoints.length) | ||
} finally { | ||
proc?.kill() | ||
await agent?.stop() | ||
} | ||
} | ||
|
||
it('should send fastify endpoints via telemetry', async () => { | ||
await runEndpointTest('fastify') | ||
}) | ||
}) |
This file contains hidden or 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,67 @@ | ||
'use strict' | ||
|
||
const tracer = require('dd-trace') | ||
tracer.init({ | ||
flushInterval: 0 | ||
}) | ||
|
||
const fastify = require('fastify') | ||
const app = fastify() | ||
|
||
// Basic routes | ||
app.get('/users', async (_, reply) => reply.send('ok')) | ||
app.post('/users/', async (_, reply) => reply.send('ok')) | ||
app.put('/users/:id', async (_, reply) => reply.send('ok')) | ||
app.delete('/users/:id', async (_, reply) => reply.send('ok')) | ||
CarlesDD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
app.patch('/users/:id/:name', async (_, reply) => reply.send('ok')) | ||
app.options('/users/:id?', async (_, reply) => reply.send('ok')) | ||
|
||
// Route with regex | ||
app.delete('/regex/:hour(^\\d{2})h:minute(^\\d{2})m', async (_, reply) => reply.send('ok')) | ||
|
||
// Additional methods | ||
app.trace('/trace-test', async (_, reply) => reply.send('ok')) | ||
app.head('/head-test', async (_, reply) => reply.send('ok')) | ||
|
||
CarlesDD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Custom method | ||
app.addHttpMethod('MKCOL', { hasBody: true }) | ||
app.mkcol('/example/near/:lat-:lng/radius/:r', async (_, reply) => reply.send('ok')) | ||
|
||
// Using app.route() | ||
app.route({ | ||
method: ['POST', 'PUT', 'PATCH'], | ||
url: '/multi-method', | ||
handler: async (_, reply) => reply.send('ok') | ||
}) | ||
|
||
// All supported methods route | ||
app.all('/all-methods', async (_, reply) => reply.send('ok')) | ||
|
||
// Nested routes with Router | ||
app.register(async function (router) { | ||
router.put('/nested/:id', async (_, reply) => reply.send('ok')) | ||
}, { prefix: '/v1' }) | ||
|
||
// Deeply nested routes | ||
app.register(async function (router) { | ||
CarlesDD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
router.get('/nested', async (_, reply) => reply.send('ok')) | ||
router.register(async function (subRouter) { | ||
subRouter.get('/deep', async (_, reply) => reply.send('ok')) | ||
subRouter.post('/deep/:id', async (_, reply) => reply.send('ok')) | ||
}, { prefix: '/sub' }) | ||
}, { prefix: '/api' }) | ||
|
||
// Wildcard routes | ||
app.get('/wildcard/*', async (_, reply) => reply.send('ok')) | ||
app.get('*', async (_, reply) => reply.send('ok')) | ||
|
||
const start = async () => { | ||
await app.listen({ port: 0, host: '127.0.0.1' }) | ||
const port = app.server.address().port | ||
process.send({ port }) | ||
} | ||
|
||
setTimeout(() => { | ||
app.get('/later', async (_, reply) => reply.send('ok')) | ||
start() | ||
}, 2e3) |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.