forked from micromdm/micromdm
/
get.go
366 lines (323 loc) · 8.5 KB
/
get.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"context"
"crypto/x509"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"text/tabwriter"
"github.com/go-kit/kit/log"
"github.com/micromdm/micromdm/pkg/crypto"
"github.com/micromdm/micromdm/platform/blueprint"
"github.com/micromdm/micromdm/platform/device"
"github.com/micromdm/micromdm/platform/profile"
"github.com/pkg/errors"
)
type getCommand struct {
config *ServerConfig
*remoteServices
}
func (cmd *getCommand) setup() error {
cfg, err := LoadServerConfig()
if err != nil {
return err
}
cmd.config = cfg
logger := log.NewLogfmtLogger(os.Stderr)
remote, err := setupClient(logger)
if err != nil {
return err
}
cmd.remoteServices = remote
return nil
}
func (cmd *getCommand) Run(args []string) error {
if len(args) < 1 {
cmd.Usage()
os.Exit(1)
}
if err := cmd.setup(); err != nil {
return err
}
var run func([]string) error
switch strings.ToLower(args[0]) {
case "dev", "device", "devices":
run = cmd.getDevices
case "dep-devices":
run = cmd.getDEPDevices
case "dep-account":
run = cmd.getDEPAccount
case "dep-profiles":
run = cmd.getDEPProfiles
case "dep-tokens":
run = cmd.getDepTokens
case "blueprints":
run = cmd.getBlueprints
case "profiles":
run = cmd.getProfiles
case "users":
run = cmd.getUsers
case "apps":
run = cmd.getApps
case "dep-autoassigners":
run = cmd.getDEPAutoAssigners
default:
cmd.Usage()
os.Exit(1)
}
return run(args[1:])
}
func (cmd *getCommand) Usage() error {
const getUsage = `
Display one or many resources.
Valid resource types:
* devices
* blueprints
* dep-tokens
* dep-devices
* dep-account
* dep-profiles
* dep-autoassigners
* users
* profiles
* apps
Examples:
# Get a list of devices
mdmctl get devices
# Get a device by serial (TODO implement filtering)
mdmctl get devices -serials=C02ABCDEF
`
fmt.Println(getUsage)
return nil
}
type devicesTableOutput struct{ w *tabwriter.Writer }
func (out *devicesTableOutput) BasicHeader() {
fmt.Fprintf(out.w, "UDID\tSerialNumber\tEnrollmentStatus\tLastSeen\n")
}
func (out *devicesTableOutput) BasicFooter() {
out.w.Flush()
}
func (cmd *getCommand) getDevices(args []string) error {
flagset := flag.NewFlagSet("devices", flag.ExitOnError)
var (
flFilterSerials = flagset.String("serials", "", "device serial, optionally comma-separated")
)
flagset.Usage = usageFor(flagset, "mdmctl get devices [flags]")
if err := flagset.Parse(args); err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
out := &devicesTableOutput{w}
out.BasicHeader()
defer out.BasicFooter()
ctx := context.Background()
// convert string to []string in case a filter serial is defined
// nil serials will return all serials from the database
var flFilterSerialsSlice []string
if *flFilterSerials != "" {
flFilterSerialsSlice = strings.Split(*flFilterSerials, ",")
}
devices, err := cmd.devicesvc.ListDevices(ctx, device.ListDevicesOption{FilterSerial: flFilterSerialsSlice})
if err != nil {
return err
}
for _, d := range devices {
fmt.Fprintf(out.w, "%s\t%s\t%v\t%s\n", d.UDID, d.SerialNumber, d.EnrollmentStatus, d.LastSeen)
}
return nil
}
const defaultmdmctlFilesPath = "mdm-files"
func (cmd *getCommand) getDepTokens(args []string) error {
flagset := flag.NewFlagSet("dep-tokens", flag.ExitOnError)
var (
flFullCK = flagset.Bool("v", false, "Display full ConsumerKey in summary list")
flPublicKeyPath = flagset.String(
"export-public-key",
filepath.Join(defaultmdmctlFilesPath, "DEPPublicKey"),
"Filename of public key to write (to be uploaded to deploy.apple.com)",
)
flTokenPath = flagset.String(
"export-token",
filepath.Join(defaultmdmctlFilesPath, "DEPOAuthToken.json"),
"Filename to save decrypted oauth token (JSON)")
)
flagset.Usage = usageFor(flagset, "mdmctl get dep-tokens [flags]")
if err := flagset.Parse(args); err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintf(w, "ConsumerKey\tAccessTokenExpiry\n")
ctx := context.Background()
tokens, certBytes, err := cmd.configsvc.GetDEPTokens(ctx)
if err != nil {
return err
}
var ckTrimmed string
for _, t := range tokens {
if len(t.ConsumerKey) > 40 && !*flFullCK {
ckTrimmed = t.ConsumerKey[0:39] + "…"
} else {
ckTrimmed = t.ConsumerKey
}
fmt.Fprintf(w, "%s\t%s\n", ckTrimmed, t.AccessTokenExpiry.String())
}
w.Flush()
if *flPublicKeyPath != "" && certBytes != nil {
if err := os.MkdirAll(filepath.Dir(*flPublicKeyPath), 0755); err != nil {
return errors.Wrapf(err, "create directory %s", filepath.Dir(*flPublicKeyPath))
}
cert, err := x509.ParseCertificate(certBytes)
if err != nil {
return err
}
err = crypto.WritePEMCertificateFile(cert, *flPublicKeyPath)
if err != nil {
return err
}
fmt.Printf("\nWrote DEP public key to: %s\n", *flPublicKeyPath)
}
if *flTokenPath != "" && len(tokens) > 0 {
t := tokens[0]
if err := os.MkdirAll(filepath.Dir(*flTokenPath), 0755); err != nil {
return errors.Wrapf(err, "create directory %s", filepath.Dir(*flTokenPath))
}
tokenFile, err := os.Create(*flTokenPath)
if err != nil {
return err
}
defer tokenFile.Close()
err = json.NewEncoder(tokenFile).Encode(t)
if err != nil {
return err
}
fmt.Printf("\nWrote DEP token JSON to: %s\n", *flTokenPath)
if len(tokens) > 1 {
fmt.Println("WARNING: more than one DEP token returned; only saved first")
}
}
return nil
}
func (cmd *getCommand) getBlueprints(args []string) error {
flagset := flag.NewFlagSet("blueprints", flag.ExitOnError)
var (
flBlueprintName = flagset.String("name", "", "name of blueprint")
flJSONName = flagset.String("f", "-", "filename of JSON to save to")
)
flagset.Usage = usageFor(flagset, "mdmctl get blueprints [flags]")
if err := flagset.Parse(args); err != nil {
return err
}
ctx := context.Background()
blueprints, err := cmd.blueprintsvc.GetBlueprints(ctx, blueprint.GetBlueprintsOption{FilterName: *flBlueprintName})
if err != nil {
return err
}
if *flBlueprintName == "" || len(blueprints) < 1 {
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintf(w, "Name\tUUID\tManifests\tProfiles\tApply At\n")
for _, bp := range blueprints {
var applyAtStr string
if len(bp.ApplyAt) > 0 {
applyAtStr = strings.Join(bp.ApplyAt, ",")
} else {
applyAtStr = "(None)"
}
fmt.Fprintf(
w,
"%s\t%s\t%d\t%d\t%s\n",
bp.Name,
bp.UUID,
len(bp.ApplicationURLs),
len(bp.ProfileIdentifiers),
applyAtStr,
)
}
w.Flush()
} else if *flJSONName != "" {
bp := blueprints[0]
var output *os.File
{
if *flJSONName == "-" {
output = os.Stdout
} else {
var err error
output, err = os.Create(*flJSONName)
if err != nil {
return err
}
defer output.Close()
}
}
enc := json.NewEncoder(output)
enc.SetIndent("", " ")
err = enc.Encode(bp)
if err != nil {
return err
}
if *flJSONName != "-" {
fmt.Printf("wrote blueprint %s to: %s\n", *flBlueprintName, *flJSONName)
if len(blueprints) > 1 {
fmt.Println("WARNING: more than one Blueprint returned; only saved first")
}
}
}
return nil
}
func (cmd *getCommand) getProfiles(args []string) error {
flagset := flag.NewFlagSet("profiles", flag.ExitOnError)
var (
flProfilePath = flagset.String("f", "-", "filename of profile to write")
flIdentifier = flagset.String("id", "", "profile Identifier")
)
flagset.Usage = usageFor(flagset, "mdmctl get profiles [flags]")
if err := flagset.Parse(args); err != nil {
return err
}
ctx := context.Background()
profiles, err := cmd.profilesvc.GetProfiles(ctx, profile.GetProfilesOption{Identifier: *flIdentifier})
if err != nil {
return err
}
if *flIdentifier == "" || len(profiles) < 1 {
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintf(w, "Identifier\tLength\n")
for _, p := range profiles {
fmt.Fprintf(
w,
"%s\t%d\n",
p.Identifier,
len(p.Mobileconfig),
)
}
w.Flush()
} else if *flProfilePath != "" {
p := profiles[0]
var output *os.File
{
if *flProfilePath == "-" {
output = os.Stdout
} else {
var err error
output, err = os.Create(*flProfilePath)
if err != nil {
return err
}
defer output.Close()
}
}
_, err = output.Write([]byte(p.Mobileconfig))
if err != nil {
return err
}
if *flProfilePath != "-" {
fmt.Printf("wrote profile id %s to: %s\n", p.Identifier, *flProfilePath)
if len(profiles) > 1 {
fmt.Println("WARNING: more than one Profile returned; only saved first")
}
}
}
return nil
}