Skip to content

Commit bd59605

Browse files
committed
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.
1 parent 01bc36c commit bd59605

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

firewalldb/actions.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ type AddActionReq struct {
8484
RPCParamsJson []byte
8585
}
8686

87+
// MacaroonId returns the 4 byte macaroon ID that is derived from the
88+
// MacaroonRootKeyID. If the MacaroonRootKeyID is not set, then this will return
89+
// an empty 4 byte array.
90+
func (a *AddActionReq) MacaroonId() [4]byte {
91+
var macID [4]byte
92+
a.MacaroonRootKeyID.WhenSome(func(rootID uint64) {
93+
macID = session.IDFromMacRootKeyID(rootID)
94+
})
95+
96+
return macID
97+
}
98+
8799
// Action represents an RPC call made through the firewall.
88100
type Action struct {
89101
AddActionReq

firewalldb/actions_kvdb.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ func (db *BoltDB) AddAction(ctx context.Context,
6161
// If no macaroon is provided, then an empty 4-byte array is used as the
6262
// macaroon ID. Note that the kvdb implementation only stores the last
6363
// 4 bytes of the macaroon root key ID.
64-
var macaroonID [4]byte
65-
req.MacaroonRootKeyID.WhenSome(func(rootID uint64) {
66-
macaroonID = session.IDFromMacRootKeyID(rootID)
67-
})
64+
macaroonID := req.MacaroonId()
6865

6966
// If the new action links to a session, the session must exist.
7067
// For the bbolt impl of the store, this is our best effort attempt

firewalldb/actions_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,8 @@ func TestListActions(t *testing.T) {
243243
assertActions := func(dbActions []*Action, al []*action) {
244244
require.Len(t, dbActions, len(al))
245245
for i, a := range al {
246-
rID, err := dbActions[i].MacaroonRootKeyID.UnwrapOrErr(
247-
fmt.Errorf("macaroon root key is none"),
248-
)
249-
require.NoError(t, err)
250246
require.EqualValues(
251-
t, a.sessionID, session.IDFromMacRootKeyID(rID),
247+
t, a.sessionID, dbActions[i].MacaroonId(),
252248
)
253249
require.Equal(t, a.actionID, dbActions[i].FeatureName)
254250
}

session_rpcserver.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,10 +817,7 @@ func (s *sessionRpcServer) ListActions(ctx context.Context,
817817
sessionID = id
818818
})
819819

820-
var macID [4]byte
821-
a.MacaroonRootKeyID.WhenSome(func(rootID uint64) {
822-
macID = session.IDFromMacRootKeyID(rootID)
823-
})
820+
macID := a.MacaroonId()
824821

825822
resp[i] = &litrpc.Action{
826823
SessionId: sessionID[:],

0 commit comments

Comments
 (0)