Skip to content

Commit ad680d8

Browse files
committed
fix: don't expose the old log file time format string
Signed-off-by: Xe Iaso <me@xeiaso.net>
1 parent 7a9590e commit ad680d8

4 files changed

Lines changed: 46 additions & 74 deletions

File tree

docs/docs/admin/policies.mdx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,14 @@ This isn't currently used by Anubis, but will be in the future for "slightly imp
372372

373373
The `file` sink makes Anubis write its logs to the filesystem and rotate them out when the log file meets certain thresholds. This logging sink takes the following parameters:
374374

375-
| Name | Type | Example | Description |
376-
| :------------------ | :-------------------- | :-------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
377-
| `file` | string | `/var/log/anubis.log` | The file where Anubis logs should be written to. Make sure the user Anubis is running as has write and file creation permissions to this directory. |
378-
| `maxBackups` | number | `3` | The number of old log files that should be maintained when log files are rotated out. |
379-
| `maxBytes` | number of bytes | `67108864` (64Mi) | The maximum size of each log file before it is rotated out. |
380-
| `maxAge` | number of days | `7` | If a log file is more than this many days old, rotate it out. |
381-
| `oldFileTimeFormat` | Go time format string | `2006-01-02T15-04-05` | A [Go time format string](https://pkg.go.dev/time#Layout) that describes how the filenames of old log files should be constructed. This default somewhat conforms to [RFC 3339](https://www.rfc-editor.org/rfc/rfc3339). |
382-
| `compress` | boolean | `true` | If true, compress old log files with gzip. This should be set to `true` and is only exposed as an option for dealing with legacy workflows where there is pre-existing magical thinking about log files at play. |
383-
| `useLocalTime` | boolean | `false` | If true, use the system local time zone to create log filenames instead of UTC. This should almost always be set to `false` and is only exposed for legacy workflows where there is pre-existing magical thinking about time zones at play. |
375+
| Name | Type | Example | Description |
376+
| :------------- | :-------------- | :-------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
377+
| `file` | string | `/var/log/anubis.log` | The file where Anubis logs should be written to. Make sure the user Anubis is running as has write and file creation permissions to this directory. |
378+
| `maxBackups` | number | `3` | The number of old log files that should be maintained when log files are rotated out. |
379+
| `maxBytes` | number of bytes | `67108864` (64Mi) | The maximum size of each log file before it is rotated out. |
380+
| `maxAge` | number of days | `7` | If a log file is more than this many days old, rotate it out. |
381+
| `compress` | boolean | `true` | If true, compress old log files with gzip. This should be set to `true` and is only exposed as an option for dealing with legacy workflows where there is magical thinking about log files at play. |
382+
| `useLocalTime` | boolean | `false` | If true, use the system local time zone to create log filenames instead of UTC. This should almost always be set to `false` and is only exposed for legacy workflows where there is magical thinking about time zones at play. |
384383

385384
```yaml
386385
logging:
@@ -390,11 +389,12 @@ logging:
390389
maxBackups: 3 # keep at least 3 old copies
391390
maxBytes: 67108864 # each file can have up to 64 Mi of logs
392391
maxAge: 7 # rotate files out every n days
393-
oldFileTimeFormat: 2006-01-02T15-04-05 # RFC 3339-ish
394392
compress: true # gzip-compress old log files
395393
useLocalTime: false # timezone for rotated files is UTC
396394
```
397395

396+
When files are rotated out, the old files will be named after the rotation timestamp in [RFC 3339 format](https://www.rfc-editor.org/rfc/rfc3339).
397+
398398
### `stdio` sink
399399

400400
By default, Anubis logs everything to the standard error stream of its process. This requires no configuration:

lib/config/logging.go

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import (
44
"errors"
55
"fmt"
66
"log/slog"
7-
"time"
87
)
98

109
var (
11-
ErrMissingLoggingFileConfig = errors.New("config.Logging: missing value parameters in logging block")
12-
ErrInvalidLoggingSink = errors.New("config.Logging: invalid sink")
13-
ErrInvalidLoggingFileConfig = errors.New("config.LoggingFileConfig: invalid parameters")
14-
ErrInvalidLoggingFileConfigOldTimeFormat = errors.New("config.LoggingFileConfig: invalid old time format")
15-
ErrOutOfRange = errors.New("config: error out of range")
10+
ErrMissingLoggingFileConfig = errors.New("config.Logging: missing value parameters in logging block")
11+
ErrInvalidLoggingSink = errors.New("config.Logging: invalid sink")
12+
ErrInvalidLoggingFileConfig = errors.New("config.LoggingFileConfig: invalid parameters")
13+
ErrOutOfRange = errors.New("config: error out of range")
1614
)
1715

1816
type Logging struct {
@@ -58,13 +56,12 @@ func (Logging) Default() *Logging {
5856
}
5957

6058
type LoggingFileConfig struct {
61-
Filename string `json:"file"`
62-
OldFileTimeFormat string `json:"oldFileTimeFormat"`
63-
MaxBackups int `json:"maxBackups"`
64-
MaxBytes int64 `json:"maxBytes"`
65-
MaxAge int `json:"maxAge"`
66-
Compress bool `json:"compress"`
67-
UseLocalTime bool `json:"useLocalTime"`
59+
Filename string `json:"file"`
60+
MaxBackups int `json:"maxBackups"`
61+
MaxBytes int64 `json:"maxBytes"`
62+
MaxAge int `json:"maxAge"`
63+
Compress bool `json:"compress"`
64+
UseLocalTime bool `json:"useLocalTime"`
6865
}
6966

7067
func (lfc *LoggingFileConfig) Valid() error {
@@ -82,10 +79,6 @@ func (lfc *LoggingFileConfig) Valid() error {
8279
errs = append(errs, fmt.Errorf("%w: filename", ErrMissingValue))
8380
}
8481

85-
if _, err := time.Parse(lfc.OldFileTimeFormat, time.Now().UTC().Format(time.RFC3339)); err != nil {
86-
errs = append(errs, fmt.Errorf("%w: %w", ErrInvalidLoggingFileConfigOldTimeFormat, err))
87-
}
88-
8982
if lfc.MaxBackups < 0 {
9083
errs = append(errs, fmt.Errorf("%w: max backup count %d is not greater than or equal to zero", ErrOutOfRange, lfc.MaxBackups))
9184
}
@@ -105,7 +98,6 @@ func (lfc *LoggingFileConfig) Valid() error {
10598
func (lfc LoggingFileConfig) Zero() bool {
10699
for _, cond := range []bool{
107100
lfc.Filename != "",
108-
lfc.OldFileTimeFormat != "",
109101
lfc.MaxBackups != 0,
110102
lfc.MaxBytes != 0,
111103
lfc.MaxAge != 0,
@@ -122,12 +114,11 @@ func (lfc LoggingFileConfig) Zero() bool {
122114

123115
func (LoggingFileConfig) Default() *LoggingFileConfig {
124116
return &LoggingFileConfig{
125-
Filename: "./var/anubis.log",
126-
OldFileTimeFormat: time.RFC3339,
127-
MaxBackups: 3,
128-
MaxBytes: 104857600, // 100 Mi
129-
MaxAge: 7, // 7 days
130-
Compress: true,
131-
UseLocalTime: false,
117+
Filename: "./var/anubis.log",
118+
MaxBackups: 3,
119+
MaxBytes: 104857600, // 100 Mi
120+
MaxAge: 7, // 7 days
121+
Compress: true,
122+
UseLocalTime: false,
132123
}
133124
}

lib/config/logging_test.go

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package config
33
import (
44
"errors"
55
"testing"
6-
"time"
76
)
87

98
func TestLoggingValid(t *testing.T) {
@@ -50,45 +49,27 @@ func TestLoggingValid(t *testing.T) {
5049
input: &Logging{
5150
Sink: LogSinkFile,
5251
Parameters: &LoggingFileConfig{
53-
Filename: "",
54-
OldFileTimeFormat: time.RFC3339,
55-
MaxBackups: 3,
56-
MaxBytes: 104857600, // 100 Mi
57-
MaxAge: 7, // 7 days
58-
Compress: true,
59-
UseLocalTime: false,
52+
Filename: "",
53+
MaxBackups: 3,
54+
MaxBytes: 104857600, // 100 Mi
55+
MaxAge: 7, // 7 days
56+
Compress: true,
57+
UseLocalTime: false,
6058
},
6159
},
6260
want: ErrMissingValue,
6361
},
64-
{
65-
name: "file sink with wrong time format",
66-
input: &Logging{
67-
Sink: LogSinkFile,
68-
Parameters: &LoggingFileConfig{
69-
Filename: "./var/anubis.log",
70-
OldFileTimeFormat: "2025-01-01",
71-
MaxBackups: 3,
72-
MaxBytes: 104857600, // 100 Mi
73-
MaxAge: 7, // 7 days
74-
Compress: true,
75-
UseLocalTime: false,
76-
},
77-
},
78-
want: ErrInvalidLoggingFileConfigOldTimeFormat,
79-
},
8062
{
8163
name: "file sink with negative max backups",
8264
input: &Logging{
8365
Sink: LogSinkFile,
8466
Parameters: &LoggingFileConfig{
85-
Filename: "./var/anubis.log",
86-
OldFileTimeFormat: time.RFC3339,
87-
MaxBackups: -3,
88-
MaxBytes: 104857600, // 100 Mi
89-
MaxAge: 7, // 7 days
90-
Compress: true,
91-
UseLocalTime: false,
67+
Filename: "./var/anubis.log",
68+
MaxBackups: -3,
69+
MaxBytes: 104857600, // 100 Mi
70+
MaxAge: 7, // 7 days
71+
Compress: true,
72+
UseLocalTime: false,
9273
},
9374
},
9475
want: ErrOutOfRange,
@@ -98,13 +79,12 @@ func TestLoggingValid(t *testing.T) {
9879
input: &Logging{
9980
Sink: LogSinkFile,
10081
Parameters: &LoggingFileConfig{
101-
Filename: "./var/anubis.log",
102-
OldFileTimeFormat: time.RFC3339,
103-
MaxBackups: 3,
104-
MaxBytes: 104857600, // 100 Mi
105-
MaxAge: -7, // 7 days
106-
Compress: true,
107-
UseLocalTime: false,
82+
Filename: "./var/anubis.log",
83+
MaxBackups: 3,
84+
MaxBytes: 104857600, // 100 Mi
85+
MaxAge: -7, // 7 days
86+
Compress: true,
87+
UseLocalTime: false,
10888
},
10989
},
11090
want: ErrOutOfRange,

lib/policy/policy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log/slog"
99
"os"
1010
"sync/atomic"
11+
"time"
1112

1213
"github.com/TecharoHQ/anubis/internal"
1314
"github.com/TecharoHQ/anubis/lib/config"
@@ -216,7 +217,7 @@ func ParseConfig(ctx context.Context, fin io.Reader, fname string, defaultDiffic
216217
case config.LogSinkFile:
217218
out := &logrotate.Logger{
218219
Filename: c.Logging.Parameters.Filename,
219-
FilenameTimeFormat: c.Logging.Parameters.OldFileTimeFormat,
220+
FilenameTimeFormat: time.RFC3339,
220221
MaxBytes: c.Logging.Parameters.MaxBytes,
221222
MaxAge: c.Logging.Parameters.MaxAge,
222223
MaxBackups: c.Logging.Parameters.MaxBackups,

0 commit comments

Comments
 (0)