/
delete.go
242 lines (189 loc) · 8.97 KB
/
delete.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 repo
import (
"context"
"fmt"
"strings"
"github.com/kyma-incubator/compass/components/director/pkg/log"
"github.com/kyma-incubator/compass/components/director/pkg/resource"
"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
"github.com/kyma-incubator/compass/components/director/pkg/persistence"
"github.com/pkg/errors"
)
// Deleter is an interface for deleting tenant scoped entities with either externally managed tenant accesses (m2m table or view) or embedded tenant in them.
type Deleter interface {
DeleteOne(ctx context.Context, resourceType resource.Type, tenant string, conditions Conditions) error
DeleteMany(ctx context.Context, resourceType resource.Type, tenant string, conditions Conditions) error
}
// DeleterGlobal is an interface for deleting global entities.
type DeleterGlobal interface {
DeleteOneGlobal(ctx context.Context, conditions Conditions) error
DeleteManyGlobal(ctx context.Context, conditions Conditions) error
}
// DeleterConditionTree deletes tenant scoped entities matching the provided condition tree with tenant isolation.
type DeleterConditionTree interface {
DeleteConditionTree(ctx context.Context, resourceType resource.Type, tenant string, conditionTree *ConditionTree) error
}
type universalDeleter struct {
tableName string
resourceType resource.Type
tenantColumn *string
}
// NewDeleter is a constructor for Deleter about entities with externally managed tenant accesses (m2m table or view)
func NewDeleter(tableName string) Deleter {
return &universalDeleter{tableName: tableName}
}
// NewDeleterConditionTree is a constructor for Deleter about entities with externally managed tenant accesses (m2m table or view)
func NewDeleterConditionTree(tableName string) DeleterConditionTree {
return &universalDeleter{tableName: tableName}
}
// NewDeleterConditionTreeWithEmbeddedTenant is a constructor for Deleter about entities with externally managed tenant accesses (m2m table or view)
func NewDeleterConditionTreeWithEmbeddedTenant(tableName string, tenantColumn string) DeleterConditionTree {
return &universalDeleter{tableName: tableName, tenantColumn: &tenantColumn}
}
// NewDeleterWithEmbeddedTenant is a constructor for Deleter about entities with tenant embedded in them.
func NewDeleterWithEmbeddedTenant(tableName string, tenantColumn string) Deleter {
return &universalDeleter{tableName: tableName, tenantColumn: &tenantColumn}
}
// NewDeleterGlobal is a constructor for DeleterGlobal about global entities.
func NewDeleterGlobal(resourceType resource.Type, tableName string) DeleterGlobal {
return &universalDeleter{tableName: tableName, resourceType: resourceType}
}
// DeleteOne deletes exactly one entity from the database if the calling tenant has owner access to it. It returns an error if more than one entity matches the provided conditions.
// If the tenantColumn is configured the isolation is based on equal condition on tenantColumn.
func (g *universalDeleter) DeleteOne(ctx context.Context, resourceType resource.Type, tenant string, conditions Conditions) error {
if tenant == "" {
return apperrors.NewTenantRequiredError()
}
if g.tenantColumn != nil {
conditions = append(Conditions{NewEqualCondition(*g.tenantColumn, tenant)}, conditions...)
return g.delete(ctx, resourceType, conditions, true)
}
tenantIsolation, err := NewTenantIsolationCondition(resourceType, tenant, true)
if err != nil {
return err
}
conditions = append(conditions, tenantIsolation)
return g.delete(ctx, resourceType, conditions, true)
}
// DeleteMany deletes all the entities that match the provided conditions from the database if the calling tenant has owner access to them.
// If the tenantColumn is configured the isolation is based on equal condition on tenantColumn.
func (g *universalDeleter) DeleteMany(ctx context.Context, resourceType resource.Type, tenant string, conditions Conditions) error {
if tenant == "" {
return apperrors.NewTenantRequiredError()
}
if g.tenantColumn != nil {
conditions = append(Conditions{NewEqualCondition(*g.tenantColumn, tenant)}, conditions...)
return g.delete(ctx, resourceType, conditions, false)
}
tenantIsolation, err := NewTenantIsolationCondition(resourceType, tenant, true)
if err != nil {
return err
}
conditions = append(conditions, tenantIsolation)
return g.delete(ctx, resourceType, conditions, false)
}
// DeleteOneGlobal deletes exactly one entity from the database. It returns an error if more than one entity matches the provided conditions.
func (g *universalDeleter) DeleteOneGlobal(ctx context.Context, conditions Conditions) error {
return g.delete(ctx, g.resourceType, conditions, true)
}
// DeleteManyGlobal deletes all the entities that match the provided conditions from the database.
func (g *universalDeleter) DeleteManyGlobal(ctx context.Context, conditions Conditions) error {
return g.delete(ctx, g.resourceType, conditions, false)
}
// DeleteConditionTree lists tenant scoped entities matching the provided condition tree with tenant isolation.
// If the tenantColumn is configured the isolation is based on equal condition on tenantColumn.
// If the tenantColumn is not configured an entity with externally managed tenant accesses in m2m table / view is assumed.
func (g *universalDeleter) DeleteConditionTree(ctx context.Context, resourceType resource.Type, tenant string, conditionTree *ConditionTree) error {
return g.treeConditionDeleterWithTenantScope(ctx, resourceType, tenant, NoLock, conditionTree)
}
func (g *universalDeleter) treeConditionDeleterWithTenantScope(ctx context.Context, resourceType resource.Type, tenant string, lockClause string, conditionTree *ConditionTree) error {
if tenant == "" {
return apperrors.NewTenantRequiredError()
}
if g.tenantColumn != nil {
conditions := And(&ConditionTree{Operand: NewEqualCondition(*g.tenantColumn, tenant)}, conditionTree)
return g.deleteWithConditionTree(ctx, resourceType, lockClause, conditions)
}
tenantIsolation, err := NewTenantIsolationCondition(resourceType, tenant, true)
if err != nil {
return err
}
conditions := And(&ConditionTree{Operand: tenantIsolation}, conditionTree)
return g.deleteWithConditionTree(ctx, resourceType, lockClause, conditions)
}
func (g *universalDeleter) deleteWithConditionTree(ctx context.Context, resourceType resource.Type, lockClause string, conditionTree *ConditionTree) error {
persist, err := persistence.FromCtx(ctx)
if err != nil {
return err
}
query, args := buildDeleteQueryFromTree(g.tableName, conditionTree, lockClause, true)
log.C(ctx).Debugf("Executing DB query: %s", query)
_, err = persist.ExecContext(ctx, query, args...)
return persistence.MapSQLError(ctx, err, resourceType, resource.List, "while fetching list of objects from '%s' table", g.tableName)
}
func buildDeleteQueryFromTree(tableName string, conditions *ConditionTree, lockClause string, isRebindingNeeded bool) (string, []interface{}) {
var stmtBuilder strings.Builder
stmtBuilder.WriteString(fmt.Sprintf("DELETE FROM %s", tableName))
var allArgs []interface{}
if conditions != nil {
stmtBuilder.WriteString(" WHERE ")
var subquery string
subquery, allArgs = conditions.BuildSubquery()
stmtBuilder.WriteString(subquery)
}
writeLockClause(&stmtBuilder, lockClause)
if isRebindingNeeded {
return getQueryFromBuilder(stmtBuilder), allArgs
}
return stmtBuilder.String(), allArgs
}
func (g *universalDeleter) delete(ctx context.Context, resourceType resource.Type, conditions Conditions, requireSingleRemoval bool) error {
persist, err := persistence.FromCtx(ctx)
if err != nil {
return err
}
var stmtBuilder strings.Builder
stmtBuilder.WriteString(fmt.Sprintf("DELETE FROM %s", g.tableName))
if len(conditions) > 0 {
stmtBuilder.WriteString(" WHERE")
}
err = writeEnumeratedConditions(&stmtBuilder, conditions)
if err != nil {
return errors.Wrap(err, "while writing enumerated conditions")
}
allArgs := getAllArgs(conditions)
query := getQueryFromBuilder(stmtBuilder)
log.C(ctx).Debugf("Executing DB query: %s", query)
res, err := persist.ExecContext(ctx, query, allArgs...)
if err = persistence.MapSQLError(ctx, err, resourceType, resource.Delete, "while deleting object from '%s' table", g.tableName); err != nil {
return err
}
isTenantScopedDelete := g.tenantColumn != nil
if requireSingleRemoval {
affected, err := res.RowsAffected()
if err != nil {
return errors.Wrap(err, "while checking affected rows")
}
if (affected == 0 && hasTenantIsolationCondition(conditions)) || (affected == 0 && isTenantScopedDelete) {
return apperrors.NewUnauthorizedError(apperrors.ShouldBeOwnerMsg)
}
if affected != 1 {
return apperrors.NewInternalError("delete should remove single row, but removed %d rows", affected)
}
}
return nil
}
func hasTenantIsolationCondition(conditions Conditions) bool {
for _, cond := range conditions {
if _, ok := cond.(*tenantIsolationCondition); ok {
return true
}
}
return false
}
// IDs keeps IDs retrieved from the Compass storage.
type IDs []string
// Len returns the length of the IDs
func (i IDs) Len() int {
return len(i)
}