|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('node:assert/strict') |
| 4 | +const { beforeEach, describe, it } = require('mocha') |
| 5 | +const proxyquire = require('proxyquire') |
| 6 | +const sinon = require('sinon') |
| 7 | + |
| 8 | +require('../setup/core') |
| 9 | + |
| 10 | +describe('OpenFeature configuration source', () => { |
| 11 | + let config |
| 12 | + let configurationSource |
| 13 | + let log |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + config = { |
| 17 | + DD_API_KEY: 'test-api-key', |
| 18 | + site: 'datadoghq.com', |
| 19 | + env: 'my env', |
| 20 | + experimental: { |
| 21 | + flaggingProvider: { |
| 22 | + configurationSource: 'agentless', |
| 23 | + agentlessBaseUrl: undefined, |
| 24 | + agentlessPollIntervalSeconds: 30, |
| 25 | + agentlessRequestTimeoutSeconds: 2, |
| 26 | + }, |
| 27 | + }, |
| 28 | + } |
| 29 | + log = { |
| 30 | + debug: sinon.spy(), |
| 31 | + error: sinon.spy(), |
| 32 | + warn: sinon.spy(), |
| 33 | + } |
| 34 | + configurationSource = proxyquire('../../src/openfeature/configuration_source', { |
| 35 | + '../log': log, |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | + for (const value of [undefined, null, '', ' ', ' AgEnTlEsS ']) { |
| 40 | + it(`normalizes ${JSON.stringify(value)} to the default agentless source`, () => { |
| 41 | + config.experimental.flaggingProvider.configurationSource = value |
| 42 | + |
| 43 | + assert.strictEqual(configurationSource.resolve(config).mode, 'agentless') |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + it('defaults to the Datadog UFC CDN endpoint and includes the environment', () => { |
| 48 | + config.DD_SITE = 'raw-env-key.invalid' |
| 49 | + const resolved = configurationSource.resolve(config) |
| 50 | + |
| 51 | + assert.strictEqual( |
| 52 | + resolved.endpoint.toString(), |
| 53 | + 'https://ufc-server.ff-cdn.datadoghq.com/api/v2/feature-flagging/config/rules-based/server?dd_env=my+env' |
| 54 | + ) |
| 55 | + assert.strictEqual(resolved.apiKey, 'test-api-key') |
| 56 | + assert.strictEqual(resolved.pollIntervalMs, 30_000) |
| 57 | + assert.strictEqual(resolved.requestTimeoutMs, 2000) |
| 58 | + }) |
| 59 | + |
| 60 | + it('derives the staging UFC CDN endpoint from DD_SITE', () => { |
| 61 | + config.site = 'datad0g.com' |
| 62 | + config.env = 'staging' |
| 63 | + |
| 64 | + assert.strictEqual( |
| 65 | + configurationSource.resolve(config).endpoint.toString(), |
| 66 | + 'https://ufc-server.ff-cdn.datad0g.com/api/v2/feature-flagging/config/rules-based/server?dd_env=staging' |
| 67 | + ) |
| 68 | + }) |
| 69 | + |
| 70 | + it('appends the standard path to a configured origin', () => { |
| 71 | + config.experimental.flaggingProvider.agentlessBaseUrl = 'http://127.0.0.1:8080/' |
| 72 | + |
| 73 | + const resolved = configurationSource.resolve(config) |
| 74 | + assert.strictEqual( |
| 75 | + resolved.endpoint.toString(), |
| 76 | + 'http://127.0.0.1:8080/api/v2/feature-flagging/config/rules-based/server' |
| 77 | + ) |
| 78 | + }) |
| 79 | + |
| 80 | + it('preserves an exact configured path and query', () => { |
| 81 | + config.experimental.flaggingProvider.agentlessBaseUrl = 'https://example.com/custom/ufc?tenant=one' |
| 82 | + |
| 83 | + assert.strictEqual( |
| 84 | + configurationSource.resolve(config).endpoint.toString(), |
| 85 | + 'https://example.com/custom/ufc?tenant=one' |
| 86 | + ) |
| 87 | + }) |
| 88 | + |
| 89 | + it('derives the managed GovCloud endpoint without hard-coding availability', () => { |
| 90 | + config.site = 'DDOG-GOV.COM' |
| 91 | + config.env = 'prod' |
| 92 | + |
| 93 | + const resolved = configurationSource.resolve(config) |
| 94 | + |
| 95 | + assert.strictEqual( |
| 96 | + resolved.endpoint.toString(), |
| 97 | + 'https://ufc-server.ff-cdn.ddog-gov.com/api/v2/feature-flagging/config/rules-based/server?dd_env=prod' |
| 98 | + ) |
| 99 | + sinon.assert.notCalled(log.warn) |
| 100 | + }) |
| 101 | + |
| 102 | + it('allows an operator-owned agentless endpoint on GovCloud', () => { |
| 103 | + config.site = 'ddog-gov.com' |
| 104 | + config.experimental.flaggingProvider.agentlessBaseUrl = 'https://flags.example.test/custom/ufc?tenant=test' |
| 105 | + |
| 106 | + const resolved = configurationSource.resolve(config) |
| 107 | + |
| 108 | + assert.strictEqual(resolved.endpoint.toString(), 'https://flags.example.test/custom/ufc?tenant=test') |
| 109 | + sinon.assert.notCalled(log.warn) |
| 110 | + }) |
| 111 | + |
| 112 | + it('rejects non-HTTP endpoints', () => { |
| 113 | + config.experimental.flaggingProvider.agentlessBaseUrl = 'file:///tmp/ufc.json' |
| 114 | + |
| 115 | + assert.throws( |
| 116 | + () => configurationSource.resolve(config), |
| 117 | + /must use HTTP or HTTPS/ |
| 118 | + ) |
| 119 | + }) |
| 120 | + |
| 121 | + it('recognizes explicit Remote Config without resolving agentless settings', () => { |
| 122 | + config.experimental.flaggingProvider.configurationSource = ' REMOTE_CONFIG ' |
| 123 | + delete config.site |
| 124 | + |
| 125 | + assert.deepStrictEqual(configurationSource.resolve(config), { mode: 'remote_config' }) |
| 126 | + assert.strictEqual(configurationSource.isRemoteConfig(config), true) |
| 127 | + }) |
| 128 | + |
| 129 | + for (const value of ['offline', 'other']) { |
| 130 | + it(`fails closed for the unsupported ${value} source`, () => { |
| 131 | + config.experimental.flaggingProvider.configurationSource = value |
| 132 | + |
| 133 | + assert.throws(() => configurationSource.resolve(config), /Unsupported Feature Flagging configuration source/) |
| 134 | + assert.strictEqual(configurationSource.isRemoteConfig(config), false) |
| 135 | + sinon.assert.calledOnce(log.error) |
| 136 | + }) |
| 137 | + } |
| 138 | + |
| 139 | + it('falls back to positive timing defaults with warnings', () => { |
| 140 | + config.experimental.flaggingProvider.agentlessPollIntervalSeconds = 0 |
| 141 | + config.experimental.flaggingProvider.agentlessRequestTimeoutSeconds = -1 |
| 142 | + |
| 143 | + assert.strictEqual(configurationSource.isRemoteConfig(config), false) |
| 144 | + sinon.assert.notCalled(log.warn) |
| 145 | + |
| 146 | + const resolved = configurationSource.resolve(config) |
| 147 | + |
| 148 | + assert.strictEqual(resolved.pollIntervalMs, 30_000) |
| 149 | + assert.strictEqual(resolved.requestTimeoutMs, 2000) |
| 150 | + sinon.assert.calledTwice(log.warn) |
| 151 | + }) |
| 152 | + |
| 153 | + it('caps the polling interval at one hour', () => { |
| 154 | + config.experimental.flaggingProvider.agentlessPollIntervalSeconds = 4 * 60 * 60 |
| 155 | + |
| 156 | + const resolved = configurationSource.resolve(config) |
| 157 | + |
| 158 | + assert.strictEqual(resolved.pollIntervalMs, 60 * 60 * 1000) |
| 159 | + sinon.assert.calledOnceWithExactly( |
| 160 | + log.warn, |
| 161 | + 'Feature Flagging agentless %s %s exceeds the maximum of %ss; using %ss', |
| 162 | + 'poll interval', |
| 163 | + 4 * 60 * 60, |
| 164 | + 60 * 60, |
| 165 | + 60 * 60 |
| 166 | + ) |
| 167 | + }) |
| 168 | +}) |
0 commit comments