Skip to content

Commit

Permalink
use two services to test propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
iunanua committed Jun 18, 2024
1 parent b149c91 commit 70b84e5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 55 deletions.
96 changes: 45 additions & 51 deletions integration-tests/standalone-asm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
} = require('./helpers')

describe('Standalone ASM', () => {
let sandbox, cwd, startupTestFile, agent, proc, env, port
let sandbox, cwd, startupTestFile, agent, proc, env
before(async () => {
sandbox = await createSandbox(['express'], true)
cwd = sandbox.folder
Expand All @@ -35,8 +35,6 @@ describe('Standalone ASM', () => {
const execArgv = []

proc = await spawnProc(startupTestFile, { cwd, env, execArgv })

port = parseInt(proc.url.substring(proc.url.lastIndexOf(':') + 1), 10)
})

afterEach(async () => {
Expand Down Expand Up @@ -169,86 +167,82 @@ describe('Standalone ASM', () => {
})

context('propagation', () => {
const traceId = '12312312'
const parentId = '42424242'
let proc2
let port2

beforeEach(async () => {
const execArgv = []

proc2 = await spawnProc(startupTestFile, { cwd, env, execArgv })

port2 = parseInt(proc2.url.substring(proc2.url.lastIndexOf(':') + 1), 10)
})

afterEach(async () => {
proc2.kill()
})

// proc/drop-and-call-sdk:
// after setting a manual.drop calls to downstream proc2/sdk which triggers an appsec event
it('should keep trace even if parent prio is -1', async () => {
await doRequests(proc)
await doRequests(proc2)

const options = {
url: {
host: 'localhost',
port,
path: '/sdk',
headers: {
'x-datadog-trace-id': traceId,
'x-datadog-parent-id': parentId,
'x-datadog-sampling-priority': '-1'
}
}
}
return curlAndAssertMessage(agent, options, ({ headers, payload }) => {
const url = `${proc.url}/propagation-after-drop-and-call-sdk?port=${port2}`
return curlAndAssertMessage(agent, url, ({ headers, payload }) => {
assert.propertyVal(headers, 'datadog-client-computed-stats', 'yes')
assert.isArray(payload)
assert.strictEqual(payload.length, 4)

const expressReq4 = payload[3][0]
assertKeep(expressReq4)
})
const innerReq = payload.find(p => p[0].resource === 'GET /sdk')
if (innerReq) {
assertKeep(innerReq[0])
}
}, undefined, 2)
})

// proc/propagation-with-event triggers an appsec ev and calls downstream proc2/down with no event
it('should keep if parent trace is (prio:2, _dd.p.appsec:1) but there is no ev in the local trace', async () => {
await doRequests(proc)

const options = {
url: {
host: 'localhost',
port,
path: '/',
headers: {
'x-datadog-trace-id': traceId,
'x-datadog-parent-id': parentId,
'x-datadog-sampling-priority': '2',
'x-datadog-tags': '_dd.p.appsec=1,_dd.p.dm=-5'
}
}
}
return curlAndAssertMessage(agent, options, ({ headers, payload }) => {
const url = `${proc.url}/propagation-without-event?port=${port2}`
return curlAndAssertMessage(agent, url, ({ headers, payload }) => {
assert.propertyVal(headers, 'datadog-client-computed-stats', 'yes')
assert.isArray(payload)
assert.strictEqual(payload.length, 4)

const expressReq4 = payload[3][0]
assertKeep(expressReq4, false)
})
const innerReq = payload.find(p => p[0].resource === 'GET /down')
if (innerReq) {
assertKeep(innerReq[0])
}
}, undefined, 2)
})

it('should remove parent trace data if there is no ev in the local trace', async () => {
await doRequests(proc)

const url = `${proc.url}/propagation-without-event`
const url = `${proc.url}/propagation-without-event?port=${port2}`
return curlAndAssertMessage(agent, url, ({ headers, payload }) => {
assert.propertyVal(headers, 'datadog-client-computed-stats', 'yes')
assert.isArray(payload)
assert.strictEqual(payload.length, 5)

const downReq = payload[3][0]
assert.notProperty(downReq.meta, '_dd.p.other')
})
const innerReq = payload.find(p => p[0].resource === 'GET /down')
if (innerReq) {
assert.notProperty(innerReq[0].meta, '_dd.p.other')
}
}, undefined, 2)
})

it('should not remove parent trace data if there is ev in the local trace', async () => {
await doRequests(proc)

const url = `${proc.url}/propagation-with-event`
const url = `${proc.url}/propagation-with-event?port=${port2}`
return curlAndAssertMessage(agent, url, ({ headers, payload }) => {
assert.propertyVal(headers, 'datadog-client-computed-stats', 'yes')
assert.isArray(payload)
assert.strictEqual(payload.length, 5)

const downReq = payload[3][0]
assert.property(downReq.meta, '_dd.p.other')
})
const innerReq = payload.find(p => p[0].resource === 'GET /down')
if (innerReq) {
assert.property(innerReq[0].meta, '_dd.p.other')
}
}, undefined, 2)
})
})
})
Expand Down
23 changes: 19 additions & 4 deletions integration-tests/standalone-asm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ if (process.env.AGENT_URL) {
}

if (process.env.DD_EXPERIMENTAL_APPSEC_STANDALONE_ENABLED) {
options.experimental.appsec.standalone.enabled = process.env.DD_EXPERIMENTAL_APPSEC_STANDALONE_ENABLED === 'true'
options.experimental.appsec.standalone.enabled = process.env.DD_EXPERIMENTAL_APPSEC_STANDALONE_ENABLED === 'true' ||
process.env.DD_EXPERIMENTAL_APPSEC_STANDALONE_ENABLED === '1'
}

const tracer = require('dd-trace')
Expand Down Expand Up @@ -59,7 +60,7 @@ app.get('/vulnerableReadFile', (req, res) => {
app.get('/propagation-with-event', async (req, res) => {
tracer.appsec.trackCustomEvent('custom-event')

const port = server.address().port
const port = req.query.port || server.address().port
const url = `http://localhost:${port}/down`

const resFetch = await fetch(url, {
Expand All @@ -73,7 +74,7 @@ app.get('/propagation-with-event', async (req, res) => {
})

app.get('/propagation-without-event', async (req, res) => {
const port = server.address().port
const port = req.query.port || server.address().port
const url = `http://localhost:${port}/down`

const resFetch = await fetch(url, {
Expand All @@ -90,7 +91,21 @@ app.get('/down', async (req, res) => {
res.status(200).send('down')
})

app.get('/propagation-after-drop-and-call-sdk', async (req, res) => {
const span = tracer.scope().active()
span?.setTag('manual.drop', 'true')

const port = req.query.port

const url = `http://localhost:${port}/sdk`

const resFetch = await fetch(url)
const sdkRes = await resFetch.text()

res.status(200).send(`drop-and-call-sdk ${sdkRes}`)
})

const server = http.createServer(app).listen(0, () => {
const port = server.address().port
process.send({ port })
process.send?.({ port })
})

0 comments on commit 70b84e5

Please sign in to comment.