fleet/server
Dan Tsekhanskiy aff440236e
Add cache option for software packages to skip re-downloading unchanged content (#42216)
**Related issue:** 
Ref #34797
Ref #42675 

## Problem

When a software installer spec has no `hash_sha256`, Fleet re-downloads
the package, re-extracts metadata, and re-upserts the DB on every GitOps
run, even if the upstream file hasn't changed. For deployments with 50+
URL-only packages across multiple teams, this wastes bandwidth and
processing time on every run.

## Solution

By default, use etags to avoid unnecessary downloads:

1. First run: Fleet downloads the package normally and stores the
server's ETag header
2. Subsequent runs: Fleet sends a conditional GET with `If-None-Match`.
If the server returns 304 Not Modified, Fleet skips the download,
metadata extraction, S3 upload, and DB upsert entirely

Opt-out with `always_download:true`, meaning packages continue to be
downloaded and re-processed on every run, same as today. No UI changes
needed.

```yaml
url: https://nvidia.gpcloudservice.com/global-protect/getmsi.esp?version=64&platform=windows
always_download: true
install_script:
  path: install.ps1
```

### Why conditional GET instead of HEAD

Fleet team [analysis of 276 maintained
apps](https://github.com/fleetdm/fleet/pull/42216#issuecomment-4105430061)
showed 7 apps where HEAD requests fail (405, 403, timeout) but GET works
for all. Conditional GET eliminates that failure class: if the server
doesn't support conditional requests, it returns 200 with the full body,
same as today.

### Why opt-in

5 of 276 apps (1.8%) have stale ETags (content changes but ETag stays
the same), caused by CDN caching artifacts (CloudFront, Cloudflare,
nginx inode-based ETags). The `cache` key lets users opt in per package
for URLs where they've verified ETag behavior is correct.

Validation rejects `always_download: true` when hash_sha256` is set

## Changes

- New YAML field: `cache` (bool, package-level)
- New migration: `http_etag` VARCHAR(512) column (explicit
`utf8mb4_unicode_ci` collation) + composite index `(global_or_team_id,
url(255))` on `software_installers`
- New datastore method: `GetInstallerByTeamAndURL`
- `downloadURLFn` accepts optional `If-None-Match` header, returns 304
as `(resp, nil, nil)` with `http.NoBody`
- ETag validated per RFC 7232 (ASCII printable only, no control chars,
max 512 bytes) at both write and read time
- Cache skipped for `.ipa` packages (multi-platform extraInstallers)
- TempFileReader and HTTP response leak prevention on download retry
- Docs updated in `yaml-files.md`

## What doesn't change

- Packages with `hash_sha256`: existing hash-based skip, untouched
- FMA packages: FMA version cache, untouched
- Packages with `always_download: true`: identical to current behavior
- Fleet UI: no changes

## Test plan

Automated testing:
- [x] 16 unit tests for `validETag`
- [x] 8 unit tests for conditional GET behavior (304, 200, 403, 500,
weak ETag, S3 multipart, no ETag)
- [x] MySQL integration test for `GetInstallerByTeamAndURL`
- [x] All 23 existing `TestSoftwareInstallers` datastore tests pass
- [x] All existing service tests pass

Manual testing:
- [x] E2E: 86 packages across 6 CDN patterns, second apply shows 51
conditional hits (304)
- [x] @sgress454 used a local fileserver tool to test w/ a new instance
and dummy packages


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

* **New Features**
* ETag-based conditional downloads to skip unchanged remote installer
files.
  * New always_download flag to force full re-downloads.

* **Tests**
* Added integration and unit tests covering conditional GETs, ETag
validation, retries, edge cases, and payload behavior.

* **Chores**
* Persist HTTP ETag and related metadata; DB migration and index to
speed installer lookups.
* Added installer lookup by team+URL to support conditional download
flow.

* **Bug Fix**
* Rejects using always_download together with an explicit SHA256 in
uploads.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Scott Gress <scott@fleetdm.com>
Co-authored-by: Scott Gress <scott@pigandcow.com>
Co-authored-by: Ian Littman <iansltx@gmail.com>
2026-04-14 13:01:33 -05:00
..
acl ACME MDM -> main (#42926) 2026-04-02 15:56:31 -05:00
activity Removed the ptr helper package from Activity bounded context (#42161) 2026-03-23 14:10:07 -05:00
api_endpoints Implement GET /api/v1/fleet/rest_api (#42883) 2026-04-10 11:12:38 -04:00
archtest Refactor endpoint_utils for modularization (#36484) 2025-12-31 09:12:00 -06:00
authz Implement GET /api/v1/fleet/rest_api (#42883) 2026-04-10 11:12:38 -04:00
aws_common Feat 1817 add iam auth to mysql and redis (#32488) 2025-09-04 10:08:47 -05:00
bindata Allow users to be readded if they were ever removed (#1945) 2021-09-07 13:33:40 -03:00
config Move enable_custom_os_updates_and_filevault and allow_all_declarations to Fleet server configuration (#42938) 2026-04-13 09:55:18 -04:00
contexts Move PostJSONWithTimeout to platform/http package and activity cleanup (#40561) 2026-02-26 17:39:10 -06:00
cron slog migration: initLogger + serve.go + cron + schedule (#40699) 2026-02-27 14:29:27 -06:00
crypto Crypto package for db encryption (#41139) 2026-03-11 16:45:59 -06:00
datastore Add cache option for software packages to skip re-downloading unchanged content (#42216) 2026-04-14 13:01:33 -05:00
dev_mode Add lock semantics around dev_mode.IsEnabled to avoid data races (#42646) 2026-03-31 07:49:45 -04:00
errorstore Incremental migration to slog (#40120) 2026-02-19 15:35:35 -06:00
fleet Add cache option for software packages to skip re-downloading unchanged content (#42216) 2026-04-14 13:01:33 -05:00
goose Use UTC timestamps for DB migrations (#36228) 2025-11-24 15:49:10 -06:00
health slog migration: service layer + subsystem libraries (#40661) 2026-02-26 17:40:46 -06:00
launcher Final slog migration PR: test infrastructure + tools + remaining standalone files (#40727) 2026-02-28 05:52:21 -06:00
live_query Incremental migration to slog (#40120) 2026-02-19 15:35:35 -06:00
logging Migrated logging and google calendar files to use slog (#40541) 2026-02-26 12:48:54 -06:00
mail Run multiple independent Fleet dev servers in parallel (#41865) 2026-03-18 13:58:58 -05:00
mdm Verifying jwt signing algo to prevent vulnerability (#43474) 2026-04-13 19:11:55 -05:00
mock Add cache option for software packages to skip re-downloading unchanged content (#42216) 2026-04-14 13:01:33 -05:00
platform Android Wi-Fi profile withheld until cert installed on device (#42877) 2026-04-07 16:26:09 -05:00
policies Migrating maintained apps, failing policies, and webhooks to slog. (#40149) 2026-02-23 08:50:40 -06:00
ptr Fix issue with GitOps incorrectly wiping policy stats (#43282) 2026-04-08 17:03:08 -05:00
pubsub Incremental migration to slog (#40120) 2026-02-19 15:35:35 -06:00
service Add cache option for software packages to skip re-downloading unchanged content (#42216) 2026-04-14 13:01:33 -05:00
shellquote Updating golangci-lint to 1.61.0 (#22973) 2024-10-18 12:38:26 -05:00
sso End-user authentication for Window/Linux setup experience: backend (#34835) 2025-10-31 11:16:42 -05:00
test Move NewActivity to activity bounded context (#39521) 2026-02-25 14:11:03 -06:00
variables DCSW: Support all IDP variables in Windows config profiles (#34707) 2025-10-24 10:10:58 -03:00
version Move external dependency fleetdm/kolide-kit to monorepo (#15861) 2024-01-02 18:22:52 -03:00
vulnerabilities Add source-agnostic RHEL vulnerability integration tests (#43185) 2026-04-11 13:43:28 -06:00
webhooks Feat/31914 patch policy (#41518) 2026-03-13 16:47:09 -04:00
websocket Enable errcheck linter for golangci-lint (#8899) 2022-12-05 16:50:49 -06:00
worker Clean up setup experience cancellation behavior (#43437) 2026-04-14 09:39:26 -05:00
utils.go Move PostJSONWithTimeout to platform/http package and activity cleanup (#40561) 2026-02-26 17:39:10 -06:00
utils_test.go feature: target profiles by labels (#16202) 2024-01-26 11:00:58 -05:00