Skip to content

Commit

Permalink
Fix the updater
Browse files Browse the repository at this point in the history
- fix the ignore processing and updating
  • Loading branch information
Owen Rumney committed Jan 10, 2022
1 parent e57cdc2 commit 2079b50
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 48 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Ignore codes will be automatically resolved and the description of the error wil

## Release Notes

### 1.4.1
- Fix updater

### 1.4.0
- Use output channel instead of terminal for better cross platform command support
- Remove explicit run command and use refresh to update the list with a fresh run
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "tfsec",
"publisher": "tfsec",
"description": "tfsec integration for Visual Studio Code",
"version": "1.4.0",
"version": "1.4.1",
"engines": {
"vscode": "^1.54.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/explorer/issues_treeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class TfsecIssueProvider implements vscode.TreeDataProvider<TfsecTreeItem
this.taintResults = !this.taintResults;
}
catch {
console.debug(`Error loading results file ${this.resultsStoragePath}`)
console.debug(`Error loading results file ${this.resultsStoragePath}`);
}
}
} else {
Expand Down
35 changes: 23 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { addIgnore, triggerDecoration, IgnoreDetails } from './ignore';
import { addIgnore, triggerDecoration, IgnoreDetails, FileIgnores } from './ignore';
import { TfsecIssueProvider } from './explorer/issues_treeview';
import { TfsecTreeItem, TfsecTreeItemType } from './explorer/tfsec_treeitem';
import { getInstalledTfsecVersion, getBinaryPath } from './utils';
Expand Down Expand Up @@ -31,9 +31,9 @@ export function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(vscode.commands.registerCommand('tfsec.refresh', () => issueProvider.refresh()));
context.subscriptions.push(vscode.commands.registerCommand('tfsec.version', () => showCurrentTfsecVersion()));
context.subscriptions.push(vscode.commands.registerCommand('tfsec.ignore', (element: TfsecTreeItem) => ignoreInstance(element)));
context.subscriptions.push(vscode.commands.registerCommand('tfsec.ignoreAll', (element: TfsecTreeItem) => ignoreAllInstances(element, issueProvider)));
context.subscriptions.push(vscode.commands.registerCommand('tfsec.ignoreSeverity', (element: TfsecTreeItem) => ignoreAllInstances(element, issueProvider)));
context.subscriptions.push(vscode.commands.registerCommand('tfsec.ignore', (element: TfsecTreeItem) => ignoreInstance(element, outputChannel)));
context.subscriptions.push(vscode.commands.registerCommand('tfsec.ignoreAll', (element: TfsecTreeItem) => ignoreAllInstances(element, issueProvider, outputChannel)));
context.subscriptions.push(vscode.commands.registerCommand('tfsec.ignoreSeverity', (element: TfsecTreeItem) => ignoreAllInstances(element, issueProvider, outputChannel)));
context.subscriptions.push(vscode.commands.registerCommand("tfsec.run", () => runTfsec(issueProvider, outputChannel)));
context.subscriptions.push(vscode.commands.registerCommand("tfsec.updatebinary", () => updateBinary(outputChannel)));

Expand All @@ -56,9 +56,9 @@ export function activate(context: vscode.ExtensionContext) {
showCurrentTfsecVersion();
}

function ignoreInstance(element: TfsecTreeItem) {
function ignoreInstance(element: TfsecTreeItem, outputChannel: vscode.OutputChannel) {
const details = [new IgnoreDetails(element.code, element.startLineNumber, element.endLineNumber)];
addIgnore(element.filename, details);
addIgnore(element.filename, details, outputChannel);

rerunIfRequired();
}
Expand All @@ -73,12 +73,14 @@ function rerunIfRequired() {
};
}

function ignoreAllInstances(element: TfsecTreeItem, issueProvider: TfsecIssueProvider) {
async function ignoreAllInstances(element: TfsecTreeItem, issueProvider: TfsecIssueProvider, outputChannel: vscode.OutputChannel) {
outputChannel.show();
outputChannel.appendLine("\nSetting ignores - ");

var seenIgnores: string[] = [];
var ignoreMap = new Map<string, IgnoreDetails[]>();

let severityIgnore = element.treeItemType === TfsecTreeItemType.issueSeverity;

for (let index = 0; index < issueProvider.resultData.length; index++) {
var r = issueProvider.resultData[index];
if (r === undefined) {
Expand All @@ -101,11 +103,19 @@ function ignoreAllInstances(element: TfsecTreeItem, issueProvider: TfsecIssuePro
ignoreMap.set(r.filename, ignores);
}

var edits: FileIgnores[] = [];
ignoreMap.forEach((ignores: IgnoreDetails[], filename: string) => {
addIgnore(filename, ignores);
edits.push(new FileIgnores(filename, ignores));
});

await edits.reduce(
(p, x) =>
p.then(_ => addIgnore(x.filename, x.ignores, outputChannel)),
Promise.resolve()
).then(() => {
outputChannel.appendLine("Checking if re-run is enabled....");
rerunIfRequired();
});
Promise.resolve();
rerunIfRequired();
}


Expand Down Expand Up @@ -137,6 +147,7 @@ function buildCommand(resultsStoragePath: string, scanPath: string) {
}

function runTfsec(issueProvider: TfsecIssueProvider, outputChannel: vscode.OutputChannel) {
outputChannel.show();
outputChannel.appendLine("");
outputChannel.appendLine("Running tfsec to update results");

Expand Down Expand Up @@ -172,7 +183,7 @@ function updateBinary(outputChannel: vscode.OutputChannel) {
outputChannel.appendLine("Attempting to download the latest version");
var binary = getBinaryPath();
try {
let result: Buffer = child.execSync(binary + " --update --verbdose");
let result: Buffer = child.execSync(binary + " --update --verbose");
outputChannel.appendLine(result.toLocaleString());
} catch (err) {
vscode.window.showErrorMessage("There was a problem with the update, check the output window");
Expand Down
85 changes: 51 additions & 34 deletions src/ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class IgnoreDetails {
}
}

class FileIgnores {
public constructor(public readonly filename: string, public readonly ignores: IgnoreDetails[]) { }
}

const tfsecIgnoreDecoration = vscode.window.createTextEditorDecorationType({
fontStyle: 'italic',
color: new vscode.ThemeColor("editorGutter.commentRangeForeground"),
Expand Down Expand Up @@ -65,45 +69,58 @@ function getTfsecDescription(tfsecCode: string) {
return check;
}

const addIgnore = async (filename: string, ignores: IgnoreDetails[], outputChannel: vscode.OutputChannel): Promise<void> => {
if (!filename.endsWith(".tf")) {
outputChannel.appendLine(`${filename} is not a tf file`);
return Promise.resolve();
}

ignores = ignores.sort(function (a: IgnoreDetails, b: IgnoreDetails): number {
if (a.startLine > b.startLine) {
return 1;
} else if (a.startLine < b.startLine) {
return -1;
}
return 0;
});

const addIgnore = (filename: string, ignores: IgnoreDetails[]) => {
Promise.resolve(vscode.workspace.openTextDocument(vscode.Uri.file(filename)).then((file: vscode.TextDocument) => {
Promise.resolve(vscode.window.showTextDocument(file, 1, false).then(e => {
e.edit(edit => {
for (let index = 0; index < ignores.length; index++) {
let element = ignores[index];

if (element === undefined) { continue; }
const ignoreCode = `#tfsec:ignore:${element.code}`;
var ignoreLine: vscode.TextLine | undefined;
var startPos: number | undefined;
if (element.startLine === element.endLine) {
let errorLine = vscode.window.activeTextEditor?.document.lineAt(element.startLine);
if (errorLine !== null && errorLine !== undefined) {
let ignoreLinePos = errorLine.lineNumber - 1;
ignoreLine = vscode.window.activeTextEditor?.document.lineAt(ignoreLinePos);
startPos = ignoreLine?.text.length;
}
} else {
let ignoreLinePos = element.startLine - 1;
await vscode.window.showTextDocument(vscode.Uri.file(filename)).then(e => {
e.edit(edit => {
for (let index = 0; index < ignores.length; index++) {
let element = ignores[index];

if (element === undefined) { continue; }
const ignoreCode = `#tfsec:ignore:${element.code}`;
outputChannel.appendLine(`Adding ignore for ${ignoreCode}`);
var ignoreLine: vscode.TextLine | undefined;
var startPos: number | undefined;
if (element.startLine === element.endLine) {
let errorLine = vscode.window.activeTextEditor?.document.lineAt(element.startLine);
if (errorLine !== null && errorLine !== undefined) {
let ignoreLinePos = element.startLine;
ignoreLine = vscode.window.activeTextEditor?.document.lineAt(ignoreLinePos);
startPos = ignoreLine?.text.length;
}
if (ignoreLine === undefined || ignoreLine.text.includes(ignoreCode)) {
continue;
}
if (ignoreLine.text.includes('tfsec:')) {
edit.insert(new vscode.Position(element.startLine - 1, 0), `${ignoreCode} `);
} else {
if (startPos === undefined) {
startPos = 0;
}
edit.insert(new vscode.Position(ignoreLine.lineNumber, startPos), ` ${ignoreCode}\n`);
} else {
let ignoreLinePos = element.startLine;
ignoreLine = vscode.window.activeTextEditor?.document.lineAt(ignoreLinePos);
}
if (ignoreLine === undefined || ignoreLine.text.includes(ignoreCode)) {
continue;
}
if (ignoreLine !== undefined && ignoreLine.text !== undefined && ignoreLine.text.includes('tfsec:')) {
edit.insert(new vscode.Position(ignoreLine.lineNumber - 1, 0), `${ignoreCode} `);
} else {
if (startPos === undefined) {
startPos = 0;
}
edit.insert(new vscode.Position(ignoreLine.lineNumber - 1, 0), `${ignoreCode}\n`);
}
});
file.save();
}));
}));
}
});
e.document.save();
});
};

export { addIgnore, triggerDecoration, IgnoreDetails };
export { addIgnore, triggerDecoration, IgnoreDetails, FileIgnores };

0 comments on commit 2079b50

Please sign in to comment.