Skip to content

Commit cd1f6bf

Browse files
committed
session: convert some NewSession params to functional ops
1 parent 8247fbb commit cd1f6bf

File tree

5 files changed

+98
-34
lines changed

5 files changed

+98
-34
lines changed

session/interface.go

+71-13
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,9 @@ type Session struct {
100100

101101
// buildSession creates a new session with the given user-defined parameters.
102102
func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
103-
created, expiry time.Time, serverAddr string, devServer bool,
103+
created, expiry time.Time, serverAddr string,
104104
perms []bakery.Op, caveats []macaroon.Caveat,
105-
featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID,
106-
flags PrivacyFlags, options ...Option) (*Session, error) {
105+
options ...Option) (*Session, error) {
107106

108107
opts := defaultSessionOptions()
109108
for _, o := range options {
@@ -120,10 +119,10 @@ func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
120119
// The group ID will by default be the same as the Session ID
121120
// unless this session links to a previous session.
122121
groupID := id
123-
if linkedGroupID != nil {
122+
if opts.linkedGroupID != nil {
124123
// If this session is linked to a previous session, then the
125124
// group ID is the same as the linked session's group ID.
126-
groupID = *linkedGroupID
125+
groupID = *opts.linkedGroupID
127126
}
128127

129128
sess := &Session{
@@ -134,14 +133,14 @@ func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
134133
Expiry: expiry.UTC(),
135134
CreatedAt: created.UTC(),
136135
ServerAddr: serverAddr,
137-
DevServer: devServer,
136+
DevServer: opts.devServer,
138137
MacaroonRootKey: macRootKey,
139138
PairingSecret: pairingSecret,
140139
LocalPrivateKey: localPrivKey,
141140
LocalPublicKey: localPrivKey.PubKey(),
142141
RemotePublicKey: nil,
143-
WithPrivacyMapper: privacy,
144-
PrivacyFlags: flags,
142+
WithPrivacyMapper: opts.privacy,
143+
PrivacyFlags: opts.privacyFlags,
145144
GroupID: groupID,
146145
}
147146

@@ -156,8 +155,8 @@ func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
156155
}
157156
}
158157

159-
if len(featureConfig) != 0 {
160-
sess.FeatureConfig = &featureConfig
158+
if len(opts.featureConfig) != 0 {
159+
sess.FeatureConfig = &opts.featureConfig
161160
}
162161

163162
return sess, nil
@@ -179,13 +178,36 @@ type sessionOptions struct {
179178
// immediateActivation can be used to immediately move a session to
180179
// the StateCreated state on creation.
181180
immediateActivation bool
181+
182+
// privacy indicates if a privacy map should be used with this session.
183+
privacy bool
184+
185+
// privacyFlags to use in combination with the session's privacy mapper.
186+
privacyFlags PrivacyFlags
187+
188+
// featureConfig holds any feature configuration bytes to use for this
189+
// session.
190+
featureConfig FeaturesConfig
191+
192+
// linkedGroupID is the ID of the group that this session is linked
193+
// to. By default, a session is not linked to another group.
194+
linkedGroupID *ID
195+
196+
// devServer is true if TLS should be skipped when connecting to the
197+
// mailbox server.
198+
devServer bool
182199
}
183200

184201
// defaultSessionOptions returns a new sessionOptions struct with default
185202
// values set.
186203
func defaultSessionOptions() *sessionOptions {
187204
return &sessionOptions{
188205
immediateActivation: false,
206+
privacy: false,
207+
privacyFlags: PrivacyFlags{},
208+
featureConfig: FeaturesConfig{},
209+
linkedGroupID: nil,
210+
devServer: false,
189211
}
190212
}
191213

@@ -201,16 +223,52 @@ func WithImmediateActivation() Option {
201223
}
202224
}
203225

226+
// WithPrivacy can be used to enable the privacy mapper for this session.
227+
func WithPrivacy(privacy bool) Option {
228+
return func(o *sessionOptions) {
229+
o.privacy = privacy
230+
}
231+
}
232+
233+
// WithPrivacyFlags can be used to set the privacy flags for this session.
234+
func WithPrivacyFlags(flags PrivacyFlags) Option {
235+
return func(o *sessionOptions) {
236+
o.privacyFlags = flags
237+
}
238+
}
239+
240+
// WithFeatureConfig can be used to set the feature configuration bytes for
241+
// this session.
242+
func WithFeatureConfig(config FeaturesConfig) Option {
243+
return func(o *sessionOptions) {
244+
o.featureConfig = config
245+
}
246+
}
247+
248+
// WithLinkedGroupID can be used to link this session to a previous session.
249+
func WithLinkedGroupID(groupID *ID) Option {
250+
return func(o *sessionOptions) {
251+
o.linkedGroupID = groupID
252+
}
253+
}
254+
255+
// WithDevServer can be used to set if TLS verification should be skipped when
256+
// connecting to the mailbox server.
257+
func WithDevServer(dev bool) Option {
258+
return func(o *sessionOptions) {
259+
o.devServer = dev
260+
}
261+
}
262+
204263
// Store is the interface a persistent storage must implement for storing and
205264
// retrieving Terminal Connect sessions.
206265
type Store interface {
207266
// NewSession creates a new session with the given user-defined
208267
// parameters. The session will remain in the StateReserved state until
209268
// CreateSession is called for the session.
210269
NewSession(label string, typ Type, expiry time.Time, serverAddr string,
211-
devServer bool, perms []bakery.Op, caveats []macaroon.Caveat,
212-
featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID,
213-
flags PrivacyFlags, opts ...Option) (*Session, error)
270+
perms []bakery.Op, caveats []macaroon.Caveat,
271+
opts ...Option) (*Session, error)
214272

215273
// CreateSession moves the given session from the StateReserved state to
216274
// the StateCreated state.

session/kvdb_store.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,8 @@ func getSessionKey(session *Session) []byte {
188188
//
189189
// NOTE: this is part of the Store interface.
190190
func (db *BoltStore) NewSession(label string, typ Type, expiry time.Time,
191-
serverAddr string, devServer bool, perms []bakery.Op,
192-
caveats []macaroon.Caveat, featureConfig FeaturesConfig, privacy bool,
193-
linkedGroupID *ID, flags PrivacyFlags, options ...Option) (*Session,
194-
error) {
191+
serverAddr string, perms []bakery.Op,
192+
caveats []macaroon.Caveat, options ...Option) (*Session, error) {
195193

196194
var session *Session
197195
err := db.Update(func(tx *bbolt.Tx) error {
@@ -207,8 +205,8 @@ func (db *BoltStore) NewSession(label string, typ Type, expiry time.Time,
207205

208206
session, err = buildSession(
209207
id, localPrivKey, label, typ, db.clock.Now(), expiry,
210-
serverAddr, devServer, perms, caveats, featureConfig,
211-
privacy, linkedGroupID, flags, options...,
208+
serverAddr, perms, caveats,
209+
options...,
212210
)
213211
if err != nil {
214212
return err

session/store_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,12 @@ func reserveSession(db Store, label string,
329329

330330
return db.NewSession(label, opts.sessType,
331331
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
332-
"foo.bar.baz:1234", true, nil, nil, nil, true, opts.groupID,
333-
[]PrivacyFlag{ClearPubkeys},
332+
"foo.bar.baz:1234",
333+
nil, nil,
334+
WithDevServer(true),
335+
WithPrivacy(true),
336+
WithLinkedGroupID(opts.groupID),
337+
WithPrivacyFlags(PrivacyFlags{ClearPubkeys}),
334338
)
335339
}
336340

session/tlv_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ func TestSerializeDeserializeSession(t *testing.T) {
134134
time.Now(),
135135
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
136136
"foo.bar.baz:1234", true, test.perms,
137-
test.caveats, test.featureConfig, true,
138-
test.linkedGroupID,
139-
[]PrivacyFlag{ClearPubkeys},
137+
test.caveats,
138+
WithFeatureConfig(test.featureConfig),
139+
WithPrivacy(true),
140+
WithLinkedGroupID(test.linkedGroupID),
141+
WithPrivacyFlags(PrivacyFlags{ClearPubkeys}),
140142
)
141143
require.NoError(t, err)
142144

@@ -188,8 +190,7 @@ func TestGroupIDForOlderSessions(t *testing.T) {
188190
id, priv, "test-session", TypeMacaroonAdmin,
189191
time.Now(),
190192
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
191-
"foo.bar.baz:1234", true, nil, nil, nil, false, nil,
192-
PrivacyFlags{},
193+
"foo.bar.baz:1234", true, nil, nil,
193194
)
194195
require.NoError(t, err)
195196

@@ -224,8 +225,7 @@ func TestGroupID(t *testing.T) {
224225
id, priv, "test-session", TypeMacaroonAdmin,
225226
time.Now(),
226227
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
227-
"foo.bar.baz:1234", true, nil, nil, nil, false, nil,
228-
PrivacyFlags{},
228+
"foo.bar.baz:1234", true, nil, nil,
229229
)
230230
require.NoError(t, err)
231231

@@ -239,8 +239,8 @@ func TestGroupID(t *testing.T) {
239239
id, priv, "test-session", TypeMacaroonAdmin,
240240
time.Now(),
241241
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
242-
"foo.bar.baz:1234", true, nil, nil, nil, false,
243-
&session1.GroupID, PrivacyFlags{},
242+
"foo.bar.baz:1234", true, nil, nil,
243+
WithLinkedGroupID(&session1.GroupID),
244244
)
245245
require.NoError(t, err)
246246

session_rpcserver.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ func (s *sessionRpcServer) AddSession(ctx context.Context,
310310

311311
sess, err := s.cfg.db.NewSession(
312312
req.Label, typ, expiry, req.MailboxServerAddr,
313-
req.DevServer, uniquePermissions, caveats, nil, false, nil,
314-
session.PrivacyFlags{},
313+
uniquePermissions, caveats,
314+
session.WithDevServer(req.DevServer),
315315
session.WithImmediateActivation(),
316316
)
317317
if err != nil {
@@ -1101,8 +1101,12 @@ func (s *sessionRpcServer) AddAutopilotSession(ctx context.Context,
11011101

11021102
sess, err := s.cfg.db.NewSession(
11031103
req.Label, session.TypeAutopilot, expiry,
1104-
req.MailboxServerAddr, req.DevServer, perms, caveats,
1105-
clientConfig, privacy, linkedGroupID, privacyFlags,
1104+
req.MailboxServerAddr, perms, caveats,
1105+
session.WithDevServer(req.DevServer),
1106+
session.WithFeatureConfig(clientConfig),
1107+
session.WithPrivacy(privacy),
1108+
session.WithPrivacyFlags(privacyFlags),
1109+
session.WithLinkedGroupID(linkedGroupID),
11061110
)
11071111
if err != nil {
11081112
return nil, fmt.Errorf("error creating new session: %v", err)

0 commit comments

Comments
 (0)