-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable token auth for Jupyter notebooks [MD-404] (#9452)
Adds notebook sessions table to persist and fetch Jupyter tokens
- Loading branch information
1 parent
ea929fc
commit 553521e
Showing
13 changed files
with
271 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/o1egl/paseto" | ||
|
||
"github.com/determined-ai/determined/master/pkg/model" | ||
) | ||
|
||
// StartNotebookSession persists a new notebook session row into the database. | ||
func StartNotebookSession( | ||
ctx context.Context, | ||
userSessionID model.SessionID, | ||
taskID model.TaskID, | ||
token *string, | ||
) error { | ||
notebookSession := &model.NotebookSession{ | ||
UserSessionID: userSessionID, | ||
TaskID: taskID, | ||
Token: token, | ||
} | ||
|
||
if _, err := Bun().NewInsert().Model(notebookSession). | ||
Returning("id").Exec(ctx, ¬ebookSession.ID); err != nil { | ||
return fmt.Errorf("failed to create notebook session for task (%s): %w", taskID, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// GenerateNotebookSessionToken generates a token for a notebook session. | ||
func GenerateNotebookSessionToken( | ||
userSessionID model.SessionID, | ||
taskID model.TaskID, | ||
) (string, error) { | ||
notebookSession := &model.NotebookSession{ | ||
UserSessionID: userSessionID, | ||
TaskID: taskID, | ||
} | ||
|
||
v2 := paseto.NewV2() | ||
token, err := v2.Sign(GetTokenKeys().PrivateKey, notebookSession, nil) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to generate task authentication token: %w", err) | ||
} | ||
return token, nil | ||
} | ||
|
||
// DeleteNotebookSessionByTask deletes the notebook session associated with the task. | ||
func DeleteNotebookSessionByTask( | ||
ctx context.Context, | ||
taskID model.TaskID, | ||
) error { | ||
if _, err := Bun().NewDelete(). | ||
Table("notebook_sessions"). | ||
Where("task_id = ?", taskID). | ||
Exec(ctx); err != nil { | ||
return fmt.Errorf("failed to delete notebook session for task (%s): %w", taskID, err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package model | ||
|
||
import "github.com/uptrace/bun" | ||
|
||
// NotebookSession corresponds to a row in the "notebook_sessions" DB table. | ||
type NotebookSession struct { | ||
bun.BaseModel `bun:"table:notebook_sessions"` | ||
ID SessionID `db:"id" bun:"id,pk,autoincrement" json:"id"` | ||
TaskID TaskID `db:"task_id" bun:"task_id" json:"task_id"` | ||
UserSessionID SessionID `db:"user_session_id" bun:"user_session_id" json:"user_session_id"` | ||
Token *string `db:"token" bun:"token" json:"token"` | ||
} | ||
|
||
// NotebookSessionEnvVar is the environment variable name for notebook task tokens. | ||
const NotebookSessionEnvVar = "DET_NOTEBOOK_TOKEN" |
Oops, something went wrong.