Skip to content

Commit e923fff

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 8f5f35b commit e923fff

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
@@ -71,8 +71,8 @@ type Session struct {
7171
GroupID ID
7272
}
7373

74-
// NewSession creates a new session with the given user-defined parameters.
75-
func NewSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
74+
// buildSession creates a new session with the given user-defined parameters.
75+
func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
7676
expiry time.Time, serverAddr string, devServer bool, perms []bakery.Op,
7777
caveats []macaroon.Caveat, featureConfig FeaturesConfig,
7878
privacy bool, linkedGroupID *ID, flags PrivacyFlags) (*Session, error) {
@@ -139,6 +139,18 @@ type IDToGroupIndex interface {
139139
// Store is the interface a persistent storage must implement for storing and
140140
// retrieving Terminal Connect sessions.
141141
type Store interface {
142+
// NewSession creates a new session with the given user-defined
143+
// parameters.
144+
//
145+
// NOTE: currently this purely a constructor of the Session type and
146+
// does not make any database calls. This will be changed in a future
147+
// commit.
148+
NewSession(id ID, localPrivKey *btcec.PrivateKey, label string,
149+
typ Type, expiry time.Time, serverAddr string, devServer bool,
150+
perms []bakery.Op, caveats []macaroon.Caveat,
151+
featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID,
152+
flags PrivacyFlags) (*Session, error)
153+
142154
// CreateSession adds a new session to the store. If a session with the
143155
// same local public key already exists an error is returned. This
144156
// can only be called with a Session with an ID that the Store has

session/kvdb_store.go

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

1212
"github.com/btcsuite/btcd/btcec/v2"
1313
"go.etcd.io/bbolt"
14+
"gopkg.in/macaroon-bakery.v2/bakery"
15+
"gopkg.in/macaroon.v2"
1416
)
1517

1618
var (
@@ -173,6 +175,24 @@ func getSessionKey(session *Session) []byte {
173175
return session.LocalPublicKey.SerializeCompressed()
174176
}
175177

178+
// NewSession creates a new session with the given user-defined parameters.
179+
//
180+
// NOTE: currently this purely a constructor of the Session type and does not
181+
// make any database calls. This will be changed in a future commit.
182+
//
183+
// NOTE: this is part of the Store interface.
184+
func (db *BoltStore) NewSession(id ID, localPrivKey *btcec.PrivateKey,
185+
label string, typ Type, expiry time.Time, serverAddr string,
186+
devServer bool, perms []bakery.Op, caveats []macaroon.Caveat,
187+
featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID,
188+
flags PrivacyFlags) (*Session, error) {
189+
190+
return buildSession(
191+
id, localPrivKey, label, typ, expiry, serverAddr, devServer,
192+
perms, caveats, featureConfig, privacy, linkedGroupID, flags,
193+
)
194+
}
195+
176196
// CreateSession adds a new session to the store. If a session with the same
177197
// local public key already exists an error is returned.
178198
//

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
@@ -318,7 +318,7 @@ func (s *sessionRpcServer) AddSession(ctx context.Context,
318318
return nil, err
319319
}
320320

321-
sess, err := session.NewSession(
321+
sess, err := s.cfg.db.NewSession(
322322
id, localPrivKey, req.Label, typ, expiry, req.MailboxServerAddr,
323323
req.DevServer, uniquePermissions, caveats, nil, false, nil,
324324
session.PrivacyFlags{},
@@ -1148,7 +1148,7 @@ func (s *sessionRpcServer) AddAutopilotSession(ctx context.Context,
11481148
return nil, err
11491149
}
11501150

1151-
sess, err := session.NewSession(
1151+
sess, err := s.cfg.db.NewSession(
11521152
id, localPrivKey, req.Label, session.TypeAutopilot, expiry,
11531153
req.MailboxServerAddr, req.DevServer, perms, caveats,
11541154
clientConfig, privacy, linkedGroupID, privacyFlags,

0 commit comments

Comments
 (0)