This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyzer.go
225 lines (188 loc) · 5.92 KB
/
analyzer.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package cortex
import (
"context"
"errors"
"fmt"
"net/http"
"time"
)
const (
analyzersURL = APIRoute + "/analyzer"
analyzersByType = analyzersURL + "/type/"
)
// Analyzer defines a specific Cortex Analyzer
type Analyzer struct {
Author string `json:"author"`
BaseConfig string `json:"baseConfig"`
Configuration map[string]interface{} `json:"configuration"`
CreatedAt int64 `json:"createdAt"`
CreatedBy string `json:"createdBy"`
DataTypeList []string `json:"dataTypeList"`
DefinitionID string `json:"analyzerDefinitionId"`
Description string `json:"description"`
ID string `json:"id"`
JobCache interface{} `json:"jobCache,omitempty"` // unknown
License string `json:"license"`
Name string `json:"name"`
Rate int `json:"rate,omitempty"`
RateUnit string `json:"rateUnit,omitempty"`
URL string `json:"url"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
UpdatedBy string `json:"updatedBy,omitempty"`
Version string `json:"version"`
}
// AnalyzerService is an interface for managing analyzers
type AnalyzerService interface {
Get(context.Context, string) (*Analyzer, *http.Response, error)
List(context.Context) ([]Analyzer, *http.Response, error)
ListByType(context.Context, string) ([]Analyzer, *http.Response, error)
Run(context.Context, string, Observable, time.Duration) (*Report, error)
StartJob(context.Context, string, Observable) (*Job, *http.Response, error)
NewMultiRun(context.Context, time.Duration) *MultiRun
DataTypes(context.Context) ([]string, error)
}
// AnalyzerServiceOp handles analyzer methods from Cortex API
type AnalyzerServiceOp struct {
client *Client
}
// Get a specified Cortex analyzer by its name
func (a *AnalyzerServiceOp) Get(ctx context.Context, id string) (*Analyzer, *http.Response, error) {
als, resp, err := a.List(ctx)
if err != nil {
return nil, nil, err
}
var alid string
for i := range als {
if als[i].Name == id {
alid = als[i].ID
break
}
}
if alid == "" {
return nil, resp, fmt.Errorf("no analyzer found with name %s", id)
}
req, err := a.client.NewRequest("GET", fmt.Sprintf(analyzersURL+"/%s", alid), nil)
if err != nil {
return nil, nil, err
}
var an Analyzer
resp, err = a.client.Do(ctx, req, &an)
if err != nil {
return nil, resp, err
}
return &an, resp, err
}
// List all Cortex analyzers with pagination
func (a *AnalyzerServiceOp) List(ctx context.Context) ([]Analyzer, *http.Response, error) {
var analyzers []Analyzer
start := 0
for {
pagedURL := fmt.Sprintf("%s?range=%d-%d", analyzersURL, start, a.client.PageSize)
req, err := a.client.NewRequest("GET", pagedURL, nil)
if err != nil {
return nil, nil, err
}
var paginatedAns []Analyzer
resp, err := a.client.Do(ctx, req, &paginatedAns)
if err != nil {
return nil, resp, err
}
if len(paginatedAns) < a.client.PageSize {
analyzers = append(analyzers, paginatedAns...)
break
}
analyzers = append(analyzers, paginatedAns...)
start = start + a.client.PageSize
}
return analyzers, nil, nil
}
// ListByType lists Cortex analyzers by datatype
func (a *AnalyzerServiceOp) ListByType(ctx context.Context, t string) ([]Analyzer, *http.Response, error) {
req, err := a.client.NewRequest("GET", analyzersByType+t, nil)
if err != nil {
return nil, nil, err
}
var analyzers []Analyzer
resp, err := a.client.Do(ctx, req, &analyzers)
if err != nil {
return nil, resp, err
}
return analyzers, resp, nil
}
// Run will start the observable analysis using specified analyzer,
// wait for a certain duration and return a report
func (a *AnalyzerServiceOp) Run(ctx context.Context, anid string, o Observable, d time.Duration) (*Report, error) {
an, _, err := a.Get(ctx, anid)
if err != nil {
return nil, err
}
return a.run(ctx, an.ID, o, d)
}
// run is a more lighter version that uses Cortex Analyzer ID directly
func (a *AnalyzerServiceOp) run(ctx context.Context, id string, o Observable, d time.Duration) (*Report, error) {
j, _, err := a.StartJob(ctx, id, o)
if err != nil {
return nil, err
}
jso := &JobServiceOp{a.client}
report, resp, err := jso.WaitReport(ctx, j.ID, d)
if err != nil {
if resp != nil && resp.StatusCode == 500 {
return nil, fmt.Errorf("job passed maximum execution time %s", d.String())
}
if resp != nil && resp.StatusCode == 429 {
return nil, errors.New("rate limit exceeded")
}
return nil, err
}
return report, err
}
// StartJob starts observable analysis
func (a *AnalyzerServiceOp) StartJob(ctx context.Context, anid string, o Observable) (*Job, *http.Response, error) {
var req *http.Request
var err error
switch o.Type() {
case "file":
obsData := o.(*FileTask)
req, err = a.client.NewFileRequest("POST", fmt.Sprintf(analyzersURL+"/%s/run", anid), &obsData, obsData.FileName, obsData.Reader)
if err != nil {
return nil, nil, err
}
default:
obsData := o.(*Task)
req, err = a.client.NewRequest("POST", fmt.Sprintf(analyzersURL+"/%s/run", anid), &obsData)
if err != nil {
return nil, nil, err
}
}
var j Job
resp, err := a.client.Do(ctx, req, &j)
if err != nil {
return nil, nil, err
}
return &j, resp, nil
}
// DataTypes returns all available data types that Cortex can analyse.
// The entries are not sorted.
func (a *AnalyzerServiceOp) DataTypes(ctx context.Context) ([]string, error) {
ans, _, err := a.List(ctx)
if err != nil {
return nil, err
}
dtm := make(map[string]bool)
for _, a := range ans {
addDataTypes(dtm, a.DataTypeList)
}
var dts []string
for k := range dtm {
dts = append(dts, k)
}
return dts, nil
}
func addDataTypes(m map[string]bool, dts []string) {
for _, dt := range dts {
if _, ok := m[dt]; !ok {
m[dt] = true
}
}
}