From 33469077dc556f5a0b7ae7beada8826f22814eac Mon Sep 17 00:00:00 2001 From: Ian Littman Date: Sat, 7 Sep 2024 20:44:46 -0500 Subject: [PATCH] Cleanup on activities, software installer handling --- docs/Contributing/Audit-logs.md | 2 +- ee/server/service/software_installers.go | 27 +++++++++++++++--------- server/fleet/activities.go | 4 ++-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/docs/Contributing/Audit-logs.md b/docs/Contributing/Audit-logs.md index 017b5b4b35a4..37af48f8cc3c 100644 --- a/docs/Contributing/Audit-logs.md +++ b/docs/Contributing/Audit-logs.md @@ -1222,7 +1222,7 @@ Generated when a software installer is updated in Fleet. This activity contains the following fields: - "software_title": Name of the software. -- "software_package": Filename of the installer. +- "software_package": Filename of the installer. `null` if the installer package was not modified. - "team_name": Name of the team on which this software was edited. `null` if it was edited on no team. - "team_id": The ID of the team on which this software was edited. `null` if it was edited on no team. - "self_service": Whether the software is available for installation by the end user. diff --git a/ee/server/service/software_installers.go b/ee/server/service/software_installers.go index e735f1fb5adb..9d9e922c3863 100644 --- a/ee/server/service/software_installers.go +++ b/ee/server/service/software_installers.go @@ -169,6 +169,13 @@ func (svc *Service) UpdateSoftwareInstaller(ctx context.Context, titleID uint, p installer.SelfService = *payload.SelfService } + activity := fleet.ActivityTypeEditedSoftware{ + SoftwareTitle: installer.SoftwareTitle, + TeamName: teamName, + TeamID: payload.TeamID, + SelfService: installer.SelfService, + } + if payload.InstallerFile != nil || payload.PreInstallQuery != nil || payload.InstallScript != nil || payload.PostInstallScript != nil || payload.UninstallScript != nil { @@ -196,12 +203,17 @@ func (svc *Service) UpdateSoftwareInstaller(ctx context.Context, titleID uint, p var newInstallerFileMeta *file.InstallerMetadata = nil if payload.InstallerFile != nil { - newInstallerFileMeta, err = file.ExtractInstallerMetadata(payload.InstallerFile) + payloadForInstallerFile := &fleet.UploadSoftwareInstallerPayload{ + InstallerFile: payload.InstallerFile, + Filename: payload.Filename, + } + + newInstallerExtension, err := svc.addMetadataToSoftwarePayload(ctx, payloadForInstallerFile) if err != nil { return nil, ctxerr.Wrap(ctx, err, "extracting updated installer metadata") } - if newInstallerFileMeta.Extension != existingInstallerFileMeta.Extension { + if newInstallerExtension != existingInstallerFileMeta.Extension { return nil, &fleet.BadRequestError{ Message: "You must upload an installer of the same type as the currently uploaded installer. To switch installer types, delete and re-add.", InternalErr: ctxerr.Wrap(ctx, err, "installer extension mismatch"), @@ -211,7 +223,8 @@ func (svc *Service) UpdateSoftwareInstaller(ctx context.Context, titleID uint, p // TODO check if installer file is for a different software title (if so, fail; this will implicitly fail on source mismatch) // noop if uploaded installer is identical to previous installer - if bytes.Compare(newInstallerFileMeta.SHASum, existingInstallerFileMeta.SHASum) != 0 { + if payloadForInstallerFile.StorageID != hex.EncodeToString(existingInstallerFileMeta.SHASum) { + activity.SoftwarePackage = &payload.Filename dirty = true cancelPendingInstalls = true resetInstallCounts = true @@ -286,13 +299,7 @@ func (svc *Service) UpdateSoftwareInstaller(ctx context.Context, titleID uint, p } // Create activity - if err := svc.NewActivity(ctx, vc.User, fleet.ActivityTypeEditedSoftware{ - SoftwareTitle: payload.Title, - SoftwarePackage: payload.Filename, - TeamName: teamName, - TeamID: payload.TeamID, - SelfService: installer.SelfService, - }); err != nil { + if err := svc.NewActivity(ctx, vc.User, activity); err != nil { return nil, ctxerr.Wrap(ctx, err, "creating activity for edited software") } diff --git a/server/fleet/activities.go b/server/fleet/activities.go index e02b2dc344be..523e619a81c6 100644 --- a/server/fleet/activities.go +++ b/server/fleet/activities.go @@ -1583,7 +1583,7 @@ func (a ActivityTypeAddedSoftware) Documentation() (string, string, string) { type ActivityTypeEditedSoftware struct { SoftwareTitle string `json:"software_title"` - SoftwarePackage string `json:"software_package"` + SoftwarePackage *string `json:"software_package"` TeamName *string `json:"team_name"` TeamID *uint `json:"team_id"` SelfService bool `json:"self_service"` @@ -1596,7 +1596,7 @@ func (a ActivityTypeEditedSoftware) ActivityName() string { func (a ActivityTypeEditedSoftware) Documentation() (string, string, string) { return `Generated when a software installer is updated in Fleet.`, `This activity contains the following fields: - "software_title": Name of the software. -- "software_package": Filename of the installer. +- "software_package": Filename of the installer.` + " `null` " + `if the installer package was not modified." - "team_name": Name of the team on which this software was updated.` + " `null` " + `if it was updated on no team." + - "team_id": The ID of the team on which this software was updated.` + " `null` " + `if it was updated on no team. - "self_service": Whether the software is available for installation by the end user.`, `{