From bd59605516c64dceb02206e182d94aa8fcd920e3 Mon Sep 17 00:00:00 2001 From: Viktor Torstensson Date: Fri, 3 Oct 2025 11:35:45 +0200 Subject: [PATCH] multi: add `AddActionReq` `MacaroonId` helper func Add helper method to `AddActionReq` returns the 4 byte macaroon ID that is derived from the MacaroonRootKeyID. Using the helper removes some code repetition at call sites, and makes the intended usage clearer. --- firewalldb/actions.go | 12 ++++++++++++ firewalldb/actions_kvdb.go | 5 +---- firewalldb/actions_test.go | 6 +----- session_rpcserver.go | 5 +---- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/firewalldb/actions.go b/firewalldb/actions.go index ddaae65b..9a4e350b 100644 --- a/firewalldb/actions.go +++ b/firewalldb/actions.go @@ -84,6 +84,18 @@ type AddActionReq struct { RPCParamsJson []byte } +// MacaroonId returns the 4 byte macaroon ID that is derived from the +// MacaroonRootKeyID. If the MacaroonRootKeyID is not set, then this will return +// an empty 4 byte array. +func (a *AddActionReq) MacaroonId() [4]byte { + var macID [4]byte + a.MacaroonRootKeyID.WhenSome(func(rootID uint64) { + macID = session.IDFromMacRootKeyID(rootID) + }) + + return macID +} + // Action represents an RPC call made through the firewall. type Action struct { AddActionReq diff --git a/firewalldb/actions_kvdb.go b/firewalldb/actions_kvdb.go index 75dc3042..adf58eb0 100644 --- a/firewalldb/actions_kvdb.go +++ b/firewalldb/actions_kvdb.go @@ -61,10 +61,7 @@ func (db *BoltDB) AddAction(ctx context.Context, // If no macaroon is provided, then an empty 4-byte array is used as the // macaroon ID. Note that the kvdb implementation only stores the last // 4 bytes of the macaroon root key ID. - var macaroonID [4]byte - req.MacaroonRootKeyID.WhenSome(func(rootID uint64) { - macaroonID = session.IDFromMacRootKeyID(rootID) - }) + macaroonID := req.MacaroonId() // If the new action links to a session, the session must exist. // For the bbolt impl of the store, this is our best effort attempt diff --git a/firewalldb/actions_test.go b/firewalldb/actions_test.go index b3ee2f78..8aa70509 100644 --- a/firewalldb/actions_test.go +++ b/firewalldb/actions_test.go @@ -243,12 +243,8 @@ func TestListActions(t *testing.T) { assertActions := func(dbActions []*Action, al []*action) { require.Len(t, dbActions, len(al)) for i, a := range al { - rID, err := dbActions[i].MacaroonRootKeyID.UnwrapOrErr( - fmt.Errorf("macaroon root key is none"), - ) - require.NoError(t, err) require.EqualValues( - t, a.sessionID, session.IDFromMacRootKeyID(rID), + t, a.sessionID, dbActions[i].MacaroonId(), ) require.Equal(t, a.actionID, dbActions[i].FeatureName) } diff --git a/session_rpcserver.go b/session_rpcserver.go index 6bebb73d..0d56bd79 100644 --- a/session_rpcserver.go +++ b/session_rpcserver.go @@ -817,10 +817,7 @@ func (s *sessionRpcServer) ListActions(ctx context.Context, sessionID = id }) - var macID [4]byte - a.MacaroonRootKeyID.WhenSome(func(rootID uint64) { - macID = session.IDFromMacRootKeyID(rootID) - }) + macID := a.MacaroonId() resp[i] = &litrpc.Action{ SessionId: sessionID[:],