/
loadCis.go
64 lines (54 loc) · 1.64 KB
/
loadCis.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
package main
import (
"encoding/json"
"errors"
"io"
"github.com/Symantec/Dominator/lib/log"
"github.com/Symantec/Dominator/lib/mdb"
)
func newCisGenerator(args []string, logger log.DebugLogger) (generator, error) {
return sourceGenerator{loadCis, args[0]}, nil
}
func loadCis(reader io.Reader, datacentre string, logger log.Logger) (
*mdb.Mdb, error) {
type instanceMetadataType struct {
RequiredImage string `json:"required_image"`
PlannedImage string `json:"planned_image"`
DisableUpdates bool `json:"disable_updates"`
OwnerGroup string `json:"owner_group"`
}
type sourceType struct {
HostName string `json:"host_name"`
InstanceMetadata instanceMetadataType `json:"instance_metadata"`
Fqdn string
}
type hitType struct {
Source sourceType `json:"_source"`
}
type hitListType struct {
Hits []hitType
}
type inMdbType struct {
Hits hitListType
}
var inMdb inMdbType
var outMdb mdb.Mdb
decoder := json.NewDecoder(reader)
if err := decoder.Decode(&inMdb); err != nil {
return nil, errors.New("Error decoding: " + err.Error())
}
for _, hit := range inMdb.Hits.Hits {
var outMachine mdb.Machine
if hit.Source.Fqdn != "" {
outMachine.Hostname = hit.Source.Fqdn
} else {
outMachine.Hostname = hit.Source.HostName
}
outMachine.RequiredImage = hit.Source.InstanceMetadata.RequiredImage
outMachine.PlannedImage = hit.Source.InstanceMetadata.PlannedImage
outMachine.DisableUpdates = hit.Source.InstanceMetadata.DisableUpdates
outMachine.OwnerGroup = hit.Source.InstanceMetadata.OwnerGroup
outMdb.Machines = append(outMdb.Machines, outMachine)
}
return &outMdb, nil
}