Skip to content

Commit

Permalink
added reset command
Browse files Browse the repository at this point in the history
  • Loading branch information
rajpreet-s committed Oct 10, 2024
1 parent 649fc4b commit 938eec1
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 37 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@
"title": "Retrieve access token",
"category": "debricked",
"icon": "$(account)"
},
{
"command": "debricked.debricked.reset",
"title": "Reset Debricked",
"category": "debricked",
"icon": "$(trash)"
}
],
"menus": {
Expand Down
1 change: 1 addition & 0 deletions src/commands/debrickedCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class DebrickedCommand {
this.registerCommand(context, baseSubCommands[3].command, Logger.openLogFile);
this.registerCommand(context, baseSubCommands[4].command, baseCommandService.login);
this.registerCommand(context, baseSubCommands[5].command, SentryHelper.reConfigureSentry);
this.registerCommand(context, baseSubCommands[6].command, baseCommandService.reset);
}

//Register auth sub-commands
Expand Down
6 changes: 6 additions & 0 deletions src/constants/debricked_cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ export class DebrickedCommands {
cli_command: "sentry",
description: "Debricked sentry logs",
},
{
label: "Reset",
command: "debricked.debricked.reset",
cli_command: "reset",
description: "Reset Debricked",
},
],
flags: [
{
Expand Down
2 changes: 1 addition & 1 deletion src/constants/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export enum Environment {
TEST = "test",
}

export enum TokenType {
export enum Secrets {
ACCESS = "access",
BEARER = "bearer",
}
Expand Down
4 changes: 2 additions & 2 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DebrickedCommands } from "./debricked_cli";
import { Messages } from "./messages";
import { MessageStatus, Environment, TokenType, SupportedFilesToScan } from "./enums";
import { MessageStatus, Environment, Secrets, SupportedFilesToScan } from "./enums";
import { Organization } from "./organization";
import { SecondService } from "./secondService";
import { Regex } from "./regex";
Expand All @@ -17,7 +17,7 @@ export {
PolicyRules,
PolicyTriggerEvents,
Environment,
TokenType,
Secrets,
Icons,
SupportedFilesToScan,
};
1 change: 1 addition & 0 deletions src/constants/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export class Messages {
static readonly INSTALLATION_ERROR = "Installation script execution failed.";

// Miscellaneous Messages
static readonly RESET_SUCCESS = "Successfully reset debricked.";
}
3 changes: 1 addition & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export async function activate(context: vscode.ExtensionContext) {
progress.report({ increment: progressCount });

const globalState = globalStore.getGlobalStateInstance();
// For dev - Clears the globalData - uncomment to clear the globalData
// await globalState?.clearAllGlobalData();

progress.report({
message: "Activating VS Code Extension",
increment: (progressCount += 20),
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosR
import { AuthHelper } from "./authHelper";
import { ErrorHandler } from "./errorHandler";
import { Logger } from "./loggerHelper";
import { TokenType } from "../constants";
import { Secrets } from "../constants";
import * as Sentry from "@sentry/node";

export class ApiClient {
Expand All @@ -17,7 +17,7 @@ export class ApiClient {

this.axiosInstance.interceptors.request.use(
async (config: AxiosRequestConfig): Promise<InternalAxiosRequestConfig> => {
const token = await authHelper.getToken(true, TokenType.BEARER);
const token = await authHelper.getToken(true, Secrets.BEARER);
if (token) {
config.headers = {
...config.headers,
Expand Down
16 changes: 8 additions & 8 deletions src/helpers/authHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Messages, TokenType } from "../constants/index";
import { Messages, Secrets } from "../constants/index";
import { ShowInputBoxHelper } from "./showInputBoxHelper";
import { StatusBarMessageHelper } from "./statusBarMessageHelper";
import { Logger } from "./loggerHelper";
Expand All @@ -17,7 +17,7 @@ export class AuthHelper {
* @param void
* @returns Promise<string | undefined>
*/
async getToken(useDefaultToken = true, tokenKey: TokenType): Promise<string | undefined> {
async getToken(useDefaultToken = true, tokenKey: Secrets): Promise<string | undefined> {
try {
let token: string | undefined;
const defaultToken: any = await this.globalStore.getGlobalStateInstance()?.getSecretData(tokenKey);
Expand All @@ -31,12 +31,12 @@ export class AuthHelper {
this.logger.logInfo("InputBox Opened for tokens");

token = await this.showInputBoxHelper.promptForInput({
prompt: tokenKey === TokenType.ACCESS ? Messages.ENTER_ACCESS_TOKEN : Messages.ENTER_BEARER_TOKEN,
prompt: tokenKey === Secrets.ACCESS ? Messages.ENTER_ACCESS_TOKEN : Messages.ENTER_BEARER_TOKEN,
ignoreFocusOut: true,
password: true,
title: tokenKey === TokenType.ACCESS ? Messages.ACCESS_TOKEN : Messages.BEARER_TOKEN,
title: tokenKey === Secrets.ACCESS ? Messages.ACCESS_TOKEN : Messages.BEARER_TOKEN,
placeHolder:
tokenKey === TokenType.ACCESS ? Messages.ENTER_ACCESS_TOKEN : Messages.ENTER_BEARER_TOKEN,
tokenKey === Secrets.ACCESS ? Messages.ENTER_ACCESS_TOKEN : Messages.ENTER_BEARER_TOKEN,
});

this.setToken(tokenKey, token);
Expand All @@ -49,13 +49,13 @@ export class AuthHelper {
}
}

async setToken(tokenKey: TokenType, token: string | undefined): Promise<void> {
async setToken(tokenKey: Secrets, token: string | undefined): Promise<void> {
if (token) {
await this.globalStore.getGlobalStateInstance()?.setSecretData(tokenKey, token);
const message = tokenKey === TokenType.ACCESS ? Messages.ACCESS_TOKEN_SAVED : Messages.BEARER_TOKEN_SAVED;
const message = tokenKey === Secrets.ACCESS ? Messages.ACCESS_TOKEN_SAVED : Messages.BEARER_TOKEN_SAVED;
this.statusBarMessageHelper.showInformationMessage(message);
} else {
const message = tokenKey === TokenType.ACCESS ? Messages.ACCESS_TOKEN_RQD : Messages.BEARER_TOKEN_RQD;
const message = tokenKey === Secrets.ACCESS ? Messages.ACCESS_TOKEN_RQD : Messages.BEARER_TOKEN_RQD;
throw new Error(message);
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/helpers/commandHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DebrickedCommands, Messages, MessageStatus, TokenType } from "../constants/index";
import { DebrickedCommands, Messages, MessageStatus, Secrets } from "../constants/index";
import { exec } from "child_process";
import * as vscode from "vscode";
import { promisify } from "util";
Expand Down Expand Up @@ -31,7 +31,7 @@ export class Command {

if (accessTokenRequired) {
const globalFlags = DebrickedCommands.getCommandSpecificFlags("Debricked", true) || [];
const accessToken = await this.authHelper.getToken(true, TokenType.ACCESS);
const accessToken = await this.authHelper.getToken(true, Secrets.ACCESS);

if (accessToken) {
this.logger.logMessageByStatus(
Expand All @@ -56,7 +56,8 @@ export class Command {
}
span.end(new Date());
return stdout.trim();
} catch (error) {
} catch (error: any) {
this.logger.logMessageByStatus(MessageStatus.ERROR, `command error: ${error.message}`);
this.logger.logError("Error in executeAsyncCommand");
throw error;
}
Expand Down
8 changes: 8 additions & 0 deletions src/helpers/globalState.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Secrets } from "../constants";
import * as vscode from "vscode";

export class GlobalState {
Expand Down Expand Up @@ -59,4 +60,11 @@ export class GlobalState {
public deleteSecretData(key: string): Thenable<void> {
return this.context.secrets.delete(key);
}

public async resetDebrickedData(): Promise<void> {
for (const secret in Secrets) {
this.context.secrets.delete(Secrets[secret as keyof typeof Secrets]);
}
await this.clearAllGlobalData();
}
}
4 changes: 4 additions & 0 deletions src/helpers/statusBarMessageHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export class StatusBarMessageHelper {
return await vscode.window.showInformationMessage(`${Organization.nameCaps}: ` + message, ...items);
}

public async showWarningMessageWithItems(message: string, items: string[]): Promise<string | undefined> {
return await vscode.window.showWarningMessage(`${Organization.nameCaps}: ` + message, ...items);
}

public showWarningMessage(message: string): void {
vscode.window.showWarningMessage(`${Organization.nameCaps}: ` + message);
}
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/terminalHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DebrickedCommands, Messages, MessageStatus, Organization, TokenType } from "../constants/index";
import { DebrickedCommands, Messages, MessageStatus, Organization, Secrets } from "../constants/index";
import { AuthHelper } from "./authHelper";
import { Logger } from "./loggerHelper";

Expand All @@ -19,7 +19,7 @@ export class Terminal {
let command: string = Organization.debrickedCli;
if (accessTokenRequired) {
const flags = DebrickedCommands.getCommandSpecificFlags("Debricked") || [];
const accessToken = await this.authHelper.getToken(useDefaultAccessToken, TokenType.ACCESS);
const accessToken = await this.authHelper.getToken(useDefaultAccessToken, Secrets.ACCESS);

if (accessToken) {
this.logger.logMessageByStatus(
Expand Down
32 changes: 25 additions & 7 deletions src/services/baseCommandService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DebrickedCommandNode } from "../types";
import { DebrickedCommands, Messages, MessageStatus, Organization, TokenType } from "../constants/index";
import { DebrickedCommands, Messages, MessageStatus, Organization, Secrets } from "../constants/index";
import {
statusBarMessageHelper,
terminal,
Expand Down Expand Up @@ -108,8 +108,8 @@ export class BaseCommandService {

public async installCommand() {
try {
Logger.logMessageByStatus(MessageStatus.INFO, "Register InstallCommand");
SentryHelper.setTransactionName("Install CLI");
Logger.logMessageByStatus(MessageStatus.INFO, "Register InstallCommand");

const currentVersion = await this.getCurrentExtensionVersion();
Logger.logMessageByStatus(
Expand Down Expand Up @@ -140,8 +140,8 @@ export class BaseCommandService {

public async login(updateCredentials = true) {
try {
Logger.logInfo("Register login");
SentryHelper.setTransactionName("Login");
Logger.logInfo("Register login");

const debrickedData: any = await globalStore
.getGlobalStateInstance()
Expand Down Expand Up @@ -177,7 +177,7 @@ export class BaseCommandService {
throw new Error(bearerToken.message);
} else {
const newBearerToken = `Bearer ${bearerToken.token}`;
await authHelper.setToken(TokenType.BEARER, newBearerToken);
await authHelper.setToken(Secrets.BEARER, newBearerToken);

Logger.logInfo(`Login successful. Authentication Bearer token generated for secure access.`);
}
Expand All @@ -193,8 +193,8 @@ export class BaseCommandService {

public async updateCommand() {
try {
Logger.logMessageByStatus(MessageStatus.INFO, "Register UpdateCommand");
SentryHelper.setTransactionName("Update Token");
Logger.logMessageByStatus(MessageStatus.INFO, "Register UpdateCommand");
let subCommand: DebrickedCommandNode[] | undefined;
if (DebrickedCommands.BASE_COMMAND.sub_commands) {
subCommand = DebrickedCommands.BASE_COMMAND.sub_commands[1].sub_commands;
Expand All @@ -206,10 +206,10 @@ export class BaseCommandService {
}
switch (selectedSubCommand?.cli_command) {
case "accessToken":
authHelper.getToken(false, TokenType.ACCESS);
authHelper.getToken(false, Secrets.ACCESS);
break;
case "bearerToken":
authHelper.getToken(false, TokenType.BEARER);
authHelper.getToken(false, Secrets.BEARER);
break;
}
} catch (error: any) {
Expand All @@ -221,6 +221,24 @@ export class BaseCommandService {
}
}

public async reset() {
try {
SentryHelper.setTransactionName("Reset Debricked");
Logger.logMessageByStatus(MessageStatus.INFO, "Register ResetCommand");

const response = await statusBarMessageHelper.showWarningMessageWithItems(
"Do you want to reset Debricked?",
["Yes", "No"],
);
if (response === "Yes") {
await globalStore.getGlobalStateInstance()?.resetDebrickedData();
statusBarMessageHelper.showInformationMessage(Messages.RESET_SUCCESS);
SentryHelper.captureMessage("Reset Debricked");
}
} catch (error: any) {
errorHandler.handleError(error);
}
}
public async getCurrentExtensionVersion(): Promise<string> {
const extension = vscode.extensions.getExtension(`${Organization.name}.${Organization.packageJson.name}`);
return extension ? extension.packageJSON.version : Organization.packageJson.version;
Expand Down
4 changes: 2 additions & 2 deletions src/services/scanService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
debrickedServiceHelper,
showQuickPickHelper,
} from "../helpers";
import { DebrickedCommands, Icons, MessageStatus, Organization, SecondService, TokenType } from "../constants/index";
import { DebrickedCommands, Icons, MessageStatus, Organization, SecondService, Secrets } from "../constants/index";
import { DebrickedCommandNode, Flag, Repository, RepositoryInfo } from "../types";
import * as vscode from "vscode";
import * as fs from "fs";
Expand Down Expand Up @@ -186,7 +186,7 @@ export class ScanService {
break;

case "-t": {
const accessToken = await authHelper.getToken(true, TokenType.ACCESS);
const accessToken = await authHelper.getToken(true, Secrets.ACCESS);
if (accessToken) {
cmdParams.push(accessToken);
Logger.logMessageByStatus(MessageStatus.INFO, "Access token added");
Expand Down
16 changes: 8 additions & 8 deletions src/test/helpers/authHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AuthHelper } from "../../helpers/authHelper";
import { Logger, showInputBoxHelper, statusBarMessageHelper } from "../../helpers";
import { sinon, expect } from "../setup";
import { Messages, TokenType } from "../../constants";
import { Messages, Secrets } from "../../constants";

describe("Authorization Helper", () => {
let authHelper: AuthHelper;
Expand Down Expand Up @@ -37,12 +37,12 @@ describe("Authorization Helper", () => {
sandbox.restore();
});

const tokenTypes = [
{ type: TokenType.ACCESS, message: Messages.ACCESS_TOKEN_SAVED, promptMessage: Messages.ENTER_ACCESS_TOKEN },
{ type: TokenType.BEARER, message: Messages.BEARER_TOKEN_SAVED, promptMessage: Messages.ENTER_BEARER_TOKEN },
const secrets = [
{ type: Secrets.ACCESS, message: Messages.ACCESS_TOKEN_SAVED, promptMessage: Messages.ENTER_ACCESS_TOKEN },
{ type: Secrets.BEARER, message: Messages.BEARER_TOKEN_SAVED, promptMessage: Messages.ENTER_BEARER_TOKEN },
];

tokenTypes.forEach(({ type, message, promptMessage }) => {
secrets.forEach(({ type, message, promptMessage }) => {
describe(`${type} token`, () => {
it(`should use default ${type} token`, async () => {
const token = await authHelper.getToken(true, type);
Expand All @@ -62,7 +62,7 @@ describe("Authorization Helper", () => {
expect(promptForInputStub.calledOnce).to.be.true;
expect(promptForInputStub.firstCall.args[0]).to.include({
prompt: promptMessage,
title: type === TokenType.ACCESS ? Messages.ACCESS_TOKEN : Messages.BEARER_TOKEN,
title: type === Secrets.ACCESS ? Messages.ACCESS_TOKEN : Messages.BEARER_TOKEN,
placeHolder: promptMessage,
});
expect(token).to.equal(NEW_TOKEN);
Expand All @@ -86,9 +86,9 @@ describe("Authorization Helper", () => {

describe("setToken", () => {
it("should set the token and show a success message", async () => {
await authHelper.setToken(TokenType.ACCESS, NEW_TOKEN);
await authHelper.setToken(Secrets.ACCESS, NEW_TOKEN);

expect(globalStateInstance.setSecretData.calledOnceWith(TokenType.ACCESS, NEW_TOKEN)).to.be.true;
expect(globalStateInstance.setSecretData.calledOnceWith(Secrets.ACCESS, NEW_TOKEN)).to.be.true;
expect(statusBarMessageHelperStub.calledOnceWith(Messages.ACCESS_TOKEN_SAVED)).to.be.true;
});
});
Expand Down

0 comments on commit 938eec1

Please sign in to comment.