From 9df8e23f7a84ea2cc1f827f0209958ba3572e6a7 Mon Sep 17 00:00:00 2001 From: Victor Lyuboslavsky <2685025+getvictor@users.noreply.github.com> Date: Mon, 8 Sep 2025 10:23:43 -0500 Subject: [PATCH] Remove scripts/software from TeamConfig copy. (#32708) Reverting some changes from #31267 because they are not needed. --- server/datastore/mysql/teams.go | 4 +- server/fleet/software_installer.go | 88 ------------------------------ server/fleet/teams.go | 16 +----- 3 files changed, 6 insertions(+), 102 deletions(-) diff --git a/server/datastore/mysql/teams.go b/server/datastore/mysql/teams.go index 28bf40bcee..cd2caac5fd 100644 --- a/server/datastore/mysql/teams.go +++ b/server/datastore/mysql/teams.go @@ -558,7 +558,9 @@ func defaultTeamConfigDB(ctx context.Context, q sqlx.QueryerContext) (*fleet.Tea // SaveDefaultTeamConfig saves the configuration for "No Team" hosts. func (ds *Datastore) SaveDefaultTeamConfig(ctx context.Context, config *fleet.TeamConfig) error { - configBytes, err := json.Marshal(config) + // Create a copy to avoid saving unsupported fields such as scripts and software + configCopy := config.Copy() + configBytes, err := json.Marshal(&configCopy) if err != nil { return ctxerr.Wrap(ctx, err, "marshaling config") } diff --git a/server/fleet/software_installer.go b/server/fleet/software_installer.go index 883e26d3ec..af214754a8 100644 --- a/server/fleet/software_installer.go +++ b/server/fleet/software_installer.go @@ -621,94 +621,6 @@ type SoftwareSpec struct { AppStoreApps optjson.Slice[TeamSpecAppStoreApp] `json:"app_store_apps,omitempty"` } -// Copy returns a deep copy of SoftwareSpec. -func (s *SoftwareSpec) Copy() *SoftwareSpec { - if s == nil { - return nil - } - - out := &SoftwareSpec{} - - // Packages - out.Packages.Set = s.Packages.Set - if s.Packages.Set { - if len(s.Packages.Value) > 0 { - out.Packages.Value = make([]SoftwarePackageSpec, len(s.Packages.Value)) - for i := range s.Packages.Value { - p := s.Packages.Value[i] - pCopy := p - if p.LabelsIncludeAny != nil { - pCopy.LabelsIncludeAny = append([]string(nil), p.LabelsIncludeAny...) - } - if p.LabelsExcludeAny != nil { - pCopy.LabelsExcludeAny = append([]string(nil), p.LabelsExcludeAny...) - } - if p.Categories != nil { - pCopy.Categories = append([]string(nil), p.Categories...) - } - if p.Slug != nil { - slugVal := *p.Slug - pCopy.Slug = &slugVal - } - out.Packages.Value[i] = pCopy - } - } else { - // Preserve Set=true even when the slice is empty - out.Packages.Value = nil - } - } - - // FleetMaintainedApps - out.FleetMaintainedApps.Set = s.FleetMaintainedApps.Set - if s.FleetMaintainedApps.Set { - if len(s.FleetMaintainedApps.Value) > 0 { - out.FleetMaintainedApps.Value = make([]MaintainedAppSpec, len(s.FleetMaintainedApps.Value)) - for i := range s.FleetMaintainedApps.Value { - m := s.FleetMaintainedApps.Value[i] - mCopy := m - if m.LabelsIncludeAny != nil { - mCopy.LabelsIncludeAny = append([]string(nil), m.LabelsIncludeAny...) - } - if m.LabelsExcludeAny != nil { - mCopy.LabelsExcludeAny = append([]string(nil), m.LabelsExcludeAny...) - } - if m.Categories != nil { - mCopy.Categories = append([]string(nil), m.Categories...) - } - out.FleetMaintainedApps.Value[i] = mCopy - } - } else { - out.FleetMaintainedApps.Value = nil - } - } - - // AppStoreApps - out.AppStoreApps.Set = s.AppStoreApps.Set - if s.AppStoreApps.Set { - if len(s.AppStoreApps.Value) > 0 { - out.AppStoreApps.Value = make([]TeamSpecAppStoreApp, len(s.AppStoreApps.Value)) - for i := range s.AppStoreApps.Value { - app := s.AppStoreApps.Value[i] - appCopy := app - if app.LabelsIncludeAny != nil { - appCopy.LabelsIncludeAny = append([]string(nil), app.LabelsIncludeAny...) - } - if app.LabelsExcludeAny != nil { - appCopy.LabelsExcludeAny = append([]string(nil), app.LabelsExcludeAny...) - } - if app.Categories != nil { - appCopy.Categories = append([]string(nil), app.Categories...) - } - out.AppStoreApps.Value[i] = appCopy - } - } else { - out.AppStoreApps.Value = nil - } - } - - return out -} - // HostSoftwareInstall represents installation of software on a host from a // Fleet software installer. type HostSoftwareInstall struct { diff --git a/server/fleet/teams.go b/server/fleet/teams.go index 30bd326aa7..2a88f27d51 100644 --- a/server/fleet/teams.go +++ b/server/fleet/teams.go @@ -369,19 +369,9 @@ func (t *TeamConfig) Copy() *TeamConfig { // Deep copy all MDM fields (includes macOS/windows custom settings and setup software) clone.MDM = *t.MDM.Copy() - // Deep copy Scripts slice - if t.Scripts.Set && len(t.Scripts.Value) > 0 { - clone.Scripts = optjson.Slice[string]{ - Set: true, - Value: make([]string, len(t.Scripts.Value)), - } - copy(clone.Scripts.Value, t.Scripts.Value) - } - - // Deep copy Software if present - if t.Software != nil { - clone.Software = t.Software.Copy() - } + // Do not copy script and software since they will not be stored/cached in the database. + clone.Scripts = optjson.Slice[string]{} + clone.Software = nil return &clone }