-
Notifications
You must be signed in to change notification settings - Fork 34
/
handlers_ruletype.go
274 lines (227 loc) · 8.96 KB
/
handlers_ruletype.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
// Copyright 2023 Stacklok, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controlplane
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
"github.com/google/uuid"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine"
"github.com/stacklok/minder/internal/logger"
"github.com/stacklok/minder/internal/ruletypes"
"github.com/stacklok/minder/internal/util"
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)
// ListRuleTypes is a method to list all rule types for a given context
func (s *Server) ListRuleTypes(
ctx context.Context,
_ *minderv1.ListRuleTypesRequest,
) (*minderv1.ListRuleTypesResponse, error) {
entityCtx := engine.EntityFromContext(ctx)
err := entityCtx.ValidateProject(ctx, s.store)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "error in entity context: %v", err)
}
lrt, err := s.store.ListRuleTypesByProject(ctx, entityCtx.Project.ID)
if err != nil {
return nil, status.Errorf(codes.Unknown, "failed to get rule types: %s", err)
}
resp := &minderv1.ListRuleTypesResponse{}
for idx := range lrt {
rt := lrt[idx]
rtpb, err := engine.RuleTypePBFromDB(&rt)
if err != nil {
return nil, fmt.Errorf("cannot convert rule type %s to pb: %v", rt.Name, err)
}
resp.RuleTypes = append(resp.RuleTypes, rtpb)
}
// Telemetry logging
logger.BusinessRecord(ctx).Project = entityCtx.Project.ID
return resp, nil
}
// GetRuleTypeByName is a method to get a rule type by name
func (s *Server) GetRuleTypeByName(
ctx context.Context,
in *minderv1.GetRuleTypeByNameRequest,
) (*minderv1.GetRuleTypeByNameResponse, error) {
entityCtx := engine.EntityFromContext(ctx)
err := entityCtx.ValidateProject(ctx, s.store)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "error in entity context: %v", err)
}
resp := &minderv1.GetRuleTypeByNameResponse{}
rtdb, err := s.store.GetRuleTypeByName(ctx, db.GetRuleTypeByNameParams{
ProjectID: entityCtx.Project.ID,
Name: in.GetName(),
})
if errors.Is(err, sql.ErrNoRows) {
return nil, util.UserVisibleError(codes.NotFound, "rule type %s not found", in.GetName())
} else if err != nil {
return nil, status.Errorf(codes.Unknown, "failed to get rule type: %s", err)
}
rt, err := engine.RuleTypePBFromDB(&rtdb)
if err != nil {
return nil, fmt.Errorf("cannot convert rule type %s to pb: %v", rtdb.Name, err)
}
resp.RuleType = rt
// Telemetry logging
logger.BusinessRecord(ctx).Project = rtdb.ProjectID
logger.BusinessRecord(ctx).RuleType = logger.RuleType{Name: rtdb.Name, ID: rtdb.ID}
return resp, nil
}
// GetRuleTypeById is a method to get a rule type by id
func (s *Server) GetRuleTypeById(
ctx context.Context,
in *minderv1.GetRuleTypeByIdRequest,
) (*minderv1.GetRuleTypeByIdResponse, error) {
entityCtx := engine.EntityFromContext(ctx)
err := entityCtx.ValidateProject(ctx, s.store)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "error in entity context: %v", err)
}
resp := &minderv1.GetRuleTypeByIdResponse{}
parsedRuleTypeID, err := uuid.Parse(in.GetId())
if err != nil {
return nil, util.UserVisibleError(codes.InvalidArgument, "invalid rule type ID")
}
rtdb, err := s.store.GetRuleTypeByID(ctx, parsedRuleTypeID)
if errors.Is(err, sql.ErrNoRows) {
return nil, util.UserVisibleError(codes.NotFound, "rule type %s not found", in.GetId())
} else if err != nil {
return nil, status.Errorf(codes.Unknown, "failed to get rule type: %s", err)
}
rt, err := engine.RuleTypePBFromDB(&rtdb)
if err != nil {
return nil, fmt.Errorf("cannot convert rule type %s to pb: %v", rtdb.Name, err)
}
resp.RuleType = rt
// Telemetry logging
logger.BusinessRecord(ctx).Project = rtdb.ProjectID
logger.BusinessRecord(ctx).RuleType = logger.RuleType{Name: rtdb.Name, ID: rtdb.ID}
return resp, nil
}
// CreateRuleType is a method to create a rule type
func (s *Server) CreateRuleType(
ctx context.Context,
crt *minderv1.CreateRuleTypeRequest,
) (*minderv1.CreateRuleTypeResponse, error) {
entityCtx := engine.EntityFromContext(ctx)
err := entityCtx.ValidateProject(ctx, s.store)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "error in entity context: %v", err)
}
projectID := entityCtx.Project.ID
newRuleType, err := db.WithTransaction(s.store, func(qtx db.ExtendQuerier) (*minderv1.RuleType, error) {
return s.ruleTypes.CreateRuleType(ctx, projectID, uuid.Nil, crt.GetRuleType(), qtx)
})
if err != nil {
if errors.Is(err, ruletypes.ErrRuleTypeInvalid) {
return nil, status.Errorf(codes.InvalidArgument, "invalid rule type definition: %s", err)
} else if errors.Is(err, ruletypes.ErrRuleAlreadyExists) {
return nil, status.Errorf(codes.AlreadyExists, "rule type %s already exists", crt.RuleType.GetName())
}
return nil, status.Errorf(codes.Unknown, "failed to create rule type: %s", err)
}
return &minderv1.CreateRuleTypeResponse{
RuleType: newRuleType,
}, nil
}
// UpdateRuleType is a method to update a rule type
func (s *Server) UpdateRuleType(
ctx context.Context,
urt *minderv1.UpdateRuleTypeRequest,
) (*minderv1.UpdateRuleTypeResponse, error) {
entityCtx := engine.EntityFromContext(ctx)
err := entityCtx.ValidateProject(ctx, s.store)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "error in entity context: %v", err)
}
projectID := entityCtx.Project.ID
updatedRuleType, err := db.WithTransaction(s.store, func(qtx db.ExtendQuerier) (*minderv1.RuleType, error) {
return s.ruleTypes.UpdateRuleType(ctx, projectID, uuid.Nil, urt.GetRuleType(), qtx)
})
if err != nil {
if errors.Is(err, ruletypes.ErrRuleTypeInvalid) {
return nil, status.Errorf(codes.InvalidArgument, "invalid rule type definition: %s", err)
} else if errors.Is(err, ruletypes.ErrRuleNotFound) {
return nil, status.Errorf(codes.NotFound, "rule type %s not found", urt.RuleType.GetName())
}
return nil, status.Errorf(codes.Unknown, "failed to update rule type: %s", err)
}
return &minderv1.UpdateRuleTypeResponse{
RuleType: updatedRuleType,
}, nil
}
// DeleteRuleType is a method to delete a rule type
func (s *Server) DeleteRuleType(
ctx context.Context,
in *minderv1.DeleteRuleTypeRequest,
) (*minderv1.DeleteRuleTypeResponse, error) {
parsedRuleTypeID, err := uuid.Parse(in.GetId())
if err != nil {
return nil, util.UserVisibleError(codes.InvalidArgument, "invalid rule type ID")
}
// first read rule type by id, so we can get provider
rtdb, err := s.store.GetRuleTypeByID(ctx, parsedRuleTypeID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, util.UserVisibleError(codes.NotFound, "rule type %s not found", in.GetId())
}
return nil, status.Errorf(codes.Unknown, "failed to get rule type: %s", err)
}
// TEMPORARY HACK: Since we do not need to support the deletion of bundle
// rule types yet, reject them in the API
// TODO: Move this deletion logic to RuleTypeService
if rtdb.SubscriptionID.Valid {
return nil, status.Errorf(codes.InvalidArgument, "cannot delete rule type from bundle")
}
entityCtx := engine.EntityFromContext(ctx)
err = entityCtx.ValidateProject(ctx, s.store)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "error in entity context: %v", err)
}
profileInfo, err := s.store.ListProfilesInstantiatingRuleType(ctx, rtdb.ID)
// We have profiles that use this rule type, so we can't delete it
if err == nil {
if len(profileInfo) > 0 {
profiles := make([]string, 0, len(profileInfo))
for _, p := range profileInfo {
profiles = append(profiles, p.Name)
}
return nil, util.UserVisibleError(codes.FailedPrecondition,
fmt.Sprintf("cannot delete: rule type %s is used by profiles %s", in.GetId(), strings.Join(profiles, ", ")))
}
} else if !errors.Is(err, sql.ErrNoRows) {
// If we failed for another reason, return an error
return nil, status.Errorf(codes.Unknown, "failed to get profiles: %s", err)
}
// If there are no profiles instantiating this rule type, we can delete it
err = s.store.DeleteRuleType(ctx, parsedRuleTypeID)
if err != nil {
// The rule got deleted in parallel?
if errors.Is(err, sql.ErrNoRows) {
return nil, util.UserVisibleError(codes.NotFound, "rule type %s not found", in.GetId())
}
return nil, status.Errorf(codes.Unknown, "failed to delete rule type: %s", err)
}
// Telemetry logging
logger.BusinessRecord(ctx).Project = rtdb.ProjectID
logger.BusinessRecord(ctx).RuleType = logger.RuleType{Name: rtdb.Name, ID: rtdb.ID}
return &minderv1.DeleteRuleTypeResponse{}, nil
}