mirror of
https://github.com/ivuorinen/f2b.git
synced 2026-01-26 03:13:58 +00:00
* Go rewrite * chore(cr): apply suggestions Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Ismo Vuorinen <ismo@ivuorinen.net> * 📝 CodeRabbit Chat: Add NoOpClient to fail2ban and initialize when skip flag is true * 📝 CodeRabbit Chat: Fix malformed if-else structure and add no-op client for skip-only commands * 📝 CodeRabbit Chat: Fix malformed if-else structure and add no-op client for skip-only commands * fix(main): correct no-op branch syntax (#10) * chore(gitignore): ignore env and binary files (#11) * chore(config): remove indent_size for go files (#12) * feat(cli): inject version via ldflags (#13) * fix(security): validate filter parameter to prevent path traversal (#15) * chore(repo): anchor ignore for build artifacts (#16) * chore(ci): use golangci-lint action (#17) * feat(fail2ban): expose GetLogDir (#19) * test(cmd): improve IP mock validation (#20) * chore(ci): update golanglint * fix(ci): golanglint * fix(ci): correct args indentation in pr-lint workflow (#21) * fix(ci): avoid duplicate releases (#22) * refactor(fail2ban): remove test check from OSRunner (#23) * refactor(fail2ban): make log and filter dirs configurable (#24) * fix(ci): create single release per tag (#14) Signed-off-by: Ismo Vuorinen <ismo@ivuorinen.net> * chore(dev): add codex setup script (#27) * chore(lint): enable staticcheck (#26) * chore(ci): verify golangci config (#28) * refactor(cmd): centralize env config (#29) * chore(dev): add pre-commit config (#30) * fix(ci): disable cgo in cross compile (#31) * fix(ci): fail on formatting issues (#32) * feat(cmd): add context to logs watch (#33) * chore: fixes, roadmap, claude.md, linting * chore: fixes, linting * fix(ci): gh actions update, fixes and tweaks * chore: use reviewdog actionlint * chore: use wow-rp-addons/actions-editorconfig-check * chore: combine agent instructions, add comments, fixes * chore: linting, fixes, go revive * chore(deps): update pre-commit hooks * chore: bump go to 1.21, pin workflows * fix: install tools in lint.yml * fix: sudo timeout * fix: service command injection * fix: memory exhaustion with large logs * fix: enhanced path traversal and file security vulns * fix: race conditions * fix: context support * chore: simplify fail2ban/ code * feat: major refactoring with GoReleaser integration and code consolidation - Add GoReleaser configuration for automated multi-platform releases - Support for Linux, macOS, Windows, and BSD builds - Docker images, Homebrew tap, and Linux packages (.deb, .rpm, .apk) - GitHub Actions workflow for release automation - Consolidate duplicate code and improve architecture - Extract common command helpers to cmd/helpers.go (~230 lines) - Remove duplicate MockClient implementation from tests (~250 lines) - Create context wrapper helpers in fail2ban/context_helpers.go - Standardize error messages in fail2ban/errors.go - Enhance validation and security - Add proper IP address validation with fail2ban.ValidateIP - Fix path traversal and command injection vulnerabilities - Improve thread-safety in MockClient with consistent ordering - Optimize documentation - Reduce CLAUDE.md from 190 to 81 lines (57% reduction) - Reduce TODO.md from 633 to 93 lines (85% reduction) - Move README.md to root directory with installation instructions - Improve test reliability - Fix race conditions and test flakiness - Add sorting to ensure deterministic test output - Enhance MockClient with configurable behavior * feat: comprehensive code quality improvements and documentation reorganization This commit represents a major overhaul of code quality, documentation structure, and development tooling: **Documentation & Structure:** - Move CODE_OF_CONDUCT.md from .github to root directory - Reorganize documentation with dedicated docs/ directory - Create comprehensive architecture, security, and testing documentation - Update all references and cross-links for new documentation structure **Code Quality & Linting:** - Add 120-character line length limit across all files via EditorConfig - Enable comprehensive linting with golines, lll, usetesting, gosec, and revive - Fix all 86 revive linter issues (unused parameters, missing export comments) - Resolve security issues (file permissions 0644 → 0600, gosec warnings) - Replace deprecated os.Setenv with t.Setenv in all tests - Configure golangci-lint with auto-fix capabilities and formatter integration **Development Tooling:** - Enhance pre-commit configuration with additional hooks and formatters - Update GoReleaser configuration with improved YAML formatting - Improve GitHub workflows and issue templates for CLI-specific context - Add comprehensive Makefile with proper dependency checking **Testing & Security:** - Standardize mock patterns and context wrapper implementations - Enhance error handling with centralized error constants - Improve concurrent access testing for thread safety * perf: implement major performance optimizations with comprehensive test coverage This commit introduces three significant performance improvements along with complete linting compliance and robust test coverage: **Performance Optimizations:** 1. **Time Parsing Cache (8.6x improvement)** - Add TimeParsingCache with sync.Map for caching parsed times - Implement object pooling for string builders to reduce allocations - Create optimized BanRecordParser with pooled string slices 2. **Gzip Detection Consolidation (55x improvement)** - Consolidate ~100 lines of duplicate gzip detection logic - Fast-path extension checking before magic byte detection - Unified GzipDetector with comprehensive file handling utilities 3. **Parallel Processing (2.5-5.0x improvement)** - Generic WorkerPool implementation for concurrent operations - Smart fallback to sequential processing for single operations - Context-aware cancellation support for long-running tasks - Applied to ban/unban operations across multiple jails **New Files Added:** - fail2ban/time_parser.go: Cached time parsing with global instances - fail2ban/ban_record_parser.go: Optimized ban record parsing - fail2ban/gzip_detection.go: Unified gzip handling utilities - fail2ban/parallel_processing.go: Generic parallel processing framework - cmd/parallel_operations.go: Command-level parallel operation support **Code Quality & Linting:** - Resolve all golangci-lint issues (0 remaining) - Add proper #nosec annotations for legitimate file operations - Implement sentinel errors replacing nil/nil anti-pattern - Fix context parameter handling and error checking **Comprehensive Test Coverage:** - 500+ lines of new tests with benchmarks validating all improvements - Concurrent access testing for thread safety - Edge case handling and error condition testing - Performance benchmarks demonstrating measured improvements **Modified Files:** - fail2ban/fail2ban.go: Integration with new optimized parsers - fail2ban/logs.go: Use consolidated gzip detection (-91 lines) - cmd/ban.go & cmd/unban.go: Add conditional parallel processing * test: comprehensive test infrastructure overhaul with real test data Major improvements to test code quality and organization: • Added comprehensive test data infrastructure with 6 anonymized log files • Extracted common test helpers reducing ~200 lines to ~50 reusable functions • Enhanced ban record parser tests with real production log patterns • Improved gzip detection tests with actual compressed test data • Added integration tests for full log processing and concurrent operations • Updated .gitignore to allow testdata log files while excluding others • Updated TODO.md to reflect completed test infrastructure improvements * fix: comprehensive security hardening and critical bug fixes Security Enhancements: - Add command injection protection with allowlist validation for all external commands - Add security documentation to gzip functions warning about path traversal risks - Complete TODO.md security audit - all critical vulnerabilities addressed Bug Fixes: - Fix negative index access vulnerability in parallel operations (prevent panic) - Fix parsing inconsistency between BannedIn and BannedInWithContext functions - Fix nil error handling in concurrent log reading tests - Fix benchmark error simulation to measure actual performance vs error paths Implementation Details: - Add ValidateCommand() with allowlist for fail2ban-client, fail2ban-regex, service, systemctl, sudo - Integrate command validation into all OSRunner methods before execution - Replace manual string parsing with ParseBracketedList() for consistency - Add bounds checking (index >= 0) to prevent negative array access - Replace nil error with descriptive error message in concurrent error channels - Update banFunc in benchmark to return success instead of permanent errors Test Coverage: - Add comprehensive security validation tests with injection attempt patterns - Add parallel operations safety tests with index validation - Add parsing consistency tests between context/non-context functions - Add error handling demonstration tests for concurrent operations - Add gzip function security requirement documentation tests * perf: implement ultra-optimized log and ban record parsing with significant performance gains Major performance improvements to core fail2ban processing with comprehensive benchmarking: Performance Achievements: • Ban record parsing: 15% faster, 39% less memory, 45% fewer allocations • Log processing: 27% faster, 64% less memory, 32% fewer allocations • Cache performance: 624x faster cache hits with zero allocations • String pooling: 4.7x improvement with zero memory allocations Core Optimizations: • Object pooling (sync.Pool) for string slices, scanner buffers, and line buffers • Comprehensive caching (sync.Map) for gzip detection, file info, and path patterns • Fast path optimizations with extension-based gzip detection • Byte-level operations to reduce string allocations in filtering • Ultra-optimized parsers with smart field parsing and efficient time handling New Files: • fail2ban/ban_record_parser_optimized.go - High-performance ban record parser • fail2ban/log_performance_optimized.go - Ultra-optimized log processor with caching • fail2ban/ban_record_parser_benchmark_test.go - Ban record parsing benchmarks • fail2ban/log_performance_benchmark_test.go - Log performance benchmarks • fail2ban/ban_record_parser_compatibility_test.go - Compatibility verification tests Updated: • fail2ban/fail2ban.go - Integration with ultra-optimized parsers • TODO.md - Marked performance optimization tasks as completed * fix(ci): install dev dependencies for pre-commit * refactor: streamline pre-commit config and extract test helpers - Replace local hooks with upstream pre-commit repositories for better maintainability - Add new hooks: shellcheck, shfmt, checkov for enhanced code quality - Extract common test helpers into dedicated test_helpers.go to reduce duplication - Add warning logs for unreadable log files in fail2ban and logs packages - Remove hard-coded GID checks in sudo.go for better cross-platform portability - Update golangci-lint installation method in Makefile * fix(security): path traversal, log file validation * feat: complete pre-release modernization with comprehensive testing - Remove all deprecated legacy functions and dead code paths - Add security hardening with sanitized error messages - Implement comprehensive performance benchmarks and security audit tests - Mark all pre-release modernization tasks as completed (10/10) - Update project documentation to reflect full completion status * fix(ci): linting, and update gosec install source * feat: implement comprehensive test framework with 60-70% code reduction Major test infrastructure modernization: - Create fluent CommandTestBuilder framework for streamlined test creation - Add MockClientBuilder pattern for advanced mock configuration - Standardize table test field naming (expectedOut→wantOutput, expectError→wantError) - Consolidate test code: 3,796 insertions, 3,104 deletions (net +692 lines with enhanced functionality) Framework achievements: - 168+ tests passing with zero regressions - 5 cmd test files fully migrated to new framework - 63 field name standardizations applied - Advanced mock patterns with fluent interface File organization improvements: - Rename all test files with consistent prefixes (cmd_*, fail2ban_*, main_*) - Split monolithic test files into focused, maintainable modules - Eliminate cmd_test.go (622 lines) and main_test.go (825 lines) - Create specialized test files for better organization Documentation enhancements: - Update docs/testing.md with complete framework documentation - Optimize TODO.md from 231→72 lines (69% token reduction) - Add comprehensive migration guides and best practices Test framework components: - command_test_framework.go: Core fluent interface implementation - MockClientBuilder: Advanced mock configuration with builder pattern - table_test_standards.go: Standardized field naming conventions - Enhanced test helpers with error checking consolidation * chore: fixes, .go-version, linting * fix(ci) editorconfig in .pre-commit-config.yaml * fix: too broad gitignore * chore: update fail2ban/fail2ban_path_security_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Ismo Vuorinen <ismo@ivuorinen.net> * chore: code review fixes * chore: code review fixes * fix: more code review fixes * fix: more code review fixes * feat: cleanup, fixes, testing * chore: minor config file updates - Add quotes to F2B_TIMEOUT value in .env.example for clarity - Remove testdata log exception from .gitignore (simplified) * feat: implement comprehensive monitoring with structured logging and metrics - Add structured logging with context propagation throughout codebase - Implement ContextualLogger with request tracking and operation timing - Add context values for operation, IP, jail, command, and request ID - Integrate with existing logrus logging infrastructure - Add request/response timing metrics collection - Create comprehensive Metrics system with atomic counters - Track command executions, ban/unban operations, and client operations - Implement latency distribution buckets for performance analysis - Add validation cache hit/miss tracking - Enhance ban/unban commands with structured logging - Add LogOperation wrapper for automatic timing and context - Log individual jail operations with success/failure status - Integrate metrics recording with ban/unban operations - Add new 'metrics' command to expose collected metrics - Support both plain text and JSON output formats - Display system metrics (uptime, memory, goroutines) - Show operation counts, failures, and average latencies - Include latency distribution histograms - Update test infrastructure - Add tests for metrics command - Fix test helper to support persistent flags - Ensure all tests pass with new logging This completes the high-priority performance monitoring and structured logging requirements from TODO.md, providing comprehensive operational visibility into the f2b application. * docs: update TODO.md to reflect completed monitoring work - Mark structured logging and timing metrics as completed - Update test coverage stats (cmd/ improved from 66.4% to 76.8%) - Add completed infrastructure section for today's work - Update current status date and add monitoring to health indicators * feat: complete TODO.md technical debt cleanup Complete all remaining TODO.md tasks with comprehensive implementation: ## 🎯 Validation Caching Implementation - Thread-safe validation cache with sync.RWMutex protection - MetricsRecorder interface to avoid circular dependencies - Cached validation for IP, jail, filter, and command validation - Integration with existing metrics system for cache hit/miss tracking - 100% test coverage for caching functionality ## 🔧 Constants Extraction - Fail2Ban status codes: Fail2BanStatusSuccess, Fail2BanStatusAlreadyProcessed - Command constants: Fail2BanClientCommand, Fail2BanRegexCommand, Fail2BanServerCommand - File permissions: DefaultFilePermissions (0600), DefaultDirectoryPermissions (0750) - Timeout limits: MaxCommandTimeout, MaxFileTimeout, MaxParallelTimeout - Updated all references throughout codebase to use named constants ## 📊 Test Coverage Improvement - Increased fail2ban package coverage from 62.0% to 70.3% (target: 70%+) - Added 6 new comprehensive test files with 200+ additional test cases - Coverage improvements across all major components: - Context helpers, validation cache, mock clients, OS runner methods - Error constructors, timing operations, cache statistics - Thread safety and concurrency testing ## 🛠️ Code Quality & Fixes - Fixed all linting issues (golangci-lint, revive, errcheck) - Resolved unused parameter warnings and error handling - Fixed timing-dependent test failures in worker pool cancellation - Enhanced thread safety in validation caching ## 📈 Final Metrics - Overall test coverage: 72.4% (up from ~65%) - fail2ban package: 70.3% (exceeds 70% target) - cmd package: 76.9% - Zero TODO/FIXME/HACK comments in production code - 100% linting compliance * fix: resolve test framework issues and update documentation - Remove unnecessary defer/recover block in comprehensive_framework_test.go - Fix compilation error in command_test_framework.go variable redeclaration - Update TODO.md to reflect all 12 completed code quality fixes - Clean up dead code and improve test maintainability - Fix linting issues: error handling, code complexity, security warnings - Break down complex test function to reduce cyclomatic complexity * fix: replace dangerous test commands with safe placeholders Replaces actual dangerous commands in test cases with safe placeholder patterns to prevent accidental execution while maintaining comprehensive security testing. - Replace 'rm -rf /', 'cat /etc/passwd' with 'DANGEROUS_RM_COMMAND', 'DANGEROUS_SYSTEM_CALL' - Update GetDangerousCommandPatterns() to recognize both old and new patterns - Enhance filter validation with command injection protection (semicolons, pipes, backticks, dollar signs) - Add package documentation comments for all packages (main, cmd, fail2ban) - Fix GoReleaser static linking configuration for cross-platform builds - Remove Docker platform restriction to enable multi-arch support - Apply code formatting and linting fixes All security validation tests continue to pass with the safe placeholders. * fix: resolve TestMixedConcurrentOperations race condition and command key mismatches The concurrency test was failing due to several issues: 1. **Command Key Mismatch**: Test setup used "sudo test arg" key but MockRunner looked for "test arg" because "test" command doesn't require sudo 2. **Invalid Commands**: Using "test" and "echo" commands that aren't in the fail2ban command allowlist, causing validation failures 3. **Race Conditions**: Multiple goroutines setting different MockRunners simultaneously, overwriting responses **Solution:** - Replace invalid test commands ("test", "echo") with valid fail2ban commands ("fail2ban-client status", "fail2ban-client -V") - Pre-configure shared MockRunner with all required response keys for both sudo and non-sudo execution paths - Improve test structure to reduce race conditions between setup and execution All tests now pass reliably, resolving the CI failure. * fix: address code quality issues and improve test coverage - Replace unsafe type assertion with comma-ok idiom in logging - Fix TestTestFilter to use created filter instead of nonexistent - Add warning logs for invalid log level configurations - Update TestVersionCommand to use consistent test framework pattern - Remove unused LoggerContextKey constant - Add version command support to test framework - Fix trailing whitespace in test files * feat: add timeout handling and multi-architecture Docker support * test: enhance path traversal security test coverage * chore: comprehensive documentation update and linting fixes Updated all documentation to reflect current capabilities including context-aware operations, multi-architecture Docker support, advanced security features, and performance monitoring. Removed unused functions and fixed all linting issues. * fix(lint): .goreleaser.yaml * feat: add markdown link checker and fix all linting issues - Add markdown-link-check to pre-commit hooks with comprehensive configuration - Fix GitHub workflow structure (sync-labels.yml) with proper job setup - Add JSON schemas to all configuration files for better IDE support - Update tool installation in Makefile for markdown-link-check dependency - Fix all revive linting issues (Boolean literals, defer in loop, if-else simplification, method naming) - Resolve broken relative link in CONTRIBUTING.md - Configure rate limiting and ignore patterns for GitHub URLs - Enhance CLAUDE.md with link checking documentation * fix(ci): sync-labels permissions * docs: comprehensive documentation update reflecting current project status - Updated TODO.md to show production-ready status with 21 commands - Enhanced README.md with enterprise-grade features and capabilities - Added performance monitoring and timeout configuration to FAQ - Updated CLAUDE.md with accurate project architecture overview - Fixed all line length issues to meet EditorConfig requirements - Added .mega-linter.yml configuration for enhanced linting * fix: address CodeRabbitAI review feedback - Split .goreleaser.yaml builds for static/dynamic linking by architecture - Update docs to accurately reflect 7 path traversal patterns (not 17) - Fix containsPathTraversal to allow valid absolute paths - Replace runnerCombinedRunWithSudoContext with RunnerCombinedOutputWithSudoContext - Fix ldflags to use uppercase Version variable name - Remove duplicate test coverage metrics in TODO.md - Fix .markdown-link-check.json schema violations - Add v8r JSON validator to pre-commit hooks * chore(ci): update workflows, switch v8r to check-jsonschema * fix: restrict static linking to amd64 only in .goreleaser.yaml - Move arm64 from static to dynamic build configuration - Static linking now only applies to linux/amd64 - Prevents build failures due to missing static libc on ARM64 - All architectures remain supported with appropriate linking * fix(ci): caching * fix(ci): python caching with pip, node with npm * fix(ci): no caching for node then * fix(ci): no requirements.txt, no cache * refactor: address code review feedback - Pin Alpine base image to v3.20 for reproducible builds - Remove redundant --platform flags in GoReleaser Docker configs - Fix unused parameters in concurrency test goroutines - Simplify string search helper using strings.Contains() - Remove redundant error checking logic in security tests --------- Signed-off-by: Ismo Vuorinen <ismo@ivuorinen.net> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
785 lines
19 KiB
Go
785 lines
19 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/ivuorinen/f2b/fail2ban"
|
|
)
|
|
|
|
func TestParseLogLevel(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
level string
|
|
expected logrus.Level
|
|
}{
|
|
{
|
|
name: "debug level",
|
|
level: "debug",
|
|
expected: logrus.DebugLevel,
|
|
},
|
|
{
|
|
name: "info level",
|
|
level: "info",
|
|
expected: logrus.InfoLevel,
|
|
},
|
|
{
|
|
name: "warn level",
|
|
level: "warn",
|
|
expected: logrus.WarnLevel,
|
|
},
|
|
{
|
|
name: "warning level",
|
|
level: "warning",
|
|
expected: logrus.WarnLevel,
|
|
},
|
|
{
|
|
name: "error level",
|
|
level: "error",
|
|
expected: logrus.ErrorLevel,
|
|
},
|
|
{
|
|
name: "fatal level",
|
|
level: "fatal",
|
|
expected: logrus.FatalLevel,
|
|
},
|
|
{
|
|
name: "panic level",
|
|
level: "panic",
|
|
expected: logrus.PanicLevel,
|
|
},
|
|
{
|
|
name: "unknown level defaults to info",
|
|
level: "unknown",
|
|
expected: logrus.InfoLevel,
|
|
},
|
|
{
|
|
name: "empty level defaults to info",
|
|
level: "",
|
|
expected: logrus.InfoLevel,
|
|
},
|
|
{
|
|
name: "uppercase level",
|
|
level: "DEBUG",
|
|
expected: logrus.InfoLevel, // case sensitive, so falls back to default
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := parseLogLevel(tt.level)
|
|
if result != tt.expected {
|
|
t.Errorf("parseLogLevel(%q) = %v, want %v", tt.level, result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConfigDefaults(t *testing.T) {
|
|
// Test that Config struct has reasonable defaults
|
|
config := Config{}
|
|
|
|
// Initially empty
|
|
if config.LogDir != "" {
|
|
t.Errorf("expected empty LogDir, got %q", config.LogDir)
|
|
}
|
|
if config.FilterDir != "" {
|
|
t.Errorf("expected empty FilterDir, got %q", config.FilterDir)
|
|
}
|
|
if config.Format != "" {
|
|
t.Errorf("expected empty Format, got %q", config.Format)
|
|
}
|
|
}
|
|
|
|
func TestEnvironmentVariableSetup(t *testing.T) {
|
|
// Save original environment
|
|
// Set up environment variables using t.Setenv for automatic cleanup
|
|
t.Setenv("F2B_LOG_DIR", os.Getenv("F2B_LOG_DIR"))
|
|
t.Setenv("F2B_FILTER_DIR", os.Getenv("F2B_FILTER_DIR"))
|
|
t.Setenv("F2B_LOG_LEVEL", os.Getenv("F2B_LOG_LEVEL"))
|
|
t.Setenv("F2B_LOG_FILE", os.Getenv("F2B_LOG_FILE"))
|
|
|
|
tests := []struct {
|
|
name string
|
|
envVar string
|
|
envValue string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "F2B_LOG_DIR environment variable",
|
|
envVar: "F2B_LOG_DIR",
|
|
envValue: "/custom/log/dir",
|
|
expected: "/custom/log/dir",
|
|
},
|
|
{
|
|
name: "F2B_FILTER_DIR environment variable",
|
|
envVar: "F2B_FILTER_DIR",
|
|
envValue: "/custom/filter/dir",
|
|
expected: "/custom/filter/dir",
|
|
},
|
|
{
|
|
name: "F2B_LOG_LEVEL environment variable",
|
|
envVar: "F2B_LOG_LEVEL",
|
|
envValue: "debug",
|
|
expected: "debug",
|
|
},
|
|
{
|
|
name: "F2B_LOG_FILE environment variable",
|
|
envVar: "F2B_LOG_FILE",
|
|
envValue: "/tmp/f2b.log",
|
|
expected: "/tmp/f2b.log",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Set environment variable using t.Setenv for automatic cleanup
|
|
t.Setenv(tt.envVar, tt.envValue)
|
|
|
|
// Get the value
|
|
result := os.Getenv(tt.envVar)
|
|
if result != tt.expected {
|
|
t.Errorf("expected %q, got %q", tt.expected, result)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConfigStructure(t *testing.T) {
|
|
config := Config{
|
|
LogDir: "/test/log",
|
|
FilterDir: "/test/filter",
|
|
Format: "json",
|
|
}
|
|
|
|
if config.LogDir != "/test/log" {
|
|
t.Errorf("expected LogDir '/test/log', got %q", config.LogDir)
|
|
}
|
|
if config.FilterDir != "/test/filter" {
|
|
t.Errorf("expected FilterDir '/test/filter', got %q", config.FilterDir)
|
|
}
|
|
if config.Format != "json" {
|
|
t.Errorf("expected Format 'json', got %q", config.Format)
|
|
}
|
|
}
|
|
|
|
func TestCompletionCmdStructure(t *testing.T) {
|
|
cmd := completionCmd()
|
|
|
|
if cmd.Use != "completion [bash|zsh|fish|powershell]" {
|
|
t.Errorf("unexpected completion command Use: %q", cmd.Use)
|
|
}
|
|
|
|
if cmd.Short != "Generate shell completion scripts" {
|
|
t.Errorf("unexpected completion command Short: %q", cmd.Short)
|
|
}
|
|
|
|
expectedValidArgs := []string{"bash", "zsh", "fish", "powershell"}
|
|
if len(cmd.ValidArgs) != len(expectedValidArgs) {
|
|
t.Errorf("expected %d ValidArgs, got %d", len(expectedValidArgs), len(cmd.ValidArgs))
|
|
}
|
|
|
|
for i, expected := range expectedValidArgs {
|
|
if i >= len(cmd.ValidArgs) || cmd.ValidArgs[i] != expected {
|
|
t.Errorf("expected ValidArgs[%d] = %q, got %q", i, expected, cmd.ValidArgs[i])
|
|
}
|
|
}
|
|
|
|
if !cmd.DisableFlagsInUseLine {
|
|
t.Errorf("expected DisableFlagsInUseLine to be true")
|
|
}
|
|
}
|
|
|
|
func TestGlobalVariables(t *testing.T) {
|
|
// Test that global variables are properly initialized
|
|
if rootCmd == nil {
|
|
t.Fatal("rootCmd should be initialized")
|
|
}
|
|
|
|
if rootCmd.Use != "f2b" {
|
|
t.Errorf("expected rootCmd.Use to be 'f2b', got %q", rootCmd.Use)
|
|
}
|
|
|
|
if rootCmd.Short != "Fail2Ban CLI helper" {
|
|
t.Errorf("expected rootCmd.Short to be 'Fail2Ban CLI helper', got %q", rootCmd.Short)
|
|
}
|
|
|
|
expectedLong := "Fail2Ban CLI tool implemented in Go using Cobra."
|
|
if rootCmd.Long != expectedLong {
|
|
t.Errorf("expected rootCmd.Long to be %q, got %q", expectedLong, rootCmd.Long)
|
|
}
|
|
}
|
|
|
|
// BenchmarkParseLogLevel benchmarks the log level parsing function
|
|
func BenchmarkParseLogLevel(b *testing.B) {
|
|
levels := []string{"debug", "info", "warn", "error", "unknown"}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
level := levels[i%len(levels)]
|
|
parseLogLevel(level)
|
|
}
|
|
}
|
|
|
|
// TestDefaultValues tests the default values used in the configuration
|
|
func TestDefaultValues(t *testing.T) {
|
|
// Clear environment variables for this test using t.Setenv
|
|
t.Setenv("F2B_LOG_DIR", "")
|
|
t.Setenv("F2B_FILTER_DIR", "")
|
|
|
|
// Test default values when environment variables are not set
|
|
logDir := os.Getenv("F2B_LOG_DIR")
|
|
if logDir != "" {
|
|
t.Errorf("expected empty F2B_LOG_DIR, got %q", logDir)
|
|
}
|
|
|
|
filterDir := os.Getenv("F2B_FILTER_DIR")
|
|
if filterDir != "" {
|
|
t.Errorf("expected empty F2B_FILTER_DIR, got %q", filterDir)
|
|
}
|
|
}
|
|
|
|
func TestExecute(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
setupClient func() fail2ban.Client
|
|
config Config
|
|
wantError bool
|
|
}{
|
|
{
|
|
name: "successful execution with mock client",
|
|
setupClient: func() fail2ban.Client {
|
|
return fail2ban.NewMockClient()
|
|
},
|
|
config: Config{
|
|
LogDir: "/tmp/test",
|
|
FilterDir: "/tmp/filters",
|
|
Format: "plain",
|
|
},
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "execution with json format",
|
|
setupClient: func() fail2ban.Client {
|
|
return fail2ban.NewMockClient()
|
|
},
|
|
config: Config{
|
|
LogDir: "/var/log",
|
|
FilterDir: "/etc/fail2ban/filter.d",
|
|
Format: "json",
|
|
},
|
|
wantError: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
client := tt.setupClient()
|
|
|
|
// Capture stdout to prevent output during tests
|
|
oldStdout := os.Stdout
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("failed to create pipe: %v", err)
|
|
}
|
|
os.Stdout = w
|
|
|
|
// Set up a simple test command that will exit quickly
|
|
originalArgs := os.Args
|
|
os.Args = []string{"f2b", "version"}
|
|
|
|
err = Execute(client, tt.config)
|
|
|
|
// Restore stdout
|
|
if err := w.Close(); err != nil {
|
|
t.Fatalf("failed to close writer: %v", err)
|
|
}
|
|
os.Stdout = oldStdout
|
|
os.Args = originalArgs
|
|
|
|
// Read and discard output
|
|
var buf bytes.Buffer
|
|
if _, err := buf.ReadFrom(r); err != nil {
|
|
t.Fatalf("failed to read output: %v", err)
|
|
}
|
|
|
|
AssertError(t, err, tt.wantError, tt.name)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExecuteWithRealCommands(t *testing.T) {
|
|
// Test that Execute properly adds all commands
|
|
client := fail2ban.NewMockClient()
|
|
config := Config{
|
|
LogDir: "/tmp",
|
|
FilterDir: "/tmp",
|
|
Format: "plain",
|
|
}
|
|
|
|
// Create a new root command to test command registration
|
|
originalRootCmd := rootCmd
|
|
rootCmd = &cobra.Command{
|
|
Use: "f2b",
|
|
Short: "Fail2Ban CLI helper",
|
|
Long: "Fail2Ban CLI tool implemented in Go using Cobra.",
|
|
}
|
|
|
|
// Capture stdout
|
|
oldStdout := os.Stdout
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("failed to create pipe: %v", err)
|
|
}
|
|
os.Stdout = w
|
|
|
|
originalArgs := os.Args
|
|
os.Args = []string{"f2b", "help"}
|
|
|
|
err = Execute(client, config)
|
|
|
|
// Restore
|
|
if err := w.Close(); err != nil {
|
|
t.Fatalf("failed to close writer: %v", err)
|
|
}
|
|
os.Stdout = oldStdout
|
|
os.Args = originalArgs
|
|
rootCmd = originalRootCmd
|
|
|
|
// Read output
|
|
var buf bytes.Buffer
|
|
if _, err := buf.ReadFrom(r); err != nil {
|
|
t.Fatalf("failed to read output: %v", err)
|
|
}
|
|
output := buf.String()
|
|
|
|
AssertError(t, err, false, "root help command")
|
|
|
|
// Check that help output contains expected commands
|
|
expectedCommands := []string{
|
|
"list-jails",
|
|
"status",
|
|
"banned",
|
|
"ban",
|
|
"unban",
|
|
"test",
|
|
"logs",
|
|
"logs-watch",
|
|
"service",
|
|
"version",
|
|
"test-filter",
|
|
"completion",
|
|
}
|
|
for _, cmd := range expectedCommands {
|
|
if !strings.Contains(output, cmd) {
|
|
t.Errorf("expected help output to contain command %q", cmd)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCompletionCmdExecution(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
args []string
|
|
wantOutput string
|
|
wantError bool
|
|
}{
|
|
{
|
|
name: "bash completion",
|
|
args: []string{"bash"},
|
|
wantOutput: "__start_f2b",
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "zsh completion",
|
|
args: []string{"zsh"},
|
|
wantOutput: "#compdef f2b",
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "fish completion",
|
|
args: []string{"fish"},
|
|
wantOutput: "complete -c f2b",
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "powershell completion",
|
|
args: []string{"powershell"},
|
|
wantOutput: "Register-ArgumentCompleter",
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "unsupported shell",
|
|
args: []string{"unsupported"},
|
|
wantError: true, // Cobra returns an error for invalid args due to OnlyValidArgs
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Framework doesn't support completion cmd yet, so keeping manual approach:
|
|
// Create a proper root command structure for the test
|
|
testRoot := &cobra.Command{
|
|
Use: "f2b",
|
|
Short: "Test root command",
|
|
}
|
|
|
|
// Add mock client for commands that need it
|
|
mockClient := NewMockClient()
|
|
testConfig := Config{Format: "plain"}
|
|
|
|
// Add all the f2b subcommands to create a realistic structure
|
|
testRoot.AddCommand(ListJailsCmd(mockClient, &testConfig))
|
|
testRoot.AddCommand(StatusCmd(mockClient, &testConfig))
|
|
testRoot.AddCommand(BannedCmd(mockClient, &testConfig))
|
|
testRoot.AddCommand(BanCmd(mockClient, &testConfig))
|
|
testRoot.AddCommand(UnbanCmd(mockClient, &testConfig))
|
|
testRoot.AddCommand(TestIPCmd(mockClient, &testConfig))
|
|
testRoot.AddCommand(LogsCmd(mockClient, &testConfig))
|
|
testRoot.AddCommand(LogsWatchCmd(context.Background(), mockClient, &testConfig))
|
|
testRoot.AddCommand(ServiceCmd(&testConfig))
|
|
testRoot.AddCommand(VersionCmd(&testConfig))
|
|
testRoot.AddCommand(TestFilterCmd(mockClient, &testConfig))
|
|
testRoot.AddCommand(completionCmd())
|
|
|
|
// Execute the completion command via the root
|
|
// Capture stdout
|
|
var outBuf bytes.Buffer
|
|
testRoot.SetOut(&outBuf)
|
|
|
|
// Capture stderr
|
|
var errBuf bytes.Buffer
|
|
testRoot.SetErr(&errBuf)
|
|
|
|
args := append([]string{"completion"}, tt.args...)
|
|
testRoot.SetArgs(args)
|
|
err := testRoot.Execute()
|
|
|
|
AssertError(t, err, tt.wantError, tt.name)
|
|
|
|
output := outBuf.String() + errBuf.String()
|
|
if tt.wantOutput != "" && !tt.wantError {
|
|
// Check for substring anywhere in the output, ignoring leading/trailing whitespace
|
|
if !strings.Contains(output, tt.wantOutput) {
|
|
t.Errorf("expected output to contain %q, got %q", tt.wantOutput, strings.TrimSpace(output))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInitFunctionCoverage(t *testing.T) {
|
|
// Test that init function sets up flags correctly
|
|
// We can't directly test init() but we can test its effects
|
|
|
|
// Test that persistent flags are set
|
|
if rootCmd.PersistentFlags().Lookup("log-dir") == nil {
|
|
t.Errorf("expected log-dir persistent flag to be set")
|
|
}
|
|
|
|
if rootCmd.PersistentFlags().Lookup("filter-dir") == nil {
|
|
t.Errorf("expected filter-dir persistent flag to be set")
|
|
}
|
|
|
|
if rootCmd.PersistentFlags().Lookup("format") == nil {
|
|
t.Errorf("expected format persistent flag to be set")
|
|
}
|
|
|
|
if rootCmd.PersistentFlags().Lookup("log-file") == nil {
|
|
t.Errorf("expected log-file persistent flag to be set")
|
|
}
|
|
|
|
if rootCmd.PersistentFlags().Lookup("log-level") == nil {
|
|
t.Errorf("expected log-level persistent flag to be set")
|
|
}
|
|
}
|
|
|
|
func TestPersistentPreRun(t *testing.T) {
|
|
// Test the PersistentPreRun function
|
|
if rootCmd.PersistentPreRun == nil {
|
|
t.Errorf("expected PersistentPreRun to be set")
|
|
return
|
|
}
|
|
|
|
// Create a temporary log file
|
|
tmpFile, err := os.CreateTemp(t.TempDir(), "f2b-test-*.log")
|
|
if err != nil {
|
|
t.Fatalf("failed to create temp file: %v", err)
|
|
}
|
|
defer func() {
|
|
if err := os.Remove(tmpFile.Name()); err != nil {
|
|
t.Fatalf("failed to remove temp file: %v", err)
|
|
}
|
|
}()
|
|
defer func() {
|
|
if err := tmpFile.Close(); err != nil {
|
|
t.Fatalf("failed to close temp file: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Test with log file flag
|
|
cmd := &cobra.Command{}
|
|
cmd.Flags().String("log-file", tmpFile.Name(), "test log file")
|
|
cmd.Flags().String("log-level", "debug", "test log level")
|
|
|
|
// Save original logger output
|
|
originalOutput := Logger.Out
|
|
|
|
// Run PersistentPreRun
|
|
rootCmd.PersistentPreRun(cmd, []string{})
|
|
|
|
// Restore original logger output
|
|
Logger.SetOutput(originalOutput)
|
|
|
|
// Test log level parsing
|
|
tests := []struct {
|
|
name string
|
|
logLevel string
|
|
}{
|
|
{"debug", "debug"},
|
|
{"info", "info"},
|
|
{"warn", "warn"},
|
|
{"error", "error"},
|
|
{"invalid", "invalid"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run("log_level_"+tt.name, func(_ *testing.T) {
|
|
cmd := &cobra.Command{}
|
|
cmd.Flags().String("log-file", "", "")
|
|
cmd.Flags().String("log-level", tt.logLevel, "")
|
|
|
|
// This should not panic
|
|
rootCmd.PersistentPreRun(cmd, []string{})
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPersistentPreRunWithInvalidLogFile(t *testing.T) {
|
|
// Test PersistentPreRun with invalid log file path
|
|
cmd := &cobra.Command{}
|
|
cmd.Flags().String("log-file", "/invalid/path/to/logfile.log", "invalid log file")
|
|
cmd.Flags().String("log-level", "info", "test log level")
|
|
|
|
// Capture stderr to check for error message
|
|
oldStderr := os.Stderr
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("failed to create pipe: %v", err)
|
|
}
|
|
os.Stderr = w
|
|
|
|
// This should handle the error gracefully
|
|
rootCmd.PersistentPreRun(cmd, []string{})
|
|
|
|
if err := w.Close(); err != nil {
|
|
t.Fatalf("failed to close writer: %v", err)
|
|
}
|
|
os.Stderr = oldStderr
|
|
|
|
var buf bytes.Buffer
|
|
if _, err := buf.ReadFrom(r); err != nil {
|
|
t.Fatalf("failed to read output: %v", err)
|
|
}
|
|
output := buf.String()
|
|
|
|
// Should contain error message about failed to open log file
|
|
if !strings.Contains(output, "Failed to open log file") {
|
|
t.Errorf("expected error message about failed to open log file, got: %s", output)
|
|
}
|
|
}
|
|
|
|
func TestCompletionCmdLongDescription(t *testing.T) {
|
|
cmd := completionCmd()
|
|
|
|
// Test that the long description contains instructions for all shells
|
|
expectedShells := []string{"Bash:", "Zsh:", "Fish:", "PowerShell:"}
|
|
for _, shell := range expectedShells {
|
|
if !strings.Contains(cmd.Long, shell) {
|
|
t.Errorf("expected completion long description to contain %q", shell)
|
|
}
|
|
}
|
|
|
|
// Test that it contains example commands
|
|
expectedExamples := []string{
|
|
"f2b completion bash",
|
|
"f2b completion zsh",
|
|
"f2b completion fish",
|
|
"f2b completion powershell",
|
|
}
|
|
for _, example := range expectedExamples {
|
|
if !strings.Contains(cmd.Long, example) {
|
|
t.Errorf("expected completion long description to contain example %q", example)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGlobalConfigVariable(t *testing.T) {
|
|
// Test that global cfg variable can be accessed and modified
|
|
originalCfg := cfg
|
|
defer func() { cfg = originalCfg }()
|
|
|
|
cfg = Config{
|
|
LogDir: "/test/log",
|
|
FilterDir: "/test/filter",
|
|
Format: "json",
|
|
}
|
|
|
|
if cfg.LogDir != "/test/log" {
|
|
t.Errorf("expected LogDir to be '/test/log', got %q", cfg.LogDir)
|
|
}
|
|
if cfg.FilterDir != "/test/filter" {
|
|
t.Errorf("expected FilterDir to be '/test/filter', got %q", cfg.FilterDir)
|
|
}
|
|
if cfg.Format != "json" {
|
|
t.Errorf("expected Format to be 'json', got %q", cfg.Format)
|
|
}
|
|
}
|
|
|
|
// TestExecuteIntegration tests the Execute function with different command combinations
|
|
func TestExecuteIntegration(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
args []string
|
|
config Config
|
|
setupEnv func()
|
|
cleanup func()
|
|
}{
|
|
{
|
|
name: "execute with environment variables",
|
|
args: []string{"f2b", "version"},
|
|
config: Config{
|
|
LogDir: "/tmp/test",
|
|
FilterDir: "/tmp/filters",
|
|
Format: "plain",
|
|
},
|
|
setupEnv: func() {
|
|
// Environment variables will be set using t.Setenv in test loop
|
|
},
|
|
cleanup: func() {
|
|
// Cleanup handled automatically by t.Setenv
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Integration test requires manual approach:
|
|
// Set up environment variables using t.Setenv for automatic cleanup
|
|
if tt.config.LogDir != "" {
|
|
t.Setenv("F2B_LOG_DIR", tt.config.LogDir)
|
|
}
|
|
if tt.config.FilterDir != "" {
|
|
t.Setenv("F2B_FILTER_DIR", tt.config.FilterDir)
|
|
}
|
|
|
|
client := fail2ban.NewMockClient()
|
|
|
|
// Capture output
|
|
oldStdout := os.Stdout
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("failed to create pipe: %v", err)
|
|
}
|
|
os.Stdout = w
|
|
|
|
originalArgs := os.Args
|
|
os.Args = tt.args
|
|
|
|
err = Execute(client, tt.config)
|
|
|
|
// Restore
|
|
if closeErr := w.Close(); closeErr != nil {
|
|
t.Fatalf("failed to close writer: %v", closeErr)
|
|
}
|
|
os.Stdout = oldStdout
|
|
os.Args = originalArgs
|
|
|
|
// Read output
|
|
var buf bytes.Buffer
|
|
if _, readErr := buf.ReadFrom(r); readErr != nil {
|
|
t.Fatalf("failed to read output: %v", readErr)
|
|
}
|
|
|
|
AssertError(t, err, false, tt.name)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCompletionCmdWithUnsupportedShell(t *testing.T) {
|
|
cmd := completionCmd()
|
|
|
|
// Capture stderr to check for error message
|
|
var errBuf bytes.Buffer
|
|
cmd.SetErr(&errBuf)
|
|
|
|
cmd.SetArgs([]string{"invalid-shell"})
|
|
err := cmd.Execute()
|
|
|
|
// Should return error due to Cobra's OnlyValidArgs validation
|
|
if err == nil {
|
|
t.Errorf("expected error for invalid shell type")
|
|
}
|
|
|
|
// Error should mention invalid argument
|
|
if !strings.Contains(err.Error(), "invalid argument") && !strings.Contains(err.Error(), "invalid") {
|
|
t.Errorf("expected error message about invalid argument, got: %v", err)
|
|
}
|
|
}
|
|
|
|
// Benchmark tests
|
|
func BenchmarkParseLogLevelExtended(b *testing.B) {
|
|
levels := []string{"debug", "info", "warn", "warning", "error", "fatal", "panic", "invalid", ""}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
level := levels[i%len(levels)]
|
|
parseLogLevel(level)
|
|
}
|
|
}
|
|
|
|
func BenchmarkExecute(b *testing.B) {
|
|
client := fail2ban.NewMockClient()
|
|
config := Config{
|
|
LogDir: "/tmp",
|
|
FilterDir: "/tmp",
|
|
Format: "plain",
|
|
}
|
|
|
|
// Suppress output
|
|
oldStdout := os.Stdout
|
|
devNull, err := os.Open(os.DevNull)
|
|
if err != nil {
|
|
b.Fatalf("failed to open dev null: %v", err)
|
|
}
|
|
defer func() {
|
|
if cerr := devNull.Close(); cerr != nil {
|
|
b.Fatalf("failed to close dev null: %v", cerr)
|
|
}
|
|
}()
|
|
os.Stdout = devNull
|
|
|
|
defer func() {
|
|
os.Stdout = oldStdout
|
|
}()
|
|
|
|
originalArgs := os.Args
|
|
defer func() {
|
|
os.Args = originalArgs
|
|
}()
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
os.Args = []string{"f2b", "version"}
|
|
if err := Execute(client, config); err != nil {
|
|
b.Fatalf("execute failed: %v", err)
|
|
}
|
|
}
|
|
}
|