-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
119 lines (102 loc) · 3.66 KB
/
init.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
package featuretest
import (
"fmt"
"sync"
"time"
"github.com/greenbone/ospd-openvas/smoketest/connection"
"github.com/greenbone/ospd-openvas/smoketest/nasl"
"github.com/greenbone/ospd-openvas/smoketest/policies"
"github.com/greenbone/ospd-openvas/smoketest/scan"
"github.com/greenbone/ospd-openvas/smoketest/usecases"
"github.com/greenbone/scanner-lab/feature-tests/kubeutils"
)
// Result is used to return some information about a test run
type Result struct {
Name string // Name of the test
FailureDescription string // Description if there was a failure; if there was None then it is empty
Duration time.Duration // How long did it take?
TargetIDX []int // which targts are used? To reduce memory just the index, empty for all
Resp *scan.GetScansResponse // Responses of a scan or nil when none available
}
// ExecInformation contains all information a FeatureTest may need to run
type ExecInformation struct {
OSPDAddr connection.OSPDSender // The address to the OSPD Socket
Protocoll string
Targets []kubeutils.Target // All available targets found via kubernetes
NASLCache *nasl.Cache // All known NASL plugins; is mainly used to lookup OIDs when selecting a policy
PolicyCache *policies.Cache // All available Policies
}
// Runner is used to create the start command and a verifier.
//
// The actual start of a test is done in Delegator run. This is to control the output and runtime behaviour.
type Runner interface {
Name() string // The name of the test
Start() scan.Start // Uses ExecInformation to create a scan.Start command
Verify(*usecases.GetScanResponseFailure) Result // Uses response of a run to return a Result
}
// Delegator is used to start multiple feature tests
type Delegator struct {
sync.RWMutex
ExecInformation
Runner []Runner
}
type ProgressHandler struct {
name string
}
func (r *ProgressHandler) Each(resp scan.GetScansResponse) {
fmt.Printf("\r%s: progress %d", r.name, resp.Scan.Progress)
// block the execution loop for a second to ease up
// CPU usage while running a test.
time.Sleep(1 * time.Second)
}
func (r *ProgressHandler) Last(resp scan.GetScansResponse) {
fmt.Printf("\r%s: progress %d; status: %s; ", r.name, resp.Scan.Progress, resp.Scan.Status)
}
func (d *Delegator) Run() ([]Result, error) {
result := make([]Result, 0, len(d.Runner))
if len(d.Runner) == 0 {
return nil, fmt.Errorf("No runner given.")
}
fmt.Println("Running tests")
for _, r := range d.Runner {
start := time.Now()
sr := usecases.StartScanGetLastStatus(r.Start(), d.ExecInformation.OSPDAddr, &ProgressHandler{name: r.Name()})
elapsed := time.Now().Sub(start)
re := r.Verify(&sr)
re.Duration = elapsed
if re.FailureDescription == "" {
fmt.Printf("succeeded\n")
} else {
fmt.Printf("failed: %s\n", re.FailureDescription)
}
result = append(result, re)
}
return result, nil
}
func (d *Delegator) RegisterTest(t Runner) {
d.Lock()
d.Runner = append(d.Runner, t)
d.Unlock()
}
// New initializes the needed caches, get targets and creates a Delegator
func New(ts []kubeutils.Target, vtDIR string, policyPath string, address connection.OSPDSender) (result *Delegator, err error) {
naslCache, err := nasl.InitCache(vtDIR)
if err != nil {
return
}
policyCache, err := policies.InitCache(policyPath)
if err != nil {
return
}
result = &Delegator{
ExecInformation: ExecInformation{
OSPDAddr: address,
Targets: ts,
NASLCache: naslCache,
PolicyCache: policyCache,
Protocoll: "tcp",
},
Runner: make([]Runner, 0, 10),
}
return
}