-
Notifications
You must be signed in to change notification settings - Fork 407
feat(evp_proxy): add direct intake route #9739
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
2 commits
Select commit
Hold shift + click to select a range
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,64 @@ | ||
| 'use strict' | ||
|
|
||
| const { format } = require('node:url') | ||
|
|
||
| const { HttpsProxyAgent } = require('https-proxy-agent') | ||
| const { getProxyForUrl } = require('proxy-from-env') | ||
| const log = require('../log') | ||
|
|
||
| /** | ||
| * @typedef {object} DirectEVPRoute | ||
| * @property {URL} url - Direct intake URL | ||
| * @property {string} basePath - Direct intake base path | ||
| * @property {object} headers - Direct intake authentication headers | ||
| * @property {import('node:https').Agent} [agent] - Optional HTTPS proxy agent | ||
| */ | ||
|
|
||
| /** | ||
| * Creates an authenticated direct EVP intake route. | ||
| * | ||
| * This helper does not perform local receiver discovery. | ||
| * | ||
| * @param {import('../config/config-base')} config - Tracer configuration | ||
| * @param {string} intake - EVP intake subdomain | ||
| * @returns {DirectEVPRoute|undefined} Direct route when credentials and site are available | ||
| */ | ||
| function createDirectEVPRoute (config, intake) { | ||
| const apiKey = config.DD_API_KEY | ||
| if (!apiKey || !config.site) return | ||
|
|
||
| try { | ||
| const hostname = `${intake}.${config.site}`.toLowerCase() | ||
| const url = new URL(format({ | ||
| protocol: 'https:', | ||
| hostname, | ||
| })) | ||
| if ( | ||
| url.hostname !== hostname || | ||
| url.username || | ||
| url.password || | ||
| url.port || | ||
| url.pathname !== '/' || | ||
| url.search || | ||
| url.hash | ||
| ) { | ||
| throw new Error('Invalid direct EVP intake URL') | ||
| } | ||
|
|
||
| const proxyUrl = getProxyForUrl(url.href) | ||
| const agent = proxyUrl ? new HttpsProxyAgent(proxyUrl) : undefined | ||
|
leoromanovsky marked this conversation as resolved.
|
||
|
|
||
| return { | ||
| url, | ||
| basePath: '', | ||
| headers: { | ||
| 'DD-API-KEY': apiKey, | ||
| }, | ||
|
leoromanovsky marked this conversation as resolved.
|
||
| ...(agent && { agent }), | ||
| } | ||
| } catch (error) { | ||
| log.debug('Unable to configure direct EVP intake: %s', error.message) | ||
| } | ||
| } | ||
|
|
||
| module.exports = { createDirectEVPRoute } | ||
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,120 @@ | ||
| 'use strict' | ||
|
|
||
| const assert = require('node:assert/strict') | ||
|
|
||
| const { describe, it, beforeEach } = require('mocha') | ||
| const proxyquire = require('proxyquire').noPreserveCache() | ||
| const sinon = require('sinon') | ||
|
|
||
| describe('direct EVP route', () => { | ||
| let createDirectEVPRoute | ||
| let getProxyForUrl | ||
| let HttpsProxyAgent | ||
| let log | ||
|
|
||
| beforeEach(() => { | ||
| getProxyForUrl = sinon.stub().returns('') | ||
| HttpsProxyAgent = sinon.stub().callsFake(proxyUrl => ({ proxyUrl })) | ||
| log = { debug: sinon.spy() } | ||
|
|
||
| ;({ createDirectEVPRoute } = proxyquire('../../src/evp_proxy/direct', { | ||
| 'https-proxy-agent': { HttpsProxyAgent }, | ||
| 'proxy-from-env': { getProxyForUrl }, | ||
| '../log': log, | ||
| })) | ||
| }) | ||
|
|
||
| it('creates an authenticated route from API key and site', () => { | ||
| const route = createDirectEVPRoute({ | ||
| DD_API_KEY: 'test-api-key', | ||
| site: 'datadoghq.com', | ||
| }, 'event-platform-intake') | ||
|
|
||
| assert.deepStrictEqual(route, { | ||
| url: new URL('https://event-platform-intake.datadoghq.com'), | ||
| basePath: '', | ||
| headers: { | ||
| 'DD-API-KEY': 'test-api-key', | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('normalizes site casing', () => { | ||
| const route = createDirectEVPRoute({ | ||
| DD_API_KEY: 'test-api-key', | ||
| site: 'DATADOGHQ.EU', | ||
| }, 'event-platform-intake') | ||
|
|
||
| assert.deepStrictEqual(route, { | ||
| url: new URL('https://event-platform-intake.datadoghq.eu'), | ||
| basePath: '', | ||
| headers: { | ||
| 'DD-API-KEY': 'test-api-key', | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('uses the standard HTTPS proxy for direct intake', () => { | ||
| const proxyUrl = 'http://proxy:8202' | ||
| getProxyForUrl.returns(proxyUrl) | ||
|
|
||
| const route = createDirectEVPRoute({ | ||
| DD_API_KEY: 'test-api-key', | ||
| site: 'datadoghq.com', | ||
| }, 'event-platform-intake') | ||
|
|
||
| assert.deepStrictEqual(route.agent, { proxyUrl }) | ||
| sinon.assert.calledOnceWithExactly( | ||
| getProxyForUrl, | ||
| 'https://event-platform-intake.datadoghq.com/' | ||
| ) | ||
| sinon.assert.calledOnceWithExactly(HttpsProxyAgent, proxyUrl) | ||
| }) | ||
|
|
||
| it('does not create a route without an API key', () => { | ||
| assert.strictEqual(createDirectEVPRoute({ | ||
| site: 'datadoghq.com', | ||
| }, 'event-platform-intake'), undefined) | ||
| }) | ||
|
|
||
| it('does not create a route without a site', () => { | ||
| assert.strictEqual(createDirectEVPRoute({ | ||
| DD_API_KEY: 'test-api-key', | ||
| }, 'event-platform-intake'), undefined) | ||
| }) | ||
|
|
||
| it('does not create a route for an invalid site', () => { | ||
| assert.strictEqual(createDirectEVPRoute({ | ||
| DD_API_KEY: 'test-api-key', | ||
| site: 'not a host', | ||
| }, 'event-platform-intake'), undefined) | ||
|
|
||
| sinon.assert.calledOnceWithExactly( | ||
| log.debug, | ||
| 'Unable to configure direct EVP intake: %s', | ||
| sinon.match.string | ||
| ) | ||
| }) | ||
|
|
||
| for (const site of [ | ||
| 'datadoghq.com@evil.example', | ||
| 'datadoghq.com:password@evil.example', | ||
| 'datadoghq.com:443', | ||
| 'datadoghq.com/path', | ||
| 'datadoghq.com?query', | ||
| 'datadoghq.com#fragment', | ||
| ]) { | ||
| it(`does not create a route for a site with URL components: ${site}`, () => { | ||
| assert.strictEqual(createDirectEVPRoute({ | ||
| DD_API_KEY: 'test-api-key', | ||
| site, | ||
| }, 'event-platform-intake'), undefined) | ||
|
|
||
| sinon.assert.calledOnceWithExactly( | ||
| log.debug, | ||
| 'Unable to configure direct EVP intake: %s', | ||
| sinon.match.string | ||
| ) | ||
| }) | ||
| } | ||
| }) |
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.
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.