-
Notifications
You must be signed in to change notification settings - Fork 6
/
macos_configuration_profile_handling.go
69 lines (58 loc) · 2.15 KB
/
macos_configuration_profile_handling.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package utils
import (
"fmt"
"io"
"os"
"github.com/mitchellh/mapstructure"
"howett.net/plist"
)
// Struct to mirror MacOS .plist conifguration profile data with bucket for unexpected values
type ConfigurationProfile struct {
PayloadContent []PayloadContentListItem
PayloadDisplayName string
PayloadIdentifier string
PayloadType string
PayloadUuid string
PayloadVersion int
UnexpectedValues map[string]interface{} `mapstructure:",remain"`
}
// Struct to mirror xml payload item with key for all dynamic values
type PayloadContentListItem struct {
PayloadDisplayName string
PayloadIdentifier string
PayloadType string
PayloadUuid string
PayloadVersion int
PayloadSpecificValues map[string]interface{} `mapstructure:",remain"`
}
// ConfigurationFilePlistToStruct takes filepath of MacOS Configuration Profile .plist file and returns &ConfigurationProfile
func ConfigurationFilePlistToStructFromFile(filepath string) (*ConfigurationProfile, error) {
plistFile, err := os.Open(filepath)
if err != nil {
return nil, fmt.Errorf("failed to open file: %v", err)
}
defer plistFile.Close()
xmlData, err := io.ReadAll(plistFile)
if err != nil {
return nil, fmt.Errorf("failed to read plist/xml file: %v", err)
}
return plistDataToStruct(xmlData)
}
// ConfigurationProfilePlistToStructFromString takes xml of MacOS Configuration Profile .plist file and returns &ConfigurationProfile
func ConfigurationProfilePlistToStructFromString(plistData string) (*ConfigurationProfile, error) {
return plistDataToStruct([]byte(plistData))
}
// plistDataToStruct takes xml .plist bytes data and returns ConfigurationProfile
func plistDataToStruct(plistBytes []byte) (*ConfigurationProfile, error) {
var unmarshalledPlist map[string]interface{}
_, err := plist.Unmarshal(plistBytes, &unmarshalledPlist)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal plist/xml: %v", err)
}
var out ConfigurationProfile
err = mapstructure.Decode(unmarshalledPlist, &out)
if err != nil {
return nil, fmt.Errorf("(mapstructure) failed to map unmarshaled configuration profile to struct: %v", err)
}
return &out, nil
}