-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontents.go
100 lines (83 loc) · 2.48 KB
/
contents.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
package main
import (
"io/ioutil"
"os"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go/service/ec2"
)
const (
START = "### Generate by ec2-vuls-config ###"
END = "### ec2-vuls-config end ###"
)
func GenerateServerSection(instances []*ec2.Instance) []byte {
b := make([]byte, 0, 1024)
b = append(b, START+"\n"...)
b = append(b, "# Updated "+time.Now().Format(time.RFC3339)+"\n\n"...)
for _, instance := range instances {
if name := GetTagValue(instance, "Name"); name != nil {
b = append(b, "[servers."+*name+"]\n"...)
} else {
continue
}
b = append(b, "host = \""+*instance.PrivateIpAddress+"\"\n"...)
if port := GetTagValue(instance, "vuls:port"); port != nil {
b = append(b, "port = \""+*port+"\"\n"...)
}
if user := GetTagValue(instance, "vuls:user"); user != nil {
b = append(b, "user = \""+*user+"\"\n"...)
}
if keyPath := GetTagValue(instance, "vuls:keyPath"); keyPath != nil {
b = append(b, "keyPath = \""+*keyPath+"\"\n"...)
}
if cpeNames := GetTagValue(instance, "vuls:cpeNames"); cpeNames != nil {
b = append(b, "cpeNames = [\n"...)
for _, cpeName := range strings.Split(*cpeNames, ",") {
b = append(b, "\""+cpeName+"\",\n"...)
}
b = append(b, "]\n"...)
}
if ignoreCves := GetTagValue(instance, "vuls:ignoreCves"); ignoreCves != nil {
b = append(b, "ignoreCves = [\n"...)
for _, ignoreCve := range strings.Split(*ignoreCves, ",") {
b = append(b, "\""+ignoreCve+"\",\n"...)
}
b = append(b, "]\n"...)
}
b = append(b, "\n"...)
}
b = append(b, END...)
return b
}
func MergeConfig(currentConfig, newConfig []byte) []byte {
// If it has already been created, it is rewritten.
re := regexp.MustCompile("(?m)" + START + "[\\s\\S]*?" + END)
if re.Match(currentConfig) {
return re.ReplaceAll(currentConfig, newConfig)
}
// If it finds servers section, it is appended.
re = regexp.MustCompile("(?m)\\[servers.*\\][\\s\\S]*")
if re.Match(currentConfig) {
currentConfig = append(currentConfig, newConfig...)
return currentConfig
}
// In the case that it doesn't finds servers section.
currentConfig = append(currentConfig, []byte("[servers]\n")...)
currentConfig = append(currentConfig, newConfig...)
return currentConfig
}
func LoadFile(path string) ([]byte, error) {
bs, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return bs, nil
}
func WriteFile(path string, content []byte) error {
err := ioutil.WriteFile(path, content, os.ModePerm)
if err != nil {
return err
}
return nil
}