-
Notifications
You must be signed in to change notification settings - Fork 1
/
report.go
184 lines (168 loc) · 5.52 KB
/
report.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
package handler
import (
"encoding/json"
"fmt"
"github.com/ScoreTrak/ScoreTrak/pkg/policy"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/ScoreTrak/ScoreTrak/pkg/policy/policyclient"
reportpb "github.com/ScoreTrak/ScoreTrak/pkg/proto/report/v1"
"github.com/ScoreTrak/ScoreTrak/pkg/report"
"github.com/ScoreTrak/ScoreTrak/pkg/report/reportclient"
"github.com/ScoreTrak/ScoreTrak/pkg/report/reportservice"
"github.com/ScoreTrak/ScoreTrak/pkg/user"
"github.com/gofrs/uuid"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type ReportController struct {
svc reportservice.Serv
reportClient *reportclient.Client
policyClient *policyclient.Client
reportpb.UnimplementedReportServiceServer
}
func removeDisabledAndHidden(simpleReport *report.SimpleReport) {
for t := range simpleReport.Teams {
if simpleReport.Teams[t].Hide {
delete(simpleReport.Teams, t)
continue
}
for h := range simpleReport.Teams[t].Hosts {
if simpleReport.Teams[t].Hosts[h].Hide || (simpleReport.Teams[t].Hosts[h].HostGroup != nil && simpleReport.Teams[t].Hosts[h].HostGroup.Hide) {
delete(simpleReport.Teams[t].Hosts, h)
continue
}
for s := range simpleReport.Teams[t].Hosts[h].Services {
if simpleReport.Teams[t].Hosts[h].Services[s].Hide || !simpleReport.Teams[t].Hosts[h].Services[s].SimpleServiceGroup.Enabled {
delete(simpleReport.Teams[t].Hosts[h].Services, s)
continue
}
}
}
}
}
func calculateTotalPoints(simpleReport *report.SimpleReport) {
for t := range simpleReport.Teams {
for h := range simpleReport.Teams[t].Hosts {
for s := range simpleReport.Teams[t].Hosts[h].Services {
simpleReport.Teams[t].TotalPoints += simpleReport.Teams[t].Hosts[h].Services[s].Points + simpleReport.Teams[t].Hosts[h].Services[s].PointsBoost
}
}
}
}
func filterBlueTeams(simpleReport *report.SimpleReport, tID uuid.UUID, p *policy.Policy) {
for t := range simpleReport.Teams {
for h := range simpleReport.Teams[t].Hosts {
for s := range simpleReport.Teams[t].Hosts[h].Services {
propFilterHide := map[string]*report.SimpleProperty{}
for key, val := range simpleReport.Teams[t].Hosts[h].Services[s].Properties {
if val.Status != "Hide" {
propFilterHide[key] = val
}
}
simpleReport.Teams[t].Hosts[h].Services[s].Properties = propFilterHide
if t != tID {
if simpleReport.Teams[t].Hosts[h].Services[s].Check != nil {
simpleReport.Teams[t].Hosts[h].Services[s].Check.Err = ""
simpleReport.Teams[t].Hosts[h].Services[s].Check.Log = ""
}
prop := map[string]*report.SimpleProperty{}
if *p.ShowAddresses {
if val, ok := simpleReport.Teams[t].Hosts[h].Services[s].Properties["Port"]; ok {
prop["Port"] = val
}
}
simpleReport.Teams[t].Hosts[h].Services[s].Properties = prop
if !*p.ShowPoints {
simpleReport.Teams[t].Hosts[h].Services[s].Points = 0
simpleReport.Teams[t].Hosts[h].Services[s].PointsBoost = 0
simpleReport.Teams[t].TotalPoints = 0
}
}
}
if t != tID {
if !*p.ShowAddresses {
simpleReport.Teams[t].Hosts[h].Address = ""
}
}
}
}
}
func (r *ReportController) filterReport(rol string, tID uuid.UUID, lr *report.Report) (*reportpb.Report, error) {
simpleReport := &report.SimpleReport{}
err := json.Unmarshal([]byte(lr.Cache), simpleReport)
if err != nil {
return nil, err
}
removeDisabledAndHidden(simpleReport)
calculateTotalPoints(simpleReport)
if rol != user.Black {
filterBlueTeams(simpleReport, tID, r.policyClient.GetPolicy())
}
ret, err := json.Marshal(simpleReport)
if err != nil {
return nil, err
}
return &reportpb.Report{
Cache: string(ret),
UpdatedAt: timestamppb.New(lr.UpdatedAt),
}, nil
}
func (r *ReportController) Get(_ *reportpb.GetRequest, server reportpb.ReportService_GetServer) error {
rol := user.Anonymous
tID := uuid.UUID{}
lr, err := r.svc.Get(server.Context())
if err != nil {
return status.Errorf(codes.Internal,
fmt.Sprintf("Unable to retrieve report: %v", err))
}
claims := extractUserClaim(server.Context())
if claims != nil {
rol = claims.Role
tID = uuid.FromStringOrNil(claims.TeamID)
}
if rol == user.Anonymous && !r.policyClient.GetAllowUnauthenticatedUsers() {
return status.Error(codes.PermissionDenied, "You must login in order to access this resource")
}
frep, err := r.filterReport(rol, tID, lr)
if err != nil {
return status.Errorf(codes.Internal,
fmt.Sprintf("Unable to filter report: %v", err))
}
err = server.Send(&reportpb.GetResponse{Report: frep})
if err != nil {
return err
}
uid, ch := r.reportClient.Subscribe()
defer r.reportClient.Unsubscribe(uid)
for {
select {
case <-ch:
if rol == user.Anonymous && !r.policyClient.GetAllowUnauthenticatedUsers() {
return status.Error(codes.PermissionDenied, "You must login in order to access this resource")
}
lr, err := r.svc.Get(server.Context())
if err != nil {
return status.Errorf(codes.Internal,
fmt.Sprintf("Unable to retrieve report: %v", err))
}
frep, err = r.filterReport(rol, tID, lr)
if err != nil {
return status.Errorf(codes.Internal,
fmt.Sprintf("Unable to filter report: %v", err))
}
err = server.Send(&reportpb.GetResponse{Report: frep})
if err != nil {
return err
}
case <-server.Context().Done():
return nil
}
}
}
func NewReportController(svc reportservice.Serv, reportClient *reportclient.Client, client *policyclient.Client) *ReportController {
return &ReportController{
svc: svc,
reportClient: reportClient,
policyClient: client,
}
}