Files
f2b/fail2ban/gzip_detection.go
Ismo Vuorinen 605f2b9580 refactor: linting, simplification and fixes (#119)
* refactor: consolidate test helpers and reduce code duplication

- Fix prealloc lint issue in cmd_logswatch_test.go
- Add validateIPAndJails helper to consolidate IP/jail validation
- Add WithTestRunner/WithTestSudoChecker helpers for cleaner test setup
- Replace setupBasicMockResponses duplicates with StandardMockSetup
- Add SetupStandardResponses/SetupJailResponses to MockRunner
- Delegate cmd context helpers to fail2ban implementations
- Document context wrapper pattern in context_helpers.go

* refactor: consolidate duplicate code patterns across cmd and fail2ban packages

Add helper functions to reduce code duplication found by dupl:

- safeCloseFile/safeCloseReader: centralize file close error logging
- createTimeoutContext: consolidate timeout context creation pattern
- withContextCheck: wrap context cancellation checks
- recordOperationMetrics: unify metrics recording for commands/clients

Also includes Phase 1 consolidations:
- copyBuckets helper for metrics snapshots
- Table-driven context extraction in logging
- processWithValidation helper for IP processors

* refactor: consolidate LoggerInterface by embedding LoggerEntry

Both interfaces had identical method signatures. LoggerInterface now
embeds LoggerEntry to eliminate code duplication.

* refactor: consolidate test framework helpers and fix test patterns

- Add checkJSONFieldValue and failMissingJSONField helpers to reduce
  duplication in JSON assertion methods
- Add ParallelTimeout to default test config
- Fix test to use WithTestRunner inside test loop for proper mock scoping

* refactor: unify ban/unban operations with OperationType pattern

Introduce OperationType struct to consolidate duplicate ban/unban logic:
- Add ProcessOperation and ProcessOperationWithContext generic functions
- Add ProcessOperationParallel and ProcessOperationParallelWithContext
- Existing ProcessBan*/ProcessUnban* functions now delegate to generic versions
- Reduces ~120 lines of duplicate code between ban and unban operations

* refactor: consolidate time parsing cache pattern

Add ParseWithLayout method to BoundedTimeCache that consolidates the
cache-lookup-parse-store pattern. FastTimeCache and TimeParsingCache
now delegate to this method instead of duplicating the logic.

* refactor: consolidate command execution patterns in fail2ban

- Add validateCommandExecution helper for command/argument validation
- Add runWithTimerContext helper for timed runner operations
- Add executeIPActionWithContext to unify BanIP/UnbanIP implementations
- Reduces duplicate validation and execution boilerplate

* refactor: consolidate logrus adapter with embedded loggerCore

Introduce loggerCore type that provides the 8 standard logging methods
(Debug, Info, Warn, Error, Debugf, Infof, Warnf, Errorf). Both
logrusAdapter and logrusEntryAdapter now embed this type, eliminating
16 duplicate method implementations.

* refactor: consolidate path validation patterns

- Add validateConfigPathWithFallback helper in cmd/config_utils.go
  for the validate-or-fallback-with-logging pattern
- Add validateClientPath helper in fail2ban/helpers.go for client
  path validation delegation

* fix: add context cancellation checks to wrapper functions

- wrapWithContext0/1/2 now check ctx.Err() before invoking wrapped function
- WithCommand now validates and trims empty command strings

* refactor: extract formatLatencyBuckets for deterministic metrics output

Add formatLatencyBuckets helper that writes latency bucket distribution
with sorted keys for deterministic output, eliminating duplicate
formatting code for command and client latency buckets.

* refactor: add generic setNestedMapValue helper for mock configuration

Add setNestedMapValue[T] generic helper that consolidates the repeated
pattern of mutex-protected nested map initialization and value setting
used by SetBanError, SetBanResult, SetUnbanError, and SetUnbanResult.

* fix: use cmd.Context() for signal propagation and correct mock status

- ExecuteIPCommand now uses cmd.Context() instead of context.Background()
  to inherit Cobra's signal cancellation
- MockRunner.SetupJailResponses uses shared.Fail2BanStatusSuccess ("0")
  instead of literal "1" for proper success path simulation

* fix: restore operation-specific log messages in ProcessOperationWithContext

Add back Logger.WithFields().Info(opType.Message) call that was lost
during refactoring. This restores the distinction between ban and unban
operation messages (shared.MsgBanResult vs shared.MsgUnbanResult).

* fix: return aggregated errors from parallel operations

Previously, errors from individual parallel operations were silently
swallowed - converted to status strings but never returned to callers.

Now processOperations collects all errors and returns them aggregated
via errors.Join, allowing callers to distinguish partial failures from
complete success while still receiving all results.

* fix: add input validation to processOperations before parallel execution

Validate IP and jail inputs at the start of processOperations() using
fail2ban.CachedValidateIP and CachedValidateJail. This prevents invalid
or malicious inputs (empty values, path traversal attempts, malformed
IPs) from reaching the operation functions. All validation errors are
aggregated and returned before any operations execute.
2026-01-25 19:07:45 +02:00

198 lines
5.8 KiB
Go

package fail2ban
import (
"bufio"
"compress/gzip"
"errors"
"io"
"os"
"strings"
"github.com/ivuorinen/f2b/shared"
)
// safeCloseFile closes a file and logs any error.
// This helper consolidates the duplicate close-and-log pattern.
func safeCloseFile(f *os.File, path string) {
if f == nil {
return
}
if closeErr := f.Close(); closeErr != nil {
getLogger().WithError(closeErr).
WithField(shared.LogFieldFile, path).
Warn("Failed to close file")
}
}
// safeCloseReader closes a reader and logs any error.
// This helper consolidates the duplicate close-and-log pattern for io.ReadCloser.
func safeCloseReader(r io.ReadCloser, path string) {
if r == nil {
return
}
if closeErr := r.Close(); closeErr != nil {
getLogger().WithError(closeErr).
WithField(shared.LogFieldFile, path).
Warn("Failed to close reader")
}
}
// GzipDetector provides utilities for detecting and handling gzip-compressed files
type GzipDetector struct{}
// NewGzipDetector creates a new gzip detector instance
func NewGzipDetector() *GzipDetector {
return &GzipDetector{}
}
// IsGzipFile checks if a file is gzip compressed by examining the file extension first,
// then falling back to magic byte detection for better performance
func (gd *GzipDetector) IsGzipFile(path string) (bool, error) {
// Fast path: check file extension first
if strings.HasSuffix(strings.ToLower(path), shared.GzipExtension) {
return true, nil
}
// Fallback: check magic bytes for files without .gz extension
return gd.hasGzipMagicBytes(path)
}
// hasGzipMagicBytes checks if a file has gzip magic bytes (0x1f, 0x8b)
func (gd *GzipDetector) hasGzipMagicBytes(path string) (bool, error) {
// #nosec G304 - Path is validated by caller, this is a legitimate file operation
f, err := os.Open(path)
if err != nil {
return false, err
}
defer safeCloseFile(f, path)
var magic [2]byte
n, err := f.Read(magic[:])
if err != nil && !errors.Is(err, io.EOF) {
return false, err
}
// Check if we have gzip magic bytes (0x1f, 0x8b)
if n < 2 {
return false, nil
}
// #nosec G602 - Length check above guarantees slice has at least 2 elements
return magic[0] == 0x1f && magic[1] == 0x8b, nil
}
// OpenGzipAwareReader opens a file and returns appropriate reader (gzip or regular)
func (gd *GzipDetector) OpenGzipAwareReader(path string) (io.ReadCloser, error) {
// #nosec G304 - Path is validated by caller, this is a legitimate file operation
f, err := os.Open(path)
if err != nil {
return nil, err
}
isGzip, err := gd.IsGzipFile(path)
if err != nil {
safeCloseFile(f, path)
return nil, err
}
if isGzip {
// For gzip files, we need to position at the beginning and create gzip reader
_, err = f.Seek(0, io.SeekStart)
if err != nil {
safeCloseFile(f, path)
return nil, err
}
gz, err := gzip.NewReader(f)
if err != nil {
safeCloseFile(f, path)
return nil, err
}
// Return a composite closer that closes both gzip reader and file
return &gzipFileReader{gz: gz, file: f}, nil
}
return f, nil
}
// CreateGzipAwareScanner creates a scanner for the file, handling gzip compression automatically
func (gd *GzipDetector) CreateGzipAwareScanner(path string) (*bufio.Scanner, func(), error) {
return gd.CreateGzipAwareScannerWithBuffer(path, 0)
}
// CreateGzipAwareScannerWithBuffer creates a scanner with custom buffer size
func (gd *GzipDetector) CreateGzipAwareScannerWithBuffer(path string, maxLineSize int) (*bufio.Scanner, func(), error) {
reader, err := gd.OpenGzipAwareReader(path)
if err != nil {
return nil, nil, err
}
scanner := bufio.NewScanner(reader)
// Set buffer size limit if specified
if maxLineSize > 0 {
buf := make([]byte, 0, maxLineSize)
scanner.Buffer(buf, maxLineSize)
}
cleanup := func() {
safeCloseReader(reader, path)
}
return scanner, cleanup, nil
}
// gzipFileReader wraps both gzip.Reader and os.File to ensure both are closed
type gzipFileReader struct {
gz *gzip.Reader
file *os.File
}
func (gfr *gzipFileReader) Read(p []byte) (n int, err error) {
return gfr.gz.Read(p)
}
func (gfr *gzipFileReader) Close() error {
// Close gzip reader first
gzErr := gfr.gz.Close()
// Then close file
fileErr := gfr.file.Close()
// Return the first error encountered
if gzErr != nil {
return gzErr
}
return fileErr
}
// Global detector instance for convenience
var defaultGzipDetector = NewGzipDetector()
// IsGzipFile checks if a file is gzip compressed using the default detector.
// SECURITY: The caller must validate and sanitize the path argument to prevent
// path traversal attacks and ensure the file is within allowed directories.
func IsGzipFile(path string) (bool, error) {
return defaultGzipDetector.IsGzipFile(path)
}
// OpenGzipAwareReader opens a file with automatic gzip detection using the default detector.
// SECURITY: The caller must validate and sanitize the path argument to prevent
// path traversal attacks and ensure the file is within allowed directories.
func OpenGzipAwareReader(path string) (io.ReadCloser, error) {
return defaultGzipDetector.OpenGzipAwareReader(path)
}
// CreateGzipAwareScanner creates a scanner with automatic gzip detection using the default detector.
// SECURITY: The caller must validate and sanitize the path argument to prevent
// path traversal attacks and ensure the file is within allowed directories.
func CreateGzipAwareScanner(path string) (*bufio.Scanner, func(), error) {
return defaultGzipDetector.CreateGzipAwareScanner(path)
}
// CreateGzipAwareScannerWithBuffer creates a scanner with custom buffer size using the default detector.
// SECURITY: The caller must validate and sanitize the path argument to prevent
// path traversal attacks and ensure the file is within allowed directories.
func CreateGzipAwareScannerWithBuffer(path string, maxLineSize int) (*bufio.Scanner, func(), error) {
return defaultGzipDetector.CreateGzipAwareScannerWithBuffer(path, maxLineSize)
}