Skip to content

Commit

Permalink
Patch: made the source code of bashaway testing utils public
Browse files Browse the repository at this point in the history
  • Loading branch information
Akalanka47000 committed Oct 22, 2023
1 parent 4f0afc2 commit 84b27c3
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 4 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,4 @@ p*/**/jest.config.js
p*/**/LICENSE

p*/**/node_modules/
p*/**/logs/.turbo

packages/bashaway/src/_
p*/**/logs/.turbo
9 changes: 9 additions & 0 deletions packages/bashaway/src/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import exec from "@sliit-foss/actions-exec-wrapper";

const countLines = (str) => str.split(/\r\n|\r|\n/).length;

export const dependencyCount = () => exec("npm ls --parseable").then((output) => countLines(output) - 2);

export const globalDependencyCount = () => exec("npm ls -g --parseable").then((output) => countLines(output) - 2);

export const prohibitedCommands = /(npm|pnpm|yarn|npx)\s+(i|install|add|exec|dlx)\s+\S+/g;
31 changes: 31 additions & 0 deletions packages/bashaway/src/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import exec from "@sliit-foss/actions-exec-wrapper";
import axios from "axios";

export const commitList = async (urlOrPath) => {
if (urlOrPath?.includes("https://")) {
await exec(`git clone ${urlOrPath} out`);
process.chdir("out");
} else {
process.chdir(urlOrPath);
}
const commits = await exec(`git log --pretty=format:"%h (%cd) %s|||%cn|||%ce|||%an|||%ae" --date=iso`);
return commits
?.split("\n")
?.filter((c) => c)
?.map((commit) => {
const [, date, time, offset, ...info] = commit?.split(" ");
const [message, commiterName, commiterEmail, authorName, authorEmail] = info?.join(" ")?.split("|||");
return {
message,
timestamp: new Date(`${date.replace("(", "")} ${time} ${offset.replace(")", "")}`).getTime(),
commiterName,
commiterEmail,
authorName,
authorEmail
};
});
};

export const ghOrgRepos = async (org) => {
return axios.get(`https://api.github.com/orgs/${org}/repos?per_page=1000`).then((res) => res.data);
};
18 changes: 17 additions & 1 deletion packages/bashaway/src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
import { compactString, functionCallFollowedByAnd, functionCalls } from "./patterns";

export * from "./commands";
export * from "./git";
export * from "./patterns";
export * from "./restrict";
export * from "./scan";
export * from "./_";
export * from "./secrets";

export const cleanLogs = (code) =>
compactString(
code.replace(
code.includes("console.log")
? functionCallFollowedByAnd("console.log")
: functionCalls(["print", "System.out.println", "Console.WriteLine"]),
""
)
);
13 changes: 13 additions & 0 deletions packages/bashaway/src/patterns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const functionCall = (fn) => new RegExp(`${fn}\\([^)]*\\);?`, "g");

export const functionCallFollowedByAnd = (fn) => new RegExp(`${fn}\\([^)]*\\);?(\\s*&&\\s*)?`, "g");

export const functionCalls = (fns) => new RegExp(`(${fns.join("|")})\\([^)]*\\);?`, "g");

export const compactString = (str) =>
str
.replace(/\/\/.*\n/g, "")
.replace(/\s/g, "")
.trim();

export const isStrongPassword = (str) => str.match(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/);
12 changes: 12 additions & 0 deletions packages/bashaway/src/restrict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const restrictJavascript = (script) => {
expect(script).not.toContain("node --eval");
expect(script).not.toContain("node -e");
expect(script).not.toContain("deno run");
expect(script).not.toContain("bun");
};

export const restrictPython = (script) => {
expect(script).not.toContain("python");
expect(script).not.toContain("python3");
expect(script).not.toContain("pip install");
};
30 changes: 30 additions & 0 deletions packages/bashaway/src/secrets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const generateSecrets = (content, service) => {
const services = content
.split("---")
?.filter((service) => !service.split("\n")?.every((svc) => ["\r", "", " "].includes(svc)))
.map((service) => {
const lines = service.split("\n");
const keyStartIndex = lines.indexOf(lines.find((l) => l.includes("stringData:")));
const keys = lines.reduce((acc, line, index) => {
if (index > keyStartIndex && !line.trim()?.startsWith("#") && line.includes(":") && line.split(":")?.[1]) {
let key = line.split(":").splice(1).join(":").trim();
if (key.startsWith('"') && key.endsWith('"')) {
key = key.substring(1, key.length - 1);
}
acc[line.split(":")[0]?.trim()] = key;
}
return acc;
}, {});
return {
name: lines
.find((l) => l.includes("name:"))
?.split(":")[1]
.trim(),
keys
};
});
const serviceKeys = services.find((s) => s.name === `${service}-secret`)?.keys;
return Object.keys(serviceKeys)
.map((key) => `${key}=${serviceKeys[key]}`)
?.join("\n");
};

0 comments on commit 84b27c3

Please sign in to comment.