-
Notifications
You must be signed in to change notification settings - Fork 311
/
warehousegrpc.go
281 lines (254 loc) · 8.58 KB
/
warehousegrpc.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package warehouse
import (
"context"
"encoding/json"
"errors"
"fmt"
backendconfig "github.com/rudderlabs/rudder-server/backend-config"
proto "github.com/rudderlabs/rudder-server/proto/warehouse"
"github.com/rudderlabs/rudder-server/warehouse/client/controlplane"
"github.com/rudderlabs/rudder-server/warehouse/internal/model"
warehouseutils "github.com/rudderlabs/rudder-server/warehouse/utils"
"github.com/rudderlabs/rudder-server/warehouse/validations"
"google.golang.org/genproto/googleapis/rpc/code"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
type warehouseGRPC struct {
proto.UnimplementedWarehouseServer
CPClient controlplane.InternalControlPlane
EnableTunnelling bool
}
func (*warehouseGRPC) GetWHUploads(_ context.Context, request *proto.WHUploadsRequest) (*proto.WHUploadsResponse, error) {
uploadsReq := UploadsReq{
WorkspaceID: request.WorkspaceId,
SourceID: request.SourceId,
DestinationID: request.DestinationId,
DestinationType: request.DestinationType,
Status: request.Status,
Limit: request.Limit,
Offset: request.Offset,
API: UploadAPI,
}
uploadsReq.API.log.Infof(
"[GetWHUploads] Fetching warehouse uploads for WorkspaceId: %s, SourceId: %s, DestinationId: %s",
uploadsReq.WorkspaceID,
uploadsReq.SourceID,
uploadsReq.DestinationID,
)
res, err := uploadsReq.GetWhUploads()
return res, err
}
func (*warehouseGRPC) TriggerWHUploads(_ context.Context, request *proto.WHUploadsRequest) (*proto.TriggerWhUploadsResponse, error) {
uploadsReq := UploadsReq{
WorkspaceID: request.WorkspaceId,
SourceID: request.SourceId,
DestinationID: request.DestinationId,
API: UploadAPI,
}
uploadsReq.API.log.Infof(
"[TriggerWHUploads] Triggering warehouse uploads for WorkspaceId: %s, SourceId: %s, DestinationId: %s",
uploadsReq.WorkspaceID,
uploadsReq.SourceID,
uploadsReq.DestinationID,
)
res, err := uploadsReq.TriggerWhUploads()
return res, err
}
func (*warehouseGRPC) GetWHUpload(_ context.Context, request *proto.WHUploadRequest) (*proto.WHUploadResponse, error) {
uploadReq := UploadReq{
UploadId: request.UploadId,
WorkspaceID: request.WorkspaceId,
API: UploadAPI,
}
uploadReq.API.log.Infof(
"[GetWHUpload] Fetching warehouse upload for WorkspaceId: %s, UploadId: %d",
uploadReq.WorkspaceID,
uploadReq.UploadId,
)
res, err := uploadReq.GetWHUpload()
return res, err
}
func (*warehouseGRPC) GetHealth(context.Context, *emptypb.Empty) (*wrapperspb.BoolValue, error) {
return wrapperspb.Bool(UploadAPI.enabled), nil
}
func (*warehouseGRPC) TriggerWHUpload(_ context.Context, request *proto.WHUploadRequest) (*proto.TriggerWhUploadsResponse, error) {
uploadReq := UploadReq{
UploadId: request.UploadId,
WorkspaceID: request.WorkspaceId,
API: UploadAPI,
}
uploadReq.API.log.Infof(
"[TriggerWHUpload] Triggering warehouse upload for WorkspaceId: %s, UploadId: %d",
uploadReq.WorkspaceID,
uploadReq.UploadId,
)
res, err := uploadReq.TriggerWHUpload()
return res, err
}
func (grpc *warehouseGRPC) manageTunnellingSecrets(ctx context.Context, config map[string]interface{}) error {
if !warehouseutils.ReadAsBool("useSSH", config) {
return nil
}
sshKeyId, ok := config["sshKeyId"]
if !ok {
return fmt.Errorf("missing sshKeyId in validation payload")
}
keys, err := grpc.CPClient.GetSSHKeys(ctx, sshKeyId.(string))
if err != nil {
return fmt.Errorf("fetching destination ssh keys: %w", err)
}
config["sshPrivateKey"] = keys.PrivateKey
return nil
}
func (grpc *warehouseGRPC) Validate(ctx context.Context, req *proto.WHValidationRequest) (*proto.WHValidationResponse, error) {
var (
err error
resModel struct {
Destination backendconfig.DestinationT `json:"destination"`
}
)
err = json.Unmarshal(json.RawMessage(req.Body), &resModel)
if err != nil {
return &proto.WHValidationResponse{}, fmt.Errorf("unmarshal request body: %w", err)
}
destination := resModel.Destination
if len(destination.Config) == 0 {
return &proto.WHValidationResponse{}, errors.New("destination config is empty")
}
// adding ssh tunnelling info, given we have
// useSSH enabled from upstream
if grpc.EnableTunnelling {
err = grpc.manageTunnellingSecrets(ctx, destination.Config)
if err != nil {
return &proto.WHValidationResponse{}, fmt.Errorf("fetching destination ssh keys: %w", err)
}
}
res, err := validations.Validate(&model.ValidationRequest{
Path: req.Path,
Step: req.Step,
Destination: &destination,
})
if err != nil {
return &proto.WHValidationResponse{}, fmt.Errorf("validate: %w", err)
}
return &proto.WHValidationResponse{
Error: res.Error,
Data: res.Data,
}, nil
}
func (*warehouseGRPC) RetryWHUploads(ctx context.Context, req *proto.RetryWHUploadsRequest) (response *proto.RetryWHUploadsResponse, err error) {
retryReq := &RetryRequest{
WorkspaceID: req.WorkspaceId,
SourceID: req.SourceId,
DestinationID: req.DestinationId,
DestinationType: req.DestinationType,
IntervalInHours: req.IntervalInHours,
ForceRetry: req.ForceRetry,
UploadIds: req.UploadIds,
API: UploadAPI,
}
retryReq.API.log.Infof(
"[RetryWHUploads] Retrying warehouse upload for WorkspaceId: %s, SourceId: %s, DestinationId: %s, DestinationType: %s, IntervalInHours: %d",
retryReq.WorkspaceID,
retryReq.SourceID,
retryReq.DestinationID,
retryReq.DestinationType,
retryReq.IntervalInHours,
)
r, err := retryReq.RetryWHUploads(ctx)
response = &proto.RetryWHUploadsResponse{
Message: r.Message,
StatusCode: r.StatusCode,
}
return
}
type ObjectStorageValidationRequest struct {
Type string `json:"type"`
Config map[string]interface{} `json:"config"`
}
func validateObjectStorageRequestBody(request *proto.ValidateObjectStorageRequest) (*ObjectStorageValidationRequest, error) {
byt, err := json.Marshal(request)
if err != nil {
return nil, status.Errorf(
codes.Code(code.Code_INTERNAL),
"unable to marshal the request proto message with error: \n%s", err.Error())
}
r := &ObjectStorageValidationRequest{}
if err := json.Unmarshal(byt, r); err != nil {
return nil, status.Errorf(
codes.Code(code.Code_INTERNAL),
"unable to extract data into validation request with error: \n%s", err)
}
switch r.Type {
case "AZURE_BLOB":
if !checkMapForValidKey(r.Config, "containerName") {
err = fmt.Errorf("containerName invalid or not present")
}
case "GCS", "MINIO", "S3", "DIGITAL_OCEAN_SPACES":
if !checkMapForValidKey(r.Config, "bucketName") {
err = fmt.Errorf("bucketName invalid or not present")
}
default:
err = fmt.Errorf("type: %v not supported", r.Type)
}
if err != nil {
return nil, status.Errorf(
codes.Code(code.Code_INVALID_ARGUMENT),
"invalid argument err: \n%s", err.Error())
}
return r, nil
}
func (*warehouseGRPC) ValidateObjectStorageDestination(ctx context.Context, request *proto.ValidateObjectStorageRequest) (response *proto.ValidateObjectStorageResponse, err error) {
r, err := validateObjectStorageRequestBody(request)
if err != nil {
return nil, err
}
UploadAPI.log.Infof(
"[ValidateObjectStorageDestination] Validating object storage destination for type: %s",
r.Type,
)
err = validateObjectStorage(ctx, r)
if err != nil {
if errors.As(err, &InvalidDestinationCredErr{}) {
return &proto.ValidateObjectStorageResponse{
IsValid: false,
Error: err.Error(),
}, nil
}
return &proto.ValidateObjectStorageResponse{},
status.Errorf(codes.Code(code.Code_INTERNAL), "unable to handle validate storage request call: %s", err)
}
return &proto.ValidateObjectStorageResponse{
IsValid: true,
Error: "",
}, nil
}
func (*warehouseGRPC) CountWHUploadsToRetry(ctx context.Context, req *proto.RetryWHUploadsRequest) (response *proto.RetryWHUploadsResponse, err error) {
retryReq := &RetryRequest{
WorkspaceID: req.WorkspaceId,
SourceID: req.SourceId,
DestinationID: req.DestinationId,
DestinationType: req.DestinationType,
IntervalInHours: req.IntervalInHours,
ForceRetry: req.ForceRetry,
UploadIds: req.UploadIds,
API: UploadAPI,
}
retryReq.API.log.Infof(
"[RetryWHUploads] Retrying warehouse uploads for WorkspaceId: %s, SourceId: %s, DestinationId: %s, IntervalInHours: %d",
retryReq.WorkspaceID,
retryReq.SourceID,
retryReq.DestinationID,
retryReq.IntervalInHours,
)
r, err := retryReq.UploadsToRetry(ctx)
response = &proto.RetryWHUploadsResponse{
Count: r.Count,
Message: r.Message,
StatusCode: r.StatusCode,
}
return
}