Completed
Push — master ( 76037c...7585f9 )
by kota
09:11 queued 01:58
created

gost.Base.isFetchViaHTTP   A

Complexity

Conditions 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
nop 0
1
/* Vuls - Vulnerability Scanner
2
Copyright (C) 2016  Future Architect, Inc. 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 gost
19
20
import (
21
	"fmt"
22
	"net/http"
23
	"strings"
24
25
	cnf "github.com/future-architect/vuls/config"
26
	"github.com/future-architect/vuls/models"
27
	"github.com/knqyf263/gost/db"
28
	"github.com/parnurzeal/gorequest"
29
)
30
31
// Client is the interface of OVAL client.
32
type Client interface {
33
	FillWithGost(db.DB, *models.ScanResult) (int, error)
34
35
	//TODO implement
36
	// CheckHTTPHealth() error
37
	// CheckIfGostFetched checks if Gost entries are fetched
38
	// CheckIfGostFetched(db.DB, string, string) (bool, error)
39
	// CheckIfGostFresh(db.DB, string, string) (bool, error)
40
}
41
42
// NewClient make Client by family
43
func NewClient(family string) Client {
44
	switch family {
45
	case cnf.RedHat, cnf.CentOS:
46
		return RedHat{}
47
	case cnf.Debian:
48
		return Debian{}
49
	case cnf.Windows:
50
		return Microsoft{}
51
	default:
52
		return Pseudo{}
53
	}
54
}
55
56
// Base is a base struct
57
type Base struct {
58
	family string
59
}
60
61
// CheckHTTPHealth do health check
62
func (b Base) CheckHTTPHealth() error {
63
	if !cnf.Conf.Gost.IsFetchViaHTTP() {
64
		return nil
65
	}
66
67
	url := fmt.Sprintf("%s/health", cnf.Conf.Gost.URL)
68
	var errs []error
69
	var resp *http.Response
70
	resp, _, errs = gorequest.New().Get(url).End()
71
	//  resp, _, errs = gorequest.New().SetDebug(config.Conf.Debug).Get(url).End()
72
	//  resp, _, errs = gorequest.New().Proxy(api.httpProxy).Get(url).End()
73
	if 0 < len(errs) || resp == nil || resp.StatusCode != 200 {
74
		return fmt.Errorf("Failed to connect to gost server. url: %s, errs: %v",
75
			url, errs)
76
	}
77
	return nil
78
}
79
80
// CheckIfGostFetched checks if oval entries are in DB by family, release.
81
func (b Base) CheckIfGostFetched(driver db.DB, osFamily string) (fetched bool, err error) {
82
	//TODO
83
	return true, nil
84
}
85
86
// CheckIfGostFresh checks if oval entries are fresh enough
87
func (b Base) CheckIfGostFresh(driver db.DB, osFamily string) (ok bool, err error) {
88
	//TODO
89
	return true, nil
90
}
91
92
// Pseudo is Gost client except for RedHat family and Debian
93
type Pseudo struct {
94
	Base
95
}
96
97
// FillWithGost fills cve information that has in Gost
98
func (pse Pseudo) FillWithGost(driver db.DB, r *models.ScanResult) (int, error) {
99
	return 0, nil
100
}
101
102
func major(osVer string) (majorVersion string) {
103
	return strings.Split(osVer, ".")[0]
104
}
105