@@ -325,3 +325,85 @@ func validateJSONAgentOptionsSet(rawJSON json.RawMessage) error {
325
325
}
326
326
return nil
327
327
}
328
+
329
+ func FindAgentOptionsKeyPath (key string ) ([]string , error ) {
330
+ if key == "script_execution_timeout" {
331
+ return []string {"script_execution_timeout" }, nil
332
+ }
333
+
334
+ configPath , err := locateStructJSONKeyPath (key , "config" , osqueryAgentOptions {})
335
+ if err != nil {
336
+ return nil , fmt .Errorf ("locating key path in agent options: %w" , err )
337
+ }
338
+ if configPath != nil {
339
+ return configPath , nil
340
+ }
341
+
342
+ if key == "overrides" {
343
+ return []string {"overrides" }, nil
344
+ }
345
+ if key == "platforms" {
346
+ return []string {"overrides" , "platforms" }, nil
347
+ }
348
+
349
+ commandLinePath , err := locateStructJSONKeyPath (key , "command_line_flags" , osqueryCommandLineFlags {})
350
+ if err != nil {
351
+ return nil , fmt .Errorf ("locating key path in agent command line options: %w" , err )
352
+ }
353
+ if commandLinePath != nil {
354
+ return commandLinePath , nil
355
+ }
356
+
357
+ extensionsPath , err := locateStructJSONKeyPath (key , "extensions" , ExtensionInfo {})
358
+ if err != nil {
359
+ return nil , fmt .Errorf ("locating key path in agent extensions options: %w" , err )
360
+ }
361
+ if extensionsPath != nil {
362
+ return extensionsPath , nil
363
+ }
364
+
365
+ channelsPath , err := locateStructJSONKeyPath (key , "update_channels" , OrbitUpdateChannels {})
366
+ if err != nil {
367
+ return nil , fmt .Errorf ("locating key path in agent update channels: %w" , err )
368
+ }
369
+ if channelsPath != nil {
370
+ return channelsPath , nil
371
+ }
372
+
373
+ return nil , nil
374
+ }
375
+
376
+ // Only searches two layers deep
377
+ func locateStructJSONKeyPath (key , startKey string , target any ) ([]string , error ) {
378
+ optionsBytes , err := json .Marshal (target )
379
+ if err != nil {
380
+ return nil , fmt .Errorf ("unable to marshall target: %w" , err )
381
+ }
382
+
383
+ var opts map [string ]any
384
+
385
+ if err := json .Unmarshal (optionsBytes , & opts ); err != nil {
386
+ return nil , fmt .Errorf ("unable to unmarshall target: %w" , err )
387
+ }
388
+
389
+ var path [3 ]string
390
+ path [0 ] = startKey
391
+ for k , v := range opts {
392
+ path [1 ] = k
393
+ if k == key {
394
+ return path [:2 ], nil
395
+ }
396
+
397
+ switch v .(type ) {
398
+ case map [string ]any :
399
+ for k2 := range v .(map [string ]any ) {
400
+ path [2 ] = k2
401
+ if key == k2 {
402
+ return path [:3 ], nil
403
+ }
404
+ }
405
+ }
406
+ }
407
+
408
+ return nil , nil
409
+ }
0 commit comments