-
Notifications
You must be signed in to change notification settings - Fork 84
/
connectionsettings.ts
359 lines (317 loc) · 13.8 KB
/
connectionsettings.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* --------------------------------------------------------------------------------------------
* SonarLint for VisualStudio Code
* Copyright (C) 2017-2023 SonarSource SA
* sonarlint@sonarsource.com
* Licensed under the LGPLv3 License. See LICENSE.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import * as VSCode from 'vscode';
import { Connection } from '../connected/connections';
import { SonarLintExtendedLanguageClient } from '../lsp/client';
import { logToSonarLintOutput } from '../util/logging';
import { ConnectionCheckResult } from '../lsp/protocol';
const SONARLINT_CATEGORY = 'sonarlint';
const CONNECTIONS_SECTION = 'connectedMode.connections';
const SONARQUBE = 'sonarqube';
const SONARCLOUD = 'sonarcloud';
const SONARQUBE_CONNECTIONS_CATEGORY = `${SONARLINT_CATEGORY}.${CONNECTIONS_SECTION}.${SONARQUBE}`;
const SONARCLOUD_CONNECTIONS_CATEGORY = `${SONARLINT_CATEGORY}.${CONNECTIONS_SECTION}.${SONARCLOUD}`;
async function hasUnmigratedConnections(
sqConnections: SonarQubeConnection[],
scConnections: SonarCloudConnection[],
settingsService: ConnectionSettingsService
): Promise<boolean> {
for (const connection of [...sqConnections, ...scConnections]) {
if (!(await settingsService.hasTokenForConnection(connection)) && connection.token) {
return true;
}
}
return false;
}
export async function migrateConnectedModeSettings(
settings: VSCode.WorkspaceConfiguration,
settingsService: ConnectionSettingsService
) {
const sqConnections = settings.get<SonarQubeConnection[]>(`${CONNECTIONS_SECTION}.${SONARQUBE}`, []);
const scConnections = settings.get<SonarCloudConnection[]>(`${CONNECTIONS_SECTION}.${SONARCLOUD}`, []);
if (await hasUnmigratedConnections(sqConnections, scConnections, settingsService)) {
suggestMigrationToSecureStorage(sqConnections, scConnections, settingsService);
}
}
async function suggestMigrationToSecureStorage(
sqConnections: SonarQubeConnection[],
scConnections: SonarCloudConnection[],
settingsService: ConnectionSettingsService
) {
const remindMeLaterAction = 'Ask me later';
const migrateToSecureStorageAction = 'Migrate';
const message = `SonarLint found SonarQube/SonarCloud token in settings file.
Do you want to migrate them to secure storage?`;
const selection = await VSCode.window.showWarningMessage(message, migrateToSecureStorageAction, remindMeLaterAction);
if (selection === migrateToSecureStorageAction) {
await settingsService.addTokensFromSettingsToSecureStorage(sqConnections, scConnections);
}
}
export class ConnectionSettingsService {
private static _instance: ConnectionSettingsService;
private readonly connectionCheckResults: Map<string, ConnectionCheckResult>;
constructor(
private readonly secretStorage: VSCode.SecretStorage,
private readonly client: SonarLintExtendedLanguageClient
) {
this.connectionCheckResults = new Map<string, ConnectionCheckResult>();
}
static init(context: VSCode.ExtensionContext, client: SonarLintExtendedLanguageClient): void {
ConnectionSettingsService._instance = new ConnectionSettingsService(context.secrets, client);
}
static get instance(): ConnectionSettingsService {
return ConnectionSettingsService._instance;
}
async storeUpdatedConnectionToken(connection: SonarQubeConnection | SonarCloudConnection, token: string) {
const tokenStorageKey = getTokenStorageKey(connection);
const existingToken = await this.secretStorage.get(tokenStorageKey);
if (!existingToken || existingToken !== token) {
await this.storeServerToken(tokenStorageKey, token);
return true;
}
return false;
}
/**
*
* @param serverUrlOrOrganizationKey SonarQube URL or SonarCloud organization ID
* @param token auth token
*/
async storeServerToken(serverUrlOrOrganizationKey: string, token: string): Promise<void> {
if (token) {
await this.secretStorage.store(serverUrlOrOrganizationKey, token);
}
}
async getServerToken(serverUrlOrOrganizationKey: string): Promise<string | undefined> {
return this.secretStorage.get(serverUrlOrOrganizationKey);
}
async hasTokenForConnection(connection: SonarQubeConnection | SonarCloudConnection) {
return this.hasTokenForServer(getTokenStorageKey(connection));
}
async hasTokenForServer(serverUrlOrOrganizationKey: string): Promise<boolean> {
try {
const serverToken = await this.getServerToken(serverUrlOrOrganizationKey);
return serverToken !== undefined;
} catch (errorWhileFetchingToken) {
return false;
}
}
async deleteTokenForConnection(connection: SonarQubeConnection | SonarCloudConnection): Promise<void> {
return this.deleteTokenForServer(getTokenStorageKey(connection));
}
async deleteTokenForServer(serverUrlOrOrganizationKey: string): Promise<void> {
return this.secretStorage.delete(serverUrlOrOrganizationKey);
}
getSonarQubeConnections(): SonarQubeConnection[] {
return VSCode.workspace
.getConfiguration(SONARLINT_CATEGORY)
.get<SonarQubeConnection[]>(`${CONNECTIONS_SECTION}.${SONARQUBE}`);
}
setSonarQubeConnections(sqConnections: SonarQubeConnection[]) {
VSCode.workspace
.getConfiguration()
.update(SONARQUBE_CONNECTIONS_CATEGORY, sqConnections, VSCode.ConfigurationTarget.Global);
}
async addSonarQubeConnection(connection: SonarQubeConnection) {
const connections = this.getSonarQubeConnections();
const newConnection: SonarQubeConnection = { serverUrl: connection.serverUrl };
if (connection.connectionId !== undefined) {
newConnection.connectionId = connection.connectionId;
}
if (connection.disableNotifications) {
newConnection.disableNotifications = true;
}
await this.storeUpdatedConnectionToken(connection, connection.token);
connections.push(newConnection);
VSCode.workspace
.getConfiguration()
.update(SONARQUBE_CONNECTIONS_CATEGORY, connections, VSCode.ConfigurationTarget.Global);
}
async updateSonarQubeConnection(connection: SonarQubeConnection) {
const connections = this.getSonarQubeConnections();
const connectionToUpdate = connections.find(c => c.connectionId === connection.connectionId);
if (!connectionToUpdate) {
throw new Error(`Could not find connection '${connection.connectionId}' to update`);
}
connectionToUpdate.serverUrl = connection.serverUrl;
if (connection.disableNotifications) {
connectionToUpdate.disableNotifications = true;
} else {
delete connectionToUpdate.disableNotifications;
}
const didUpdateToken = await this.storeUpdatedConnectionToken(connection, connection.token);
if (didUpdateToken) {
await this.client.onTokenUpdate(connection.connectionId, connection.token);
}
delete connectionToUpdate.token;
VSCode.workspace
.getConfiguration()
.update(SONARQUBE_CONNECTIONS_CATEGORY, connections, VSCode.ConfigurationTarget.Global);
}
getSonarCloudConnections(): SonarCloudConnection[] {
return VSCode.workspace
.getConfiguration(SONARLINT_CATEGORY)
.get<SonarCloudConnection[]>(`${CONNECTIONS_SECTION}.${SONARCLOUD}`);
}
setSonarCloudConnections(scConnections: SonarCloudConnection[]) {
VSCode.workspace
.getConfiguration()
.update(SONARCLOUD_CONNECTIONS_CATEGORY, scConnections, VSCode.ConfigurationTarget.Global);
}
async addSonarCloudConnection(connection: SonarCloudConnection) {
const connections = this.getSonarCloudConnections();
const newConnection: SonarCloudConnection = { organizationKey: connection.organizationKey };
if (connection.connectionId !== undefined) {
newConnection.connectionId = connection.connectionId;
}
if (connection.disableNotifications) {
newConnection.disableNotifications = true;
}
await this.storeUpdatedConnectionToken(connection, connection.token);
connections.push(newConnection);
VSCode.workspace
.getConfiguration()
.update(SONARCLOUD_CONNECTIONS_CATEGORY, connections, VSCode.ConfigurationTarget.Global);
}
async updateSonarCloudConnection(connection: SonarCloudConnection) {
const connections = this.getSonarCloudConnections();
const connectionToUpdate = connections.find(c => c.connectionId === connection.connectionId);
if (!connectionToUpdate) {
throw new Error(`Could not find connection '${connection.connectionId}' to update`);
}
connectionToUpdate.organizationKey = connection.organizationKey;
if (connection.disableNotifications) {
connectionToUpdate.disableNotifications = true;
} else {
delete connectionToUpdate.disableNotifications;
}
const didUpdateToken = await this.storeUpdatedConnectionToken(connection, connection.token);
if (didUpdateToken) {
await this.client.onTokenUpdate(connection.connectionId, connection.token);
}
delete connectionToUpdate.token;
VSCode.workspace
.getConfiguration()
.update(SONARCLOUD_CONNECTIONS_CATEGORY, connections, VSCode.ConfigurationTarget.Global);
}
async addTokensFromSettingsToSecureStorage(
sqConnections: SonarQubeConnection[],
scConnections: SonarCloudConnection[]
) {
await Promise.all(
[...sqConnections, ...scConnections].map(async c => {
if (c.token !== undefined && !(await this.hasTokenForConnection(c))) {
await this.storeUpdatedConnectionToken(c, c.token);
c.token = undefined;
}
})
);
await updateConfigIfNotEmpty(sqConnections, SONARQUBE_CONNECTIONS_CATEGORY);
await updateConfigIfNotEmpty(scConnections, SONARCLOUD_CONNECTIONS_CATEGORY);
}
async loadSonarQubeConnection(connectionId: string) {
const allSonarQubeConnections = this.getSonarQubeConnections();
const loadedConnection = allSonarQubeConnections.find(c => c.connectionId === connectionId);
if (loadedConnection) {
loadedConnection.token = await this.getServerToken(loadedConnection.serverUrl);
}
return loadedConnection;
}
async loadSonarCloudConnection(connectionId: string) {
const allSonarCloudConnections = this.getSonarCloudConnections();
const loadedConnection = allSonarCloudConnections.find(c => c.connectionId === connectionId);
if (loadedConnection) {
loadedConnection.token = await this.getServerToken(loadedConnection.organizationKey);
}
return loadedConnection;
}
async removeConnection(connectionItem: Promise<Connection>) {
const connection = await connectionItem;
const isSonarQube = connection.contextValue === 'sonarqubeConnection';
const deleteAction = 'Delete';
const confirm = await VSCode.window.showWarningMessage(
`Are you sure you want to delete ${isSonarQube ? 'SonarQube' : 'SonarCloud'} connection '${
connection.id
}' and project bindings related to it?`,
{ modal: true },
deleteAction
);
if (confirm !== deleteAction) {
return false;
}
if (isSonarQube) {
const sqConnections = this.getSonarQubeConnections();
const matchingConnectionIndex = sqConnections.findIndex(c => c.connectionId === connection.id);
if (matchingConnectionIndex === -1) {
showSaveSettingsWarning();
return false;
}
const foundConnection = sqConnections[matchingConnectionIndex];
await this.deleteTokenForConnection(foundConnection);
sqConnections.splice(matchingConnectionIndex, 1);
this.setSonarQubeConnections(sqConnections);
} else {
const scConnections = this.getSonarCloudConnections();
const matchingConnectionIndex = scConnections.findIndex(c => c.connectionId === connection.id);
if (matchingConnectionIndex === -1) {
showSaveSettingsWarning();
return false;
}
const foundConnection = scConnections[matchingConnectionIndex];
await this.deleteTokenForConnection(foundConnection);
scConnections.splice(matchingConnectionIndex, 1);
this.setSonarCloudConnections(scConnections);
}
this.connectionCheckResults.delete(connection.id);
return true;
}
async generateToken(baseServerUrl: string) {
const { token } = await this.client.generateToken(baseServerUrl);
if (!token) {
logToSonarLintOutput(`Could not automatically generate server token for generation params: ${baseServerUrl}`);
}
return token;
}
async checkNewConnection(token: string, serverOrOrganization: string, isSonarQube: boolean) {
return this.client.checkNewConnection(token, serverOrOrganization, isSonarQube);
}
reportConnectionCheckResult(connectionCheckResult: ConnectionCheckResult) {
this.connectionCheckResults.set(connectionCheckResult.connectionId, connectionCheckResult);
}
getStatusForConnection(connectionId: string) {
return this.connectionCheckResults.get(connectionId);
}
}
function showSaveSettingsWarning() {
const saveSettings =
'You are trying to delete connection with modified settings file.' +
' Please save your settings file and try again.';
VSCode.window.showWarningMessage(saveSettings);
}
export interface BaseConnection {
token?: string;
connectionId?: string;
disableNotifications?: boolean;
connectionCheckResult?: Promise<ConnectionCheckResult>;
}
export interface SonarQubeConnection extends BaseConnection {
serverUrl: string;
}
export interface SonarCloudConnection extends BaseConnection {
organizationKey: string;
}
export function isSonarQubeConnection(connection: BaseConnection): connection is SonarQubeConnection {
return (connection as SonarQubeConnection).serverUrl !== undefined;
}
function getTokenStorageKey(connection: SonarQubeConnection | SonarCloudConnection) {
return isSonarQubeConnection(connection) ? connection.serverUrl : connection.organizationKey;
}
async function updateConfigIfNotEmpty(connections, configCategory) {
if (connections.length > 0) {
await VSCode.workspace.getConfiguration().update(configCategory, connections, VSCode.ConfigurationTarget.Global);
}
}