Skip to content

Commit

Permalink
Merge pull request #30 from aquasecurity/owenr-add-ignore-folder-support
Browse files Browse the repository at this point in the history
feat: support ignores of files and FOLDERS
  • Loading branch information
owenrumney authored Apr 25, 2022
2 parents b06bc98 + 0bf5bcf commit 5a6acc6
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the "tfsec" extension will be documented in this file.

### 1.10.0
- Fix issue with file path names in the explorer
- Add context support for locally ignoring files and directories

### 1.9.0
- Support new tfsec filesystem (relative path resolution)
- Maintain support older versions of tfsec
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: build

build:
npm run-script esbuild
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,18 @@ Ignore codes will be automatically resolved and the description of the error wil

![ignoredesc](ignoredesc.gif)

### Ignoring filepaths

In the Explorer view, you can right click on a folder or .tf file and select `Ignore path during tfsec runs`. This will pass the path to `--exclude-path` when running tfsec and is only applicable to this workspace on this machine.

To remove ignores, edit the `tfsec.excludedPath` in the `.vscode/settings.json` file of the current workspace.

## Release Notes

### 1.10.0
- Fix issue with file path names in the explorer
- Add context support for locally ignoring files and directories

### 1.9.0
- Support new tfsec filesystem (relative path resolution)
- Maintain support older versions of tfsec
Expand Down
20 changes: 18 additions & 2 deletions 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.9.0",
"version": "1.10.0",
"engines": {
"vscode": "^1.54.0"
},
Expand All @@ -27,7 +27,8 @@
"activationEvents": [
"onView:tfsec.issueview",
"onLanguage:terraform",
"onCommand:tfsec.run"
"onCommand:tfsec.run",
"workspaceContains:**/*.tf"
],
"main": "./out/main.js",
"contributes": {
Expand Down Expand Up @@ -63,6 +64,11 @@
"type": "boolean",
"default": "false",
"description": "Run tfsec with vebose flag to get more information"
},
"tfsec.excludedPaths": {
"type": "array",
"default": [],
"description": "Run tfsec but exclude these folders"
}
}
},
Expand Down Expand Up @@ -91,6 +97,10 @@
"command": "tfsec.ignore",
"title": "Ignore this issue instance"
},
{
"command": "tfsec.ignorePath",
"title": "Ignore path during tfsec runs"
},
{
"command": "tfsec.ignoreAll",
"title": "Ignore all instances"
Expand Down Expand Up @@ -144,6 +154,12 @@
}
],
"menus": {
"explorer/context": [
{
"command": "tfsec.ignorePath",
"when": "resourceExtname == .tf || explorerResourceIsFolder"
}
],
"commandPalette": [
{
"command": "tfsec.ignore",
Expand Down
33 changes: 12 additions & 21 deletions src/explorer/issues_treeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class TfsecIssueProvider implements vscode.TreeDataProvider<TfsecTreeItem
readonly onDidChangeTreeData: vscode.Event<TfsecTreeItem | undefined | void> = this._onDidChangeTreeData.event;
public resultData: CheckResult[] = [];
private taintResults: boolean = true;
private rootpath: string = "";
public rootpath: string = "";
private storagePath: string = "";
public readonly resultsStoragePath: string = "";

Expand Down Expand Up @@ -91,10 +91,6 @@ export class TfsecIssueProvider implements vscode.TreeDataProvider<TfsecTreeItem
var results: TfsecTreeItem[] = [];
var resolvedSeverities: string[] = [];

// if (this.taintResults) {
// Promise.resolve(this.loadResultData());
// }

for (let index = 0; index < this.resultData.length; index++) {
const result = this.resultData[index];
if (result === undefined) {
Expand All @@ -116,10 +112,6 @@ export class TfsecIssueProvider implements vscode.TreeDataProvider<TfsecTreeItem
var resolvedCodes: string[] = [];


// if (this.taintResults) {
// this.loadResultData();
// }

for (let index = 0; index < this.resultData.length; index++) {
const result = this.resultData[index];

Expand Down Expand Up @@ -148,36 +140,35 @@ export class TfsecIssueProvider implements vscode.TreeDataProvider<TfsecTreeItem
if (result.code !== code) {
continue;
}
let filename = this.relativizePath(result.filename);
const filename = this.relativizePath(result.filename);
const cmd = this.createFileOpenCommand(result);
var item = new TfsecTreeItem(`${filename}:${result.startLine}`, result, vscode.TreeItemCollapsibleState.None, cmd);
results.push(item);
}
return uniqueLocations(results);
}

private absolutizePath(incomingPath: string): string {
if (path.isAbsolute(incomingPath)) {
return incomingPath;
}
return path.join(this.rootpath, incomingPath);
}

private relativizePath(incomingPath: string): string {
if (path.isAbsolute(incomingPath)) {
return path.relative(this.rootpath, incomingPath);
}
return incomingPath;
const pathParts = path.parse(this.rootpath);
const workingIncomingPath = pathParts.root + incomingPath;
return path.relative(this.rootpath, workingIncomingPath);
}

private absolutizePath(incomingPath: string): string {
const pathParts = path.parse(this.rootpath);
return path.join(pathParts.root, incomingPath);
}

private createFileOpenCommand(result: CheckResult) {
const issueRange = new vscode.Range(new vscode.Position(result.startLine - 1, 0), new vscode.Position(result.endLine, 0));

const pathToOpen = this.absolutizePath(result.filename);
return {
command: "vscode.open",
title: "",
arguments: [
vscode.Uri.file(this.absolutizePath(result.filename)),
vscode.Uri.file(pathToOpen),
{
selection: issueRange,
}
Expand Down
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { ignoreAllInstances, ignoreInstance, triggerDecoration } from './ignore';
import { ignoreAllInstances, ignoreInstance, ingorePath, triggerDecoration } from './ignore';
import { TfsecIssueProvider } from './explorer/issues_treeview';
import { TfsecTreeItem } from './explorer/tfsec_treeitem';
import { TfsecHelpProvider } from './explorer/check_helpview';
Expand Down Expand Up @@ -37,6 +37,7 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('tfsec.ignoreSeverity', (element: TfsecTreeItem) => ignoreAllInstances(element, issueProvider, outputChannel)));
context.subscriptions.push(vscode.commands.registerCommand("tfsec.run", () => tfsecWrapper.run()));
context.subscriptions.push(vscode.commands.registerCommand("tfsec.updatebinary", () => tfsecWrapper.updateBinary()));
context.subscriptions.push(vscode.commands.registerCommand('tfsec.ignorePath', (element: any) => ingorePath(element)));

context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(editor => {
// only act if this is a terraform file
Expand Down
20 changes: 19 additions & 1 deletion src/ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TfsecTreeItem, TfsecTreeItemType } from './explorer/tfsec_treeitem';

let timeout: NodeJS.Timer | undefined = undefined;
let activeEditor = vscode.window.activeTextEditor;
import * as path from 'path';

class IgnoreDetails {
public readonly code: string;
Expand Down Expand Up @@ -143,6 +144,23 @@ const ignoreInstance = (element: TfsecTreeItem, outputChannel: vscode.OutputChan
};


const ingorePath = (element: any) => {

if (vscode.workspace && vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0]) {
const rootpath = vscode.workspace.workspaceFolders[0].uri.fsPath;
const config = vscode.workspace.getConfiguration("tfsec");
let excludedPaths = config.get<string[]>("excludedPaths");

var filepath = element.fsPath;
filepath = path.relative(rootpath, filepath);

excludedPaths?.push(filepath);
excludedPaths = [...new Set(excludedPaths?.map(obj => obj))];

config.update("excludedPaths", excludedPaths, false);
}
};

const ignoreAllInstances = async (element: TfsecTreeItem, issueProvider: TfsecIssueProvider, outputChannel: vscode.OutputChannel) => {
outputChannel.show();
outputChannel.appendLine("\nSetting ignores - ");
Expand Down Expand Up @@ -189,4 +207,4 @@ const ignoreAllInstances = async (element: TfsecTreeItem, issueProvider: TfsecIs
};


export { ignoreAllInstances, ignoreInstance, triggerDecoration, IgnoreDetails, FileIgnores };
export { ignoreAllInstances, ignoreInstance, ingorePath, triggerDecoration, IgnoreDetails, FileIgnores };
8 changes: 8 additions & 0 deletions src/tfsec_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ export class TfsecWrapper {
command.push('--verbose');
}

const excludes = config.get<string[]>("excludedPaths");
if (excludes && excludes.length > 0) {
excludes.forEach((element: string) => {
command.push(`--exclude-path=${element}`);
});
}


// add soft fail for exit code
command.push('--soft-fail');
command.push('--format=json');
Expand Down

0 comments on commit 5a6acc6

Please sign in to comment.