-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.ts
More file actions
281 lines (242 loc) · 11.3 KB
/
extension.ts
File metadata and controls
281 lines (242 loc) · 11.3 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
'use strict';
import * as path from 'path';
import * as vscode from 'vscode';
import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind
} from 'vscode-languageclient/node';
import * as commands from './commands';
import { GlobalState, EXTENSION_QUALIFIED_ID, REDHAT_MAVEN_REPOSITORY, REDHAT_MAVEN_REPOSITORY_DOCUMENTATION_URL, REDHAT_CATALOG } from './constants';
import { generateRHDAReport } from './rhda';
import { globalConfig } from './config';
import { StatusMessages, PromptText } from './constants';
import { caStatusBarProvider } from './caStatusBarProvider';
import { CANotification } from './caNotification';
import { DepOutputChannel } from './depOutputChannel';
import { record, startUp, TelemetryActions } from './redhatTelemetry';
import { applySettingNameMappings, buildErrorMessage } from './utils';
let lspClient: LanguageClient;
export let outputChannelDep: DepOutputChannel;
/**
* Activates the extension upon launch.
* @param context - The extension context.
*/
export function activate(context: vscode.ExtensionContext) {
globalConfig.linkToSecretStorage(context);
startUp(context);
// show welcome message after first install or upgrade
showUpdateNotification(context);
const disposableStackAnalysisCommand = vscode.commands.registerCommand(
commands.STACK_ANALYSIS_COMMAND,
async (filePath: string, isFromCA: boolean = false) => {
filePath = filePath ? filePath : vscode.window.activeTextEditor.document.uri.fsPath;
const fileName = path.basename(filePath);
if (isFromCA) {
record(context, TelemetryActions.componentAnalysisVulnerabilityReportQuickfixOption, { manifest: fileName, fileName: fileName });
}
try {
await generateRHDAReport(context, filePath, outputChannelDep);
record(context, TelemetryActions.vulnerabilityReportDone, { manifest: fileName, fileName: fileName });
} catch (error) {
const message = applySettingNameMappings(error.message);
vscode.window.showErrorMessage(message);
outputChannelDep.error(buildErrorMessage(error));
record(context, TelemetryActions.vulnerabilityReportFailed, { manifest: fileName, fileName: fileName, error: message });
}
}
);
const disposableStackLogsCommand = vscode.commands.registerCommand(
commands.STACK_LOGS_COMMAND,
() => {
if (outputChannelDep) {
outputChannelDep.show();
} else {
vscode.window.showInformationMessage(StatusMessages.WIN_SHOW_LOGS);
}
}
);
const disposableTrackRecommendationAcceptance = vscode.commands.registerCommand(
commands.TRACK_RECOMMENDATION_ACCEPTANCE_COMMAND,
(dependency, fileName) => {
record(context, TelemetryActions.componentAnalysisRecommendationAccepted, { manifest: fileName, fileName: fileName, package: dependency.split('@')[0], version: dependency.split('@')[1] });
if (fileName === 'Dockerfile' || fileName === 'Containerfile') {
redirectToRedHatCatalog();
}
if (fileName === 'pom.xml') {
showRHRepositoryRecommendationNotification();
}
}
);
registerStackAnalysisCommands(context);
globalConfig.authorizeRHDA(context)
.then(() => {
// Create output channel
outputChannelDep = new DepOutputChannel();
// The server is implemented in node
const serverModule = context.asAbsolutePath(
path.join('dist', 'server.js')
);
// The debug options for the server
// --inspect=6010: runs the server in Node's Inspector mode so VS Code can attach to the server for debugging
const debugOptions = { execArgv: ['--nolazy', '--inspect=6010'] };
// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
const serverOptions: ServerOptions = {
run: { module: serverModule, transport: TransportKind.ipc },
debug: {
module: serverModule,
transport: TransportKind.ipc,
options: debugOptions
}
};
// Options to control the language client
const clientOptions: LanguageClientOptions = {
documentSelector: [
{ scheme: 'file', language: 'json' },
{ scheme: 'file', language: 'jsonc' },
{ scheme: 'file', language: 'xml' },
{ scheme: 'file', language: 'plaintext' },
{ scheme: 'file', language: 'pip-requirements' },
{ scheme: 'file', language: 'go' },
{ scheme: 'file', language: 'go.mod' },
{ scheme: 'file', language: 'groovy' },
{ scheme: 'file', language: 'kotlin' },
{ scheme: 'file', language: 'dockerfile' }
]
};
// Create the language client and start the client.
lspClient = new LanguageClient(
'redHatDependencyAnalyticsServer',
'Red Hat Dependency Analytics Server',
serverOptions,
clientOptions
);
lspClient.start().then(() => {
const showVulnerabilityFoundPrompt = async (msg: string, filePath: string) => {
const fileName = path.basename(filePath);
const selection = await vscode.window.showWarningMessage(`${msg}`, PromptText.FULL_STACK_PROMPT_TEXT);
if (selection === PromptText.FULL_STACK_PROMPT_TEXT) {
record(context, TelemetryActions.vulnerabilityReportPopupOpened, { manifest: fileName, fileName: fileName });
vscode.commands.executeCommand(commands.STACK_ANALYSIS_COMMAND, filePath);
} else {
record(context, TelemetryActions.vulnerabilityReportPopupIgnored, { manifest: fileName, fileName: fileName });
}
};
lspClient.onNotification('caNotification', respData => {
const notification = new CANotification(respData);
caStatusBarProvider.showSummary(notification.statusText(), notification.origin());
if (notification.hasWarning()) {
showVulnerabilityFoundPrompt(notification.popupText(), notification.origin());
record(context, TelemetryActions.componentAnalysisDone, { manifest: path.basename(notification.origin()), fileName: path.basename(notification.origin()) });
}
});
lspClient.onNotification('caError', errorData => {
const notification = new CANotification(errorData);
caStatusBarProvider.setError();
// Since CA is an automated feature, only warning message will be shown on failure
vscode.window.showWarningMessage(notification.errorMsg());
// Record telemetry event
record(context, TelemetryActions.componentAnalysisFailed, { manifest: path.basename(notification.origin()), fileName: path.basename(notification.origin()), error: notification.errorMsg() });
});
});
context.subscriptions.push(
disposableStackAnalysisCommand,
disposableStackLogsCommand,
disposableTrackRecommendationAcceptance,
// disposableSetSnykToken,
caStatusBarProvider,
);
})
.catch(error => {
vscode.window.showErrorMessage(`Failed to Authorize Red Hat Dependency Analytics extension: ${error.message}`);
throw error;
});
vscode.workspace.onDidChangeConfiguration(() => {
globalConfig.loadData();
});
}
/**
* Deactivates the extension.
* @returns A `Thenable` for void.
*/
export function deactivate(): Thenable<void> {
return lspClient?.stop();
}
/**
* Shows an update notification if the extension has been updated to a new version.
* @param context - The extension context.
* @returns A Promise that resolves once the notification has been displayed if needed.
*/
async function showUpdateNotification(context: vscode.ExtensionContext) {
const packageJSON = vscode.extensions.getExtension(EXTENSION_QUALIFIED_ID).packageJSON;
const version = packageJSON.version;
const previousVersion = context.globalState.get<string>(GlobalState.VERSION);
if (version === previousVersion) {
return;
}
context.globalState.update(GlobalState.VERSION, version);
const result = await vscode.window.showInformationMessage(
`${packageJSON.displayName} has been updated to v${version} — check out what's new!`,
'README',
'Release Notes'
);
if (result !== undefined) {
if (result === 'README') {
await vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(packageJSON.homepage));
} else if (result === 'Release Notes') {
await vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(`${packageJSON.repository.url}/releases/tag/v${version}`));
}
}
}
/**
* Redirects the user to the Red Hat certified image catalog website.
*/
function redirectToRedHatCatalog() {
vscode.env.openExternal(vscode.Uri.parse(REDHAT_CATALOG));
}
/**
* Shows a notification regarding Red Hat Dependency Analytics recommendations.
*/
function showRHRepositoryRecommendationNotification() {
const msg = 'Important: If you apply Red Hat Dependency Analytics recommendations, ' +
`make sure the Red Hat GA Repository (${REDHAT_MAVEN_REPOSITORY}) has been added to your project configuration. ` +
'This ensures that the applied dependencies work correctly. ' +
`Learn how to add the repository: [Click here](${REDHAT_MAVEN_REPOSITORY_DOCUMENTATION_URL})`;
vscode.window.showWarningMessage(msg);
}
/**
* Registers stack analysis commands to track RHDA report generations.
* @param context - The extension context.
*/
function registerStackAnalysisCommands(context: vscode.ExtensionContext) {
const invokeFullStackReport = async (filePath: string) => {
const fileName = path.basename(filePath);
try {
await generateRHDAReport(context, filePath, outputChannelDep);
record(context, TelemetryActions.vulnerabilityReportDone, { manifest: fileName, fileName: fileName });
} catch (error) {
const message = applySettingNameMappings(error.message);
vscode.window.showErrorMessage(message);
outputChannelDep.error(buildErrorMessage(error));
record(context, TelemetryActions.vulnerabilityReportFailed, { manifest: fileName, fileName: fileName, error: message });
}
};
const recordAndInvoke = (origin: string, uri: vscode.Uri) => {
const fileUri = uri || vscode.window.activeTextEditor.document.uri;
const filePath = fileUri.fsPath;
record(context, origin, { manifest: filePath.split('/').pop(), fileName: filePath.split('/').pop() });
invokeFullStackReport(filePath);
};
const registerCommand = (cmd: string, action: TelemetryActions) => {
return vscode.commands.registerCommand(cmd, recordAndInvoke.bind(null, action));
};
const stackAnalysisCommands = [
registerCommand(commands.STACK_ANALYSIS_FROM_EDITOR_COMMAND, TelemetryActions.vulnerabilityReportEditor),
registerCommand(commands.STACK_ANALYSIS_FROM_EXPLORER_COMMAND, TelemetryActions.vulnerabilityReportExplorer),
registerCommand(commands.STACK_ANALYSIS_FROM_PIE_BTN_COMMAND, TelemetryActions.vulnerabilityReportPieBtn),
registerCommand(commands.STACK_ANALYSIS_FROM_STATUS_BAR_COMMAND, TelemetryActions.vulnerabilityReportStatusBar),
];
context.subscriptions.push(...stackAnalysisCommands);
}