/
worker.go
154 lines (138 loc) · 5.13 KB
/
worker.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
143
144
145
146
147
148
149
150
151
152
153
154
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package vulnerability
import (
"context"
"fmt"
"sync"
"time"
"github.com/elastic/beats/v7/libbeat/beat"
libbeat_aws "github.com/elastic/beats/v7/x-pack/libbeat/common/aws"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/cloudbeat/internal/config"
"github.com/elastic/cloudbeat/internal/dataprovider"
"github.com/elastic/cloudbeat/internal/resources/providers/awslib"
"github.com/elastic/cloudbeat/internal/resources/providers/awslib/ec2"
)
type VulnerabilityWorker struct {
log *logp.Logger
cfg *config.Config
cleaner Cleaner
provider workerProvider
fetcher VulnerabilityFetcher
replicator VulnerabilityReplicator
verifier VulnerabilityVerifier
evaluator VulnerabilityScanner
runner VulnerabilityRunner
eventsCreator EventsCreator
wg sync.WaitGroup
}
type workerProvider interface {
DescribeInstances(ctx context.Context) ([]*ec2.Ec2Instance, error)
DescribeVolumes(ctx context.Context, instances []*ec2.Ec2Instance) ([]*ec2.Volume, error)
CreateSnapshots(ctx context.Context, ins *ec2.Ec2Instance) ([]ec2.EBSSnapshot, error)
DescribeSnapshots(ctx context.Context, snap ec2.EBSSnapshot) ([]ec2.EBSSnapshot, error)
DeleteSnapshot(ctx context.Context, snapshot ec2.EBSSnapshot) error
}
func NewVulnerabilityWorker(log *logp.Logger, c *config.Config, bdp dataprovider.CommonDataProvider, cdp dataprovider.ElasticCommonDataProvider) (*VulnerabilityWorker, error) {
log.Debug("VulnerabilityWorker: New")
awsConfig, err := libbeat_aws.InitializeAWSConfig(c.CloudConfig.Aws.Cred)
if err != nil {
return nil, fmt.Errorf("VulnerabilityWorker: failed to initialize AWS credentials: %w", err)
}
provider := ec2.NewCurrentRegionEC2Provider(log, "", awsConfig, &awslib.MultiRegionClientFactory[ec2.Client]{})
fetcher := NewVulnerabilityFetcher(log, provider)
replicator := NewVulnerabilityReplicator(log, provider)
verifier := NewVulnerabilityVerifier(log, provider)
runner, err := NewVulnerabilityRunner(log)
if err != nil {
return nil, fmt.Errorf("VulnerabilityWorker: could not get init NewVulnerabilityRunner: %w", err)
}
// TODO: Replace sequence with more generic approach
evaluator, err := NewVulnerabilityScanner(log, runner.GetRunner(), c, time.Now())
if err != nil {
return nil, fmt.Errorf("VulnerabilityWorker: could not get init NewVulnerabilityScanner: %w", err)
}
eventsCreator := NewEventsCreator(log, bdp, cdp)
cleaner := NewVulnerabilityCleaner(log, provider)
return &VulnerabilityWorker{
log: log,
cfg: c,
cleaner: cleaner,
provider: provider,
fetcher: fetcher,
replicator: replicator,
verifier: verifier,
evaluator: evaluator,
eventsCreator: eventsCreator,
runner: runner,
wg: sync.WaitGroup{},
}, nil
}
func (f *VulnerabilityWorker) Run(ctx context.Context) {
f.log.Info("Starting VulnerabilityWorker.work")
defer f.runner.GetRunner().Close(ctx)
select {
case <-ctx.Done():
f.log.Info("VulnerabilityWorker.work context canceled")
return
default:
f.wg.Add(1)
go func() {
defer f.wg.Done()
err := f.fetcher.FetchInstances(ctx)
if err != nil {
f.log.Error("VulnerabilityWorker.work FetchInstances failed: %v", err)
return
}
f.log.Info("VulnerabilityWorker.work FetchInstances finished")
}()
f.wg.Add(1)
go func() {
defer f.wg.Done()
f.cleaner.GetChan() <- f.replicator.SnapshotInstance(ctx, f.fetcher.GetChan())
f.log.Info("VulnerabilityWorker.work SnapshotInstance finished")
}()
f.wg.Add(1)
go func() {
defer f.wg.Done()
f.verifier.VerifySnapshot(ctx, f.replicator.GetChan())
f.log.Info("VulnerabilityWorker.work VerifySnapshot finished")
}()
f.wg.Add(1)
go func() {
defer f.wg.Done()
f.evaluator.ScanSnapshot(ctx, f.verifier.GetChan())
f.log.Info("VulnerabilityWorker.work ScanSnapshot finished")
}()
f.wg.Add(1)
go func() {
defer f.wg.Done()
f.eventsCreator.CreateEvents(ctx, f.evaluator.GetChan())
f.log.Info("VulnerabilityWorker.work EventsCreator finished")
}()
f.log.Info("VulnerabilityWorker.work waiting on workers")
f.wg.Wait()
f.log.Info("VulnerabilityWorker.work finished waiting on workers")
f.cleaner.CleanSnapshots(ctx)
f.log.Info("VulnerabilityWorker.work CleanSnapshots finished")
return
}
}
func (f *VulnerabilityWorker) GetChan() chan []beat.Event {
return f.eventsCreator.GetChan()
}