Skip to content

Latest commit

Β 

History

History
427 lines (329 loc) Β· 11.1 KB

File metadata and controls

427 lines (329 loc) Β· 11.1 KB
layout single
title API Reference
description Complete CLI and Python API documentation
toc true
toc_label API Topics
toc_icon code
sidebar
nav
docs

πŸ”Œ API Reference

AI Command Auditor provides both a powerful command-line interface (CLI) and a comprehensive Python API for integration into your workflows and applications.

πŸ–₯️ Command Line Interface (CLI)

The CLI is the primary way to interact with AI Command Auditor. It provides commands for validation, configuration, and integration.

Quick Reference

# Core Commands
ai-auditor check-command "your command here"    # Validate a command
ai-auditor init                                 # Initialize in project
ai-auditor setup-hooks                          # Setup git hooks

# Configuration
ai-auditor config show                          # View configuration
ai-auditor config set security.strict_mode true # Update settings

# Information
ai-auditor --version                            # Show version
ai-auditor --help                               # Show help

πŸ” Command Validation

Validate individual commands or scripts for security

View Commands

βš™οΈ Configuration

Manage configuration settings and templates

View Commands

πŸ”— Git Integration

Setup and manage git hooks integration

View Commands

πŸ“Š Reporting

Generate reports and view analysis results

View Commands

🐍 Python API

The Python API allows you to integrate AI Command Auditor into your Python applications, scripts, and automation workflows.

from ai_command_auditor import CommandAuditor, SecurityRules

# Initialize auditor
auditor = CommandAuditor()

# Check a command
result = auditor.analyze_command("rm -rf /tmp/*")
print(f"Safety Score: {result.safety_score}")
print(f"Recommendations: {result.recommendations}")

# Load custom rules
rules = SecurityRules.from_file("custom-rules.yml")
auditor.update_rules(rules)

πŸ”§ Core API

Main classes and functions for command analysis

View API

πŸ›‘οΈ Security API

Security rules, validation, and policy management

View API

πŸ€– AI Integration

AI model configuration and prompt management

View API

πŸ”Œ Extensions

Plugin system and custom validators

View API

πŸš€ Getting Started Examples

CLI Quick Start

# 1. Install and initialize
curl -fsSL https://raw.githubusercontent.com/etherisc/ai-command-auditor/main/install.sh | sh
cd your-project && ai-auditor init

# 2. Check your first command
ai-auditor check-command "sudo rm -rf /var/log/*"

# 3. Setup git hooks
ai-auditor setup-hooks

# 4. Configure for your project
ai-auditor config set templates.default "python"

Python API Quick Start

# 1. Install the package
# pip install ai-command-auditor

# 2. Basic usage
from ai_command_auditor import CommandAuditor

auditor = CommandAuditor()
result = auditor.analyze_command("python -c 'import os; os.system(\"rm -rf /\")'")

if result.is_dangerous:
    print(f"⚠️  Dangerous command detected!")
    print(f"Severity: {result.severity}")
    print(f"Reason: {result.reason}")
else:
    print("βœ… Command appears safe")

# 3. Custom configuration
auditor.configure({
    'security': {'strict_mode': True},
    'ai': {'model': 'gpt-4o'}
})

πŸ“– Detailed Documentation

CLI Documentation

The complete CLI reference covers all commands, options, and use cases:

Section Description Link
Command Validation Validate commands and scripts CLI Commands β†’
Configuration Management Manage settings and templates Config Commands β†’
Git Integration Setup hooks and automation Git Commands β†’
Reporting & Analysis Generate reports and insights Report Commands β†’
Utility Commands Helper and diagnostic commands Utility Commands β†’

Python API Documentation

Complete Python API reference with examples:

Module Description Link
CommandAuditor Main auditor class and methods Core API β†’
SecurityRules Security policy management Security API β†’
AIAnalyzer AI-powered analysis engine AI API β†’
Configuration Configuration management Config API β†’
Validators Custom validation framework Validators β†’

πŸ”— Integration Patterns

CI/CD Integration

# GitHub Actions example
name: Command Validation
on: [push, pull_request]

jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install AI Command Auditor
        run: curl -fsSL https://raw.githubusercontent.com/etherisc/ai-command-auditor/main/install.sh | sh

      - name: Validate Commands
        run: |
          ai-auditor scan-scripts scripts/
          ai-auditor validate-makefile Makefile

