|
| 1 | +--- |
| 2 | +description: Install gitleaks for detecting secrets in code |
| 3 | +--- |
| 4 | + |
| 5 | +## Name |
| 6 | +utils:install-gitleaks |
| 7 | + |
| 8 | +## Synopsis |
| 9 | +``` |
| 10 | +/utils:install-gitleaks |
| 11 | +``` |
| 12 | + |
| 13 | +## Description |
| 14 | +The `utils:install-gitleaks` command installs [gitleaks](https://github.com/gitleaks/gitleaks), a SAST tool for detecting and preventing hardcoded secrets like passwords, API keys, and tokens in git repositories and files. |
| 15 | + |
| 16 | +Gitleaks is essential for verifying that sensitive information has been properly redacted before committing code or sharing files. |
| 17 | + |
| 18 | +## Implementation |
| 19 | + |
| 20 | +### Step 1: Check if Gitleaks is Already Installed |
| 21 | + |
| 22 | +```bash |
| 23 | +which gitleaks |
| 24 | +``` |
| 25 | + |
| 26 | +**Expected outcomes:** |
| 27 | +- **Path returned** (e.g., `/usr/local/bin/gitleaks`): Gitleaks is already installed |
| 28 | + - Check version: `gitleaks version` |
| 29 | + - If version is recent (8.0+), skip to Step 4 |
| 30 | + - If version is old, proceed to Step 2 |
| 31 | +- **Command not found**: Proceed to Step 2 |
| 32 | + |
| 33 | +### Step 2: Detect Operating System and Architecture |
| 34 | + |
| 35 | +Determine the user's platform to download the correct binary: |
| 36 | + |
| 37 | +```bash |
| 38 | +uname -s |
| 39 | +uname -m |
| 40 | +``` |
| 41 | + |
| 42 | +**Common platform mappings:** |
| 43 | +- `Darwin` + `x86_64` → macOS Intel (`darwin_x64`) |
| 44 | +- `Darwin` + `arm64` → macOS Apple Silicon (`darwin_arm64`) |
| 45 | +- `Linux` + `x86_64` → Linux AMD64 (`linux_x64`) |
| 46 | +- `Linux` + `aarch64` → Linux ARM64 (`linux_arm64`) |
| 47 | + |
| 48 | +### Step 3: Install Gitleaks |
| 49 | + |
| 50 | +Choose the appropriate installation method based on the platform: |
| 51 | + |
| 52 | +#### Option A: macOS (using Homebrew - Recommended) |
| 53 | + |
| 54 | +```bash |
| 55 | +# Check if Homebrew is installed |
| 56 | +which brew |
| 57 | + |
| 58 | +# If Homebrew is available: |
| 59 | +brew install gitleaks |
| 60 | +``` |
| 61 | + |
| 62 | +#### Option B: macOS (manual installation) |
| 63 | + |
| 64 | +```bash |
| 65 | +# Determine architecture |
| 66 | +ARCH=$(uname -m) |
| 67 | +if [ "$ARCH" = "arm64" ]; then |
| 68 | + PLATFORM="darwin_arm64" |
| 69 | +else |
| 70 | + PLATFORM="darwin_x64" |
| 71 | +fi |
| 72 | + |
| 73 | +# Download latest release |
| 74 | +VERSION="8.20.1" # Check https://github.com/gitleaks/gitleaks/releases for latest |
| 75 | +curl -sSfL "https://github.com/gitleaks/gitleaks/releases/download/v${VERSION}/gitleaks_${VERSION}_${PLATFORM}.tar.gz" -o gitleaks.tar.gz |
| 76 | + |
| 77 | +# Extract and install |
| 78 | +tar -xzf gitleaks.tar.gz gitleaks |
| 79 | +sudo mv gitleaks /usr/local/bin/gitleaks |
| 80 | +sudo chmod +x /usr/local/bin/gitleaks |
| 81 | + |
| 82 | +# Clean up |
| 83 | +rm gitleaks.tar.gz |
| 84 | +``` |
| 85 | + |
| 86 | +#### Option C: Linux (manual installation) |
| 87 | + |
| 88 | +```bash |
| 89 | +# Determine architecture |
| 90 | +ARCH=$(uname -m) |
| 91 | +if [ "$ARCH" = "aarch64" ]; then |
| 92 | + PLATFORM="linux_arm64" |
| 93 | +else |
| 94 | + PLATFORM="linux_x64" |
| 95 | +fi |
| 96 | + |
| 97 | +# Download latest release |
| 98 | +VERSION="8.20.1" # Check https://github.com/gitleaks/gitleaks/releases for latest |
| 99 | +curl -sSfL "https://github.com/gitleaks/gitleaks/releases/download/v${VERSION}/gitleaks_${VERSION}_${PLATFORM}.tar.gz" -o gitleaks.tar.gz |
| 100 | + |
| 101 | +# Extract and install |
| 102 | +tar -xzf gitleaks.tar.gz gitleaks |
| 103 | +sudo mv gitleaks /usr/local/bin/gitleaks |
| 104 | +sudo chmod +x /usr/local/bin/gitleaks |
| 105 | + |
| 106 | +# Clean up |
| 107 | +rm gitleaks.tar.gz |
| 108 | +``` |
| 109 | + |
| 110 | +#### Option D: Using Go (any platform) |
| 111 | + |
| 112 | +If the user has Go installed: |
| 113 | + |
| 114 | +```bash |
| 115 | +go install github.com/gitleaks/gitleaks/v8@latest |
| 116 | +``` |
| 117 | + |
| 118 | +**Note:** This installs to `$GOPATH/bin/gitleaks` (usually `~/go/bin/gitleaks`) |
| 119 | + |
| 120 | +### Step 4: Verify Installation |
| 121 | + |
| 122 | +```bash |
| 123 | +gitleaks version |
| 124 | +``` |
| 125 | + |
| 126 | +**Expected output:** |
| 127 | +``` |
| 128 | +v8.20.1 |
| 129 | +``` |
| 130 | + |
| 131 | +Test basic functionality: |
| 132 | +```bash |
| 133 | +echo 'password = "SuperSecret123!"' > /tmp/test_leak.txt |
| 134 | +gitleaks detect --no-git --source /tmp/test_leak.txt --verbose |
| 135 | +rm /tmp/test_leak.txt |
| 136 | +``` |
| 137 | + |
| 138 | +**Expected output:** Should detect the password as a leak |
| 139 | + |
| 140 | +### Step 5: Inform User of Installation Success |
| 141 | + |
| 142 | +Provide the user with: |
| 143 | +1. Confirmation that gitleaks is installed |
| 144 | +2. The installed version |
| 145 | +3. Installation location |
| 146 | +4. Basic usage example |
| 147 | + |
| 148 | +## Return Value |
| 149 | + |
| 150 | +**Success:** |
| 151 | +- Gitleaks is installed and functional |
| 152 | +- Version information displayed |
| 153 | +- Basic usage instructions provided |
| 154 | + |
| 155 | +**Failure:** |
| 156 | +- Error message explaining what went wrong |
| 157 | +- Troubleshooting steps or alternative installation methods |
| 158 | +- Link to official documentation: https://github.com/gitleaks/gitleaks#installation |
| 159 | + |
| 160 | +## Error Handling |
| 161 | + |
| 162 | +### Homebrew Not Installed (macOS) |
| 163 | + |
| 164 | +**Problem:** `brew` command not found on macOS |
| 165 | + |
| 166 | +**Solution:** |
| 167 | +1. Offer to install Homebrew: |
| 168 | + ```bash |
| 169 | + /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" |
| 170 | + ``` |
| 171 | +2. Or proceed with manual installation (Option B) |
| 172 | + |
| 173 | +### Permission Denied |
| 174 | + |
| 175 | +**Problem:** Cannot write to `/usr/local/bin/` |
| 176 | + |
| 177 | +**Solution:** |
| 178 | +1. Ask user to provide sudo password when prompted |
| 179 | +2. Or install to user directory: |
| 180 | + ```bash |
| 181 | + mkdir -p ~/bin |
| 182 | + mv gitleaks ~/bin/gitleaks |
| 183 | + chmod +x ~/bin/gitleaks |
| 184 | + # Add to PATH |
| 185 | + echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc # or ~/.zshrc |
| 186 | + ``` |
| 187 | + |
| 188 | +### Download Failed |
| 189 | + |
| 190 | +**Problem:** Cannot download release from GitHub |
| 191 | + |
| 192 | +**Solution:** |
| 193 | +1. Check internet connection |
| 194 | +2. Verify GitHub is accessible |
| 195 | +3. Try alternative installation method (Homebrew, Go, etc.) |
| 196 | + |
| 197 | +### Unsupported Platform |
| 198 | + |
| 199 | +**Problem:** Platform not supported (Windows, unusual architecture) |
| 200 | + |
| 201 | +**Solution:** |
| 202 | +1. Check https://github.com/gitleaks/gitleaks/releases for available platforms |
| 203 | +2. For Windows, recommend installing via WSL and following the Linux installation steps |
| 204 | + |
| 205 | +## Examples |
| 206 | + |
| 207 | +### Example 1: Install on macOS with Homebrew |
| 208 | + |
| 209 | +``` |
| 210 | +User: /utils:install-gitleaks |
0 commit comments