Skip to content

Commit 4cc7bf2

Browse files
authored
Merge pull request #1002 from ellemouton/sql24
[sql-24] firewalldb: thread contexts through for privacy mapper interfaces
2 parents 43a6ce3 + a395cb9 commit 4cc7bf2

18 files changed

+349
-327
lines changed

firewall/privacy_mapper.go

Lines changed: 93 additions & 64 deletions
Large diffs are not rendered by default.

firewall/privacy_mapper_test.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,9 +1073,11 @@ func newMockDB(t *testing.T, preloadRealToPseudo map[string]string,
10731073
db := mockDB{privDB: make(map[string]*mockPrivacyMapDB)}
10741074
sessDB := db.NewSessionDB(sessID)
10751075

1076-
_ = sessDB.Update(func(tx firewalldb.PrivacyMapTx) error {
1076+
_ = sessDB.Update(context.Background(), func(ctx context.Context,
1077+
tx firewalldb.PrivacyMapTx) error {
1078+
10771079
for r, p := range preloadRealToPseudo {
1078-
require.NoError(t, tx.NewPair(r, p))
1080+
require.NoError(t, tx.NewPair(ctx, r, p))
10791081
}
10801082
return nil
10811083
})
@@ -1107,25 +1109,29 @@ type mockPrivacyMapDB struct {
11071109
p2r map[string]string
11081110
}
11091111

1110-
func (m *mockPrivacyMapDB) Update(
1111-
f func(tx firewalldb.PrivacyMapTx) error) error {
1112+
func (m *mockPrivacyMapDB) Update(ctx context.Context,
1113+
f func(ctx context.Context, tx firewalldb.PrivacyMapTx) error) error {
11121114

1113-
return f(m)
1115+
return f(ctx, m)
11141116
}
11151117

1116-
func (m *mockPrivacyMapDB) View(
1117-
f func(tx firewalldb.PrivacyMapTx) error) error {
1118+
func (m *mockPrivacyMapDB) View(ctx context.Context,
1119+
f func(ctx context.Context, tx firewalldb.PrivacyMapTx) error) error {
11181120

1119-
return f(m)
1121+
return f(ctx, m)
11201122
}
11211123

1122-
func (m *mockPrivacyMapDB) NewPair(real, pseudo string) error {
1124+
func (m *mockPrivacyMapDB) NewPair(_ context.Context, real,
1125+
pseudo string) error {
1126+
11231127
m.r2p[real] = pseudo
11241128
m.p2r[pseudo] = real
11251129
return nil
11261130
}
11271131

1128-
func (m *mockPrivacyMapDB) PseudoToReal(pseudo string) (string, error) {
1132+
func (m *mockPrivacyMapDB) PseudoToReal(_ context.Context, pseudo string) (
1133+
string, error) {
1134+
11291135
r, ok := m.p2r[pseudo]
11301136
if !ok {
11311137
return "", firewalldb.ErrNoSuchKeyFound
@@ -1134,7 +1140,9 @@ func (m *mockPrivacyMapDB) PseudoToReal(pseudo string) (string, error) {
11341140
return r, nil
11351141
}
11361142

1137-
func (m *mockPrivacyMapDB) RealToPseudo(real string) (string, error) {
1143+
func (m *mockPrivacyMapDB) RealToPseudo(_ context.Context, real string) (string,
1144+
error) {
1145+
11381146
p, ok := m.r2p[real]
11391147
if !ok {
11401148
return "", firewalldb.ErrNoSuchKeyFound
@@ -1143,8 +1151,8 @@ func (m *mockPrivacyMapDB) RealToPseudo(real string) (string, error) {
11431151
return p, nil
11441152
}
11451153

1146-
func (m *mockPrivacyMapDB) FetchAllPairs() (*firewalldb.PrivacyMapPairs,
1147-
error) {
1154+
func (m *mockPrivacyMapDB) FetchAllPairs(_ context.Context) (
1155+
*firewalldb.PrivacyMapPairs, error) {
11481156

11491157
return firewalldb.NewPrivacyMapPairs(m.r2p), nil
11501158
}

firewall/rule_enforcer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ func (r *RuleEnforcer) initRule(ctx context.Context, reqID uint64, name string,
395395
privMap := r.newPrivMap(session.GroupID)
396396

397397
ruleValues, err = ruleValues.PseudoToReal(
398-
privMap, session.PrivacyFlags,
398+
ctx, privMap, session.PrivacyFlags,
399399
)
400400
if err != nil {
401401
return nil, fmt.Errorf("could not prepare rule "+

firewalldb/kvdb_store.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package firewalldb
2+
3+
import (
4+
"context"
5+
6+
"go.etcd.io/bbolt"
7+
)
8+
9+
// kvdbExecutor is a concrete implementation of the DBExecutor interface that
10+
// uses a bbolt database as its backing store.
11+
type kvdbExecutor[T any] struct {
12+
db *bbolt.DB
13+
wrapTx func(tx *bbolt.Tx) T
14+
}
15+
16+
// Update opens a database read/write transaction and executes the function f
17+
// with the transaction passed as a parameter. After f exits, if f did not
18+
// error, the transaction is committed. Otherwise, if f did error, the
19+
// transaction is rolled back. If the rollback fails, the original error
20+
// returned by f is still returned. If the commit fails, the commit error is
21+
// returned.
22+
//
23+
// NOTE: this is part of the DBExecutor interface.
24+
func (e *kvdbExecutor[T]) Update(ctx context.Context,
25+
fn func(ctx context.Context, tx T) error) error {
26+
27+
return e.db.Update(func(tx *bbolt.Tx) error {
28+
return fn(ctx, e.wrapTx(tx))
29+
})
30+
}
31+
32+
// View opens a database read transaction and executes the function f with the
33+
// transaction passed as a parameter. After f exits, the transaction is rolled
34+
// back. If f errors, its error is returned, not a rollback error (if any
35+
// occur).
36+
//
37+
// NOTE: this is part of the DBExecutor interface.
38+
func (e *kvdbExecutor[T]) View(ctx context.Context,
39+
fn func(ctx context.Context, tx T) error) error {
40+
41+
return e.db.View(func(tx *bbolt.Tx) error {
42+
return fn(ctx, e.wrapTx(tx))
43+
})
44+
}

firewalldb/kvstores.go

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -107,62 +107,28 @@ type RulesDB interface {
107107
func (db *DB) GetKVStores(rule string, groupID session.ID,
108108
feature string) KVStores {
109109

110-
return &kvStores{
111-
db: db.DB,
112-
ruleName: rule,
113-
groupID: groupID,
114-
featureName: feature,
110+
return &kvdbExecutor[KVStoreTx]{
111+
db: db.DB,
112+
wrapTx: func(tx *bbolt.Tx) KVStoreTx {
113+
return &kvStoreTx{
114+
boltTx: tx,
115+
kvStores: &kvStores{
116+
ruleName: rule,
117+
groupID: groupID,
118+
featureName: feature,
119+
},
120+
}
121+
},
115122
}
116123
}
117124

118125
// kvStores implements the rules.KVStores interface.
119126
type kvStores struct {
120-
db *bbolt.DB
121127
ruleName string
122128
groupID session.ID
123129
featureName string
124130
}
125131

126-
// Update opens a database read/write transaction and executes the function f
127-
// with the transaction passed as a parameter. After f exits, if f did not
128-
// error, the transaction is committed. Otherwise, if f did error, the
129-
// transaction is rolled back. If the rollback fails, the original error
130-
// returned by f is still returned. If the commit fails, the commit error is
131-
// returned.
132-
//
133-
// NOTE: this is part of the KVStores interface.
134-
func (s *kvStores) Update(ctx context.Context, fn func(ctx context.Context,
135-
tx KVStoreTx) error) error {
136-
137-
return s.db.Update(func(tx *bbolt.Tx) error {
138-
boltTx := &kvStoreTx{
139-
boltTx: tx,
140-
kvStores: s,
141-
}
142-
143-
return fn(ctx, boltTx)
144-
})
145-
}
146-
147-
// View opens a database read transaction and executes the function f with the
148-
// transaction passed as a parameter. After f exits, the transaction is rolled
149-
// back. If f errors, its error is returned, not a rollback error (if any
150-
// occur).
151-
//
152-
// NOTE: this is part of the KVStores interface.
153-
func (s *kvStores) View(ctx context.Context, fn func(ctx context.Context,
154-
tx KVStoreTx) error) error {
155-
156-
return s.db.View(func(tx *bbolt.Tx) error {
157-
boltTx := &kvStoreTx{
158-
boltTx: tx,
159-
kvStores: s,
160-
}
161-
162-
return fn(ctx, boltTx)
163-
})
164-
}
165-
166132
// getBucketFunc defines the signature of the bucket creation/fetching function
167133
// required by kvStoreTx. If create is true, then all the bucket (and all
168134
// buckets leading up to the bucket) should be created if they do not already

0 commit comments

Comments
 (0)