Disable calendar callbacks (#20547)

This commit is contained in:
Tim Lee 2024-07-17 15:27:03 -06:00 committed by GitHub
parent c5657016c6
commit a47db8bbaf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 71 additions and 66 deletions

View file

@ -8,7 +8,6 @@ import (
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
@ -192,35 +191,41 @@ func (lowLevelAPI *GoogleCalendarLowLevelAPI) DeleteEvent(id string) error {
}
func (lowLevelAPI *GoogleCalendarLowLevelAPI) Watch(eventUUID string, channelID string, ttl uint64) (resourceID string, err error) {
resp, err := lowLevelAPI.withRetry(
func() (any, error) {
return lowLevelAPI.service.Events.Watch(calendarID, &calendar.Channel{
Id: channelID, // channelID is also used for authentication -- it should be a random value
Type: "web_hook",
Address: fmt.Sprintf("%s/api/v1/fleet/calendar/webhook/%s",
lowLevelAPI.serverURL, eventUUID),
Params: map[string]string{
"ttl": strconv.FormatUint(ttl, 10),
},
}).EventTypes("default").Do()
},
)
if err != nil {
return "", err
}
return resp.(*calendar.Channel).ResourceId, nil
// Disabling this feature to address bugs
return "", nil
// resp, err := lowLevelAPI.withRetry(
// func() (any, error) {
// return lowLevelAPI.service.Events.Watch(calendarID, &calendar.Channel{
// Id: channelID, // channelID is also used for authentication -- it should be a random value
// Type: "web_hook",
// Address: fmt.Sprintf("%s/api/v1/fleet/calendar/webhook/%s",
// lowLevelAPI.serverURL, eventUUID),
// Params: map[string]string{
// "ttl": strconv.FormatUint(ttl, 10),
// },
// }).EventTypes("default").Do()
// },
// )
// if err != nil {
// return "", err
// }
// return resp.(*calendar.Channel).ResourceId, nil
}
func (lowLevelAPI *GoogleCalendarLowLevelAPI) Stop(channelID string, resourceID string) error {
_, err := lowLevelAPI.withRetry(
func() (any, error) {
return nil, lowLevelAPI.service.Channels.Stop(&calendar.Channel{
Id: channelID,
ResourceId: resourceID,
}).Do()
},
)
return err
// Disabling this feature to address bugs
return nil
// _, err := lowLevelAPI.withRetry(
// func() (any, error) {
// return nil, lowLevelAPI.service.Channels.Stop(&calendar.Channel{
// Id: channelID,
// ResourceId: resourceID,
// }).Do()
// },
// )
// return err
}
func (lowLevelAPI *GoogleCalendarLowLevelAPI) withRetry(fn func() (any, error)) (any, error) {

View file

@ -2,55 +2,53 @@ package service
import (
"context"
"net/http"
"net/url"
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/gorilla/mux"
)
type calendarWebhookRequest struct {
eventUUID string
googleChannelID string
googleResourceState string
}
// Disabling the calendarWebhookEndpoint to address bugs
// type calendarWebhookRequest struct {
// eventUUID string
// googleChannelID string
// googleResourceState string
// }
// DecodeRequest implement requestDecoder interface to take full control of decoding the request
func (calendarWebhookRequest) DecodeRequest(_ context.Context, r *http.Request) (interface{}, error) {
var req calendarWebhookRequest
eventUUID, ok := mux.Vars(r)["event_uuid"]
if !ok {
return nil, errBadRoute
}
unescaped, err := url.PathUnescape(eventUUID)
if err != nil {
return "", ctxerr.Wrap(r.Context(), err, "unescape value in path")
}
req.eventUUID = unescaped
// func (calendarWebhookRequest) DecodeRequest(_ context.Context, r *http.Request) (interface{}, error) {
// var req calendarWebhookRequest
// eventUUID, ok := mux.Vars(r)["event_uuid"]
// if !ok {
// return nil, errBadRoute
// }
// unescaped, err := url.PathUnescape(eventUUID)
// if err != nil {
// return "", ctxerr.Wrap(r.Context(), err, "unescape value in path")
// }
// req.eventUUID = unescaped
req.googleChannelID = r.Header.Get("X-Goog-Channel-Id")
req.googleResourceState = r.Header.Get("X-Goog-Resource-State")
// req.googleChannelID = r.Header.Get("X-Goog-Channel-Id")
// req.googleResourceState = r.Header.Get("X-Goog-Resource-State")
return &req, nil
}
// return &req, nil
// }
type calendarWebhookResponse struct {
Err error `json:"error,omitempty"`
}
// type calendarWebhookResponse struct {
// Err error `json:"error,omitempty"`
// }
func (r calendarWebhookResponse) error() error { return r.Err }
// func (r calendarWebhookResponse) error() error { return r.Err }
func calendarWebhookEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (errorer, error) {
req := request.(*calendarWebhookRequest)
err := svc.CalendarWebhook(ctx, req.eventUUID, req.googleChannelID, req.googleResourceState)
if err != nil {
return calendarWebhookResponse{Err: err}, err
}
// func calendarWebhookEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (errorer, error) {
// req := request.(*calendarWebhookRequest)
// err := svc.CalendarWebhook(ctx, req.eventUUID, req.googleChannelID, req.googleResourceState)
// if err != nil {
// return calendarWebhookResponse{Err: err}, err
// }
resp := calendarWebhookResponse{}
return resp, nil
}
// resp := calendarWebhookResponse{}
// return resp, nil
// }
func (svc *Service) CalendarWebhook(ctx context.Context, eventUUID string, channelID string, resourceState string) error {
// skipauth: No authorization check needed due to implementation returning only license error.

View file

@ -941,7 +941,8 @@ func attachFleetAPIRoutes(r *mux.Router, svc fleet.Service, config config.FleetC
ne.HEAD("/api/fleet/orbit/ping", orbitPingEndpoint, orbitPingRequest{})
// This is a callback endpoint for calendar integration -- it is called to notify an event change in a user calendar
ne.POST("/api/_version_/fleet/calendar/webhook/{event_uuid}", calendarWebhookEndpoint, calendarWebhookRequest{})
// Disabling the calendarWebhookEndpoint to address bugs
// ne.POST("/api/_version_/fleet/calendar/webhook/{event_uuid}", calendarWebhookEndpoint, calendarWebhookRequest{})
neAppleMDM.WithCustomMiddleware(limiter.Limit("login", throttled.RateQuota{MaxRate: loginRateLimit, MaxBurst: 9})).
POST("/api/_version_/fleet/mdm/sso", initiateMDMAppleSSOEndpoint, initiateMDMAppleSSORequest{})

View file

@ -10983,8 +10983,9 @@ func (s *integrationEnterpriseTestSuite) TestPKGSoftwareReconciliation() {
}
func (s *integrationEnterpriseTestSuite) TestCalendarCallback() {
ctx := context.Background()
t := s.T()
t.Skip("disabled calendar callbacks to address bugs")
ctx := context.Background()
t.Cleanup(func() {
calendar.ClearMockEvents()
calendar.ClearMockChannels()