<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #49972 Adds custom DDM activations to the GitOps workflow. A profile entry can point at an activation file with a new `activation` key, the batch endpoint validates and stores it through the same code as the single-profile upload, and `fleetctl generate-gitops` exports it back out. ```yaml controls: macos_settings: custom_settings: - path: ./lib/profiles/passcode.json activation: ./lib/activations/passcode.json ``` `activation` is only valid on a declaration (`.json`) profile, and can't be combined with `paths:` because an activation names exactly one declaration. Removing the key removes the stored activation. # 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), JS inline code is prevented especially for url redirects, and untrusted data interpolated into shell scripts/commands is validated against shell metacharacters. ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually Verified on an ADE-enrolled Mac: exported an existing declaration and its custom activation with `generate-gitops`, removed everything by applying a config with no profiles, then re-applied the exported files. All three declarations came back with the correct scopes, the activation attached to only its own declaration, and the predicate was reported correctly on the host. ## New Fleet configuration settings - [x] 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) - [x] 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)
210 lines
6.7 KiB
Go
210 lines
6.7 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
)
|
|
|
|
// TODO(mna): those methods are unused except for an internal tool, remove or
|
|
// migrate to new endpoints (those apple-specific endpoints are deprecated)?
|
|
|
|
func (c *Client) DeleteProfile(profileID uint) error {
|
|
verb, path := "DELETE", "/api/latest/fleet/mdm/apple/profiles/"+strconv.FormatUint(uint64(profileID), 10)
|
|
var responseBody deleteMDMAppleConfigProfileResponse
|
|
return c.authenticatedRequest(nil, verb, path, &responseBody)
|
|
}
|
|
|
|
func (c *Client) ListProfiles(teamID *uint) ([]*fleet.MDMAppleConfigProfile, error) {
|
|
verb, path := "GET", "/api/latest/fleet/mdm/apple/profiles"
|
|
query := make(url.Values)
|
|
if teamID != nil {
|
|
query.Add("fleet_id", strconv.FormatUint(uint64(*teamID), 10))
|
|
}
|
|
var responseBody listMDMAppleConfigProfilesResponse
|
|
if err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query.Encode()); err != nil {
|
|
return nil, err
|
|
}
|
|
return responseBody.ConfigProfiles, nil
|
|
}
|
|
|
|
func (c *Client) ListConfigurationProfiles(teamID *uint) ([]*fleet.MDMConfigProfilePayload, error) {
|
|
verb, path := "GET", "/api/latest/fleet/configuration_profiles"
|
|
query := make(url.Values)
|
|
if teamID != nil {
|
|
query.Add("fleet_id", strconv.FormatUint(uint64(*teamID), 10))
|
|
}
|
|
var responseBody listMDMConfigProfilesResponse
|
|
if err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query.Encode()); err != nil {
|
|
return nil, err
|
|
}
|
|
return responseBody.Profiles, nil
|
|
}
|
|
|
|
// Get the contents of a saved profile.
|
|
func (c *Client) GetProfileContents(profileID string) ([]byte, error) {
|
|
verb, path := "GET", "/api/latest/fleet/mdm/profiles/"+profileID
|
|
response, err := c.AuthenticatedDo(verb, path, "alt=media", nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s %s: %w", verb, path, err)
|
|
}
|
|
defer response.Body.Close()
|
|
err = c.ParseResponse(verb, path, response, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s %s: %w", verb, path, err)
|
|
}
|
|
if response.StatusCode != http.StatusNoContent {
|
|
b, err := io.ReadAll(response.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("reading response body: %w", err)
|
|
}
|
|
return b, nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
// GetProfileActivation returns the custom activation attached to a declaration,
|
|
// or nil if it has none. GetProfileContents can't serve this because alt=media
|
|
// returns the declaration file itself, not the payload the activation rides on.
|
|
func (c *Client) GetProfileActivation(profileID string) ([]byte, error) {
|
|
verb, path := "GET", "/api/latest/fleet/mdm/profiles/"+profileID
|
|
var responseBody getMDMConfigProfileResponse
|
|
if err := c.authenticatedRequest(nil, verb, path, &responseBody); err != nil {
|
|
return nil, err
|
|
}
|
|
if responseBody.MDMConfigProfilePayload == nil {
|
|
return nil, nil
|
|
}
|
|
return responseBody.MDMConfigProfilePayload.Activation, nil
|
|
}
|
|
|
|
// ListDDMAssets returns the Apple DDM assets for the given team.
|
|
func (c *Client) ListDDMAssets(teamID *uint) ([]*fleet.DDMAsset, error) {
|
|
verb, path := "GET", "/api/latest/fleet/assets"
|
|
query := make(url.Values)
|
|
if teamID != nil {
|
|
query.Add("fleet_id", strconv.FormatUint(uint64(*teamID), 10))
|
|
}
|
|
var responseBody listAppleDDMAssetsResponse
|
|
if err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query.Encode()); err != nil {
|
|
return nil, err
|
|
}
|
|
return responseBody.Assets, nil
|
|
}
|
|
|
|
// DownloadDDMAsset returns the raw JSON contents of the DDM asset with the given UUID.
|
|
func (c *Client) DownloadDDMAsset(assetUUID string) ([]byte, error) {
|
|
verb, path := "GET", "/api/latest/fleet/assets/"+assetUUID
|
|
response, err := c.AuthenticatedDo(verb, path, "alt=media", nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s %s: %w", verb, path, err)
|
|
}
|
|
defer response.Body.Close()
|
|
if err := c.ParseResponse(verb, path, response, nil); err != nil {
|
|
return nil, fmt.Errorf("%s %s: %w", verb, path, err)
|
|
}
|
|
return io.ReadAll(response.Body)
|
|
}
|
|
|
|
func (c *Client) AddProfile(teamID uint, configurationProfile []byte) (uint, error) {
|
|
if c.token == "" {
|
|
return 0, errors.New("authentication token is empty")
|
|
}
|
|
body := &bytes.Buffer{}
|
|
writer := multipart.NewWriter(body)
|
|
teamIDField, err := writer.CreateFormField("fleet_id")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if _, err := teamIDField.Write([]byte(strconv.FormatUint(uint64(teamID), 10))); err != nil {
|
|
return 0, err
|
|
}
|
|
profileField, err := writer.CreateFormFile("profile", "mobileconfig")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if _, err := profileField.Write(configurationProfile); err != nil {
|
|
return 0, err
|
|
}
|
|
if err := writer.Close(); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
request, err := http.NewRequest(
|
|
"POST",
|
|
c.BaseURL.String()+"/api/latest/fleet/mdm/apple/profiles",
|
|
body,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
request.Header.Set("Content-Type", writer.FormDataContentType())
|
|
request.Header.Set("Accept", "application/json")
|
|
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
|
|
|
|
response, err := c.HTTP.Do(request)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.Header.Get(fleet.HeaderLicenseKey) == fleet.HeaderLicenseValueExpired {
|
|
fleet.WriteExpiredLicenseBanner(c.errWriter)
|
|
}
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
return 0, fmt.Errorf("request failed: %s", response.Status)
|
|
}
|
|
|
|
responseBody, err := io.ReadAll(response.Body)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
var addProfileResponse *newMDMAppleConfigProfileResponse
|
|
if err := json.Unmarshal(responseBody, &addProfileResponse); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return addProfileResponse.ProfileID, nil
|
|
}
|
|
|
|
func (c *Client) GetConfigProfilesSummary(teamID *uint) (*fleet.MDMProfilesSummary, error) {
|
|
verb, path := "GET", "/api/latest/fleet/mdm/profiles/summary"
|
|
query := make(url.Values)
|
|
if teamID != nil {
|
|
query.Add("fleet_id", strconv.FormatUint(uint64(*teamID), 10))
|
|
}
|
|
var responseBody getMDMProfilesSummaryResponse
|
|
if err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query.Encode()); err != nil {
|
|
return nil, err
|
|
}
|
|
return &responseBody.MDMProfilesSummary, nil
|
|
}
|
|
|
|
// Get the Apple setup assistant profile for the given team, if any.
|
|
func (c *Client) GetAppleMDMEnrollmentProfile(teamID uint) (*fleet.MDMAppleSetupAssistant, error) {
|
|
verb, path := "GET", "/api/latest/fleet/enrollment_profiles/automatic"
|
|
var query string
|
|
if teamID != 0 {
|
|
query = fmt.Sprintf("fleet_id=%d", teamID)
|
|
}
|
|
var responseBody createMDMAppleSetupAssistantResponse
|
|
if err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query); err != nil {
|
|
if isNotFoundErr(err) {
|
|
// If the profile is not found, return nil instead of an error.
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return &responseBody.MDMAppleSetupAssistant, nil
|
|
}
|