/
config_v1.go
142 lines (125 loc) · 3.68 KB
/
config_v1.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
package config
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/BurntSushi/toml"
"golang.org/x/xerrors"
)
// ConfV1 has old version Configuration for windows
var ConfV1 V1
// V1 is Struct of Configuration
type V1 struct {
Version string
Servers map[string]Server
Proxy ProxyConfig
}
// Server is Configuration of the server to be scanned.
type Server struct {
Host string
UUID string
WinUpdateSrc string
WinUpdateSrcInt int `json:"-" toml:"-"` // for internal used (not specified in config.toml)
CabPath string
IgnoredJSONKeys []string
}
// WinUpdateSrcVulsDefault is default value of WinUpdateSrc
const WinUpdateSrcVulsDefault = 2
// Windows const
const (
SystemDefault = 0
WSUS = 1
WinUpdateDirect = 2
LocalCab = 3
)
// ProxyConfig is struct of Proxy configuration
type ProxyConfig struct {
ProxyURL string
BypassList string
}
// Path of saas-credential.json
var pathToSaasJSON = "./saas-credential.json"
var vulsAuthURL = "https://auth.vuls.biz/one-time-auth"
func convertToLatestConfig(pathToToml string) error {
var convertedServerConfigList = make(map[string]ServerInfo)
for _, server := range ConfV1.Servers {
switch server.WinUpdateSrc {
case "":
server.WinUpdateSrcInt = WinUpdateSrcVulsDefault
case "0":
server.WinUpdateSrcInt = SystemDefault
case "1":
server.WinUpdateSrcInt = WSUS
case "2":
server.WinUpdateSrcInt = WinUpdateDirect
case "3":
server.WinUpdateSrcInt = LocalCab
if server.CabPath == "" {
return xerrors.Errorf("Failed to load CabPath. err: CabPath is empty")
}
default:
return xerrors.Errorf(`Specify WindUpdateSrc in "0"|"1"|"2"|"3"`)
}
convertedServerConfig := ServerInfo{
Host: server.Host,
Port: "local",
UUIDs: map[string]string{server.Host: server.UUID},
IgnoredJSONKeys: server.IgnoredJSONKeys,
Windows: &WindowsConf{
CabPath: server.CabPath,
ServerSelection: server.WinUpdateSrcInt,
},
}
convertedServerConfigList[server.Host] = convertedServerConfig
}
Conf.Servers = convertedServerConfigList
raw, err := os.ReadFile(pathToSaasJSON)
if err != nil {
return xerrors.Errorf("Failed to read saas-credential.json. err: %w", err)
}
saasJSON := SaasConf{}
if err := json.Unmarshal(raw, &saasJSON); err != nil {
return xerrors.Errorf("Failed to unmarshal saas-credential.json. err: %w", err)
}
Conf.Saas = SaasConf{
GroupID: saasJSON.GroupID,
Token: saasJSON.Token,
URL: vulsAuthURL,
}
c := struct {
Version string `toml:"version"`
Saas *SaasConf `toml:"saas"`
Default ServerInfo `toml:"default"`
Servers map[string]ServerInfo `toml:"servers"`
}{
Version: "v2",
Saas: &Conf.Saas,
Default: Conf.Default,
Servers: Conf.Servers,
}
// rename the current config.toml to config.toml.bak
info, err := os.Lstat(pathToToml)
if err != nil {
return xerrors.Errorf("Failed to lstat %s: %w", pathToToml, err)
}
realPath := pathToToml
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
if realPath, err = os.Readlink(pathToToml); err != nil {
return xerrors.Errorf("Failed to Read link %s: %w", pathToToml, err)
}
}
if err := os.Rename(realPath, realPath+".bak"); err != nil {
return xerrors.Errorf("Failed to rename %s: %w", pathToToml, err)
}
var buf bytes.Buffer
if err := toml.NewEncoder(&buf).Encode(c); err != nil {
return xerrors.Errorf("Failed to encode to toml: %w", err)
}
str := strings.Replace(buf.String(), "\n [", "\n\n [", -1)
str = fmt.Sprintf("%s\n\n%s",
"# See README for details: https://vuls.io/docs/en/usage-settings.html",
str)
return os.WriteFile(realPath, []byte(str), 0600)
}