Skip to content

Commit 9abe0ab

Browse files
authored
Merge pull request #1043 from ellemouton/sql31
[sql-31] firewalldb: Privacy Mapper schemas, queries and CRUD
2 parents b0230d4 + b9ad664 commit 9abe0ab

19 files changed

+381
-47
lines changed

db/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
// daemon.
2323
//
2424
// NOTE: This MUST be updated when a new migration is added.
25-
LatestMigrationVersion = 3
25+
LatestMigrationVersion = 4
2626
)
2727

2828
// MigrationTarget is a functional option that can be passed to applyMigrations
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DROP INDEX IF EXISTS privacy_pairs_group_id_idx;
2+
DROP INDEX IF EXISTS privacy_pairs_unique_real;
3+
DROP INDEX IF EXISTS privacy_pairs_unique_pseudo;
4+
DROP TABLE IF EXISTS privacy_pairs;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- privacy_pairs stores the privacy map pairs for a given session group.
2+
CREATE TABLE IF NOT EXISTS privacy_pairs (
3+
-- The group ID of the session that this privacy pair is associated
4+
-- with.
5+
group_id BIGINT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
6+
7+
-- The real value of the privacy pair.
8+
real_val TEXT NOT NULL,
9+
10+
-- The pseudo value of the privacy pair.
11+
pseudo_val TEXT NOT NULL
12+
);
13+
14+
-- There should be no duplicate real values for a given group ID.
15+
CREATE UNIQUE INDEX privacy_pairs_unique_real ON privacy_pairs (
16+
group_id, real_val
17+
);
18+
19+
-- There should be no duplicate pseudo values for a given group ID.
20+
CREATE UNIQUE INDEX privacy_pairs_unique_pseudo ON privacy_pairs (
21+
group_id, pseudo_val
22+
);
23+

db/sqlc/models.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/sqlc/privacy_paris.sql.go

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/sqlc/querier.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/sqlc/queries/privacy_paris.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- name: InsertPrivacyPair :exec
2+
INSERT INTO privacy_pairs (group_id, real_val, pseudo_val)
3+
VALUES ($1, $2, $3);
4+
5+
-- name: GetRealForPseudo :one
6+
SELECT real_val
7+
FROM privacy_pairs
8+
WHERE group_id = $1 AND pseudo_val = $2;
9+
10+
-- name: GetPseudoForReal :one
11+
SELECT pseudo_val
12+
FROM privacy_pairs
13+
WHERE group_id = $1 AND real_val = $2;
14+
15+
-- name: GetAllPrivacyPairs :many
16+
SELECT real_val, pseudo_val
17+
FROM privacy_pairs
18+
WHERE group_id = $1;

firewall/privacy_mapper.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ var _ mid.RequestInterceptor = (*PrivacyMapper)(nil)
6060
// PrivacyMapper is a RequestInterceptor that maps any pseudo names in certain
6161
// requests to their real values and vice versa for responses.
6262
type PrivacyMapper struct {
63-
newDB firewalldb.NewPrivacyMapDB
63+
db firewalldb.PrivacyMapper
6464
randIntn func(int) (int, error)
6565
sessionDB firewalldb.SessionDB
6666
}
6767

