Test Setup Failed
Pull Request — master (#807)
by
unknown
07:07
created

config.IsValidStaticContainerConf   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
nop 1
1
/* Vuls - Vulnerability Scanner
2
Copyright (C) 2016  Future Corporation , Japan.
3
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
package config
19
20
import (
21
	"os"
22
	"regexp"
23
	"strings"
24
25
	"github.com/BurntSushi/toml"
26
	"github.com/knqyf263/go-cpe/naming"
27
	"golang.org/x/xerrors"
28
)
29
30
// TOMLLoader loads config
31
type TOMLLoader struct {
32
}
33
34
// Load load the configuration TOML file specified by path arg.
35
func (c TOMLLoader) Load(pathToToml, keyPass string) error {
36
	var conf Config
37
	if _, err := toml.DecodeFile(pathToToml, &conf); err != nil {
38
		return err
39
	}
40
	Conf.EMail = conf.EMail
41
	Conf.Slack = conf.Slack
42
	Conf.Stride = conf.Stride
43
	Conf.HipChat = conf.HipChat
44
	Conf.ChatWork = conf.ChatWork
45
	Conf.Telegram = conf.Telegram
46
	Conf.Saas = conf.Saas
47
	Conf.Syslog = conf.Syslog
48
	Conf.HTTP = conf.HTTP
49
	Conf.AWS = conf.AWS
50
	Conf.Azure = conf.Azure
51
52
	Conf.CveDict = conf.CveDict
53
	Conf.OvalDict = conf.OvalDict
54
	Conf.Gost = conf.Gost
55
	Conf.Exploit = conf.Exploit
56
57
	d := conf.Default
58
	Conf.Default = d
59
	servers := make(map[string]ServerInfo)
60
61
	if keyPass != "" {
62
		d.KeyPassword = keyPass
63
	}
64
65
	i := 0
66
	for serverName, v := range conf.Servers {
67
		if 0 < len(v.KeyPassword) {
68
			return xerrors.Errorf("[Deprecated] KEYPASSWORD IN CONFIG FILE ARE UNSECURE. REMOVE THEM IMMEDIATELY FOR A SECURITY REASONS. THEY WILL BE REMOVED IN A FUTURE RELEASE: %s", serverName)
69
		}
70
71
		s := ServerInfo{ServerName: serverName}
72
73
		if v.Type == ServerTypeStaticContainer {
74
			if err := IsValidStaticContainerConf(v.StaticContainer); err != nil {
75
				return err
76
			}
77
			s.StaticContainer = v.StaticContainer
78
		}
79
80
		if v.Type != ServerTypePseudo && v.Type != ServerTypeStaticContainer {
81
			s.Host = v.Host
82
			if len(s.Host) == 0 {
83
				return xerrors.Errorf("%s is invalid. host is empty", serverName)
84
			}
85
86
			switch {
87
			case v.Port != "":
88
				s.Port = v.Port
89
			case d.Port != "":
90
				s.Port = d.Port
91
			default:
92
				s.Port = "22"
93
			}
94
95
			switch {
96
			case v.User != "":
97
				s.User = v.User
98
			case d.User != "":
99
				s.User = d.User
100
			default:
101
				if s.Port != "local" {
102
					return xerrors.Errorf("%s is invalid. User is empty", serverName)
103
				}
104
			}
105
106
			s.KeyPath = v.KeyPath
107
			if len(s.KeyPath) == 0 {
108
				s.KeyPath = d.KeyPath
109
			}
110
			if s.KeyPath != "" {
111
				if _, err := os.Stat(s.KeyPath); err != nil {
112
					return xerrors.Errorf(
113
						"%s is invalid. keypath: %s not exists", serverName, s.KeyPath)
114
				}
115
			}
116
117
			s.KeyPassword = v.KeyPassword
118
			if len(s.KeyPassword) == 0 {
119
				s.KeyPassword = d.KeyPassword
120
			}
121
		}
122
123
		s.ScanMode = v.ScanMode
124
		if len(s.ScanMode) == 0 {
125
			s.ScanMode = d.ScanMode
126
			if len(s.ScanMode) == 0 {
127
				s.ScanMode = []string{"fast"}
128
			}
129
		}
130
		for _, m := range s.ScanMode {
131
			switch m {
132
			case "fast":
133
				s.Mode.Set(Fast)
134
			case "fast-root":
135
				s.Mode.Set(FastRoot)
136
			case "deep":
137
				s.Mode.Set(Deep)
138
			case "offline":
139
				s.Mode.Set(Offline)
140
			default:
141
				return xerrors.Errorf("scanMode: %s of %s is invalie. Specify -fast, -fast-root, -deep or offline", m, serverName)
142
			}
143
		}
144
		if err := s.Mode.validate(); err != nil {
145
			return xerrors.Errorf("%s in %s", err, serverName)
146
		}
147
148
		s.CpeNames = v.CpeNames
149
		if len(s.CpeNames) == 0 {
150
			s.CpeNames = d.CpeNames
151
		}
152
153
		for i, n := range s.CpeNames {
154
			uri, err := toCpeURI(n)
155
			if err != nil {
156
				return xerrors.Errorf("Failed to parse CPENames %s in %s, err: %w", n, serverName, err)
0 ignored issues
show
introduced by
unrecognized printf verb 'w'
Loading history...
157
			}
158
			s.CpeNames[i] = uri
159
		}
160
161
		s.ContainersIncluded = v.ContainersIncluded
162
		if len(s.ContainersIncluded) == 0 {
163
			s.ContainersIncluded = d.ContainersIncluded
164
		}
165
166
		s.ContainersExcluded = v.ContainersExcluded
167
		if len(s.ContainersExcluded) == 0 {
168
			s.ContainersExcluded = d.ContainersExcluded
169
		}
170
171
		s.ContainerType = v.ContainerType
172
		if len(s.ContainerType) == 0 {
173
			s.ContainerType = d.ContainerType
174
		}
175
176
		s.Containers = v.Containers
177
		for contName, cont := range s.Containers {
178
			cont.IgnoreCves = append(cont.IgnoreCves, d.IgnoreCves...)
179
			s.Containers[contName] = cont
180
		}
181
182
		if len(v.DependencyCheckXMLPath) != 0 || len(d.DependencyCheckXMLPath) != 0 {
183
			return xerrors.Errorf("[DEPRECATED] dependencyCheckXMLPath IS DEPRECATED. USE owaspDCXMLPath INSTEAD: %s", serverName)
184
		}
185
186
		s.OwaspDCXMLPath = v.OwaspDCXMLPath
187
		if len(s.OwaspDCXMLPath) == 0 {
188
			s.OwaspDCXMLPath = d.OwaspDCXMLPath
189
		}
190
191
		s.Memo = v.Memo
192
		if s.Memo == "" {
193
			s.Memo = d.Memo
194
		}
195
196
		s.IgnoreCves = v.IgnoreCves
197
		for _, cve := range d.IgnoreCves {
198
			found := false
199
			for _, c := range s.IgnoreCves {
200
				if cve == c {
201
					found = true
202
					break
203
				}
204
			}
205
			if !found {
206
				s.IgnoreCves = append(s.IgnoreCves, cve)
207
			}
208
		}
209
210
		s.IgnorePkgsRegexp = v.IgnorePkgsRegexp
211
		for _, pkg := range d.IgnorePkgsRegexp {
212
			found := false
213
			for _, p := range s.IgnorePkgsRegexp {
214
				if pkg == p {
215
					found = true
216
					break
217
				}
218
			}
219
			if !found {
220
				s.IgnorePkgsRegexp = append(s.IgnorePkgsRegexp, pkg)
221
			}
222
		}
223
		for _, reg := range s.IgnorePkgsRegexp {
224
			_, err := regexp.Compile(reg)
225
			if err != nil {
226
				return xerrors.Errorf("Faild to parse %s in %s. err: %w", reg, serverName, err)
0 ignored issues
show
introduced by
unrecognized printf verb 'w'
Loading history...
227
			}
228
		}
229
		for contName, cont := range s.Containers {
230
			for _, reg := range cont.IgnorePkgsRegexp {
231
				_, err := regexp.Compile(reg)
232
				if err != nil {
233
					return xerrors.Errorf("Faild to parse %s in %s@%s. err: %w",
0 ignored issues
show
introduced by
unrecognized printf verb 'w'
Loading history...
234
						reg, contName, serverName, err)
235
				}
236
			}
237
		}
238
239
		opt := map[string]interface{}{}
240
		for k, v := range d.Optional {
241
			opt[k] = v
242
		}
243
		for k, v := range v.Optional {
244
			opt[k] = v
245
		}
246
		s.Optional = opt
247
248
		s.Enablerepo = v.Enablerepo
249
		if len(s.Enablerepo) == 0 {
250
			s.Enablerepo = d.Enablerepo
251
		}
252
		if len(s.Enablerepo) != 0 {
253
			for _, repo := range s.Enablerepo {
254
				switch repo {
255
				case "base", "updates":
256
					// nop
257
				default:
258
					return xerrors.Errorf(
259
						"For now, enablerepo have to be base or updates: %s, servername: %s",
260
						s.Enablerepo, serverName)
261
				}
262
			}
263
		}
264
265
		s.GitHubRepos = v.GitHubRepos
266
		for ownerRepo, githubSetting := range s.GitHubRepos {
267
			if ss := strings.Split(ownerRepo, "/"); len(ss) != 2 {
268
				return xerrors.Errorf("Failed to parse GitHub owner/repo: %s in %s",
269
					ownerRepo, serverName)
270
			}
271
			if githubSetting.Token == "" {
272
				return xerrors.Errorf("GitHub owner/repo: %s in %s token is empty",
273
					ownerRepo, serverName)
274
			}
275
		}
276
277
		s.UUIDs = v.UUIDs
278
		s.Type = v.Type
279
280
		s.WordPress.WPVulnDBToken = v.WordPress.WPVulnDBToken
281
		s.WordPress.CmdPath = v.WordPress.CmdPath
282
		s.WordPress.DocRoot = v.WordPress.DocRoot
283
		s.WordPress.OSUser = v.WordPress.OSUser
284
		s.WordPress.IgnoreInactive = v.WordPress.IgnoreInactive
285
286
		s.LogMsgAnsiColor = Colors[i%len(Colors)]
287
		i++
288
289
		servers[serverName] = s
290
	}
291
	Conf.Servers = servers
292
	return nil
293
}
294
295
func toCpeURI(cpename string) (string, error) {
296
	if strings.HasPrefix(cpename, "cpe:2.3:") {
297
		wfn, err := naming.UnbindFS(cpename)
298
		if err != nil {
299
			return "", err
300
		}
301
		return naming.BindToURI(wfn), nil
302
	} else if strings.HasPrefix(cpename, "cpe:/") {
303
		wfn, err := naming.UnbindURI(cpename)
304
		if err != nil {
305
			return "", err
306
		}
307
		return naming.BindToURI(wfn), nil
308
	}
309
	return "", xerrors.Errorf("Unknow CPE format: %s", cpename)
310
}
311
312
// IsValidStaticContainerConf checks a container configuration
313
func IsValidStaticContainerConf(c StaticContainerConf) error {
314
	if c.Name == "" {
315
		return xerrors.New("Invalid arguments : set staticContainer name")
316
	}
317
	if c.Tag == "" {
318
		return xerrors.New("Invalid arguments : set staticContainer tag")
319
	}
320
	return nil
321
}
322