-
Notifications
You must be signed in to change notification settings - Fork 43
/
client.go
1260 lines (1048 loc) · 29.3 KB
/
client.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package pango
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"log/slog"
"mime"
"mime/multipart"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/PaloAltoNetworks/pango/errors"
"github.com/PaloAltoNetworks/pango/generic"
"github.com/PaloAltoNetworks/pango/plugin"
"github.com/PaloAltoNetworks/pango/util"
"github.com/PaloAltoNetworks/pango/version"
"github.com/PaloAltoNetworks/pango/xmlapi"
)
type LoggingInfo struct {
SLogHandler slog.Handler `json:"-"`
LogCategories LogCategory `json:"-"`
LogSymbols []string `json:"log_symbols"`
LogLevel slog.Level `json:"log_level"`
}
// Client is an XML API client connection. If provides wrapper functions
// for invoking the various PAN-OS API methods. After creating the client,
// invoke Setup() followed by Initialize() to prepare it for use.
type Client struct {
Hostname string `json:"hostname"`
Username string `json:"username"`
Password string `json:"password"`
ApiKey string `json:"api_key"`
Protocol string `json:"protocol"`
Port int `json:"port"`
Target string `json:"target"`
ApiKeyInRequest bool `json:"api_key_in_request"`
Headers map[string]string `json:"headers"`
Logging LoggingInfo `json:"logging"`
// Set to true if you want to check environment variables
// for auth and connection properties.
CheckEnvironment bool `json:"-"`
AuthFile string `json:"-"`
// HTTP transport options. Note that the SkipVerifyCertificate setting
// is only used if you do not specify a HTTP transport yourself.
SkipVerifyCertificate bool `json:"skip_verify_certificate"`
Transport *http.Transport `json:"-"`
// Variables determined at runtime.
Version version.Number `json:"-"`
SystemInfo map[string]string `json:"-"`
Plugin []plugin.Info `json:"-"`
// Internal variables.
con *http.Client
api_url string
configTree *generic.Xml
logger *categoryLogger
// Variables for testing, response bytes, headers, and response index.
testInput []*http.Request
testOutput []*http.Response
testIndex int
authFileContent []byte
}
// Versioning returns the version number of PAN-OS.
func (c *Client) Versioning() version.Number {
return c.Version
}
// Plugins returns the list of plugins.
func (c *Client) Plugins() []plugin.Info {
return c.Plugin
}
// GetTarget returns the Target param, used in certain API calls.
func (c *Client) GetTarget() string {
return c.Target
}
// IsPanorama returns true if this is Panorama.
func (c *Client) IsPanorama() (bool, error) {
if len(c.SystemInfo) == 0 {
return false, fmt.Errorf("SystemInfo is nil")
}
model, ok := c.SystemInfo["model"]
if !ok {
return false, fmt.Errorf("model not present in SystemInfo")
}
return model == "Panorama" || strings.HasPrefix(model, "M-"), nil
}
// IsFirewall returns true if this PAN-OS seems to be a NGFW instance.
func (c *Client) IsFirewall() (bool, error) {
ans, err := c.IsPanorama()
if err != nil {
return ans, err
}
return !ans, nil
}
// Setup does validation and initialization in preparation to start executing API
// commands against PAN-OS.
func (c *Client) Setup() error {
var err error
// Load up the JSON config file.
var json_client Client
if c.AuthFile != "" {
var b []byte
if len(c.testOutput) == 0 {
b, err = os.ReadFile(c.AuthFile)
} else {
b = c.authFileContent
}
if err != nil {
return err
}
if err = json.Unmarshal(b, &json_client); err != nil {
return err
}
}
// Hostname.
if c.Hostname == "" {
if val := os.Getenv("PANOS_HOSTNAME"); c.CheckEnvironment && val != "" {
c.Hostname = val
} else if json_client.Hostname != "" {
c.Hostname = json_client.Hostname
}
}
// Username.
if c.Username == "" {
if val := os.Getenv("PANOS_USERNAME"); c.CheckEnvironment && val != "" {
c.Username = val
} else {
c.Username = json_client.Username
}
}
// Password.
if c.Password == "" {
if val := os.Getenv("PANOS_PASSWORD"); c.CheckEnvironment && val != "" {
c.Password = val
} else {
c.Password = json_client.Password
}
}
// API key.
if c.ApiKey == "" {
if val := os.Getenv("PANOS_API_KEY"); c.CheckEnvironment && val != "" {
c.ApiKey = val
} else {
c.ApiKey = json_client.ApiKey
}
}
// Protocol.
if c.Protocol == "" {
if val := os.Getenv("PANOS_PROTOCOL"); c.CheckEnvironment && val != "" {
c.Protocol = val
} else if json_client.Protocol != "" {
c.Protocol = json_client.Protocol
} else {
c.Protocol = "https"
}
}
if c.Protocol != "http" && c.Protocol != "https" {
return fmt.Errorf("Invalid protocol %q. Must be \"http\" or \"https\"", c.Protocol)
}
// Port.
if c.Port == 0 {
if val := os.Getenv("PANOS_PORT"); c.CheckEnvironment && val != "" {
if cp, err := strconv.Atoi(val); err != nil {
return fmt.Errorf("Failed to parse the env port number: %s", err)
} else {
c.Port = cp
}
} else if json_client.Port != 0 {
c.Port = json_client.Port
}
}
if c.Port > 65535 {
return fmt.Errorf("Port %d is out of bounds", c.Port)
}
// Target.
if c.Target == "" {
if val := os.Getenv("PANOS_TARGET"); c.CheckEnvironment && val != "" {
c.Target = val
} else {
c.Target = json_client.Target
}
}
// Headers.
if len(c.Headers) == 0 {
if val := os.Getenv("PANOS_HEADERS"); c.CheckEnvironment && val != "" {
if err := json.Unmarshal([]byte(val), &c.Headers); err != nil {
return err
}
}
if len(c.Headers) == 0 && len(json_client.Headers) > 0 {
c.Headers = make(map[string]string)
for k, v := range json_client.Headers {
c.Headers[k] = v
}
}
}
// Skip verify cert.
if !c.SkipVerifyCertificate {
if val := os.Getenv("PANOS_SKIP_VERIFY_CERTIFICATE"); c.CheckEnvironment && val != "" {
if vcb, err := strconv.ParseBool(val); err != nil {
return err
} else if vcb {
c.SkipVerifyCertificate = vcb
}
}
if !c.SkipVerifyCertificate && json_client.SkipVerifyCertificate {
c.SkipVerifyCertificate = true
}
}
err = c.setupLogging(c.Logging)
if err != nil {
return err
}
// Setup the client.
if c.Transport == nil {
c.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: c.SkipVerifyCertificate,
},
}
}
c.con = &http.Client{
Transport: c.Transport,
}
// Sanity check.
if c.Hostname == "" {
return fmt.Errorf("hostname must be specified")
}
if c.ApiKey == "" && (c.Username == "" && c.Password == "") {
return fmt.Errorf("either API key or both username and password must be specified")
}
// Configure the api url.
if c.Port == 0 {
c.api_url = fmt.Sprintf("%s://%s/api", c.Protocol, c.Hostname)
} else {
c.api_url = fmt.Sprintf("%s://%s:%d/api", c.Protocol, c.Hostname, c.Port)
}
return nil
}
// SetupLocalInspection configures the client for local inspection.
//
// The version number is taken from the schema if it's present. If it is not
// present, then the version number falls back to what's specified in panosVersion.
func (c *Client) SetupLocalInspection(schema, panosVersion string) error {
var err error
if err = c.LoadPanosConfig([]byte(schema)); err != nil {
return err
}
if c.Version.Major == 0 && c.Version.Minor == 0 && c.Version.Patch == 0 {
if panosVersion == "" {
return fmt.Errorf("no version found in the schema; the version must be specified")
}
c.Version, err = version.New(panosVersion)
if err != nil {
return err
}
}
return nil
}
// Initialize retrieves the API key if needed then retrieves the system info.
func (c *Client) Initialize(ctx context.Context) error {
var err error
if c.ApiKey == "" {
if err = c.RetrieveApiKey(ctx); err != nil {
return err
}
}
if err = c.RetrieveSystemInfo(ctx); err != nil {
return err
}
if err = c.RetrievePlugins(ctx); err != nil {
return err
}
return nil
}
// RetrieveSystemInfo performs "show system info" and saves it SystemInfo.
func (c *Client) RetrieveSystemInfo(ctx context.Context) error {
var err error
type req struct {
XMLName xml.Name `xml:"show"`
Cmd string `xml:"system>info"`
}
type system struct {
Tags []generic.Xml `xml:",any"`
}
type resp struct {
System system `xml:"result>system"`
}
cmd := &xmlapi.Op{
Command: req{},
Target: c.Target,
}
var ans resp
if _, _, err = c.Communicate(ctx, cmd, false, &ans); err != nil {
return err
}
c.SystemInfo = make(map[string]string, len(ans.System.Tags))
for _, t := range ans.System.Tags {
if t.TrimmedText == nil {
continue
}
c.SystemInfo[t.XMLName.Local] = *t.TrimmedText
if t.XMLName.Local == "sw-version" {
c.Version, err = version.New(*t.TrimmedText)
if err != nil {
return err
}
}
}
return nil
}
// RetrievePanosConfig retrieves the running config, candidate config,
// or the specified saved config file.
//
// If the name is candidate, then the candidate config is retrieved. If the
// name is running, then the running config is retrieved. Otherwise, then
// the name is assumed to be the name of the saved config.
func (c *Client) RetrievePanosConfig(ctx context.Context, name string) ([]byte, error) {
type req struct {
XMLName xml.Name `xml:"show"`
Running *string `xml:"config>running"`
Candidate *string `xml:"config>candidate"`
Saved *string `xml:"config>saved"`
}
type rdata struct {
Data []byte `xml:",innerxml"`
}
type resp struct {
XMLName xml.Name `xml:"response"`
Result rdata `xml:"result"`
}
var s string
cs := req{}
switch name {
case "candidate":
cs.Candidate = &s
case "running":
cs.Running = &s
default:
cs.Saved = &name
}
cmd := &xmlapi.Op{
Command: cs,
Target: c.Target,
}
var ans resp
if _, _, err := c.Communicate(ctx, cmd, false, &ans); err != nil {
return nil, err
}
return ans.Result.Data, nil
}
// RetrieveApiKey refreshes the API key.
//
// This function unsets the ApiKey value and thus requires that the Username and Password
// be specified.
func (c *Client) RetrieveApiKey(ctx context.Context) error {
type key_gen_ans struct {
Key string `xml:"result>key"`
}
cmd := &xmlapi.KeyGen{
Username: c.Username,
Password: c.Password,
}
var ans key_gen_ans
_, _, err := c.Communicate(ctx, cmd, false, &ans)
if err != nil {
return err
}
c.ApiKey = ans.Key
return nil
}
// RetrievePlugins refreshes the Plugins info from PAN-OS.
func (c *Client) RetrievePlugins(ctx context.Context) error {
cmd := &xmlapi.Op{
Command: plugin.GetPlugins{},
}
var ans plugin.PackageListing
_, _, err := c.Communicate(ctx, cmd, false, &ans)
if err != nil {
return err
}
c.Plugin = ans.Listing()
return nil
}
// LoadPanosConfig stores the given XML document into this client, allowing
// the user to use various namespace functions to query the config. This
// is referred to as local inspection mode.
//
// The config given must be in the form of `<config>...</config>`.
//
// If the 'detail-version' attribute is present in the XML, then it's saved to this
// instance's Version attribute.
func (c *Client) LoadPanosConfig(config []byte) error {
var ans generic.Xml
if err := xml.Unmarshal(config, &ans); err != nil {
return err
}
if ans.XMLName.Local != "config" {
return fmt.Errorf("Expected \"config\" at the root, found %q", ans.XMLName.Local)
}
if ans.DetailedVersion != nil {
vn, err := version.New(*ans.DetailedVersion)
if err != nil {
return err
}
c.Version = vn
}
c.configTree = &generic.Xml{
XMLName: xml.Name{
Local: "a",
},
Nodes: []generic.Xml{ans},
}
return nil
}
// ReadFromConfig returns the XML at the given XPATH location. This is
// referred to as local inspection mode.
//
// If the XPATH is a listing, then set withPackaging to false.
func (c *Client) ReadFromConfig(ctx context.Context, path []string, withPackaging bool, ans any) ([]byte, error) {
if c.configTree == nil {
return nil, fmt.Errorf("no config loaded")
}
if len(path) == 0 {
return nil, fmt.Errorf("path is empty")
}
entryPrefix := "entry[@name='"
entrySuffix := "']"
config := c.configTree
for _, pp := range path {
var tag, name string
if strings.HasPrefix(pp, entryPrefix) && strings.HasSuffix(pp, entrySuffix) {
tag = "entry"
name = strings.TrimSuffix(strings.TrimPrefix(pp, entryPrefix), entrySuffix)
} else {
tag = pp
}
found := false
for _, node := range config.Nodes {
if node.XMLName.Local == tag && (node.Name == nil || *node.Name == name) {
found = true
config = &node
break
}
}
if !found {
config = nil
break
}
}
if config == nil {
return nil, errors.ObjectNotFound()
}
b, err := xml.Marshal(config)
if err != nil {
return b, err
}
var newb []byte
if !withPackaging {
newb = append([]byte(nil), b...)
} else {
newb = make([]byte, 0, len(b)+7)
newb = append(newb, []byte("<q>")...)
newb = append(newb, b...)
newb = append(newb, []byte("</q>")...)
}
if ans == nil {
return newb, nil
}
err = xml.Unmarshal(newb, ans)
return newb, err
}
func (c *Client) Clock(ctx context.Context) (time.Time, error) {
type creq struct {
XMLName xml.Name `xml:"show"`
Cmd string `xml:"clock"`
}
type cresp struct {
Result string `xml:"result"`
}
cmd := &xmlapi.Op{
Command: creq{},
Target: c.Target,
}
var ans cresp
if _, _, err := c.Communicate(ctx, cmd, false, &ans); err != nil {
return time.Time{}, err
}
return time.Parse(time.UnixDate+"\n", ans.Result)
}
// MultiConfig does a "multi-config" type command.
//
// Param strict should be true if you want strict transactional support.
//
// Note that the error returned from this function is only if there was an error
// unmarshaling the response into the the multi config response struct. If the
// multi config itself failed, then the reason can be found in its results.
func (c *Client) MultiConfig(ctx context.Context, mc *xmlapi.MultiConfig, strict bool, extras url.Values) ([]byte, *http.Response, *xmlapi.MultiConfigResponse, error) {
cmd := &xmlapi.Config{
Action: "multi-config",
Element: mc,
StrictTransactional: strict,
Target: c.Target,
}
text, httpResp, err := c.Communicate(ctx, cmd, false, nil)
// If the text is empty, then the err will have a real error we should report to the
// invoker. However, if the text is not empty, then ignore the error and parse the
// multi config results using the specialized struct custom designed to handle it.
if len(text) == 0 {
return text, httpResp, nil, err
}
mcResp, err := xmlapi.NewMultiConfigResponse(text)
if err != nil {
return text, httpResp, nil, err
}
if !mcResp.Ok() {
return text, httpResp, mcResp, mcResp
}
return text, httpResp, mcResp, nil
}
// RequestPasswordHash requests a password hash of the given string.
func (c *Client) RequestPasswordHash(ctx context.Context, v string) (string, error) {
type phash_req struct {
XMLName xml.Name `xml:"request"`
Val string `xml:"password-hash>password"`
}
cmd := &xmlapi.Op{
Command: phash_req{
Val: v,
},
Target: c.Target,
}
type phash_ans struct {
Hash string `xml:"result>phash"`
}
var ans phash_ans
if _, _, err := c.Communicate(ctx, cmd, false, &ans); err != nil {
return "", err
}
return ans.Hash, nil
}
// ValidateConfig performs a commit config validation check.
//
// Use WaitForJob and the uint returned from this function to get the
// results of the job.
func (c *Client) ValidateConfig(ctx context.Context, sleep time.Duration) (uint, error) {
type req struct {
XMLName xml.Name `xml:"validate"`
Cmd string `xml:"full"`
}
cmd := &xmlapi.Op{
Command: req{},
Target: c.Target,
}
id, _, _, err := c.StartJob(ctx, cmd)
return id, err
}
// WaitForLogs polls PAN-OS until the given log retrieval job is complete.
//
// The sleep param is the time to wait between polling. Note that a sleep
// time less than 2 seconds may cause PAN-OS to take longer to finish the
// job.
func (c *Client) WaitForLogs(ctx context.Context, id uint, sleep time.Duration, resp any) ([]byte, error) {
if id == 0 {
return nil, fmt.Errorf("job ID must be specified")
}
var err error
var data []byte
var prev string
cmd := &xmlapi.Log{
Action: "get",
JobId: id,
}
var ans util.BasicJob
for {
ans = util.BasicJob{}
data, _, err = c.Communicate(ctx, cmd, false, &ans)
if err != nil {
return data, err
}
if ans.Status != prev {
prev = ans.Status
// log %d id and %s prev
}
if ans.Status == "FIN" {
break
}
if sleep > 0 {
time.Sleep(sleep)
}
}
if ans.Result == "FAIL" {
if len(ans.Details.Lines) > 0 {
return data, fmt.Errorf(ans.Details.String())
} else {
return data, fmt.Errorf("Job %d has failed", id)
}
}
if resp == nil {
return data, nil
}
err = xml.Unmarshal(data, resp)
return data, err
}
// WaitForJob polls PAN-OS until the given job finishes.
//
// The sleep param is the time to wait between polling. Note that a sleep
// time less than 2 seconds may cause PAN-OS to take longer to finish the
// job.
func (c *Client) WaitForJob(ctx context.Context, id uint, sleep time.Duration, resp any) error {
var err error
var prev uint
var data []byte
dp := false
all_ok := true
type req struct {
XMLName xml.Name `xml:"show"`
Id uint `xml:"jobs>id"`
}
cmd := &xmlapi.Op{
Command: req{
Id: id,
},
Target: c.Target,
}
var ans util.BasicJob
for {
ans = util.BasicJob{}
data, _, err = c.Communicate(ctx, cmd, false, &ans)
if err != nil {
return err
}
if ans.Progress != prev {
prev = ans.Progress
// log the change.
}
// Check for device commits.
all_done := true
for _, d := range ans.Devices {
// log %q d.Serial %s d.Result
if d.Result == "PEND" {
all_done = false
break
} else if d.Result != "OK" && all_ok {
all_ok = false
}
}
// Check if the job's done.
if ans.Progress == 100 {
if all_done {
break
} else if !dp {
// log waiting for %d devices len(ans.Devices)
dp = true
}
}
if sleep > 0 {
time.Sleep(sleep)
}
}
if ans.Result == "FAIL" {
if len(ans.Details.Lines) > 0 {
return fmt.Errorf(ans.Details.String())
} else {
return fmt.Errorf("Job %d has failed", id)
}
} else if !all_ok {
return fmt.Errorf("Commit failed on one or more devices")
}
if resp == nil {
return nil
}
return xml.Unmarshal(data, resp)
}
// RevertToRunningConfig discards any changes made and reverts to the last
// committed config.
func (c *Client) RevertToRunningConfig(ctx context.Context, vsys string) error {
type req struct {
XMLName xml.Name `xml:"load"`
Cmd string `xml:"config>from"`
}
cmd := &xmlapi.Op{
Command: req{
Cmd: "running-config.xml",
},
Vsys: vsys,
Target: c.Target,
}
_, _, err := c.Communicate(ctx, cmd, false, nil)
return err
}
// StartJob sends the given command, which starts a job on PAN-OS.
//
// The uint returned is the job ID.
func (c *Client) StartJob(ctx context.Context, cmd util.PangoCommand) (uint, []byte, *http.Response, error) {
var ans util.JobResponse
b, resp, err := c.Communicate(ctx, cmd, false, &ans)
if err != nil {
return 0, b, resp, err
}
return ans.Id, b, resp, nil
}
// Communicate sends the given content to PAN-OS.
//
// The API key is sent either in the request body or as a header.
//
// The timeout for the operation is taken from the context.
func (c *Client) Communicate(ctx context.Context, cmd util.PangoCommand, strip bool, ans any) ([]byte, *http.Response, error) {
if cmd == nil {
return nil, nil, fmt.Errorf("cmd is nil")
}
data, err := cmd.AsUrlValues()
if err != nil {
return nil, nil, err
}
if c.ApiKeyInRequest && c.ApiKey != "" && data.Get("key") == "" {
data.Set("key", c.ApiKey)
}
c.logSend(data)
req, err := http.NewRequestWithContext(ctx, "POST", c.api_url, strings.NewReader(data.Encode()))
if err != nil {
return nil, nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return c.sendRequest(ctx, req, strip, ans)
}
// ImportFile imports the given file into PAN-OS.
func (c *Client) ImportFile(ctx context.Context, cmd *xmlapi.Import, content, filename, fp string, strip bool, ans any) ([]byte, *http.Response, error) {
if cmd == nil {
return nil, nil, fmt.Errorf("cmd is nil")
}
data, err := cmd.AsUrlValues()
if err != nil {
return nil, nil, err
}
if c.ApiKeyInRequest && c.ApiKey != "" && data.Get("key") == "" {
data.Set("key", c.ApiKey)
}
c.logSend(data)
buf := bytes.Buffer{}
w := multipart.NewWriter(&buf)
for k := range data {
err = w.WriteField(k, data.Get(k))
if err != nil {
return nil, nil, err
}
}
w2, err := w.CreateFormFile(fp, filename)
if err != nil {
return nil, nil, err
}
if _, err = io.Copy(w2, strings.NewReader(content)); err != nil {
return nil, nil, err
}
w.Close()
req, err := http.NewRequestWithContext(ctx, "POST", c.api_url, &buf)
if err != nil {
return nil, nil, err
}
req.Header.Set("Content-Type", w.FormDataContentType())
return c.sendRequest(ctx, req, strip, ans)
}
// ExportFile retrieves a file from PAN-OS.
func (c *Client) ExportFile(ctx context.Context, cmd *xmlapi.Export, ans any) (string, []byte, *http.Response, error) {
if cmd == nil {
return "", nil, nil, fmt.Errorf("cmd is nil")
}
data, err := cmd.AsUrlValues()
if err != nil {
return "", nil, nil, err
}
if c.ApiKeyInRequest && c.ApiKey != "" && data.Get("key") == "" {
data.Set("key", c.ApiKey)
}
req, err := http.NewRequestWithContext(ctx, "POST", c.api_url, strings.NewReader(data.Encode()))
if err != nil {
return "", nil, nil, err
}
b, resp, err := c.sendRequest(ctx, req, false, ans)
if err != nil {
return "", b, resp, err
}
var filename string
mediatype, params, err := mime.ParseMediaType(resp.Header.Get("Content-Disposition"))
if err == nil && mediatype == "attachment" {
filename = params["filename"]
}
return filename, b, resp, nil
}
// GetTechSupportFile returns the tech support file .tgz file.
func (c *Client) GetTechSupportFile(ctx context.Context) (string, []byte, error) {
cmd := &xmlapi.Export{
Category: "tech-support",
}
id, _, _, err := c.StartJob(ctx, cmd)
if err != nil {
return "", nil, err
}
cmd.Action = "status"
cmd.JobId = id
var resp util.BasicJob
var prev uint
for {
resp = util.BasicJob{}
if _, _, _, err = c.ExportFile(ctx, cmd, &resp); err != nil {
return "", nil, err
}
// The progress is not an uint when the job completes, so don't print
// the progress as 0 when the job is actually complete.
if resp.Progress != prev && resp.Progress != 0 {
prev = resp.Progress
// log %d resp.Id at %d prev percentage complete.
}
if resp.Status == "FIN" {
break
}
time.Sleep(2 * time.Second)
}
if resp.Result == "FAIL" {
return "", nil, fmt.Errorf(resp.Details.String())
}
cmd.Action = "get"
filename, b, _, err := c.ExportFile(ctx, cmd, nil)
return filename, b, err
}