Skip to content

Commit

Permalink
add more logging and check for tfsec before runs
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen Rumney committed Jan 12, 2022
1 parent 79efe99 commit af94f76
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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

### 1.5.0
- Check for tfsec before running any commands
- Add debug setting for richer output option
- remove some redundant logging

### 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ Ignore codes will be automatically resolved and the description of the error wil

## Release Notes

### 1.5.0
- Check for tfsec before running any commands
- Add debug setting for richer output option
- remove some redundant logging

### 1.4.1
- Fix updater

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
"type": "boolean",
"default": "true",
"description": "Automatically rerun tfsec when a check failure is ignored"
},
"tfsec.debug": {
"type": "boolean",
"default": "false",
"description": "Run tfsec with vebose flag to get more information"
}
}
},
Expand Down
1 change: 0 additions & 1 deletion src/explorer/issues_treeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class TfsecIssueProvider implements vscode.TreeDataProvider<TfsecTreeItem
refresh(): void {
this.taintResults = true;
this._onDidChangeTreeData.fire();
vscode.window.showInformationMessage("tfsec results updated");
}

// when there is a tfsec output file, load the results
Expand Down
36 changes: 30 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from 'vscode';
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';
import { getInstalledTfsecVersion, getBinaryPath, checkTfsecInstalled } from './utils';
import { TfsecHelpProvider } from './explorer/check_helpview';
import * as semver from 'semver';
import * as child from 'child_process';
Expand Down Expand Up @@ -30,7 +30,7 @@ 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.version', () => showCurrentTfsecVersion(outputChannel)));
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)));
Expand All @@ -53,7 +53,9 @@ export function activate(context: vscode.ExtensionContext) {
if (activeEditor) {
triggerDecoration();
}
showCurrentTfsecVersion();


showCurrentTfsecVersion(outputChannel);
}

function ignoreInstance(element: TfsecTreeItem, outputChannel: vscode.OutputChannel) {
Expand Down Expand Up @@ -119,7 +121,11 @@ async function ignoreAllInstances(element: TfsecTreeItem, issueProvider: TfsecIs
}


function showCurrentTfsecVersion() {
function showCurrentTfsecVersion(outputChannel: vscode.OutputChannel) {
if (!checkTfsecInstalled(outputChannel)) {
vscode.window.showErrorMessage("tfsec could not be found, check Output window");
return;
}
const currentVersion = getInstalledTfsecVersion();
if (currentVersion) {
vscode.window.showInformationMessage(`Current tfsec version is ${currentVersion}`);
Expand All @@ -139,6 +145,10 @@ function buildCommand(resultsStoragePath: string, scanPath: string) {
command.push('--exclude-downloaded-modules');
}

if (config.get<boolean>('debug')) {
command.push('--verbose');
}

command.push('--format json');
command.push(`--out "${resultsStoragePath}"`);
command.push(scanPath);
Expand All @@ -151,16 +161,27 @@ function runTfsec(issueProvider: TfsecIssueProvider, outputChannel: vscode.Outpu
outputChannel.appendLine("");
outputChannel.appendLine("Running tfsec to update results");

if (!checkTfsecInstalled(outputChannel)) {
return;
}

if (vscode.workspace && vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0
&& vscode.workspace.workspaceFolders[0] !== undefined) {

let command = buildCommand(issueProvider.resultsStoragePath, vscode.workspace.workspaceFolders[0].uri.fsPath);
outputChannel.appendLine(`command: ${command}`);
try {
let result: Buffer = child.execSync(command);

outputChannel.appendLine(result.toString());
} catch (err) {
let errMsg = (err as Error);
const config = vscode.workspace.getConfiguration('tfsec');
if (config.get<boolean>('debug')) {
outputChannel.appendLine(errMsg.toString());
}

} finally {
outputChannel.appendLine("Reloading the treeview");
setTimeout(() => { vscode.commands.executeCommand("tfsec.refresh"); }, 250);
}
}
Expand All @@ -170,9 +191,12 @@ function updateBinary(outputChannel: vscode.OutputChannel) {
outputChannel.show();
outputChannel.appendLine("");
outputChannel.appendLine("Checking the current version");
const currentVersion = getInstalledTfsecVersion();

if (!checkTfsecInstalled(outputChannel)) {
return;
}

const currentVersion = getInstalledTfsecVersion();
if (currentVersion.includes("running a locally built version")) {
outputChannel.appendLine("You are using a locally built version which cannot be updated");
}
Expand Down
14 changes: 12 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as vscode from 'vscode';
import * as child from 'child_process';
import { TfsecTreeItem } from './explorer/tfsec_treeitem';
import { existsSync } from 'fs';


function getSeverityPosition(severity: string): number {
Expand Down Expand Up @@ -83,7 +84,16 @@ const getBinaryPath = () => {
binary = "tfsec";
}
return binary;
}
};

const checkTfsecInstalled = (outputChannel: vscode.OutputChannel): boolean => {
const binaryPath = getBinaryPath();

if (!existsSync(binaryPath)) {
outputChannel.appendLine(`tfsec not found. Check the tfsec extension settings to ensure the path is correct. [${binaryPath}]`);
}
return true;
};

const getInstalledTfsecVersion = () => {
let binary = getBinaryPath();
Expand All @@ -97,4 +107,4 @@ const getInstalledTfsecVersion = () => {

const capitalize = (s: string) => (s && s[0] && s[0].toUpperCase() + s.slice(1).toLowerCase()) || "";

export { getBinaryPath, sortByCode, sortBySeverity, uniqueLocations, getInstalledTfsecVersion, capitalize };
export { getBinaryPath, sortByCode, sortBySeverity, uniqueLocations, getInstalledTfsecVersion, capitalize, checkTfsecInstalled };

0 comments on commit af94f76

Please sign in to comment.