Pre-commit Integration

# .pre-commit-config.yaml
repos:
  - repo: local
    hooks:
      - id: ai-command-auditor
        name: AI Command Auditor
        entry: ai-auditor check-staged-commands
        language: system
        stages: [commit]

Python Integration

# Custom integration example
import subprocess
from ai_command_auditor import CommandAuditor

class SafeCommandRunner:
    def __init__(self):
        self.auditor = CommandAuditor()

    def run_command(self, command: str) -> str:
        # Validate before execution
        result = self.auditor.analyze_command(command)

        if result.is_dangerous:
            raise SecurityError(f"Dangerous command blocked: {result.reason}")

        # Execute if safe
        return subprocess.check_output(command, shell=True, text=True)

# Usage
runner = SafeCommandRunner()
output = runner.run_command("ls -la")  # Safe
# runner.run_command("rm -rf /")       # Would raise SecurityError

πŸ› οΈ Development & Extension

Custom Validators

from ai_command_auditor.validators import BaseValidator

class CustomValidator(BaseValidator):
    def __init__(self):
        super().__init__("custom_validator")

    def validate(self, command: str) -> ValidationResult:
        # Custom validation logic
        if "dangerous_pattern" in command:
            return ValidationResult(
                valid=False,
                severity="high",
                message="Custom security rule violation"
            )
        return ValidationResult(valid=True)

# Register custom validator
auditor = CommandAuditor()
auditor.add_validator(CustomValidator())

Plugin Development

# Create a plugin
from ai_command_auditor.plugins import BasePlugin

class MyPlugin(BasePlugin):
    def on_command_analyzed(self, command: str, result: AnalysisResult):
        # Custom post-analysis logic
        if result.severity == "critical":
            self.send_alert(command, result)

    def send_alert(self, command: str, result: AnalysisResult):
        # Send notification, log to external system, etc.
        pass

# Load plugin
auditor.load_plugin(MyPlugin())

πŸ“Š Performance & Optimization

Async API Usage

import asyncio
from ai_command_auditor import AsyncCommandAuditor

async def validate_commands(commands: list[str]):
    auditor = AsyncCommandAuditor()

    # Validate multiple commands concurrently
    results = await asyncio.gather(*[
        auditor.analyze_command(cmd) for cmd in commands
    ])

    return results

# Usage
commands = ["ls -la", "rm temp.txt", "python script.py"]
results = asyncio.run(validate_commands(commands))

Caching and Performance

from ai_command_auditor import CommandAuditor

# Configure caching for better performance
auditor = CommandAuditor({
    'performance': {
        'cache_enabled': True,
        'cache_ttl': 3600,  # 1 hour
        'parallel_requests': 5
    }
})

# Batch processing
results = auditor.analyze_commands(command_list, batch_size=10)

πŸ” Advanced Usage

Custom AI Models

# Use custom AI model
auditor = CommandAuditor({
    'ai': {
        'model': 'custom-model',
        'api_base': 'https://your-api.com/v1',
        'api_key': 'your-api-key'
    }
})

Security Policy Management

from ai_command_auditor import SecurityPolicy

# Create custom security policy
policy = SecurityPolicy()
policy.add_rule("no_rm_rf", r"rm\s+-rf\s+/", severity="critical")
policy.add_rule("no_curl_eval", r"curl.*\|\s*sh", severity="high")

auditor = CommandAuditor()
auditor.apply_policy(policy)

πŸ“š API Reference Links

Complete Documentation

Quick Links

<style> .cli-grid, .api-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1.5rem; margin: 2rem 0; } .cli-section, .api-section { padding: 1.5rem; border: 1px solid #e1e1e1; border-radius: 8px; text-align: center; background: #f8f9fa; } .cli-section h3, .api-section h3 { margin-bottom: 1rem; color: #2c3e50; } .cli-section p, .api-section p { margin-bottom: 1rem; } table { width: 100%; margin: 1rem 0; border-collapse: collapse; } table th, table td { padding: 0.75rem; text-align: left; border-bottom: 1px solid #ddd; } table th { background-color: #f8f9fa; font-weight: 600; } @media (max-width: 768px) { .cli-grid, .api-grid { grid-template-columns: 1fr; } table { font-size: 0.9rem; } } </style>