-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
document.go
104 lines (97 loc) · 3.63 KB
/
document.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// The code that follows has been copied from github.com/gocomply/scap,
// which was originally released under the CC0 1.0 Universal license.
package scap
import (
"encoding/xml"
"fmt"
"io"
"github.com/gocomply/scap/pkg/scap/constants"
"github.com/gocomply/scap/pkg/scap/models/cdf"
"github.com/gocomply/scap/pkg/scap/models/cpe_dict"
"github.com/gocomply/scap/pkg/scap/models/ds"
"github.com/gocomply/scap/pkg/scap/models/inter"
"github.com/gocomply/scap/pkg/scap/models/oval_def"
"github.com/gocomply/scap/pkg/scap/models/oval_res"
"github.com/gocomply/scap/pkg/scap/models/oval_sc"
)
const (
xccdfBenchmarkElement = "Benchmark"
cpeCpeListElement = "cpe-list"
ovalDefinitionsElement = "oval_definitions"
ovalResultsElement = "oval_results"
ovalSystemCharacteristicsElement = "oval_system_characteristics"
dsDataStreamCollectionElement = "data-stream-collection"
ocilOcilElement = "ocil"
)
// Document contains all the returned informations of an openscap evaluation.
type Document struct {
Type constants.DocumentType `json:"-"`
*cdf.Benchmark
*cpe_dict.CpeList
*oval_def.OvalDefinitions
*oval_res.OvalResults
*oval_sc.OvalSystemCharacteristics
*ds.DataStreamCollection
*inter.Ocil
}
// ReadDocument takes an io.Reader and return a Document or error if failed.
func ReadDocument(r io.Reader) (*Document, error) {
d := xml.NewDecoder(r)
for {
token, err := d.Token()
if err != nil || token == nil {
return nil, fmt.Errorf("Could not decode XML: %v", err)
}
switch startElement := token.(type) {
case xml.StartElement:
switch startElement.Name.Local {
case dsDataStreamCollectionElement:
var sds ds.DataStreamCollection
if err := d.DecodeElement(&sds, &startElement); err != nil {
return nil, err
}
return &Document{DataStreamCollection: &sds, Type: constants.DocumentTypeSourceDataStream}, nil
case ovalDefinitionsElement:
var ovalDefs oval_def.OvalDefinitions
if err := d.DecodeElement(&ovalDefs, &startElement); err != nil {
return nil, err
}
return &Document{OvalDefinitions: &ovalDefs, Type: constants.DocumentTypeOvalDefinitions}, nil
case ovalSystemCharacteristicsElement:
var ovalSyschar oval_sc.OvalSystemCharacteristics
if err := d.DecodeElement(&ovalSyschar, &startElement); err != nil {
return nil, err
}
return &Document{OvalSystemCharacteristics: &ovalSyschar, Type: constants.DocumentTypeOvalSyschar}, nil
case ovalResultsElement:
var ovalRes oval_res.OvalResults
if err := d.DecodeElement(&ovalRes, &startElement); err != nil {
return nil, err
}
return &Document{OvalResults: &ovalRes, Type: constants.DocumentTypeOvalResults}, nil
case xccdfBenchmarkElement:
var bench cdf.Benchmark
if err := d.DecodeElement(&bench, &startElement); err != nil {
return nil, err
}
return &Document{Benchmark: &bench, Type: constants.DocumentTypeXccdfBenchmark}, nil
case cpeCpeListElement:
var cpeList cpe_dict.CpeList
if err := d.DecodeElement(&cpeList, &startElement); err != nil {
return nil, err
}
return &Document{CpeList: &cpeList, Type: constants.DocumentTypeCpeDict}, nil
case ocilOcilElement:
var ocil inter.Ocil
if err := d.DecodeElement(&ocil, &startElement); err != nil {
return nil, err
}
return &Document{Ocil: &ocil, Type: constants.DocumentTypeOcil}, nil
}
}
}
}