mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
fix: ensure agent token is from latest build in middleware (#12443)
This commit is contained in:
@ -32,7 +32,23 @@ func WorkspaceAgent(r *http.Request) database.WorkspaceAgent {
|
||||
return user
|
||||
}
|
||||
|
||||
type ExtractWorkspaceAgentConfig struct {
|
||||
type latestBuildContextKey struct{}
|
||||
|
||||
func latestBuildOptional(r *http.Request) (database.WorkspaceBuild, bool) {
|
||||
wb, ok := r.Context().Value(latestBuildContextKey{}).(database.WorkspaceBuild)
|
||||
return wb, ok
|
||||
}
|
||||
|
||||
// LatestBuild returns the Latest Build from the ExtractLatestBuild handler.
|
||||
func LatestBuild(r *http.Request) database.WorkspaceBuild {
|
||||
wb, ok := latestBuildOptional(r)
|
||||
if !ok {
|
||||
panic("developer error: agent middleware not provided or was made optional")
|
||||
}
|
||||
return wb
|
||||
}
|
||||
|
||||
type ExtractWorkspaceAgentAndLatestBuildConfig struct {
|
||||
DB database.Store
|
||||
// Optional indicates whether the middleware should be optional. If true, any
|
||||
// requests without the a token or with an invalid token will be allowed to
|
||||
@ -40,8 +56,8 @@ type ExtractWorkspaceAgentConfig struct {
|
||||
Optional bool
|
||||
}
|
||||
|
||||
// ExtractWorkspaceAgent requires authentication using a valid agent token.
|
||||
func ExtractWorkspaceAgent(opts ExtractWorkspaceAgentConfig) func(http.Handler) http.Handler {
|
||||
// ExtractWorkspaceAgentAndLatestBuild requires authentication using a valid agent token.
|
||||
func ExtractWorkspaceAgentAndLatestBuild(opts ExtractWorkspaceAgentAndLatestBuildConfig) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
@ -76,7 +92,7 @@ func ExtractWorkspaceAgent(opts ExtractWorkspaceAgentConfig) func(http.Handler)
|
||||
}
|
||||
|
||||
//nolint:gocritic // System needs to be able to get workspace agents.
|
||||
row, err := opts.DB.GetWorkspaceAgentAndOwnerByAuthToken(dbauthz.AsSystemRestricted(ctx), token)
|
||||
row, err := opts.DB.GetWorkspaceAgentAndLatestBuildByAuthToken(dbauthz.AsSystemRestricted(ctx), token)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
optionalWrite(http.StatusUnauthorized, codersdk.Response{
|
||||
@ -93,19 +109,30 @@ func ExtractWorkspaceAgent(opts ExtractWorkspaceAgentConfig) func(http.Handler)
|
||||
return
|
||||
}
|
||||
|
||||
//nolint:gocritic // System needs to be able to get owner roles.
|
||||
roles, err := opts.DB.GetAuthorizationUserRoles(dbauthz.AsSystemRestricted(ctx), row.Workspace.OwnerID)
|
||||
if err != nil {
|
||||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
||||
Message: "Internal error checking workspace agent authorization.",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
subject := rbac.Subject{
|
||||
ID: row.OwnerID.String(),
|
||||
Roles: rbac.RoleNames(row.OwnerRoles),
|
||||
Groups: row.OwnerGroups,
|
||||
ID: row.Workspace.OwnerID.String(),
|
||||
Roles: rbac.RoleNames(roles.Roles),
|
||||
Groups: roles.Groups,
|
||||
Scope: rbac.WorkspaceAgentScope(rbac.WorkspaceAgentScopeParams{
|
||||
WorkspaceID: row.WorkspaceID,
|
||||
OwnerID: row.OwnerID,
|
||||
TemplateID: row.TemplateID,
|
||||
VersionID: row.TemplateVersionID,
|
||||
WorkspaceID: row.Workspace.ID,
|
||||
OwnerID: row.Workspace.OwnerID,
|
||||
TemplateID: row.Workspace.TemplateID,
|
||||
VersionID: row.WorkspaceBuild.TemplateVersionID,
|
||||
}),
|
||||
}.WithCachedASTValue()
|
||||
|
||||
ctx = context.WithValue(ctx, workspaceAgentContextKey{}, row.WorkspaceAgent)
|
||||
ctx = context.WithValue(ctx, latestBuildContextKey{}, row.WorkspaceBuild)
|
||||
// Also set the dbauthz actor for the request.
|
||||
ctx = dbauthz.As(ctx, subject)
|
||||
next.ServeHTTP(rw, r.WithContext(ctx))
|
||||
|
@ -23,8 +23,8 @@ func TestWorkspaceAgent(t *testing.T) {
|
||||
t.Parallel()
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
|
||||
req, rtr := setup(t, db, uuid.New(), httpmw.ExtractWorkspaceAgent(
|
||||
httpmw.ExtractWorkspaceAgentConfig{
|
||||
req, rtr, _, _ := setup(t, db, uuid.New(), httpmw.ExtractWorkspaceAgentAndLatestBuild(
|
||||
httpmw.ExtractWorkspaceAgentAndLatestBuildConfig{
|
||||
DB: db,
|
||||
Optional: false,
|
||||
}))
|
||||
@ -42,8 +42,8 @@ func TestWorkspaceAgent(t *testing.T) {
|
||||
t.Parallel()
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
authToken := uuid.New()
|
||||
req, rtr := setup(t, db, authToken, httpmw.ExtractWorkspaceAgent(
|
||||
httpmw.ExtractWorkspaceAgentConfig{
|
||||
req, rtr, _, _ := setup(t, db, authToken, httpmw.ExtractWorkspaceAgentAndLatestBuild(
|
||||
httpmw.ExtractWorkspaceAgentAndLatestBuildConfig{
|
||||
DB: db,
|
||||
Optional: false,
|
||||
}))
|
||||
@ -57,9 +57,47 @@ func TestWorkspaceAgent(t *testing.T) {
|
||||
t.Cleanup(func() { _ = res.Body.Close() })
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
})
|
||||
|
||||
t.Run("Latest", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
authToken := uuid.New()
|
||||
req, rtr, ws, tpv := setup(t, db, authToken, httpmw.ExtractWorkspaceAgentAndLatestBuild(
|
||||
httpmw.ExtractWorkspaceAgentAndLatestBuildConfig{
|
||||
DB: db,
|
||||
Optional: false,
|
||||
}),
|
||||
)
|
||||
|
||||
// Create a newer build
|
||||
job := dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{
|
||||
OrganizationID: ws.OrganizationID,
|
||||
})
|
||||
resource := dbgen.WorkspaceResource(t, db, database.WorkspaceResource{
|
||||
JobID: job.ID,
|
||||
})
|
||||
_ = dbgen.WorkspaceBuild(t, db, database.WorkspaceBuild{
|
||||
WorkspaceID: ws.ID,
|
||||
JobID: job.ID,
|
||||
TemplateVersionID: tpv.ID,
|
||||
BuildNumber: 2,
|
||||
})
|
||||
_ = dbgen.WorkspaceAgent(t, db, database.WorkspaceAgent{
|
||||
ResourceID: resource.ID,
|
||||
})
|
||||
|
||||
rw := httptest.NewRecorder()
|
||||
req.Header.Set(codersdk.SessionTokenHeader, authToken.String())
|
||||
rtr.ServeHTTP(rw, req)
|
||||
|
||||
//nolint:bodyclose // Closed in `t.Cleanup`
|
||||
res := rw.Result()
|
||||
t.Cleanup(func() { _ = res.Body.Close() })
|
||||
require.Equal(t, http.StatusUnauthorized, res.StatusCode)
|
||||
})
|
||||
}
|
||||
|
||||
func setup(t testing.TB, db database.Store, authToken uuid.UUID, mw func(http.Handler) http.Handler) (*http.Request, http.Handler) {
|
||||
func setup(t testing.TB, db database.Store, authToken uuid.UUID, mw func(http.Handler) http.Handler) (*http.Request, http.Handler, database.Workspace, database.TemplateVersion) {
|
||||
t.Helper()
|
||||
org := dbgen.Organization(t, db, database.Organization{})
|
||||
user := dbgen.User(t, db, database.User{
|
||||
@ -107,5 +145,5 @@ func setup(t testing.TB, db database.Store, authToken uuid.UUID, mw func(http.Ha
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
return req, rtr
|
||||
return req, rtr, workspace, templateVersion
|
||||
}
|
||||
|
Reference in New Issue
Block a user