mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
829 lines
24 KiB
Go
829 lines
24 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/fleetdm/fleet/v4/pkg/optjson"
|
|
"github.com/fleetdm/fleet/v4/pkg/spec"
|
|
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/fleetdm/fleet/v4/server/mdm"
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
|
)
|
|
|
|
// Client is used to consume Fleet APIs from Go code
|
|
type Client struct {
|
|
*baseClient
|
|
addr string
|
|
token string
|
|
customHeaders map[string]string
|
|
|
|
outputWriter io.Writer
|
|
errWriter io.Writer
|
|
}
|
|
|
|
type ClientOption func(*Client) error
|
|
|
|
func NewClient(addr string, insecureSkipVerify bool, rootCA, urlPrefix string, options ...ClientOption) (*Client, error) {
|
|
// TODO #265 refactor all optional parameters to functional options
|
|
// API breaking change, needs a major version release
|
|
baseClient, err := newBaseClient(addr, insecureSkipVerify, rootCA, urlPrefix, nil, fleet.CapabilityMap{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
client := &Client{
|
|
baseClient: baseClient,
|
|
addr: addr,
|
|
}
|
|
|
|
for _, option := range options {
|
|
err := option(client)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return client, nil
|
|
}
|
|
|
|
func EnableClientDebug() ClientOption {
|
|
return func(c *Client) error {
|
|
httpClient, ok := c.http.(*http.Client)
|
|
if !ok {
|
|
return errors.New("client is not *http.Client")
|
|
}
|
|
httpClient.Transport = &logRoundTripper{roundtripper: httpClient.Transport}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func SetClientOutputWriter(w io.Writer) ClientOption {
|
|
return func(c *Client) error {
|
|
c.outputWriter = w
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func SetClientErrorWriter(w io.Writer) ClientOption {
|
|
return func(c *Client) error {
|
|
c.errWriter = w
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// WithCustomHeaders sets custom headers to be sent with every request made
|
|
// with the client.
|
|
func WithCustomHeaders(headers map[string]string) ClientOption {
|
|
return func(c *Client) error {
|
|
// clone the map to prevent any changes in the original affecting the client
|
|
m := make(map[string]string, len(headers))
|
|
for k, v := range headers {
|
|
m[k] = v
|
|
}
|
|
c.customHeaders = m
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (c *Client) doContextWithBodyAndHeaders(ctx context.Context, verb, path, rawQuery string, bodyBytes []byte, headers map[string]string) (*http.Response, error) {
|
|
request, err := http.NewRequestWithContext(
|
|
ctx,
|
|
verb,
|
|
c.url(path, rawQuery).String(),
|
|
bytes.NewBuffer(bodyBytes),
|
|
)
|
|
if err != nil {
|
|
return nil, ctxerr.Wrap(ctx, err, "creating request object")
|
|
}
|
|
|
|
// set the custom headers first, they should not override the actual headers
|
|
// we set explicitly.
|
|
for k, v := range c.customHeaders {
|
|
request.Header.Set(k, v)
|
|
}
|
|
for k, v := range headers {
|
|
request.Header.Set(k, v)
|
|
}
|
|
|
|
resp, err := c.http.Do(request)
|
|
if err != nil {
|
|
return nil, ctxerr.Wrap(ctx, err, "do request")
|
|
}
|
|
|
|
if resp.Header.Get(fleet.HeaderLicenseKey) == fleet.HeaderLicenseValueExpired {
|
|
fleet.WriteExpiredLicenseBanner(c.errWriter)
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
func (c *Client) doContextWithHeaders(ctx context.Context, verb, path, rawQuery string, params interface{}, headers map[string]string) (*http.Response, error) {
|
|
var bodyBytes []byte
|
|
var err error
|
|
if params != nil {
|
|
bodyBytes, err = json.Marshal(params)
|
|
if err != nil {
|
|
return nil, ctxerr.Wrap(ctx, err, "marshaling json")
|
|
}
|
|
}
|
|
return c.doContextWithBodyAndHeaders(ctx, verb, path, rawQuery, bodyBytes, headers)
|
|
}
|
|
|
|
func (c *Client) Do(verb, path, rawQuery string, params interface{}) (*http.Response, error) {
|
|
return c.DoContext(context.Background(), verb, path, rawQuery, params)
|
|
}
|
|
|
|
func (c *Client) DoContext(ctx context.Context, verb, path, rawQuery string, params interface{}) (*http.Response, error) {
|
|
headers := map[string]string{
|
|
"Content-type": "application/json",
|
|
"Accept": "application/json",
|
|
}
|
|
|
|
return c.doContextWithHeaders(ctx, verb, path, rawQuery, params, headers)
|
|
}
|
|
|
|
func (c *Client) AuthenticatedDo(verb, path, rawQuery string, params interface{}) (*http.Response, error) {
|
|
if c.token == "" {
|
|
return nil, errors.New("authentication token is empty")
|
|
}
|
|
|
|
headers := map[string]string{
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json",
|
|
"Authorization": fmt.Sprintf("Bearer %s", c.token),
|
|
}
|
|
|
|
return c.doContextWithHeaders(context.Background(), verb, path, rawQuery, params, headers)
|
|
}
|
|
|
|
func (c *Client) SetToken(t string) {
|
|
c.token = t
|
|
}
|
|
|
|
// http.RoundTripper that will log debug information about the request and
|
|
// response, including paths, timing, and body.
|
|
//
|
|
// Inspired by https://stackoverflow.com/a/39528716/491710 and
|
|
// github.com/motemen/go-loghttp
|
|
type logRoundTripper struct {
|
|
roundtripper http.RoundTripper
|
|
}
|
|
|
|
// RoundTrip implements http.RoundTripper
|
|
func (l *logRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
// Log request
|
|
fmt.Fprintf(os.Stderr, "%s %s\n", req.Method, req.URL)
|
|
reqBody, err := req.GetBody()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "GetBody error: %v\n", err)
|
|
} else {
|
|
defer reqBody.Close()
|
|
if _, err := io.Copy(os.Stderr, reqBody); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Copy body error: %v\n", err)
|
|
}
|
|
}
|
|
fmt.Fprintf(os.Stderr, "\n")
|
|
|
|
// Perform request using underlying roundtripper
|
|
start := time.Now()
|
|
res, err := l.roundtripper.RoundTrip(req)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "RoundTrip error: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
// Log response
|
|
took := time.Since(start).Truncate(time.Millisecond)
|
|
fmt.Fprintf(os.Stderr, "%s %s %s (%s)\n", res.Request.Method, res.Request.URL, res.Status, took)
|
|
|
|
resBody := &bytes.Buffer{}
|
|
resBodyReader := io.TeeReader(res.Body, resBody)
|
|
if _, err := io.Copy(os.Stderr, resBodyReader); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Read body error: %v", err)
|
|
return nil, err
|
|
}
|
|
res.Body = io.NopCloser(resBody)
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func (c *Client) authenticatedRequestWithQuery(params interface{}, verb string, path string, responseDest interface{}, query string) error {
|
|
response, err := c.AuthenticatedDo(verb, path, query, params)
|
|
if err != nil {
|
|
return fmt.Errorf("%s %s: %w", verb, path, err)
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
return c.parseResponse(verb, path, response, responseDest)
|
|
}
|
|
|
|
func (c *Client) authenticatedRequest(params interface{}, verb string, path string, responseDest interface{}) error {
|
|
return c.authenticatedRequestWithQuery(params, verb, path, responseDest, "")
|
|
}
|
|
|
|
func (c *Client) CheckAnyMDMEnabled() error {
|
|
return c.runAppConfigChecks(func(ac *fleet.EnrichedAppConfig) error {
|
|
if !ac.MDM.EnabledAndConfigured && !ac.MDM.WindowsEnabledAndConfigured {
|
|
return errors.New(fleet.MDMNotConfiguredMessage)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (c *Client) CheckAppleMDMEnabled() error {
|
|
return c.runAppConfigChecks(func(ac *fleet.EnrichedAppConfig) error {
|
|
if !ac.MDM.EnabledAndConfigured {
|
|
return errors.New(fleet.AppleMDMNotConfiguredMessage)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (c *Client) CheckPremiumMDMEnabled() error {
|
|
return c.runAppConfigChecks(func(ac *fleet.EnrichedAppConfig) error {
|
|
if ac.License == nil || !ac.License.IsPremium() {
|
|
return errors.New("missing or invalid license")
|
|
}
|
|
if !ac.MDM.EnabledAndConfigured {
|
|
return errors.New(fleet.AppleMDMNotConfiguredMessage)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (c *Client) runAppConfigChecks(fn func(ac *fleet.EnrichedAppConfig) error) error {
|
|
appCfg, err := c.GetAppConfig()
|
|
if err != nil {
|
|
var sce kithttp.StatusCoder
|
|
if errors.As(err, &sce) && sce.StatusCode() == http.StatusForbidden {
|
|
// do not return an error, user may not have permission to read app
|
|
// config (e.g. gitops) and those appconfig checks are just convenience
|
|
// to avoid the round-trip with potentially large payload to the server.
|
|
// Those will still be validated with the actual API call.
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
return fn(appCfg)
|
|
}
|
|
|
|
// getProfilesContents takes file paths and creates a map of profile contents
|
|
// keyed by the name of the profile (the file name on Windows,
|
|
// PayloadDisplayName on macOS)
|
|
func getProfilesContents(baseDir string, paths []string) (map[string][]byte, error) {
|
|
files := resolveApplyRelativePaths(baseDir, paths)
|
|
fileContents := make(map[string][]byte, len(files))
|
|
for _, f := range files {
|
|
b, err := os.ReadFile(f)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("applying fleet config: %w", err)
|
|
}
|
|
|
|
// by default, use the file name. macOS profiles use their PayloadDisplayName
|
|
name := strings.TrimSuffix(filepath.Base(f), filepath.Ext(f))
|
|
if mdm.GetRawProfilePlatform(b) == "darwin" {
|
|
mc, err := fleet.NewMDMAppleConfigProfile(b, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("applying fleet config: %w", err)
|
|
}
|
|
name = strings.TrimSpace(mc.Name)
|
|
}
|
|
if _, isDuplicate := fileContents[name]; isDuplicate {
|
|
return nil, errors.New("Couldn't edit windows_settings.custom_settings. More than one configuration profile have the same name (Windows .xml file name or macOS PayloadDisplayName).")
|
|
}
|
|
fileContents[name] = b
|
|
}
|
|
|
|
return fileContents, nil
|
|
}
|
|
|
|
// ApplyGroup applies the given spec group to Fleet.
|
|
func (c *Client) ApplyGroup(
|
|
ctx context.Context,
|
|
specs *spec.Group,
|
|
baseDir string,
|
|
logf func(format string, args ...interface{}),
|
|
opts fleet.ApplySpecOptions,
|
|
) error {
|
|
logfn := func(format string, args ...interface{}) {
|
|
if logf != nil {
|
|
logf(format, args...)
|
|
}
|
|
}
|
|
|
|
// specs.Queries must be applied before specs.Packs because packs reference queries.
|
|
if len(specs.Queries) > 0 {
|
|
if opts.DryRun {
|
|
logfn("[!] ignoring queries, dry run mode only supported for 'config' and 'team' specs\n")
|
|
} else {
|
|
if err := c.ApplyQueries(specs.Queries); err != nil {
|
|
return fmt.Errorf("applying queries: %w", err)
|
|
}
|
|
logfn("[+] applied %d queries\n", len(specs.Queries))
|
|
}
|
|
}
|
|
|
|
if len(specs.Labels) > 0 {
|
|
if opts.DryRun {
|
|
logfn("[!] ignoring labels, dry run mode only supported for 'config' and 'team' specs\n")
|
|
} else {
|
|
if err := c.ApplyLabels(specs.Labels); err != nil {
|
|
return fmt.Errorf("applying labels: %w", err)
|
|
}
|
|
logfn("[+] applied %d labels\n", len(specs.Labels))
|
|
}
|
|
}
|
|
|
|
if len(specs.Policies) > 0 {
|
|
if opts.DryRun {
|
|
logfn("[!] ignoring policies, dry run mode only supported for 'config' and 'team' specs\n")
|
|
} else {
|
|
// Policy names must be unique, return error if duplicate policy names are found
|
|
if policyName := fleet.FirstDuplicatePolicySpecName(specs.Policies); policyName != "" {
|
|
return fmt.Errorf("applying policies: policy names must be globally unique. Please correct policy %q and try again.", policyName)
|
|
}
|
|
|
|
// If set, override the team in all the policies.
|
|
if opts.TeamForPolicies != "" {
|
|
for _, policySpec := range specs.Policies {
|
|
policySpec.Team = opts.TeamForPolicies
|
|
}
|
|
}
|
|
if err := c.ApplyPolicies(specs.Policies); err != nil {
|
|
return fmt.Errorf("applying policies: %w", err)
|
|
}
|
|
logfn("[+] applied %d policies\n", len(specs.Policies))
|
|
}
|
|
}
|
|
|
|
if len(specs.Packs) > 0 {
|
|
if opts.DryRun {
|
|
logfn("[!] ignoring packs, dry run mode only supported for 'config' and 'team' specs\n")
|
|
} else {
|
|
if err := c.ApplyPacks(specs.Packs); err != nil {
|
|
return fmt.Errorf("applying packs: %w", err)
|
|
}
|
|
logfn("[+] applied %d packs\n", len(specs.Packs))
|
|
}
|
|
}
|
|
|
|
if specs.AppConfig != nil {
|
|
windowsCustomSettings := extractAppCfgWindowsCustomSettings(specs.AppConfig)
|
|
macosCustomSettings := extractAppCfgMacOSCustomSettings(specs.AppConfig)
|
|
allCustomSettings := append(macosCustomSettings, windowsCustomSettings...)
|
|
|
|
if len(allCustomSettings) > 0 {
|
|
fileContents, err := getProfilesContents(baseDir, allCustomSettings)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := c.ApplyNoTeamProfiles(fileContents, opts); err != nil {
|
|
return fmt.Errorf("applying custom settings: %w", err)
|
|
}
|
|
}
|
|
if macosSetup := extractAppCfgMacOSSetup(specs.AppConfig); macosSetup != nil {
|
|
if macosSetup.BootstrapPackage.Value != "" {
|
|
pkg, err := c.ValidateBootstrapPackageFromURL(macosSetup.BootstrapPackage.Value)
|
|
if err != nil {
|
|
return fmt.Errorf("applying fleet config: %w", err)
|
|
}
|
|
|
|
if !opts.DryRun {
|
|
if err := c.EnsureBootstrapPackage(pkg, uint(0)); err != nil {
|
|
return fmt.Errorf("applying fleet config: %w", err)
|
|
}
|
|
}
|
|
}
|
|
if macosSetup.MacOSSetupAssistant.Value != "" {
|
|
content, err := c.validateMacOSSetupAssistant(resolveApplyRelativePath(baseDir, macosSetup.MacOSSetupAssistant.Value))
|
|
if err != nil {
|
|
return fmt.Errorf("applying fleet config: %w", err)
|
|
}
|
|
if !opts.DryRun {
|
|
if err := c.uploadMacOSSetupAssistant(content, nil, macosSetup.MacOSSetupAssistant.Value); err != nil {
|
|
return fmt.Errorf("applying fleet config: %w", err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if scripts := extractAppCfgScripts(specs.AppConfig); scripts != nil {
|
|
files := resolveApplyRelativePaths(baseDir, scripts)
|
|
scriptPayloads := make([]fleet.ScriptPayload, len(files))
|
|
for i, f := range files {
|
|
b, err := os.ReadFile(f)
|
|
if err != nil {
|
|
return fmt.Errorf("applying fleet config: %w", err)
|
|
}
|
|
scriptPayloads[i] = fleet.ScriptPayload{
|
|
ScriptContents: b,
|
|
Name: filepath.Base(f),
|
|
}
|
|
}
|
|
if err := c.ApplyNoTeamScripts(scriptPayloads, opts); err != nil {
|
|
return fmt.Errorf("applying custom settings: %w", err)
|
|
}
|
|
}
|
|
if err := c.ApplyAppConfig(specs.AppConfig, opts); err != nil {
|
|
return fmt.Errorf("applying fleet config: %w", err)
|
|
}
|
|
if opts.DryRun {
|
|
logfn("[+] would've applied fleet config\n")
|
|
} else {
|
|
logfn("[+] applied fleet config\n")
|
|
}
|
|
}
|
|
|
|
if specs.EnrollSecret != nil {
|
|
if opts.DryRun {
|
|
logfn("[!] ignoring enroll secrets, dry run mode only supported for 'config' and 'team' specs\n")
|
|
} else {
|
|
if err := c.ApplyEnrollSecretSpec(specs.EnrollSecret); err != nil {
|
|
return fmt.Errorf("applying enroll secrets: %w", err)
|
|
}
|
|
logfn("[+] applied enroll secrets\n")
|
|
}
|
|
}
|
|
|
|
if len(specs.Teams) > 0 {
|
|
// extract the teams' custom settings and resolve the files immediately, so
|
|
// that any non-existing file error is found before applying the specs.
|
|
tmMDMSettings := extractTmSpecsMDMCustomSettings(specs.Teams)
|
|
|
|
tmFileContents := make(map[string]map[string][]byte, len(tmMDMSettings))
|
|
for k, paths := range tmMDMSettings {
|
|
fileContents, err := getProfilesContents(baseDir, paths)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tmFileContents[k] = fileContents
|
|
}
|
|
|
|
tmMacSetup := extractTmSpecsMacOSSetup(specs.Teams)
|
|
tmBootstrapPackages := make(map[string]*fleet.MDMAppleBootstrapPackage, len(tmMacSetup))
|
|
tmMacSetupAssistants := make(map[string][]byte, len(tmMacSetup))
|
|
for k, setup := range tmMacSetup {
|
|
if setup.BootstrapPackage.Value != "" {
|
|
bp, err := c.ValidateBootstrapPackageFromURL(setup.BootstrapPackage.Value)
|
|
if err != nil {
|
|
return fmt.Errorf("applying teams: %w", err)
|
|
}
|
|
tmBootstrapPackages[k] = bp
|
|
}
|
|
if setup.MacOSSetupAssistant.Value != "" {
|
|
b, err := c.validateMacOSSetupAssistant(resolveApplyRelativePath(baseDir, setup.MacOSSetupAssistant.Value))
|
|
if err != nil {
|
|
return fmt.Errorf("applying teams: %w", err)
|
|
}
|
|
tmMacSetupAssistants[k] = b
|
|
}
|
|
}
|
|
|
|
tmScripts := extractTmSpecsScripts(specs.Teams)
|
|
tmScriptsPayloads := make(map[string][]fleet.ScriptPayload, len(tmScripts))
|
|
for k, paths := range tmScripts {
|
|
files := resolveApplyRelativePaths(baseDir, paths)
|
|
scriptPayloads := make([]fleet.ScriptPayload, len(files))
|
|
for i, f := range files {
|
|
b, err := os.ReadFile(f)
|
|
if err != nil {
|
|
return fmt.Errorf("applying fleet config: %w", err)
|
|
}
|
|
scriptPayloads[i] = fleet.ScriptPayload{
|
|
ScriptContents: b,
|
|
Name: filepath.Base(f),
|
|
}
|
|
}
|
|
tmScriptsPayloads[k] = scriptPayloads
|
|
}
|
|
|
|
// Next, apply the teams specs before saving the profiles, so that any
|
|
// non-existing team gets created.
|
|
teamIDsByName, err := c.ApplyTeams(specs.Teams, opts)
|
|
if err != nil {
|
|
return fmt.Errorf("applying teams: %w", err)
|
|
}
|
|
|
|
if len(tmFileContents) > 0 {
|
|
for tmName, profs := range tmFileContents {
|
|
if err := c.ApplyTeamProfiles(tmName, profs, opts); err != nil {
|
|
return fmt.Errorf("applying custom settings for team %q: %w", tmName, err)
|
|
}
|
|
}
|
|
}
|
|
if len(tmBootstrapPackages)+len(tmMacSetupAssistants) > 0 && !opts.DryRun {
|
|
for tmName, tmID := range teamIDsByName {
|
|
if bp, ok := tmBootstrapPackages[tmName]; ok {
|
|
if err := c.EnsureBootstrapPackage(bp, tmID); err != nil {
|
|
return fmt.Errorf("uploading bootstrap package for team %q: %w", tmName, err)
|
|
}
|
|
}
|
|
if b, ok := tmMacSetupAssistants[tmName]; ok {
|
|
if err := c.uploadMacOSSetupAssistant(b, &tmID, tmMacSetup[tmName].MacOSSetupAssistant.Value); err != nil {
|
|
return fmt.Errorf("uploading macOS setup assistant for team %q: %w", tmName, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if len(tmScriptsPayloads) > 0 {
|
|
for tmName, scripts := range tmScriptsPayloads {
|
|
if err := c.ApplyTeamScripts(tmName, scripts, opts); err != nil {
|
|
return fmt.Errorf("applying scripts for team %q: %w", tmName, err)
|
|
}
|
|
}
|
|
}
|
|
if opts.DryRun {
|
|
logfn("[+] would've applied %d teams\n", len(specs.Teams))
|
|
} else {
|
|
logfn("[+] applied %d teams\n", len(specs.Teams))
|
|
}
|
|
}
|
|
|
|
if specs.UsersRoles != nil {
|
|
if opts.DryRun {
|
|
logfn("[!] ignoring user roles, dry run mode only supported for 'config' and 'team' specs\n")
|
|
} else {
|
|
if err := c.ApplyUsersRoleSecretSpec(specs.UsersRoles); err != nil {
|
|
return fmt.Errorf("applying user roles: %w", err)
|
|
}
|
|
logfn("[+] applied user roles\n")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func extractAppCfgMacOSSetup(appCfg any) *fleet.MacOSSetup {
|
|
asMap, ok := appCfg.(map[string]interface{})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
mmdm, ok := asMap["mdm"].(map[string]interface{})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
mos, ok := mmdm["macos_setup"].(map[string]interface{})
|
|
if !ok || mos == nil {
|
|
return nil
|
|
}
|
|
bp, _ := mos["bootstrap_package"].(string) // if not a string, bp == ""
|
|
msa, _ := mos["macos_setup_assistant"].(string)
|
|
return &fleet.MacOSSetup{
|
|
BootstrapPackage: optjson.SetString(bp),
|
|
MacOSSetupAssistant: optjson.SetString(msa),
|
|
}
|
|
}
|
|
|
|
func resolveApplyRelativePath(baseDir, path string) string {
|
|
return resolveApplyRelativePaths(baseDir, []string{path})[0]
|
|
}
|
|
|
|
// resolves the paths to an absolute path relative to the baseDir, which should
|
|
// be the path of the YAML file where the relative paths were specified. If the
|
|
// path is already absolute, it is left untouched.
|
|
func resolveApplyRelativePaths(baseDir string, paths []string) []string {
|
|
if baseDir == "" {
|
|
return paths
|
|
}
|
|
|
|
resolved := make([]string, 0, len(paths))
|
|
for _, p := range paths {
|
|
if !filepath.IsAbs(p) {
|
|
p = filepath.Join(baseDir, p)
|
|
}
|
|
resolved = append(resolved, p)
|
|
}
|
|
return resolved
|
|
}
|
|
|
|
func extractAppCfgMacOSCustomSettings(appCfg interface{}) []string {
|
|
asMap, ok := appCfg.(map[string]interface{})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
mmdm, ok := asMap["mdm"].(map[string]interface{})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
mos, ok := mmdm["macos_settings"].(map[string]interface{})
|
|
if !ok || mos == nil {
|
|
return nil
|
|
}
|
|
|
|
cs, ok := mos["custom_settings"]
|
|
if !ok {
|
|
// custom settings is not present
|
|
return nil
|
|
}
|
|
|
|
csAny, ok := cs.([]interface{})
|
|
if !ok || csAny == nil {
|
|
// return a non-nil, empty slice instead, so the caller knows that the
|
|
// custom_settings key was actually provided.
|
|
return []string{}
|
|
}
|
|
|
|
csStrings := make([]string, 0, len(csAny))
|
|
for _, v := range csAny {
|
|
s, _ := v.(string)
|
|
if s != "" {
|
|
csStrings = append(csStrings, s)
|
|
}
|
|
}
|
|
return csStrings
|
|
}
|
|
|
|
func extractAppCfgWindowsCustomSettings(appCfg interface{}) []string {
|
|
asMap, ok := appCfg.(map[string]interface{})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
mmdm, ok := asMap["mdm"].(map[string]interface{})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
mos, ok := mmdm["windows_settings"].(map[string]interface{})
|
|
if !ok || mos == nil {
|
|
return nil
|
|
}
|
|
|
|
cs, ok := mos["custom_settings"]
|
|
if !ok {
|
|
// custom settings is not present
|
|
return nil
|
|
}
|
|
|
|
csAny, ok := cs.([]interface{})
|
|
if !ok || csAny == nil {
|
|
// return a non-nil, empty slice instead, so the caller knows that the
|
|
// custom_settings key was actually provided.
|
|
return []string{}
|
|
}
|
|
|
|
csStrings := make([]string, 0, len(csAny))
|
|
for _, v := range csAny {
|
|
s, _ := v.(string)
|
|
if s != "" {
|
|
csStrings = append(csStrings, s)
|
|
}
|
|
}
|
|
return csStrings
|
|
}
|
|
|
|
func extractAppCfgScripts(appCfg interface{}) []string {
|
|
asMap, ok := appCfg.(map[string]interface{})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
scripts, ok := asMap["scripts"]
|
|
if !ok {
|
|
// scripts is not present
|
|
return nil
|
|
}
|
|
|
|
scriptsAny, ok := scripts.([]interface{})
|
|
if !ok || scriptsAny == nil {
|
|
// return a non-nil, empty slice instead, so the caller knows that the
|
|
// scripts key was actually provided.
|
|
return []string{}
|
|
}
|
|
|
|
scriptsStrings := make([]string, 0, len(scriptsAny))
|
|
for _, v := range scriptsAny {
|
|
s, _ := v.(string)
|
|
if s != "" {
|
|
scriptsStrings = append(scriptsStrings, s)
|
|
}
|
|
}
|
|
return scriptsStrings
|
|
}
|
|
|
|
// returns the custom macOS and Windows settings keyed by team name.
|
|
func extractTmSpecsMDMCustomSettings(tmSpecs []json.RawMessage) map[string][]string {
|
|
var m map[string][]string
|
|
for _, tm := range tmSpecs {
|
|
var spec struct {
|
|
Name string `json:"name"`
|
|
MDM struct {
|
|
MacOSSettings struct {
|
|
CustomSettings json.RawMessage `json:"custom_settings"`
|
|
} `json:"macos_settings"`
|
|
WindowsSettings struct {
|
|
CustomSettings json.RawMessage `json:"custom_settings"`
|
|
} `json:"windows_settings"`
|
|
} `json:"mdm"`
|
|
}
|
|
if err := json.Unmarshal(tm, &spec); err != nil {
|
|
// ignore, this will fail in the call to apply team specs
|
|
continue
|
|
}
|
|
if spec.Name != "" {
|
|
var macOSSettings []string
|
|
var windowsSettings []string
|
|
|
|
// to keep existing bahavior, if any of the custom
|
|
// settings is provided, make the map a non-nil map
|
|
if len(spec.MDM.MacOSSettings.CustomSettings) > 0 ||
|
|
len(spec.MDM.WindowsSettings.CustomSettings) > 0 {
|
|
if m == nil {
|
|
m = make(map[string][]string)
|
|
}
|
|
}
|
|
|
|
if len(spec.MDM.MacOSSettings.CustomSettings) > 0 {
|
|
if err := json.Unmarshal(spec.MDM.MacOSSettings.CustomSettings, &macOSSettings); err != nil {
|
|
// ignore, will fail in apply team specs call
|
|
continue
|
|
}
|
|
if macOSSettings == nil {
|
|
// to be consistent with the AppConfig custom settings, set it to an
|
|
// empty slice if the provided custom settings are present but empty.
|
|
macOSSettings = []string{}
|
|
}
|
|
}
|
|
if len(spec.MDM.WindowsSettings.CustomSettings) > 0 {
|
|
if err := json.Unmarshal(spec.MDM.WindowsSettings.CustomSettings, &windowsSettings); err != nil {
|
|
// ignore, will fail in apply team specs call
|
|
continue
|
|
}
|
|
if windowsSettings == nil {
|
|
// to be consistent with the AppConfig custom settings, set it to an
|
|
// empty slice if the provided custom settings are present but empty.
|
|
windowsSettings = []string{}
|
|
}
|
|
}
|
|
|
|
if macOSSettings != nil || windowsSettings != nil {
|
|
m[spec.Name] = append(macOSSettings, windowsSettings...)
|
|
}
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
|
|
func extractTmSpecsScripts(tmSpecs []json.RawMessage) map[string][]string {
|
|
var m map[string][]string
|
|
for _, tm := range tmSpecs {
|
|
var spec struct {
|
|
Name string `json:"name"`
|
|
Scripts json.RawMessage `json:"scripts"`
|
|
}
|
|
if err := json.Unmarshal(tm, &spec); err != nil {
|
|
// ignore, this will fail in the call to apply team specs
|
|
continue
|
|
}
|
|
if spec.Name != "" && len(spec.Scripts) > 0 {
|
|
if m == nil {
|
|
m = make(map[string][]string)
|
|
}
|
|
var scripts []string
|
|
if err := json.Unmarshal(spec.Scripts, &scripts); err != nil {
|
|
// ignore, will fail in apply team specs call
|
|
continue
|
|
}
|
|
if scripts == nil {
|
|
// to be consistent with the AppConfig custom settings, set it to an
|
|
// empty slice if the provided custom settings are present but empty.
|
|
scripts = []string{}
|
|
}
|
|
m[spec.Name] = scripts
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
|
|
// returns the macos_setup keyed by team name.
|
|
func extractTmSpecsMacOSSetup(tmSpecs []json.RawMessage) map[string]*fleet.MacOSSetup {
|
|
var m map[string]*fleet.MacOSSetup
|
|
for _, tm := range tmSpecs {
|
|
var spec struct {
|
|
Name string `json:"name"`
|
|
MDM struct {
|
|
MacOSSetup fleet.MacOSSetup `json:"macos_setup"`
|
|
} `json:"mdm"`
|
|
}
|
|
if err := json.Unmarshal(tm, &spec); err != nil {
|
|
// ignore, this will fail in the call to apply team specs
|
|
continue
|
|
}
|
|
if spec.Name != "" {
|
|
if m == nil {
|
|
m = make(map[string]*fleet.MacOSSetup)
|
|
}
|
|
m[spec.Name] = &spec.MDM.MacOSSetup
|
|
}
|
|
}
|
|
return m
|
|
}
|