-
Notifications
You must be signed in to change notification settings - Fork 8
/
policies.go
306 lines (263 loc) · 11.3 KB
/
policies.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package jamf
import (
"fmt"
)
const uriPolicys = "/JSSResource/policies"
type Policy struct {
General PolicyGeneral `xml:"general"`
Scope PolicyScope `xml:"scope,omitempty"`
SelfService PolicySelfService `xml:"self_service"`
PackageConfiguration PolicyPackageConfiguration `xml:"package_configuration,omitempty"`
ScriptsConfiguration PolicyScripts `xml:"scripts,omitempty"`
Reboot PolicyReboot `xml:"reboot"`
Maintenance PolicyMaintenance `xml:"maintenance"`
FilesAndProcesses PolicyFilesAndProcesses `xml:"files_processes"`
UserInteraction PolicyUserInteraction `xml:"user_interaction"`
}
type PolicyGeneral struct {
ID int `xml:"id"`
Name string `xml:"name"`
Enabled bool `xml:"enabled"`
Trigger string `xml:"trigger"`
TriggerCheckin bool `xml:"trigger_checkin"`
TriggerEnrollmentComplete bool `xml:"trigger_enrollment_complete"`
TriggerLogin bool `xml:"trigger_login"`
TriggerLogout bool `xml:"trigger_logout"`
TriggerNetworkStateChanged bool `xml:"trigger_network_state_changed"`
TriggerStartup bool `xml:"trigger_startup"`
TriggerOther string `xml:"trigger_other"`
Frequency string `xml:"frequency"`
RetryEvent string `xml:"retry_event"`
RetryAttempts int `xml:"retry_attempts"`
NotifyOnEachFailedRetry bool `xml:"notify_on_each_failed_retry"`
LocationUserOnly bool `xml:"location_user_only"`
TargetDrive string `xml:"target_drive"`
Offline bool `xml:"offline"`
Category GeneralCategory `xml:"category,omitempty"`
DateTimeLimitations PolicyGeneralDateTimeLimitations `xml:"date_time_limitations"`
NetworkLimitations PolicyGeneralNetworkLimitations `xml:"network_limitations"`
OverrideDefaultSettings PolicyGeneralOverrideDefaultSettings `xml:"override_default_settings"`
NetworkRequirements string `xml:"network_requirements"`
Site Site `xml:"site"`
}
// TODO Get types and test
type PolicyGeneralDateTimeLimitations struct {
ActivationDate string `xml:"activation_date"`
ActivationDateEpoch int `xml:"activation_date_epoch"`
ActivationDateUtc string `xml:"activation_date_utc"`
ExpirationDate string `xml:"expiration_date"`
ExpirationDateEpoch int `xml:"expiration_date_epoch"`
ExpirationDateUtc string `xml:"expiration_date_utc"`
NoExecuteOn string `xml:"no_execute_on"`
NoExecuteStart string `xml:"no_execute_start"`
NoExecuteEnd string `xml:"no_execute_end"`
}
// TODO Get types and test
type PolicyGeneralNetworkLimitations struct {
MinimumNetworkConnection string `xml:"minimum_network_connection"`
AnyIpAddress bool `xml:"any_ip_address"`
NetworkSegments string `xml:"network_segments"`
}
// TODO Get types and test
type PolicyGeneralOverrideDefaultSettings struct {
TargetDrive string `xml:"target_drive"`
DistributionPoint string `xml:"distribution_point"`
ForceAfpSmb bool `xml:"force_afp_smb"`
Sus string `xml:"sus"`
NetbootServer string `xml:"netboot_server"`
}
type PolicyScope struct {
AllComputers bool `xml:"all_computers"`
Computers []ComputerScope `xml:"computers>computer,omitempty"`
ComputerGroups []ComputerGroupListResponse `xml:"computer_groups>computer_group,omitempty"`
Buildings []BuildingScope `xml:"buildings>building,omitempty"`
Departments []DepartmentScope `xml:"departments>department,omitempty"`
Exclusions PolicyScopeExclusions `xml:"exclusions,omitempty"`
}
type PolicySelfService struct {
UseForSelfService bool `xml:"use_for_self_service"`
SelfServiceDisplayName string `xml:"self_service_display_name"`
InstallButtonText string `xml:"install_button_text"`
ReinstallButtonText string `xml:"reinstall_button_text"`
SelfServiceDescription string `xml:"self_service_description"`
ForceUsersToViewDescription bool `xml:"force_users_to_view_description"`
SelfServiceIcon SelfServiceIcon `xml:"self_service_icon,omitempty"`
FeatureOnMainPage bool `xml:"feature_on_main_page"`
SelfServiceCategories []SelfServiceCategory `xml:"self_service_categories>category,omitempty"`
}
type PolicyScopeExclusions struct {
Computers []ComputerScope `xml:"computers>computer,omitempty"`
ComputerGroups []ComputerGroupListResponse `xml:"computer_groups>computer_group,omitempty"`
Buildings []BuildingScope `xml:"buildings>building,omitempty"`
Departments []DepartmentScope `xml:"departments>department,omitempty"`
}
type PolicyPackageConfiguration struct {
Packages []PolicyPackageConfigurationPackage `xml:"packages>package,omitempty"`
}
type PolicyPackageConfigurationPackage struct {
ID int `xml:"id,omitempty"`
Name string `xml:"name,omitempty"`
Action string `xml:"action,omitempty"`
FillUserTemplate bool `xml:"fut,omitempty"`
FillExistingUsers bool `xml:"feu,omitempty"`
UpdateAutorun bool `xml:"update_autorun,omitempty"`
}
type PolicyScripts struct {
Scripts []PolicyScript `xml:"script,omitempty"`
}
type PolicyScript struct {
ID string `xml:"id,omitempty"`
Name string `xml:"name,omitempty"`
Priority string `xml:"priority,omitempty"`
Parameter4 string `xml:"parameter4,omitempty"`
Parameter5 string `xml:"parameter5,omitempty"`
Parameter6 string `xml:"parameter6,omitempty"`
Parameter7 string `xml:"parameter7,omitempty"`
Parameter8 string `xml:"parameter8,omitempty"`
Parameter9 string `xml:"parameter9,omitempty"`
Parameter10 string `xml:"parameter10,omitempty"`
Parameter11 string `xml:"parameter11,omitempty"`
}
type PolicyReboot struct {
Message string `xml:"message"`
StartupDisk string `xml:"startup_disk"`
SpecifyStartup string `xml:"specify_startup"`
NoUserLoggedIn string `xml:"no_user_logged_in"`
UserLoggedIn string `xml:"user_logged_in"`
MinutesUntilReboot int `xml:"minutes_until_reboot"`
StartRebootTimerImmediately bool `xml:"start_reboot_timer_immediately"`
FileVault2Reboot bool `xml:"file_vault_2_reboot"`
}
type PolicyMaintenance struct {
Recon bool `xml:"recon"`
ResetName bool `xml:"reset_name"`
InstallAllCachedPackages bool `xml:"install_all_cached_packages"`
Heal bool `xml:"heal"`
Prebindings bool `xml:"prebindings"`
Permissions bool `xml:"permissions"`
Byhost bool `xml:"byhost"`
SystemCache bool `xml:"system_cache"`
UserCache bool `xml:"user_cache"`
Verify bool `xml:"verify"`
}
type PolicyFilesAndProcesses struct {
SearchByPath string `xml:"search_by_path"`
DeleteFile bool `xml:"delete_file"`
LocateFile string `xml:"locate_file"`
UpdateLocateDatabase bool `xml:"update_locate_database"`
SpotlightSearch string `xml:"spotlight_search"`
SearchForProcess string `xml:"search_for_process"`
KillProcess bool `xml:"kill_process"`
RunCommand string `xml:"run_command"`
}
type PolicyUserInteraction struct {
MessageStart string `xml:"message_start"`
AllowUsersToDefer bool `xml:"allow_users_to_defer"`
AllowDeferralUntilUtc string `xml:"allow_deferral_until_utc"`
AllowDeferralMinutes int `xml:"allow_deferral_minutes"`
MessageFinish string `xml:"message_finish"`
}
type PolicyListResponse struct {
Size int `xml:"size"`
Results []PolicyListItem `xml:"policy"`
}
type PolicyListItem struct {
ID int `xml:"id"`
Name string `xml:"name"`
}
func (c *Client) GetPolicyIdByName(name string) (int, error) {
var id int
d, err := c.GetPolicies()
if err != nil {
return -1, err
}
for _, v := range d.Results {
if v.Name == name {
id = v.ID
break
}
}
return id, err
}
func (c *Client) GetPolicyByName(name string) (*Policy, error) {
id, err := c.GetPolicyIdByName(name)
if err != nil {
return nil, err
}
return c.GetPolicy(id)
}
func (c *Client) GetPolicy(id int) (*Policy, error) {
var out *Policy
uri := fmt.Sprintf("%s/id/%v", uriPolicys, id)
err := c.DoRequest("GET", uri, nil, nil, &out)
return out, err
}
func (c *Client) GetPolicies() (*PolicyListResponse, error) {
out := &PolicyListResponse{}
err := c.DoRequest("GET", uriPolicys, nil, nil, out)
return out, err
}
func (c *Client) CreatePolicy(d *Policy) (int, error) {
// Setting defaults, per jamf unwritten requirements :/
// Category Defaults
if d.General.Category.ID == "" || d.General.Category.Name == "" {
d.General.Category.ID = "-1"
d.General.Category.Name = "No category assigned"
}
// Reboot Defaults
if d.Reboot.StartupDisk == "" {
d.Reboot.StartupDisk = "Current Startup Disk"
}
if d.Reboot.NoUserLoggedIn == "" {
d.Reboot.NoUserLoggedIn = "Do not restart"
}
if d.Reboot.UserLoggedIn == "" {
d.Reboot.UserLoggedIn = "Do not restart"
}
if d.Reboot.MinutesUntilReboot == 0 {
d.Reboot.MinutesUntilReboot = 5
}
// Package Defaults
if len(d.PackageConfiguration.Packages) != 0 {
for i := range d.PackageConfiguration.Packages {
if d.PackageConfiguration.Packages[i].Action == "" {
d.PackageConfiguration.Packages[i].Action = "INSTALL"
}
}
}
// Script Defaults
if len(d.ScriptsConfiguration.Scripts) != 0 {
for i := range d.ScriptsConfiguration.Scripts {
if d.ScriptsConfiguration.Scripts[i].Priority == "" {
d.ScriptsConfiguration.Scripts[i].Priority = "After"
}
}
}
res := &PolicyListItem{}
reqBody := &struct {
*Policy
XMLName struct{} `xml:"policy"`
}{
Policy: d,
}
err := c.DoRequest("POST", fmt.Sprintf("%s/id/0", uriPolicys), reqBody, nil, res)
return res.ID, err
}
func (c *Client) UpdatePolicy(d *Policy) (int, error) {
res := &PolicyListItem{}
uri := fmt.Sprintf("%s/id/%v", uriPolicys, d.General.ID)
reqBody := &struct {
*Policy
XMLName struct{} `xml:"policy"`
}{
Policy: d,
}
err := c.DoRequest("PUT", uri, reqBody, nil, res)
return res.ID, err
}
func (c *Client) DeletePolicy(id int) (int, error) {
policy := &Policy{}
uri := fmt.Sprintf("%s/id/%v", uriPolicys, id)
err := c.DoRequest("DELETE", uri, nil, nil, policy)
return policy.General.ID, err
}