Skip to content
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

[lambda][flare] Build endpoint URL from environment variables #967

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/commands/lambda/__tests__/__snapshots__/flare.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ exports[`lambda flare getAllLogs throws an error when unable to get log events 1

exports[`lambda flare getAllLogs throws an error when unable to get log streams 1`] = `[Error: Unable to get log streams: Error getting log streams]`;

exports[`lambda flare getEndpointUrl should not throw error if the site is invalid and DD_CI_BYPASS_SITE_VALIDATION is set 1`] = `"https://datad0ge.com/api/ui/support/serverless/flare"`;

exports[`lambda flare getEndpointUrl should return correct endpoint url 1`] = `"https://datadoghq.com/api/ui/support/serverless/flare"`;

exports[`lambda flare getEndpointUrl should throw error if the site is invalid 1`] = `"Invalid site: datad0ge.com. Must be one of: datadoghq.com, datadoghq.eu, us3.datadoghq.com, us5.datadoghq.com, ap1.datadoghq.com, ddog-gov.com"`;

exports[`lambda flare getEndpointUrl should use DEFAULT_DD_SITE if CI_SITE_ENV_VAR and SITE_ENV_VAR are not set 1`] = `"https://datadoghq.com/api/ui/support/serverless/flare"`;

exports[`lambda flare getEndpointUrl should use SITE_ENV_VAR if CI_SITE_ENV_VAR is not set 1`] = `"https://us3.datadoghq.com/api/ui/support/serverless/flare"`;

exports[`lambda flare getTags should return the tags when they exist 1`] = `
Object {
"Key1": "Value1",
Expand Down
54 changes: 53 additions & 1 deletion src/commands/lambda/__tests__/flare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ import FormData from 'form-data'
import inquirer from 'inquirer'
import JSZip from 'jszip'

import {API_KEY_ENV_VAR, AWS_DEFAULT_REGION_ENV_VAR, CI_API_KEY_ENV_VAR} from '../constants'
import {
API_KEY_ENV_VAR,
AWS_DEFAULT_REGION_ENV_VAR,
CI_API_KEY_ENV_VAR,
CI_SITE_ENV_VAR,
SITE_ENV_VAR,
} from '../constants'
import {
convertToCSV,
createDirectories,
deleteFolder,
getAllLogs,
getEndpointUrl,
getLogEvents,
getLogStreamNames,
getMasking,
Expand Down Expand Up @@ -722,6 +729,51 @@ describe('lambda flare', () => {
})
})

describe('getEndpointUrl', () => {
const ORIGINAL_ENV = process.env

beforeEach(() => {
process.env = {...ORIGINAL_ENV}
})

afterAll(() => {
process.env = ORIGINAL_ENV
})
Comment on lines +735 to +741
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing!


it('should return correct endpoint url', () => {
process.env[CI_SITE_ENV_VAR] = 'datadoghq.com'
const url = getEndpointUrl()
expect(url).toMatchSnapshot()
})

it('should throw error if the site is invalid', () => {
process.env[CI_SITE_ENV_VAR] = 'datad0ge.com'
expect(() => getEndpointUrl()).toThrowErrorMatchingSnapshot()
})

it('should not throw error if the site is invalid and DD_CI_BYPASS_SITE_VALIDATION is set', () => {
process.env['DD_CI_BYPASS_SITE_VALIDATION'] = 'true'
process.env[CI_SITE_ENV_VAR] = 'datad0ge.com'
const url = getEndpointUrl()
expect(url).toMatchSnapshot()
delete process.env['DD_CI_BYPASS_SITE_VALIDATION']
})

it('should use SITE_ENV_VAR if CI_SITE_ENV_VAR is not set', () => {
delete process.env[CI_SITE_ENV_VAR]
process.env[SITE_ENV_VAR] = 'us3.datadoghq.com'
const url = getEndpointUrl()
expect(url).toMatchSnapshot()
})

it('should use DEFAULT_DD_SITE if CI_SITE_ENV_VAR and SITE_ENV_VAR are not set', () => {
delete process.env[CI_SITE_ENV_VAR]
delete process.env[SITE_ENV_VAR]
const url = getEndpointUrl()
expect(url).toMatchSnapshot()
})
})

describe('send to Datadog', () => {
// File system mocks
beforeAll(() => {
Expand Down
34 changes: 30 additions & 4 deletions src/commands/lambda/flare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,25 @@ import FormData from 'form-data'
import inquirer from 'inquirer'
import JSZip from 'jszip'

import {API_KEY_ENV_VAR, AWS_DEFAULT_REGION_ENV_VAR, CI_API_KEY_ENV_VAR, SKIP_MASKING_ENV_VARS} from './constants'
import {DATADOG_SITE_US1, DATADOG_SITES} from '../../constants'
import {isValidDatadogSite} from '../../helpers/validation'

import {
API_KEY_ENV_VAR,
AWS_DEFAULT_REGION_ENV_VAR,
CI_API_KEY_ENV_VAR,
CI_SITE_ENV_VAR,
SITE_ENV_VAR,
SKIP_MASKING_ENV_VARS,
} from './constants'
import {getAWSCredentials, getLambdaFunctionConfig, getRegion} from './functions/commons'
import {confirmationQuestion, requestAWSCredentials} from './prompt'
import * as commonRenderer from './renderers/common-renderer'
import * as flareRenderer from './renderers/flare-renderer'

const {version} = require('../../../package.json')

const ENDPOINT_URL = 'https://datad0g.com/api/ui/support/serverless/flare'
const ENDPOINT_PATH = '/api/ui/support/serverless/flare'
nhulston marked this conversation as resolved.
Show resolved Hide resolved
const FLARE_OUTPUT_DIRECTORY = '.datadog-ci'
const LOGS_DIRECTORY = 'logs'
const FUNCTION_CONFIG_FILE_NAME = 'function_config.json'
Expand Down Expand Up @@ -253,7 +263,7 @@ export class LambdaFlareCommand extends Command {
await zipContents(rootFolderPath, zipPath)

// Send to Datadog
this.context.stdout.write('\n🚀 Sending to Datadog Support...\n')
this.context.stdout.write(`\n🚀 Sending to Datadog Support...\n`)
nhulston marked this conversation as resolved.
Show resolved Hide resolved
await sendToDatadog(zipPath, this.caseId!, this.email!, this.apiKey!, rootFolderPath)
this.context.stdout.write('\n✅ Successfully sent flare file to Datadog Support!\n')

Expand Down Expand Up @@ -574,8 +584,23 @@ export const zipContents = async (rootFolderPath: string, zipPath: string) => {
}
}

/**
* Calculates the full endpoint URL
* @throws Error if the site is invalid
* @returns the full endpoint URL
*/
export const getEndpointUrl = () => {
const baseUrl = process.env[CI_SITE_ENV_VAR] ?? process.env[SITE_ENV_VAR] ?? DATADOG_SITE_US1
if (!isValidDatadogSite(baseUrl)) {
throw Error(`Invalid site: ${baseUrl}. Must be one of: ${DATADOG_SITES.join(', ')}`)
}

return 'https://' + baseUrl + ENDPOINT_PATH
}

/**
* Send the zip file to Datadog support
* @param endpointUrl
nhulston marked this conversation as resolved.
Show resolved Hide resolved
* @param zipPath
* @param caseId
* @param email
Expand All @@ -590,6 +615,7 @@ export const sendToDatadog = async (
apiKey: string,
rootFolderPath: string
) => {
const endpointUrl = getEndpointUrl()
const form = new FormData()
form.append('case_id', caseId)
form.append('flare_file', fs.createReadStream(zipPath))
Expand All @@ -603,7 +629,7 @@ export const sendToDatadog = async (
}

try {
await axios.post(ENDPOINT_URL, form, headerConfig)
await axios.post(endpointUrl, form, headerConfig)
} catch (err) {
// Ensure the root folder is deleted if the request fails
deleteFolder(rootFolderPath)
Expand Down