Skip to content

Commit

Permalink
feat: logs tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
iamnasirudeen committed Jan 24, 2025
1 parent da33513 commit 55441c3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
20 changes: 11 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,34 +79,36 @@ 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 <string>", "Path to configuration file")
.option("--file <string>", "Path to input file containing data to decay")
.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);
}
});
Expand Down
53 changes: 42 additions & 11 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
);
Expand All @@ -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) => {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
},
});
}

Expand All @@ -121,7 +152,7 @@ export class DataFormatHandlers {
return format;
}
}
return 'string';
return "string";
}

public getHandler(format: string): DataFormat | undefined {
Expand Down

0 comments on commit 55441c3

Please sign in to comment.