-
Notifications
You must be signed in to change notification settings - Fork 11
/
store.go
286 lines (245 loc) · 7.59 KB
/
store.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
// Copyright (c) 2016 Pani Networks
// All Rights Reserved.
//
// 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.
//
// Backing store for firewall.
package firewall
import (
"github.com/romana/core/common"
"github.com/romana/core/common/log/trace"
"github.com/romana/core/common/store"
log "github.com/romana/rlog"
"sync"
)
// FirewallStore defines how database should be passed into firewall instance.
type FirewallStore interface {
// GetDb Returns fully initialized DbStore object
GetDb() store.RdbmsStore
// GetMutex return instance of mutex used guard firewall database.
GetMutex() *sync.RWMutex
}
// firewallStore implement FirewallStore
type firewallStore struct {
store.RdbmsStore
mu *sync.RWMutex
}
// Entities implements Entities method of
// Service interface.
func (firewallStore *firewallStore) Entities() []interface{} {
retval := make([]interface{}, 1)
retval[0] = new(IPtablesRule)
return retval
}
// CreateSchemaPostProcess implements common.ServiceStore.CreateSchemaPostProcess()
func (fs firewallStore) CreateSchemaPostProcess() error {
return nil
}
// GetDb implements firewall.FirewallStore
func (fs firewallStore) GetDb() store.DbStore {
return fs.DbStore
}
// GetMutex implements firewall.FirewallStore
func (fs firewallStore) GetMutex() *sync.RWMutex {
return fs.mu
}
// IPtablesRule represents a single iptables rule managed by the agent.
type IPtablesRule struct {
ID uint64 `sql:"AUTO_INCREMENT"`
Body string
State string
}
// GetBody implements FirewallRule interface.
func (r *IPtablesRule) GetBody() string {
return r.Body
}
// GetType implements FirewallRule interface.
func (r *IPtablesRule) GetType() string {
return "iptables"
}
// SetBody implements FirewallRule interface
func (r *IPtablesRule) SetBody(body string) {
r.Body = body
}
func (firewallStore *firewallStore) addIPtablesRule(rule *IPtablesRule) error {
log.Info("Acquiring store mutex for addIPtablesRule")
if rule == nil {
panic("In addIPtablesRule(), received nil rule")
}
firewallStore.mu.Lock()
defer func() {
log.Info("Releasing store mutex for addIPtablesRule")
firewallStore.mu.Unlock()
}()
log.Info("Acquired store mutex for addIPtablesRule")
err := firewallStore.addIPtablesRuleUnsafe(rule)
return err
}
// addIPtablesRuleUnsafe is a non thread safe implementation of addIPtablesRule.
// Unsafe implementation is needed for functions which are already managing same mutex.
func (firewallStore *firewallStore) addIPtablesRuleUnsafe(rule *IPtablesRule) error {
db := firewallStore.DbStore.Db
// db := firewallStore.GetDb()
log.Info("In addIPtablesRule() after GetDb")
if db == nil {
panic("In addIPtablesRule(), db is nil")
}
firewallStore.DbStore.Db.Create(rule)
log.Info("In addIPtablesRule() after Db.Create")
if db.Error != nil {
return db.Error
}
firewallStore.DbStore.Db.NewRecord(*rule)
err := common.MakeMultiError(db.GetErrors())
if err != nil {
return err
}
if db.Error != nil {
return db.Error
}
return nil
}
// listIPtablesRules returns a list of all firewall rules in a database.
func (firewallStore *firewallStore) listIPtablesRules() ([]IPtablesRule, error) {
log.Info("Acquiring store mutex for listIPtablesRules")
firewallStore.mu.Lock()
defer func() {
log.Info("Releasing store mutex for listIPtablesRules")
firewallStore.mu.Unlock()
}()
log.Info("Acquired store mutex for listIPtablesRules")
var iPtablesRule []IPtablesRule
firewallStore.DbStore.Db.Find(&iPtablesRule)
err := common.MakeMultiError(firewallStore.DbStore.Db.GetErrors())
if err != nil {
return nil, err
}
return iPtablesRule, nil
}
// ensureIPtablesRule checks if given rule exists in a database and if not, creates it.
func (firewallStore *firewallStore) ensureIPtablesRule(rule *IPtablesRule) error {
log.Info("Acquiring store mutex for listIPtablesRules")
firewallStore.mu.Lock()
defer func() {
log.Info("Releasing store mutex for listIPtablesRules")
firewallStore.mu.Unlock()
}()
log.Info("Acquired store mutex for listIPtablesRules")
if firewallStore.DbStore.Db.Where("body = ?", rule.Body).First(rule).RecordNotFound() {
log.Tracef(trace.Inside, "In ensureIPtablesRule(), rule %s not found in db - creating", rule.Body)
err0 := firewallStore.addIPtablesRuleUnsafe(rule)
if err0 != nil {
log.Errorf("In ensureIPtablesRule() failed to store rule %v", rule)
return err0
}
} else {
log.Tracef(trace.Inside, "In ensureIPtablesRule(), rule %s already in db - nothing to do", rule.Body)
}
return nil
}
// deleteIPtablesRule deletes firewall rules from database.
func (firewallStore *firewallStore) deleteIPtablesRule(rule *IPtablesRule) error {
log.Info("Acquiring store mutex for deleteIPtablesRule")
firewallStore.mu.Lock()
defer func() {
log.Info("Releasing store mutex for deleteIPtablesRule")
firewallStore.mu.Unlock()
}()
log.Info("Acquired store mutex for deleteIPtablesRule")
db := firewallStore.DbStore.Db
firewallStore.DbStore.Db.Delete(rule)
err := common.MakeMultiError(db.GetErrors())
if err != nil {
return err
}
if db.Error != nil {
return db.Error
}
return nil
}
func (firewallStore *firewallStore) findIPtablesRules(subString string) (*[]IPtablesRule, error) {
log.Info("Acquiring store mutex for findIPtablesRule")
firewallStore.mu.Lock()
defer func() {
log.Info("Releasing store mutex for findIPtablesRule")
firewallStore.mu.Unlock()
}()
log.Info("Acquired store mutex for findIPtablesRule")
var rules []IPtablesRule
db := firewallStore.DbStore.Db
searchString := "%" + subString + "%"
firewallStore.DbStore.Db.Where("body LIKE ?", searchString).Find(&rules)
err := common.MakeMultiError(db.GetErrors())
if err != nil {
return nil, err
}
if db.Error != nil {
return nil, db.Error
}
return &rules, nil
}
// opSwitchIPtables represents action to be taken in switchIPtablesRule
type opSwitchIPtables int
const (
setRuleActive opSwitchIPtables = iota
setRuleInactive
toggleRule
)
func (op opSwitchIPtables) String() string {
var result string
switch op {
case setRuleActive:
result = "active"
case setRuleInactive:
result = "inactive"
case toggleRule:
result = "toggleRule"
}
return result
}
// switchIPtablesRule changes IPtablesRule state.
func (firewallStore *firewallStore) switchIPtablesRule(rule *IPtablesRule, op opSwitchIPtables) error {
// Fast track return if nothing to be done
if rule.State == op.String() {
log.Infof("switchIPtablesRule nothing to be done for %s", rule.State)
return nil
}
log.Info("Acquiring store mutex for switchIPtablesRule")
firewallStore.mu.Lock()
defer func() {
log.Info("Releasing store mutex for switchIPtablesRule")
firewallStore.mu.Unlock()
}()
log.Info("Acquired store mutex for switchIPtablesRule")
// if toggle requested then reverse current state
if op == toggleRule {
if rule.State == setRuleInactive.String() {
rule.State = setRuleActive.String()
} else {
rule.State = setRuleInactive.String()
}
// otherwise just assign op value
} else {
rule.State = op.String()
}
db := firewallStore.DbStore.Db
firewallStore.DbStore.Db.Save(rule)
err := common.MakeMultiError(db.GetErrors())
if err != nil {
return err
}
if db.Error != nil {
return db.Error
}
return nil
}