-
Notifications
You must be signed in to change notification settings - Fork 55
/
upload.ts
158 lines (132 loc) · 5.27 KB
/
upload.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {Command} from 'clipanion'
import {BufferedMetricsLogger} from 'datadog-metrics'
import fs from 'fs'
import path from 'path'
import {getApiHostForSite} from '../../helpers/utils'
import {apiConstructor} from './api'
import {Payload} from './interfaces'
import {getMetricsLogger} from './metrics'
import {
renderCannotFindFile,
renderCommandInfo,
renderDryRunUpload,
renderFailedUpload,
renderFailedUploadBecauseOf403,
renderMissingEnvironmentVariable,
renderMissingParameter,
renderMissingReleaseVersionParameter,
renderSuccessfulCommand,
renderUnsupportedParameterValue,
renderUpload,
} from './renderer'
export class UploadCommand extends Command {
public static SUPPORTED_SOURCES = ['snyk']
public static usage = Command.Usage({
description: 'Upload dependencies graph to Datadog.',
details:
'Uploads dependencies graph to Datadog to detect runtime vulnerabilities by Continuous Profiler. See README for details.',
examples: [
[
'Upload dependency graph generated by `snyk test --print-deps --sub-project=my-project --json > ./snyk_deps.json` command',
'datadog-ci dependencies upload ./snyk_deps.json --source snyk --service my-service --release-version 1.234',
],
],
})
private static INVALID_INPUT_EXIT_CODE = 1
private static MISSING_FILE_EXIT_CODE = 2
private static UPLOAD_ERROR_EXIT_CODE = 3
private config = {
apiHost: getApiHostForSite(process.env.DATADOG_SITE || 'datadoghq.com'),
apiKey: process.env.DATADOG_API_KEY,
appKey: process.env.DATADOG_APP_KEY,
}
private dependenciesFilePath!: string
private dryRun = false
private releaseVersion?: string
private service?: string
private source?: string
public async execute() {
// Validate input
if (!this.source) {
this.context.stderr.write(renderMissingParameter('--source', UploadCommand.SUPPORTED_SOURCES))
return UploadCommand.INVALID_INPUT_EXIT_CODE
}
if (UploadCommand.SUPPORTED_SOURCES.indexOf(this.source) === -1) {
this.context.stderr.write(
renderUnsupportedParameterValue('--source', this.source, UploadCommand.SUPPORTED_SOURCES)
)
return UploadCommand.INVALID_INPUT_EXIT_CODE
}
if (!this.service) {
this.context.stderr.write(renderMissingParameter('--service'))
return UploadCommand.INVALID_INPUT_EXIT_CODE
}
if (!this.config.appKey) {
this.context.stderr.write(renderMissingEnvironmentVariable('DATADOG_APP_KEY'))
return UploadCommand.INVALID_INPUT_EXIT_CODE
}
if (!this.config.apiKey) {
this.context.stderr.write(renderMissingEnvironmentVariable('DATADOG_API_KEY'))
return UploadCommand.INVALID_INPUT_EXIT_CODE
}
// Display warning for missing --release-version
if (!this.releaseVersion) {
this.context.stdout.write(renderMissingReleaseVersionParameter())
}
// Check if file exists (we are not validating the content of the file)
this.dependenciesFilePath = path.resolve(this.dependenciesFilePath)
if (!fs.existsSync(this.dependenciesFilePath)) {
this.context.stderr.write(renderCannotFindFile(this.dependenciesFilePath))
return UploadCommand.MISSING_FILE_EXIT_CODE
}
// Upload dependencies
const metricsLogger = getMetricsLogger(this.config.apiHost, this.service, this.releaseVersion)
this.context.stdout.write(
renderCommandInfo(this.dependenciesFilePath!, this.source, this.service, this.releaseVersion, this.dryRun)
)
try {
const initialTime = Date.now()
const payload: Payload = {
dependenciesFilePath: this.dependenciesFilePath,
service: this.service,
source: this.source,
version: this.releaseVersion,
}
await this.uploadDependencies(payload, metricsLogger)
const totalTimeSeconds = (Date.now() - initialTime) / 1000
this.context.stdout.write(renderSuccessfulCommand(totalTimeSeconds))
metricsLogger.gauge('duration', totalTimeSeconds)
} catch (error) {
this.context.stderr.write(error.message)
return UploadCommand.UPLOAD_ERROR_EXIT_CODE
} finally {
metricsLogger.flush()
}
}
private async uploadDependencies(payload: Payload, metricsLogger: BufferedMetricsLogger) {
const api = apiConstructor(`https://${this.config.apiHost}`, this.config.apiKey!, this.config.appKey!)
try {
if (this.dryRun) {
this.context.stdout.write(renderDryRunUpload())
return
}
this.context.stdout.write(renderUpload())
await api.uploadDependencies(payload)
metricsLogger.increment('success', 1)
} catch (error) {
if (error.isAxiosError && error.response && error.response.status === 403) {
this.context.stdout.write(renderFailedUploadBecauseOf403(error.message))
} else {
this.context.stdout.write(renderFailedUpload(error.message))
}
metricsLogger.increment('failed', 1)
throw error
}
}
}
UploadCommand.addPath('dependencies', 'upload')
UploadCommand.addOption('dependenciesFilePath', Command.String({required: true}))
UploadCommand.addOption('source', Command.String('--source'))
UploadCommand.addOption('releaseVersion', Command.String('--release-version'))
UploadCommand.addOption('service', Command.String('--service'))
UploadCommand.addOption('dryRun', Command.Boolean('--dry-run'))