mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
Zsh: check if safe-chain is installed before running it.
This commit is contained in:
parent
fe1ca396b4
commit
09300eade6
4 changed files with 177 additions and 45 deletions
|
|
@ -2,6 +2,10 @@ import chalk from "chalk";
|
||||||
import { ui } from "../environment/userInteraction.js";
|
import { ui } from "../environment/userInteraction.js";
|
||||||
import { detectShells } from "./shellDetection.js";
|
import { detectShells } from "./shellDetection.js";
|
||||||
import { knownAikidoTools } from "./helpers.js";
|
import { knownAikidoTools } from "./helpers.js";
|
||||||
|
import fs from "fs";
|
||||||
|
import os from "os";
|
||||||
|
import path from "path";
|
||||||
|
import { fileURLToPath } from "url";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loops over the detected shells and calls the setup function for each.
|
* Loops over the detected shells and calls the setup function for each.
|
||||||
|
|
@ -13,6 +17,8 @@ export async function setup() {
|
||||||
);
|
);
|
||||||
ui.emptyLine();
|
ui.emptyLine();
|
||||||
|
|
||||||
|
copyStartupFiles();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const shells = detectShells();
|
const shells = detectShells();
|
||||||
if (shells.length === 0) {
|
if (shells.length === 0) {
|
||||||
|
|
@ -72,3 +78,23 @@ function setupShell(shell) {
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function copyStartupFiles() {
|
||||||
|
const startupFiles = ["init-zsh.sh"];
|
||||||
|
|
||||||
|
for (const file of startupFiles) {
|
||||||
|
const targetPath = path.join(os.homedir(), ".safe-chain", "scripts", file);
|
||||||
|
|
||||||
|
// Create target directory if it doesn't exist
|
||||||
|
const targetDir = targetPath.substring(0, targetPath.lastIndexOf("/"));
|
||||||
|
if (!fs.existsSync(targetDir)) {
|
||||||
|
fs.mkdirSync(targetDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use absolute path for source
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
const sourcePath = path.resolve(__dirname, "startup-scripts", file);
|
||||||
|
fs.copyFileSync(sourcePath, targetPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
81
src/shell-integration/startup-scripts/init-zsh.sh
Normal file
81
src/shell-integration/startup-scripts/init-zsh.sh
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
|
||||||
|
function installIfCommandNotFound() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
# Check if the command already exists
|
||||||
|
if command -v "$cmd" > /dev/null 2>&1; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if Node.js version is below 18
|
||||||
|
# Safe-chain requires Node.js 18 or higher
|
||||||
|
local node_version=$(node -v | sed 's/v//' | cut -d'.' -f1)
|
||||||
|
if [ "$node_version" -lt 18 ]; then
|
||||||
|
return 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Command not found, ask user if they want to install safe-chain
|
||||||
|
printf "The command '%s' is not available. Do you want to install safe-chain to provide it? (y/N): " "$cmd"
|
||||||
|
read -r response
|
||||||
|
|
||||||
|
if [[ "$response" =~ ^[Yy]$ ]]; then
|
||||||
|
printf "Installing safe-chain...\n"
|
||||||
|
installSafeChain
|
||||||
|
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
printf "\nFailed to install safe-chain. Exiting.\n"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
printf "Skipping safe-chain installation. Using original command instead.\n"
|
||||||
|
return 2
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function installSafeChain() {
|
||||||
|
command npm install -g @aikidosec/safe-chain
|
||||||
|
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf "------\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
function wrapCommand() {
|
||||||
|
local original_cmd="$1"
|
||||||
|
local aikido_cmd="$2"
|
||||||
|
|
||||||
|
# Remove the first 2 arguments (original_cmd and aikido_cmd) from $@
|
||||||
|
# so that "$@" now contains only the arguments passed to the original command
|
||||||
|
shift 2
|
||||||
|
|
||||||
|
installIfCommandNotFound "$aikido_cmd"
|
||||||
|
local install_result=$?
|
||||||
|
if [ $install_result -eq 2 ]; then
|
||||||
|
command "$original_cmd" "$@"
|
||||||
|
else
|
||||||
|
"$aikido_cmd" "$@"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function npx() {
|
||||||
|
wrapCommand "npx" "aikido-npx" "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
function yarn() {
|
||||||
|
wrapCommand "yarn" "aikido-yarn" "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
function npm() {
|
||||||
|
if [[ "$1" == "-v" || "$1" == "--version" ]] && [[ $# -eq 1 ]]; then
|
||||||
|
# If args is just -v or --version and nothing else, just run the npm version command
|
||||||
|
# This is because nvm uses this to check the version of npm
|
||||||
|
command npm "$@"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
wrapCommand "npm" "aikido-npm" "$@"
|
||||||
|
}
|
||||||
|
|
@ -20,19 +20,23 @@ function teardown() {
|
||||||
// This will remove the safe-chain aliases for npm, npx, and yarn commands.
|
// This will remove the safe-chain aliases for npm, npx, and yarn commands.
|
||||||
removeLinesMatchingPattern(startupFile, /^alias\s+(npm|npx|yarn)=/);
|
removeLinesMatchingPattern(startupFile, /^alias\s+(npm|npx|yarn)=/);
|
||||||
|
|
||||||
|
// Removes the line that sources the safe-chain zsh initialization script (~/.aikido/scripts/init-zsh.sh)
|
||||||
|
removeLinesMatchingPattern(
|
||||||
|
startupFile,
|
||||||
|
/^source\s+~\/\.safe-chain\/scripts\/init-zsh\.sh/
|
||||||
|
);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setup(tools) {
|
function setup() {
|
||||||
const startupFile = execAndGetOutput(startupFileCommand, executableName);
|
const startupFile = execAndGetOutput(startupFileCommand, executableName);
|
||||||
teardown();
|
teardown();
|
||||||
|
|
||||||
for (const tool of tools) {
|
addLineToFile(
|
||||||
addLineToFile(
|
startupFile,
|
||||||
startupFile,
|
`source ~/.safe-chain/scripts/init-zsh.sh # Safe-chain Zsh initialization script`
|
||||||
`alias ${tool}="aikido-${tool}" # Safe-chain alias for ${tool}`
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -59,49 +59,40 @@ describe("Zsh shell integration", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("setup", () => {
|
describe("setup", () => {
|
||||||
it("should add aliases for all provided tools", () => {
|
it("should add source line for zsh initialization script", () => {
|
||||||
const tools = ["npm", "npx", "yarn"];
|
const result = zsh.setup();
|
||||||
|
|
||||||
const result = zsh.setup(tools);
|
|
||||||
assert.strictEqual(result, true);
|
assert.strictEqual(result, true);
|
||||||
|
|
||||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
assert.ok(
|
assert.ok(
|
||||||
content.includes('alias npm="aikido-npm" # Safe-chain alias for npm')
|
content.includes(
|
||||||
);
|
"source ~/.safe-chain/scripts/init-zsh.sh # Safe-chain Zsh initialization script"
|
||||||
assert.ok(
|
)
|
||||||
content.includes('alias npx="aikido-npx" # Safe-chain alias for npx')
|
|
||||||
);
|
|
||||||
assert.ok(
|
|
||||||
content.includes('alias yarn="aikido-yarn" # Safe-chain alias for yarn')
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should call teardown before setup", () => {
|
it("should call teardown before setup", () => {
|
||||||
// Pre-populate file with existing aliases
|
// Pre-populate file with existing source line
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
mockStartupFile,
|
mockStartupFile,
|
||||||
'alias npm="old-npm"\nalias npx="old-npx"\n',
|
"source ~/.safe-chain/scripts/init-zsh.sh\n",
|
||||||
"utf-8"
|
"utf-8"
|
||||||
);
|
);
|
||||||
|
|
||||||
const tools = ["npm"];
|
zsh.setup();
|
||||||
zsh.setup(tools);
|
|
||||||
|
|
||||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
assert.ok(!content.includes('alias npm="old-npm"'));
|
const sourceMatches = (content.match(/source.*init-zsh\.sh/g) || [])
|
||||||
assert.ok(content.includes('alias npm="aikido-npm"'));
|
.length;
|
||||||
|
assert.strictEqual(sourceMatches, 1, "Should not duplicate source lines");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle empty tools array", () => {
|
it("should handle empty startup file", () => {
|
||||||
const result = zsh.setup([]);
|
const result = zsh.setup();
|
||||||
assert.strictEqual(result, true);
|
assert.strictEqual(result, true);
|
||||||
|
|
||||||
// File should be created during teardown call even if no tools are provided
|
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
if (fs.existsSync(mockStartupFile)) {
|
assert.ok(content.includes("source ~/.safe-chain/scripts/init-zsh.sh"));
|
||||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
|
||||||
assert.strictEqual(content.trim(), "");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -129,6 +120,23 @@ describe("Zsh shell integration", () => {
|
||||||
assert.ok(content.includes("alias grep="));
|
assert.ok(content.includes("alias grep="));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should remove zsh initialization script source line", () => {
|
||||||
|
const initialContent = [
|
||||||
|
"#!/bin/zsh",
|
||||||
|
"source ~/.safe-chain/scripts/init-zsh.sh",
|
||||||
|
"alias ls='ls --color=auto'",
|
||||||
|
].join("\n");
|
||||||
|
|
||||||
|
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||||
|
|
||||||
|
const result = zsh.teardown();
|
||||||
|
assert.strictEqual(result, true);
|
||||||
|
|
||||||
|
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
|
assert.ok(!content.includes("source ~/.safe-chain/scripts/init-zsh.sh"));
|
||||||
|
assert.ok(content.includes("alias ls="));
|
||||||
|
});
|
||||||
|
|
||||||
it("should handle file that doesn't exist", () => {
|
it("should handle file that doesn't exist", () => {
|
||||||
if (fs.existsSync(mockStartupFile)) {
|
if (fs.existsSync(mockStartupFile)) {
|
||||||
fs.unlinkSync(mockStartupFile);
|
fs.unlinkSync(mockStartupFile);
|
||||||
|
|
@ -138,7 +146,7 @@ describe("Zsh shell integration", () => {
|
||||||
assert.strictEqual(result, true);
|
assert.strictEqual(result, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle file with no relevant aliases", () => {
|
it("should handle file with no relevant aliases or source lines", () => {
|
||||||
const initialContent = [
|
const initialContent = [
|
||||||
"#!/bin/zsh",
|
"#!/bin/zsh",
|
||||||
"alias ls='ls --color=auto'",
|
"alias ls='ls --color=auto'",
|
||||||
|
|
@ -171,30 +179,43 @@ describe("Zsh shell integration", () => {
|
||||||
|
|
||||||
describe("integration tests", () => {
|
describe("integration tests", () => {
|
||||||
it("should handle complete setup and teardown cycle", () => {
|
it("should handle complete setup and teardown cycle", () => {
|
||||||
const tools = ["npm", "yarn"];
|
|
||||||
|
|
||||||
// Setup
|
// Setup
|
||||||
zsh.setup(tools);
|
zsh.setup();
|
||||||
let content = fs.readFileSync(mockStartupFile, "utf-8");
|
let content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
assert.ok(content.includes('alias npm="aikido-npm"'));
|
assert.ok(content.includes("source ~/.safe-chain/scripts/init-zsh.sh"));
|
||||||
assert.ok(content.includes('alias yarn="aikido-yarn"'));
|
|
||||||
|
|
||||||
// Teardown
|
// Teardown
|
||||||
zsh.teardown();
|
zsh.teardown();
|
||||||
content = fs.readFileSync(mockStartupFile, "utf-8");
|
content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
assert.ok(!content.includes("alias npm="));
|
assert.ok(!content.includes("source ~/.safe-chain/scripts/init-zsh.sh"));
|
||||||
assert.ok(!content.includes("alias yarn="));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle multiple setup calls", () => {
|
it("should handle multiple setup calls", () => {
|
||||||
const tools = ["npm"];
|
zsh.setup();
|
||||||
|
zsh.setup();
|
||||||
zsh.setup(tools);
|
|
||||||
zsh.setup(tools);
|
|
||||||
|
|
||||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
const npmMatches = (content.match(/alias npm="/g) || []).length;
|
const sourceMatches = (content.match(/source.*init-zsh\.sh/g) || [])
|
||||||
assert.strictEqual(npmMatches, 1, "Should not duplicate aliases");
|
.length;
|
||||||
|
assert.strictEqual(sourceMatches, 1, "Should not duplicate source lines");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle mixed content with aliases and source lines", () => {
|
||||||
|
const initialContent = [
|
||||||
|
"#!/bin/zsh",
|
||||||
|
"alias npm='old-npm'",
|
||||||
|
"source ~/.safe-chain/scripts/init-zsh.sh",
|
||||||
|
"alias ls='ls --color=auto'",
|
||||||
|
].join("\n");
|
||||||
|
|
||||||
|
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||||
|
|
||||||
|
// Teardown should remove both aliases and source line
|
||||||
|
zsh.teardown();
|
||||||
|
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
|
assert.ok(!content.includes("alias npm="));
|
||||||
|
assert.ok(!content.includes("source ~/.safe-chain/scripts/init-zsh.sh"));
|
||||||
|
assert.ok(content.includes("alias ls="));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue