-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_sql.go
360 lines (298 loc) · 8.95 KB
/
manager_sql.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright © 2017 Aeneas Rekkas <aeneas+oss@aeneas.io>
//
// 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 sql
import (
"crypto/sha256"
"database/sql"
"encoding/json"
"fmt"
"strings"
"github.com/jmoiron/sqlx"
. "github.com/ory/ladon"
"github.com/ory/ladon/compiler"
"github.com/pkg/errors"
"github.com/rubenv/sql-migrate"
)
// SQLManager is a postgres implementation for Manager to store policies persistently.
type SQLManager struct {
db *sqlx.DB
database string
}
// NewSQLManager initializes a new SQLManager for given db instance.
func NewSQLManager(db *sqlx.DB, schema []string) *SQLManager {
database := db.DriverName()
switch database {
case "pgx", "pq":
database = "postgres"
}
return &SQLManager{
db: db,
database: database,
}
}
// CreateSchemas creates ladon_policy tables
func (s *SQLManager) CreateSchemas(schema, table string) (int, error) {
if _, ok := Migrations[s.database]; !ok {
return 0, errors.Errorf("Database %s is not supported", s.database)
}
source := Migrations[s.database].Migrations
migrate.SetSchema(schema)
migrate.SetTable(table)
n, err := migrate.Exec(s.db.DB, s.database, source, migrate.Up)
if err != nil {
return 0, errors.Wrapf(err, "Could not migrate sql schema, applied %d migrations", n)
}
return n, nil
}
// Update updates an existing policy.
func (s *SQLManager) Update(policy Policy) error {
tx, err := s.db.Beginx()
if err != nil {
return errors.WithStack(err)
}
if err := s.delete(policy.GetID(), tx); err != nil {
if rollErr := tx.Rollback(); rollErr != nil {
return errors.Wrap(err, rollErr.Error())
}
return errors.WithStack(err)
}
if err := s.create(policy, tx); err != nil {
if rollErr := tx.Rollback(); rollErr != nil {
return errors.Wrap(err, rollErr.Error())
}
return errors.WithStack(err)
}
if err = tx.Commit(); err != nil {
if rollErr := tx.Rollback(); rollErr != nil {
return errors.Wrap(err, rollErr.Error())
}
return errors.WithStack(err)
}
return nil
}
// Create inserts a new policy
func (s *SQLManager) Create(policy Policy) (err error) {
tx, err := s.db.Beginx()
if err != nil {
return errors.WithStack(err)
}
if err := s.create(policy, tx); err != nil {
if rollErr := tx.Rollback(); rollErr != nil {
return errors.Wrap(err, rollErr.Error())
}
return errors.WithStack(err)
}
if err = tx.Commit(); err != nil {
if rollErr := tx.Rollback(); rollErr != nil {
return errors.Wrap(err, rollErr.Error())
}
return errors.WithStack(err)
}
return nil
}
func (s *SQLManager) create(policy Policy, tx *sqlx.Tx) (err error) {
conditions := []byte("{}")
if policy.GetConditions() != nil {
cs := policy.GetConditions()
conditions, err = json.Marshal(&cs)
if err != nil {
return errors.WithStack(err)
}
}
if _, ok := Migrations[s.database]; !ok {
return errors.Errorf("Database %s is not supported", s.database)
}
if _, err = tx.Exec(s.db.Rebind(Migrations[s.database].QueryInsertPolicy), policy.GetID(), policy.GetDescription(), policy.GetEffect(), conditions); err != nil {
return errors.WithStack(err)
}
type relation struct {
p []string
t string
}
var relations = []relation{
{p: policy.GetActions(), t: "action"},
{p: policy.GetResources(), t: "resource"},
{p: policy.GetSubjects(), t: "subject"},
}
for _, rel := range relations {
var query string
var queryRel string
switch rel.t {
case "action":
query = Migrations[s.database].QueryInsertPolicyActions
queryRel = Migrations[s.database].QueryInsertPolicyActionsRel
case "resource":
query = Migrations[s.database].QueryInsertPolicyResources
queryRel = Migrations[s.database].QueryInsertPolicyResourcesRel
case "subject":
query = Migrations[s.database].QueryInsertPolicySubjects
queryRel = Migrations[s.database].QueryInsertPolicySubjectsRel
}
for _, template := range rel.p {
h := sha256.New()
h.Write([]byte(template))
id := fmt.Sprintf("%x", h.Sum(nil))
compiled, err := compiler.CompileRegex(template, policy.GetStartDelimiter(), policy.GetEndDelimiter())
if err != nil {
return errors.WithStack(err)
}
if _, err := tx.Exec(s.db.Rebind(query), id, template, compiled.String(), strings.Index(template, string(policy.GetStartDelimiter())) >= -1); err != nil {
return errors.WithStack(err)
}
if _, err := tx.Exec(s.db.Rebind(queryRel), policy.GetID(), id); err != nil {
return errors.WithStack(err)
}
}
}
return nil
}
func (s *SQLManager) FindRequestCandidates(r *Request) (Policies, error) {
query := Migrations[s.database].QueryRequestCandidates
rows, err := s.db.Query(s.db.Rebind(query), r.Subject, r.Subject)
if err == sql.ErrNoRows {
return nil, NewErrResourceNotFound(err)
} else if err != nil {
return nil, errors.WithStack(err)
}
defer rows.Close()
return scanRows(rows)
}
func scanRows(rows *sql.Rows) (Policies, error) {
var policies = map[string]*DefaultPolicy{}
for rows.Next() {
var p DefaultPolicy
var conditions []byte
var resource, subject, action sql.NullString
p.Actions = []string{}
p.Subjects = []string{}
p.Resources = []string{}
if err := rows.Scan(&p.ID, &p.Effect, &conditions, &p.Description, &subject, &resource, &action); err == sql.ErrNoRows {
return nil, NewErrResourceNotFound(err)
} else if err != nil {
return nil, errors.WithStack(err)
}
p.Conditions = Conditions{}
if err := json.Unmarshal(conditions, &p.Conditions); err != nil {
return nil, errors.WithStack(err)
}
if c, ok := policies[p.ID]; ok {
if action.Valid {
policies[p.ID].Actions = append(c.Actions, action.String)
}
if subject.Valid {
policies[p.ID].Subjects = append(c.Subjects, subject.String)
}
if resource.Valid {
policies[p.ID].Resources = append(c.Resources, resource.String)
}
} else {
if action.Valid {
p.Actions = []string{action.String}
}
if subject.Valid {
p.Subjects = []string{subject.String}
}
if resource.Valid {
p.Resources = []string{resource.String}
}
policies[p.ID] = &p
}
}
var result = make(Policies, len(policies))
var count int
for _, v := range policies {
v.Actions = uniq(v.Actions)
v.Resources = uniq(v.Resources)
v.Subjects = uniq(v.Subjects)
result[count] = v
count++
}
return result, nil
}
var getQuery = `SELECT
p.id, p.effect, p.conditions, p.description,
subject.template as subject, resource.template as resource, action.template as action
FROM
ladon_policy as p
LEFT JOIN ladon_policy_subject_rel as rs ON rs.policy = p.id
LEFT JOIN ladon_policy_action_rel as ra ON ra.policy = p.id
LEFT JOIN ladon_policy_resource_rel as rr ON rr.policy = p.id
LEFT JOIN ladon_subject as subject ON rs.subject = subject.id
LEFT JOIN ladon_action as action ON ra.action = action.id
LEFT JOIN ladon_resource as resource ON rr.resource = resource.id
`
// GetAll returns all policies
func (s *SQLManager) GetAll(limit, offset int64) (Policies, error) {
query := s.db.Rebind(getQuery + "ORDER BY p.id LIMIT ? OFFSET ?")
rows, err := s.db.Query(query, limit, offset)
if err != nil {
return nil, errors.WithStack(err)
}
defer rows.Close()
return scanRows(rows)
}
// Get retrieves a policy.
func (s *SQLManager) Get(id string) (Policy, error) {
query := s.db.Rebind(getQuery + "WHERE p.id=?")
rows, err := s.db.Query(query, id)
if err == sql.ErrNoRows {
return nil, NewErrResourceNotFound(err)
} else if err != nil {
return nil, errors.WithStack(err)
}
defer rows.Close()
policies, err := scanRows(rows)
if err != nil {
return nil, errors.WithStack(err)
} else if len(policies) == 0 {
return nil, NewErrResourceNotFound(sql.ErrNoRows)
}
return policies[0], nil
}
// Delete removes a policy.
func (s *SQLManager) Delete(id string) error {
tx, err := s.db.Beginx()
if err != nil {
return errors.WithStack(err)
}
if err := s.delete(id, tx); err != nil {
if rollErr := tx.Rollback(); rollErr != nil {
return errors.Wrap(err, rollErr.Error())
}
return errors.WithStack(err)
}
if err = tx.Commit(); err != nil {
if rollErr := tx.Rollback(); rollErr != nil {
return errors.Wrap(err, rollErr.Error())
}
return errors.WithStack(err)
}
return nil
}
// Delete removes a policy.
func (s *SQLManager) delete(id string, tx *sqlx.Tx) error {
_, err := tx.Exec(s.db.Rebind("DELETE FROM ladon_policy WHERE id=?"), id)
return errors.WithStack(err)
}
func uniq(input []string) []string {
u := make([]string, 0, len(input))
m := make(map[string]bool)
for _, val := range input {
if _, ok := m[val]; !ok {
m[val] = true
u = append(u, val)
}
}
return u
}