From 6b2ab24aeb417cb7481e600f39c98e1ce56a5ff1 Mon Sep 17 00:00:00 2001 From: Victor Lyuboslavsky Date: Tue, 16 Apr 2024 15:15:08 -0500 Subject: [PATCH] Fixed fleetctl gitops dry-run validation issues when enabling calendar integration for the first time. (#18301) #18299 Fixed fleetctl gitops dry-run validation issues when enabling calendar integration for the first time. # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [x] Added/updated tests - [x] Manual QA for all new/changed functionality --- changes/18299-gitops-calendar-validation | 1 + cmd/fleetctl/gitops_test.go | 30 ++++++++++++++---------- ee/server/service/teams.go | 10 ++++---- 3 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 changes/18299-gitops-calendar-validation diff --git a/changes/18299-gitops-calendar-validation b/changes/18299-gitops-calendar-validation new file mode 100644 index 0000000000..7de5bae83b --- /dev/null +++ b/changes/18299-gitops-calendar-validation @@ -0,0 +1 @@ +Fixed fleetctl gitops dry-run validation issues when enabling calendar integration for the first time. diff --git a/cmd/fleetctl/gitops_test.go b/cmd/fleetctl/gitops_test.go index 0c4947ca29..283c7dd32b 100644 --- a/cmd/fleetctl/gitops_test.go +++ b/cmd/fleetctl/gitops_test.go @@ -379,23 +379,23 @@ func TestFullTeamGitOps(t *testing.T) { // License is not needed because we are not using any premium features in our config. _, ds := runServerWithMockedDS( t, &service.TestServerOpts{ - License: license, - MDMStorage: new(mock.MDMAppleStore), - MDMPusher: mockPusher{}, - FleetConfig: &fleetCfg, + License: license, + MDMStorage: new(mock.MDMAppleStore), + MDMPusher: mockPusher{}, + FleetConfig: &fleetCfg, + NoCacheDatastore: true, }, ) + appConfig := fleet.AppConfig{ + // During dry run, the global calendar integration setting may not be set + MDM: fleet.MDM{ + EnabledAndConfigured: true, + WindowsEnabledAndConfigured: true, + }, + } ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) { - return &fleet.AppConfig{ - MDM: fleet.MDM{ - EnabledAndConfigured: true, - WindowsEnabledAndConfigured: true, - }, - Integrations: fleet.Integrations{ - GoogleCalendar: []*fleet.GoogleCalendarIntegration{{}}, - }, - }, nil + return &appConfig, nil } var appliedScripts []*fleet.Script @@ -525,6 +525,10 @@ func TestFullTeamGitOps(t *testing.T) { assert.Len(t, appliedWinProfiles, 0) // Real run + // Setting global calendar config + appConfig.Integrations = fleet.Integrations{ + GoogleCalendar: []*fleet.GoogleCalendarIntegration{{}}, + } _ = runAppForTest(t, []string{"gitops", "-f", file}) require.NotNil(t, savedTeam) assert.Equal(t, teamName, savedTeam.Name) diff --git a/ee/server/service/teams.go b/ee/server/service/teams.go index 40a79017e9..d7dda71261 100644 --- a/ee/server/service/teams.go +++ b/ee/server/service/teams.go @@ -214,7 +214,7 @@ func (svc *Service) ModifyTeam(ctx context.Context, teamID uint, payload fleet.T // Only update the calendar integration if it's not nil if payload.Integrations.GoogleCalendar != nil { invalid := &fleet.InvalidArgumentError{} - _ = svc.validateTeamCalendarIntegrations(payload.Integrations.GoogleCalendar, appCfg, invalid) + _ = svc.validateTeamCalendarIntegrations(payload.Integrations.GoogleCalendar, appCfg, false, invalid) if invalid.HasErrors() { return nil, ctxerr.Wrap(ctx, invalid) } @@ -1095,7 +1095,7 @@ func (svc *Service) editTeamFromSpec( } if spec.Integrations.GoogleCalendar != nil { - err = svc.validateTeamCalendarIntegrations(spec.Integrations.GoogleCalendar, appCfg, invalid) + err = svc.validateTeamCalendarIntegrations(spec.Integrations.GoogleCalendar, appCfg, dryRun, invalid) if err != nil { return ctxerr.Wrap(ctx, err, "validate team calendar integrations") } @@ -1170,13 +1170,13 @@ func (svc *Service) editTeamFromSpec( func (svc *Service) validateTeamCalendarIntegrations( calendarIntegration *fleet.TeamGoogleCalendarIntegration, - appCfg *fleet.AppConfig, invalid *fleet.InvalidArgumentError, + appCfg *fleet.AppConfig, dryRun bool, invalid *fleet.InvalidArgumentError, ) error { if !calendarIntegration.Enable { return nil } - // Check that global configs exist - if len(appCfg.Integrations.GoogleCalendar) == 0 { + // Check that global configs exist. During dry run, the global config may not be available yet. + if len(appCfg.Integrations.GoogleCalendar) == 0 && !dryRun { invalid.Append("integrations.google_calendar.enable_calendar_events", "global Google Calendar integration is not configured") } // Validate URL