-
Notifications
You must be signed in to change notification settings - Fork 4
/
subject_mapping.go
299 lines (243 loc) · 10.5 KB
/
subject_mapping.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package subjectmapping
import (
"context"
"fmt"
"log/slog"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
sm "github.com/opentdf/platform/protocol/go/policy/subjectmapping"
"github.com/opentdf/platform/service/internal/logger"
"github.com/opentdf/platform/service/internal/logger/audit"
"github.com/opentdf/platform/service/pkg/db"
"github.com/opentdf/platform/service/pkg/serviceregistry"
policydb "github.com/opentdf/platform/service/policy/db"
)
type SubjectMappingService struct { //nolint:revive // SubjectMappingService is a valid name for this struct
sm.UnimplementedSubjectMappingServiceServer
dbClient policydb.PolicyDBClient
logger *logger.Logger
}
func NewRegistration() serviceregistry.Registration {
return serviceregistry.Registration{
ServiceDesc: &sm.SubjectMappingService_ServiceDesc,
RegisterFunc: func(srp serviceregistry.RegistrationParams) (any, serviceregistry.HandlerServer) {
return &SubjectMappingService{dbClient: policydb.NewClient(srp.DBClient), logger: srp.Logger}, func(ctx context.Context, mux *runtime.ServeMux, s any) error {
server, ok := s.(sm.SubjectMappingServiceServer)
if !ok {
return fmt.Errorf("failed to assert server as sm.SubjectMappingServiceServer")
}
return sm.RegisterSubjectMappingServiceHandlerServer(ctx, mux, server)
}
},
}
}
/* ---------------------------------------------------
* ----------------- SubjectMappings -----------------
* --------------------------------------------------*/
func (s SubjectMappingService) CreateSubjectMapping(ctx context.Context,
req *sm.CreateSubjectMappingRequest,
) (*sm.CreateSubjectMappingResponse, error) {
rsp := &sm.CreateSubjectMappingResponse{}
s.logger.Debug("creating subject mapping")
auditParams := audit.PolicyEventParams{
ActionType: audit.ActionTypeCreate,
ObjectType: audit.ObjectTypeSubjectMapping,
}
sm, err := s.dbClient.CreateSubjectMapping(ctx, req)
if err != nil {
s.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextCreationFailed, slog.String("subjectMapping", req.String()))
}
auditParams.ObjectID = sm.GetId()
s.logger.Audit.PolicyCRUDSuccess(ctx, auditParams)
rsp.SubjectMapping = sm
return rsp, nil
}
func (s SubjectMappingService) ListSubjectMappings(ctx context.Context,
_ *sm.ListSubjectMappingsRequest,
) (*sm.ListSubjectMappingsResponse, error) {
rsp := &sm.ListSubjectMappingsResponse{}
s.logger.Debug("listing subject mappings")
mappings, err := s.dbClient.ListSubjectMappings(ctx)
if err != nil {
return nil, db.StatusifyError(err, db.ErrTextListRetrievalFailed)
}
rsp.SubjectMappings = mappings
return rsp, nil
}
func (s SubjectMappingService) GetSubjectMapping(ctx context.Context,
req *sm.GetSubjectMappingRequest,
) (*sm.GetSubjectMappingResponse, error) {
rsp := &sm.GetSubjectMappingResponse{}
s.logger.Debug("getting subject mapping", slog.String("id", req.GetId()))
mapping, err := s.dbClient.GetSubjectMapping(ctx, req.GetId())
if err != nil {
return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", req.GetId()))
}
rsp.SubjectMapping = mapping
return rsp, nil
}
func (s SubjectMappingService) UpdateSubjectMapping(ctx context.Context,
req *sm.UpdateSubjectMappingRequest,
) (*sm.UpdateSubjectMappingResponse, error) {
rsp := &sm.UpdateSubjectMappingResponse{}
subjectMappingID := req.GetId()
s.logger.Debug("updating subject mapping", slog.String("subjectMapping", req.String()))
auditParams := audit.PolicyEventParams{
ActionType: audit.ActionTypeUpdate,
ObjectType: audit.ObjectTypeSubjectMapping,
ObjectID: subjectMappingID,
}
originalSM, err := s.dbClient.GetSubjectMapping(ctx, subjectMappingID)
if err != nil {
s.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", subjectMappingID))
}
item, err := s.dbClient.UpdateSubjectMapping(ctx, req)
if err != nil {
s.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextUpdateFailed, slog.String("id", req.GetId()), slog.String("subjectMapping fields", req.String()))
}
// UpdateSubjectMapping returns only the ID of the subject mapping so we need
// to fetch the updated subject mapping to compute the diff for audit
updatedSM, err := s.dbClient.GetSubjectMapping(ctx, subjectMappingID)
if err != nil {
s.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", subjectMappingID))
}
auditParams.Original = originalSM
auditParams.Updated = updatedSM
s.logger.Audit.PolicyCRUDSuccess(ctx, auditParams)
rsp.SubjectMapping = item
return rsp, nil
}
func (s SubjectMappingService) DeleteSubjectMapping(ctx context.Context,
req *sm.DeleteSubjectMappingRequest,
) (*sm.DeleteSubjectMappingResponse, error) {
rsp := &sm.DeleteSubjectMappingResponse{}
s.logger.Debug("deleting subject mapping", slog.String("id", req.GetId()))
subjectMappingID := req.GetId()
auditParams := audit.PolicyEventParams{
ActionType: audit.ActionTypeDelete,
ObjectType: audit.ObjectTypeSubjectMapping,
ObjectID: subjectMappingID,
}
sm, err := s.dbClient.DeleteSubjectMapping(ctx, subjectMappingID)
if err != nil {
s.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextDeletionFailed, slog.String("id", subjectMappingID))
}
s.logger.Audit.PolicyCRUDSuccess(ctx, auditParams)
rsp.SubjectMapping = sm
return rsp, nil
}
func (s SubjectMappingService) MatchSubjectMappings(ctx context.Context,
req *sm.MatchSubjectMappingsRequest,
) (*sm.MatchSubjectMappingsResponse, error) {
rsp := &sm.MatchSubjectMappingsResponse{}
s.logger.Debug("matching subject mappings", slog.Any("subjectProperties", req.GetSubjectProperties()))
smList, err := s.dbClient.GetMatchedSubjectMappings(ctx, req.GetSubjectProperties())
if err != nil {
return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.Any("subjectProperties", req.GetSubjectProperties()))
}
rsp.SubjectMappings = smList
return rsp, nil
}
/* --------------------------------------------------------
* ----------------- SubjectConditionSets -----------------
* -------------------------------------------------------*/
func (s SubjectMappingService) GetSubjectConditionSet(ctx context.Context,
req *sm.GetSubjectConditionSetRequest,
) (*sm.GetSubjectConditionSetResponse, error) {
rsp := &sm.GetSubjectConditionSetResponse{}
s.logger.Debug("getting subject condition set", slog.String("id", req.GetId()))
conditionSet, err := s.dbClient.GetSubjectConditionSet(ctx, req.GetId())
if err != nil {
return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", req.GetId()))
}
rsp.SubjectConditionSet = conditionSet
return rsp, nil
}
func (s SubjectMappingService) ListSubjectConditionSets(ctx context.Context,
_ *sm.ListSubjectConditionSetsRequest,
) (*sm.ListSubjectConditionSetsResponse, error) {
rsp := &sm.ListSubjectConditionSetsResponse{}
s.logger.Debug("listing subject condition sets")
conditionSets, err := s.dbClient.ListSubjectConditionSets(ctx)
if err != nil {
return nil, db.StatusifyError(err, db.ErrTextListRetrievalFailed)
}
rsp.SubjectConditionSets = conditionSets
return rsp, nil
}
func (s SubjectMappingService) CreateSubjectConditionSet(ctx context.Context,
req *sm.CreateSubjectConditionSetRequest,
) (*sm.CreateSubjectConditionSetResponse, error) {
rsp := &sm.CreateSubjectConditionSetResponse{}
s.logger.Debug("creating subject condition set", slog.String("subjectConditionSet", req.String()))
auditParams := audit.PolicyEventParams{
ActionType: audit.ActionTypeCreate,
ObjectType: audit.ObjectTypeConditionSet,
}
conditionSet, err := s.dbClient.CreateSubjectConditionSet(ctx, req.GetSubjectConditionSet())
if err != nil {
s.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextCreationFailed, slog.String("subjectConditionSet", req.String()))
}
auditParams.ObjectID = conditionSet.GetId()
s.logger.Audit.PolicyCRUDSuccess(ctx, auditParams)
rsp.SubjectConditionSet = conditionSet
return rsp, nil
}
func (s SubjectMappingService) UpdateSubjectConditionSet(ctx context.Context,
req *sm.UpdateSubjectConditionSetRequest,
) (*sm.UpdateSubjectConditionSetResponse, error) {
rsp := &sm.UpdateSubjectConditionSetResponse{}
s.logger.Debug("updating subject condition set", slog.String("subjectConditionSet", req.String()))
subjectConditionSetID := req.GetId()
auditParams := audit.PolicyEventParams{
ActionType: audit.ActionTypeUpdate,
ObjectType: audit.ObjectTypeConditionSet,
ObjectID: subjectConditionSetID,
}
originalConditionSet, err := s.dbClient.GetSubjectConditionSet(ctx, subjectConditionSetID)
if err != nil {
s.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", subjectConditionSetID))
}
item, err := s.dbClient.UpdateSubjectConditionSet(ctx, req)
if err != nil {
return nil, db.StatusifyError(err, db.ErrTextUpdateFailed, slog.String("id", req.GetId()), slog.String("subjectConditionSet fields", req.String()))
}
// UpdateSubjectConditionSet returns only the ID of the subject condition set so we need
// to fetch the updated subject condition set to compute the diff for audit
updatedConditionSet, err := s.dbClient.GetSubjectConditionSet(ctx, subjectConditionSetID)
if err != nil {
s.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", subjectConditionSetID))
}
auditParams.Original = originalConditionSet
auditParams.Updated = updatedConditionSet
s.logger.Audit.PolicyCRUDSuccess(ctx, auditParams)
rsp.SubjectConditionSet = item
return rsp, nil
}
func (s SubjectMappingService) DeleteSubjectConditionSet(ctx context.Context,
req *sm.DeleteSubjectConditionSetRequest,
) (*sm.DeleteSubjectConditionSetResponse, error) {
rsp := &sm.DeleteSubjectConditionSetResponse{}
s.logger.Debug("deleting subject condition set", slog.String("id", req.GetId()))
conditionSetID := req.GetId()
auditParams := audit.PolicyEventParams{
ActionType: audit.ActionTypeDelete,
ObjectType: audit.ObjectTypeConditionSet,
ObjectID: conditionSetID,
}
conditionSet, err := s.dbClient.DeleteSubjectConditionSet(ctx, conditionSetID)
if err != nil {
s.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextDeletionFailed, slog.String("id", conditionSetID))
}
s.logger.Audit.PolicyCRUDSuccess(ctx, auditParams)
rsp.SubjectConditionSet = conditionSet
return rsp, nil
}