Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test
  • Loading branch information
sreya committed May 16, 2023
commit 49fb3b576b97ed6ec19008bb34e4b24ca11bab4f
7 changes: 5 additions & 2 deletions coderd/provisionerdserver/provisionerdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1468,8 +1468,8 @@ func (server *Server) regenerateSessionToken(ctx context.Context, user database.
_, err = tx.InsertAPIKey(ctx, database.InsertAPIKeyParams{
ID: id,
UserID: workspace.OwnerID,
LifetimeSeconds: int64(server.DeploymentValues.SessionDuration.Value().Seconds()),
ExpiresAt: database.Now().Add(server.DeploymentValues.SessionDuration.Value()).UTC(),
LifetimeSeconds: int64(server.DeploymentValues.MaxTokenLifetime.Value().Seconds()),
ExpiresAt: database.Now().Add(server.DeploymentValues.MaxTokenLifetime.Value()).UTC(),
IPAddress: pqtype.Inet{
IPNet: net.IPNet{
IP: ip,
Expand All @@ -1484,6 +1484,9 @@ func (server *Server) regenerateSessionToken(ctx context.Context, user database.
Scope: database.APIKeyScopeAll,
TokenName: workspaceSessionTokenName(workspace),
})
if err != nil {
return xerrors.Errorf("insert API key: %w", err)
}

return nil
}, nil)
Expand Down
19 changes: 19 additions & 0 deletions coderd/provisionerdserver/provisionerdserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/json"
"net/url"
"strings"
"sync/atomic"
"testing"
"time"
Expand All @@ -15,6 +16,7 @@ import (
"golang.org/x/oauth2"

"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/cli/clibase"
"github.com/coder/coder/coderd/audit"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/dbfake"
Expand Down Expand Up @@ -61,6 +63,7 @@ func TestAcquireJob(t *testing.T) {
Auditor: mockAuditor(),
TemplateScheduleStore: testTemplateScheduleStore(),
Tracer: trace.NewNoopTracerProvider().Tracer("noop"),
DeploymentValues: &codersdk.DeploymentValues{},
}
job, err := srv.AcquireJob(context.Background(), nil)
require.NoError(t, err)
Expand Down Expand Up @@ -102,6 +105,10 @@ func TestAcquireJob(t *testing.T) {
t.Parallel()
srv := setup(t, false)
gitAuthProvider := "github"
// Set the max session token lifetime so we can assert we
// create an API key with an expiration within the bounds of the
// deployment config.
srv.DeploymentValues.MaxTokenLifetime = clibase.Duration(time.Hour)
srv.GitAuthConfigs = []*gitauth.Config{{
ID: gitAuthProvider,
OAuth2Config: &testutil.OAuth2Config{},
Expand Down Expand Up @@ -216,6 +223,16 @@ func TestAcquireJob(t *testing.T) {
got, err := json.Marshal(job.Type)
require.NoError(t, err)

// Validate that a session token is generated during the job.
sessionToken := job.Type.(*proto.AcquiredJob_WorkspaceBuild_).WorkspaceBuild.Metadata.CoderSessionToken
require.NotEmpty(t, sessionToken)
toks := strings.Split(sessionToken, "-")
require.Len(t, toks, 2, "invalid api key")
key, err := srv.Database.GetAPIKeyByID(ctx, toks[0])
require.NoError(t, err)
require.Equal(t, int64(srv.DeploymentValues.MaxTokenLifetime.Value().Seconds()), key.LifetimeSeconds)
require.WithinDuration(t, time.Now().Add(srv.DeploymentValues.MaxTokenLifetime.Value()), key.ExpiresAt, time.Minute)

want, err := json.Marshal(&proto.AcquiredJob_WorkspaceBuild_{
WorkspaceBuild: &proto.AcquiredJob_WorkspaceBuild{
WorkspaceBuildId: build.ID.String(),
Expand Down Expand Up @@ -247,6 +264,7 @@ func TestAcquireJob(t *testing.T) {
WorkspaceOwnerId: user.ID.String(),
TemplateName: template.Name,
TemplateVersion: version.Name,
CoderSessionToken: sessionToken,
},
},
})
Expand Down Expand Up @@ -1205,6 +1223,7 @@ func setup(t *testing.T, ignoreLogErrors bool) *provisionerdserver.Server {
Auditor: mockAuditor(),
TemplateScheduleStore: testTemplateScheduleStore(),
Tracer: trace.NewNoopTracerProvider().Tracer("noop"),
DeploymentValues: &codersdk.DeploymentValues{},
}
}

Expand Down