-
Notifications
You must be signed in to change notification settings - Fork 111
/
server.go
186 lines (163 loc) · 6.13 KB
/
server.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
package server
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"time"
"github.com/chef/automate/api/interservice/compliance/ingest/events/compliance"
cc_reporting "github.com/chef/automate/api/interservice/compliance/reporting"
"github.com/chef/automate/api/interservice/report_manager"
"github.com/chef/automate/components/report-manager-service/config"
"github.com/chef/automate/components/report-manager-service/objstore"
"github.com/chef/automate/components/report-manager-service/storage"
"github.com/chef/automate/components/report-manager-service/utils"
"github.com/chef/automate/components/report-manager-service/worker"
"github.com/chef/automate/lib/cereal"
"github.com/golang/protobuf/ptypes"
"github.com/google/uuid"
"github.com/minio/minio-go/v7"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/emptypb"
)
// Server implementation for reporting
type Server struct {
ObjStoreClient objstore.ObjectStore
ComplianceReportingClient cc_reporting.ReportingServiceClient
CerealManager *cereal.Manager
ctx context.Context
ObjBucket string
DataStore *storage.DB
EnableLargeReporting bool
TSL TSL
}
type TSL struct {
TSLEnabled bool
ClientCert string
}
// New creates a new server
func New(objStoreClient *minio.Client, cerealManager *cereal.Manager, conf config.ReportManager, db *storage.DB,
complianceReportingClient cc_reporting.ReportingServiceClient) *Server {
return &Server{
ObjStoreClient: objstore.ReportManagerObjStore{
ObjStoreClient: objStoreClient,
},
ComplianceReportingClient: complianceReportingClient,
CerealManager: cerealManager,
ctx: context.Background(),
ObjBucket: conf.ObjStore.BucketName,
DataStore: db,
EnableLargeReporting: conf.Service.EnableLargeReporting,
TSL: TSL{
TSLEnabled: conf.Minio.EnableSsl,
ClientCert: conf.Minio.Cert,
},
}
}
func (s *Server) StoreReport(stream report_manager.ReportManagerService_StoreReportServer) error {
if !s.EnableLargeReporting {
return fmt.Errorf("customer not enabled for large reporting")
}
reportData := bytes.Buffer{}
for {
req, err := stream.Recv()
if err == io.EOF {
//reached end of file
break
}
if err != nil {
return fmt.Errorf("error received from stream: %w", err)
}
chunk := req.GetContent()
_, err = reportData.Write(chunk)
if err != nil {
return fmt.Errorf("cannot write chunk data: %w", err)
}
}
complianceReport := compliance.Report{}
err := json.Unmarshal(reportData.Bytes(), &complianceReport)
if err != nil {
return fmt.Errorf("error in converting report bytes to compliance report struct: %w", err)
}
objName := utils.GetObjName(complianceReport.GetReportUuid())
//TODO:: Add an expiry based on user configuration
info, err := s.ObjStoreClient.PutObject(s.ctx, s.ObjBucket, objName, &reportData, -1, minio.PutObjectOptions{})
if err != nil {
return fmt.Errorf("error in storing the report %s in object store: %w", complianceReport.GetReportUuid(), err)
}
logrus.Infof("report with uuid %s of size %d stroed in object store with key:%s", complianceReport.GetReportUuid(), info.Size, info.Key)
return stream.SendAndClose(&emptypb.Empty{})
}
func (s *Server) PrepareCustomReport(ctx context.Context, req *report_manager.CustomReportRequest) (
*report_manager.CustomReportResponse, error) {
if !s.EnableLargeReporting {
return nil, fmt.Errorf("customer not enabled for large reporting")
}
id := uuid.New()
err := s.CerealManager.EnqueueWorkflow(s.ctx, worker.ReportWorkflowName,
fmt.Sprintf("%s-%s", "report-workflow", id.String()),
worker.ReportWorkflowParameters{
JobID: id.String(),
RequestorID: req.RequestorId,
Retries: 2,
RequestToProcess: req,
})
if err != nil {
return &report_manager.CustomReportResponse{},
fmt.Errorf("error in enqueuing the workflow for request id %s: %w", id.String(), err)
}
return &report_manager.CustomReportResponse{
AcknowledgementId: id.String(),
}, nil
}
func (s *Server) GetAllRequestsStatus(ctx context.Context, req *report_manager.AllStatusRequest) (*report_manager.AllStatusResponse, error) {
if !s.EnableLargeReporting {
return nil, fmt.Errorf("customer not enabled for large reporting")
}
if req.RequestorId == "" {
return &report_manager.AllStatusResponse{}, fmt.Errorf("empty requestore information")
}
resp := &report_manager.AllStatusResponse{}
dbResp, err := s.DataStore.GetAllStatus(req.RequestorId, time.Now().Add(-24*time.Hour))
if err != nil {
return nil, err
}
for _, reportStatus := range dbResp {
createdAt, err := ptypes.TimestampProto(reportStatus.StartTime)
if err != nil {
return nil, errors.Wrapf(err, "error in converting the created at with value %s to timestamppb.Timestamp", reportStatus.StartTime)
}
endedAt, err := ptypes.TimestampProto(reportStatus.EndTime)
if err != nil {
return nil, errors.Wrapf(err, "error in converting the ended at with value %s to timestamppb.Timestamp", reportStatus.EndTime)
}
resp.Data = append(resp.Data, &report_manager.StatusResponse{
AcknowledgementId: reportStatus.ID,
Status: reportStatus.Status,
ReportSize: reportStatus.ReportSize.Int64,
ErrMessage: reportStatus.Message.String,
CreatedAt: createdAt,
EndedAt: endedAt,
Duration: utils.ComputeDuration(reportStatus.StartTime, reportStatus.EndTime),
ReportType: reportStatus.ReportType.String,
})
}
return resp, nil
}
func (s *Server) GetPresignedURL(ctx context.Context, req *report_manager.GetPresignedURLRequest) (
*report_manager.GetPresignedURLResponse, error) {
if !s.EnableLargeReporting {
return nil, fmt.Errorf("customer not enabled for large reporting")
}
if req.Id == "" || req.RequestorId == "" {
return &report_manager.GetPresignedURLResponse{}, fmt.Errorf("id and requestor should not be empty")
}
resp, err := s.DataStore.GetPreSignedURL(req.Id, req.RequestorId)
if err == nil {
resp.EnabledSsl = s.TSL.TSLEnabled
resp.ClientCert = s.TSL.ClientCert
}
return resp, err
}