Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sql-14] sessions: atomic session creation #980

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

ellemouton
Copy link
Member

@ellemouton ellemouton commented Feb 17, 2025

See #979 for more context.

In this PR, we do the refactor to make session creation happen in 1 atomic DB transaction as described in the issue above.

req.DevServer, uniquePermissions, caveats, nil, false, nil,
session.PrivacyFlags{},
)
if err != nil {
return nil, fmt.Errorf("error creating new session: %v", err)
}

if err := s.cfg.db.CreateSession(sess); err != nil {
sess, err = s.cfg.db.CreateSession(sess.ID)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: in this follow up, we add a way to do this in one go for normal (non-autopilot) sessions

Copy link
Contributor

@ViktorTigerstrom ViktorTigerstrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good 🚀! Leaving one comment regarding state enforcement during session revoking that I believe we should fix :)

Comment on lines 30 to 32
/---> StateExpired
StateReserved ---> StateCreated ---
\---> StateRevoked
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is nothing in the database layer preventing a session from being immediately moved from StateReserved to StateRevoked.

I think we should enforce this constraint, either at the database layer or directly in the RevokeSession RPC endpoint. Otherwise, although extremely unlikely with the current code, a user could potentially revoke the session after the NewSession call in AddAutopilotSession but before or during the s.cfg.autopilot.RegisterSession call within the same function. That'd likely create a lot of unintended errors.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what sort of errors would that create?

but yes, can add a tight-map constraint (above DB layer as it isnt db related). But i think we can make it so that it's fine to always go from any state to "revoked"

Copy link
Contributor

@ViktorTigerstrom ViktorTigerstrom Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think mainly there's 2 errors that would occur that I can think of right now, but there might be more:

  1. The issue described in the comment below ([sql-14] sessions: atomic session creation #980 (comment)) would occur, because calling s.cfg.db.CreateSession(sess.ID) after registering the session with the autopilot server results in an error in the AddAutopilotSession call, since the session is not in the Reserved state.

  2. The actual s.cfg.autopilot.SessionRevoked(ctx, pubKey) call in RevokeSession would not actually revoke the session on the autopilot server, as the session has not yet been registered on the server. It'll then actually get added to server, when the call to register the session to the autopilot server gets executed, after it has already been revoked on the client. I don't know this could lead to unintended consequences that I can't think of right now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have added the strict state shift stuff here: #985

this PR now builds on top of that one

Comment on lines 1190 to 1215
err = s.cfg.db.UpdateSessionRemotePubKey(sess.LocalPublicKey, remoteKey)
if err != nil {
return nil, fmt.Errorf("error setting remote pubkey: %v", err)
}

// We only activate the session if the Autopilot server registration
// was successful.
sess, err = s.cfg.db.CreateSession(sess.ID)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's nothing to change here, but just a reminder, as we discussed in the original session-linking PR, that we should allow session revocation based on groupIDs at the autopilot server level. 🤓 Otherwise, this could lead to a scenario where the autopilot server persists the reserved session, but litd removes it on the next startup if an error occurs here. That'd become problematic for linked sessions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, that's a great point

And only allow legal state shifts.
In this commit, we let StateReserved be the new initial state of a
session for when NewSession is called. We then do predicate checks for
linked sessions along with unique session alias (ID) and priv key
derivations all under the same DB transaction in NewSession.

ShiftState then moves a session to StateCreated. Only in StateCreated
does a session become usable.

With this change, we no longer need to ensure atomic session creation by
acquiring the `sessRegMu` mutex in the session RPC server.
This was used to check that all linked sessions are no longer
active before attempting to register an autopilot session. But this is
no longer needed since this is done within NewSession.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-changelog This PR is does not require a release notes entry sql-ize
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants