caddy icon indicating copy to clipboard operation
caddy copied to clipboard

logging: add DirMode options and propagate FileMode to rotations

Open DeRuina opened this issue 4 months ago • 3 comments

Summary

This PR enhances the FileWriter logging module to give users explicit control over directory creation permissions and ensures that rotated log files inherit the correct file mode from configuration. This solves issue #7314

Changes

  • Added DirMode option to FileWriter:

    • "inherit" → copies the nearest existing parent directory’s permissions, normalizing rx for directories.
    • "from_file" → derives directory permissions from the file’s mode (e.g., 0644 → 0755, 0600 → 0700).
    • Octal strings (e.g., "0755") → directly specify directory permissions.
    • Default remains 0700 (same as before, ensuring backward compatibility).
  • Improved permission handling:

    • When DirMode is set, directories are created using the requested mode.
    • Added normalizeDirPerm() to ensure directories with read bits also gain execute bits (so they’re traversable).
  • Propagated file mode to timberjack:

    • Added FileMode: os.FileMode(fw.Mode) to the embedded timberjack.Logger.
    • Ensures rotated log files now use the same mode as the original log file.
  • Security-conscious defaults:

    • Maintains restrictive defaults (0700 directories, 0600 files).
    • Umask still applies.
    • r→x normalization applies only to directories — not to files.

Tests Added

  • Extended test coverage in:

    • filewriter_test.go (Unix)
    • filewriter_test_window.go (Windows)
  • Added cases for:

    • DirMode values: inherit, from_file, explicit octal, and default.
    • Directory creation under different umasks.
    • Correct parsing of dir_mode from the Caddyfile.
    • Windows smoke test to ensure successful file creation across modes.

Assistance Disclosure

AI was used to generate this PR summery and also to expand some tests I wrote. Everything was reviewed by me.

DeRuina avatar Nov 03 '25 07:11 DeRuina

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Nov 03 '25 07:11 CLAassistant

Thanks! Please don't forget to sign the CLA

francislavoie avatar Nov 03 '25 07:11 francislavoie

I just signed :+1:

DeRuina avatar Nov 03 '25 09:11 DeRuina