From 55441c366d1ce801331b0ecc36eaad52227e3912 Mon Sep 17 00:00:00 2001 From: Nasirudeen Olohundare Date: Fri, 24 Jan 2025 16:54:12 +0100 Subject: [PATCH] feat: logs tracking --- src/index.ts | 20 +++++++++-------- src/shared/index.ts | 53 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0ac9eb2..6b001e7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -79,7 +79,7 @@ program .description("Scan secrets in a string") .action((options: ScanStringOptions) => scanString(options)); - program +program .command("decay") .argument("[data]", "Data to decay (optional if using --file)") .option("--config ", "Path to configuration file") @@ -87,26 +87,28 @@ program .description("Decay sensitive data from input or file") .action(async (data: string | undefined, options: DecayOptions) => { try { - const decayer = decay(options.config); let inputData: any; if (options.file) { inputData = readInputFile(options.file); } else if (data) { - inputData = data; + inputData = data; } else { - throw new Error("No input provided. Use --file or provide data directly."); + throw new Error( + "No input provided. Use --file or provide data directly." + ); } const redactedData = decayer.redact(inputData); - - console.log(typeof redactedData === 'object' - ? JSON.stringify(redactedData, null, 2) - : redactedData + + console.log( + typeof redactedData === "object" + ? JSON.stringify(redactedData, null, 2) + : redactedData ); } catch (error: any) { - console.error('Error:', error.message); + console.error("Error:", error.message); process.exit(1); } }); diff --git a/src/shared/index.ts b/src/shared/index.ts index 9cf3663..08b0380 100644 --- a/src/shared/index.ts +++ b/src/shared/index.ts @@ -3,12 +3,24 @@ * and other securelog libraries */ +import axios from "axios"; import { AhoCorasickCore } from "../ahocorasick"; +import { decay } from "../decay"; import { buildCustomDetectors } from "../regexHandler"; import { DataFormat, ScanStringOptions } from "../types"; import { DetectorConfig } from "../types/detector"; import { maskString } from "../util"; -import yaml from 'yaml'; +import yaml from "yaml"; + +const API_BASE_URL = "https://api.securelog.com"; + +const validateRequestApiKey = async (apikey: string) => { + try { + const { data } = await axios.get(`${API_BASE_URL}/auth`); + } catch (error) { + throw new Error("Invalid API Key"); + } +}; const handleCustomDetectors = (customDetectors?: DetectorConfig[]) => { const parsedDetectors = customDetectors?.length @@ -19,7 +31,13 @@ const handleCustomDetectors = (customDetectors?: DetectorConfig[]) => { return parsedDetectors; }; -export const redactSensitiveData = async (options: ScanStringOptions) => { +export const redactSensitiveData = async ( + apiKey: string, + options: ScanStringOptions +) => { + if (!apiKey) throw new Error("Please attach an API key"); + await validateRequestApiKey(apiKey); + const core = new AhoCorasickCore( handleCustomDetectors(options.customDetectors) ); @@ -44,7 +62,20 @@ export const redactSensitiveData = async (options: ScanStringOptions) => { }) ); - return { rawValue: modifiedValue, secrets }; + const maskedValues = modifiedValue; + const decayer = decay(); + const redactedValues = decayer.redact(maskedValues); + + try { + await axios.post(`${API_BASE_URL}/log-redacted-secret`, { + rawValue: redactedValues, + secrets, + }); + } catch (error) { + console.log("Error occured logging secrets"); + } + + return { rawValue: redactedValues, maskedValues, redactedValues, secrets }; }; export const scanStringAndReturnJson = async (options: ScanStringOptions) => { @@ -74,7 +105,7 @@ export class DataFormatHandlers { private registerDefaultFormats() { // JSON handler - this.formats.set('json', { + this.formats.set("json", { detect: (data: string) => { try { JSON.parse(data); @@ -84,11 +115,11 @@ export class DataFormatHandlers { } }, parse: JSON.parse, - stringify: (data: any) => JSON.stringify(data, null, 2) + stringify: (data: any) => JSON.stringify(data, null, 2), }); // YAML handler - this.formats.set('yaml', { + this.formats.set("yaml", { detect: (data: string) => { try { yaml.parse(data); @@ -98,20 +129,20 @@ export class DataFormatHandlers { } }, parse: yaml.parse, - stringify: yaml.stringify + stringify: yaml.stringify, }); // XML handler - this.formats.set('xml', { + this.formats.set("xml", { detect: (data: string) => /^\s*<[^>]+>/.test(data), parse: (data: string) => { const parser = new DOMParser(); - return parser.parseFromString(data, 'text/xml'); + return parser.parseFromString(data, "text/xml"); }, stringify: (data: any) => { const serializer = new XMLSerializer(); return serializer.serializeToString(data); - } + }, }); } @@ -121,7 +152,7 @@ export class DataFormatHandlers { return format; } } - return 'string'; + return "string"; } public getHandler(format: string): DataFormat | undefined {