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.
This commit is contained in:
Elle Mouton 2025-02-09 11:21:53 +02:00
parent 8f5f35b571
commit e923fff46c
No known key found for this signature in database
GPG key ID: D7D916376026F177
5 changed files with 41 additions and 9 deletions

View file

@ -71,8 +71,8 @@ type Session struct {
GroupID ID
}
// NewSession creates a new session with the given user-defined parameters.
func NewSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
// buildSession creates a new session with the given user-defined parameters.
func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
expiry time.Time, serverAddr string, devServer bool, perms []bakery.Op,
caveats []macaroon.Caveat, featureConfig FeaturesConfig,
privacy bool, linkedGroupID *ID, flags PrivacyFlags) (*Session, error) {
@ -139,6 +139,18 @@ type IDToGroupIndex interface {
// Store is the interface a persistent storage must implement for storing and
// retrieving Terminal Connect sessions.
type Store interface {
// NewSession creates a new session with the given user-defined
// parameters.
//
// NOTE: currently this purely a constructor of the Session type and
// does not make any database calls. This will be changed in a future
// commit.
NewSession(id ID, localPrivKey *btcec.PrivateKey, label string,
typ Type, expiry time.Time, serverAddr string, devServer bool,
perms []bakery.Op, caveats []macaroon.Caveat,
featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID,
flags PrivacyFlags) (*Session, error)
// CreateSession adds a new session to the store. If a session with the
// same local public key already exists an error is returned. This
// can only be called with a Session with an ID that the Store has

View file

@ -11,6 +11,8 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
"go.etcd.io/bbolt"
"gopkg.in/macaroon-bakery.v2/bakery"
"gopkg.in/macaroon.v2"
)
var (
@ -173,6 +175,24 @@ func getSessionKey(session *Session) []byte {
return session.LocalPublicKey.SerializeCompressed()
}
// NewSession creates a new session with the given user-defined parameters.
//
// NOTE: currently this purely a constructor of the Session type and does not
// make any database calls. This will be changed in a future commit.
//
// NOTE: this is part of the Store interface.
func (db *BoltStore) NewSession(id ID, localPrivKey *btcec.PrivateKey,
label string, typ Type, expiry time.Time, serverAddr string,
devServer bool, perms []bakery.Op, caveats []macaroon.Caveat,
featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID,
flags PrivacyFlags) (*Session, error) {
return buildSession(
id, localPrivKey, label, typ, expiry, serverAddr, devServer,
perms, caveats, featureConfig, privacy, linkedGroupID, flags,
)
}
// CreateSession adds a new session to the store. If a session with the same
// local public key already exists an error is returned.
//

View file

@ -285,7 +285,7 @@ func newSession(t *testing.T, db Store, label string,
id, priv, err := db.GetUnusedIDAndKeyPair()
require.NoError(t, err)
session, err := NewSession(
session, err := buildSession(
id, priv, label, TypeMacaroonAdmin,
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
"foo.bar.baz:1234", true, nil, nil, nil, true, linkedGroupID,

View file

@ -129,7 +129,7 @@ func TestSerializeDeserializeSession(t *testing.T) {
priv, id, err := NewSessionPrivKeyAndID()
require.NoError(t, err)
session, err := NewSession(
session, err := buildSession(
id, priv, test.name, test.sessType,
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
"foo.bar.baz:1234", true, test.perms,
@ -183,7 +183,7 @@ func TestGroupIDForOlderSessions(t *testing.T) {
priv, id, err := NewSessionPrivKeyAndID()
require.NoError(t, err)
session, err := NewSession(
session, err := buildSession(
id, priv, "test-session", TypeMacaroonAdmin,
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
"foo.bar.baz:1234", true, nil, nil, nil, false, nil,
@ -218,7 +218,7 @@ func TestGroupID(t *testing.T) {
require.NoError(t, err)
// Create session 1 which is not linked to any previous session.
session1, err := NewSession(
session1, err := buildSession(
id, priv, "test-session", TypeMacaroonAdmin,
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
"foo.bar.baz:1234", true, nil, nil, nil, false, nil,
@ -232,7 +232,7 @@ func TestGroupID(t *testing.T) {
// Create session 2 and link it to session 1.
priv, id, err = NewSessionPrivKeyAndID()
require.NoError(t, err)
session2, err := NewSession(
session2, err := buildSession(
id, priv, "test-session", TypeMacaroonAdmin,
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
"foo.bar.baz:1234", true, nil, nil, nil, false,

View file

@ -318,7 +318,7 @@ func (s *sessionRpcServer) AddSession(ctx context.Context,
return nil, err
}
sess, err := session.NewSession(
sess, err := s.cfg.db.NewSession(
id, localPrivKey, req.Label, typ, expiry, req.MailboxServerAddr,
req.DevServer, uniquePermissions, caveats, nil, false, nil,
session.PrivacyFlags{},
@ -1148,7 +1148,7 @@ func (s *sessionRpcServer) AddAutopilotSession(ctx context.Context,
return nil, err
}
sess, err := session.NewSession(
sess, err := s.cfg.db.NewSession(
id, localPrivKey, req.Label, session.TypeAutopilot, expiry,
req.MailboxServerAddr, req.DevServer, perms, caveats,
clientConfig, privacy, linkedGroupID, privacyFlags,