diff --git a/.changeset/ingestion-key-enforce-before-team.md b/.changeset/ingestion-key-enforce-before-team.md new file mode 100644 index 0000000000..2204816c21 --- /dev/null +++ b/.changeset/ingestion-key-enforce-before-team.md @@ -0,0 +1,8 @@ +--- +'@hyperdx/api': patch +--- + +fix: require `INGESTION_API_KEY` on the app image before any team exists + +With `ENABLE_INGESTION_API_KEY` set and no team created yet, the collector's OTLP +receiver accepted data without authentication. It now requires the preset key. diff --git a/packages/api/src/opamp/controllers/__tests__/opampController.test.ts b/packages/api/src/opamp/controllers/__tests__/opampController.test.ts index bedfda5dc1..dadaaf6a3c 100644 --- a/packages/api/src/opamp/controllers/__tests__/opampController.test.ts +++ b/packages/api/src/opamp/controllers/__tests__/opampController.test.ts @@ -199,6 +199,24 @@ describe('opampController', () => { expect(tokens).toEqual(bearerTokenVariants(['k1', 'ingest-key'])); }); + it('enforces INGESTION_API_KEY before any team exists when ENABLE_INGESTION_API_KEY is set', () => { + configState.ENABLE_INGESTION_API_KEY = true; + configState.INGESTION_API_KEY = 'ingest-key'; + + const cfg = buildOtelCollectorConfig([]); + + expect(cfg.extensions['bearertokenauth/hyperdx'].tokens).toEqual( + bearerTokenVariants(['ingest-key']), + ); + expect(cfg.receivers['otlp/hyperdx']?.protocols.grpc.auth).toEqual({ + authenticator: 'bearertokenauth/hyperdx', + }); + expect(cfg.receivers['otlp/hyperdx']?.protocols.http.auth).toEqual({ + authenticator: 'bearertokenauth/hyperdx', + }); + expect(cfg.service.extensions).toContain('bearertokenauth/hyperdx'); + }); + it('adds nothing when ENABLE_INGESTION_API_KEY is set but INGESTION_API_KEY is empty', () => { configState.ENABLE_INGESTION_API_KEY = true; @@ -249,6 +267,24 @@ describe('opampController', () => { expect(cfg.service.pipelines['logs/in'].receivers).toContain('datadog'); }); + it('requires INGESTION_API_KEY on the datadog receiver before any team exists when ENABLE_INGESTION_API_KEY is set', () => { + configState.ENABLE_DATADOG_RECEIVER = true; + configState.ENABLE_INGESTION_API_KEY = true; + configState.INGESTION_API_KEY = 'ingest-key'; + + const cfg = buildOtelCollectorConfig([]); + + expect(cfg.extensions['bearertokenauth/datadog']).toEqual({ + header: 'DD-API-KEY', + scheme: '', + tokens: ['ingest-key'], + }); + expect(cfg.receivers.datadog?.auth).toEqual({ + authenticator: 'bearertokenauth/datadog', + }); + expect(cfg.service.extensions).toContain('bearertokenauth/datadog'); + }); + it('leaves the datadog receiver unauthenticated when no team API keys exist', () => { configState.ENABLE_DATADOG_RECEIVER = true; diff --git a/packages/api/src/opamp/controllers/opampController.ts b/packages/api/src/opamp/controllers/opampController.ts index 24d6d87f48..6181c82b96 100644 --- a/packages/api/src/opamp/controllers/opampController.ts +++ b/packages/api/src/opamp/controllers/opampController.ts @@ -195,8 +195,11 @@ export const buildOtelCollectorConfig = ( } } + // Before any team exists, an opted-in INGESTION_API_KEY is the only key, + // so enforce it rather than open the receiver without auth. const collectorAuthenticationEnforced = - teams[0]?.collectorAuthenticationEnforced; + teams[0]?.collectorAuthenticationEnforced ?? + (teams.length === 0 && config.ENABLE_INGESTION_API_KEY); const otelCollectorConfig: CollectorConfig = { extensions: {},