From 97b65d109a2ae7dc62f61735530e9eedec973709 Mon Sep 17 00:00:00 2001 From: Victor Vrantchan Date: Sun, 19 Mar 2017 20:16:02 +0000 Subject: [PATCH] read blueprint from path --- dep_hardcode.go | 168 +++++++++++++++--------------------------------- 1 file changed, 52 insertions(+), 116 deletions(-) diff --git a/dep_hardcode.go b/dep_hardcode.go index 02dd164c..b497b5b2 100644 --- a/dep_hardcode.go +++ b/dep_hardcode.go @@ -2,8 +2,11 @@ package main import ( "context" + "encoding/json" "fmt" + "io/ioutil" "log" + "os" "time" "github.com/micromdm/mdm" @@ -12,7 +15,24 @@ import ( "github.com/pkg/errors" ) +const blueprintPath = `/etc/micromdm/blueprint.json` + func hardcodeCommands(sm *config) error { + if _, err := os.Stat(blueprintPath); os.IsNotExist(err) { + fmt.Printf("no blueprint specified in %s, skipping default device actions\n", blueprintPath) + return nil + } + + bpData, err := ioutil.ReadFile(blueprintPath) + if err != nil { + return errors.Wrap(err, "reading blueprint.json file") + } + + var blueprint Blueprint + if err := json.Unmarshal(bpData, &blueprint); err != nil { + return errors.Wrap(err, "decoding blueprint json file") + } + sub := sm.pubclient cmdsvc := sm.commandService pushsvc := sm.pushService @@ -31,7 +51,7 @@ func hardcodeCommands(sm *config) error { fmt.Println(err) continue } - if err := hardcodeList(cmdsvc, ev.Command.UDID); err != nil { + if err := hardcodeList(cmdsvc, &blueprint, ev.Command.UDID); err != nil { log.Println(err) continue } @@ -47,42 +67,44 @@ func hardcodeCommands(sm *config) error { return nil } -func hardcodeList(svc command.Service, udid string) error { +type Blueprint struct { + ApplicationsURLs []string `json:"install_application_manifest_urls"` + Profiles []string `json:"install_profile_mobileconfig_paths"` +} + +func hardcodeList(svc command.Service, blueprint *Blueprint, udid string) error { ctx := context.Background() - devInfo := &mdm.CommandRequest{ - RequestType: "DeviceInformation", - UDID: udid, - Queries: []string{"UDID"}, + var requests []*mdm.CommandRequest + for _, appURL := range blueprint.ApplicationsURLs { + requests = append(requests, &mdm.CommandRequest{ + RequestType: "InstallApplication", + UDID: udid, + InstallApplication: mdm.InstallApplication{ + ManifestURL: appURL, + ManagementFlags: 1, + }, + }) + } + + for _, profilePath := range blueprint.Profiles { + profilePayload, err := ioutil.ReadFile(profilePath) + if err != nil { + fmt.Printf("error reading profile %s\n, err: %s ", profilePath, err) + } + requests = append(requests, &mdm.CommandRequest{ + RequestType: "InstallProfile", + UDID: udid, + InstallProfile: mdm.InstallProfile{ + Payload: profilePayload, + }, + }) } devConfigured := &mdm.CommandRequest{ RequestType: "DeviceConfigured", UDID: udid, } - - installProfile := &mdm.CommandRequest{ - RequestType: "InstallProfile", - UDID: udid, - InstallProfile: mdm.InstallProfile{ - Payload: debugProfile, - }, - } - - munki := &mdm.CommandRequest{ - RequestType: "InstallApplication", - UDID: udid, - InstallApplication: mdm.InstallApplication{ - ManifestURL: "https://dev.micromdm.io/repo/munkitools-2.8.2.2855.plist", - ManagementFlags: 1, - }, - } - - var requests = []*mdm.CommandRequest{ - devInfo, - installProfile, - munki, - devConfigured, - } + requests = append(requests, devConfigured) for _, r := range requests { _, err := svc.NewCommand(ctx, r) @@ -92,89 +114,3 @@ func hardcodeList(svc command.Service, udid string) error { } return nil } - -var debugProfile = []byte(` - - - - PayloadContent - - - PayloadDisplayName - ManagedClient logging - PayloadEnabled - - PayloadIdentifier - com.apple.logging.ManagedClient.1 - PayloadType - com.apple.system.logging - PayloadUUID - ED5DE307-A5FC-434F-AD88-187677F02222 - PayloadVersion - 1 - Subsystems - - com.apple.ManagedClient - - DEFAULT-OPTIONS - - Default-Privacy-Setting - Public - Level - - Enable - debug - Persist - debug - - - - - - - PayloadDisplayName - MDM debug mode - PayloadType - com.apple.mdmclient - EnableDebug - - PayloadIdentifier - com.apple.logging.ManagedClient.3 - PayloadUUID - 3EFF8784-7AE1-43E0-A2BA-6B77BBA54341 - PayloadVersion - 1 - - - PayloadDisplayName - ALR debug mode - PayloadType - com.apple.mcx.alr - EnableDebug - - PayloadIdentifier - com.apple.logging.ManagedClient.4 - PayloadUUID - 126C9C6B-AE28-4EA6-9BDB-FBB058A291B8 - PayloadVersion - 1 - - - PayloadDescription - Enables ManagedClient debug mode and logging - PayloadDisplayName - MCX debug mode and logging - PayloadIdentifier - com.apple.logging.ManagedClient - PayloadRemovalDisallowed - - PayloadScope - System - PayloadType - Configuration - PayloadUUID - D30C25BD-E0C1-44C8-830A-964F27DAD4BA - PayloadVersion - 1 - -`)