GitGuardian ggshield

1.49.0 · active · verified Wed Apr 15

ggshield is a CLI application that runs in your local environment or in a CI environment to detect over 500 types of secrets, as well as other potential security vulnerabilities or policy breaks. It uses the GitGuardian public API through `py-gitguardian` for scanning. The current version is 1.49.0, with frequent releases addressing new features and fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to run a basic secret scan using `ggshield` via Python's `subprocess` module. It creates a temporary file with a simulated secret, scans it, and prints the output. Prior to running, you must authenticate `ggshield` by either running `ggshield auth login` in your terminal or by setting the `GITGUARDIAN_API_KEY` environment variable for non-interactive environments like CI/CD.

import os
import subprocess

# --- Step 1: Authenticate ---
# The recommended way is to run 'ggshield auth login' interactively in your terminal.
# This example assumes you've already authenticated or set GITGUARDIAN_API_KEY.
# For CI/CD, set the GITGUARDIAN_API_KEY environment variable.
# Example: export GITGUARDIAN_API_KEY="your_gitguardian_api_key_here"

# Simulate a file with a potential secret for scanning
with open('temp_secret_file.txt', 'w') as f:
    f.write('This is some test content.\n')
    f.write('API_KEY=ghs_test_this_is_a_fake_api_key_1234567890abcdef')

print("\n--- Running ggshield secret scan path on temp_secret_file.txt ---")
try:
    # Scan a specific file
    result = subprocess.run(
        ['ggshield', 'secret', 'scan', 'path', 'temp_secret_file.txt'],
        capture_output=True, text=True, check=False
    )
    print("Scan Output:\n", result.stdout)
    if result.stderr:
        print("Scan Errors:\n", result.stderr)
    if result.returncode != 0:
        print("Secret(s) detected or scan failed. Exit code:", result.returncode)
    else:
        print("No secrets detected.")
except FileNotFoundError:
    print("Error: 'ggshield' command not found. Please ensure ggshield is installed and in your PATH.")
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up the temporary file
    if os.path.exists('temp_secret_file.txt'):
        os.remove('temp_secret_file.txt')

view raw JSON →