-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrata-import.go
1241 lines (1041 loc) · 33.1 KB
/
errata-import.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 main
import "compress/bzip2"
import "encoding/json"
import "encoding/xml"
import "fmt"
import "io/ioutil"
import "github.com/DavidGamba/go-getoptions"
import "github.com/hashicorp/logutils"
import "github.com/kolo/xmlrpc"
import "log"
import "os"
import "regexp"
import "strings"
import "strconv"
import "syscall"
import "time"
import "net"
// These two need to be loaded if cert-check is to be disabled
import "net/http"
import "crypto/tls"
// URLs (used in --simple mode)
const errataurl = "https://cefs.steve-meier.de/errata.latest.json.bz2"
const rhsaovalurl = "https://www.redhat.com/security/data/oval/com.redhat.rhsa-all.xml.bz2"
const Version int = 20200419
const timelayout = "2006-01-02 15:04:05"
var SupportedAPI = []float64{10.9, // Spacewalk 0.6
10.10, // Spacewalk 0.7
10.11, // Spacewalk 0.8 through 1.1
10.15, // Spacewalk 1.2
10.16, // Spacewalk 1.3 and 1.4
11.00, // Spacewalk 1.5
11.1, // Spacewalk 1.6 through 1.8
12, // Spacewalk 1.9
13, // Spacewalk 2.0
13.0,
14, // Spacewalk 2.1
14.0,
15, // Spacewalk 2.2
15.0,
16, // Spacewalk 2.3
16.0,
17, // Spacewalk 2.4
17.0,
18, // Spacewalk 2.5
18.0,
19, // Spacewalk 2.6
19.0,
20, // Spacewalk 2.7
20.0,
21, // Spacewalk 2.8
21.0,
22, // Spacewalk 2.9
22.0,
23, // Spacewalk 2.10
23.0,
}
type Meta struct {
Author string
Disclaimer string
License string
Timestamp string
}
type Erratum struct {
Id string `json:"id"` // Only needed in array approach
Description string `json:"description"`
From string `json:"from"`
IssueDate string `json:"issue_date"`
Keywords []string `json:"keywords"`
Manual string `json:"manual"`
Notes string `json:"notes"`
OsArch []string `json:"os_arch"`
OsRelease []string `json:"os_release"`
Packages []string `json:"packages"`
Product string `json:"product"`
References string `json:"references"`
Release string `json:"release"`
Severity string `json:"severity"`
Solution string `json:"solution"`
Synopsis string `json:"synopsis"`
Topic string `json:"topic"`
Type string `json:"type"`
}
type Raw struct {
Advisories []Erratum
Meta Meta
}
type SWerrata struct {
Synopsis string `xmlrpc:"synopsis"`
AdvisoryName string `xmlrpc:"advisory_name"`
AdvisoryRelease int `xmlrpc:"advisory_release"`
AdvisoryType string `xmlrpc:"advisory_type"`
From string `xmlrpc:"errataFrom"`
Product string `xmlrpc:"product"`
Topic string `xmlrpc:"topic"`
Description string `xmlrpc:"description"`
References string `xmlrpc:"references"`
Notes string `xmlrpc:"notes"`
Solution string `xmlrpc:"solution"`
}
// The Url field is not supported in all versions of Spacewalk
// Version 1.3 and newer seems to support it
type Bugzilla struct {
Text string `xml:",chardata" xmlrpc:"summary"`
Href string `xml:"href,attr" xmlrpc:"url"`
ID int64 `xml:"id,attr" xmlrpc:"id"`
}
type Inventory struct {
filename2id map[string]int64
id2channels map[int64][]string
id2filename map[int64]string
}
type OvalData struct {
Description string
References []string
Rights string
Bugs []Bugzilla
}
func main () {
var debug bool
var quiet bool
var autopush bool
var publish bool
var server string
var created int
var updated int
var security bool
var bugfix bool
var enhancement bool
var ignoreapiversion bool
var protocol string
var insecure bool
var inchannels *[]string
var exchannels *[]string
var syncchannels bool
var synctimeout int
var exerrata *[]string
var erratafile string
var rhsaovalfile string
opt := getoptions.New()
opt.BoolVar(&debug, "debug", false)
opt.BoolVar(&quiet, "quiet", false)
opt.StringVar(&server, "server", "localhost")
opt.BoolVar(&publish, "publish", false)
opt.BoolVar(&autopush, "autopush", false)
opt.BoolVar(&security, "security", false)
opt.BoolVar(&bugfix, "bugfix", false)
opt.BoolVar(&enhancement, "enhancement", false)
opt.BoolVar(&ignoreapiversion, "ignore-api-version", false)
opt.StringVar(&protocol, "protocol", "http")
opt.BoolVar(&insecure, "insecure", false)
inchannels = opt.StringSlice("include-channels", 1, 255)
exchannels = opt.StringSlice("exclude-channels", 1, 255)
opt.BoolVar(&syncchannels, "sync-channels", false)
opt.IntVar(&synctimeout, "sync-timeout", 600)
exerrata = opt.StringSlice("exclude-errata", 1, 255)
opt.StringVar(&erratafile, "errata", "")
opt.StringVar(&rhsaovalfile, "rhsa-oval", "")
// Parse options
remaining, err := opt.Parse(os.Args[1:])
// Set up logger
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG","INFO","WARNING","ERROR"},
MinLevel: logutils.LogLevel(min_log_level(debug, quiet)),
Writer: os.Stdout,
}
// Set up log filter
log.SetOutput(filter)
log.Printf("[DEBUG] Version is %d\n", Version)
if len(os.Args[1:]) == 0 {
log.Print(opt.Help())
os.Exit(4)
}
if err != nil {
log.Printf("[ERROR] Failed to parse options: %v\n", err)
os.Exit(4)
}
if len(remaining) > 0 {
log.Printf("[ERROR] The following options are unrecognized: %v\n", remaining)
os.Exit(4)
}
// Check if running as root
if running_as_root() {
log.Println("[INFO] Running as root is not recommended!")
}
// --autopush is deprecated
if autopush {
log.Println("[INFO] The --autopush option is deprecated and has no effect anymore")
}
// If no errata type is selected, enable all
if (!(security || bugfix || enhancement)) {
security, bugfix, enhancement = true, true, true
}
// Load errata data
var allerrata Raw
if erratafile != "" {
log.Printf("[INFO] Loading errata data from %s\n", erratafile)
allerrata = ParseErrata(*file_content(erratafile))
} else {
log.Printf("[INFO] Loading errata data from %s\n", errataurl)
allerrata = ParseErrata(*download_bzip2(errataurl))
}
if len(allerrata.Advisories) > 0 {
log.Printf("[INFO] Loaded %d advisories from errata file\n", len(allerrata.Advisories))
} else {
log.Printf("[ERROR] Could not parse errata data from %s\n", erratafile)
os.Exit(5)
}
// Load Red Hat OVAL data
var oval map[string]OvalData
if rhsaovalfile != "" {
log.Printf("[INFO] Loading RHSA oval data from %s\n", rhsaovalfile)
oval = ParseOval(*file_content(rhsaovalfile))
} else {
log.Printf("[INFO] Loading RHSA oval data from %s\n", rhsaovalurl)
oval = ParseOval(*download_bzip2(rhsaovalurl))
}
if len(oval) > 0 {
log.Printf("[INFO] Loaded %d datasets from Red Hat OVAL file\n", len(oval))
}
// Configure timeout
// Source: https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779
// and TLS options
// Source: https://stackoverflow.com/questions/12122159/how-to-do-a-https-request-with-bad-certificate
var netTransport = &http.Transport{ Dial: (&net.Dialer{ Timeout: 5 * time.Second, }).Dial,
TLSHandshakeTimeout: 5 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, }
// Create XML-RPC client
client, err := xmlrpc.NewClient(protocol + "://" + server + "/rpc/api", netTransport)
if err != nil {
log.Println("[ERROR] Could not create XML-RPC client: ", err.Error())
os.Exit(2)
}
// Get server version
var apiversion string
err = client.Call("api.get_version", nil, &apiversion)
if err != nil {
if strings.Contains(err.Error(), "cannot validate certificate") {
log.Println("[ERROR] Certicate verification failed. Use --insecure if you have a self-signed cert")
os.Exit(6)
}
if strings.Contains(err.Error(), "i/o timeout") {
log.Println("[ERROR] Timeout connecting to server")
os.Exit(6)
}
log.Printf("[ERROR] Could not determine server version: %v\n", err)
os.Exit(2)
}
if (!check_api_support(apiversion, SupportedAPI) && !ignoreapiversion) {
log.Printf("[ERROR] API version %s is not supported!\n", apiversion)
os.Exit(3)
} else {
log.Printf("[INFO] API version %s is supported", apiversion)
}
// Read and check credentials
username := os.Getenv("SPACEWALK_USER")
password := os.Getenv("SPACEWALK_PASS")
if (username == "") || (password == "") {
log.Println("[ERROR] Credentials not set!")
os.Exit(3)
}
// Authenticate and get sessionKey
// Setup defered closing of session
var sessionkey string = init_session(client, username, password)
if sessionkey == "" {
log.Println("[ERROR] Authentication failed!")
os.Exit(1)
}
defer close_session(client, sessionkey)
// Check admin status
if publish {
if (user_is_admin(client, sessionkey, username)) {
log.Printf("[INFO] User %s has administrator access to this server\n", username)
} else {
log.Printf("[ERROR] User %s does NOT have administrator access", username);
log.Println("[ERROR] You have set --publish but your user has insufficient access rights");
log.Println("[ERROR] Either use an account that is Satellite/Org/Channel Administator privileges or omit --publish");
os.Exit(1)
}
}
// List all channels
var channels []string = get_channel_list(client, sessionkey)
// Handle channel includes and excludes
channels = include_channels(channels, inchannels)
channels = exclude_channels(channels, exchannels)
// Check that there are still channels left
if len(channels) == 0 {
log.Println("[ERROR] All channels have been excluded")
os.Exit(8)
}
// Sync channels, if requested
if string_to_float(apiversion) < 11 {
log.Println("[INFO] This API version does not support synching")
syncchannels = false
}
if syncchannels {
for _, channel := range channels {
log.Printf("[INFO] Starting Repository Sync for channel %s\n", channel)
if channel_sync_repo(client, sessionkey, channel) {
syncstart := time.Now()
for {
time.Sleep(30 * time.Second)
if get_channel_last_sync(client, sessionkey, channel).After(syncstart) {
// Sync finished
log.Printf("[INFO] Sync for channel %s finished\n", channel)
break
}
if time.Now().After(syncstart.Add(time.Duration(synctimeout) * time.Second)) {
// Sync failed
log.Printf("[ERROR] Repository Sync for channel %s failed!\n", channel)
break
}
}
}
}
}
// Get packages of channel
log.Println("[INFO] Getting server inventory")
var inv Inventory = get_inventory(client, sessionkey, channels)
// Get existing errata
var existing = get_existing_errata(client, sessionkey, channels)
// Process errata
for _, errata := range allerrata.Advisories {
if errata_is_excluded(errata.Id, exerrata) {
log.Printf("[INFO] Excluding %s\n", errata.Id)
continue
}
if (errata.Type == "Security Advisory" && !security) {
log.Printf("[INFO] Skipping %s\n", errata.Id)
continue
}
if (errata.Type == "Bug Fix Advisory" && !bugfix) {
log.Printf("[INFO] Skipping %s\n", errata.Id)
continue
}
if (errata.Type == "Product Enhancement Advisory" && !enhancement) {
log.Printf("[INFO] Skipping %s\n", errata.Id)
continue
}
log.Printf("[INFO] Processing %s\n", errata.Id)
var pkglist []int64 = get_packages_for_errata(errata, inv)
if len(pkglist) == 0 {
log.Printf("[INFO] Skipping errata %s (%s) -- No packages found\n", errata.Id, errata.Synopsis);
continue
}
var chanlist []string = get_channels_of_packages(pkglist, inv)
var info SWerrata
info.AdvisoryName = errata.Id
info.AdvisoryType = errata.Type
info.Synopsis = errata.Synopsis
info.Description = errata.Description
info.Product = errata.Product
info.References = errata.References
info.Solution = errata.Solution
info.Topic = errata.Topic
info.Notes = errata.Notes
info.From = errata.From
// If Red Hat Oval data is available, use it
if oval[(errata.Id)].Description != "" {
info.Description = oval[(errata.Id)].Description
}
if oval[(errata.Id)].Rights != "" {
info.Notes = oval[(errata.Id)].Rights
}
var success bool
if exists := existing[(errata.Id)]; !exists {
// Create Errata
log.Printf("[INFO] Creating errata for %s (%s) (%d of %d)\n", errata.Id, errata.Synopsis, len(pkglist), len(errata.Packages))
if string_to_float(apiversion) >= 10.16 {
success = create_errata(client, sessionkey, info, oval[(errata.Id)].Bugs, errata.Keywords, pkglist, false, []string{})
if success { created++ }
} else {
success = create_errata(client, sessionkey, info, []Bugzilla{}, errata.Keywords, pkglist, false, []string{})
if success { created++ }
}
if string_to_float(apiversion) >= 12 {
log.Printf("[INFO] Adding issue date to %s\n", errata.Id)
issuedate, _ := time.Parse(timelayout, errata.IssueDate)
success = add_issue_date(client, sessionkey, errata.Id, issuedate)
if !success { log.Printf("[ERROR] Adding issue date to %s failed\n", errata.Id) }
}
if string_to_float(apiversion) >= 21 && errata.Severity != "" {
log.Printf("[INFO] Adding severity %s to %s\n", errata.Severity, errata.Id)
success = add_severity(client, sessionkey, errata.Id, errata.Severity)
if !success { log.Printf("[ERROR] Adding severity to %s failed\n", errata.Id) }
}
if publish {
for _, singlechannel := range chanlist {
log.Printf("[INFO] Publishing %s to channel %s\n", errata.Id, singlechannel)
success = publish_errata(client, sessionkey, errata.Id, []string{singlechannel})
if !success { log.Printf("[ERROR] Publishing %s to channel %s failed\n", errata.Id, singlechannel) }
}
if errata.Type == "Security Advisory" && oval[(errata.Id)].References != nil {
log.Printf("[INFO] Adding CVE information to %s\n", errata.Id)
success = add_cve_to_errata(client, sessionkey, info, oval[(errata.Id)].References)
if !success { log.Printf("[INFO] Adding CVE information to %s failed\n", errata.Id) }
}
}
} else {
// Update Errata
var curlist []int64 = list_packages(client, sessionkey, errata.Id)
var newlist []int64 = only_in_first_int64(pkglist, curlist)
if len(pkglist) > len(curlist) {
log.Printf("[INFO] Adding packages to %s\n", errata.Id)
var pkgsadded int64 = add_packages(client, sessionkey, errata.Id, newlist)
if pkgsadded > 0 { updated++ }
if publish {
for _, channel := range get_channels_of_packages(newlist, inv) {
log.Printf("[INFO] Republishing %s to channel %s\n", errata.Id, channel)
success = publish_errata(client, sessionkey, errata.Id, []string{channel})
if !success { log.Printf("[ERROR] Republishing %s to channel %s failed\n", errata.Id, channel) }
}
}
}
}
// Restore channel membership of package after publishing to multiple channels
if publish && len(chanlist) > 1 {
for _, pkg := range pkglist {
oldchannels := inv.id2channels[pkg]
nowchannels := list_providing_channels(client, sessionkey, pkg)
pushedto := only_in_first_string(nowchannels, oldchannels)
for _, channel := range pushedto {
log.Printf("[INFO] Removing package %s from channel %s\n", inv.id2filename[pkg], channel)
remove_packages_from_channel(client, sessionkey, channel, []int64{pkg})
}
}
}
}
log.Printf("[INFO] Errata created: %d\n", created);
log.Printf("[INFO] Errata updated: %d\n", updated);
if !publish && created > 0 {
log.Println("[INFO] Errata have been created but NOT published!");
log.Println("[INFO] Please go to: Errata -> Manage Errata -> Unpublished to find them");
log.Println("[INFO] If you want to publish them please delete the unpublished Errata and run this script again");
log.Println("[INFO] with the --publish parameter");
}
os.Exit(0)
}
func init_session (client *xmlrpc.Client, username string, password string) string {
params := make([]interface{}, 2)
params[0] = username
params[1] = password
var sessionkey string
err := client.Call("auth.login", params, &sessionkey)
if err != nil {
return ""
}
return sessionkey
}
func close_session (client *xmlrpc.Client, sessionkey string) bool {
params := make([]interface{}, 1)
params[0] = sessionkey
err := client.Call("auth.logout", params, nil)
return err == nil
}
func user_is_admin (client *xmlrpc.Client, sessionkey string, username string) bool {
params := make([]interface{}, 2)
params[0] = sessionkey
params[1] = username
var roles []string
err := client.Call("user.list_roles", params, &roles)
if err != nil {
return false
}
for _, role := range roles {
if (role == "satellite_admin" || role == "org_admin" || role == "channel_admin") {
return true
}
}
return false
}
func get_channel_list (client *xmlrpc.Client, sessionkey string) []string {
params := make([]interface{}, 1)
params[0] = sessionkey
var channels []interface{}
err := client.Call("channel.list_all_channels", params, &channels)
var channelnames []string
if err != nil {
return channelnames
}
for _, channel := range channels {
if details, ok := channel.(map[string]interface{}); ok {
channelnames = append(channelnames, details["label"].(string))
}
}
return channelnames
}
func get_inventory (client *xmlrpc.Client, sessionkey string, channels []string) Inventory {
params := make([]interface{}, 2)
var inv Inventory
inv.filename2id = make(map[string]int64)
inv.id2channels = make(map[int64][]string)
inv.id2filename = make(map[int64]string)
for _, channel := range channels {
params[0] = sessionkey
params[1] = channel
var packages []interface{}
log.Printf("[INFO] Scanning channel %s\n", channel)
err := client.Call("channel.software.list_all_packages", params, &packages)
if err != nil {
return inv
}
for _, pkg := range packages {
if details, ok := pkg.(map[string]interface{}); ok {
id := details["id"].(int64)
filename, inchannels := get_package_details(client, sessionkey, id)
inv.filename2id[filename] = id
inv.id2channels[id] = inchannels
inv.id2filename[id] = filename
}
}
}
return inv
}
func get_existing_errata (client *xmlrpc.Client, sessionkey string, channels []string) map[string]bool {
params := make([]interface{}, 2)
params[0] = sessionkey
existing := make(map[string]bool)
type Response struct {
Id int64 `xmlrpc:"id"`
Date string `xmlrpc:"date"`
AdvisoryType string `xmlrpc:"advisory_type"`
AdvisoryName string `xmlrpc:"advisory_name"`
AdvisorySynopsis string `xmlrpc:"advisory_synopsis"`
Advisory string `xmlrpc:"advisory"`
IssueDate string `xmlrpc:"issue_date"`
UpdateDate string `xmlrpc:"update_date"`
Synopsis string `xmlrpc:"synopsis"`
LastModified string `xmlrpc:"last_modified_date"`
}
var response []Response
type Unpub struct {
Id int64 `xmlrpc:"id"`
Published int64 `xmlrpc:"published"`
Advisory string `xmlrpc:"advisory"`
AdvisoryName string `xmlrpc:"advisory_name"`
AdvisoryType string `xmlrpc:"advisory_type"`
Synopsis string `xmlrpc:"synopsis"`
Created time.Time `xmlrpc:"created"`
UpdateDate time.Time `xmlrpc:"update_date"`
}
var unpub []Unpub
log.Println("[INFO] Fetching unpublished errata")
err := client.Call("errata.list_unpublished_errata", params, &unpub)
if err != nil {
return existing
}
for _, errata := range unpub {
existing[(errata.AdvisoryName)] = true
}
for _, channel := range channels {
params[1] = channel
log.Printf("[INFO] Fetching existing errata for channel %s\n", channel)
err := client.Call("channel.software.list_errata", params, &response)
if err != nil {
return existing
}
for _, errata := range response {
existing[(errata.AdvisoryName)] = true
}
}
return existing
}
func get_package_details (client *xmlrpc.Client, sessionkey string, id int64) (string, []string) {
params := make([]interface{}, 2)
params[0] = sessionkey
params[1] = id
var details interface{}
var inchannels []string
err := client.Call("packages.get_details", params, &details)
if err != nil {
return "", []string{}
}
if detail, ok := details.(map[string]interface{}); ok {
for _, provchan := range detail["providing_channels"].([]interface{}) {
inchannels = append(inchannels, provchan.(string))
}
return detail["file"].(string), inchannels
}
return "", []string{}
}
func ParseErrata (data []byte) Raw {
var allerrata Raw
err := json.Unmarshal(data, &allerrata)
if err != nil {
log.Printf("[ERROR] Parsing JSON data failed: %v\n", err.Error())
os.Exit(5)
}
return allerrata
}
func ParseOval (data []byte) map[string]OvalData {
// OvalDefinitions was generated 2019-04-24 22:06:30 by root on localhost.localdomain.
type OvalDefinitions struct {
XMLName xml.Name `xml:"oval_definitions"`
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
Oval string `xml:"oval,attr"`
RedDef string `xml:"red-def,attr"`
UnixDef string `xml:"unix-def,attr"`
Xsi string `xml:"xsi,attr"`
SchemaLocation string `xml:"schemaLocation,attr"`
Definitions struct {
// Text string `xml:",chardata"`
Definition []struct {
// Text string `xml:",chardata"`
// Class string `xml:"class,attr"`
ID string `xml:"id,attr"`
// Version string `xml:"version,attr"`
Metadata struct {
// Text string `xml:",chardata"`
// Title string `xml:"title"`
// Affected struct {
// Text string `xml:",chardata"`
// Family string `xml:"family,attr"`
// Platform []string `xml:"platform"`
// } `xml:"affected"`
Reference []struct {
Text string `xml:",chardata"`
RefID string `xml:"ref_id,attr"`
// RefURL string `xml:"ref_url,attr"`
// Source string `xml:"source,attr"`
} `xml:"reference"`
Description string `xml:"description"`
Advisory struct {
Text string `xml:",chardata"`
// From string `xml:"from,attr"`
// Severity string `xml:"severity"`
Rights string `xml:"rights"`
// Issued struct {
// Text string `xml:",chardata"`
// Date string `xml:"date,attr"`
// } `xml:"issued"`
// Updated struct {
// Text string `xml:",chardata"`
// Date string `xml:"date,attr"`
// } `xml:"updated"`
Cve []struct {
Text string `xml:",chardata"`
Href string `xml:"href,attr"`
Public string `xml:"public,attr"`
Impact string `xml:"impact,attr"`
Cwe string `xml:"cwe,attr"`
Cvss2 string `xml:"cvss2,attr"`
Cvss3 string `xml:"cvss3,attr"`
} `xml:"cve"`
Bugzilla []struct {
Text string `xml:",chardata" xmlrpc:"summary"`
Href string `xml:"href,attr" xmlrpc:"url"`
ID int64 `xml:"id,attr" xmlrpc:"id"`
} `xml:"bugzilla"`
} `xml:"advisory"`
} `xml:"metadata"`
} `xml:"definition"`
} `xml:"definitions"`
}
var ovaldata OvalDefinitions
_ = xml.Unmarshal([]byte(data), &ovaldata)
oval := make(map[string]OvalData)
for _, def := range ovaldata.Definitions.Definition {
id := def.ID
id = "CESA-" + id[len(id)-8:len(id)-4] + ":" + id[len(id)-4:]
var cves []string
var bugs []Bugzilla
cvere, _ := regexp.Compile(`^CVE`)
for _, ref := range def.Metadata.Reference {
if cvere.MatchString(ref.RefID) {
cves = append(cves, ref.RefID)
}
}
for _, bug := range def.Metadata.Advisory.Bugzilla {
bugs = append(bugs, bug)
}
var current = oval[id]
current.Description = def.Metadata.Description
current.Rights = def.Metadata.Advisory.Rights
current.References = cves
current.Bugs = bugs
oval[id] = current
}
return oval
}
func get_packages_for_errata (errata Erratum, inv Inventory) []int64 {
var pkglist []int64
for _, rpm := range errata.Packages {
if pkgid, ok := inv.filename2id[rpm]; ok {
pkglist = append(pkglist, pkgid)
}
}
return pkglist
}
func create_errata (client *xmlrpc.Client, sessionkey string, info SWerrata, bugs []Bugzilla, keywords []string, pkglist []int64, publish bool, channels []string) bool {
params := make([]interface{}, 7)
params[0] = sessionkey
params[1] = info
params[2] = bugs
params[3] = keywords
params[4] = pkglist
params[5] = publish
params[6] = channels
type Response struct {
Id int64 `xmlrpc:"id"`
Date string `xmlrpc:"date"`
Advisory_Type string `xmlrpc:"advisory_type"`
Advisory_Name string `xmlrpc:"advisory_name"`
Advisory_Synopsis string `xmlrpc:"advisory_synopsis"`
}
var response Response
err := client.Call("errata.create", params, &response)
if err == nil && response.Id > 0 {
return true
}
return false
}
func check_api_support (version string, supported []float64) bool {
for _, i := range supported {
if version == float_to_string(i) {
return true
}
}
return false
}
func include_channels (channels []string, include *[]string) []string {
var result []string
if len(*include) == 0 {
return channels
}
for _, channel := range channels {
var included bool = false
for _, inc := range *include {
if channel == inc {
included = true
}
}
if included {
result = append(result, channel)
}
}
return result
}
func exclude_channels (channels []string, exclude *[]string) []string {
var result []string
for _, channel := range channels {
var excluded bool = false
for _, exc := range *exclude {
if channel == exc {
excluded = true
}
}
if !excluded {
result = append(result, channel)
}
}
return result
}
func float_to_string (input float64) string {
if input == float64(int64(input)) {
return fmt.Sprintf("%.0f", input)
}
return fmt.Sprintf("%.2f", input)
}
func string_to_float (input string) float64 {
result, err := strconv.ParseFloat(input, 64)
if err == nil {
return result
} else {
return 0
}
}
func add_issue_date (client *xmlrpc.Client, sessionkey string, errata string, issuedate time.Time) bool {
type Details struct {
IssueDate time.Time `xmlrpc:"issue_date"`
UpdateDate time.Time `xmlrpc:"update_date"`
}
var details Details
details.IssueDate = issuedate
details.UpdateDate = issuedate
params := make([]interface{}, 3)
params[0] = sessionkey
params[1] = errata
params[2] = details
var response int64
err := client.Call("errata.set_details", params, &response)
if err == nil && response > 0 {
return true
}
return false
}
func add_severity (client *xmlrpc.Client, sessionkey string, errata string, severity string) bool {
type Details struct {
Severity string `xmlrpc:"severity"`
}
var details Details
details.Severity = severity
params := make([]interface{}, 3)
params[0] = sessionkey
params[1] = errata
params[2] = details
var response int64
err := client.Call("errata.set_details", params, &response)
if err == nil && response > 0 {
return true
}
return false
}
func get_channels_of_packages (pkglist []int64, inv Inventory) []string {
labels := make(map[string]bool)
var result []string
for _, pkg := range pkglist {
for _, channel := range inv.id2channels[pkg] {
labels[channel] = true
}
}
for key := range labels {
result = append(result, key)
}
return result
}
func publish_errata (client *xmlrpc.Client, sessionkey string, errata string, channels []string) bool {
params := make([]interface{}, 3)
params[0] = sessionkey
params[1] = errata
params[2] = channels
type Response struct {
Id int `xmlrpc:"id"`
Date string `xmlrpc:"date"`
AdvisoryType string `xmlrpc:"advisory_type"`
AdvisoryName string `xmlrpc:"advisory_name"`
AdvisorySynopsis string `xmlrpc:"advisory_synopsis"`
}
var response Response
err := client.Call("errata.publish", params, &response)
return err == nil
}
func add_cve_to_errata (client *xmlrpc.Client, sessionkey string, errata SWerrata, cves []string) bool {
if cves == nil {
// called without CVE information, so we bail nicely
return true
}
type SWerrata2 struct {
Synopsis string `xmlrpc:"synopsis"`
AdvisoryName string `xmlrpc:"advisory_name"`
AdvisoryRelease int `xmlrpc:"advisory_release"`
AdvisoryType string `xmlrpc:"advisory_type"`
From string `xmlrpc:"errataFrom"`
Product string `xmlrpc:"product"`
Topic string `xmlrpc:"topic"`
Description string `xmlrpc:"description"`
References string `xmlrpc:"references"`