/
rel_osint_datasource.go
247 lines (223 loc) · 8.83 KB
/
rel_osint_datasource.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package osint
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/ca-risken/core/proto/project"
"github.com/ca-risken/datasource-api/pkg/message"
"github.com/ca-risken/datasource-api/pkg/model"
"github.com/ca-risken/datasource-api/proto/aws"
"github.com/ca-risken/datasource-api/proto/osint"
"github.com/golang/protobuf/ptypes/empty"
"gorm.io/gorm"
)
func (o *OsintService) ListRelOsintDataSource(ctx context.Context, req *osint.ListRelOsintDataSourceRequest) (*osint.ListRelOsintDataSourceResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
list, err := o.repository.ListRelOsintDataSource(ctx, req.ProjectId, req.OsintId, req.OsintDataSourceId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return &osint.ListRelOsintDataSourceResponse{}, nil
}
o.logger.Errorf(ctx, "Failed to List RelOsintDataSource. error: %v", err)
return nil, err
}
data := osint.ListRelOsintDataSourceResponse{}
for _, d := range *list {
data.RelOsintDataSource = append(data.RelOsintDataSource, convertRelOsintDataSource(&d))
}
return &data, nil
}
func (o *OsintService) GetRelOsintDataSource(ctx context.Context, req *osint.GetRelOsintDataSourceRequest) (*osint.GetRelOsintDataSourceResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
getData, err := o.repository.GetRelOsintDataSource(ctx, req.ProjectId, req.RelOsintDataSourceId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return &osint.GetRelOsintDataSourceResponse{}, nil
}
o.logger.Errorf(ctx, "Failed to Get RelOsintDataSource. error: %v", err)
return nil, err
}
return &osint.GetRelOsintDataSourceResponse{RelOsintDataSource: convertRelOsintDataSource(getData)}, nil
}
func (o *OsintService) PutRelOsintDataSource(ctx context.Context, req *osint.PutRelOsintDataSourceRequest) (*osint.PutRelOsintDataSourceResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
data := &model.RelOsintDataSource{
RelOsintDataSourceID: req.RelOsintDataSource.RelOsintDataSourceId,
ProjectID: req.ProjectId,
OsintDataSourceID: req.RelOsintDataSource.OsintDataSourceId,
OsintID: req.RelOsintDataSource.OsintId,
Status: req.RelOsintDataSource.Status.String(),
StatusDetail: req.RelOsintDataSource.StatusDetail,
ScanAt: time.Unix(req.RelOsintDataSource.ScanAt, 0),
}
registeredData, err := o.repository.UpsertRelOsintDataSource(ctx, data)
if err != nil {
o.logger.Errorf(ctx, "Failed to Put RelOsintDataSource. error: %v", err)
return nil, err
}
if !registeredData.ErrorNotifiedAt.IsZero() && registeredData.Status != aws.Status_ERROR.String() {
if err := o.repository.UpdateOsintErrorNotifiedAt(ctx, gorm.Expr("NULL"), registeredData.RelOsintDataSourceID, registeredData.ProjectID); err != nil {
return nil, err
}
}
return &osint.PutRelOsintDataSourceResponse{RelOsintDataSource: convertRelOsintDataSource(registeredData)}, nil
}
func (o *OsintService) DeleteRelOsintDataSource(ctx context.Context, req *osint.DeleteRelOsintDataSourceRequest) (*empty.Empty, error) {
if err := req.Validate(); err != nil {
return nil, err
}
if err := o.deleteRelOsintDataSourceWithDetectWord(ctx, req.ProjectId, req.RelOsintDataSourceId); err != nil {
o.logger.Errorf(ctx, "Failed to DeleteRelOsintDataSource. error: %v", err)
return nil, err
}
return &empty.Empty{}, nil
}
func (o *OsintService) deleteRelOsintDataSourceWithDetectWord(ctx context.Context, projectID, relOsintDataSourceID uint32) error {
detectWords, err := o.repository.ListOsintDetectWord(ctx, projectID, relOsintDataSourceID)
if err != nil {
return err
}
for _, d := range *detectWords {
if err := o.repository.DeleteOsintDetectWord(ctx, projectID, d.OsintDetectWordID); err != nil {
return err
}
}
if err := o.repository.DeleteRelOsintDataSource(ctx, projectID, relOsintDataSourceID); err != nil {
return err
}
return nil
}
func convertRelOsintDataSource(data *model.RelOsintDataSource) *osint.RelOsintDataSource {
if data == nil {
return &osint.RelOsintDataSource{}
}
return &osint.RelOsintDataSource{
RelOsintDataSourceId: data.RelOsintDataSourceID,
OsintDataSourceId: data.OsintDataSourceID,
OsintId: data.OsintID,
ProjectId: data.ProjectID,
CreatedAt: data.CreatedAt.Unix(),
UpdatedAt: data.CreatedAt.Unix(),
Status: getStatus(data.Status),
StatusDetail: data.StatusDetail,
ScanAt: data.ScanAt.Unix(),
}
}
func (o *OsintService) InvokeScan(ctx context.Context, req *osint.InvokeScanRequest) (*osint.InvokeScanResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
relOsintDataSourceData, err := o.repository.GetRelOsintDataSource(ctx, req.ProjectId, req.RelOsintDataSourceId)
if err != nil {
return nil, err
}
osintDataSourceData, err := o.repository.GetOsintDataSource(ctx, relOsintDataSourceData.ProjectID, relOsintDataSourceData.OsintDataSourceID)
if err != nil {
return nil, err
}
osintData, err := o.repository.GetOsint(ctx, relOsintDataSourceData.ProjectID, relOsintDataSourceData.OsintID)
if err != nil {
return nil, err
}
detectWord, err := o.repository.ListOsintDetectWord(ctx, relOsintDataSourceData.ProjectID, relOsintDataSourceData.RelOsintDataSourceID)
if err != nil {
return nil, err
}
jsonDetectWord, err := json.Marshal(map[string][]model.OsintDetectWord{"DetectWord": *detectWord})
if err != nil {
return nil, err
}
msg := &message.OsintQueueMessage{
DataSource: osintDataSourceData.Name,
RelOsintDataSourceID: req.RelOsintDataSourceId,
OsintID: relOsintDataSourceData.OsintID,
OsintDataSourceID: relOsintDataSourceData.OsintDataSourceID,
ProjectID: req.ProjectId,
ResourceType: osintData.ResourceType,
ResourceName: osintData.ResourceName,
DetectWord: string(jsonDetectWord),
ScanOnly: req.ScanOnly,
}
var resp *sqs.SendMessageOutput
if msg.DataSource == message.SubdomainDataSource {
resp, err = o.sqs.Send(ctx, o.sqs.OSINTSubdomainQueueURL, msg)
} else if msg.DataSource == message.WebsiteDataSource {
resp, err = o.sqs.Send(ctx, o.sqs.OSINTWebsiteQueueURL, msg)
} else {
return nil, fmt.Errorf("unknown datasource, datasource=%s", msg.DataSource)
}
if err != nil {
return nil, err
}
if _, err = o.repository.UpsertRelOsintDataSource(ctx, &model.RelOsintDataSource{
RelOsintDataSourceID: relOsintDataSourceData.RelOsintDataSourceID,
OsintID: relOsintDataSourceData.OsintID,
OsintDataSourceID: relOsintDataSourceData.OsintDataSourceID,
ProjectID: relOsintDataSourceData.ProjectID,
Status: osint.Status_IN_PROGRESS.String(),
StatusDetail: fmt.Sprintf("Start scan at %+v", time.Now().Format(time.RFC3339)),
ScanAt: relOsintDataSourceData.ScanAt,
}); err != nil {
o.logger.Errorf(ctx, "Failed to update scan status: %+v", err)
return nil, err
}
o.logger.Infof(ctx, "Invoked scan. MessageId: %v", *resp.MessageId)
return &osint.InvokeScanResponse{Message: "Invoke Scan."}, nil
}
func (o *OsintService) InvokeScanAll(ctx context.Context, req *osint.InvokeScanAllRequest) (*empty.Empty, error) {
list, err := o.repository.ListAllRelOsintDataSource(ctx, req.OsintDataSourceId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return &empty.Empty{}, nil
}
o.logger.Errorf(ctx, "Failed to List AllRelOsintDataSource. error: %v", err)
return nil, err
}
for _, relOsintDataSource := range *list {
if resp, err := o.projectClient.IsActive(ctx, &project.IsActiveRequest{ProjectId: relOsintDataSource.ProjectID}); err != nil {
o.logger.Errorf(ctx, "Failed to project.IsActive API, err=%+v", err)
return nil, err
} else if !resp.Active {
o.logger.Infof(ctx, "Skip deactive project, project_id=%d", relOsintDataSource.ProjectID)
continue
}
if _, err := o.InvokeScan(ctx, &osint.InvokeScanRequest{
ProjectId: relOsintDataSource.ProjectID,
RelOsintDataSourceId: relOsintDataSource.RelOsintDataSourceID,
ScanOnly: true,
}); err != nil {
o.logger.Errorf(ctx, "InvokeScanAll error: project_id=%d, rel_osint_data_source_id=%d, err=%+v",
relOsintDataSource.ProjectID, relOsintDataSource.RelOsintDataSourceID, err)
return nil, err
}
}
return &empty.Empty{}, nil
}
func getStatus(s string) osint.Status {
statusKey := strings.ToUpper(s)
if _, ok := osint.Status_value[statusKey]; !ok {
return osint.Status_UNKNOWN
}
switch statusKey {
case osint.Status_OK.String():
return osint.Status_OK
case osint.Status_CONFIGURED.String():
return osint.Status_CONFIGURED
case osint.Status_IN_PROGRESS.String():
return osint.Status_IN_PROGRESS
case osint.Status_ERROR.String():
return osint.Status_ERROR
default:
return osint.Status_UNKNOWN
}
}