Skip to content

Commit

Permalink
dynamoevents: Fix panic if cursor is outside of [fromUTC,toUTC] range (
Browse files Browse the repository at this point in the history
…#42247)

If clients send a request where the cursor falls outside of the [From.UTC,To.UTC] interval, dynamodbevents will panic after consuming all possible values in `dates`.
This happens because `dates` is an array of full days between
`[from,to]`.

The patched for loop tried to discard days that were already consumed by
the latest request but if the cursor fall outside of this window, the
loop consumed all days and eventually panic.

Signed-off-by: Tiago Silva <tiago.silva@goteleport.com>
  • Loading branch information
tigrato authored Jun 1, 2024
1 parent 17fc030 commit f570dd3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/events/dynamoevents/dynamoevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,17 @@ func (l *Log) searchEventsRaw(ctx context.Context, fromUTC, toUTC time.Time, nam
return nil, "", trace.Wrap(err)
}

if checkpoint.Date != "" {
if t, err := time.Parse(time.DateOnly, checkpoint.Date); err == nil {
d := fromUTC.Unix()
// if fromUTC at 00:00:00 is bigger than the cursor,
// reset the cursor and advance to next day.
if time.Unix(d-d%(24*3600), 0).After(t) {
checkpoint = checkpointKey{}
}
}
}

totalSize := 0
dates := daysBetween(fromUTC, toUTC)
if order == types.EventOrderDescending {
Expand All @@ -668,9 +679,15 @@ func (l *Log) searchEventsRaw(ctx context.Context, fromUTC, toUTC time.Time, nam
// We need to perform a guard check on the length of `dates` here in case a query is submitted with
// `toUTC` occurring before `fromUTC`.
if checkpoint.Date != "" && len(dates) > 0 {
for dates[0] != checkpoint.Date {
for len(dates) > 0 && dates[0] != checkpoint.Date {
dates = dates[1:]
}
// if the initial data wasn't found in [fromUTC,toUTC]
// dates will be empty and we can return early since we
// won't find any events.
if len(dates) == 0 {
return nil, "", nil
}
}

foundStart := checkpoint.EventKey == ""
Expand Down
27 changes: 27 additions & 0 deletions lib/events/dynamoevents/dynamoevents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package dynamoevents

import (
"context"
"encoding/json"
"fmt"
"math/rand"
"net/url"
Expand Down Expand Up @@ -116,6 +117,32 @@ func TestSearchSessionEvensBySessionID(t *testing.T) {
tt.suite.SearchSessionEventsBySessionID(t)
}

// TestCheckpointOutsideOfWindow tests if [Log] doesn't panic
// if checkpoint date is outside of the window [fromUTC,toUTC].
func TestCheckpointOutsideOfWindow(t *testing.T) {
tt := &Log{}

key := checkpointKey{
Date: "2022-10-02",
}
keyB, err := json.Marshal(key)
require.NoError(t, err)

results, nextKey, err := tt.SearchEvents(
context.Background(),
events.SearchEventsRequest{
From: time.Date(2021, 10, 10, 0, 0, 0, 0, time.UTC),
To: time.Date(2021, 11, 10, 0, 0, 0, 0, time.UTC),
Limit: 100,
StartKey: string(keyB),
Order: types.EventOrderAscending,
},
)
require.NoError(t, err)
require.Empty(t, results)
require.Empty(t, nextKey)
}

func TestSizeBreak(t *testing.T) {
tt := setupDynamoContext(t)

Expand Down

0 comments on commit f570dd3

Please sign in to comment.