Skip to content

Commit 69a6346

Browse files
committed
authzquery: fixes to templates and parameters
- add doc comment to authorizedQueryWithRelated - handle sql.ErrNoRows in parameterRBACResource() - fix incorrect logic in GetTemplateVersionByOrganizationAndName
1 parent b08fc44 commit 69a6346

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

coderd/authzquery/authz.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ func authorizedFetchSet[ArgumentType any, ObjectType rbac.Objecter,
219219
}
220220
}
221221

222+
// authorizedQueryWithRelated performs the same function as authorizedQuery, except that
223+
// RBAC checks are performed on the result of relatedFunc() instead of the result of fetch().
224+
// This is useful for cases where ObjectType does not implement RBACObjecter.
225+
// For example, a TemplateVersion object does not implement RBACObjecter, but it is
226+
// related to a Template object, which does. Thus, any operations on a TemplateVersion
227+
// are predicated on the RBAC permissions of the related Template object.
222228
func authorizedQueryWithRelated[ObjectType any, ArgumentType any, Related rbac.Objecter](
223229
// Arguments
224230
authorizer rbac.Authorizer,

coderd/authzquery/parameters.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package authzquery
22

33
import (
44
"context"
5+
"database/sql"
6+
"errors"
57

68
"github.com/google/uuid"
79
"golang.org/x/xerrors"
@@ -20,6 +22,11 @@ func (q *AuthzQuerier) parameterRBACResource(ctx context.Context, scope database
2022
var version database.TemplateVersion
2123
version, err = q.database.GetTemplateVersionByJobID(ctx, scopeID)
2224
if err != nil {
25+
if errors.Is(err, sql.ErrNoRows) {
26+
// Template version does not exist yet, fall back to rbac.ResourceTemplate
27+
resource = rbac.ResourceTemplate
28+
err = nil
29+
}
2330
break
2431
}
2532
var template database.Template

coderd/authzquery/template.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,11 @@ func (q *AuthzQuerier) GetTemplateVersionByOrganizationAndName(ctx context.Conte
9696
// An actor can read the template version if they can read the related template in the organization.
9797
fetchRelated := func(tv database.TemplateVersion, p database.GetTemplateVersionByOrganizationAndNameParams) (rbac.Objecter, error) {
9898
if !tv.TemplateID.Valid {
99-
// If no linked template exists, check if the actor can read a template in the organization.
99+
// If no linked template exists, check if the actor can read
100+
// any template in the organization.
100101
return rbac.ResourceTemplate.InOrg(p.OrganizationID), nil
101102
}
102-
return q.database.GetTemplateByOrganizationAndName(ctx, database.GetTemplateByOrganizationAndNameParams{
103-
OrganizationID: arg.OrganizationID,
104-
Name: tv.Name,
105-
})
103+
return q.database.GetTemplateByID(ctx, tv.TemplateID.UUID)
106104
}
107105

108106
return authorizedQueryWithRelated(

0 commit comments

Comments
 (0)