From b95f5ac50c899873353a1fcba3fe264b39bd2b44 Mon Sep 17 00:00:00 2001 From: Scott Gress Date: Thu, 6 Mar 2025 16:42:58 -0600 Subject: [PATCH] Don't allow gitops to disable gitops mode (#26794) For #26743 This PR fixes an issue where using `fleetctl gitops` unsets the "gitops mode" settings in the UI. The code which prepares the config spec to send to the "modify config" endpoint deliberately copies over the current app settings for gitops mode to facilitate this. I updated an existing test to verify the new behavior. --- cmd/fleetctl/gitops_test.go | 11 ++++++++++- server/service/client.go | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cmd/fleetctl/gitops_test.go b/cmd/fleetctl/gitops_test.go index 0e3bd5d90d..90e87c6ad1 100644 --- a/cmd/fleetctl/gitops_test.go +++ b/cmd/fleetctl/gitops_test.go @@ -234,7 +234,13 @@ func TestGitOpsBasicGlobalPremium(t *testing.T) { // Mock appConfig savedAppConfig := &fleet.AppConfig{} ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) { - return &fleet.AppConfig{}, nil + return &fleet.AppConfig{ + // Set a GitOps UI mode to verify that applying GitOps config won't overwrite it. + UIGitOpsMode: fleet.UIGitOpsModeConfig{ + GitopsModeEnabled: true, + RepositoryURL: "https://didsomeonesaygitops.biz", + }, + }, nil } ds.SaveAppConfigFunc = func(ctx context.Context, config *fleet.AppConfig) error { savedAppConfig = config @@ -326,6 +332,9 @@ software: assert.Empty(t, enrolledSecrets) assert.True(t, savedAppConfig.Integrations.NDESSCEPProxy.Valid) assert.Equal(t, "https://ndes.example.com/scep", savedAppConfig.Integrations.NDESSCEPProxy.Value.URL) + // GitOps should not overwrite GitOps UI Mode. + assert.Equal(t, savedAppConfig.UIGitOpsMode.GitopsModeEnabled, true) + assert.Equal(t, savedAppConfig.UIGitOpsMode.RepositoryURL, "https://didsomeonesaygitops.biz") } func TestGitOpsBasicTeam(t *testing.T) { diff --git a/server/service/client.go b/server/service/client.go index 895fb03a60..8f3ed38c8e 100644 --- a/server/service/client.go +++ b/server/service/client.go @@ -574,6 +574,14 @@ func (c *Client) ApplyGroup( specs.AppConfig.(map[string]interface{})["yara_rules"] = rulePayloads } + // Keep any existing GitOps mode config rather than attempting to set via GitOps. + if appconfig != nil { + specs.AppConfig.(map[string]interface{})["gitops"] = fleet.UIGitOpsModeConfig{ + GitopsModeEnabled: appconfig.UIGitOpsMode.GitopsModeEnabled, + RepositoryURL: appconfig.UIGitOpsMode.RepositoryURL, + } + } + if err := c.ApplyAppConfig(specs.AppConfig, opts.ApplySpecOptions); err != nil { return nil, nil, nil, nil, fmt.Errorf("applying fleet config: %w", err) }