fleet/server/service/client_teams.go
Ian Littman 3f703b557a
Allow setting software icons via GitOps (#32886)
Fixes #31897.

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [ ] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)

## Testing

- [ ] Added/updated automated tests

- [ ] QA'd all new/changed functionality manually

## New Fleet configuration settings

- [ ] Verified that the setting is exported via `fleetctl
generate-gitops`
- [x] Verified the setting is documented in a separate PR to [the GitOps
documentation](https://github.com/fleetdm/fleet/blob/main/docs/Configuration/yaml-files.md#L485)
- [ ] Verified that the setting is cleared on the server if it is not
supplied in a YAML file (or that it is documented as being optional)
- [x] Verified that any relevant UI is disabled when GitOps mode is
enabled

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- New Features
- GitOps now supports software icons: generate and include icon
files/paths in specs for packages and App Store apps.
  - CLI adds flags to control concurrent icon uploads/updates.
- Icons are uploaded, updated, or deleted automatically during GitOps
runs.
  - UI YAML modal now includes icon_url and offers icon download.

- Improvements
  - Robust path resolution for icon assets across specs.
  - Non-YAML outputs handle both string and byte file contents.

- Bug Fixes
  - Removes stale icons after App Store app re-association.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Scott Gress <scottmgress@gmail.com>
Co-authored-by: Scott Gress <scott@fleetdm.com>
Co-authored-by: Jahziel Villasana-Espinoza <jahziel@fleetdm.com>
2025-09-26 15:59:48 -05:00

152 lines
5.5 KiB
Go

package service
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"github.com/fleetdm/fleet/v4/server/fleet"
)
// ListTeams retrieves the list of teams.
func (c *Client) ListTeams(query string) ([]fleet.Team, error) {
verb, path := "GET", "/api/latest/fleet/teams"
var responseBody listTeamsResponse
err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query)
if err != nil {
return nil, err
}
return responseBody.Teams, nil
}
// CreateTeam creates a new team.
func (c *Client) CreateTeam(teamPayload fleet.TeamPayload) (*fleet.Team, error) {
req := createTeamRequest{
TeamPayload: teamPayload,
}
verb, path := "POST", "/api/latest/fleet/teams"
var responseBody teamResponse
err := c.authenticatedRequest(req, verb, path, &responseBody)
if err != nil {
return nil, err
}
return responseBody.Team, nil
}
func (c *Client) GetTeam(teamID uint) (*fleet.Team, error) {
verb, path := "GET", fmt.Sprintf("/api/latest/fleet/teams/%d", teamID)
var responseBody getTeamResponse
if err := c.authenticatedRequest(nil, verb, path, &responseBody); err != nil {
return nil, err
}
return responseBody.Team, nil
}
// DeleteTeam deletes a team.
func (c *Client) DeleteTeam(teamID uint) error {
verb, path := "DELETE", "/api/latest/fleet/teams/"+strconv.FormatUint(uint64(teamID), 10)
var responseBody deleteTeamResponse
return c.authenticatedRequest(nil, verb, path, &responseBody)
}
// ApplyTeams sends the list of Teams to be applied to the
// Fleet instance.
func (c *Client) ApplyTeams(specs []json.RawMessage, opts fleet.ApplyTeamSpecOptions) (map[string]uint, error) {
verb, path := "POST", "/api/latest/fleet/spec/teams"
var responseBody applyTeamSpecsResponse
params := map[string]interface{}{"specs": specs}
if opts.DryRun && opts.DryRunAssumptions != nil {
params["dry_run_assumptions"] = opts.DryRunAssumptions
}
err := c.authenticatedRequestWithQuery(params, verb, path, &responseBody, opts.RawQuery())
if err != nil {
return nil, err
}
return responseBody.TeamIDsByName, nil
}
// ApplyTeamProfiles sends the list of profiles to be applied for the specified
// team.
func (c *Client) ApplyTeamProfiles(tmName string, profiles []fleet.MDMProfileBatchPayload, opts fleet.ApplyTeamSpecOptions) error {
verb, path := "POST", "/api/latest/fleet/mdm/profiles/batch"
query, err := url.ParseQuery(opts.RawQuery())
if err != nil {
return err
}
query.Add("team_name", tmName)
if opts.DryRunAssumptions != nil && opts.DryRunAssumptions.WindowsEnabledAndConfigured.Valid {
query.Add("assume_enabled", strconv.FormatBool(opts.DryRunAssumptions.WindowsEnabledAndConfigured.Value))
}
return c.authenticatedRequestWithQuery(map[string]interface{}{"profiles": profiles}, verb, path, nil, query.Encode())
}
// ApplyTeamScripts sends the list of scripts to be applied for the specified
// team.
func (c *Client) ApplyTeamScripts(tmName string, scripts []fleet.ScriptPayload, opts fleet.ApplySpecOptions) ([]fleet.ScriptResponse, error) {
verb, path := "POST", "/api/latest/fleet/scripts/batch"
query, err := url.ParseQuery(opts.RawQuery())
if err != nil {
return nil, err
}
query.Add("team_name", tmName)
var resp batchSetScriptsResponse
err = c.authenticatedRequestWithQuery(map[string]interface{}{"scripts": scripts}, verb, path, &resp, query.Encode())
return resp.Scripts, err
}
func (c *Client) ApplyTeamSoftwareInstallers(tmName string, softwareInstallers []fleet.SoftwareInstallerPayload, opts fleet.ApplySpecOptions) ([]fleet.SoftwarePackageResponse, error) {
query, err := url.ParseQuery(opts.RawQuery())
if err != nil {
return nil, err
}
query.Add("team_name", tmName)
return c.applySoftwareInstallers(softwareInstallers, query, opts.DryRun)
}
func (c *Client) ApplyTeamAppStoreAppsAssociation(tmName string, vppBatchPayload []fleet.VPPBatchPayload, opts fleet.ApplySpecOptions) ([]fleet.VPPAppResponse, error) {
query, err := url.ParseQuery(opts.RawQuery())
if err != nil {
return nil, err
}
query.Add("team_name", tmName)
return c.applyAppStoreAppsAssociation(vppBatchPayload, query)
}
func (c *Client) ApplyNoTeamAppStoreAppsAssociation(vppBatchPayload []fleet.VPPBatchPayload, opts fleet.ApplySpecOptions) ([]fleet.VPPAppResponse, error) {
query, err := url.ParseQuery(opts.RawQuery())
if err != nil {
return nil, err
}
return c.applyAppStoreAppsAssociation(vppBatchPayload, query)
}
func (c *Client) applyAppStoreAppsAssociation(vppBatchPayload []fleet.VPPBatchPayload, query url.Values) ([]fleet.VPPAppResponse, error) {
verb, path := "POST", "/api/latest/fleet/software/app_store_apps/batch"
var appsResponse batchAssociateAppStoreAppsResponse
err := c.authenticatedRequestWithQuery(map[string]interface{}{"app_store_apps": vppBatchPayload}, verb, path, &appsResponse, query.Encode())
if err != nil {
return nil, err
}
return matchAppStoreAppCustomIcons(vppBatchPayload, appsResponse.Apps), nil
}
// matchAppStoreAppCustomIcons hydrates VPP responses with references to icons in the request payload, so we can track
// which API calls to make to add/update/delete icons
func matchAppStoreAppCustomIcons(request []fleet.VPPBatchPayload, response []fleet.VPPAppResponse) []fleet.VPPAppResponse {
byAdamID := make(map[string]fleet.VPPBatchPayload)
for _, clientSide := range request {
byAdamID[clientSide.AppStoreID] = clientSide
}
for i := range response {
serverSide := &response[i]
if clientSide, ok := byAdamID[serverSide.AppStoreID]; ok {
serverSide.LocalIconHash = clientSide.IconHash
serverSide.LocalIconPath = clientSide.IconPath
}
}
return response
}