Skip to content

Handle null HostID on calendar webhook endpoint #30130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/10744-calendar-webhook-deleted-hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Fixed bug with calendar/webhook endpoint that caused an error if the calendar event relates to a deleted host.
19 changes: 16 additions & 3 deletions ee/server/service/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ func (svc *Service) CalendarWebhook(ctx context.Context, eventUUID string, chann
}
return err
}

// Check if the event belongs to a deleted host ... in which case we do a noop (the event will eventually be
// removed by the cleanup CRON job)
if eventDetails.HostID == nil {
svc.authz.SkipAuthorization(ctx)
return nil
}

if eventDetails.TeamID == nil {
// Should not happen
return fmt.Errorf("calendar event %s has no team ID", eventUUID)
Expand Down Expand Up @@ -196,8 +204,13 @@ func (svc *Service) processCalendarEvent(ctx context.Context, eventDetails *flee
}

var hosts []fleet.HostPolicyMembershipData
hosts, err = svc.ds.GetTeamHostsPolicyMemberships(ctx, googleCalendarIntegrationConfig.Domain, team.ID, policyIDs,
&eventDetails.HostID)
hosts, err = svc.ds.GetTeamHostsPolicyMemberships(
ctx,
googleCalendarIntegrationConfig.Domain,
team.ID,
policyIDs,
eventDetails.HostID,
)
if err != nil {
return "", false, err
}
Expand Down Expand Up @@ -242,7 +255,7 @@ func (svc *Service) processCalendarEvent(ctx context.Context, eventDetails *flee
return ctxerr.Wrap(ctx, err, "save calendar event body tag")
}
_, err = svc.ds.CreateOrUpdateCalendarEvent(ctx, event.UUID, event.Email, event.StartTime, event.EndTime, event.Data,
event.TimeZone, eventDetails.HostID, fleet.CalendarWebhookStatusNone)
event.TimeZone, *eventDetails.HostID, fleet.CalendarWebhookStatusNone)
if err != nil {
return ctxerr.Wrap(ctx, err, "create or update calendar event")
}
Expand Down
2 changes: 1 addition & 1 deletion server/datastore/mysql/calendar_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func testUpdateCalendarEvent(t *testing.T, ds *Datastore) {
require.NoError(t, err)
assert.Equal(t, strings.ToUpper(eventUUIDNew), eventDetails.UUID)
assert.Equal(t, *calendarEvent, eventDetails.CalendarEvent)
assert.Equal(t, host.ID, eventDetails.HostID)
assert.Equal(t, host.ID, *eventDetails.HostID)
assert.Nil(t, eventDetails.TeamID)

// TODO(lucas): Add more tests here.
Expand Down
2 changes: 1 addition & 1 deletion server/fleet/calendar_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (ce *CalendarEvent) SaveDataItems(keysAndValues ...string) error {
type CalendarEventDetails struct {
CalendarEvent
TeamID *uint `db:"team_id"` // Should not be nil, but is nullable in the database
HostID uint `db:"host_id"`
HostID *uint `db:"host_id"`
}

type CalendarWebhookStatus int
Expand Down
8 changes: 8 additions & 0 deletions server/service/integration_enterprise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14399,6 +14399,14 @@ func (s *integrationEnterpriseTestSuite) TestCalendarCallback() {
require.Len(t, team1CalendarEvents, 1)
assert.Equal(t, previousEvent, team1CalendarEvents[0])

err = s.ds.DeleteHost(ctx, host1Team1.ID)
require.NoError(t, err)
_ = s.DoRawWithHeaders("POST", "/api/v1/fleet/calendar/webhook/"+eventRecreated.UUID, []byte(""), http.StatusOK,
map[string]string{
"X-Goog-Channel-Id": details.ChannelID,
"X-Goog-Resource-State": "exists",
})

// Trigger calendar should cleanup the events
triggerAndWait(ctx, t, s.ds, s.calendarSchedule, 5*time.Second)
assert.Equal(t, 0, calendar.MockChannelsCount())
Expand Down
Loading