-
Notifications
You must be signed in to change notification settings - Fork 167
/
manager_sql_migration_0_5_to_0_6.go
170 lines (147 loc) · 5.02 KB
/
manager_sql_migration_0_5_to_0_6.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
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package ladon_manager
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"github.com/jmoiron/sqlx"
. "github.com/ory/ladon"
"github.com/ory/ladon/compiler"
"github.com/pkg/errors"
)
type SQLManagerMigrateFromMajor0Minor6ToMajor0Minor7 struct {
DB *sqlx.DB
SQLManager *SQLManager
}
func (s *SQLManagerMigrateFromMajor0Minor6ToMajor0Minor7) GetManager() Manager {
return s.SQLManager
}
// Migrate retrieves a policy.
func (s *SQLManagerMigrateFromMajor0Minor6ToMajor0Minor7) Migrate() error {
rows, err := s.DB.Query(s.DB.Rebind("SELECT id, description, effect, conditions FROM ladon_policy"))
if err != nil {
return errors.WithStack(err)
}
defer rows.Close()
var pols = Policies{}
for rows.Next() {
var p DefaultPolicy
var conditions []byte
if err := rows.Scan(&p.ID, &p.Description, &p.Effect, &conditions); err != nil {
return errors.WithStack(err)
}
p.Conditions = Conditions{}
if err := json.Unmarshal(conditions, &p.Conditions); err != nil {
return errors.WithStack(err)
}
subjects, err := getLinkedSQL(s.DB, "ladon_policy_subject", p.GetID())
if err != nil {
return errors.WithStack(err)
}
permissions, err := getLinkedSQL(s.DB, "ladon_policy_permission", p.GetID())
if err != nil {
return errors.WithStack(err)
}
resources, err := getLinkedSQL(s.DB, "ladon_policy_resource", p.GetID())
if err != nil {
return errors.WithStack(err)
}
log.Printf("[DEBUG] Found policy %s", p.GetID())
p.Actions = permissions
p.Subjects = subjects
p.Resources = resources
pols = append(pols, &p)
}
log.Printf("[DEBUG] Found %d policies, migrating", len(pols))
for _, p := range pols {
log.Printf("[DEBUG] Inserting policy %s", p.GetID())
if err := s.SQLManager.Create(p); err != nil {
log.Printf("[DEBUG] Unable to insert policy %s: %s", p.GetID(), err)
return errors.WithStack(err)
}
}
log.Printf("[DEBUG] Migrated %d policies successfully", len(pols))
return nil
}
func getLinkedSQL(db *sqlx.DB, table, policy string) ([]string, error) {
urns := []string{}
rows, err := db.Query(db.Rebind(fmt.Sprintf("SELECT template FROM %s WHERE policy=?", table)), policy)
if err == sql.ErrNoRows {
return nil, errors.Wrap(ErrNotFound, "")
} else if err != nil {
return nil, errors.WithStack(err)
}
defer rows.Close()
for rows.Next() {
var urn string
if err = rows.Scan(&urn); err != nil {
return []string{}, errors.WithStack(err)
}
urns = append(urns, urn)
}
return urns, nil
}
// Create inserts a new policy
func (s *SQLManagerMigrateFromMajor0Minor6ToMajor0Minor7) Create(policy Policy) (err error) {
conditions := []byte("{}")
if policy.GetConditions() != nil {
cs := policy.GetConditions()
conditions, err = json.Marshal(&cs)
if err != nil {
return errors.WithStack(err)
}
}
if tx, err := s.DB.Begin(); err != nil {
return errors.WithStack(err)
} else if _, err = tx.Exec(s.DB.Rebind("INSERT INTO ladon_policy (id, description, effect, conditions) VALUES (?, ?, ?, ?)"), policy.GetID(), policy.GetDescription(), policy.GetEffect(), conditions); err != nil {
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(err)
} else if err = createLinkSQL(s.DB, tx, "ladon_policy_subject", policy, policy.GetSubjects()); err != nil {
return err
} else if err = createLinkSQL(s.DB, tx, "ladon_policy_permission", policy, policy.GetActions()); err != nil {
return err
} else if err = createLinkSQL(s.DB, tx, "ladon_policy_resource", policy, policy.GetResources()); err != nil {
return err
} else if err = tx.Commit(); err != nil {
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(err)
}
return nil
}
func createLinkSQL(db *sqlx.DB, tx *sql.Tx, table string, p Policy, templates []string) error {
for _, template := range templates {
reg, _ := compiler.CompileRegex(template, p.GetStartDelimiter(), p.GetEndDelimiter())
// Execute SQL statement
query := db.Rebind(fmt.Sprintf("INSERT INTO %s (policy, template, compiled) VALUES (?, ?, ?)", table))
if _, err := tx.Exec(query, p.GetID(), template, reg.String()); err != nil {
if rb := tx.Rollback(); rb != nil {
return errors.WithStack(rb)
}
return errors.WithStack(err)
}
}
return nil
}