Skip to content

Commit 4010832

Browse files
committed
session: add NewSession to Store interface
For now, it makes no DB calls. But this is in prepartion for letting this call persist a new session. This will also let us use a shared `clock` for the time fields in a Session.
1 parent f49c1bf commit 4010832

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

session/interface.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ type Session struct {
7575
type MacaroonBaker func(ctx context.Context, rootKeyID uint64,
7676
recipe *MacaroonRecipe) (string, error)
7777

78-
// NewSession creates a new session with the given user-defined parameters.
79-
func NewSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
78+
// buildSession creates a new session with the given user-defined parameters.
79+
func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
8080
expiry time.Time, serverAddr string, devServer bool, perms []bakery.Op,
8181
caveats []macaroon.Caveat, featureConfig FeaturesConfig,
8282
privacy bool, linkedGroupID *ID, flags PrivacyFlags) (*Session, error) {
@@ -143,6 +143,18 @@ type IDToGroupIndex interface {
143143
// Store is the interface a persistent storage must implement for storing and
144144
// retrieving Terminal Connect sessions.
145145
type Store interface {
146+
// NewSession creates a new session with the given user-defined
147+
// parameters.
148+
//
149+
// NOTE: currently this purely a constructor of the Session type and
150+
// does not make any database calls. This will be changed in a future
151+
// commit.
152+
NewSession(id ID, localPrivKey *btcec.PrivateKey, label string,
153+
typ Type, expiry time.Time, serverAddr string, devServer bool,
154+
perms []bakery.Op, caveats []macaroon.Caveat,
155+
featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID,
156+
flags PrivacyFlags) (*Session, error)
157+
146158
// CreateSession adds a new session to the store. If a session with the
147159
// same local public key already exists an error is returned. This
148160
// can only be called with a Session with an ID that the Store has

session/store.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
"github.com/btcsuite/btcd/btcec/v2"
1010
"go.etcd.io/bbolt"
11+
"gopkg.in/macaroon-bakery.v2/bakery"
12+
"gopkg.in/macaroon.v2"
1113
)
1214

1315
var (
@@ -60,6 +62,24 @@ func getSessionKey(session *Session) []byte {
6062
return session.LocalPublicKey.SerializeCompressed()
6163
}
6264

65+
// NewSession creates a new session with the given user-defined parameters.
66+
//
67+
// NOTE: currently this purely a constructor of the Session type and does not
68+
// make any database calls. This will be changed in a future commit.
69+
//
70+
// NOTE: this is part of the Store interface.
71+
func (db *DB) NewSession(id ID, localPrivKey *btcec.PrivateKey, label string,
72+
typ Type, expiry time.Time, serverAddr string, devServer bool,
73+
perms []bakery.Op, caveats []macaroon.Caveat,
74+
featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID,
75+
flags PrivacyFlags) (*Session, error) {
76+
77+
return buildSession(
78+
id, localPrivKey, label, typ, expiry, serverAddr, devServer,
79+
perms, caveats, featureConfig, privacy, linkedGroupID, flags,
80+
)
81+
}
82+
6383
// CreateSession adds a new session to the store. If a session with the same
6484
// local public key already exists an error is returned.
6585
//

session/store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func newSession(t *testing.T, db Store, label string,
285285
id, priv, err := db.GetUnusedIDAndKeyPair()
286286
require.NoError(t, err)
287287

288-
session, err := NewSession(
288+
session, err := buildSession(
289289
id, priv, label, TypeMacaroonAdmin,
290290
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
291291
"foo.bar.baz:1234", true, nil, nil, nil, true, linkedGroupID,

session/tlv_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestSerializeDeserializeSession(t *testing.T) {
129129
priv, id, err := NewSessionPrivKeyAndID()
130130
require.NoError(t, err)
131131

132-
session, err := NewSession(
132+
session, err := buildSession(
133133
id, priv, test.name, test.sessType,
134134
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
135135
"foo.bar.baz:1234", true, test.perms,
@@ -183,7 +183,7 @@ func TestGroupIDForOlderSessions(t *testing.T) {
183183
priv, id, err := NewSessionPrivKeyAndID()
184184
require.NoError(t, err)
185185

186-
session, err := NewSession(
186+
session, err := buildSession(
187187
id, priv, "test-session", TypeMacaroonAdmin,
188188
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
189189
"foo.bar.baz:1234", true, nil, nil, nil, false, nil,
@@ -218,7 +218,7 @@ func TestGroupID(t *testing.T) {
218218
require.NoError(t, err)
219219

220220
// Create session 1 which is not linked to any previous session.
221-
session1, err := NewSession(
221+
session1, err := buildSession(
222222
id, priv, "test-session", TypeMacaroonAdmin,
223223
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
224224
"foo.bar.baz:1234", true, nil, nil, nil, false, nil,
@@ -232,7 +232,7 @@ func TestGroupID(t *testing.T) {
232232
// Create session 2 and link it to session 1.
233233
priv, id, err = NewSessionPrivKeyAndID()
234234
require.NoError(t, err)
235-
session2, err := NewSession(
235+
session2, err := buildSession(
236236
id, priv, "test-session", TypeMacaroonAdmin,
237237
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
238238
"foo.bar.baz:1234", true, nil, nil, nil, false,

session_rpcserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (s *sessionRpcServer) AddSession(ctx context.Context,
321321
return nil, err
322322
}
323323

324-
sess, err := session.NewSession(
324+
sess, err := s.cfg.db.NewSession(
325325
id, localPrivKey, req.Label, typ, expiry, req.MailboxServerAddr,
326326
req.DevServer, uniquePermissions, caveats, nil, false, nil,
327327
session.PrivacyFlags{},
@@ -1155,7 +1155,7 @@ func (s *sessionRpcServer) AddAutopilotSession(ctx context.Context,
11551155
return nil, err
11561156
}
11571157

1158-
sess, err := session.NewSession(
1158+
sess, err := s.cfg.db.NewSession(
11591159
id, localPrivKey, req.Label, session.TypeAutopilot, expiry,
11601160
req.MailboxServerAddr, req.DevServer, perms, caveats,
11611161
clientConfig, privacy, linkedGroupID, privacyFlags,

0 commit comments

Comments
 (0)