-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
syschar.go
86 lines (75 loc) · 2.7 KB
/
syschar.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
// 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.
// Package scap implements some internal parsing of the OpenSCAP analysis
// results.
package scap
import "fmt"
// SystemCharacteristics contains the internal charasteristics of an OVAL
// evaluation.
type SystemCharacteristics struct {
SystemInfo SystemInfo `json:"system_info"`
Objects []Object `json:"objects,omitempty"`
}
// SystemInfo is an internal field of SystemCharacteristics contains the
// global system informations.
type SystemInfo struct {
OsName string `json:"os_name"`
OsVersion string `json:"os_version"`
Architecture string `json:"architecture"`
PrimaryHostName string `json:"primary_host_name"`
}
// Object contains the informations of a specific OVAL Object.
type Object struct {
ID string `json:"id"`
Items []Item `json:"items,omitempty"`
}
// Item contains the messages of an OVAL Object's fields.
type Item struct {
ID string `json:"id"`
Messages map[string]string `json:"messages"`
}
// SysChar returns the refined system characteristics from a Document.
func SysChar(doc *Document) (*SystemCharacteristics, error) {
if doc.OvalSystemCharacteristics == nil {
return nil, fmt.Errorf("OvalSystemCharacteristics is nil")
}
systemCharacteristics := SystemCharacteristics{}
systemInfo := SystemInfo{
OsName: doc.OvalSystemCharacteristics.SystemInfo.OsName,
OsVersion: doc.OvalSystemCharacteristics.SystemInfo.OsVersion,
Architecture: doc.OvalSystemCharacteristics.SystemInfo.Architecture,
PrimaryHostName: doc.OvalSystemCharacteristics.SystemInfo.PrimaryHostName,
}
systemCharacteristics.SystemInfo = systemInfo
if doc.OvalSystemCharacteristics.CollectedObjects != nil {
for _, object := range doc.OvalSystemCharacteristics.CollectedObjects.Object {
o := Object{
ID: string(object.Id),
}
if object.Flag != "complete" {
continue
}
if doc.OvalSystemCharacteristics.SystemData != nil {
for _, reference := range object.Reference {
for _, item := range doc.OvalSystemCharacteristics.SystemData.Item {
if reference.ItemRef != item.Id {
continue
}
i := Item{
ID: item.XMLName.Local,
Messages: make(map[string]string, len(item.Message)),
}
for _, message := range item.Message {
i.Messages[message.XMLName.Local] = message.Text
}
o.Items = append(o.Items, i)
}
}
}
systemCharacteristics.Objects = append(systemCharacteristics.Objects, o)
}
}
return &systemCharacteristics, nil
}