Skip to content

Commit 4a5259e

Browse files
committed
firewalldb: best effort tight coupling of actions and sessions
In this commit, we do our best to ensure that at least at the time of action creation, if the session ID is set, then our bbolt actions store impl will at least first check that the session really does exist. This also forces us to update our tests in preparation for the SQL store which will tightly couple the actions and sessions.
1 parent a513aae commit 4a5259e

File tree

3 files changed

+142
-87
lines changed

3 files changed

+142
-87
lines changed

firewalldb/actions_kvdb.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,21 @@ var (
5454
)
5555

5656
// AddAction serialises and adds an Action to the DB under the given sessionID.
57-
func (db *BoltDB) AddAction(_ context.Context,
57+
func (db *BoltDB) AddAction(ctx context.Context,
5858
req *AddActionReq) (ActionLocator, error) {
5959

60+
// If the new action links to a session, the session must exist.
61+
// For the bbolt impl of the store, this is our best effort attempt
62+
// at ensuring each action links to a session. If the session is
63+
// deleted later on, however, then the action will still exist.
64+
var err error
65+
req.SessionID.WhenSome(func(id session.ID) {
66+
_, err = db.sessionIDIndex.GetSession(ctx, id)
67+
})
68+
if err != nil {
69+
return nil, err
70+
}
71+
6072
action := &Action{
6173
AddActionReq: *req,
6274
AttemptedAt: db.clock.Now().UTC(),
@@ -69,7 +81,7 @@ func (db *BoltDB) AddAction(_ context.Context,
6981
}
7082

7183
var locator kvdbActionLocator
72-
err := db.DB.Update(func(tx *bbolt.Tx) error {
84+
err = db.DB.Update(func(tx *bbolt.Tx) error {
7385
mainActionsBucket, err := getBucket(tx, actionsBucketKey)
7486
if err != nil {
7587
return err

0 commit comments

Comments
 (0)