6868
// NewPrivacyMapper returns a new instance of PrivacyMapper. The randIntn
6969
// function is used to draw randomness for request field obfuscation.
70-
func NewPrivacyMapper(newDB firewalldb.NewPrivacyMapDB,
70+
func NewPrivacyMapper(newDB firewalldb.PrivacyMapper,
7171
randIntn func(int) (int, error),
7272
sessionDB firewalldb.SessionDB) *PrivacyMapper {
7373

7474
return &PrivacyMapper{
75-
newDB: newDB,
75+
db: newDB,
7676
randIntn: randIntn,
7777
sessionDB: sessionDB,
7878
}
@@ -195,7 +195,7 @@ func (p *PrivacyMapper) checkAndReplaceIncomingRequest(ctx context.Context,
195195
return nil, err
196196
}
197197

198-
db := p.newDB(session.GroupID)
198+
db := p.db.PrivacyDB(session.GroupID)
199199

200200
// If we don't have a handler for the URI, we don't allow the request
201201
// to go through.
@@ -225,7 +225,7 @@ func (p *PrivacyMapper) replaceOutgoingResponse(ctx context.Context, uri string,
225225
return nil, err
226226
}
227227

228-
db := p.newDB(session.GroupID)
228+
db := p.db.PrivacyDB(session.GroupID)
229229

230230
// If we don't have a handler for the URI, we don't allow the response
231231
// to go to avoid accidental leaks.

firewall/privacy_mapper_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ func TestPrivacyMapper(t *testing.T) {
902902

903903
// randIntn is used for deterministic testing.
904904
randIntn := func(n int) (int, error) { return 100, nil }
905-
p := NewPrivacyMapper(db.NewSessionDB, randIntn, pd)
905+
p := NewPrivacyMapper(db, randIntn, pd)
906906

907907
rawMsg, err := proto.Marshal(test.msg)
908908
require.NoError(t, err)
@@ -978,7 +978,7 @@ func TestPrivacyMapper(t *testing.T) {
978978
rawMsg, err := proto.Marshal(msg)
979979
require.NoError(t, err)
980980

981-
p := NewPrivacyMapper(db.NewSessionDB, CryptoRandIntn, pd)
981+
p := NewPrivacyMapper(db, CryptoRandIntn, pd)
982982
require.NoError(t, err)
983983

984984
// We test the independent outgoing amount (incoming amount
@@ -1071,7 +1071,7 @@ func newMockDB(t *testing.T, preloadRealToPseudo map[string]string,
10711071
sessID session.ID) mockDB {
10721072

10731073
db := mockDB{privDB: make(map[string]*mockPrivacyMapDB)}
1074-
sessDB := db.NewSessionDB(sessID)
1074+
sessDB := db.PrivacyDB(sessID)
10751075

10761076
_ = sessDB.Update(context.Background(), func(ctx context.Context,
10771077
tx firewalldb.PrivacyMapTx) error {
@@ -1085,14 +1085,14 @@ func newMockDB(t *testing.T, preloadRealToPseudo map[string]string,
10851085
return db
10861086
}
10871087

1088-
func (m mockDB) NewSessionDB(sessionID session.ID) firewalldb.PrivacyMapDB {
1089-
db, ok := m.privDB[string(sessionID[:])]
1088+
func (m mockDB) PrivacyDB(groupID session.ID) firewalldb.PrivacyMapDB {
1089+
db, ok := m.privDB[string(groupID[:])]
10901090
if ok {
10911091
return db
10921092
}
10931093

10941094
newDB := newMockPrivacyMapDB()
1095-
m.privDB[string(sessionID[:])] = newDB
1095+
m.privDB[string(groupID[:])] = newDB
10961096

10971097
return newDB
10981098
}

firewall/rule_enforcer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type RuleEnforcer struct {
3333
actionsDB firewalldb.ActionReadDBGetter
3434
sessionDB firewalldb.SessionDB
3535
markActionErrored func(reqID uint64, reason string) error
36-
newPrivMap firewalldb.NewPrivacyMapDB
36+
privMapDB firewalldb.PrivacyMapper
3737

3838
permsMgr *perms.Manager
3939
getFeaturePerms featurePerms
@@ -64,7 +64,7 @@ func NewRuleEnforcer(ruleDB firewalldb.RulesDB,
6464
lndClient lndclient.LightningClient, lndConnID string,
6565
ruleMgrs rules.ManagerSet,
6666
markActionErrored func(reqID uint64, reason string) error,
67-
privMap firewalldb.NewPrivacyMapDB) *RuleEnforcer {
67+
privMap firewalldb.PrivacyMapper) *RuleEnforcer {
6868

6969
return &RuleEnforcer{
7070
ruleDB: ruleDB,
@@ -76,7 +76,7 @@ func NewRuleEnforcer(ruleDB firewalldb.RulesDB,
7676
lndClient: lndClient,
7777
ruleMgrs: ruleMgrs,
7878
markActionErrored: markActionErrored,
79-
newPrivMap: privMap,
79+
privMapDB: privMap,
8080
sessionDB: sessionIDIndex,
8181
lndConnID: lndConnID,
8282
}
@@ -392,7 +392,7 @@ func (r *RuleEnforcer) initRule(ctx context.Context, reqID uint64, name string,
392392
}
393393

394394
if privacy {
395-
privMap := r.newPrivMap(session.GroupID)
395+
privMap := r.privMapDB.PrivacyDB(session.GroupID)
396396

397397
ruleValues, err = ruleValues.PseudoToReal(
398398
ctx, privMap, session.PrivacyFlags,

0 commit comments

Comments
 (0)