-
Notifications
You must be signed in to change notification settings - Fork 11
[APPS] Default apps plugin auth to OAuth without keys #427
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,13 +4,47 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { validateOptions } from '@dd/apps-plugin/validate'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const authEnvVars = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'DATADOG_API_KEY', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'DD_API_KEY', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'DATADOG_APP_KEY', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'DD_APP_KEY', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'DATADOG_APPS_AUTH_METHOD', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'DD_APPS_AUTH_METHOD', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ] as const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const savedAuthEnv = Object.fromEntries( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| authEnvVars.map((key) => [key, process.env[key]]), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) as Record<(typeof authEnvVars)[number], string | undefined>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const restoreAuthEnv = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const key of authEnvVars) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const value = savedAuthEnv[key]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value === undefined) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| delete process.env[key]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.env[key] = value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+29
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have this build-plugins/packages/tests/src/_jest/helpers/env.ts Lines 68 to 99 in ad82fc2
Currently, we only call it in beforeAll/afterAll but maybe we could extend/modify it to suit your use-case? (And also make it leverage OVERRIDE_VARIABLES instead of duplicating them) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('Apps Plugin - validateOptions', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const key of authEnvVars) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| delete process.env[key]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| afterEach(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| restoreAuthEnv(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('defaults', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('Should set defaults when nothing is provided', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('Should default to OAuth when API/App keys are not provided', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = validateOptions({}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result).toEqual({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| authOverrides: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method: 'apiKey', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method: 'oauth', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dryRun: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| include: [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -19,6 +53,35 @@ describe('Apps Plugin - validateOptions', () => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('Should default to API-key auth when API/App keys are configured', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = validateOptions({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auth: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| apiKey: 'api-key', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| appKey: 'app-key', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.authOverrides.method).toBe('apiKey'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('Should default to API-key auth when API/App keys are configured through env vars', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.env.DATADOG_API_KEY = 'api-key'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.env.DATADOG_APP_KEY = 'app-key'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = validateOptions({}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.authOverrides.method).toBe('apiKey'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('Should default to OAuth when API-key auth is incomplete', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = validateOptions({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auth: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| apiKey: 'api-key', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.authOverrides.method).toBe('oauth'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('Should set dryRun to false when DATADOG_APPS_UPLOAD_ASSETS is set', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.env.DATADOG_APPS_UPLOAD_ASSETS = '1'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -63,7 +126,7 @@ describe('Apps Plugin - validateOptions', () => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result).toEqual({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| authOverrides: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method: 'apiKey', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method: 'oauth', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dryRun: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| include: ['public/**/*', 'dist/**/*'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -83,14 +146,44 @@ describe('Apps Plugin - validateOptions', () => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.authOverrides.method).toBe('oauth'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('Should prefer configured OAuth over available API/App keys', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = validateOptions({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auth: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| apiKey: 'api-key', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| appKey: 'app-key', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| apps: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| enable: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| authOverrides: { method: 'oauth' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.authOverrides.method).toBe('oauth'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('Should enable API-key method when configured', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = validateOptions({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| apps: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| enable: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| authOverrides: { method: 'apiKey' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.authOverrides.method).toBe('apiKey'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('Should allow env vars to opt into OAuth', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.env.DATADOG_APPS_AUTH_METHOD = 'oauth'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = validateOptions({ apps: {} }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.authOverrides.method).toBe('oauth'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| delete process.env.DATADOG_APPS_AUTH_METHOD; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = validateOptions({ apps: {} }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.authOverrides.method).toBe('oauth'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('Should allow env vars to opt into API-key auth', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.env.DATADOG_APPS_AUTH_METHOD = 'apiKey'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = validateOptions({ apps: {} }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.authOverrides.method).toBe('apiKey'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if OAuth is loaded when nothing is configured, then I think the default is
oauthnow.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be when
DD_API_KEYandDD_APP_KEYare set, the default isapiKey, otherwise the default isoauth?