Skip to content

Commit

Permalink
RetentionConfig: restrict days to uint16
Browse files Browse the repository at this point in the history
As discussed in #821, allowing huge retention values - as the type
uint64 allows - may result in overflows. Especially Go's time.AddDate()
method silently overflows for huge values, resulting in unexpected
comparison times.

> $ ./icingadb --config <(echo 'retention: {history-days: -1}')
> can't parse YAML file /proc/self/fd/11: cannot unmarshal -1 into Go value of type uint16 ( overflow )
>
> $ ./icingadb --config <(echo 'retention: {history-days: -100000}')
> can't parse YAML file /proc/self/fd/11: cannot unmarshal -100000 into Go value of type uint16 ( overflow )
  • Loading branch information
oxzi committed Oct 10, 2024
1 parent 20fbda2 commit 5581c83
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ type Flags struct {

// RetentionConfig defines configuration for history retention.
type RetentionConfig struct {
HistoryDays uint64 `yaml:"history-days"`
SlaDays uint64 `yaml:"sla-days"`
HistoryDays uint16 `yaml:"history-days"`
SlaDays uint16 `yaml:"sla-days"`
Interval time.Duration `yaml:"interval" default:"1h"`
Count uint64 `yaml:"count" default:"5000"`
Options history.RetentionOptions `yaml:"options"`
Expand Down
12 changes: 6 additions & 6 deletions pkg/icingadb/history/retention.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ var RetentionStatements = []retentionStatement{{
}}

// RetentionOptions defines the non-default mapping of history categories with their retention period in days.
type RetentionOptions map[string]uint64
type RetentionOptions map[string]uint16

// Validate checks constraints in the supplied retention options and
// returns an error if they are violated.
Expand All @@ -120,16 +120,16 @@ func (o RetentionOptions) Validate() error {
type Retention struct {
db *database.DB
logger *logging.Logger
historyDays uint64
slaDays uint64
historyDays uint16
slaDays uint16
interval time.Duration
count uint64
options RetentionOptions
}

// NewRetention returns a new Retention.
func NewRetention(
db *database.DB, historyDays uint64, slaDays uint64, interval time.Duration,
db *database.DB, historyDays, slaDays uint16, interval time.Duration,
count uint64, options RetentionOptions, logger *logging.Logger,
) *Retention {
return &Retention{
Expand All @@ -156,7 +156,7 @@ func (r *Retention) Start(ctx context.Context) error {
errs := make(chan error, 1)

for _, stmt := range RetentionStatements {
var days uint64
var days uint16
switch stmt.RetentionType {
case RetentionHistory:
if d, ok := r.options[stmt.Category]; ok {
Expand All @@ -177,7 +177,7 @@ func (r *Retention) Start(ctx context.Context) error {
fmt.Sprintf("Starting history retention for category %s", stmt.Category),
zap.Uint64("count", r.count),
zap.Duration("interval", r.interval),
zap.Uint64("retention-days", days),
zap.Uint16("retention-days", days),
)

stmt := stmt
Expand Down

0 comments on commit 5581c83

Please sign in to comment.