- Notifications
You must be signed in to change notification settings - Fork1.1k
feat: add session token injection to provisioner#7461
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
478001a feat: add session token to provisioner
sreya49fb3b5 add test
sreya8b52ca5 fix perms
sreyab49d18b fix panic
sreya3ff4539 move api key generation to its own package
sreya01f7a5e lint
sreya905cd1a add apikey pkg test
sreya6ad169d update provisionerd test
sreyabc6dec0 fix apikey test
sreya1d35967 pr comments
sreya4183989 rename proto field
sreya0bba543 stuff
sreyaFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
pr comments
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit1d35967bae5630c89905606ec71e89ca78a5f7d6
There are no files selected for viewing
4 changes: 2 additions & 2 deletionscoderd/apikey.go
This file contains hidden or 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
12 changes: 6 additions & 6 deletionscoderd/apikey/apikey.go
This file contains hidden or 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
35 changes: 18 additions & 17 deletionscoderd/apikey/apikey_test.go
This file contains hidden or 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 |
|---|---|---|
| @@ -7,6 +7,7 @@ import ( | ||
| "time" | ||
| "github.com/google/uuid" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/coder/coder/cli/clibase" | ||
| @@ -100,7 +101,7 @@ func TestGenerate(t *testing.T) { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
sreya marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| t.Parallel() | ||
| key, keystr, err := apikey.Generate(tc.params) | ||
| if tc.fail { | ||
| require.Error(t, err) | ||
| return | ||
| @@ -117,46 +118,46 @@ func TestGenerate(t *testing.T) { | ||
| // Assert that the hashed secret is correct. | ||
| hashed := sha256.Sum256([]byte(keytokens[1])) | ||
| assert.ElementsMatch(t, hashed, key.HashedSecret[:]) | ||
| assert.Equal(t, tc.params.UserID, key.UserID) | ||
| assert.WithinDuration(t, database.Now(), key.CreatedAt, time.Second*5) | ||
| assert.WithinDuration(t, database.Now(), key.UpdatedAt, time.Second*5) | ||
| if tc.params.LifetimeSeconds > 0 { | ||
| assert.Equal(t, tc.params.LifetimeSeconds, key.LifetimeSeconds) | ||
| } else if !tc.params.ExpiresAt.IsZero() { | ||
| // Should not be a delta greater than 5 seconds. | ||
| assert.InDelta(t, time.Until(tc.params.ExpiresAt).Seconds(), key.LifetimeSeconds, 5) | ||
| } else { | ||
| assert.Equal(t, int64(tc.params.DeploymentValues.SessionDuration.Value().Seconds()), key.LifetimeSeconds) | ||
| } | ||
| if !tc.params.ExpiresAt.IsZero() { | ||
| assert.Equal(t, tc.params.ExpiresAt.UTC(), key.ExpiresAt) | ||
| } else if tc.params.LifetimeSeconds > 0 { | ||
| assert.WithinDuration(t, database.Now().Add(time.Duration(tc.params.LifetimeSeconds)), key.ExpiresAt, time.Second*5) | ||
| } else { | ||
| assert.WithinDuration(t, database.Now().Add(tc.params.DeploymentValues.SessionDuration.Value()), key.ExpiresAt, time.Second*5) | ||
| } | ||
| if tc.params.RemoteAddr != "" { | ||
| assert.Equal(t, tc.params.RemoteAddr, key.IPAddress.IPNet.IP.String()) | ||
| } else { | ||
| assert.Equal(t, "0.0.0.0", key.IPAddress.IPNet.IP.String()) | ||
| } | ||
| if tc.params.Scope != "" { | ||
| assert.Equal(t, tc.params.Scope, key.Scope) | ||
| } else { | ||
| assert.Equal(t, database.APIKeyScopeAll, key.Scope) | ||
| } | ||
| if tc.params.TokenName != "" { | ||
| assert.Equal(t, tc.params.TokenName, key.TokenName) | ||
| } | ||
| if tc.params.LoginType != "" { | ||
| assert.Equal(t, tc.params.LoginType, key.LoginType) | ||
| } | ||
| }) | ||
| } | ||
51 changes: 32 additions & 19 deletionscoderd/provisionerdserver/provisionerdserver.go
This file contains hidden or 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 |
|---|---|---|
| @@ -203,7 +203,7 @@ func (server *Server) AcquireJob(ctx context.Context, _ *proto.Empty) (*proto.Ac | ||
| return nil, failJob(fmt.Sprintf("regenerate session token: %s", err)) | ||
| } | ||
| case database.WorkspaceTransitionStop, database.WorkspaceTransitionDelete: | ||
| err = deleteSessionToken(ctx, server.Database, workspace) | ||
| if err != nil { | ||
| return nil, failJob(fmt.Sprintf("delete session token: %s", err)) | ||
| } | ||
| @@ -1432,7 +1432,7 @@ func workspaceSessionTokenName(workspace database.Workspace) string { | ||
| } | ||
| func (server *Server) regenerateSessionToken(ctx context.Context, user database.User, workspace database.Workspace) (string, error) { | ||
sreya marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| newkey, sessionToken, err := apikey.Generate(apikey.CreateParams{ | ||
| UserID: user.ID, | ||
| LoginType: user.LoginType, | ||
| DeploymentValues: server.DeploymentValues, | ||
| @@ -1443,30 +1443,43 @@ func (server *Server) regenerateSessionToken(ctx context.Context, user database. | ||
| return "", xerrors.Errorf("generate API key: %w", err) | ||
| } | ||
| err = server.Database.InTx(func(tx database.Store) error { | ||
| err := deleteSessionToken(ctx, tx, workspace) | ||
| if err != nil { | ||
| return xerrors.Errorf("delete session token: %w", err) | ||
| } | ||
| _, err = tx.InsertAPIKey(ctx, newkey) | ||
| if err != nil { | ||
| return xerrors.Errorf("insert API key: %w", err) | ||
| } | ||
| return nil | ||
| }, nil) | ||
| if err != nil { | ||
| return "", xerrors.Errorf("create API key: %w", err) | ||
| } | ||
| returnsessionToken, nil | ||
| } | ||
| func deleteSessionToken(ctx context.Context, db database.Store, workspace database.Workspace) error { | ||
| err := db.InTx(func(tx database.Store) error { | ||
| key, err := tx.GetAPIKeyByName(ctx, database.GetAPIKeyByNameParams{ | ||
| UserID: workspace.OwnerID, | ||
| TokenName: workspaceSessionTokenName(workspace), | ||
| }) | ||
| if err == nil { | ||
| err = tx.DeleteAPIKeyByID(ctx, key.ID) | ||
| } | ||
| if err != nil && !xerrors.Is(err, sql.ErrNoRows) { | ||
| return xerrors.Errorf("get api key by name: %w", err) | ||
| } | ||
| return nil | ||
| }, nil) | ||
| if err != nil { | ||
| return xerrors.Errorf("in tx: %w", err) | ||
| } | ||
| return nil | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.