-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
transact_server.go
242 lines (212 loc) · 6.25 KB
/
transact_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
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
package relationtuple
import (
"context"
"encoding/json"
"net/http"
acl "github.com/ory/keto/proto/ory/keto/acl/v1alpha1"
"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/pkg/errors"
)
var _ acl.WriteServiceServer = (*handler)(nil)
func protoTuplesWithAction(deltas []*acl.RelationTupleDelta, action acl.RelationTupleDelta_Action) (filtered []*InternalRelationTuple, err error) {
for _, d := range deltas {
if d.Action == action {
it, err := (&InternalRelationTuple{}).FromDataProvider(d.RelationTuple)
if err != nil {
return nil, err
}
filtered = append(filtered, it)
}
}
return
}
func (h *handler) TransactRelationTuples(ctx context.Context, req *acl.TransactRelationTuplesRequest) (*acl.TransactRelationTuplesResponse, error) {
insertTuples, err := protoTuplesWithAction(req.RelationTupleDeltas, acl.RelationTupleDelta_INSERT)
if err != nil {
return nil, err
}
deleteTuples, err := protoTuplesWithAction(req.RelationTupleDeltas, acl.RelationTupleDelta_DELETE)
if err != nil {
return nil, err
}
err = h.d.RelationTupleManager().TransactRelationTuples(ctx, insertTuples, deleteTuples)
if err != nil {
return nil, err
}
snaptokens := make([]string, len(insertTuples))
for i := range insertTuples {
snaptokens[i] = "not yet implemented"
}
return &acl.TransactRelationTuplesResponse{
Snaptokens: snaptokens,
}, nil
}
// The basic ACL relation tuple
//
// swagger:parameters postCheck createRelationTuple
// nolint:deadcode,unused
type bodyRelationTuple struct {
// in: body
Payload RelationQuery
}
// The basic ACL relation tuple
//
// swagger:parameters getCheck deleteRelationTuple
// nolint:deadcode,unused
type queryRelationTuple struct {
// Namespace of the Relation Tuple
//
// in: query
// required: true
Namespace string `json:"namespace"`
// Object of the Relation Tuple
//
// in: query
// required: true
Object string `json:"object"`
// Relation of the Relation Tuple
//
// in: query
// required: true
Relation string `json:"relation"`
// SubjectID of the Relation Tuple
//
// in: query
// Either subject_set.* or subject_id are required.
SubjectID string `json:"subject_id"`
// Namespace of the Subject Set
//
// in: query
// Either subject_set.* or subject_id are required.
SNamespace string `json:"subject_set.namespace"`
// Object of the Subject Set
//
// in: query
// Either subject_set.* or subject_id are required.
SObject string `json:"subject_set.object"`
// Relation of the Subject Set
//
// in: query
// Either subject_set.* or subject_id are required.
SRelation string `json:"subject_set.relation"`
}
// swagger:route PUT /relation-tuples write createRelationTuple
//
// Create a Relation Tuple
//
// Use this endpoint to create a relation tuple.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 201: RelationQuery
// 400: genericError
// 500: genericError
func (h *handler) createRelation(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var rel InternalRelationTuple
if err := json.NewDecoder(r.Body).Decode(&rel); err != nil {
h.d.Writer().WriteError(w, r, errors.WithStack(herodot.ErrBadRequest.WithError(err.Error())))
return
}
h.d.Logger().WithFields(rel.ToLoggerFields()).Debug("creating relation tuple")
if err := h.d.RelationTupleManager().WriteRelationTuples(r.Context(), &rel); err != nil {
h.d.Logger().WithError(err).WithFields(rel.ToLoggerFields()).Errorf("got an error while creating the relation tuple")
h.d.Writer().WriteError(w, r, err)
return
}
q, err := rel.ToURLQuery()
if err != nil {
h.d.Writer().WriteError(w, r, err)
return
}
h.d.Writer().WriteCreated(w, r, RouteBase+"?"+q.Encode(), &rel)
}
// swagger:route DELETE /relation-tuples write deleteRelationTuple
//
// Delete a Relation Tuple
//
// Use this endpoint to delete a relation tuple.
//
// Consumes:
// - application/x-www-form-urlencoded
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 204: emptyResponse
// 400: genericError
// 500: genericError
func (h *handler) deleteRelation(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
rel, err := (&InternalRelationTuple{}).FromURLQuery(r.URL.Query())
if err != nil {
h.d.Writer().WriteError(w, r, herodot.ErrBadRequest.WithError(err.Error()))
return
}
if err := h.d.RelationTupleManager().DeleteRelationTuples(r.Context(), rel); err != nil {
h.d.Logger().WithError(err).WithFields(rel.ToLoggerFields()).Errorf("got an error while deleting the relation tuple")
h.d.Writer().WriteError(w, r, herodot.ErrInternalServerError.WithError(err.Error()))
return
}
w.WriteHeader(http.StatusNoContent)
}
func internalTuplesWithAction(deltas []*PatchDelta, action patchAction) (filtered []*InternalRelationTuple) {
for _, d := range deltas {
if d.Action == action {
filtered = append(filtered, d.RelationTuple)
}
}
return
}
// swagger:route PATCH /relation-tuples write patchRelationTuples
//
// Patch Multiple Relation Tuples
//
// Use this endpoint to patch one or more relation tuples.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 204: emptyResponse
// 400: genericError
// 404: genericError
// 500: genericError
func (h *handler) patchRelations(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var deltas []*PatchDelta
if err := json.NewDecoder(r.Body).Decode(&deltas); err != nil {
h.d.Writer().WriteError(w, r, herodot.ErrBadRequest.WithError(err.Error()))
return
}
for _, d := range deltas {
if d.RelationTuple == nil {
h.d.Writer().WriteError(w, r, herodot.ErrBadRequest.WithError("relation_tuple is missing"))
return
}
switch d.Action {
case ActionDelete, ActionInsert:
default:
h.d.Writer().WriteError(w, r, herodot.ErrBadRequest.WithError("unknown action "+string(d.Action)))
return
}
}
if err := h.d.RelationTupleManager().TransactRelationTuples(r.Context(), internalTuplesWithAction(deltas, ActionInsert), internalTuplesWithAction(deltas, ActionDelete)); err != nil {
h.d.Writer().WriteError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}