fleet/pkg/fleetdbase/fleetd_base.go
Ian Littman 2f25580c3a
Only allow FLEET_DEV_* env vars when --dev is passed, allow overriding configs one at a time in dev (#38652)
Resolves #38484. This includes a CI job change to make sure we don't
introduce any more env vars that don't get proxied (and thus turned off
outside `--dev`).

# Checklist for submitter

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

- [x] 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

- [x] Added/updated automated tests

Manual QA touched hot paths, but did _not_ manually test every
FLEET_DEV_* environment variable change.

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

* **New Features**
* Centralized dev-mode environment management for consistent FLEET_DEV_*
handling and test-friendly overrides.
* Dev-mode allows targeted overrides for certain dev-only configuration
when running with --dev.

* **Chores**
* Migrated environment access to the centralized dev-mode helper across
the codebase.
  * Added CI checks to enforce proper usage of FLEET_DEV_* variables.

* **Documentation**
  * Added guidance on dev-mode environment variable rules and overrides.

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Victor Lyuboslavsky <2685025+getvictor@users.noreply.github.com>
2026-01-27 14:32:56 -06:00

60 lines
1.5 KiB
Go

// pacakge fleetdbase contains functions to interact with downloads.fleetdm.com
package fleetdbase
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/fleetdm/fleet/v4/server/dev_mode"
)
type Metadata struct {
MSIURL string `json:"fleetd_base_msi_url"`
MSISha256 string `json:"fleetd_base_msi_sha256"`
PKGURL string `json:"fleetd_base_pkg_url"`
PKGSha256 string `json:"fleetd_base_pkg_sha256"`
ManifestPlistURL string `json:"fleetd_base_manifest_plist_url"`
Version string `json:"version"`
}
func getBaseURL() string {
devURL := dev_mode.Env("FLEET_DEV_DOWNLOAD_FLEETDM_URL")
if devURL != "" {
return devURL
}
return "https://download.fleetdm.com"
}
func GetMetadata() (*Metadata, error) {
baseURL := getBaseURL()
rawURL := fmt.Sprintf("%s/stable/meta.json", baseURL)
parsedURL, err := url.Parse(rawURL)
if err != nil {
return nil, fmt.Errorf("invalid URL: %w", err)
}
resp, err := http.Get(parsedURL.String())
if err != nil {
return nil, fmt.Errorf("failed to fetch metadata: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
var meta Metadata
if err := json.NewDecoder(resp.Body).Decode(&meta); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &meta, nil
}
func GetPKGManifestURL() string {
baseURL := getBaseURL()
return fmt.Sprintf("%s/stable/fleetd-base-manifest.plist", baseURL)
}